blob: 89fb91d0c929d4cc74c8c4971d6f0f67fdd7651d [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__
Daniel Santosca623c92013-02-21 16:41:44 -080015#define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0)
Paul Gortmaker35edd912011-11-16 23:51:05 -050016#define BUILD_BUG_ON_ZERO(e) (0)
17#define BUILD_BUG_ON_NULL(e) ((void*)0)
Tushar Beherac5782e92012-11-26 16:29:38 -080018#define BUILD_BUG_ON_INVALID(e) (0)
Daniel Santosca623c92013-02-21 16:41:44 -080019#define BUILD_BUG_ON(condition) (0)
Paul Gortmaker35edd912011-11-16 23:51:05 -050020#define BUILD_BUG() (0)
21#else /* __CHECKER__ */
22
23/* Force a compilation error if a constant expression is not a power of 2 */
24#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
25 BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
26
27/* Force a compilation error if condition is true, but also produce a
28 result (of value 0 and type size_t), so the expression can be used
29 e.g. in a structure initializer (or where-ever else comma expressions
30 aren't permitted). */
31#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
32#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
33
Konstantin Khlebnikovbaf05aa2012-05-29 15:06:27 -070034/*
35 * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the
36 * expression but avoids the generation of any code, even if that expression
37 * has side-effects.
38 */
39#define BUILD_BUG_ON_INVALID(e) ((void)(sizeof((__force long)(e))))
40
Paul Gortmaker35edd912011-11-16 23:51:05 -050041/**
42 * BUILD_BUG_ON - break compile if a condition is true.
43 * @condition: the condition which the compiler should know is false.
44 *
45 * 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
47 * detect if someone changes it.
48 *
49 * The implementation uses gcc's reluctance to create a negative array, but
50 * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
51 * to inline functions). So as a fallback we use the optimizer; if it can't
52 * prove the condition is false, it will cause a link error on the undefined
53 * "__build_bug_on_failed". This error message can be harder to track down
54 * though, hence the two different methods.
55 */
56#ifndef __OPTIMIZE__
57#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
58#else
59extern int __build_bug_on_failed;
60#define BUILD_BUG_ON(condition) \
61 do { \
Daniel Santos1d6a0d12013-02-21 16:41:45 -080062 bool __cond = !!(condition); \
63 ((void)sizeof(char[1 - 2 * __cond])); \
64 if (__cond) __build_bug_on_failed = 1; \
65 } while (0)
Paul Gortmaker35edd912011-11-16 23:51:05 -050066#endif
67
68/**
69 * BUILD_BUG - break compile if used.
70 *
71 * If you have some code that you expect the compiler to eliminate at
72 * build time, you should use BUILD_BUG to detect if it is
73 * unexpectedly used.
74 */
75#define BUILD_BUG() \
76 do { \
77 extern void __build_bug_failed(void) \
Daniel Santos6ae8d042013-02-21 16:41:42 -080078 __compiletime_error("BUILD_BUG failed");\
Paul Gortmaker35edd912011-11-16 23:51:05 -050079 __build_bug_failed(); \
80 } while (0)
81
82#endif /* __CHECKER__ */
83
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080084#ifdef CONFIG_GENERIC_BUG
85#include <asm-generic/bug.h>
86
87static inline int is_warning_bug(const struct bug_entry *bug)
88{
89 return bug->flags & BUGFLAG_WARNING;
90}
91
92const struct bug_entry *find_bug(unsigned long bugaddr);
93
Heiko Carstens608e2612007-07-15 23:41:39 -070094enum bug_trap_type report_bug(unsigned long bug_addr, struct pt_regs *regs);
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080095
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -080096/* These are defined by the architecture */
97int is_valid_bugaddr(unsigned long addr);
98
99#else /* !CONFIG_GENERIC_BUG */
100
Heiko Carstens608e2612007-07-15 23:41:39 -0700101static inline enum bug_trap_type report_bug(unsigned long bug_addr,
102 struct pt_regs *regs)
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -0800103{
104 return BUG_TRAP_TYPE_BUG;
105}
Jeremy Fitzhardinge7664c5a2006-12-08 02:36:19 -0800106
107#endif /* CONFIG_GENERIC_BUG */
108#endif /* _LINUX_BUG_H */