Debian Codegraph @

gcc/testsuite/g++.dg/ext/flexary19.C

raw
1 // { dg-do compile }
2 // { dg-additional-options "-Wpedantic -Wno-error=pedantic" }
3
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
9 {
10 int i;
11
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-warning "in an otherwise empty" }
16 } s;
17 };
18
19 struct S2
20 {
21 int i;
22
23 struct {
24 int a[]; // { dg-warning "in an otherwise empty" }
25 } s[1];
26 };
27
28 struct S3
29 {
30 int i;
31
32 struct {
33 int a[]; // { dg-warning "in an otherwise empty" }
34 } s[];
35 };
36
37 struct S4
38 {
39 int i;
40
41 struct {
42 int a[]; // { dg-warning "in an otherwise empty" }
43 } s[2];
44 };
45
46 struct S5
47 {
48 int i;
49
50 struct {
51 int a[]; // { dg-warning "in an otherwise empty" }
52 } s[1][2];
53 };
54
55 struct S6
56 {
57 int i;
58
59 struct {
60 int a[]; // { dg-warning "in an otherwise empty" }
61 } s[][2];
62 };
63
64 struct S7
65 {
66 int i;
67
68 struct {
69 int a[]; // { dg-warning "in an otherwise empty" }
70 } *s;
71 };
72
73 struct S8
74 {
75 int i;
76
77 struct {
78 int a[]; // { dg-warning "in an otherwise empty" }
79 } **s;
80 };
81
82 struct S9
83 {
84 int i;
85
86 struct {
87 int a[]; // { dg-warning "in an otherwise empty" }
88 } *s[1];
89 };
90
91 struct S10
92 {
93 int i;
94
95 struct {
96 int a[]; // { dg-warning "in an otherwise empty" }
97 } *s[];
98 };
99
100 struct S11
101 {
102 int i;
103
104 struct {
105 int a[]; // { dg-warning "in an otherwise empty" }
106 } **s[1];
107 };
108
109 struct S12
110 {
111 int i;
112
113 struct {
114 int a[]; // { dg-warning "in an otherwise empty" }
115 } **s[];
116 };
117
118 struct S13
119 {
120 int i;
121
122 struct {
123 int a[]; // { dg-warning "in an otherwise empty" }
124 } **s[2];
125 };
126
127 struct S14
128 {
129 int i;
130
131 struct {
132 int a[]; // { dg-warning "in an otherwise empty" }
133 } &s;
134 };
135
136 struct S15
137 {
138 int i;
139
140 typedef struct {
141 int a[]; // { dg-warning "in an otherwise empty" }
142 } T15;
143 };
144
145 struct S16
146 {
147 int i;
148
149 struct { // { dg-warning "10:ISO C\\+\\+ prohibits anonymous struct|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 };
156 };
157
158 struct S17
159 {
160 int i;
161
162 union { // { dg-warning "invalid use" }
163 int a[]; // { dg-warning "flexible array member in union" }
164 };
165 };
166
167 struct S18
168 {
169 int i;
170
171 struct {
172 int j, a[]; // { dg-message "declared here" }
173 } s; // { dg-warning "invalid use" }
174 };
175
176 struct S19
177 {
178 int i;
179
180 struct { // { dg-warning "10:ISO C\\+\\+ prohibits anonymous struct|invalid use" }
181 int j, a[]; // { dg-message "declared here" }
182 };
183 };
184
185 struct S20
186 {
187 static int i;
188 typedef int A[];
189
190 struct {
191 int j;
192 A a; // { dg-message "declared here" }
193 } s; // { dg-warning "invalid use" }
194 };
195
196 struct S21
197 {
198 static int i;
199 typedef int A[];
200
201 struct { // { dg-warning "10:ISO C\\+\\+ prohibits anonymous struct|invalid use" }
202 int j;
203 A a; // { dg-message "declared here" }
204 };
205 };
206
207 struct S22
208 {
209 struct S22S {
210 static int i;
211
212 int a[]; // { dg-warning "in an otherwise empty" }
213 } s; // { dg-warning "invalid use" }
214 };
215
216 struct S23
217 {
218 struct { // { dg-warning "10:ISO C\\+\\+ prohibits anonymous struct" }
219 static int i; // { dg-error "static data member" }
220
221 int a[]; // { dg-warning "in an otherwise empty" }
222 };
223 };
224
225 struct S24
226 {
227 static int i;
228
229 struct {
230 int a[]; // { dg-warning "in an otherwise empty" }
231 } s;
232 };
233
234 struct S25
235 {
236 int i;
237
238 struct {
239 int j, a[]; // { dg-message "declared here" }
240 } s; // { dg-warning "invalid use" }
241
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;
245 };
246
247 struct S26
248 {
249 template <class>
250 struct S26S {
251 static int a;
252 };
253
254 struct {
255 int a[]; // { dg-warning "in an otherwise empty" }
256 } s;
257 };
258
259 struct S27
260 {
261 S27 *p;
262 int a[];
263 };
264
265 struct S28
266 {
267 struct A {
268 struct B {
269 S28 *ps28;
270 A *pa;
271 B *pb;
272 } b, *pb;
273 A *pa;
274 } a, *pa;
275
276 S28::A *pa2;
277 S28::A::B *pb;
278
279 int flexarray[];
280 };
281
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
286 {
287 union A
288 {
289 struct {
290 struct {
291 int i, a[]; // { dg-message "declared here" }
292 } c; // { dg-warning "invalid use" }
293 } d;
294 int j;
295 };
296
297 union B
298 {
299 struct { // { dg-warning "10:ISO C\\+\\+ prohibits anonymous struct" }
300 struct { // { dg-warning "12:ISO C\\+\\+ prohibits anonymous struct|invalid use" }
301 int i, a[]; // { dg-message "declared here" }
302 };
303 };
304 int j;
305 };
306
307 }
308
309 typedef struct Opaque* P29;
310 struct S30 { P29 p; };
311 struct S31 { S30 s; };
312
313 typedef struct { } S32;
314 typedef struct { S32 *ps32; } S33;
315 typedef struct
316 {
317 S33 *ps33;
318 } S34;
319
320 struct S35
321 {
322 struct A {
323 int i1, a1[];
324 };
325
326 struct B {
327 int i2, a2[];
328 };
329
330 typedef struct {
331 int i3, a3[];
332 } C;
333
334 typedef struct {
335 int i4, a4[];
336 } D;
337
338 typedef A A2;
339 typedef B B2;
340 typedef C C2;
341 typedef D D2;
342 };
343
344 // { dg-prune-output "forbids flexible array member" }