aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/kernel.h
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2011-01-24 15:45:10 -0500
committerRusty Russell <rusty@rustcorp.com.au>2011-01-23 23:15:10 -0500
commit7ef88ad561457c0346355dfd1f53e503ddfde719 (patch)
tree22f5e53aec61e0508f8d1aee130160fe24801144 /include/linux/kernel.h
parent1bae4ce27c9c90344f23c65ea6966c50ffeae2f5 (diff)
BUILD_BUG_ON: make it handle more cases
BUILD_BUG_ON used to use the optimizer to do code elimination or fail at link time; it was changed to first the size of a negative array (a nicer compile time error), then (in 8c87df457cb58fe75b9b893007917cf8095660a0) to a bitfield. This forced us to change some non-constant cases to MAYBE_BUILD_BUG_ON(); as Jan points out in that commit, it didn't work as intended anyway. bitfields: needs a literal constant at parse time, and can't be put under "if (__builtin_constant_p(x))" for example. negative array: can handle anything, but if the compiler can't tell it's a constant, silently has no effect. link time: breaks link if the compiler can't determine the value, but the linker output is not usually as informative as a compiler error. If we use the negative-array-size method *and* the link time trick, we get the ability to use BUILD_BUG_ON() under __builtin_constant_p() branches, and maximal ability for the compiler to detect errors at build time. We also document it thoroughly. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Cc: Jan Beulich <JBeulich@novell.com> Acked-by: Hollis Blanchard <hollisb@us.ibm.com>
Diffstat (limited to 'include/linux/kernel.h')
-rw-r--r--include/linux/kernel.h33
1 files changed, 27 insertions, 6 deletions
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index d07d8057e440..864712f3653d 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -575,12 +575,6 @@ struct sysinfo {
575 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */ 575 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
576}; 576};
577 577
578/* Force a compilation error if condition is true */
579#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
580
581/* Force a compilation error if condition is constant and true */
582#define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
583
584/* Force a compilation error if a constant expression is not a power of 2 */ 578/* Force a compilation error if a constant expression is not a power of 2 */
585#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ 579#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
586 BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) 580 BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
@@ -592,6 +586,33 @@ struct sysinfo {
592#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) 586#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
593#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); })) 587#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
594 588
589/**
590 * BUILD_BUG_ON - break compile if a condition is true.
591 * @cond: the condition which the compiler should know is false.
592 *
593 * If you have some code which relies on certain constants being equal, or
594 * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
595 * detect if someone changes it.
596 *
597 * The implementation uses gcc's reluctance to create a negative array, but
598 * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
599 * to inline functions). So as a fallback we use the optimizer; if it can't
600 * prove the condition is false, it will cause a link error on the undefined
601 * "__build_bug_on_failed". This error message can be harder to track down
602 * though, hence the two different methods.
603 */
604#ifndef __OPTIMIZE__
605#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
606#else
607extern int __build_bug_on_failed;
608#define BUILD_BUG_ON(condition) \
609 do { \
610 ((void)sizeof(char[1 - 2*!!(condition)])); \
611 if (condition) __build_bug_on_failed = 1; \
612 } while(0)
613#endif
614#define MAYBE_BUILD_BUG_ON(condition) BUILD_BUG_ON(condition)
615
595/* Trap pasters of __FUNCTION__ at compile-time */ 616/* Trap pasters of __FUNCTION__ at compile-time */
596#define __FUNCTION__ (__func__) 617#define __FUNCTION__ (__func__)
597 618