]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/testsuite/g++.dg/ext/flexary5.C
trans.c (check_inlining_for_nested_subprog): Quote reserved names.
[thirdparty/gcc.git] / gcc / testsuite / g++.dg / ext / flexary5.C
CommitLineData
7e9a3ad3
MS
1// { dg-do compile }
2// { dg-options "-Wno-error=pedantic" }
3
4// Test to verify flexible array members handling in base and derived
5// classes.
6
7#include "flexary.h"
8
9template <class T>
10struct S_no_diag: T {
11 char a[]; // cannot be diagnosed unless/until T is known
12};
13
14template <class T>
15struct STx_1: T {
16 char a[]; // { dg-error "flexible array member" }
17};
18
19template <class T, int I>
20struct STI: T {
21 char a[I]; // cannot be diagnosed unless/until T and I are known
22};
23
24template <class T, int I>
25struct STIx: T {
26 char a[I];
27};
28
29template <int> struct E { };
30
31STx_1<E<0> > stx_empty_1;
32STIx<E<0>, 0> stix_empty_1;
33
34// Verify that a sole flexible array member in a class with all empty
35// base classes is diagnosed.
36struct E1: E<0>, E<1> { };
37struct E2: E<2>, E<3> { };
38struct D1: E1, E2
39{
40 char a[]; // { dg-error "flexible array member" }
41};
42
43struct NE { size_t i; };
44
45struct A1x { int n, a[]; };
46struct D2: A1x, E1, E2 { };
47
48// Verify that the offset of the flexible array member is equal
49// to the size of each of the valid structs.
50ASSERT_AT_END (D2, a);
51
52struct D3: E1, A1x, E2 { };
53
54ASSERT_AT_END (D3, a);
55
56struct D4: E1, E2, A1x { };
57
58ASSERT_AT_END (D4, a);
59
60// Class with non-static data members and at least one base class
61// with such a member is not a standard layout class. The warning
62// below is benign since GCC computes the expected value.
63struct D5: E1, E2, NE { char a[]; };
64
a9c697b8 65ASSERT_AT_END (D5, a); // { dg-warning "'offsetof' within non-standard-layout" }
7e9a3ad3 66
96032047 67struct A2x_1 {
7e9a3ad3 68 size_t n;
96032047
MS
69 size_t a[]; // { dg-error "not at end of .struct D6." }
70};
71
72struct A2x_2 {
73 size_t n;
74 size_t a[]; // { dg-error "not at end of .struct D7." }
75};
76
77struct A2x_3 {
78 size_t n;
79 size_t a[]; // { dg-error "not at end of .struct D8." }
7e9a3ad3
MS
80};
81
82// Verify that the flexible array member in A2x above is diagnosed
83// for each of the three struct defintions below which also derive
84// from another struct with a flexible array member.
96032047
MS
85struct D6: A2x_1, E1, A1x { };
86struct D7: E1, A2x_2, E2, A1x { };
87struct D8: E1, E2, A2x_3, A1x { };
7e9a3ad3 88
96032047 89struct DA2x: A2x_1 { };
7e9a3ad3
MS
90
91struct D9: DA2x, E1, E2 { };
92
93ASSERT_AT_END (D9, a);
94
95struct D10: E1, DA2x, E2 { };
96
97ASSERT_AT_END (D10, a);
98
99struct D11: E1, E2, DA2x { };
100
101ASSERT_AT_END (D11, a);
102
103struct A3x {
104 size_t n;
105 size_t a[]; // { dg-error "not at end of .struct D12.| D13.| D14.| D15." }
106};
107
108// Verify that the flexible array member in A3x above is diagnosed
109// for each of the three struct defintions below which also derive
110// from another struct with a non-static member.
111struct D12: A3x, E1, NE { };
112struct D13: E1, A3x, NE { };
113struct D14: E1, E2, A3x, NE { };
114struct D15: E1, E2, NE, A3x { };
115
116struct A4x {
117 A4x ();
118 ~A4x ();
119
120 size_t n;
121 struct AS {
122 AS (int);
123 ~AS ();
124 size_t i;
125 } a[];
126};
127
128struct D16: A4x, E1, E2 { };
129
130ASSERT_AT_END (D16, a);
131
132struct D17: E1, A4x, E2 { };
133
134ASSERT_AT_END (D17, a);
135
136struct D18: E1, E2, A4x { };
137
138ASSERT_AT_END (D18, a);
139
140struct DA4x: A4x { };
141
142struct D19: DA4x, E1, E2 { };
143
144ASSERT_AT_END (D19, a);
145
146struct D20: E1, DA4x, E2 { };
147
148ASSERT_AT_END (D20, a);
149
150struct D21: E1, E2, DA4x { };
151
152ASSERT_AT_END (D21, a);
153
154
155struct A5x {
156 A5x (int);
157 virtual ~A5x ();
158
159 size_t n;
160 struct AS {
161 AS (int);
162 ~AS ();
163 size_t i;
164 } a[];
165};
166
167struct D22: A5x, E1, E2 { };
168
a9c697b8 169ASSERT_AT_END (D22, a); // { dg-warning "'offsetof' within non-standard-layout" }
7e9a3ad3
MS
170
171struct D23: E1, A5x, E2 { };
172
a9c697b8 173ASSERT_AT_END (D23, a); // { dg-warning "'offsetof' within non-standard-layout" }
7e9a3ad3
MS
174
175struct D24: E1, E2, A5x { };
176
a9c697b8 177ASSERT_AT_END (D24, a); // { dg-warning "'offsetof' within non-standard-layout" }
7e9a3ad3
MS
178
179struct DA5x: A5x { };
180
181struct D25: DA5x, E1, E2 { };
182
a9c697b8 183ASSERT_AT_END (D25, a); // { dg-warning "'offsetof' within non-standard-layout" }
7e9a3ad3
MS
184
185struct D26: E1, DA5x, E2 { };
186
a9c697b8 187ASSERT_AT_END (D26, a); // { dg-warning "'offsetof' within non-standard-layout" }
7e9a3ad3
MS
188
189struct D27: E1, E2, DA5x { };
190
a9c697b8 191ASSERT_AT_END (D27, a); // { dg-warning "'offsetof' within non-standard-layout" }
7e9a3ad3
MS
192
193// Verfify that a flexible array member is diagnosed even when deep
194// in the base class hierarchy.
195struct A6x {
196 size_t n;
197 size_t a[]; // { dg-error "not at end of .struct D28.| D29." }
198};
199
200struct AA6x: A6x { };
201struct NE1: NE { };
202struct NE2: NE { };
203
204struct D28: NE1, AA6x { };
205struct D29: AA6x, NE1 { };
206
7e9a3ad3
MS
207struct A7x {
208 size_t n;
96032047 209 size_t a[]; // { dg-error "flexible array member .A7x::a. not at end of .struct D33." }
7e9a3ad3
MS
210};
211
96032047
MS
212// Verify that a flexible array member in a virtual base class is not
213// diagnosed.
7e9a3ad3
MS
214struct DA7xV1: virtual A7x { };
215struct DA7xV2: virtual A7x { };
216
217struct D30: DA7xV1, DA7xV2 { };
218struct D31: DA7xV1, DA7xV2 { };
219struct D32: D30, D31 { };
96032047
MS
220
221// Verify the diagnostic when the flexible array is in an anonymous struct.
222struct A8x {
223 struct { // { dg-message "next member .A8x::<unnamed struct> A8x::<anonymous>. declared here" }
224 size_t n;
225 size_t a[];
226 };
227};
228
229struct D33: // { dg-message "in the definition of .struct D33." }
230 A7x, A8x { };