Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 1 | #ifndef _LINUX_BUG_H |
| 2 | #define _LINUX_BUG_H |
| 3 | |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 4 | #include <asm/bug.h> |
| 5 | |
| 6 | enum bug_trap_type { |
| 7 | BUG_TRAP_TYPE_NONE = 0, |
| 8 | BUG_TRAP_TYPE_WARN = 1, |
| 9 | BUG_TRAP_TYPE_BUG = 2, |
| 10 | }; |
| 11 | |
Heiko Carstens | 608e261 | 2007-07-15 23:41:39 -0700 | [diff] [blame] | 12 | struct pt_regs; |
| 13 | |
Paul Gortmaker | 35edd91 | 2011-11-16 23:51:05 -0500 | [diff] [blame] | 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 |
| 51 | extern 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 | |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 75 | #ifdef CONFIG_GENERIC_BUG |
| 76 | #include <asm-generic/bug.h> |
| 77 | |
| 78 | static inline int is_warning_bug(const struct bug_entry *bug) |
| 79 | { |
| 80 | return bug->flags & BUGFLAG_WARNING; |
| 81 | } |
| 82 | |
| 83 | const struct bug_entry *find_bug(unsigned long bugaddr); |
| 84 | |
Heiko Carstens | 608e261 | 2007-07-15 23:41:39 -0700 | [diff] [blame] | 85 | enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs); |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 86 | |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 87 | /* These are defined by the architecture */ |
| 88 | int is_valid_bugaddr(unsigned long addr); |
| 89 | |
| 90 | #else /* !CONFIG_GENERIC_BUG */ |
| 91 | |
Heiko Carstens | 608e261 | 2007-07-15 23:41:39 -0700 | [diff] [blame] | 92 | static inline enum bug_trap_type report_bug(unsigned long bug_addr, |
| 93 | struct pt_regs *regs) |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 94 | { |
| 95 | return BUG_TRAP_TYPE_BUG; |
| 96 | } |
Jeremy Fitzhardinge | 7664c5a | 2006-12-08 02:36:19 -0800 | [diff] [blame] | 97 | |
| 98 | #endif /* CONFIG_GENERIC_BUG */ |
| 99 | #endif /* _LINUX_BUG_H */ |