Jan Beulich
2012-Jul-26 15:09 UTC
[PATCH] use C11''s _Static_assert() for BUILD_BUG_ON etc when available
This produces a more meaningful compiler diagnostic in case it happens to trigger. As a minor piece of cleanup, at once also use BUILD_BUG_ON_ZERO() to actually define BUILD_BUG_ON() in the legacy case, slightly reducing redundancy. Signed-off-by: Jan Beulich <jbeulich@suse.com> --- a/xen/include/xen/lib.h +++ b/xen/include/xen/lib.h @@ -14,14 +14,20 @@ void __warn(char *file, int line); #define BUG_ON(p) do { if (unlikely(p)) BUG(); } while (0) #define WARN_ON(p) do { if (unlikely(p)) WARN(); } while (0) +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) /* Force a compilation error if condition is true */ -#define BUILD_BUG_ON(condition) ((void)sizeof(struct { int:-!!(condition); })) +#define BUILD_BUG_ON(cond) ({ _Static_assert(!(cond), "!(" #cond ")"); }) /* 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); })) +#define BUILD_BUG_ON_ZERO(cond) \ + sizeof(struct { _Static_assert(!(cond), "!(" #cond ")"); }) +#else +#define BUILD_BUG_ON_ZERO(cond) sizeof(struct { int:-!!(cond); }) +#define BUILD_BUG_ON(cond) ((void)BUILD_BUG_ON_ZERO(cond)) +#endif #ifndef assert_failed #define assert_failed(p) \ _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel
Keir Fraser
2012-Jul-26 15:32 UTC
Re: [PATCH] use C11''s _Static_assert() for BUILD_BUG_ON etc when available
On 26/07/2012 16:09, "Jan Beulich" <JBeulich@suse.com> wrote:> This produces a more meaningful compiler diagnostic in case it happens > to trigger. > > As a minor piece of cleanup, at once also use BUILD_BUG_ON_ZERO() to > actually define BUILD_BUG_ON() in the legacy case, slightly reducing > redundancy. > > Signed-off-by: Jan Beulich <jbeulich@suse.com>Acked-by: Keir Fraser <keir@xen.org>> --- a/xen/include/xen/lib.h > +++ b/xen/include/xen/lib.h > @@ -14,14 +14,20 @@ void __warn(char *file, int line); > #define BUG_ON(p) do { if (unlikely(p)) BUG(); } while (0) > #define WARN_ON(p) do { if (unlikely(p)) WARN(); } while (0) > > +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) > /* Force a compilation error if condition is true */ > -#define BUILD_BUG_ON(condition) ((void)sizeof(struct { int:-!!(condition); > })) > +#define BUILD_BUG_ON(cond) ({ _Static_assert(!(cond), "!(" #cond ")"); }) > > /* 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); })) > +#define BUILD_BUG_ON_ZERO(cond) \ > + sizeof(struct { _Static_assert(!(cond), "!(" #cond ")"); }) > +#else > +#define BUILD_BUG_ON_ZERO(cond) sizeof(struct { int:-!!(cond); }) > +#define BUILD_BUG_ON(cond) ((void)BUILD_BUG_ON_ZERO(cond)) > +#endif > > #ifndef assert_failed > #define assert_failed(p) \ > > > > _______________________________________________ > Xen-devel mailing list > Xen-devel@lists.xen.org > http://lists.xen.org/xen-devel