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