On Thu, Jul 07, 2022 at 01:46:28PM -0500, Eric Blake wrote:
I see that is now done as part of 0fa23df5c. While include/array-size.h looks okay, I personally have a tough time with include/compiler-macros.h BUILD_BUG_ON_ZERO(expr). Every time I encounter a similarly-named macro in other projects, I have to go re-read the docs, because (to at least me) the name is not self-describing.
You're not the first person to complain about that patch. How about the attached patch which incorporates your suggestion from later in the email.
I'm also worried that your code uses 'struct { int _array_size_failed[(expr)...]; }', which may end up causing VLA situations, where (mistakenly) passing in a non-constant expr could compile (as a VLA) rather than letting the compiler diagnose that expr wasn't consant. Hence, I like bitfields better than array sizes when trying to force a conditional compiler error, as it gets VLA out of the picture.
If I were designing it from scratch, I might create:
/* If EXPR is true, produce a non-zero struct size. Otherwise, fail the build */ #define BUILD_BUG_STRUCT_SIZE(expr) \ (sizeof (struct { int: (expr) ? 1 : -1; }))
One thing I didn't understand about this is how the unnamed bitfield works, I didn't know that was permitted? Or am I not parsing this right? Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW