xref: /llvm-project/clang/test/Sema/flexible-array-init.c (revision 1cba5fe1d153da2d2a7bb6c81867d0dbe1b854a3)
1 // RUN: clang -fsyntax-only -pedantic -verify %s
2 struct one {
3   int a;
4   int values[];
5 } x = {5, {1, 2, 3}};
6 
7 struct one x2 = { 5, 1, 2, 3 }; // expected-warning{{excess elements in struct initializer}}
8 
9 void test() {
10   struct one x3 = {5, {1, 2, 3}};
11 }
12 
13 struct foo {
14   int x;
15   int y[]; // expected-note 4 {{initialized flexible array member 'y' is here}}
16 };
17 struct bar { struct foo z; }; // expected-warning {{'z' may not be nested in a struct due to flexible array member}}
18 
19 struct foo a = { 1, { 2, 3, 4 } };        // Valid.
20 struct bar b = { { 1, { 2, 3, 4 } } };    // expected-error{{non-empty initialization of flexible array member inside subobject}}
21 struct bar c = { { 1, { } } };            // Valid. \
22               // expected-warning{{use of GNU empty initializer extension}} \
23               // expected-warning{{zero size arrays are an extension}}
24 struct foo d[1] = { { 1, { 2, 3, 4 } } };  // expected-warning{{'struct foo' may not be used as an array element due to flexible array member}} \
25               // expected-error{{non-empty initialization of flexible array member inside subobject}}
26 
27 struct foo desig_foo = { .y = {2, 3, 4} };
28 struct bar desig_bar = { .z.y = { } }; // expected-warning{{use of GNU empty initializer extension}} \
29   // expected-warning{{zero size arrays are an extension}}
30 struct bar desig_bar2 = { .z.y = { 2, 3, 4} }; // expected-error{{non-empty initialization of flexible array member inside subobject}}
31 struct foo design_foo2 = { .y = 2 }; // expected-error{{flexible array requires brace-enclosed initializer}}
32 
33 struct point {
34   int x, y;
35 };
36 
37 struct polygon {
38   int numpoints;
39   struct point points[]; // expected-note{{initialized flexible array member 'points' is here}}
40 };
41 struct polygon poly = {
42   .points[2] = { 1, 2} }; // expected-error{{designator into flexible array member subobject}}
43 
44 // PR3540
45 struct X {
46   int a;
47   int b;
48   char data[];
49 };
50 
51 struct Y {
52   int a:4;
53   int b:4;
54   int c;
55   int d;
56   int e;
57   struct X xs[]; // expected-warning{{'struct X' may not be used as an array element due to flexible array member}}
58 };
59