aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/bug.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/linux/bug.h')
-rw-r--r--include/linux/bug.h61
1 files changed, 61 insertions, 0 deletions
diff --git a/include/linux/bug.h b/include/linux/bug.h
index d276b5510c83..72961c39576a 100644
--- a/include/linux/bug.h
+++ b/include/linux/bug.h
@@ -11,6 +11,67 @@ enum bug_trap_type {
11 11
12struct pt_regs; 12struct pt_regs;
13 13
14#ifdef __CHECKER__
15#define BUILD_BUG_ON_NOT_POWER_OF_2(n)
16#define BUILD_BUG_ON_ZERO(e) (0)
17#define BUILD_BUG_ON_NULL(e) ((void*)0)
18#define BUILD_BUG_ON(condition)
19#define BUILD_BUG() (0)
20#else /* __CHECKER__ */
21
22/* Force a compilation error if a constant expression is not a power of 2 */
23#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
24 BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
25
26/* Force a compilation error if condition is true, but also produce a
27 result (of value 0 and type size_t), so the expression can be used
28 e.g. in a structure initializer (or where-ever else comma expressions
29 aren't permitted). */
30#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
31#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(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 * 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
42 * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
43 * to inline functions). So as a fallback we use the optimizer; if it can't
44 * prove the condition is false, it will cause a link error on the undefined
45 * "__build_bug_on_failed". This error message can be harder to track down
46 * though, hence the two different methods.
47 */
48#ifndef __OPTIMIZE__
49#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
50#else
51extern int __build_bug_on_failed;
52#define BUILD_BUG_ON(condition) \
53 do { \
54 ((void)sizeof(char[1 - 2*!!(condition)])); \
55 if (condition) __build_bug_on_failed = 1; \
56 } while(0)
57#endif
58
59/**
60 * BUILD_BUG - break compile if used.
61 *
62 * If you have some code that you expect the compiler to eliminate at
63 * build time, you should use BUILD_BUG to detect if it is
64 * unexpectedly used.
65 */
66#define BUILD_BUG() \
67 do { \
68 extern void __build_bug_failed(void) \
69 __linktime_error("BUILD_BUG failed"); \
70 __build_bug_failed(); \
71 } while (0)
72
73#endif /* __CHECKER__ */
74
14#ifdef CONFIG_GENERIC_BUG 75#ifdef CONFIG_GENERIC_BUG
15#include <asm-generic/bug.h> 76#include <asm-generic/bug.h>
16 77