Masahiro Yamada | 75035f3 | 2017-09-16 14:10:44 +0900 | [diff] [blame] | 1 | #ifndef _LINUX_BUILD_BUG_H |
| 2 | #define _LINUX_BUILD_BUG_H |
| 3 | |
| 4 | #include <linux/compiler.h> |
| 5 | |
| 6 | #ifdef __CHECKER__ |
Masahiro Yamada | 75035f3 | 2017-09-16 14:10:44 +0900 | [diff] [blame] | 7 | #define BUILD_BUG_ON_ZERO(e) (0) |
Masahiro Yamada | 75035f3 | 2017-09-16 14:10:44 +0900 | [diff] [blame] | 8 | #else /* __CHECKER__ */ |
Christophe Leroy | d7b87b7 | 2023-05-05 10:39:39 +0200 | [diff] [blame] | 9 | /* |
| 10 | * Force a compilation error if condition is true, but also produce a |
| 11 | * result (of value 0 and type int), so the expression can be used |
| 12 | * e.g. in a structure initializer (or where-ever else comma expressions |
| 13 | * aren't permitted). |
| 14 | */ |
| 15 | #define BUILD_BUG_ON_ZERO(e) ((int)sizeof(struct { int:(-!!(e)); })) |
| 16 | #endif /* __CHECKER__ */ |
Masahiro Yamada | 75035f3 | 2017-09-16 14:10:44 +0900 | [diff] [blame] | 17 | |
| 18 | /* Force a compilation error if a constant expression is not a power of 2 */ |
| 19 | #define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \ |
| 20 | BUILD_BUG_ON(((n) & ((n) - 1)) != 0) |
| 21 | #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ |
| 22 | BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) |
| 23 | |
| 24 | /* |
Masahiro Yamada | 75035f3 | 2017-09-16 14:10:44 +0900 | [diff] [blame] | 25 | * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the |
| 26 | * expression but avoids the generation of any code, even if that expression |
| 27 | * has side-effects. |
| 28 | */ |
| 29 | #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) |
| 30 | |
| 31 | /** |
| 32 | * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied |
| 33 | * error message. |
| 34 | * @condition: the condition which the compiler should know is false. |
| 35 | * |
| 36 | * See BUILD_BUG_ON for description. |
| 37 | */ |
| 38 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) |
| 39 | |
| 40 | /** |
| 41 | * BUILD_BUG_ON - break compile if a condition is true. |
| 42 | * @condition: the condition which the compiler should know is false. |
| 43 | * |
| 44 | * If you have some code which relies on certain constants being equal, or |
| 45 | * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to |
| 46 | * detect if someone changes it. |
Masahiro Yamada | 75035f3 | 2017-09-16 14:10:44 +0900 | [diff] [blame] | 47 | */ |
Masahiro Yamada | 75035f3 | 2017-09-16 14:10:44 +0900 | [diff] [blame] | 48 | #define BUILD_BUG_ON(condition) \ |
| 49 | BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) |
Masahiro Yamada | 75035f3 | 2017-09-16 14:10:44 +0900 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * BUILD_BUG - break compile if used. |
| 53 | * |
| 54 | * If you have some code that you expect the compiler to eliminate at |
| 55 | * build time, you should use BUILD_BUG to detect if it is |
| 56 | * unexpectedly used. |
| 57 | */ |
| 58 | #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") |
| 59 | |
Rasmus Villemoes | c84e08c | 2021-05-18 11:19:46 +0200 | [diff] [blame] | 60 | /** |
| 61 | * static_assert - check integer constant expression at build time |
| 62 | * |
| 63 | * static_assert() is a wrapper for the C11 _Static_assert, with a |
| 64 | * little macro magic to make the message optional (defaulting to the |
| 65 | * stringification of the tested expression). |
| 66 | * |
| 67 | * Contrary to BUILD_BUG_ON(), static_assert() can be used at global |
| 68 | * scope, but requires the expression to be an integer constant |
| 69 | * expression (i.e., it is not enough that __builtin_constant_p() is |
| 70 | * true for expr). |
| 71 | * |
| 72 | * Also note that BUILD_BUG_ON() fails the build if the condition is |
| 73 | * true, while static_assert() fails the build if the expression is |
| 74 | * false. |
| 75 | */ |
| 76 | #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr) |
| 77 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg) |
| 78 | |
Masahiro Yamada | 75035f3 | 2017-09-16 14:10:44 +0900 | [diff] [blame] | 79 | #endif /* _LINUX_BUILD_BUG_H */ |