diff options
author | Stephan Diestelhorst <stephan.diestelhorst@arm.com> | 2014-03-07 15:56:23 -0500 |
---|---|---|
committer | Stephan Diestelhorst <stephan.diestelhorst@arm.com> | 2014-03-07 15:56:23 -0500 |
commit | 45677ffa97e44816881b9ec636d11798eb100fcb (patch) | |
tree | 1efa017279b6c5acc5b1abd87d03d2d0ab66830c /src/base | |
parent | b9a9d99b226768dc972f0c40488f332066396e69 (diff) | |
download | gem5-45677ffa97e44816881b9ec636d11798eb100fcb.tar.xz |
misc: Add panic_if / fatal_if / chatty_assert
This snippet can be used to replace if + {panics, fatals, asserts} constructs.
The idea is to have both the condition checking and a verbose printout in a single statement. The interface is as follows:
panic_if(foo != bar, "These should be equal: foo %i bar %i", foo, bar);
fatal_if(foo != bar, "These should be equal: foo %i bar %i", foo, bar);
chatty_assert(foo == bar, "These should be equal: foo %i bar %i", foo, bar);
Diffstat (limited to 'src/base')
-rw-r--r-- | src/base/misc.hh | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/base/misc.hh b/src/base/misc.hh index e09f2d0c2..82fe8c4c0 100644 --- a/src/base/misc.hh +++ b/src/base/misc.hh @@ -1,4 +1,16 @@ /* + * Copyright (c) 2014 ARM Limited + * All rights reserved + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * * Copyright (c) 2002-2005 The Regents of The University of Michigan * All rights reserved. * @@ -82,6 +94,37 @@ M5_PRAGMA_NORETURN(__exit_message) // #define fatal(...) exit_message("fatal", -1, __VA_ARGS__) +/** + * Conditional panic macro that checks the supplied condition and only panics + * if the condition is true and allows the programmer to specify diagnostic + * printout. Useful to replace if + panic, or if + print + assert, etc. + * + * @param cond Condition that is checked; if true -> panic + * @param ... Printf-based format string with arguments, extends printout. + */ +#define panic_if(cond, ...) \ + do { \ + if ((cond)) \ + exit_message("panic condition "#cond" occurred", -1, __VA_ARGS__); \ + } while (0) + + +/** + * Conditional fatal macro that checks the supplied condition and only causes a + * fatal error if the condition is true and allows the programmer to specify + * diagnostic printout. Useful to replace if + fatal, or if + print + assert, + * etc. + * + * @param cond Condition that is checked; if true -> fatal + * @param ... Printf-based format string with arguments, extends printout. + */ +#define fatal_if(cond, ...) \ + do { \ + if ((cond)) \ + exit_message("fatal condition "#cond" occurred", 1, __VA_ARGS__); \ + } while (0) + + void __base_message(std::ostream &stream, const char *prefix, bool verbose, const char *func, const char *file, int line, @@ -146,4 +189,24 @@ extern bool want_hack, hack_verbose; #define hack_once(...) \ cond_message_once(want_hack, std::cerr, "hack", hack_verbose, __VA_ARGS__) +/** + * 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. Like the normal assertion, the chatty_assert + * will not be active in fast builds. + * + * @param cond Condition that is checked; if false -> assert + * @param ... Printf-based format string with arguments, extends printout. + */ +#ifdef NDEBUG +#define chatty_assert(cond, ...) +#else //!NDEBUG +#define chatty_assert(cond, ...) \ + do { \ + if (!(cond)) { \ + base_message(std::cerr, "assert("#cond") failing", 1, __VA_ARGS__); \ + assert(cond); \ + } \ + } while (0) +#endif // NDEBUG #endif // __BASE_MISC_HH__ |