From 55bb9ebe7bdc74b94fa9a0eebc33f0ee26e3d92f Mon Sep 17 00:00:00 2001 From: Gabriel Busnot Date: Fri, 30 Apr 2021 17:13:35 +0200 Subject: [PATCH] base: Define the gem5_assert macro gem5_assert is a drop-in replacement for regular assert, except that the condition must always be valid, compilable code. It allows to perform clean-up before exiting using the exit method of ExitLogger. The need for clean-up is detailed in the following issue: https://gem5.atlassian.net/browse/GEM5-968 Change-Id: Icad1719c0e6fbb066471d1fecfb12eedd65aa690 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/45027 Reviewed-by: Daniel Carvalho Reviewed-by: Jason Lowe-Power Maintainer: Daniel Carvalho Tested-by: kokoro --- src/base/logging.hh | 20 +++++++++++++++++++- src/base/logging.test.cc | 12 ++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/base/logging.hh b/src/base/logging.hh index f00fa201a9..f2d28228fd 100644 --- a/src/base/logging.hh +++ b/src/base/logging.hh @@ -309,6 +309,24 @@ class Logger } while (0) /** @} */ // end of api_logger -} // namespace gem5 +/** + * 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. + * + * @param cond Condition that is checked; if false -> panic + * + * \def gem5_assert(cond) + * + * @ingroup api_logger + */ +#define gem5_assert(cond) \ + do { \ + if (GEM5_UNLIKELY(!NDEBUG_DEFINED && !static_cast(cond))) \ + panic("assert(" # cond ") failed"); \ + } while (0) +/** @} */ // end of api_logger +} // namespace gem5 #endif // __BASE_LOGGING_HH__ diff --git a/src/base/logging.test.cc b/src/base/logging.test.cc index 31b363d6ec..da8068232e 100644 --- a/src/base/logging.test.cc +++ b/src/base/logging.test.cc @@ -554,3 +554,15 @@ TEST(LoggingDeathTest, ChattyAssert) ASSERT_DEATH(chatty_assert(false, "message\n"), ::testing::HasSubstr( "panic: assert(false) failed: message\nMemory Usage:")); } + +/** Test macro gem5_assert. */ +TEST(LoggingDeathTest, gem5Assert) +{ +#ifdef NDEBUG + GTEST_SKIP() << "Skipping as assertions are " + "stripped out of fast builds"; +#endif + gem5_assert(true); + ASSERT_DEATH(gem5_assert(false), ::testing::HasSubstr( + "panic: assert(false) failed\nMemory Usage:")); +}