1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// RUN: %clang_cc1 -fsyntax-only -Wgnu -Wc11-extensions -verify %s
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// REQUIRES: LP64
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdochextern int foof() = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstatic int x, y, z;
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstatic int ary[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdochint ary2[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdochextern int fileScopeExtern[3] = { 1, 3, 5 }; // expected-warning{{'extern' variable has an initializer}}
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstatic long ary3[] = { 1, "abc", 3, 4 }; // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid func() {
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int x = 1;
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  typedef int TInt = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int xComputeSize[] = { 1, 3, 5 };
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int x3[x] = { 1, 2 }; // expected-error{{variable-sized object may not be initialized}}
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int x4 = { 1, 2 }; // expected-warning{{excess elements in scalar initializer}}
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int y[4][3] = {
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 1, 3, 5 },
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 2, 4, 6 },
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 3, 5, 7 },
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  };
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int y2[4][3] = {
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    1, 3, 5, 2, 4, 6, 3, 5, 7
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  };
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int y3[4][3] = {
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 1, 3, 5 },
396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    { 2, 4, 6 },
406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    { 3, 5, 7 },
416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    { 4, 6, 8 },
426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    { 5 }, // expected-warning{{excess elements in array initializer}}
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  };
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  struct threeElements {
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    int a,b,c;
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  } z = { 1 };
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  struct threeElements *p = 7; // expected-warning{{incompatible integer to pointer conversion initializing 'struct threeElements *' with an expression of type 'int'}}
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  extern int blockScopeExtern[3] = { 1, 3, 5 }; // expected-error{{'extern' variable cannot have an initializer}}
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  static long x2[3] = { 1.0,
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                        "abc", // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                         5.8 }; // expected-warning {{implicit conversion from 'double' to 'long' changes value from 5.8 to 5}}
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid test() {
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int y1[3] = {
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 1, 2, 3 } // expected-warning{{excess elements in scalar initializer}}
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  };
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int y3[4][3] = {
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 1, 3, 5 },
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 2, 4, 6 },
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 3, 5, 7 },
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 4, 6, 8 },
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    {  }, // expected-warning{{use of GNU empty initializer extension}} expected-warning{{excess elements in array initializer}}
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  };
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int y4[4][3] = {
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 1, 3, 5, 2 }, // expected-warning{{excess elements in array initializer}}
716e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    { 4, 6 },
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 3, 5, 7 },
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 4, 6, 8 },
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  };
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid allLegalAndSynonymous() {
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  short q[4][3][2] = {
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 1 },
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 2, 3 },
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 4, 5, 6 }
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  };
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  short q2[4][3][2] = {
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 1, 0, 0, 0, 0, 0 },
85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 2, 3, 0, 0, 0, 0 },
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 4, 5, 6 }
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  };
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  short q3[4][3][2] = {
896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    {
906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      { 1 },
916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    },
92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    {
93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      { 2, 3 },
94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    },
95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    {
96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      { 4, 5 },
97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      { 6 },
98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    },
99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  };
100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void legal() {
103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  short q[][3][2] = {
104116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 1 },
105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 2, 3 },
106116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 4, 5, 6 }
107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  };
108116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int q_sizecheck[(sizeof(q) / sizeof(short [3][2])) == 3? 1 : -1];
109116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
111116680a4aac90f2aa7413d9095a592090648e557Ben Murdochunsigned char asso_values[] = { 34 };
112116680a4aac90f2aa7413d9095a592090648e557Ben Murdochint legal2() {
113116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return asso_values[0];
114116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
1156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
1166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void illegal() {
1176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  short q2[4][][2] = { // expected-error{{array has incomplete element type 'short [][2]'}}
1186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    { 1, 0, 0, 0, 0, 0 },
119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 2, 3, 0, 0, 0, 0 },
120116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    { 4, 5, 6 }
121116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  };
122116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  short q3[4][3][] = { // expected-error{{array has incomplete element type 'short []'}}
123116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    {
124116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      { 1 },
125116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    },
126116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    {
127116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      { 2, 3 },
128116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    },
129116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    {
130116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      { 4, 5 },
131116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      { 6 },
132116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    },
133116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  };
134116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int a[][] = { 1, 2 }; // expected-error{{array has incomplete element type 'int []'}}
135116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
136116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
137116680a4aac90f2aa7413d9095a592090648e557Ben Murdochtypedef int AryT[];
138116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
139116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid testTypedef()
140116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch{
141116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  AryT a = { 1, 2 }, b = { 3, 4, 5 };
142116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int a_sizecheck[(sizeof(a) / sizeof(int)) == 2? 1 : -1];
143116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int b_sizecheck[(sizeof(b) / sizeof(int)) == 3? 1 : -1];
144116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
145116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
146116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstatic char const xx[] = "test";
147116680a4aac90f2aa7413d9095a592090648e557Ben Murdochint xx_sizecheck[(sizeof(xx) / sizeof(char)) == 5? 1 : -1];
148116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstatic char const yy[5] = "test";
149116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstatic char const zz[3] = "test"; // expected-warning{{initializer-string for char array is too long}}
150116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
151116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid charArrays() {
152116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  static char const test[] = "test";
153  int test_sizecheck[(sizeof(test) / sizeof(char)) == 5? 1 : -1];
154  static char const test2[] = { "weird stuff" };
155  static char const test3[] = { "test", "excess stuff" }; // expected-warning{{excess elements in char array initializer}}
156
157  char* cp[] = { "Hello" };
158
159  char c[] = { "Hello" };
160  int l[sizeof(c) == 6 ? 1 : -1];
161
162  int i[] = { "Hello "}; // expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [7]'}}
163  char c2[] = { "Hello", "Good bye" }; //expected-warning{{excess elements in char array initializer}}
164
165  int i2[1] = { "Hello" }; //expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [6]'}}
166  char c3[5] = { "Hello" };
167  char c4[4] = { "Hello" }; //expected-warning{{initializer-string for char array is too long}}
168
169  int i3[] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
170}
171
172void variableArrayInit() {
173  int a = 4;
174  char strlit[a] = "foo"; //expected-error{{variable-sized object may not be initialized}}
175  int b[a] = { 1, 2, 4 }; //expected-error{{variable-sized object may not be initialized}}
176}
177
178// Pure array tests
179float r1[10] = {{7}}; //expected-warning{{braces around scalar initializer}}
180float r2[] = {{8}}; //expected-warning{{braces around scalar initializer}}
181char r3[][5] = {1,2,3,4,5,6};
182int r3_sizecheck[(sizeof(r3) / sizeof(char[5])) == 2? 1 : -1];
183char r3_2[sizeof r3 == 10 ? 1 : -1];
184float r4[1][2] = {1,{2},3,4}; //expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}}
185char r5[][5] = {"aa", "bbb", "ccccc"};
186char r6[sizeof r5 == 15 ? 1 : -1];
187const char r7[] = "zxcv";
188char r8[5] = "5char";
189char r9[5] = "6chars"; //expected-warning{{initializer-string for char array is too long}}
190unsigned char r10[] = __extension__ (_Generic(0, int: (__extension__ "foo" )));
191int r11[0] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
192
193// Some struct tests
194void autoStructTest() {
195struct s1 {char a; char b;} t1;
196struct s2 {struct s1 c;} t2 = { t1 };
197// The following is a less than great diagnostic (though it's on par with EDG).
198struct s1 t3[] = {t1, t1, "abc", 0}; //expected-warning{{incompatible pointer to integer conversion initializing 'char' with an expression of type 'char [4]'}}
199int t4[sizeof t3 == 6 ? 1 : -1];
200}
201struct foo { int z; } w;
202int bar (void) {
203  struct foo z = { w }; //expected-error{{initializing 'int' with an expression of incompatible type 'struct foo'}}
204  return z.z;
205}
206struct s3 {void (*a)(void);} t5 = {autoStructTest};
207struct {int a; int b[];} t6 = {1, {1, 2, 3}}; // expected-warning{{flexible array initialization is a GNU extension}} \
208// expected-note{{initialized flexible array member 'b' is here}}
209union {char a; int b;} t7[] = {1, 2, 3};
210int t8[sizeof t7 == (3*sizeof(int)) ? 1 : -1];
211
212struct bittest{int : 31, a, :21, :12, b;};
213struct bittest bittestvar = {1, 2, 3, 4}; //expected-warning{{excess elements in struct initializer}}
214
215// Not completely sure what should happen here...
216int u1 = {}; //expected-warning{{use of GNU empty initializer extension}} expected-error{{scalar initializer cannot be empty}}
217int u2 = {{3}}; //expected-warning{{too many braces around scalar initializer}}
218
219// PR2362
220void varArray() {
221  int c[][x] = { 0 }; //expected-error{{variable-sized object may not be initialized}}
222}
223
224// PR2151
225void emptyInit() {struct {} x[] = {6};} //expected-warning{{empty struct is a GNU extension}} \
226// expected-error{{initializer for aggregate with no elements}}
227
228void noNamedInit() {
229  struct {int:5;} x[] = {6}; //expected-error{{initializer for aggregate with no elements}} \
230// expected-warning {{struct without named members is a GNU extension}}
231}
232struct {int a; int:5;} noNamedImplicit[] = {1,2,3};
233int noNamedImplicitCheck[sizeof(noNamedImplicit) == 3 * sizeof(*noNamedImplicit) ? 1 : -1];
234
235
236// ptrs are constant
237struct soft_segment_descriptor {
238  long ssd_base;
239};
240static int dblfault_tss;
241
242union uniao { int ola; } xpto[1];
243
244struct soft_segment_descriptor gdt_segs[] = {
245  {(long) &dblfault_tss},
246  { (long)xpto},
247};
248
249static void sppp_ipv6cp_up();
250const struct {} ipcp = { sppp_ipv6cp_up }; //expected-warning{{empty struct is a GNU extension}} \
251// expected-warning{{excess elements in struct initializer}}
252
253struct _Matrix { union { float m[4][4]; }; }; //expected-warning{{anonymous unions are a C11 extension}}
254typedef struct _Matrix Matrix;
255void test_matrix() {
256  const Matrix mat1 = {
257    { { 1.0f, 2.0f, 3.0f, 4.0f,
258        5.0f, 6.0f, 7.0f, 8.0f,
259        9.0f, 10.0f, 11.0f, 12.0f,
260        13.0f, 14.0f, 15.0f, 16.0f } }
261  };
262
263  const Matrix mat2 = {
264    1.0f, 2.0f, 3.0f, 4.0f,
265    5.0f, 6.0f, 7.0f, 8.0f,
266    9.0f, 10.0f, 11.0f, 12.0f,
267    13.0f, 14.0f, 15.0f, 16.0f
268  };
269}
270
271char badchararray[1] = { badchararray[0], "asdf" }; // expected-warning {{excess elements in array initializer}} expected-error {{initializer element is not a compile-time constant}}
272
273// Test the GNU extension for initializing an array from an array
274// compound literal. PR9261.
275typedef int int5[5];
276int 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}}
277int 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}}
278int 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}}
279int 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}}
280int 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}}
281
282int a6[5] = (int[]){1, 2, 3}; // expected-error{{cannot initialize array of type 'int [5]' with array of type 'int [3]'}}
283
284int nonconst_value();
285int a7[5] = (int[5]){ 1,
286                      2,
287                      3,
288                      4,
289                      nonconst_value() // expected-error{{initializer element is not a compile-time constant}}
290};
291
292// <rdar://problem/10636946>
293__attribute__((weak)) const unsigned int test10_bound = 10;
294char test10_global[test10_bound]; // expected-error {{variable length array declaration not allowed at file scope}}
295void test10() {
296  char test10_local[test10_bound] = "help"; // expected-error {{variable-sized object may not be initialized}}
297}
298