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