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