]> git.kaiwu.me - haproxy.git/commitdiff
DEBUG: add BUG_ON_STATIC(): a compile-time BUG_ON()
authorMaxime Henrion <mhenrion@haproxy.com>
Thu, 16 Jul 2026 16:18:17 +0000 (12:18 -0400)
committerOlivier Houchard <cognet@ci0.org>
Thu, 16 Jul 2026 16:48:05 +0000 (18:48 +0200)
Fails the build when <cond>, a constant expression, is true. Maps to
_Static_assert() on C11+; older dialects declare a negatively-sized
extern array instead, which emits no storage nor symbol. Usable at
file and block scope.

include/haproxy/bug.h

index 32db432fbc0d8e0ed6f208fbe3b7bc0b720d7d7d..bb1fc0147d6a5e5dd0b40dfb115f60b43030b161 100644 (file)
@@ -439,6 +439,15 @@ extern __attribute__((__weak__)) struct debug_count __stop_dbg_cnt  HA_SECTION_S
 # define BUG_ON_STRESS(cond, ...)  do { } while (0)
 #endif
 
+/* BUG_ON_STATIC(cond): fail the build if <cond> is true. */
+#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
+#define BUG_ON_STATIC(cond) \
+       _Static_assert(!(cond), "bug condition \"" #cond "\" matched")
+#else
+#define BUG_ON_STATIC(cond) \
+       extern char CONCAT(bug_on_static_, __LINE__)[(cond) ? -1 : 1] __maybe_unused
+#endif
+
 /* When not optimizing, clang won't remove that code, so only compile it in when optimizing */
 #if defined(__GNUC__) && defined(__OPTIMIZE__)
 #define HA_LINK_ERROR(what)                                                  \