diff options
author | Nick Mathewson <nickm@torproject.org> | 2016-04-12 10:35:16 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2016-04-14 16:25:07 -0400 |
commit | a86ed1d717482f52e0b0ab390151401c28079d87 (patch) | |
tree | d51e9d892a9aace4d25fc1c0dc936012ad48f291 | |
parent | 532820b11c9d0566b45b3dd19f01cf3e16d984ef (diff) | |
download | tor-a86ed1d717482f52e0b0ab390151401c28079d87.tar.gz tor-a86ed1d717482f52e0b0ab390151401c28079d87.zip |
Add an IF_BUG_ONCE macro, since that's a pretty common pattern too.
-rw-r--r-- | src/common/util_bug.h | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/common/util_bug.h b/src/common/util_bug.h index a5f78f2cd8..26134502c6 100644 --- a/src/common/util_bug.h +++ b/src/common/util_bug.h @@ -102,6 +102,39 @@ : 0) #endif +#ifdef __GNUC__ +#define IF_BUG_ONCE__(cond,var) \ + if (({ \ + static int var = 0; \ + int bool_result = (cond); \ + if (bool_result && !var) { \ + var = 1; \ + tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1); \ + } \ + var; })) +#else +#define IF_BUG_ONCE__(cond,var) \ + static int var = 0; \ + if ((cond) ? \ + (var ? 1 : \ + (var=1, \ + tor_bug_occurred_(SHORT_FILE__, __LINE__, __func__, #cond, 1), \ + 1)) \ + : 0) +#endif +#define IF_BUG_ONCE_VARNAME_(a) \ + warning_logged_on_ ## a ## __ +#define IF_BUG_ONCE_VARNAME__(a) \ + IF_BUG_ONCE_VARNAME_(a) + +/** This macro behaves as 'if (bug(x))', except that it only logs its + * warning once, no matter how many times it triggers. + */ + +#define IF_BUG_ONCE(cond) \ + IF_BUG_ONCE__((cond), \ + IF_BUG_ONCE_VARNAME__(__LINE__)) + /** Define this if you want Tor to crash when any problem comes up, * so you can get a coredump and track things down. */ // #define tor_fragile_assert() tor_assert_unreached(0) |