blob: aaac4bba6f5c7faa1d2b95b13983323658c8188c [file] [log] [blame]
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -08001#ifndef _LINUX_BUG_H
2#define _LINUX_BUG_H
3
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -08004#include <asm/bug.h>
5
6enum bug_trap_type {
7 BUG_TRAP_TYPE_NONE = 0,
8 BUG_TRAP_TYPE_WARN = 1,
9 BUG_TRAP_TYPE_BUG = 2,
10};
11
Heiko Carstens608e2612007-07-15 23:41:39 -070012struct pt_regs;
13
Paul Gortmaker35edd912011-11-16 23:51:05 -050014#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
Konstantin Khlebnikovbaf05aa2012-05-29 15:06:27 -070033/*
34 * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
35 * expression but avoids the generation of any code, even if that expression
36 * has side-effects.
37 */
38#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
39
Paul Gortmaker35edd912011-11-16 23:51:05 -050040/**
41 * BUILD_BUG_ON - break compile if a condition is true.
42 * @condition: the condition which the compiler should know is false.
43 *
44 * If you have some code which relies on certain constants being equal, or
45 * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
46 * detect if someone changes it.
47 *
48 * The implementation uses gcc's reluctance to create a negative array, but
49 * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
50 * to inline functions). So as a fallback we use the optimizer; if it can't
51 * prove the condition is false, it will cause a link error on the undefined
52 * "__build_bug_on_failed". This error message can be harder to track down
53 * though, hence the two different methods.
54 */
55#ifndef __OPTIMIZE__
56#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
57#else
58extern int __build_bug_on_failed;
59#define BUILD_BUG_ON(condition) \
60 do { \
61 ((void)sizeof(char[1 - 2*!!(condition)])); \
62 if (condition) __build_bug_on_failed = 1; \
63 } while(0)
64#endif
65
66/**
67 * BUILD_BUG - break compile if used.
68 *
69 * If you have some code that you expect the compiler to eliminate at
70 * build time, you should use BUILD_BUG to detect if it is
71 * unexpectedly used.
72 */
73#define BUILD_BUG() \
74 do { \
75 extern void __build_bug_failed(void) \
76 __linktime_error("BUILD_BUG failed"); \
77 __build_bug_failed(); \
78 } while (0)
79
80#endif /* __CHECKER__ */
81
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080082#ifdef CONFIG_GENERIC_BUG
83#include <asm-generic/bug.h>
84
85static inline int is_warning_bug(const struct bug_entry *bug)
86{
87 return bug->flags & BUGFLAG_WARNING;
88}
89
90const struct bug_entry *find_bug(unsigned long bugaddr);
91
Heiko Carstens608e2612007-07-15 23:41:39 -070092enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080093
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080094/* These are defined by the architecture */
95int is_valid_bugaddr(unsigned long addr);
96
97#else /* !CONFIG_GENERIC_BUG */
98
Heiko Carstens608e2612007-07-15 23:41:39 -070099static inline enum bug_trap_type report_bug(unsigned long bug_addr,
100 struct pt_regs *regs)
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -0800101{
102 return BUG_TRAP_TYPE_BUG;
103}
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -0800104
105#endif /* CONFIG_GENERIC_BUG */
106#endif /* _LINUX_BUG_H */