Don't include <cassert>. (#2148) (#2152)
* Don't include <cassert>. (#2148)
This commit replaces use of the assert() macro in format-inl.h with
FMT_ASSERT(). This allows us to drop the cassert include.
* FMT_GCC_VERSION is not defined when we include test-assert.h, use __GCC__ instead.
* Don't explicitly suppress GCC's -Wterminate in tests' FMT_ASSERT.
Throwing from a separate function is enough to silence the warning, no need to
explicitly suppress it.
* Remove messages from assertions added in 2f699d2.
* Correct formatting around throw_assertion_failure().
diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h
index be1ddf4..d4bd6af 100644
--- a/include/fmt/format-inl.h
+++ b/include/fmt/format-inl.h
@@ -9,7 +9,6 @@
#define FMT_FORMAT_INL_H_
#include <algorithm>
-#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
@@ -148,7 +147,7 @@
if (message.size() <= inline_buffer_size - error_code_size)
format_to(it, FMT_STRING("{}{}"), message, SEP);
format_to(it, FMT_STRING("{}{}"), ERROR_STR, error_code);
- assert(out.size() <= inline_buffer_size);
+ FMT_ASSERT(out.size() <= inline_buffer_size, "");
}
FMT_FUNC void report_error(format_func func, int error_code,
@@ -1225,7 +1224,7 @@
if (lower < n) ++upper;
}
void operator>>=(int shift) {
- assert(shift == 32);
+ FMT_ASSERT(shift == 32, "");
(void)shift;
lower = (upper << 32) | (lower >> 32);
upper >>= 32;
@@ -1304,7 +1303,7 @@
public:
bigint() : exp_(0) {}
explicit bigint(uint64_t n) { assign(n); }
- ~bigint() { assert(bigits_.capacity() <= bigits_capacity); }
+ ~bigint() { FMT_ASSERT(bigits_.capacity() <= bigits_capacity, ""); }
bigint(const bigint&) = delete;
void operator=(const bigint&) = delete;
@@ -1330,7 +1329,7 @@
int num_bigits() const { return static_cast<int>(bigits_.size()) + exp_; }
FMT_NOINLINE bigint& operator<<=(int shift) {
- assert(shift >= 0);
+ FMT_ASSERT(shift >= 0, "");
exp_ += shift / bigit_bits;
shift %= bigit_bits;
if (shift == 0) return *this;
@@ -1392,7 +1391,7 @@
// Assigns pow(10, exp) to this bigint.
void assign_pow10(int exp) {
- assert(exp >= 0);
+ FMT_ASSERT(exp >= 0, "");
if (exp == 0) return assign(1);
// Find the top bit.
int bitmask = 1;
@@ -2563,11 +2562,11 @@
--exp_pos;
} while (*exp_pos != 'e');
char sign = exp_pos[1];
- assert(sign == '+' || sign == '-');
+ FMT_ASSERT(sign == '+' || sign == '-', "");
int exp = 0;
auto p = exp_pos + 2; // Skip 'e' and sign.
do {
- assert(is_digit(*p));
+ FMT_ASSERT(is_digit(*p), "");
exp = exp * 10 + (*p++ - '0');
} while (p != end);
if (sign == '-') exp = -exp;
diff --git a/test/test-assert.h b/test/test-assert.h
index f613722..3406e1b 100644
--- a/test/test-assert.h
+++ b/test/test-assert.h
@@ -22,8 +22,14 @@
void assertion_failure::avoid_weak_vtable() {}
+// We use a separate function (rather than throw directly from FMT_ASSERT) to
+// avoid GCC's -Wterminate warning when FMT_ASSERT is used in a destructor.
+inline void throw_assertion_failure(const char* message) {
+ throw assertion_failure(message);
+}
+
#define FMT_ASSERT(condition, message) \
- if (!(condition)) throw assertion_failure(message);
+ if (!(condition)) throw_assertion_failure(message);
// Expects an assertion failure.
#define EXPECT_ASSERT(stmt, message) \