|
| 1 | +/* There is no include guard. Just like <assert.h>, we can include this header |
| 2 | + * multiple times to update the macros for NDEBUG/C_MORE_ASSERT changes. |
| 3 | + * |
| 4 | + * The user can define NDEBUG to disable all asserts. |
| 5 | + * |
| 6 | + * The user can define C_MORE_ASSERT to a non-negative number to control |
| 7 | + * which assertions are enabled. |
| 8 | + */ |
| 9 | + |
| 10 | +#include <assert.h> |
| 11 | +#include <c-stdaux-generic.h> |
| 12 | + |
| 13 | +/** |
| 14 | + * C_MORE_ASSERT: user define to configure assertion levels (similar to NDEBUG). |
| 15 | + * |
| 16 | + * If NDEBUG is defined, then assert() is a nop. This also implies |
| 17 | + * C_MORE_ASSERT_LEVEL of zero, which means that c_more_assert() does not |
| 18 | + * evaluate the condition at runtime. |
| 19 | + * |
| 20 | + * Otherwise, if C_MORE_ASSERT is defined it determines the |
| 21 | + * C_MORE_ASSERT_LEVEL. If C_MORE_ASSERT, is undefined, C_MORE_ASSERT_LEVEL |
| 22 | + * defaults to 1. |
| 23 | + * |
| 24 | + * The effective C_MORE_ASSERT_LEVEL affects whether c_more_assert() and |
| 25 | + * c_more_assert_with() evaluates the condition at runtime. The purpose is that |
| 26 | + * more assertions are disabled by default (and in release builds). For |
| 27 | + * debugging and testing, define C_MORE_ASSERT to a number larger than 1. |
| 28 | + */ |
| 29 | +#undef C_MORE_ASSERT_LEVEL |
| 30 | +#ifdef NDEBUG |
| 31 | +#define C_MORE_ASSERT_LEVEL 0 |
| 32 | +#elif !defined(C_MORE_ASSERT) |
| 33 | +#define C_MORE_ASSERT_LEVEL 1 |
| 34 | +#else |
| 35 | +#define C_MORE_ASSERT_LEVEL (C_MORE_ASSERT) |
| 36 | +#endif |
| 37 | + |
| 38 | +#undef _c_assert_fail |
| 39 | +#if C_MORE_ASSERT_LEVEL > 0 && defined(__GNU_LIBRARY__) |
| 40 | +/* Depending on "_with_msg", we hide the "msg" string unless we build with |
| 41 | + * "C_MORE_ASSERT > 1". The point is to avoid embedding debugging strings in |
| 42 | + * the binary with release builds. |
| 43 | + * |
| 44 | + * The assertion failure messages are often not very useful for the end user |
| 45 | + * and for the developer __FILE__:__LINE__ is sufficient. |
| 46 | + * |
| 47 | + * __assert_fail() also exists on musl, but we don't have a separate detection |
| 48 | + * for musl. |
| 49 | + */ |
| 50 | +#define _c_assert_fail(_with_msg, msg) \ |
| 51 | + __assert_fail( \ |
| 52 | + C_MORE_ASSERT_LEVEL > 1 || (_with_msg) ? "" msg "" : "<dropped>", \ |
| 53 | + __FILE__, __LINE__, \ |
| 54 | + C_MORE_ASSERT_LEVEL > 1 || (_with_msg) ? "<unknown-fcn>" : __func__) |
| 55 | +#else |
| 56 | +#define _c_assert_fail(_with_msg, msg) \ |
| 57 | + do { \ |
| 58 | + assert(false && msg); \ |
| 59 | + _c_unreachable_code(); \ |
| 60 | + } while (0) |
| 61 | +#endif |
| 62 | + |
| 63 | +/* There is an include guard. The remainder of this header is only evaluated |
| 64 | + * once upon multiple inclusions. */ |
| 65 | +#if !defined(C_HAS_STDAUX_ASSERT) |
| 66 | +#define C_HAS_STDAUX_ASSERT |
| 67 | + |
| 68 | +#if defined(C_COMPILER_GNUC) |
| 69 | +#define _c_unreachable_code() __builtin_unreachable() |
| 70 | +#else /* defined(C_COMPILER_GNUC) */ |
| 71 | +#define _c_unreachable_code() \ |
| 72 | + do { \ |
| 73 | + /* Infinite loop without side effects is undefined behavior and marks \ |
| 74 | + * unreachable code. */ \ |
| 75 | + } while (1) |
| 76 | +#endif /* defined(C_COMPILER_GNUC) */ |
| 77 | + |
| 78 | +#if defined(C_COMPILER_GNUC) |
| 79 | +#define _c_assert_constant(_cond) \ |
| 80 | + do { \ |
| 81 | + if (__builtin_constant_p(_cond) && !(_cond)) { \ |
| 82 | + /* With gcc, constant expressions are still evaluated and result \ |
| 83 | + * in unreachable code too. \ |
| 84 | + * \ |
| 85 | + * The point is to avoid compiler warnings with \ |
| 86 | + * c_more_assert(false) and NDEBUG. \ |
| 87 | + */ \ |
| 88 | + _c_unreachable_code(); \ |
| 89 | + } \ |
| 90 | + } while (0) |
| 91 | +#else /* defined(C_COMPILER_GNUC) */ |
| 92 | +#define _c_assert_constant(_cond) \ |
| 93 | + do { \ |
| 94 | + /* This does nothing. */ \ |
| 95 | + } while (0) |
| 96 | +#endif /* defined(C_COMPILER_GNUC) */ |
| 97 | + |
| 98 | +/** |
| 99 | + * c_more_assert_with() - Conditional runtime assertion. |
| 100 | + * @_level: Assertion level that determines whether the assertion is evaluated, |
| 101 | + * based on comparison with C_MORE_ASSERT_LEVEL. |
| 102 | + * @_cond: Condition or expression to validate. |
| 103 | + * |
| 104 | + * This macro performs an assertion based on the specified _level in comparison |
| 105 | + * to the compile-time constant C_MORE_ASSERT_LEVEL. C_MORE_ASSERT_LEVEL |
| 106 | + * typically defaults to 1 but can be modified by defining NDEBUG or |
| 107 | + * C_MORE_ASSERT. |
| 108 | + * |
| 109 | + * - If _level is less than C_MORE_ASSERT_LEVEL, the condition is ignored and |
| 110 | + * the assertion code is excluded from the final build, allowing for performance |
| 111 | + * optimizations. |
| 112 | + * |
| 113 | + * - If _cond is a constant expression that fails, the compiler will mark the code |
| 114 | + * path as unreachable, regardless of NDEBUG or the configured C_MORE_ASSERT_LEVEL. |
| 115 | + * |
| 116 | + * Unlike c_assert(), which always evaluates the condition, |
| 117 | + * `c_more_assert_with()` * only evaluates the condition if the specified _level * |
| 118 | + * meets the configured assertion threshold. This conditional behavior requires * |
| 119 | + * that _cond has no side effects, as it may not be evaluated in all cases. |
| 120 | + * |
| 121 | + * Note: This macro is usually excluded from regular builds unless explicitly |
| 122 | + * enabled by defining C_MORE_ASSERT, making it particularly useful for debugging |
| 123 | + * and testing without incurring runtime costs in production builds. |
| 124 | + * |
| 125 | + * The macro is async-signal-safe, if @_cond is and the assertion doesn't fail. |
| 126 | + */ |
| 127 | +#define c_more_assert_with(_level, _cond) \ |
| 128 | + do { \ |
| 129 | + /* c_more_assert_with() must do *nothing* of effect, \ |
| 130 | + * except evaluating @_cond (0 or 1 times). \ |
| 131 | + * \ |
| 132 | + * As such, it is async-signal-safe (provided @_cond and \ |
| 133 | + * @_level is, and the assertion does not fail). */ \ |
| 134 | + if ((_level) < C_MORE_ASSERT_LEVEL) { \ |
| 135 | + _c_assert_constant(_cond); \ |
| 136 | + } else if (_c_likely_(_cond)) { \ |
| 137 | + /* pass */ \ |
| 138 | + } else { \ |
| 139 | + _c_assert_fail(false, #_cond); \ |
| 140 | + } \ |
| 141 | + } while (0) |
| 142 | + |
| 143 | +/** |
| 144 | + * c_more_assert() - Conditional runtime assertion. |
| 145 | + * @_cond: Condition or expression to validate. |
| 146 | + * |
| 147 | + * This is the same as c_more_assert_with(2, _cond). This means that |
| 148 | + * the assertion is usually disabled in regular builds unless the user |
| 149 | + * opts in by setting C_MORE_ASSERT to 2 or larger. |
| 150 | + * |
| 151 | + * The macro is async-signal-safe, if @_cond is and the assertion doesn't fail. |
| 152 | + */ |
| 153 | +#define c_more_assert(_cond) c_more_assert_with(2, _cond) |
| 154 | + |
| 155 | +/** |
| 156 | + * c_assert() - Runtime assertions |
| 157 | + * @_cond: Result of an expression |
| 158 | + * |
| 159 | + * This function behaves like the standard ``assert(3)`` macro. That is, if |
| 160 | + * ``NDEBUG`` is defined, it is a no-op. In all other cases it will assert that |
| 161 | + * the result of the passed expression is true. |
| 162 | + * |
| 163 | + * Unlike the standard ``assert(3)`` macro, this function always evaluates its |
| 164 | + * argument. This means side-effects will always be evaluated! However, if the |
| 165 | + * macro is used with constant expressions, the compiler will be able to |
| 166 | + * optimize it away. |
| 167 | + * |
| 168 | + * The macro is async-signal-safe, if @_cond is and the assertion doesn't fail. |
| 169 | + */ |
| 170 | +#define c_assert(_cond) \ |
| 171 | + do { \ |
| 172 | + if (!_c_likely_(_cond)) { \ |
| 173 | + _c_assert_fail(false, #_cond); \ |
| 174 | + } \ |
| 175 | + } while (0) |
| 176 | + |
| 177 | +/** |
| 178 | + * c_assert_not_reached() - Fail assertion when called. |
| 179 | + * |
| 180 | + * With C_COMPILER_GNUC, the macro calls assert(false) and marks the code |
| 181 | + * path as __builtin_unreachable(). The benefit is that also with NDEBUG the |
| 182 | + * compiler considers the path unreachable. |
| 183 | + * |
| 184 | + * Otherwise, just calls assert(false). |
| 185 | + * |
| 186 | + * The macro is async-signal-safe. |
| 187 | + */ |
| 188 | +#define c_assert_not_reached() _c_assert_fail(true, "unreachable") |
| 189 | + |
| 190 | +#endif /* !defined(C_HAS_STDAUX_ASSERT) */ |
| 191 | + |
| 192 | +#ifdef __cplusplus |
| 193 | +} |
| 194 | +#endif |
0 commit comments