1 /* Test for constant expressions: cases involving VLAs.  */
2 /* Origin: Joseph Myers <joseph@codesourcery.com> */
3 /* { dg-do compile } */
4 /* { dg-options "-std=iso9899:1999 -pedantic-errors" } */
5 
6 /* It appears address constants may contain casts to variably modified
7    types.  Whether they should be permitted was discussed in
8    <http://groups.google.com/group/comp.std.c/msg/923eee5ab690fd98>
9    <LV7g2Vy3ARF$Ew9Q@romana.davros.org>; since static pointers to VLAs
10    are definitely permitted within functions and may be initialized
11    and such initialization involves implicit conversion to a variably
12    modified type, allowing explicit casts seems appropriate.  Thus,
13    GCC allows them as long as the "evaluated" size expressions do not
14    contain the various operators not permitted to be evaluated in a
15    constant expression, and as long as the result is genuinely
16    constant (meaning that pointer arithmetic using the size of the VLA
17    is generally not permitted).  */
18 
19 static int sa[100];
20 
21 volatile int nv;
22 
23 int
f(int m,int n)24 f (int m, int n)
25 {
26   static int (*a1)[n] = &sa;
27   static int (*a2)[n] = (int (*)[n])sa;
28   static int (*a3)[n] = (int (*)[(int){n}])sa;
29   static int (*a4)[n] = (int (*)[(int){m++}])sa; /* { dg-error "constant" } */
30   static int (*a5)[n] = (int (*)[(int){++m}])sa; /* { dg-error "constant" } */
31   static int (*a6)[n] = (int (*)[(int){m--}])sa; /* { dg-error "constant" } */
32   static int (*a7)[n] = (int (*)[(int){--m}])sa; /* { dg-error "constant" } */
33   static int (*a8)[n] = (int (*)[(m=n)])sa; /* { dg-error "constant" } */
34   static int (*a9)[n] = (int (*)[(m+=n)])sa; /* { dg-error "constant" } */
35   static int (*a10)[n] = (int (*)[f(m,n)])sa; /* { dg-error "constant" } */
36   static int (*a11)[n] = (int (*)[(m,n)])sa; /* { dg-error "constant" } */
37   static int (*a12)[n] = (int (*)[sizeof(int[n])])sa;
38   static int (*a13)[n] = (int (*)[sizeof(int[m++])])sa; /* { dg-error "constant" } */
39   static int (*a14)[n] = (int (*)[sizeof(*a1)])sa;
40   static int (*a15)[n] = (int (*)[sizeof(*(int (*)[n])sa)])sa;
41   static int (*a16)[n] = (int (*)[sizeof(*(int (*)[m++])sa)])sa; /* { dg-error "constant" } */
42   static int (*a17)[n] = (int (*)[nv])sa;
43   typedef int (*vmt)[m++];
44   static int (*a18)[n] = (vmt)sa;
45   return n;
46 }
47