diff --git a/src/base/logging.hh b/src/base/logging.hh index 8949b0cced..22fd2a84d2 100644 --- a/src/base/logging.hh +++ b/src/base/logging.hh @@ -43,7 +43,6 @@ #include #include -#include #include #include "base/compiler.hh" @@ -289,24 +288,10 @@ class Logger #define NDEBUG_DEFINED 0 #endif -template -inline std::string -_assertMsg(const std::string &format, Args... args) -{ - return std::string(": ") + csprintf(format, args...); -} - -inline const char * -_assertMsg() -{ - return ""; -} - /** * The assert macro will function like a normal assert, but will use panic * instead of straight abort(). This allows to perform some cleaning up in - * ExitLogger::exit() before calling abort(). This macro will not check its - * condition in fast builds, but it must still be valid code. + * ExitLogger::exit() before calling abort(). * * @param cond Condition that is checked; if false -> panic * @param ... Printf-based format string with arguments, extends printout. @@ -315,11 +300,17 @@ _assertMsg() * * @ingroup api_logger */ -#define gem5_assert(cond, ...) \ - do { \ - if (GEM5_UNLIKELY(!NDEBUG_DEFINED && !static_cast(cond))) { \ - panic("assert(" #cond ") failed%s", _assertMsg(__VA_ARGS__)); \ - } \ +#define gem5_assert(cond, ...) \ + do { \ + GEM5_UNLIKELY(NDEBUG_DEFINED || static_cast(cond)) ? \ + void(0) : \ + [](const auto&... args) { \ + auto msg = [&]{ \ + if constexpr (sizeof...(args) == 0) return ""; \ + else return std::string(": ") + csprintf(args...); \ + }; \ + panic("assert(" #cond ") failed%s", msg()); \ + }(__VA_ARGS__); \ } while (0) /** @} */ // end of api_logger diff --git a/src/base/logging.test.cc b/src/base/logging.test.cc index 38cc6059db..5d10f6e33a 100644 --- a/src/base/logging.test.cc +++ b/src/base/logging.test.cc @@ -553,6 +553,9 @@ TEST(LoggingDeathTest, gem5Assert) gem5_assert(true, "message\n"); ASSERT_DEATH(gem5_assert(false, "message\n"), ::testing::HasSubstr( "panic: assert(false) failed: message\nMemory Usage:")); + ASSERT_DEATH(gem5_assert(false, "%s, %s!\n", "Hello", "World"), + ::testing::HasSubstr( + "panic: assert(false) failed: Hello, World!\nMemory Usage:")); gem5_assert(true); ASSERT_DEATH(gem5_assert(false), ::testing::HasSubstr( "panic: assert(false) failed\nMemory Usage:"));