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