This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [RFC] [PATCH] Add __array_size keyword


On Sat, 13 Feb 2016, Stuart Brady wrote:

> > Critical issues to define and cover thoroughly in tests include the
> > rules for when operands of sizeof are evaluated, as adapted
> > appropriately for this keyword, and for when it returns various kinds
> > of constants.
> 
> So in other words, adapting all of the sizeof tests would be appropriate,
> and sizeof tests for non-array types would change from expected passes to
> expected failures?

It's not very clear what's a sizeof test, but all that are testing sizeof 
(as opposed to incidentally using it) and applicable to this keyword, yes.

> > Is the rule for your keyword that the operand is evaluated, and the
> > result not an integer constant, iff the operand is an array with a
> > variable number of elements (as opposed to an array with a constant
> > number of elements that themselves are variable-sized, for example)?
> 
> If I've understood correctly, then yes:
> 
>    #include <stdio.h>
>    void foo(int i) {
>      int a[i], b[__array_size(a)];
>      printf("%zi, %zi\n", __array_size(a), __array_size(b));
>    };
>    int main() { foo(42); }

That test doesn't relate to my question, which is about when arguments are 
evaluated and when results are or are not integer constant expressions.

For whether arguments are evaluated, you need __array_size with arguments 
that have side effects, and then test whether those side effects occurred.  
For whether results are integer constant expressions, you can test e.g. 
whether __array_size (a) - __array_size (a) is accepted in a context 
requiring a pointer (whether it acts as a valid null pointer constant).

> > C11 6.5.3.4#2 (sizeof) would need testing, 
> 
> Does this section differ from the September 7th draft in any way?

I don't know.

> > Presumably this keyword can be applied to an array at function prototype
> > scope whose size is explicitly or implicitly [*], though nothing useful
> > can be done with the results, as with [*]? (Cf. gcc.dg/vla-5.c.)
> 
> I'm not sure I quite understand the meaning of an implicit [*].  Does that
> just mean __array_size(foo) with an int foo[*] as another parameter?

Implicit [*] is e.g.

void f (int a, int (*b)[a], int (*c)[__array_size (*b)]);

where the VLA *b is at function prototype scope and so gets treated as [*] 
- and then __array_size (*) effectively means "an indeterminate value of 
type size_t" (but since that value only ever gets used in ways that end up 
with it being discarded, possibly through another implicit conversion to 
[*] as here, manipulating such indeterminate values is never a problem).

-- 
Joseph S. Myers
joseph@codesourcery.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]