[ccan] BUILD_ASSERT macro

Rusty Russell rusty at rustcorp.com.au
Thu May 19 13:37:22 EST 2011


On Wed, 18 May 2011 06:19:14 -0700 (PDT), Stephen Cameron <smcameron at yahoo.com> wrote:
> 
> I happened to be browsing include/linux/kernel.h, and noticed that BUILD_BUG_ON was changed a bit to handle some gcc 4.4 behavior differences.
> 
> http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commitdiff;h=7ef88ad561457c0346355dfd1f53e503ddfde719
> 
> Presumably BUILD_ASSERT could use similar changes.
> 
> Oh, hmmm.  Rusty is the committer on that change.  I guess you probably already knew about that then.

Indeed, it's a bit messy.  We can't what the kernel does, since it
assumes gcc optimizations and CCAN can't (we could put it under __GNUC__
and __OPTIMIZE__, but that seems fragile to me...).

So we're probably best off restricting BUILD_ASSERT() to actual compile
time constants (currently, if something isn't a constant, you get
neither a warning nor an error, eg "BUILD_ASSERT(some_var == 0)".

... Except I just tried that, and it breaks ARRAY_SIZE:

#define _array_size_chk(arr)						\
	BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(typeof(arr),	\
							typeof(&(arr)[0])))

It seems that __builtin_types_compatible_p() is *not* constant enough
for such usage.  Annoying!

So our current version must remain until someone comes up with something
better...

Thanks,
Rusty.


More information about the ccan mailing list