Add FMT_CUSTOM_ASSERT_FAIL (#4505)

That way one can provide ones own implementation for assert_fail, which
is moved out of the detail namespace. For binary compatibility the
detail version stays to call the outer version.
This commit is contained in:
Björn Schäpers
2025-08-25 18:02:00 +02:00
committed by GitHub
parent 43c88d00ae
commit e181e94140
3 changed files with 14 additions and 4 deletions

View File

@@ -348,6 +348,9 @@ template <typename T> constexpr auto max_of(T a, T b) -> T {
return a > b ? a : b;
}
FMT_NORETURN FMT_API void assert_fail(const char* file, int line,
const char* message);
namespace detail {
// Suppresses "unused variable" warnings with the method described in
// https://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/.
@@ -388,7 +391,7 @@ FMT_NORETURN FMT_API void assert_fail(const char* file, int line,
# define FMT_ASSERT(condition, message) \
((condition) /* void() fails with -Winvalid-constexpr on clang 4.0.1 */ \
? (void)0 \
: fmt::detail::assert_fail(__FILE__, __LINE__, (message)))
: ::fmt::assert_fail(__FILE__, __LINE__, (message)))
#endif
#ifdef FMT_USE_INT128

View File

@@ -31,14 +31,22 @@
#endif
FMT_BEGIN_NAMESPACE
namespace detail {
#ifndef FMT_CUSTOM_ASSERT_FAIL
FMT_FUNC void assert_fail(const char* file, int line, const char* message) {
// Use unchecked std::fprintf to avoid triggering another assertion when
// writing to stderr fails.
fprintf(stderr, "%s:%d: assertion failed: %s", file, line, message);
abort();
}
#endif
namespace detail {
// For binary compatibility.
FMT_FUNC void assert_fail(const char* file, int line, const char* message) {
::fmt::assert_fail(file, line, message);
}
FMT_FUNC void format_error_code(detail::buffer<char>& out, int error_code,
string_view message) noexcept {

View File

@@ -170,8 +170,7 @@ template <typename T> struct iterator_traits<fmt::basic_appender<T>> {
#elif FMT_USE_EXCEPTIONS
# define FMT_THROW(x) throw x
#else
# define FMT_THROW(x) \
::fmt::detail::assert_fail(__FILE__, __LINE__, (x).what())
# define FMT_THROW(x) ::fmt::assert_fail(__FILE__, __LINE__, (x).what())
#endif
// Defining FMT_REDUCE_INT_INSTANTIATIONS to 1, will reduce the number of