blob: 78fc7c5e0df3700cd15f81ce2d008f06e47358cb [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
Douglas Gregoreeb15d42009-02-04 22:46:25 +00002struct one {
3 int a;
Douglas Gregorc6eddf52010-10-15 23:53:28 +00004 int values[]; // expected-note 4{{initialized flexible array member 'values' is here}}
Douglas Gregora6457962009-03-20 00:32:56 +00005} x = {5, {1, 2, 3}}; // expected-warning{{flexible array initialization is a GNU extension}}
Douglas Gregoreeb15d42009-02-04 22:46:25 +00006
Douglas Gregora6457962009-03-20 00:32:56 +00007struct one x2 = { 5, 1, 2, 3 }; // expected-warning{{flexible array initialization is a GNU extension}}
Douglas Gregoreeb15d42009-02-04 22:46:25 +00008
9void test() {
Eli Friedmanf40fd6b2011-08-23 22:24:57 +000010 struct one x3 = {5, {1, 2, 3}}; // expected-error{{initialization of flexible array member is not allowed}}
Douglas Gregorc6eddf52010-10-15 23:53:28 +000011 struct one x3a = { 5 };
12 struct one x3b = { .a = 5 };
13 struct one x3c = { 5, {} }; // expected-warning{{use of GNU empty initializer extension}} \
14 // expected-warning{{flexible array initialization is a GNU extension}} \
15 // expected-warning{{zero size arrays are an extension}}
Douglas Gregoreeb15d42009-02-04 22:46:25 +000016}
17
18struct foo {
19 int x;
Eli Friedmanf40fd6b2011-08-23 22:24:57 +000020 int y[]; // expected-note 8 {{initialized flexible array member 'y' is here}}
Douglas Gregoreeb15d42009-02-04 22:46:25 +000021};
Douglas Gregor0bfe54f2009-02-10 21:49:46 +000022struct bar { struct foo z; }; // expected-warning {{'z' may not be nested in a struct due to flexible array member}}
Douglas Gregoreeb15d42009-02-04 22:46:25 +000023
Douglas Gregora6457962009-03-20 00:32:56 +000024struct foo a = { 1, { 2, 3, 4 } }; // expected-warning{{flexible array initialization is a GNU extension}}
Eli Friedmanf40fd6b2011-08-23 22:24:57 +000025struct bar b = { { 1, { 2, 3, 4 } } }; // expected-error{{initialization of flexible array member is not allowed}}
Douglas Gregora6457962009-03-20 00:32:56 +000026struct bar c = { { 1, { } } }; // // expected-warning{{flexible array initialization is a GNU extension}} \
Douglas Gregor0bfe54f2009-02-10 21:49:46 +000027 // expected-warning{{use of GNU empty initializer extension}} \
28 // expected-warning{{zero size arrays are an extension}}
29struct foo d[1] = { { 1, { 2, 3, 4 } } }; // expected-warning{{'struct foo' may not be used as an array element due to flexible array member}} \
Eli Friedmanf40fd6b2011-08-23 22:24:57 +000030 // expected-error{{initialization of flexible array member is not allowed}}
Douglas Gregoreeb15d42009-02-04 22:46:25 +000031
Eli Friedmanf40fd6b2011-08-23 22:24:57 +000032struct foo desig_foo = { .y = {2, 3, 4} }; // expected-warning{{flexible array initialization is a GNU extension}}
Douglas Gregor0bfe54f2009-02-10 21:49:46 +000033struct bar desig_bar = { .z.y = { } }; // expected-warning{{use of GNU empty initializer extension}} \
Eli Friedmanf40fd6b2011-08-23 22:24:57 +000034 // expected-warning{{zero size arrays are an extension}} \
35 // expected-warning{{flexible array initialization is a GNU extension}}
36struct bar desig_bar2 = { .z.y = { 2, 3, 4} }; // expected-error{{initialization of flexible array member is not allowed}}
Douglas Gregoreeb15d42009-02-04 22:46:25 +000037struct foo design_foo2 = { .y = 2 }; // expected-error{{flexible array requires brace-enclosed initializer}}
38
39struct point {
40 int x, y;
41};
42
43struct polygon {
44 int numpoints;
45 struct point points[]; // expected-note{{initialized flexible array member 'points' is here}}
46};
47struct polygon poly = {
48 .points[2] = { 1, 2} }; // expected-error{{designator into flexible array member subobject}}
Douglas Gregor0bfe54f2009-02-10 21:49:46 +000049
50// PR3540
51struct X {
52 int a;
53 int b;
54 char data[];
55};
56
57struct Y {
58 int a:4;
59 int b:4;
60 int c;
61 int d;
62 int e;
63 struct X xs[]; // expected-warning{{'struct X' may not be used as an array element due to flexible array member}}
64};
Chris Lattner16c5dea2010-10-10 18:16:20 +000065
66
67// PR8217
68struct PR8217a {
69 int i;
Eli Friedmanf40fd6b2011-08-23 22:24:57 +000070 char v[]; // expected-note 2 {{initialized flexible array member 'v' is here}}
Chris Lattner16c5dea2010-10-10 18:16:20 +000071};
72
73void PR8217() {
Eli Friedmanf40fd6b2011-08-23 22:24:57 +000074 struct PR8217a foo1 = { .i = 0, .v = "foo" }; // expected-error {{initialization of flexible array member is not allowed}}
Douglas Gregorc6eddf52010-10-15 23:53:28 +000075 struct PR8217a foo2 = { .i = 0 };
Eli Friedmanf40fd6b2011-08-23 22:24:57 +000076 struct PR8217a foo3 = { .i = 0, .v = { 'b', 'a', 'r', '\0' } }; // expected-error {{initialization of flexible array member is not allowed}}
Chris Lattner16c5dea2010-10-10 18:16:20 +000077 struct PR8217a bar;
78}
79
Eli Friedmanf40fd6b2011-08-23 22:24:57 +000080typedef struct PR10648 {
81 unsigned long n;
82 int v[]; // expected-note {{initialized flexible array member 'v' is here}}
83} PR10648;
84int f10648() {
85 return (PR10648){2, {3, 4}}.v[1]; // expected-error {{initialization of flexible array member is not allowed}}
86}
87
88struct FlexWithUnnamedBitfield { int : 10; int x; int y[]; }; // expected-note {{initialized flexible array member 'y' is here}}
89void TestFlexWithUnnamedBitfield() {
90 struct FlexWithUnnamedBitfield x = {10, {3}}; // expected-error {{initialization of flexible array member is not allowed}}
91}