.. | .. |
---|
5 | 5 | #include <linux/compiler.h> |
---|
6 | 6 | |
---|
7 | 7 | #ifdef __CHECKER__ |
---|
8 | | -#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) |
---|
9 | | -#define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) |
---|
10 | 8 | #define BUILD_BUG_ON_ZERO(e) (0) |
---|
11 | | -#define BUILD_BUG_ON_INVALID(e) (0) |
---|
12 | | -#define BUILD_BUG_ON_MSG(cond, msg) (0) |
---|
13 | | -#define BUILD_BUG_ON(condition) (0) |
---|
14 | | -#define BUILD_BUG() (0) |
---|
15 | 9 | #else /* __CHECKER__ */ |
---|
| 10 | +/* |
---|
| 11 | + * Force a compilation error if condition is true, but also produce a |
---|
| 12 | + * result (of value 0 and type int), so the expression can be used |
---|
| 13 | + * e.g. in a structure initializer (or where-ever else comma expressions |
---|
| 14 | + * aren't permitted). |
---|
| 15 | + */ |
---|
| 16 | +#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); }))) |
---|
| 17 | +#endif /* __CHECKER__ */ |
---|
16 | 18 | |
---|
17 | 19 | /* Force a compilation error if a constant expression is not a power of 2 */ |
---|
18 | 20 | #define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \ |
---|
19 | 21 | BUILD_BUG_ON(((n) & ((n) - 1)) != 0) |
---|
20 | 22 | #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ |
---|
21 | 23 | BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) |
---|
22 | | - |
---|
23 | | -/* |
---|
24 | | - * Force a compilation error if condition is true, but also produce a |
---|
25 | | - * result (of value 0 and type size_t), so the expression can be used |
---|
26 | | - * e.g. in a structure initializer (or where-ever else comma expressions |
---|
27 | | - * aren't permitted). |
---|
28 | | - */ |
---|
29 | | -#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); })) |
---|
30 | 24 | |
---|
31 | 25 | /* |
---|
32 | 26 | * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the |
---|
.. | .. |
---|
51 | 45 | * If you have some code which relies on certain constants being equal, or |
---|
52 | 46 | * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to |
---|
53 | 47 | * detect if someone changes it. |
---|
54 | | - * |
---|
55 | | - * The implementation uses gcc's reluctance to create a negative array, but gcc |
---|
56 | | - * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to |
---|
57 | | - * inline functions). Luckily, in 4.3 they added the "error" function |
---|
58 | | - * attribute just for this type of case. Thus, we use a negative sized array |
---|
59 | | - * (should always create an error on gcc versions older than 4.4) and then call |
---|
60 | | - * an undefined function with the error attribute (should always create an |
---|
61 | | - * error on gcc 4.3 and later). If for some reason, neither creates a |
---|
62 | | - * compile-time error, we'll still have a link-time error, which is harder to |
---|
63 | | - * track down. |
---|
64 | 48 | */ |
---|
65 | | -#ifndef __OPTIMIZE__ |
---|
66 | | -#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) |
---|
67 | | -#else |
---|
68 | 49 | #define BUILD_BUG_ON(condition) \ |
---|
69 | 50 | BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) |
---|
70 | | -#endif |
---|
71 | 51 | |
---|
72 | 52 | /** |
---|
73 | 53 | * BUILD_BUG - break compile if used. |
---|
.. | .. |
---|
78 | 58 | */ |
---|
79 | 59 | #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") |
---|
80 | 60 | |
---|
81 | | -#endif /* __CHECKER__ */ |
---|
| 61 | +/** |
---|
| 62 | + * static_assert - check integer constant expression at build time |
---|
| 63 | + * |
---|
| 64 | + * static_assert() is a wrapper for the C11 _Static_assert, with a |
---|
| 65 | + * little macro magic to make the message optional (defaulting to the |
---|
| 66 | + * stringification of the tested expression). |
---|
| 67 | + * |
---|
| 68 | + * Contrary to BUILD_BUG_ON(), static_assert() can be used at global |
---|
| 69 | + * scope, but requires the expression to be an integer constant |
---|
| 70 | + * expression (i.e., it is not enough that __builtin_constant_p() is |
---|
| 71 | + * true for expr). |
---|
| 72 | + * |
---|
| 73 | + * Also note that BUILD_BUG_ON() fails the build if the condition is |
---|
| 74 | + * true, while static_assert() fails the build if the expression is |
---|
| 75 | + * false. |
---|
| 76 | + */ |
---|
| 77 | +#define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr) |
---|
| 78 | +#define __static_assert(expr, msg, ...) _Static_assert(expr, msg) |
---|
82 | 79 | |
---|
83 | 80 | #ifdef __GENKSYMS__ |
---|
84 | 81 | /* genksyms gets confused by _Static_assert */ |
---|