1 // PR c++/42121 - g++ should warn or error on internal 0 size array in struct
3 // { dg-options "-Wno-error=pedantic" }
5 // Flexible array members are a feature of C99 (and newer) not provided
6 // by C++ 2014 and prior. G++ supports both the C99/C11 kind of flexible
7 // array members and pre-C99 zero-size arrays (defining an array of size
8 // zero). Since both features are provided for compatibility with C,
9 // G++ allows them in the same contexts as in C.
14 int a[]; // { dg-error "in an otherwise empty" }
17 // Verify that non-data members or static data members either before
18 // or after a flexible array member in an otherwise empty struct don't
19 // suppress the diagnostic.
21 int a[]; // { dg-error "in an otherwise empty" }
27 int a[]; // { dg-error "in an otherwise empty" }
31 int a[]; // { dg-error "in an otherwise empty" }
37 int a[]; // { dg-error "in an otherwise empty" }
41 int a[]; // { dg-error "in an otherwise empty" }
47 int a[]; // { dg-error "in an otherwise empty" }
51 int a[]; // { dg-error "in an otherwise empty" }
57 int a[]; // { dg-error "in an otherwise empty" }
61 int a[]; // { dg-error "in an otherwise empty" }
67 int a[]; // { dg-error "in an otherwise empty" }
71 int a[]; // { dg-error "in an otherwise empty" }
72 virtual void foo () = 0;
76 virtual void foo () = 0;
77 int a[]; // { dg-error "in an otherwise empty" }
81 int a[][1]; // { dg-error "in an otherwise empty" }
86 A a; // { dg-error "in an otherwise empty" }
89 // Verify also that a zero-size array doesn't suppress the diagnostic.
91 // a_0 below is diagnosed with -Wpedantic only and emits
92 // warning: ISO C++ forbids zero-size arrays
94 int a_x []; // { dg-error "in an otherwise empty" }
98 int a_x []; // { dg-error "flexible array member" }
100 // a_0 below is diagnosed with -Wpedantic only and emits
101 // warning: ISO C++ forbids zero-size arrays
105 // An empty struct is treated as if it had a single member of type
106 // char but the member cannot be accessed. Therefore, a struct
107 // containing a flexible array member followed by an empty struct
108 // is diagnosed to prevent the former subobject from sharing space
111 int a_x []; // { dg-error "flexible array member" }
112 struct { /* empty */ } s;
115 // Anonymous structs are a G++ extension. Members of anonymous structs
116 // are treated as if they were declared in the enclosing class.
118 struct { int i; }; // anonymous struct
122 // Unlike in the case above, a named struct is not anonymous and
123 // so doesn't contribute its member to that of the enclosing struct.
126 int a_x []; // { dg-error "in an otherwise empty" }
130 int a_x []; // { dg-error "not at end" }
135 int a_x []; // { dg-error "not at end" }
146 S a_x []; // { dg-error "incomplete type" }
151 S a_x []; // { dg-error "flexible array member" }
156 a_x []; // { dg-error "flexible array member" }
165 ASSERT_AT_END (Sx27, a_x);
169 a_x []; // { dg-error "not at end" }
174 // Pointer to an array of unknown size.
179 // Reference to an array of unknown size.
184 int a []; // { dg-error "not at end" }
193 ASSERT_AT_END (Sx32, a);
196 int a []; // { dg-error "otherwise empty" }
202 int a []; // { dg-error "otherwise empty" }
205 // Verify that intervening non-field declarations of members other
206 // than non-static data members don't affect the diagnostics.
208 int a[]; // { dg-error "not at end" }
219 ASSERT_AT_END (Sx36, a);
222 int a[]; // { dg-error "not at end" }
233 ASSERT_AT_END (Sx38, a);
236 int a[]; // { dg-error "not at end" }
247 ASSERT_AT_END (Sx40, a);
250 int a[]; // { dg-error "not at end" }
261 ASSERT_AT_END (Sx42, a);
264 int a[]; // { dg-error "not at end" }
275 ASSERT_AT_END (Sx44, a);
280 int a[]; // { dg-error "flexible array member" }
285 // Since members of an anonymous struct or union are considered to be
286 // members of the enclosing union the below defintions are valid and
296 ASSERT_AT_END (Anon1, good);
300 // The following is not an anonymous struct -- the type is unnamed
301 // but the object has a name.
303 int bad[]; // { dg-error "otherwise empty" }
316 ASSERT_AT_END (Anon2, good);
327 ASSERT_AT_END (Anon3, good);
331 int in_empty_struct[]; // { dg-error "in an otherwise empty" }
337 int not_at_end[]; // { dg-error "not at end" }
345 int not_at_end[]; // { dg-error "not at end" }
355 int not_at_end[]; // { dg-error "not at end" }
366 ASSERT_AT_END (Six, a);
369 int a[]; // { dg-error "flexible array member" }
378 int a[]; // { dg-error "not at end" }
393 int ax[]; // { dg-error "flexible array member" }
402 ASSERT_AT_END (S_a0_i_ax, ax);
410 ASSERT_AT_END (Si_a0_ax, ax);
414 int ax[]; // { dg-error "not at end" }
420 int ax[]; // { dg-error "flexible array member" }