introduce __must_be_array
PATCH 1/1 introduce __must_be_array
Cloning the similar construct from Linux, allowing to detect improper uses of ARRAY_SIZE() at build time.
xen/include/xen/compiler.h +4 -0
@@ -35,6 +35,10 @@#define offsetof (a,b) ((unsigned long)&(((a *)0)->b))#endif +/* &a[0] degrades to a pointer: a different type from an array */ +#define __must_be_array (a) \ + BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0]))) +#ifdef GCC_HAS_VISIBILITY_ATTRIBUTE /* Results in more efficient PIC code (no indirections through GOT or PLT). */ #pragma GCC visibility push(hidden)
xen/include/xen/config.h +1 -1
@@ -11,7 +11,7 @@#define EXPORT_SYMBOL (var)#define EXPORT_SYMBOL_GPL (var) -#define ARRAY_SIZE (x) (sizeof(x) / sizeof((x)[0])) +#define ARRAY_SIZE (x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))/* * The following log levels are as follows:
xen/include/xen/lib.h +6 -0
@@ -18,6 +18,12 @@ void __warn(char *file, int line);/* Force a compilation error if condition is true */ #define BUILD_BUG_ON (condition) ((void)sizeof(struct { int:-!!(condition); })) +/* Force a compilation error if condition is true, but also produce a +result (of value 0 and type size_t), so the expression can be used +e.g. in a structure initializer (or where-ever else comma expressions +aren't permitted). */ +#define BUILD_BUG_ON_ZERO (e) (sizeof(struct { int:-!!(e); })) +#ifndef assert_failed#define assert_failed (p) \ do { \
Quoted 4 lines · Jan Beulich
Cloning the similar construct from Linux, allowing to detect improper uses of
ARRAY_SIZE()at build time.Signed-off-by Jan Beulich <[email protected]>
Would simply removing the cast to void from BUILD_BUG_ON() lead to compiler warnings?
-- Keir
Quoted 10 lines
Quoted 1 line
Quoted 1 line
Keir Fraser <[email protected]> 20.05.09 15:15 >>>
Quoted 4 lines · Jan Beulich
Cloning the similar construct from Linux, allowing to detect improper uses of
ARRAY_SIZE()at build time.Signed-off-by Jan Beulich <[email protected]>Would simply removing the cast to void from
BUILD_BUG_ON()lead to compiler warnings?
While I didn't try it, I'm convinced it would.
Jan
Quoted 4 lines · Jan Beulich
Quoted 2 lines
Would simply removing the cast to void from
BUILD_BUG_ON()lead to compiler warnings?While I didn't try it, I'm convinced it would.
I tried it (gcc 4.1.2) and it didn't. And I'm not sure what the theoretical warning message would be: Throwing away a non-void value? We do that with return values from functions very often.
I'll change BUILD_BUG_ON() and avoid BUILD_BUG_ON_ZERO(), but revert to your approach if some gccs turn out to act differently.
-- Keir
Quoted 8 lines · Keir Fraser
Quoted 1 line
While I didn't try it, I'm convinced it would.
I tried it (gcc 4.1.2) and it didn't. And I'm not sure what the theoretical warning message would be: Throwing away a non-void value? We do that with return values from functions very often.
I'll change
BUILD_BUG_ON()and avoidBUILD_BUG_ON_ZERO(), but revert to your approach if some gccs turn out to act differently.
Actually I suppose having ZERO in the name is nice in arithmetic expressions. So I'll just check in as-is after all.
-- Keir