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