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.h47
1 files changed, 26 insertions, 21 deletions
diff --git a/include/linux/bug.h b/include/linux/bug.h
index b1cf40de847e..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
6enum bug_trap_type { 7enum bug_trap_type {
7 BUG_TRAP_TYPE_NONE = 0, 8 BUG_TRAP_TYPE_NONE = 0,
@@ -12,11 +13,12 @@ enum bug_trap_type {
12struct pt_regs; 13struct 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_INVALID(e) (0) 19#define BUILD_BUG_ON_INVALID(e) (0)
19#define BUILD_BUG_ON(condition) 20#define BUILD_BUG_ON_MSG(cond, msg) (0)
21#define BUILD_BUG_ON(condition) (0)
20#define BUILD_BUG() (0) 22#define BUILD_BUG() (0)
21#else /* __CHECKER__ */ 23#else /* __CHECKER__ */
22 24
@@ -39,29 +41,37 @@ struct pt_regs;
39#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e)))) 41#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
40 42
41/** 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/**
42 * BUILD_BUG_ON - break compile if a condition is true. 53 * BUILD_BUG_ON - break compile if a condition is true.
43 * @condition: the condition which the compiler should know is false. 54 * @condition: the condition which the compiler should know is false.
44 * 55 *
45 * 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
46 * 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
47 * detect if someone changes it. 58 * detect if someone changes it.
48 * 59 *
49 * 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
50 * 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
51 * 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
52 * 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
53 * "__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
54 * 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.
55 */ 69 */
56#ifndef __OPTIMIZE__ 70#ifndef __OPTIMIZE__
57#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 71#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
58#else 72#else
59extern int __build_bug_on_failed; 73#define BUILD_BUG_ON(condition) \
60#define BUILD_BUG_ON(condition) \ 74 BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
61 do { \
62 ((void)sizeof(char[1 - 2*!!(condition)])); \
63 if (condition) __build_bug_on_failed = 1; \
64 } while(0)
65#endif 75#endif
66 76
67/** 77/**
@@ -71,12 +81,7 @@ extern int __build_bug_on_failed;
71 * 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
72 * unexpectedly used. 82 * unexpectedly used.
73 */ 83 */
74#define BUILD_BUG() \ 84#define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed")
75 do { \
76 extern void __build_bug_failed(void) \
77 __linktime_error("BUILD_BUG failed"); \
78 __build_bug_failed(); \
79 } while (0)
80 85
81#endif /* __CHECKER__ */ 86#endif /* __CHECKER__ */
82 87