Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Ian Abbott | bc6245e | 2017-07-10 15:51:07 -0700 | [diff] [blame] | 2 | #ifndef _LINUX_BUILD_BUG_H |
| 3 | #define _LINUX_BUILD_BUG_H |
| 4 | |
| 5 | #include <linux/compiler.h> |
| 6 | |
| 7 | #ifdef __CHECKER__ |
Ian Abbott | bc6245e | 2017-07-10 15:51:07 -0700 | [diff] [blame] | 8 | #define BUILD_BUG_ON_ZERO(e) (0) |
Ian Abbott | bc6245e | 2017-07-10 15:51:07 -0700 | [diff] [blame] | 9 | #else /* __CHECKER__ */ |
Ian Abbott | bc6245e | 2017-07-10 15:51:07 -0700 | [diff] [blame] | 10 | /* |
| 11 | * Force a compilation error if condition is true, but also produce a |
| 12 | * result (of value 0 and type size_t), 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) (sizeof(struct { int:(-!!(e)); })) |
Masahiro Yamada | 527edbc | 2019-01-03 15:26:23 -0800 | [diff] [blame^] | 17 | #endif /* __CHECKER__ */ |
| 18 | |
| 19 | /* Force a compilation error if a constant expression is not a power of 2 */ |
| 20 | #define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \ |
| 21 | BUILD_BUG_ON(((n) & ((n) - 1)) != 0) |
| 22 | #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ |
| 23 | BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) |
Ian Abbott | bc6245e | 2017-07-10 15:51:07 -0700 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the |
| 27 | * expression but avoids the generation of any code, even if that expression |
| 28 | * has side-effects. |
| 29 | */ |
| 30 | #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) |
| 31 | |
| 32 | /** |
| 33 | * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied |
| 34 | * error message. |
| 35 | * @condition: the condition which the compiler should know is false. |
| 36 | * |
| 37 | * See BUILD_BUG_ON for description. |
| 38 | */ |
| 39 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) |
| 40 | |
| 41 | /** |
| 42 | * BUILD_BUG_ON - break compile if a condition is true. |
| 43 | * @condition: the condition which the compiler should know is false. |
| 44 | * |
| 45 | * If you have some code which relies on certain constants being equal, or |
| 46 | * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to |
| 47 | * detect if someone changes it. |
Ian Abbott | bc6245e | 2017-07-10 15:51:07 -0700 | [diff] [blame] | 48 | */ |
Ian Abbott | bc6245e | 2017-07-10 15:51:07 -0700 | [diff] [blame] | 49 | #define BUILD_BUG_ON(condition) \ |
| 50 | BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) |
Ian Abbott | bc6245e | 2017-07-10 15:51:07 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * BUILD_BUG - break compile if used. |
| 54 | * |
| 55 | * If you have some code that you expect the compiler to eliminate at |
| 56 | * build time, you should use BUILD_BUG to detect if it is |
| 57 | * unexpectedly used. |
| 58 | */ |
| 59 | #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") |
| 60 | |
Ian Abbott | bc6245e | 2017-07-10 15:51:07 -0700 | [diff] [blame] | 61 | #endif /* _LINUX_BUILD_BUG_H */ |