flexible-array-init.c revision 16c5dea6c2d3e4cf529de9d9b37f6a40441acb2c
1// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
2struct 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
7struct one x2 = { 5, 1, 2, 3 }; // expected-warning{{flexible array initialization is a GNU extension}}
8
9void 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
15struct foo {
16  int x;
17  int y[]; // expected-note 6 {{initialized flexible array member 'y' is here}}
18};
19struct bar { struct foo z; }; // expected-warning {{'z' may not be nested in a struct due to flexible array member}}
20
21struct foo a = { 1, { 2, 3, 4 } };        // expected-warning{{flexible array initialization is a GNU extension}}
22struct bar b = { { 1, { 2, 3, 4 } } };    // expected-error{{non-empty initialization of flexible array member inside subobject}}
23struct 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}}
26struct 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
29struct foo desig_foo = { .y = {2, 3, 4} };
30struct bar desig_bar = { .z.y = { } }; // expected-warning{{use of GNU empty initializer extension}} \
31  // expected-warning{{zero size arrays are an extension}}
32struct bar desig_bar2 = { .z.y = { 2, 3, 4} }; // expected-error{{non-empty initialization of flexible array member inside subobject}}
33struct foo design_foo2 = { .y = 2 }; // expected-error{{flexible array requires brace-enclosed initializer}}
34
35struct point {
36  int x, y;
37};
38
39struct polygon {
40  int numpoints;
41  struct point points[]; // expected-note{{initialized flexible array member 'points' is here}}
42};
43struct polygon poly = {
44  .points[2] = { 1, 2} }; // expected-error{{designator into flexible array member subobject}}
45
46// PR3540
47struct X {
48  int a;
49  int b;
50  char data[];
51};
52
53struct 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
64struct PR8217a {
65  int  i;
66  char v[];
67};
68
69void 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