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