bug.h revamp, easier to use

This commit is contained in:
2024-07-15 08:18:35 +02:00
parent a48ebf9099
commit 180839c960

View File

@@ -11,20 +11,7 @@
* *
*/ */
/* we allow multiple inclusions, so that BUG_ON/WARN_ON can change during #ifndef BUG_H
* compilation.
*/
#ifdef BUG_H
#undef BUG_H
/* reset all macros */
#undef bug_on
#undef warn_on
#undef warn
#endif /* BUG_H */
#define BUG_H #define BUG_H
#include <stdio.h> #include <stdio.h>
@@ -32,44 +19,43 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdarg.h> #include <stdarg.h>
#ifndef BUG_ON #define bug_on_always(expr) do { \
#define BUG_ON 0 if (expr) { \
#endif fprintf(stderr, \
#ifndef WARN_ON "** BUG ON %s[%s:%d]: assertion '%s' failed.\n", \
#define WARN_ON 0 __func__, __FILE__,__LINE__, #expr); \
#endif abort(); \
/* not reached */ \
#include "likely.h" } \
#if (BUG_ON + 0 == 1)
#define bug_on(expr) do { \
if (unlikely(expr)) { \
fprintf(stderr, \
"** BUG IN %s[%s:%d]: assertion \"" #expr "\" failed.\n", \
__func__, __FILE__,__LINE__); \
abort(); \
} \
} while (0) } while (0)
#define warn(expr, args...) ({ \
int _ret = !!(expr); \
if (_ret) \
fprintf(stderr, ##args); \
_ret; \
})
#define warn_on_always(expr) ({ \
warn(expr, \
"** WARN ON %s[%s:%d]: assertion '%s' failed.\n", \
__func__, __FILE__,__LINE__, #expr); \
})
#ifdef BUG_ON
#define bug_on(expr) bug_on_always(expr)
#define warn_on(expr) warn_on_always(expr)
#define warn_on_or_eval(expr) warn_on(expr)
#else #else
#define bug_on(expr) #define bug_on(expr)
#endif
#if (WARN_ON + 0 == 1) #define warn_on(expr)
#define warn_on(expr) ({ \ #define warn_on_or_eval(expr) (expr)
int _ret = !!(expr); \
if (unlikely(_ret)) \
fprintf(stderr, \
"** WARN ON %s[%s:%d]: assertion \"" #expr "\" failed.\n", \
__func__, __FILE__,__LINE__); \
_ret; \
})
#else
#define warn_on(expr) ({ 0; })
#endif
#define warn(expr, args...) ({ \ #endif /* BUG_ON */
int _ret = !!(expr); \
if (unlikely(_ret)) \ #endif /* BUG_H */
fprintf(stderr, ##args); \
_ret; \
})