Gabriel Busnot has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/45027 )

Change subject: base: Define the gem5_assert macro
......................................................................

base: Define the gem5_assert macro

gem5_assert is a drop-in replacement for regular assert.

Change-Id: Icad1719c0e6fbb066471d1fecfb12eedd65aa690
---
M src/base/logging.hh
M src/base/logging.test.cc
2 files changed, 36 insertions(+), 0 deletions(-)



diff --git a/src/base/logging.hh b/src/base/logging.hh
index 650aecd..20d7ecb 100644
--- a/src/base/logging.hh
+++ b/src/base/logging.hh
@@ -299,4 +299,27 @@
     } while (0)
 #endif // NDEBUG
 /** @} */ // end of api_logger
+
+/**
+ * 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 is not active in fast
+ * builds.
+ *
+ * @param cond Condition that is checked; if false -> panic
+ *
+ * \def gem5_assert(cond)
+ *
+ * @ingroup api_logger
+ */
+#ifdef NDEBUG
+// Fallback to regular gem5_assert behavior (normally, do nothing)
+#define gem5_assert(cond) assert(cond)
+#else //!NDEBUG
+#define gem5_assert(cond)                                             \
+    (static_cast<bool>(cond) ?                                      \
+        void (0) :                                                  \
+        []{panic(std::string("(") + #cond + ") failed");}())
+#endif // NDEBUG
+/** @} */ // end of api_logger
 #endif // __BASE_LOGGING_HH__
diff --git a/src/base/logging.test.cc b/src/base/logging.test.cc
index 3f16070..13b1ee6 100644
--- a/src/base/logging.test.cc
+++ b/src/base/logging.test.cc
@@ -552,3 +552,16 @@
     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(true), ::testing::HasSubstr(
+        "panic: (false) failed: message\nMemory Usage:"));
+}
+

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/45027
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: Icad1719c0e6fbb066471d1fecfb12eedd65aa690
Gerrit-Change-Number: 45027
Gerrit-PatchSet: 1
Gerrit-Owner: Gabriel Busnot <garbage2collec...@gmail.com>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to