Pārlūkot izejas kodu

Add an IF_BUG_ONCE macro, since that's a pretty common pattern too.

Nick Mathewson 8 gadi atpakaļ
vecāks
revīzija
a86ed1d717
1 mainītis faili ar 33 papildinājumiem un 0 dzēšanām
  1. 33 0
      src/common/util_bug.h

+ 33 - 0
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)