]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: debug: make the BUG_ON() macros check the condition in the outer one
authorWilly Tarreau <w@1wt.eu>
Mon, 21 Oct 2024 16:17:25 +0000 (18:17 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 21 Oct 2024 16:17:25 +0000 (18:17 +0200)
The BUG_ON() macros are made of two levels so as to resolve the condition
to a string. However this doesn't offer much flexibility for performing
other operations when the condition is validated, so let's adjust them so
that the condition is checked in the outer macro and the operations are
performed in the inner one.

include/haproxy/bug.h

index 5e683974fe1f17aceebc3f45f94b6135217ec990..8e75f1f90a9ed4acf0ddb56ced879058d1f5eb46 100644 (file)
@@ -167,11 +167,13 @@ static __attribute__((noinline,noreturn,unused)) void abort_with_line(uint line)
  * on a second line after the condition message, to give a bit more context
  * about the problem.
  */
-#define _BUG_ON(cond, file, line, crash, pfx, sfx, ...)                 \
-       __BUG_ON(cond, file, line, crash, pfx, sfx, __VA_ARGS__)
+#define _BUG_ON(cond, file, line, crash, pfx, sfx, ...)                                \
+       (void)(unlikely(cond) ? ({                                              \
+               __BUG_ON(cond, file, line, crash, pfx, sfx, __VA_ARGS__);       \
+               1; /* let's return the true condition */                        \
+       }) : 0)
 
-#define __BUG_ON(cond, file, line, crash, pfx, sfx, ...)                \
-       (void)(unlikely(cond) ? ({                                      \
+#define __BUG_ON(cond, file, line, crash, pfx, sfx, ...) do {          \
                const char *msg;                                        \
                if (sizeof("" __VA_ARGS__) > 1)                         \
                        msg ="\n" pfx "condition \"" #cond "\" matched at " file ":" #line "" sfx "\n" __VA_ARGS__ "\n"; \
@@ -182,8 +184,7 @@ static __attribute__((noinline,noreturn,unused)) void abort_with_line(uint line)
                        ABORT_NOW();                                    \
                else                                                    \
                        DUMP_TRACE();                                   \
-               1; /* let's return the true condition */                \
-       }) : 0)
+       } while (0)
 
 /* This one is equivalent except that it only emits the message once by
  * maintaining a static counter. This may be used with warnings to detect
@@ -191,10 +192,12 @@ static __attribute__((noinline,noreturn,unused)) void abort_with_line(uint line)
  * possible to verify these counters.
  */
 #define _BUG_ON_ONCE(cond, file, line, crash, pfx, sfx, ...)                   \
-       __BUG_ON_ONCE(cond, file, line, crash, pfx, sfx, __VA_ARGS__)
+       (void)(unlikely(cond) ? ({                                              \
+               __BUG_ON_ONCE(cond, file, line, crash, pfx, sfx, __VA_ARGS__);  \
+               1; /* let's return the true condition */                        \
+       }) : 0)
 
-#define __BUG_ON_ONCE(cond, file, line, crash, pfx, sfx, ...)                \
-       (void)(unlikely(cond) ? ({                                      \
+#define __BUG_ON_ONCE(cond, file, line, crash, pfx, sfx, ...) do {     \
                static int __match_count_##line;                        \
                const char *msg;                                        \
                if (sizeof("" __VA_ARGS__) > 1)                         \
@@ -206,8 +209,8 @@ static __attribute__((noinline,noreturn,unused)) void abort_with_line(uint line)
                        ABORT_NOW();                                    \
                else                                                    \
                        DUMP_TRACE();                                   \
-               1; /* let's return the true condition */                \
-       }) : 0)
+       } while (0)
+
 
 /* DEBUG_STRICT enables/disables runtime checks on condition <cond>
  * DEBUG_STRICT_ACTION indicates the level of verification on the rules when