| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef _LINUX_BUILD_BUG_H |
| 3 | #define _LINUX_BUILD_BUG_H |
| 4 | |
| 5 | #include <linux/compiler.h> |
| 6 | |
| 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 | #define BUILD_BUG_ON_ZERO(e) (0) |
| 11 | #define BUILD_BUG_ON_NULL(e) ((void *)0) |
| 12 | #define BUILD_BUG_ON_INVALID(e) (0) |
| 13 | #define BUILD_BUG_ON_MSG(cond, msg) (0) |
| 14 | #define BUILD_BUG_ON(condition) (0) |
| 15 | #define BUILD_BUG() (0) |
| 16 | #else /* __CHECKER__ */ |
| 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 | /* |
| 25 | * Force a compilation error if condition is true, but also produce a |
| 26 | * result (of value 0 and type size_t), so the expression can be used |
| 27 | * e.g. in a structure initializer (or where-ever else comma expressions |
| 28 | * aren't permitted). |
| 29 | */ |
| 30 | #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); })) |
| 31 | #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:(-!!(e)); })) |
| 32 | |
| 33 | /* |
| 34 | * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the |
| 35 | * expression but avoids the generation of any code, even if that expression |
| 36 | * has side-effects. |
| 37 | */ |
| 38 | #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) |
| 39 | |
| 40 | /** |
| 41 | * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied |
| 42 | * error message. |
| 43 | * @condition: the condition which the compiler should know is false. |
| 44 | * |
| 45 | * See BUILD_BUG_ON for description. |
| 46 | */ |
| 47 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) |
| 48 | |
| 49 | /** |
| 50 | * BUILD_BUG_ON - break compile if a condition is true. |
| 51 | * @condition: the condition which the compiler should know is false. |
| 52 | * |
| 53 | * If you have some code which relies on certain constants being equal, or |
| 54 | * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to |
| 55 | * detect if someone changes it. |
| 56 | * |
| 57 | * The implementation uses gcc's reluctance to create a negative array, but gcc |
| 58 | * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to |
| 59 | * inline functions). Luckily, in 4.3 they added the "error" function |
| 60 | * attribute just for this type of case. Thus, we use a negative sized array |
| 61 | * (should always create an error on gcc versions older than 4.4) and then call |
| 62 | * an undefined function with the error attribute (should always create an |
| 63 | * error on gcc 4.3 and later). If for some reason, neither creates a |
| 64 | * compile-time error, we'll still have a link-time error, which is harder to |
| 65 | * track down. |
| 66 | */ |
| 67 | #ifndef __OPTIMIZE__ |
| 68 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) |
| 69 | #else |
| 70 | #define BUILD_BUG_ON(condition) \ |
| 71 | BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) |
| 72 | #endif |
| 73 | |
| 74 | /** |
| 75 | * BUILD_BUG - break compile if used. |
| 76 | * |
| 77 | * If you have some code that you expect the compiler to eliminate at |
| 78 | * build time, you should use BUILD_BUG to detect if it is |
| 79 | * unexpectedly used. |
| 80 | */ |
| 81 | #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") |
| 82 | |
| 83 | #endif /* __CHECKER__ */ |
| 84 | |
| 85 | #endif /* _LINUX_BUILD_BUG_H */ |