Gabe Black has submitted this change. ( 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, 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 <oda...@yahoo.com.br>
Reviewed-by: Jason Lowe-Power <power...@gmail.com>
Maintainer: Daniel Carvalho <oda...@yahoo.com.br>
Tested-by: kokoro <noreply+kok...@google.com>
---
M src/base/logging.hh
M src/base/logging.test.cc
2 files changed, 31 insertions(+), 1 deletion(-)

Approvals:
  Jason Lowe-Power: Looks good to me, approved
  Daniel Carvalho: Looks good to me, approved; Looks good to me, approved
  kokoro: Regressions pass



diff --git a/src/base/logging.hh b/src/base/logging.hh
index f00fa20..f2d2822 100644
--- a/src/base/logging.hh
+++ b/src/base/logging.hh
@@ -309,6 +309,24 @@
     } 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__
diff --git a/src/base/logging.test.cc b/src/base/logging.test.cc
index 31b363d..da80682 100644
--- a/src/base/logging.test.cc
+++ b/src/base/logging.test.cc
@@ -554,3 +554,15 @@
     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:"));
+}

--
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: 6
Gerrit-Owner: Gabriel Busnot <garbage2collec...@gmail.com>
Gerrit-Reviewer: Bobby R. Bruce <bbr...@ucdavis.edu>
Gerrit-Reviewer: Daniel Carvalho <oda...@yahoo.com.br>
Gerrit-Reviewer: Gabe Black <gabe.bl...@gmail.com>
Gerrit-Reviewer: Jason Lowe-Power <power...@gmail.com>
Gerrit-Reviewer: kokoro <noreply+kok...@google.com>
Gerrit-CC: Gabe Black <gabebl...@google.com>
Gerrit-MessageType: merged
_______________________________________________
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