Skip to content

Commit 35edd91

Browse files
author
Paul Gortmaker
committed
bug: consolidate BUILD_BUG_ON with other bug code
The support for BUILD_BUG in linux/kernel.h predates the addition of linux/bug.h -- with this chunk off separate, you can run into situations where a person gets a compile fail even when they've included linux/bug.h, like this: CC lib/string.o lib/string.c: In function 'strlcat': lib/string.c:225:2: error: implicit declaration of function 'BUILD_BUG_ON' make[2]: *** [lib/string.o] Error 1 $ $ grep linux/bug.h lib/string.c #include <linux/bug.h> $ Since the above violates the principle of least surprise, move the BUG chunks from kernel.h to bug.h so it is all together. Signed-off-by: Paul Gortmaker <paul.gortmaker@windriver.com>
1 parent 187f188 commit 35edd91

2 files changed

Lines changed: 61 additions & 61 deletions

File tree

‎include/linux/bug.h‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,67 @@ enum bug_trap_type {
1111

1212
struct pt_regs;
1313

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+
1475
#ifdef CONFIG_GENERIC_BUG
1576
#include <asm-generic/bug.h>
1677

‎include/linux/kernel.h‎

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -662,67 +662,6 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
662662
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
663663
(type *)( (char *)__mptr - offsetof(type,member) );})
664664

665-
#ifdef __CHECKER__
666-
#define BUILD_BUG_ON_NOT_POWER_OF_2(n)
667-
#define BUILD_BUG_ON_ZERO(e) (0)
668-
#define BUILD_BUG_ON_NULL(e) ((void*)0)
669-
#define BUILD_BUG_ON(condition)
670-
#define BUILD_BUG() (0)
671-
#else /* __CHECKER__ */
672-
673-
/* Force a compilation error if a constant expression is not a power of 2 */
674-
#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
675-
BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
676-
677-
/* Force a compilation error if condition is true, but also produce a
678-
result (of value 0 and type size_t), so the expression can be used
679-
e.g. in a structure initializer (or where-ever else comma expressions
680-
aren't permitted). */
681-
#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
682-
#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
683-
684-
/**
685-
* BUILD_BUG_ON - break compile if a condition is true.
686-
* @condition: the condition which the compiler should know is false.
687-
*
688-
* If you have some code which relies on certain constants being equal, or
689-
* other compile-time-evaluated condition, you should use BUILD_BUG_ON to
690-
* detect if someone changes it.
691-
*
692-
* The implementation uses gcc's reluctance to create a negative array, but
693-
* gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
694-
* to inline functions). So as a fallback we use the optimizer; if it can't
695-
* prove the condition is false, it will cause a link error on the undefined
696-
* "__build_bug_on_failed". This error message can be harder to track down
697-
* though, hence the two different methods.
698-
*/
699-
#ifndef __OPTIMIZE__
700-
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
701-
#else
702-
extern int __build_bug_on_failed;
703-
#define BUILD_BUG_ON(condition) \
704-
do { \
705-
((void)sizeof(char[1 - 2*!!(condition)])); \
706-
if (condition) __build_bug_on_failed = 1; \
707-
} while(0)
708-
#endif
709-
710-
/**
711-
* BUILD_BUG - break compile if used.
712-
*
713-
* If you have some code that you expect the compiler to eliminate at
714-
* build time, you should use BUILD_BUG to detect if it is
715-
* unexpectedly used.
716-
*/
717-
#define BUILD_BUG() \
718-
do { \
719-
extern void __build_bug_failed(void) \
720-
__linktime_error("BUILD_BUG failed"); \
721-
__build_bug_failed(); \
722-
} while (0)
723-
724-
#endif /* __CHECKER__ */
725-
726665
/* Trap pasters of __FUNCTION__ at compile-time */
727666
#define __FUNCTION__ (__func__)
728667

0 commit comments

Comments
 (0)