Gabe Black has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/48606 )

Change subject: base: Extend gem5_assert to subsume chatty_assert.
......................................................................

base: Extend gem5_assert to subsume chatty_assert.

If given more than just its condition, gem5_assert will assume it
should act like chatty_assert.

Because we have our own custom assert now anyway, we can fold the
behavior of both into one macro and make life easier for users.

Deprecate chatty_assert.

Change-Id: I43497b5333802a265c0ad096681f64ab6f0424b1
---
M src/base/logging.hh
M src/base/logging.test.cc
2 files changed, 32 insertions(+), 33 deletions(-)



diff --git a/src/base/logging.hh b/src/base/logging.hh
index 758a9cb..88e1109 100644
--- a/src/base/logging.hh
+++ b/src/base/logging.hh
@@ -43,6 +43,7 @@

 #include <cassert>
 #include <sstream>
+#include <tuple>
 #include <utility>

 #include "base/compiler.hh"
@@ -288,26 +289,20 @@
 #define NDEBUG_DEFINED 0
 #endif

-/**
- * The chatty assert macro will function like a normal assert, but will allow - * the specification of additional, helpful material to aid debugging why the
- * assertion actually failed. chatty_assert will not actually check its
- * condition for fast builds, but the condition must still be valid code.
- *
- * @param cond Condition that is checked; if false -> assert
- * @param ...  Printf-based format string with arguments, extends printout.
- *
- * \def chatty_assert(cond, ...)
- *
- * @ingroup api_logger
- */
-#define chatty_assert(cond, ...) \
-    do { \
-        if (GEM5_UNLIKELY(!NDEBUG_DEFINED && !static_cast<bool>(cond))) \
-            panic("assert(" # cond ") failed: %s", \
-                    ::gem5::csprintf(__VA_ARGS__)); \
-    } while (0)
-/** @} */ // end of api_logger
+template <typename ...Args>
+inline const std::string
+gem5AssertMessage(const std::string &cond, const std::tuple<Args...> &args)
+{
+    return cond + ": " + std::apply<std::string(*)(Args...)>(
+            &::gem5::csprintf, args);
+}
+
+template <>
+inline const std::string
+gem5AssertMessage<>(const std::string &cond, const std::tuple<> &args)
+{
+    return cond;
+}

 /**
  * The assert macro will function like a normal assert, but will use panic
@@ -316,17 +311,26 @@
  * condition in fast builds, but it must still be valid code.
  *
  * @param cond Condition that is checked; if false -> panic
+ * @param ...  Printf-based format string with arguments, extends printout.
  *
- * \def gem5_assert(cond)
+ * \def gem5_assert(cond, ...)
  *
  * @ingroup api_logger
  */
-#define gem5_assert(cond) \
+#define gem5_assert(cond, ...) \
     do { \
-        if (GEM5_UNLIKELY(!NDEBUG_DEFINED && !static_cast<bool>(cond))) \
-            panic("assert(" # cond ") failed"); \
+        if (GEM5_UNLIKELY(!NDEBUG_DEFINED && !static_cast<bool>(cond))) { \
+            panic(::gem5::gem5AssertMessage("assert(" #cond ") failed", \
+                        std::make_tuple(__VA_ARGS__))); \
+        } \
     } while (0)
 /** @} */ // end of api_logger

+#define chatty_assert(...) \
+    do { \
+        gem5_assert(__VA_ARGS__); \
+ GEM5_DEPRECATED_MACRO(chatty_assert, {}, "Please use gem5_assert()"); \
+    } while(0)
+
 } // namespace gem5
 #endif // __BASE_LOGGING_HH__
diff --git a/src/base/logging.test.cc b/src/base/logging.test.cc
index 0cbe3d0..fb201b6 100644
--- a/src/base/logging.test.cc
+++ b/src/base/logging.test.cc
@@ -543,21 +543,16 @@
         "fatal: fatal condition true occurred: message\nMemory Usage:"));
 }

-/** Test macro chatty_assert. */
-TEST(LoggingDeathTest, ChattyAssert)
+/** Test macro gem5_assert. */
+TEST(LoggingDeathTest, Gem5Assert)
 {
 #ifdef NDEBUG
     GTEST_SKIP() << "Skipping as assertions are "
         "stripped out of fast builds";
 #endif
-    chatty_assert(true, "message\n");
-    ASSERT_DEATH(chatty_assert(false, "message\n"), ::testing::HasSubstr(
+    gem5_assert(true, "message\n");
+    ASSERT_DEATH(gem5_assert(false, "message\n"), ::testing::HasSubstr(
         "panic: assert(false) failed: message\nMemory Usage:"));
-}
-
-/** Test macro gem5_assert. */
-TEST(LoggingDeathTest, GEM5Assert)
-{
     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/+/48606
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: I43497b5333802a265c0ad096681f64ab6f0424b1
Gerrit-Change-Number: 48606
Gerrit-PatchSet: 1
Gerrit-Owner: Gabe Black <gabe.bl...@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