array-init.c revision 0c706c29f20b6fa36759fa41333b9c3ec0bd2969
1a5728872c7702ddd09537c95bc3cbd20e1f2fb09Daniel Dunbar// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
2f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff
3410e3e206b50e0336bd294fa5fb8dfcca3ee1af5Steve Naroffextern int foof() = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
4410e3e206b50e0336bd294fa5fb8dfcca3ee1af5Steve Naroff
56f9f307d527e3451470dd07ae932475f26c6de6eSteve Naroffstatic int x, y, z;
66f9f307d527e3451470dd07ae932475f26c6de6eSteve Naroff
7d8803632d248a360a040ff03eff1162988058036Chris Lattnerstatic int ary[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}
8d8803632d248a360a040ff03eff1162988058036Chris Lattnerint ary2[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}
96f9f307d527e3451470dd07ae932475f26c6de6eSteve Naroff
106f9f307d527e3451470dd07ae932475f26c6de6eSteve Naroffextern int fileScopeExtern[3] = { 1, 3, 5 }; // expected-warning{{'extern' variable has an initializer}}
116f9f307d527e3451470dd07ae932475f26c6de6eSteve Naroff
1208a41901e18aeb91b87d031b93df70374af02564Douglas Gregorstatic long ary3[] = { 1, "abc", 3, 4 }; // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
1338374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff
14f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroffvoid func() {
15f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff  int x = 1;
16f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff
17410e3e206b50e0336bd294fa5fb8dfcca3ee1af5Steve Naroff  typedef int TInt = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
18410e3e206b50e0336bd294fa5fb8dfcca3ee1af5Steve Naroff
19d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff  int xComputeSize[] = { 1, 3, 5 };
20f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff
2138374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff  int x3[x] = { 1, 2 }; // expected-error{{variable-sized object may not be initialized}}
22f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff
23759f25237864f3a3cc23eb01f0c0ce6edcc9342dEli Friedman  int x4 = { 1, 2 }; // expected-warning{{excess elements in scalar initializer}}
24f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff
25f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff  int y[4][3] = {
26f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff    { 1, 3, 5 },
27f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff    { 2, 4, 6 },
28f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff    { 3, 5, 7 },
29f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff  };
30f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff
31f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff  int y2[4][3] = {
32f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff    1, 3, 5, 2, 4, 6, 3, 5, 7
33f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff  };
34f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff
35d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff  int y3[4][3] = {
36d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff    { 1, 3, 5 },
37d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff    { 2, 4, 6 },
38d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff    { 3, 5, 7 },
39d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff    { 4, 6, 8 },
407c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor    { 5 }, // expected-warning{{excess elements in array initializer}}
41d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff  };
42d35005ece906cca5238d75b031b8db7eac0ac6e1Steve Naroff
43f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff  struct threeElements {
44f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff    int a,b,c;
45f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff  } z = { 1 };
46f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff
4708a41901e18aeb91b87d031b93df70374af02564Douglas Gregor  struct threeElements *p = 7; // expected-warning{{incompatible integer to pointer conversion initializing 'struct threeElements *' with an expression of type 'int'}}
486f9f307d527e3451470dd07ae932475f26c6de6eSteve Naroff
496f9f307d527e3451470dd07ae932475f26c6de6eSteve Naroff  extern int blockScopeExtern[3] = { 1, 3, 5 }; // expected-error{{'extern' variable cannot have an initializer}}
5038374b05791ee93300b9fbe8ceb3957f54184b37Steve Naroff
5108a41901e18aeb91b87d031b93df70374af02564Douglas Gregor  static long x2[3] = { 1.0, "abc" , 5.8 }; // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
52f009063ab7e05be7781751ff9e4b42630f07a747Steve Naroff}
53371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff
54371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroffvoid test() {
55371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  int y1[3] = {
560c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman    { 1, 2, 3 } // expected-warning{{excess elements in scalar initializer}}
57371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  };
58371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  int y3[4][3] = {
59371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 1, 3, 5 },
60371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 2, 4, 6 },
61371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 3, 5, 7 },
62371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 4, 6, 8 },
637c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor    {  }, // expected-warning{{use of GNU empty initializer extension}} expected-warning{{excess elements in array initializer}}
64371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  };
65371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  int y4[4][3] = {
667c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor    { 1, 3, 5, 2 }, // expected-warning{{excess elements in array initializer}}
67371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 4, 6 },
68371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 3, 5, 7 },
69a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroff    { 4, 6, 8 },
70371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  };
71371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff}
72371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff
73371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroffvoid allLegalAndSynonymous() {
74371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  short q[4][3][2] = {
75371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 1 },
76371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 2, 3 },
77371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 4, 5, 6 }
78371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  };
79371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  short q2[4][3][2] = {
80371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 1, 0, 0, 0, 0, 0 },
81371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 2, 3, 0, 0, 0, 0 },
82371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 4, 5, 6 }
83371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  };
84371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  short q3[4][3][2] = {
85371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    {
86371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff      { 1 },
87371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    },
88371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    {
89371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff      { 2, 3 },
90371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    },
91371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    {
92371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff      { 4, 5 },
93371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff      { 6 },
94371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    },
95371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  };
96371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff}
97371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff
98371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroffvoid legal() {
99371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  short q[][3][2] = {
100371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 1 },
101371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 2, 3 },
102371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 4, 5, 6 }
103371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  };
1044c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  int q_sizecheck[(sizeof(q) / sizeof(short [3][2])) == 3? 1 : -1];
105371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff}
106371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff
107fd8b4a4b29a2b9d662d0c9a92c1eebd83160b10bSteve Naroffunsigned char asso_values[] = { 34 };
108fd8b4a4b29a2b9d662d0c9a92c1eebd83160b10bSteve Naroffint legal2() {
109fd8b4a4b29a2b9d662d0c9a92c1eebd83160b10bSteve Naroff  return asso_values[0];
110fd8b4a4b29a2b9d662d0c9a92c1eebd83160b10bSteve Naroff}
111fd8b4a4b29a2b9d662d0c9a92c1eebd83160b10bSteve Naroff
112371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroffvoid illegal() {
113371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  short q2[4][][2] = { // expected-error{{array has incomplete element type 'short [][2]'}}
114371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 1, 0, 0, 0, 0, 0 },
115371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 2, 3, 0, 0, 0, 0 },
116371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    { 4, 5, 6 }
117371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  };
118371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  short q3[4][3][] = { // expected-error{{array has incomplete element type 'short []'}}
119371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    {
120371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff      { 1 },
121371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    },
122371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    {
123371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff      { 2, 3 },
124371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    },
125371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    {
126371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff      { 4, 5 },
127371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff      { 6 },
128371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff    },
129371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  };
130fd89bc825026e44c68a68db72d4012fd6752e70fChris Lattner  int a[][] = { 1, 2 }; // expected-error{{array has incomplete element type 'int []'}}
131371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff}
132371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff
133371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Narofftypedef int AryT[];
134371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff
135371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroffvoid testTypedef()
136371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff{
137371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff  AryT a = { 1, 2 }, b = { 3, 4, 5 };
1384c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  int a_sizecheck[(sizeof(a) / sizeof(int)) == 2? 1 : -1];
1394c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  int b_sizecheck[(sizeof(b) / sizeof(int)) == 3? 1 : -1];
140371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff}
141371227d6dc3cf89a3165d592cd9a4cbb400ec45cSteve Naroff
1422fdc3749097a581567dbd9fe66c48c0458bd3e34Steve Naroffstatic char const xx[] = "test";
1434c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregorint xx_sizecheck[(sizeof(xx) / sizeof(char)) == 5? 1 : -1];
1442fdc3749097a581567dbd9fe66c48c0458bd3e34Steve Naroffstatic char const yy[5] = "test";
1452fdc3749097a581567dbd9fe66c48c0458bd3e34Steve Naroffstatic char const zz[3] = "test"; // expected-warning{{initializer-string for char array is too long}}
1462fdc3749097a581567dbd9fe66c48c0458bd3e34Steve Naroff
1471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid charArrays() {
1481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  static char const test[] = "test";
1491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  int test_sizecheck[(sizeof(test) / sizeof(char)) == 5? 1 : -1];
1501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  static char const test2[] = { "weird stuff" };
1511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  static char const test3[] = { "test", "excess stuff" }; // expected-warning{{excess elements in char array initializer}}
1522fdc3749097a581567dbd9fe66c48c0458bd3e34Steve Naroff
1532fdc3749097a581567dbd9fe66c48c0458bd3e34Steve Naroff  char* cp[] = { "Hello" };
1542fdc3749097a581567dbd9fe66c48c0458bd3e34Steve Naroff
1552fdc3749097a581567dbd9fe66c48c0458bd3e34Steve Naroff  char c[] = { "Hello" };
1562fdc3749097a581567dbd9fe66c48c0458bd3e34Steve Naroff  int l[sizeof(c) == 6 ? 1 : -1];
1572fdc3749097a581567dbd9fe66c48c0458bd3e34Steve Naroff
15808a41901e18aeb91b87d031b93df70374af02564Douglas Gregor  int i[] = { "Hello "}; // expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [7]'}}
1597c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor  char c2[] = { "Hello", "Good bye" }; //expected-warning{{excess elements in char array initializer}}
1602fdc3749097a581567dbd9fe66c48c0458bd3e34Steve Naroff
16108a41901e18aeb91b87d031b93df70374af02564Douglas Gregor  int i2[1] = { "Hello" }; //expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [6]'}}
1622fdc3749097a581567dbd9fe66c48c0458bd3e34Steve Naroff  char c3[5] = { "Hello" };
1632fdc3749097a581567dbd9fe66c48c0458bd3e34Steve Naroff  char c4[4] = { "Hello" }; //expected-warning{{initializer-string for char array is too long}}
1642fdc3749097a581567dbd9fe66c48c0458bd3e34Steve Naroff
165396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar  int i3[] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
1662fdc3749097a581567dbd9fe66c48c0458bd3e34Steve Naroff}
1672fdc3749097a581567dbd9fe66c48c0458bd3e34Steve Naroff
168ca107309ffbcacae31d1685ab133f75ca0d7271aSteve Naroffvoid variableArrayInit() {
169ca107309ffbcacae31d1685ab133f75ca0d7271aSteve Naroff  int a = 4;
170cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  char strlit[a] = "foo"; //expected-error{{array initializer must be an initializer list or string literal}}
171ca107309ffbcacae31d1685ab133f75ca0d7271aSteve Naroff  int b[a] = { 1, 2, 4 }; //expected-error{{variable-sized object may not be initialized}}
172ca107309ffbcacae31d1685ab133f75ca0d7271aSteve Naroff}
173a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroff
174a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroff// Pure array tests
175a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Narofffloat r1[10] = {{7}}; //expected-warning{{braces around scalar initializer}}
176a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Narofffloat r2[] = {{8}}; //expected-warning{{braces around scalar initializer}}
177a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroffchar r3[][5] = {1,2,3,4,5,6};
1784c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregorint r3_sizecheck[(sizeof(r3) / sizeof(char[5])) == 2? 1 : -1];
179a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroffchar r3_2[sizeof r3 == 10 ? 1 : -1];
1807c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregorfloat r4[1][2] = {1,{2},3,4}; //expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}}
181a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroffchar r5[][5] = {"aa", "bbb", "ccccc"};
182a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroffchar r6[sizeof r5 == 15 ? 1 : -1];
183a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroffconst char r7[] = "zxcv";
184a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroffchar r8[5] = "5char";
185a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroffchar r9[5] = "6chars"; //expected-warning{{initializer-string for char array is too long}}
186a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroff
187a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroffint r11[0] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
188a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroff
189a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroff// Some struct tests
190a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroffvoid autoStructTest() {
191a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroffstruct s1 {char a; char b;} t1;
192a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroffstruct s2 {struct s1 c;} t2 = { t1 };
193a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroff// The following is a less than great diagnostic (though it's on par with EDG).
19408a41901e18aeb91b87d031b93df70374af02564Douglas Gregorstruct s1 t3[] = {t1, t1, "abc", 0}; //expected-warning{{incompatible pointer to integer conversion initializing 'char' with an expression of type 'char [4]'}}
195a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroffint t4[sizeof t3 == 6 ? 1 : -1];
196a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroff}
197578edc6614236b224f63ac707acecaeb2a74d6b4Steve Naroffstruct foo { int z; } w;
198578edc6614236b224f63ac707acecaeb2a74d6b4Steve Naroffint bar (void) {
19908a41901e18aeb91b87d031b93df70374af02564Douglas Gregor  struct foo z = { w }; //expected-error{{initializing 'int' with an expression of incompatible type 'struct foo'}}
200578edc6614236b224f63ac707acecaeb2a74d6b4Steve Naroff  return z.z;
201578edc6614236b224f63ac707acecaeb2a74d6b4Steve Naroff}
202a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroffstruct s3 {void (*a)(void);} t5 = {autoStructTest};
203a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregorstruct {int a; int b[];} t6 = {1, {1, 2, 3}}; // expected-warning{{flexible array initialization is a GNU extension}} \
204a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor// expected-note{{initialized flexible array member 'b' is here}}
205a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroffunion {char a; int b;} t7[] = {1, 2, 3};
206a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroffint t8[sizeof t7 == (3*sizeof(int)) ? 1 : -1];
207a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroff
208a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroffstruct bittest{int : 31, a, :21, :12, b;};
2097c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregorstruct bittest bittestvar = {1, 2, 3, 4}; //expected-warning{{excess elements in struct initializer}}
210a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroff
211a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroff// Not completely sure what should happen here...
212c56c977f44d0bd8422a6f0fe87f361cbb728c06bEli Friedmanint u1 = {}; //expected-warning{{use of GNU empty initializer extension}} expected-error{{scalar initializer cannot be empty}}
21309865a903affa9c08687859d8de65470064c7bc2Eli Friedmanint u2 = {{3}}; //expected-warning{{too many braces around scalar initializer}}
214a99603333fffb57cf9ac37eabb190c8f5afc914bSteve Naroff
215638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman// PR2362
216638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedmanvoid varArray() {
217638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  int c[][x] = { 0 }; //expected-error{{variable-sized object may not be initialized}}
218638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman}
219402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman
220402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman// PR2151
2210333296d142d45bf2723635848928815b7491f91Douglas Gregorvoid emptyInit() {struct {} x[] = {6};} //expected-warning{{empty struct (accepted as an extension) has size 0 in C, size 1 in C++}} \
2220333296d142d45bf2723635848928815b7491f91Douglas Gregor// expected-error{{initializer for aggregate with no elements}}
223402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman
224d1969d803cfcc65f1c334df4cc89c7fdd33ee4c9Mike Stumpvoid noNamedInit() {
225d1969d803cfcc65f1c334df4cc89c7fdd33ee4c9Mike Stump  struct {int:5;} x[] = {6}; //expected-error{{initializer for aggregate with no elements}}
226f84eda37251c679e2f20343c47a4a3586d9a8e21Eli Friedman}
227f84eda37251c679e2f20343c47a4a3586d9a8e21Eli Friedmanstruct {int a; int:5;} noNamedImplicit[] = {1,2,3};
228f84eda37251c679e2f20343c47a4a3586d9a8e21Eli Friedmanint noNamedImplicitCheck[sizeof(noNamedImplicit) == 3 * sizeof(*noNamedImplicit) ? 1 : -1];
229f84eda37251c679e2f20343c47a4a3586d9a8e21Eli Friedman
23062b6a65f0bef3ada0d8bac1a98f7e0769e6e8ffbNuno Lopes
23162b6a65f0bef3ada0d8bac1a98f7e0769e6e8ffbNuno Lopes// ptrs are constant
23262b6a65f0bef3ada0d8bac1a98f7e0769e6e8ffbNuno Lopesstruct soft_segment_descriptor {
2331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  long ssd_base;
23462b6a65f0bef3ada0d8bac1a98f7e0769e6e8ffbNuno Lopes};
23562b6a65f0bef3ada0d8bac1a98f7e0769e6e8ffbNuno Lopesstatic int dblfault_tss;
23662b6a65f0bef3ada0d8bac1a98f7e0769e6e8ffbNuno Lopes
23762b6a65f0bef3ada0d8bac1a98f7e0769e6e8ffbNuno Lopesunion uniao { int ola; } xpto[1];
23862b6a65f0bef3ada0d8bac1a98f7e0769e6e8ffbNuno Lopes
23962b6a65f0bef3ada0d8bac1a98f7e0769e6e8ffbNuno Lopesstruct soft_segment_descriptor gdt_segs[] = {
2401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  {(long) &dblfault_tss},
2411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  { (long)xpto},
24262b6a65f0bef3ada0d8bac1a98f7e0769e6e8ffbNuno Lopes};
24362b6a65f0bef3ada0d8bac1a98f7e0769e6e8ffbNuno Lopes
24473419bf6cbf8e5f7a0f9b8855d6531db264ae899Nuno Lopesstatic void sppp_ipv6cp_up();
2450333296d142d45bf2723635848928815b7491f91Douglas Gregorconst struct {} ipcp = { sppp_ipv6cp_up }; //expected-warning{{empty struct (accepted as an extension) has size 0 in C, size 1 in C++}} \
2460333296d142d45bf2723635848928815b7491f91Douglas Gregor// expected-warning{{excess elements in struct initializer}}
2474c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
2484c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregorstruct _Matrix { union { float m[4][4]; }; }; //expected-warning{{anonymous unions are a GNU extension in C}}
2494c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregortypedef struct _Matrix Matrix;
2504c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregorvoid test_matrix() {
2514c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  const Matrix mat1 = {
2524c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    { { 1.0f, 2.0f, 3.0f, 4.0f,
2534c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        5.0f, 6.0f, 7.0f, 8.0f,
2544c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        9.0f, 10.0f, 11.0f, 12.0f,
2554c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        13.0f, 14.0f, 15.0f, 16.0f } }
2564c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  };
2574c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
2584c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  const Matrix mat2 = {
2594c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    1.0f, 2.0f, 3.0f, 4.0f,
2604c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    5.0f, 6.0f, 7.0f, 8.0f,
2614c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    9.0f, 10.0f, 11.0f, 12.0f,
2624c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    13.0f, 14.0f, 15.0f, 16.0f
2634c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  };
2644c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
265e540858b289b23653bcb23646f135729203635cbEli Friedman
266e540858b289b23653bcb23646f135729203635cbEli Friedmanchar badchararray[1] = { badchararray[0], "asdf" }; // expected-warning {{excess elements in array initializer}} expected-error {{initializer element is not a compile-time constant}}
267cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor
268cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor// Test the GNU extension for initializing an array from an array
269cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor// compound literal. PR9261.
270cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregortypedef int int5[5];
271cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregorint a1[5] = (int[]){1, 2, 3, 4, 5}; // expected-warning{{initialization of an array of type 'int [5]' from a compound literal of type 'int [5]' is a GNU extension}}
272cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregorint a2[5] = (int[5]){1, 2, 3, 4, 5}; // expected-warning{{initialization of an array of type 'int [5]' from a compound literal of type 'int [5]' is a GNU extension}}
273cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregorint a3[] = ((int[]){1, 2, 3, 4, 5}); // expected-warning{{initialization of an array of type 'int []' from a compound literal of type 'int [5]' is a GNU extension}}
274cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregorint a4[] = (int[5]){1, 2, 3, 4, 5}; // expected-warning{{initialization of an array of type 'int []' from a compound literal of type 'int [5]' is a GNU extension}}
275cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregorint a5[] = (int5){1, 2, 3, 4, 5}; // expected-warning{{initialization of an array of type 'int []' from a compound literal of type 'int5' (aka 'int [5]') is a GNU extension}}
276cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor
277cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregorint a6[5] = (int[]){1, 2, 3}; // expected-error{{cannot initialize array of type 'int [5]' with array of type 'int [3]'}}
278cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor
279cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregorint nonconst_value();
280cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregorint a7[5] = (int[5]){ 1, 2, 3, 4, nonconst_value() }; // expected-error{{initializer element is not a compile-time constant}}
281