PR c++/85553
[official-gcc.git] / gcc / testsuite / g++.dg / ext / flexary19.C
blob5eaf11b064dcbf44ca0cde053780c592f4ae5f9a
1 // { dg-do compile }
2 // { dg-additional-options "-Wpedantic -Wno-error=pedantic" }
4 // Verify that flexible array members are recognized as either valid
5 // or invalid in anonymous structs (a G++ extension) and C++ anonymous
6 // unions as well as in structs and unions that look anonymous but
7 // aren't.
8 struct S1
10   int i;
12   // The following declares a named data member of an unnamed struct
13   // (i.e., it is not an anonymous struct).
14   struct {
15     int a[];        // { dg-error "in an otherwise empty" }
16   } s;
19 struct S2
21   int i;
23   struct {
24     int a[];        // { dg-error "in an otherwise empty" }
25   } s[1];
28 struct S3
30   int i;
32   struct {
33     int a[];        // { dg-error "in an otherwise empty" }
34   } s[];
37 struct S4
39   int i;
41   struct {
42     int a[];        // { dg-error "in an otherwise empty" }
43   } s[2];
46 struct S5
48   int i;
50   struct {
51     int a[];        // { dg-error "in an otherwise empty" }
52   } s[1][2];
55 struct S6
57   int i;
59   struct {
60     int a[];        // { dg-error "in an otherwise empty" }
61   } s[][2];
64 struct S7
66   int i;
68   struct {
69     int a[];        // { dg-error "in an otherwise empty" }
70   } *s;
73 struct S8
75   int i;
77   struct {
78     int a[];        // { dg-error "in an otherwise empty" }
79   } **s;
82 struct S9
84   int i;
86   struct {
87     int a[];        // { dg-error "in an otherwise empty" }
88   } *s[1];
91 struct S10
93   int i;
95   struct {
96     int a[];        // { dg-error "in an otherwise empty" }
97   } *s[];
100 struct S11
102   int i;
104   struct {
105     int a[];        // { dg-error "in an otherwise empty" }
106   } **s[1];
109 struct S12
111   int i;
113   struct {
114     int a[];        // { dg-error "in an otherwise empty" }
115   } **s[];
118 struct S13
120   int i;
122   struct {
123     int a[];        // { dg-error "in an otherwise empty" }
124   } **s[2];
127 struct S14
129   int i;
131   struct {
132     int a[];        // { dg-error "in an otherwise empty" }
133   } &s;
136 struct S15
138   int i;
140   typedef struct {
141     int a[];        // { dg-error "in an otherwise empty" }
142   } T15;
145 struct S16
147   int i;
149   struct {          // { dg-warning "invalid use" }
150     // A flexible array as a sole member of an anonymous struct is
151     // rejected with an error in C mode but emits just a pedantic
152     // warning in C++.  Other than excessive pedantry there is no
153     // reason to reject it.
154     int a[];
155   };                // { dg-warning "anonymous struct" }
158 struct S17
160   int i;
162   union {           // anonymous union
163     int a[];        // { dg-error "flexible array member in union" }
164   };
167 struct S18
169   int i;
171   struct {
172     int j, a[];     // { dg-message "declared here" }
173   } s;              // { dg-warning "invalid use" }
176 struct S19
178   int i;
180   struct {          // { dg-warning "invalid use" }
181     int j, a[];     // { dg-message "declared here" }
182   };                // { dg-warning "anonymous struct" }
185 struct S20
187   static int i;
188   typedef int A[];
190   struct {
191     int j;
192     A a;            // { dg-message "declared here" }
193   } s;              // { dg-warning "invalid use" }
196 struct S21
198   static int i;
199   typedef int A[];
201   struct {          // { dg-warning "invalid use" }
202     int j;
203     A a;            // { dg-message "declared here" }
204   };                // { dg-warning "anonymous struct" }
207 struct S22
209   struct S22S {
210     static int i;
212     int a[];        // { dg-error "in an otherwise empty" }
213   } s;
216 struct S23
218   struct {
219     static int i;   // { dg-error "static data member" }
221     int a[];        // { dg-error "in an otherwise empty" }
222   };                // { dg-warning "anonymous struct" }
225 struct S24
227   static int i;
229   struct {
230     int a[];        // { dg-error "in an otherwise empty" }
231   } s;
234 struct S25
236   int i;
238   struct {
239     int j, a[];     // { dg-message "declared here" }
240   } s;              // { dg-warning "invalid use" }
242   // Verify that a static data member of the enclosing class doesn't
243   // cause infinite recursion or some such badness.
244   static S25 s2;
247 struct S26
249   template <class>
250   struct S26S {
251     static int a;
252   };
254   struct {
255     int a[];        // { dg-error "in an otherwise empty" }
256   } s;
259 struct S27
261   S27 *p;
262   int a[];
265 struct S28
267   struct A {
268     struct B {
269       S28 *ps28;
270       A   *pa;
271       B   *pb;
272     } b, *pb;
273     A *pa;
274   } a, *pa;
276   S28::A *pa2;
277   S28::A::B *pb;
279   int flexarray[];
282 // Verify that the notes printed along with the warnings point to the types
283 // or members they should point to and mention the correct relationships
284 // with the flexible array members.
285 namespace Notes
287 union A
289   struct {
290     struct {
291       int i, a[];   // { dg-message "declared here" }
292     } c;            // { dg-warning "invalid use" }
293   } d;
294   int j;
297 union B
299   struct {
300     struct {        // { dg-warning "invalid use" }
301       int i, a[];   // { dg-message "declared here" }
302     };              // { dg-warning "anonymous struct" }
303   };                // { dg-warning "anonymous struct" }
304   int j;
309 typedef struct Opaque* P29;
310 struct S30 { P29 p; };
311 struct S31 { S30 s; };
313 typedef struct { } S32;
314 typedef struct { S32 *ps32; } S33;
315 typedef struct
317   S33 *ps33;
318 } S34;
320 struct S35
322   struct A {
323     int i1, a1[];
324   };
326   struct B {
327     int i2, a2[];
328   };
330   typedef struct {
331     int i3, a3[];
332   } C;
334   typedef struct {
335     int i4, a4[];
336   } D;
338   typedef A A2;
339   typedef B B2;
340   typedef C C2;
341   typedef D D2;
344 // { dg-prune-output "forbids flexible array member" }