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 <odanrc@yahoo.com.br>
Reviewed-by: Jason Lowe-Power <power.jg@gmail.com>
Maintainer: Daniel Carvalho <odanrc@yahoo.com.br>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Gabriel Busnot
2021-04-30 17:13:35 +02:00
committed by Gabe Black
parent d54dd3bb52
commit 55bb9ebe7b
2 changed files with 31 additions and 1 deletions

View File

@@ -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<bool>(cond))) \
panic("assert(" # cond ") failed"); \
} while (0)
/** @} */ // end of api_logger
} // namespace gem5
#endif // __BASE_LOGGING_HH__

View File

@@ -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:"));
}