Debian Codegraph @

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

raw
1 // PR c++/71912 - [6/7 regression] flexible array in struct in union rejected
2 // { dg-do compile }
3 // { dg-additional-options "-Wpedantic -Wno-error=pedantic" }
4
5 #if __cplusplus
6
7 namespace pr71912 {
8
9 #endif
10
11 struct foo {
12 int a;
13 char s[]; // { dg-message "array member .char pr71912::foo::s \\\[\\\]. declared here" }
14 };
15
16 struct bar {
17 double d;
18 char t[];
19 };
20
21 struct baz {
22 union {
23 struct foo f;
24 struct bar b;
25 }
26 // The definition of struct foo is fine but the use of struct foo
27 // in the definition of u below is what's invalid and must be clearly
28 // diagnosed.
29 u; // { dg-warning "invalid use of .struct pr71912::foo. with a flexible array member in .struct pr71912::baz." }
30 };
31
32 struct xyyzy {
33 union {
34 struct {
35 int a;
36 char s[]; // { dg-message "declared here" }
37 } f;
38 struct {
39 double d;
40 char t[];
41 } b;
42 } u; // { dg-warning "invalid use" }
43 };
44
45 struct baz b;
46 struct xyyzy x;
47
48 #if __cplusplus
49
50 }
51
52 #endif
53
54 // The following definitions aren't strictly valid but, like those above,
55 // are accepted for compatibility with GCC (in C mode). They are benign
56 // in that the flexible array member is at the highest offset within
57 // the outermost type and doesn't overlap with other members except for
58 // those of the union.
59 union UnionStruct1 {
60 struct { int n1, a[]; } s;
61 int n2;
62 };
63
64 union UnionStruct2 {
65 struct { int n1, a1[]; } s1;
66 struct { int n2, a2[]; } s2;
67 int n3;
68 };
69
70 union UnionStruct3 {
71 struct { int n1, a1[]; } s1;
72 struct { double n2, a2[]; } s2;
73 char n3;
74 };
75
76 union UnionStruct4 {
77 struct { int n1, a1[]; } s1;
78 struct { struct { int n2, a2[]; } s2; } s3;
79 char n3;
80 };
81
82 union UnionStruct5 {
83 struct { struct { int n1, a1[]; } s1; } s2; // { dg-warning "invalid use" }
84 struct { double n2, a2[]; } s3;
85 char n3;
86 };
87
88 union UnionStruct6 {
89 struct { struct { int n1, a1[]; } s1; } s2; // { dg-warning "invalid use" }
90 struct { struct { int n2, a2[]; } s3; } s4;
91 char n3;
92 };
93
94 union UnionStruct7 {
95 struct { int n1, a1[]; } s1;
96 struct { double n2, a2[]; } s2;
97 struct { struct { int n3, a3[]; } s3; } s4;
98 };
99
100 union UnionStruct8 {
101 struct { int n1, a1[]; } s1;
102 struct { struct { int n2, a2[]; } s2; } s3;
103 struct { struct { int n3, a3[]; } s4; } s5;
104 };
105
106 union UnionStruct9 {
107 struct { struct { int n1, a1[]; } s1; } s2; // { dg-warning "invalid use" }
108 struct { struct { int n2, a2[]; } s3; } s4;
109 struct { struct { int n3, a3[]; } s5; } s6;
110 };
111
112 struct StructUnion1 {
113 union {
114 struct { int n1, a1[]; } s1; // { dg-message "declared here" }
115 struct { double n2, a2[]; } s2;
116 char n3;
117 } u; // { dg-warning "invalid use" }
118 };
119
120 // The following are invalid and rejected.
121 struct StructUnion2 {
122 union {
123 struct { int n1, a1[]; } s1; // { dg-error "not at end" }
124 } u;
125 char n3; // { dg-message "next member" }
126 };
127
128 struct StructUnion3 {
129 union {
130 struct { int n1, a1[]; } s1; // { dg-error "not at end" }
131 struct { double n2, a2[]; } s2;
132 } u;
133 char n3; // { dg-message "next member" }
134 };
135
136 struct StructUnion4 {
137 union {
138 struct { int n1, a1[]; } s1; // { dg-error "not at end" }
139 } u1;
140 union {
141 struct { double n2, a2[]; } s2;
142 } u2; // { dg-message "next member" }
143 };
144
145 struct StructUnion5 {
146 union {
147 union {
148 struct { int n1, a1[]; } s1; // { dg-message "declared here" }
149 } u1;
150 union { struct { int n2, a2[]; } s2; } u2;
151 } u; // { dg-warning "invalid use" }
152 };
153
154 struct StructUnion6 {
155 union {
156 struct { int n1, a1[]; } s1; // { dg-message "declared here" }
157 union { struct { int n2, a2[]; } s2; } u2;
158 } u; // { dg-warning "invalid use" }
159 };
160
161 struct StructUnion7 {
162 union {
163 union {
164 struct { double n2, a2[]; } s2; // { dg-message "declared here" }
165 } u2;
166 struct { int n1, a1[]; } s1;
167 } u; // { dg-warning "invalid use" }
168 };
169
170 struct StructUnion8 {
171 struct {
172 union {
173 union {
174 struct { int n1, a1[]; } s1; // { dg-error "not at end" }
175 } u1;
176 union {
177 struct { double n2, a2[]; } s2;
178 } u2;
179 } u;
180 } s1;
181
182 struct {
183 union {
184 union {
185 struct { int n1, a1[]; } s1;
186 } u1;
187 union {
188 struct { double n2, a2[]; } s2;
189 } u2;
190 } u; } s2; // { dg-message "next member" }
191 };
192
193 struct StructUnion9 { // { dg-message "in the definition" }
194 struct A1 {
195 union B1 {
196 union C1 {
197 struct Sx1 { int n1, a1[]; } sx1; // { dg-error "not at end" }
198 } c1;
199 union D1 {
200 struct Sx2 { double n2, a2[]; } sx2;
201 } d1;
202 } b1; // { dg-warning "invalid use" }
203 } a1;
204
205 struct A2 {
206 union B2 {
207 union C2 {
208 struct Sx3 { int n3, a3[]; } sx3; // { dg-message "declared here" }
209 } c2;
210 union D2 { struct Sx4 { double n4, a4[]; } sx4; } d2;
211 } b2; // { dg-warning "invalid use" }
212 } a2; // { dg-message "next member" }
213 };
214
215 // { dg-prune-output "forbids flexible array member" }