On 08/08/2018 02:48 PM, Joseph Myers wrote:
On Wed, 8 Aug 2018, Martin Sebor wrote:

Jason/Joseph, is this meant to be accepted?  It's rejected with
a hard error with -Wpedantic but I don't see any tests for it:

warning: ISO C forbids empty initializer braces [-Wpedantic]
   const char x[] = { };
                    ^
error: zero or negative size array ‘b’
   const char x[] = { };
              ^

I'll avoid handling it but I'm not sure I should add a test case
for it if it's accepted by mistake (and if I should open a bug
to reject it instead).

It's a perfectly ordinary combination of two GNU extensions (zero-size
arrays, and empty initializer braces), so yes, it should be accepted, and
have size 0 (but -pedantic should produce a pedwarn, not a hard error).

Okay, let me fix that separately.

As an aside, while adding tests for the zero-size array, I found
the handling of these extensions less than intuitive.

The empty braced initializer makes the array complete and gives
it a zero size.  I suppose that makes sense.

But for a declaration at file scope and without an initializer,
GCC warns that the array is assumed to have one element, but
then gives an error when sizeof is applied to it:

  const char a[];   // warning: array ‘a’ assumed to have one element
const int n = sizeof a; // error: invalid application of ‘sizeof’ to incomplete type ‘const char[]’

even though the array does have a size of 1.  (I guess that's
because of the [] syntax but the warning sure makes the sizeof
error surprising.)  Auto declarations of arrays with no bound
and with no intializer are rejected with an error:

  char a[];   // error: array size missing in ‘a’

I'm sure there's some explanation for this too but that doesn't
make it intuitive to work with.

It would be nice to say something about how this works and maybe
even why in the manual.  The documentation for zero-length arrays
doesn't describe these kinds of extensions or the subtle nuances
(it only talks about the [0] form where the zero is explicit, or
flexible array members).  Without tests it's hard to tell what's
meant to be valid and what could be accepted by accident.  With
the patch accepted tests will exist, but unless it's discussed
somewhere I missed I'll put together a patch to update the docs.

Martin

Reply via email to