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)
committerChristopher Faulet <cfaulet@haproxy.com>
Thu, 9 Jan 2025 09:28:05 +0000 (10:28 +0100)
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.

(cherry picked from commit 8844ed2009563d5976c42b6b2b7eb8f097344f25)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>

include/haproxy/bug.h

index 7d66f55..1103d6c 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