Skip to content

Commit 95aeea7

Browse files
Add fmt::assert_fail as a weak symbol
That way one can provide ones own implementation for fmt::assert_fail. Keeping the fmt::detail::assert_fail for binary compatibility, which now just calls fmt::assert_fail.
1 parent 72c8229 commit 95aeea7

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

include/fmt/base.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,16 @@ FMT_PRAGMA_CLANG(diagnostic push)
270270
# define FMT_WIN32 0
271271
#endif
272272

273+
#if (FMT_GCC_VERSION || FMT_CLANG_VERSION) && !defined(FMT_HEADER_ONLY) && \
274+
!FMT_WIN32
275+
// Header only (which implies everything is inline) and weak symbols do not work
276+
// together. And on Windows weak symbols seem not to work properly.
277+
# define FMT_WEAK __attribute__((weak))
278+
#endif
279+
#ifndef FMT_WEAK
280+
# define FMT_WEAK
281+
#endif
282+
273283
#if !defined(FMT_HEADER_ONLY) && FMT_WIN32
274284
# if defined(FMT_LIB_EXPORT)
275285
# define FMT_API __declspec(dllexport)
@@ -348,6 +358,11 @@ template <typename T> constexpr auto max_of(T a, T b) -> T {
348358
return a > b ? a : b;
349359
}
350360

361+
FMT_BEGIN_EXPORT
362+
FMT_NORETURN FMT_API FMT_WEAK void assert_fail(const char* file, int line,
363+
const char* message);
364+
FMT_END_EXPORT
365+
351366
namespace detail {
352367
// Suppresses "unused variable" warnings with the method described in
353368
// https://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/.
@@ -388,7 +403,7 @@ FMT_NORETURN FMT_API void assert_fail(const char* file, int line,
388403
# define FMT_ASSERT(condition, message) \
389404
((condition) /* void() fails with -Winvalid-constexpr on clang 4.0.1 */ \
390405
? (void)0 \
391-
: fmt::detail::assert_fail(__FILE__, __LINE__, (message)))
406+
: ::fmt::assert_fail(__FILE__, __LINE__, (message)))
392407
#endif
393408

394409
#ifdef FMT_USE_INT128

include/fmt/format-inl.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,22 @@
3131
#endif
3232

3333
FMT_BEGIN_NAMESPACE
34-
namespace detail {
3534

36-
FMT_FUNC void assert_fail(const char* file, int line, const char* message) {
35+
FMT_FUNC FMT_WEAK void assert_fail(const char* file, int line,
36+
const char* message) {
3737
// Use unchecked std::fprintf to avoid triggering another assertion when
3838
// writing to stderr fails.
3939
fprintf(stderr, "%s:%d: assertion failed: %s", file, line, message);
4040
abort();
4141
}
4242

43+
namespace detail {
44+
45+
// For binary compatibility.
46+
FMT_FUNC void assert_fail(const char* file, int line, const char* message) {
47+
::fmt::assert_fail(file, line, message);
48+
}
49+
4350
FMT_FUNC void format_error_code(detail::buffer<char>& out, int error_code,
4451
string_view message) noexcept {
4552
// Report error code making sure that the output fits into

include/fmt/format.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ FMT_END_NAMESPACE
183183
# define FMT_THROW(x) throw x
184184
# endif
185185
# else
186-
# define FMT_THROW(x) \
187-
::fmt::detail::assert_fail(__FILE__, __LINE__, (x).what())
186+
# define FMT_THROW(x) ::fmt::assert_fail(__FILE__, __LINE__, (x).what())
188187
# endif // FMT_USE_EXCEPTIONS
189188
#endif // FMT_THROW
190189

0 commit comments

Comments
 (0)