xref: /llvm-project/clang/test/Sema/flexible-array-init.c (revision 88fdea8ad9b7b8e695320732b61a028014e49184)
1 // RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
2 struct one {
3   int a;
4   int values[]; // expected-note 3{{initialized flexible array member 'values' is here}}
5 } x = {5, {1, 2, 3}}; // expected-warning{{flexible array initialization is a GNU extension}}
6 
7 struct one x2 = { 5, 1, 2, 3 }; // expected-warning{{flexible array initialization is a GNU extension}}
8 
9 void test() {
10   struct one x3 = {5, {1, 2, 3}}; // \
11    // expected-warning{{flexible array initialization is a GNU extension}} \
12    // expected-error {{non-static initialization of a variable with flexible array member}}
13 }
14 
15 struct foo {
16   int x;
17   int y[]; // expected-note 6 {{initialized flexible array member 'y' is here}}
18 };
19 struct bar { struct foo z; }; // expected-warning {{'z' may not be nested in a struct due to flexible array member}}
20 
21 struct foo a = { 1, { 2, 3, 4 } };        // expected-warning{{flexible array initialization is a GNU extension}}
22 struct bar b = { { 1, { 2, 3, 4 } } };    // expected-error{{non-empty initialization of flexible array member inside subobject}}
23 struct bar c = { { 1, { } } };            // // expected-warning{{flexible array initialization is a GNU extension}} \
24               // expected-warning{{use of GNU empty initializer extension}} \
25               // expected-warning{{zero size arrays are an extension}}
26 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}} \
27               // expected-error{{non-empty initialization of flexible array member inside subobject}}
28 
29 struct foo desig_foo = { .y = {2, 3, 4} };
30 struct bar desig_bar = { .z.y = { } }; // expected-warning{{use of GNU empty initializer extension}} \
31   // expected-warning{{zero size arrays are an extension}}
32 struct bar desig_bar2 = { .z.y = { 2, 3, 4} }; // expected-error{{non-empty initialization of flexible array member inside subobject}}
33 struct foo design_foo2 = { .y = 2 }; // expected-error{{flexible array requires brace-enclosed initializer}}
34 
35 struct point {
36   int x, y;
37 };
38 
39 struct polygon {
40   int numpoints;
41   struct point points[]; // expected-note{{initialized flexible array member 'points' is here}}
42 };
43 struct polygon poly = {
44   .points[2] = { 1, 2} }; // expected-error{{designator into flexible array member subobject}}
45 
46 // PR3540
47 struct X {
48   int a;
49   int b;
50   char data[];
51 };
52 
53 struct Y {
54   int a:4;
55   int b:4;
56   int c;
57   int d;
58   int e;
59   struct X xs[]; // expected-warning{{'struct X' may not be used as an array element due to flexible array member}}
60 };
61 
62 
63 // PR8217
64 struct PR8217a {
65   int  i;
66   char v[];
67 };
68 
69 void PR8217() {
70   struct PR8217a foo1 = { .i = 0, .v = "foo" }; // expected-error {{non-static initialization of a variable with flexible array member}}
71   struct PR8217a foo2 = { .i = 0 }; // expected-error {{non-static initialization of a variable with flexible array member}}
72   struct PR8217a foo3 = { .i = 0, .v = { 'b', 'a', 'r', '\0' } }; // expected-error {{non-static initialization of a variable with flexible array member}}
73   struct PR8217a bar;
74 }
75 
76