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