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