]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
DEBUG: make the _BUG_ON() macro return the condition
authorWilly Tarreau <w@1wt.eu>
Fri, 25 Feb 2022 08:10:26 +0000 (09:10 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 25 Feb 2022 10:55:47 +0000 (11:55 +0100)
By doing so it now becomes an expression and will allow for example
to use WARN_ON() in tests, for example:

    if (WARN_ON(cond))
       return NULL;

include/haproxy/bug.h

index cfd7c9f2ad47d97b344abcaf13b3e2e0fe5bec12..9a760c73328c01d9ff7f90bbbdd431d3083dd929 100644 (file)
 
 /* This is the generic low-level macro dealing with conditional warnings and
  * bugs. The caller decides whether to crash or not and what prefix and suffix
- * to pass.
+ * to pass. The macro returns the boolean value of the condition as an int for
+ * the case where it wouldn't die.
  */
 #define _BUG_ON(cond, file, line, crash, pfx, sfx)                     \
        __BUG_ON(cond, file, line, crash, pfx, sfx)
 
 #define __BUG_ON(cond, file, line, crash, pfx, sfx)                     \
-       do {                                                            \
-               if (unlikely(cond)) {                                   \
+       ({                                                              \
+               int __bug_cond = !!(cond);                              \
+               if (unlikely(__bug_cond)) {                             \
                        const char msg[] = "\n" pfx "condition \"" #cond "\" matched at " file ":" #line "" sfx "\n"; \
                        DISGUISE(write(2, msg, __builtin_strlen(msg))); \
                        if (crash)                                      \
@@ -67,7 +69,8 @@
                        else                                            \
                                DUMP_TRACE();                           \
                }                                                       \
-       } while (0)
+               __bug_cond; /* let's return the condition */            \
+       })
 
 /* BUG_ON: complains if <cond> is true when DEBUG_STRICT or DEBUG_STRICT_NOCRASH
  * are set, does nothing otherwise. With DEBUG_STRICT in addition it immediately