[RFC] [PATCH] Add __array_size keyword

Stuart Brady sdb-gcc@zubnet.me.uk
Wed Feb 17 00:29:00 GMT 2016


On Tue, Feb 16, 2016 at 11:35:24PM +0000, Joseph Myers wrote:
> What's right is:
> 
> * In cases where it should return an integer constant (you've said that's 
> when the argument is not a VLA, as for sizeof), there should be no 
> diagnostic.

Right.

> * In cases where it should not return an integer constant, there should be 
> such a diagnostic (enabled by default).

Good point.  Indeed, for a VLA, I do get such a message.

> The case I raised in my original message was e.g.
> 
> int a;
> int b[2][a];
> 
> - should __array_size (b) be an integer constant (size_t)2, or should it 
> be non-constant (size_t)2 because the argument is a VLA (albeit a VLA 
> whose top-level dimension is an integer constant expression)?

Ouch.  I would say it should be an integer constant (size_t)2, simply as
that seems to me to be a reasonable expectation.  Unfortunately, this is
not what happens with my patch, as I get a -Wint-conversion warning. :-(

Oddly with "int b[a][2];" I find that __array_size(*b) *is* an integer
constant expression.

This leads me to wonder *why* I am not getting an integer constant
expression, and whether this might be a problem for other reasons.
I'm concerned GCC may not be able to give warnings pertaining to the
outer subscript for VLAs, as these may also be dependent on the same
underlying domain max value expression as __array_size.

The fact that __array_size(b) would be an integer constant expression
in this case, whereas sizeof(b) / sizeof(*b) would not seems to be an
advantage to this keyword that I had not originally considered.

Personally, I could tolerate this not returning an integer constant
expression, but I would regard that as a bug.  As extra confusion would
be introduced when fixing this, I would rather just get it right.

> On Tue, 16 Feb 2016, Stuart Brady wrote:
> > If I use __array_size in an enumeration, this works fine, rather than
> > giving me "error: enumerator value for ‘foo’ is not an integer constant".
> > I am not sure if this is not also a valid test.
> 
> It's a valid test, *but* in various contexts, such as in an enumeration, 
> you need -pedantic to enable diagnostics for some cases of invalid integer 
> constant expressions (but for the null pointer constant case, you don't).

Thanks, that's helpful to know.
-- 
Kind regards,
Stuart Brady



More information about the Gcc-patches mailing list