BUILD_BUG_ON: make it handle more cases
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 24 Jan 2011 20:45:10 +0000 (14:45 -0600)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 24 Jan 2011 04:15:10 +0000 (14:45 +1030)
commit2459450f73edbe787a9a99e141a0439681b02048
tree22f5e53aec61e0508f8d1aee130160fe24801144
parentd3a81907fb3f349bc2b901bf118a200f0c9a1c16
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
f04a9a15264cadf1147958e4a1b03fad68de00be) 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>
include/linux/kernel.h