From: Willy Tarreau Date: Mon, 21 Oct 2024 16:17:25 +0000 (+0200) Subject: CLEANUP: debug: make the BUG_ON() macros check the condition in the outer one X-Git-Tag: v3.1-dev11~89 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8844ed2009563d5976c42b6b2b7eb8f097344f25;p=thirdparty%2Fhaproxy.git CLEANUP: debug: make the BUG_ON() macros check the condition in the outer one 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. --- diff --git a/include/haproxy/bug.h b/include/haproxy/bug.h index 5e683974fe..8e75f1f90a 100644 --- a/include/haproxy/bug.h +++ b/include/haproxy/bug.h @@ -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 * DEBUG_STRICT_ACTION indicates the level of verification on the rules when