diff options
Diffstat (limited to 'include/linux/bug.h')
| -rw-r--r-- | include/linux/bug.h | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/include/linux/bug.h b/include/linux/bug.h index aaac4bba6f5c..7f4818673c41 100644 --- a/include/linux/bug.h +++ b/include/linux/bug.h | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | #define _LINUX_BUG_H | 2 | #define _LINUX_BUG_H |
| 3 | 3 | ||
| 4 | #include <asm/bug.h> | 4 | #include <asm/bug.h> |
| 5 | #include <linux/compiler.h> | ||
| 5 | 6 | ||
| 6 | enum bug_trap_type { | 7 | enum bug_trap_type { |
| 7 | BUG_TRAP_TYPE_NONE = 0, | 8 | BUG_TRAP_TYPE_NONE = 0, |
| @@ -12,10 +13,12 @@ enum bug_trap_type { | |||
| 12 | struct pt_regs; | 13 | struct pt_regs; |
| 13 | 14 | ||
| 14 | #ifdef __CHECKER__ | 15 | #ifdef __CHECKER__ |
| 15 | #define BUILD_BUG_ON_NOT_POWER_OF_2(n) | 16 | #define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) |
| 16 | #define BUILD_BUG_ON_ZERO(e) (0) | 17 | #define BUILD_BUG_ON_ZERO(e) (0) |
| 17 | #define BUILD_BUG_ON_NULL(e) ((void*)0) | 18 | #define BUILD_BUG_ON_NULL(e) ((void*)0) |
| 18 | #define BUILD_BUG_ON(condition) | 19 | #define BUILD_BUG_ON_INVALID(e) (0) |
| 20 | #define BUILD_BUG_ON_MSG(cond, msg) (0) | ||
| 21 | #define BUILD_BUG_ON(condition) (0) | ||
| 19 | #define BUILD_BUG() (0) | 22 | #define BUILD_BUG() (0) |
| 20 | #else /* __CHECKER__ */ | 23 | #else /* __CHECKER__ */ |
| 21 | 24 | ||
| @@ -38,29 +41,37 @@ struct pt_regs; | |||
| 38 | #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) | 41 | #define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) |
| 39 | 42 | ||
| 40 | /** | 43 | /** |
| 44 | * BUILD_BUG_ON_MSG - break compile if a condition is true & emit supplied | ||
| 45 | * error message. | ||
| 46 | * @condition: the condition which the compiler should know is false. | ||
| 47 | * | ||
| 48 | * See BUILD_BUG_ON for description. | ||
| 49 | */ | ||
| 50 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg) | ||
| 51 | |||
| 52 | /** | ||
| 41 | * BUILD_BUG_ON - break compile if a condition is true. | 53 | * BUILD_BUG_ON - break compile if a condition is true. |
| 42 | * @condition: the condition which the compiler should know is false. | 54 | * @condition: the condition which the compiler should know is false. |
| 43 | * | 55 | * |
| 44 | * If you have some code which relies on certain constants being equal, or | 56 | * 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 | 57 | * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to |
| 46 | * detect if someone changes it. | 58 | * detect if someone changes it. |
| 47 | * | 59 | * |
| 48 | * The implementation uses gcc's reluctance to create a negative array, but | 60 | * The implementation uses gcc's reluctance to create a negative array, but gcc |
| 49 | * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments | 61 | * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to |
| 50 | * to inline functions). So as a fallback we use the optimizer; if it can't | 62 | * inline functions). Luckily, in 4.3 they added the "error" function |
| 51 | * prove the condition is false, it will cause a link error on the undefined | 63 | * attribute just for this type of case. Thus, we use a negative sized array |
| 52 | * "__build_bug_on_failed". This error message can be harder to track down | 64 | * (should always create an error on gcc versions older than 4.4) and then call |
| 53 | * though, hence the two different methods. | 65 | * an undefined function with the error attribute (should always create an |
| 66 | * error on gcc 4.3 and later). If for some reason, neither creates a | ||
| 67 | * compile-time error, we'll still have a link-time error, which is harder to | ||
| 68 | * track down. | ||
| 54 | */ | 69 | */ |
| 55 | #ifndef __OPTIMIZE__ | 70 | #ifndef __OPTIMIZE__ |
| 56 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) | 71 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) |
| 57 | #else | 72 | #else |
| 58 | extern int __build_bug_on_failed; | 73 | #define BUILD_BUG_ON(condition) \ |
| 59 | #define BUILD_BUG_ON(condition) \ | 74 | BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) |
| 60 | do { \ | ||
| 61 | ((void)sizeof(char[1 - 2*!!(condition)])); \ | ||
| 62 | if (condition) __build_bug_on_failed = 1; \ | ||
| 63 | } while(0) | ||
| 64 | #endif | 75 | #endif |
| 65 | 76 | ||
| 66 | /** | 77 | /** |
| @@ -70,12 +81,7 @@ extern int __build_bug_on_failed; | |||
| 70 | * build time, you should use BUILD_BUG to detect if it is | 81 | * build time, you should use BUILD_BUG to detect if it is |
| 71 | * unexpectedly used. | 82 | * unexpectedly used. |
| 72 | */ | 83 | */ |
| 73 | #define BUILD_BUG() \ | 84 | #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") |
| 74 | do { \ | ||
| 75 | extern void __build_bug_failed(void) \ | ||
| 76 | __linktime_error("BUILD_BUG failed"); \ | ||
| 77 | __build_bug_failed(); \ | ||
| 78 | } while (0) | ||
| 79 | 85 | ||
| 80 | #endif /* __CHECKER__ */ | 86 | #endif /* __CHECKER__ */ |
| 81 | 87 | ||
