From e181e941409a350a93aaea92187f9ba9071a5d09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= Date: Mon, 25 Aug 2025 18:02:00 +0200 Subject: [PATCH] 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. --- include/fmt/base.h | 5 ++++- include/fmt/format-inl.h | 10 +++++++++- include/fmt/format.h | 3 +-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index e739f4b7..dde9b241 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -348,6 +348,9 @@ template 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 diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 9861d29e..3b317af8 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -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& out, int error_code, string_view message) noexcept { diff --git a/include/fmt/format.h b/include/fmt/format.h index 161df54a..e3b85feb 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -170,8 +170,7 @@ template struct iterator_traits> { #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