1*67e74705SXin Li // RUN: %clang_cc1 -std=gnu99 -fsyntax-only -pedantic -verify %s
2*67e74705SXin Li // RUN: %clang_cc1 -std=gnu99 -fsyntax-only -Wgnu -Wc11-extensions -verify %s
3*67e74705SXin Li // REQUIRES: LP64
4*67e74705SXin Li
5*67e74705SXin Li extern int foof() = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
6*67e74705SXin Li
7*67e74705SXin Li static int x, y, z;
8*67e74705SXin Li
9*67e74705SXin Li static int ary[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}
10*67e74705SXin Li int ary2[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}
11*67e74705SXin Li
12*67e74705SXin Li extern int fileScopeExtern[3] = { 1, 3, 5 }; // expected-warning{{'extern' variable has an initializer}}
13*67e74705SXin Li
14*67e74705SXin Li static long ary3[] = { 1, "abc", 3, 4 }; // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
15*67e74705SXin Li
func()16*67e74705SXin Li void func() {
17*67e74705SXin Li int x = 1;
18*67e74705SXin Li
19*67e74705SXin Li typedef int TInt = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
20*67e74705SXin Li
21*67e74705SXin Li int xComputeSize[] = { 1, 3, 5 };
22*67e74705SXin Li
23*67e74705SXin Li int x3[x] = { 1, 2 }; // expected-error{{variable-sized object may not be initialized}}
24*67e74705SXin Li
25*67e74705SXin Li int x4 = { 1, 2 }; // expected-warning{{excess elements in scalar initializer}}
26*67e74705SXin Li
27*67e74705SXin Li int y[4][3] = {
28*67e74705SXin Li { 1, 3, 5 },
29*67e74705SXin Li { 2, 4, 6 },
30*67e74705SXin Li { 3, 5, 7 },
31*67e74705SXin Li };
32*67e74705SXin Li
33*67e74705SXin Li int y2[4][3] = {
34*67e74705SXin Li 1, 3, 5, 2, 4, 6, 3, 5, 7
35*67e74705SXin Li };
36*67e74705SXin Li
37*67e74705SXin Li int y3[4][3] = {
38*67e74705SXin Li { 1, 3, 5 },
39*67e74705SXin Li { 2, 4, 6 },
40*67e74705SXin Li { 3, 5, 7 },
41*67e74705SXin Li { 4, 6, 8 },
42*67e74705SXin Li { 5 }, // expected-warning{{excess elements in array initializer}}
43*67e74705SXin Li };
44*67e74705SXin Li
45*67e74705SXin Li struct threeElements {
46*67e74705SXin Li int a,b,c;
47*67e74705SXin Li } z = { 1 };
48*67e74705SXin Li
49*67e74705SXin Li struct threeElements *p = 7; // expected-warning{{incompatible integer to pointer conversion initializing 'struct threeElements *' with an expression of type 'int'}}
50*67e74705SXin Li
51*67e74705SXin Li extern int blockScopeExtern[3] = { 1, 3, 5 }; // expected-error{{'extern' variable cannot have an initializer}}
52*67e74705SXin Li
53*67e74705SXin Li static long x2[3] = { 1.0,
54*67e74705SXin Li "abc", // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
55*67e74705SXin Li 5.8 }; // expected-warning {{implicit conversion from 'double' to 'long' changes value from 5.8 to 5}}
56*67e74705SXin Li }
57*67e74705SXin Li
test()58*67e74705SXin Li void test() {
59*67e74705SXin Li int y1[3] = {
60*67e74705SXin Li { 1, 2, 3 } // expected-warning{{excess elements in scalar initializer}}
61*67e74705SXin Li };
62*67e74705SXin Li int y3[4][3] = {
63*67e74705SXin Li { 1, 3, 5 },
64*67e74705SXin Li { 2, 4, 6 },
65*67e74705SXin Li { 3, 5, 7 },
66*67e74705SXin Li { 4, 6, 8 },
67*67e74705SXin Li { }, // expected-warning{{use of GNU empty initializer extension}} expected-warning{{excess elements in array initializer}}
68*67e74705SXin Li };
69*67e74705SXin Li int y4[4][3] = {
70*67e74705SXin Li { 1, 3, 5, 2 }, // expected-warning{{excess elements in array initializer}}
71*67e74705SXin Li { 4, 6 },
72*67e74705SXin Li { 3, 5, 7 },
73*67e74705SXin Li { 4, 6, 8 },
74*67e74705SXin Li };
75*67e74705SXin Li }
76*67e74705SXin Li
allLegalAndSynonymous()77*67e74705SXin Li void allLegalAndSynonymous() {
78*67e74705SXin Li short q[4][3][2] = {
79*67e74705SXin Li { 1 },
80*67e74705SXin Li { 2, 3 },
81*67e74705SXin Li { 4, 5, 6 }
82*67e74705SXin Li };
83*67e74705SXin Li short q2[4][3][2] = {
84*67e74705SXin Li { 1, 0, 0, 0, 0, 0 },
85*67e74705SXin Li { 2, 3, 0, 0, 0, 0 },
86*67e74705SXin Li { 4, 5, 6 }
87*67e74705SXin Li };
88*67e74705SXin Li short q3[4][3][2] = {
89*67e74705SXin Li {
90*67e74705SXin Li { 1 },
91*67e74705SXin Li },
92*67e74705SXin Li {
93*67e74705SXin Li { 2, 3 },
94*67e74705SXin Li },
95*67e74705SXin Li {
96*67e74705SXin Li { 4, 5 },
97*67e74705SXin Li { 6 },
98*67e74705SXin Li },
99*67e74705SXin Li };
100*67e74705SXin Li }
101*67e74705SXin Li
legal()102*67e74705SXin Li void legal() {
103*67e74705SXin Li short q[][3][2] = {
104*67e74705SXin Li { 1 },
105*67e74705SXin Li { 2, 3 },
106*67e74705SXin Li { 4, 5, 6 }
107*67e74705SXin Li };
108*67e74705SXin Li int q_sizecheck[(sizeof(q) / sizeof(short [3][2])) == 3? 1 : -1];
109*67e74705SXin Li }
110*67e74705SXin Li
111*67e74705SXin Li unsigned char asso_values[] = { 34 };
legal2()112*67e74705SXin Li int legal2() {
113*67e74705SXin Li return asso_values[0];
114*67e74705SXin Li }
115*67e74705SXin Li
illegal()116*67e74705SXin Li void illegal() {
117*67e74705SXin Li short q2[4][][2] = { // expected-error{{array has incomplete element type 'short [][2]'}}
118*67e74705SXin Li { 1, 0, 0, 0, 0, 0 },
119*67e74705SXin Li { 2, 3, 0, 0, 0, 0 },
120*67e74705SXin Li { 4, 5, 6 }
121*67e74705SXin Li };
122*67e74705SXin Li short q3[4][3][] = { // expected-error{{array has incomplete element type 'short []'}}
123*67e74705SXin Li {
124*67e74705SXin Li { 1 },
125*67e74705SXin Li },
126*67e74705SXin Li {
127*67e74705SXin Li { 2, 3 },
128*67e74705SXin Li },
129*67e74705SXin Li {
130*67e74705SXin Li { 4, 5 },
131*67e74705SXin Li { 6 },
132*67e74705SXin Li },
133*67e74705SXin Li };
134*67e74705SXin Li int a[][] = { 1, 2 }; // expected-error{{array has incomplete element type 'int []'}}
135*67e74705SXin Li }
136*67e74705SXin Li
137*67e74705SXin Li typedef int AryT[];
138*67e74705SXin Li
testTypedef()139*67e74705SXin Li void testTypedef()
140*67e74705SXin Li {
141*67e74705SXin Li AryT a = { 1, 2 }, b = { 3, 4, 5 };
142*67e74705SXin Li int a_sizecheck[(sizeof(a) / sizeof(int)) == 2? 1 : -1];
143*67e74705SXin Li int b_sizecheck[(sizeof(b) / sizeof(int)) == 3? 1 : -1];
144*67e74705SXin Li }
145*67e74705SXin Li
146*67e74705SXin Li static char const xx[] = "test";
147*67e74705SXin Li int xx_sizecheck[(sizeof(xx) / sizeof(char)) == 5? 1 : -1];
148*67e74705SXin Li static char const yy[5] = "test";
149*67e74705SXin Li static char const zz[3] = "test"; // expected-warning{{initializer-string for char array is too long}}
150*67e74705SXin Li
charArrays()151*67e74705SXin Li void charArrays() {
152*67e74705SXin Li static char const test[] = "test";
153*67e74705SXin Li int test_sizecheck[(sizeof(test) / sizeof(char)) == 5? 1 : -1];
154*67e74705SXin Li static char const test2[] = { "weird stuff" };
155*67e74705SXin Li static char const test3[] = { "test", "excess stuff" }; // expected-warning{{excess elements in char array initializer}}
156*67e74705SXin Li
157*67e74705SXin Li char* cp[] = { "Hello" };
158*67e74705SXin Li
159*67e74705SXin Li char c[] = { "Hello" };
160*67e74705SXin Li int l[sizeof(c) == 6 ? 1 : -1];
161*67e74705SXin Li
162*67e74705SXin Li int i[] = { "Hello "}; // expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [7]'}}
163*67e74705SXin Li char c2[] = { "Hello", "Good bye" }; //expected-warning{{excess elements in char array initializer}}
164*67e74705SXin Li
165*67e74705SXin Li int i2[1] = { "Hello" }; //expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [6]'}}
166*67e74705SXin Li char c3[5] = { "Hello" };
167*67e74705SXin Li char c4[4] = { "Hello" }; //expected-warning{{initializer-string for char array is too long}}
168*67e74705SXin Li
169*67e74705SXin Li int i3[] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
170*67e74705SXin Li }
171*67e74705SXin Li
variableArrayInit()172*67e74705SXin Li void variableArrayInit() {
173*67e74705SXin Li int a = 4;
174*67e74705SXin Li char strlit[a] = "foo"; //expected-error{{variable-sized object may not be initialized}}
175*67e74705SXin Li int b[a] = { 1, 2, 4 }; //expected-error{{variable-sized object may not be initialized}}
176*67e74705SXin Li }
177*67e74705SXin Li
178*67e74705SXin Li // Pure array tests
179*67e74705SXin Li float r1[10] = {{7}}; //expected-warning{{braces around scalar initializer}}
180*67e74705SXin Li float r2[] = {{8}}; //expected-warning{{braces around scalar initializer}}
181*67e74705SXin Li char r3[][5] = {1,2,3,4,5,6};
182*67e74705SXin Li int r3_sizecheck[(sizeof(r3) / sizeof(char[5])) == 2? 1 : -1];
183*67e74705SXin Li char r3_2[sizeof r3 == 10 ? 1 : -1];
184*67e74705SXin Li float r4[1][2] = {1,{2},3,4}; //expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}}
185*67e74705SXin Li char r5[][5] = {"aa", "bbb", "ccccc"};
186*67e74705SXin Li char r6[sizeof r5 == 15 ? 1 : -1];
187*67e74705SXin Li const char r7[] = "zxcv";
188*67e74705SXin Li char r8[5] = "5char";
189*67e74705SXin Li char r9[5] = "6chars"; //expected-warning{{initializer-string for char array is too long}}
190*67e74705SXin Li unsigned char r10[] = __extension__ (_Generic(0, int: (__extension__ "foo" )));
191*67e74705SXin Li int r11[0] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
192*67e74705SXin Li
193*67e74705SXin Li // Some struct tests
autoStructTest()194*67e74705SXin Li void autoStructTest() {
195*67e74705SXin Li struct s1 {char a; char b;} t1;
196*67e74705SXin Li struct s2 {struct s1 c;} t2 = { t1 };
197*67e74705SXin Li // The following is a less than great diagnostic (though it's on par with EDG).
198*67e74705SXin Li struct s1 t3[] = {t1, t1, "abc", 0}; //expected-warning{{incompatible pointer to integer conversion initializing 'char' with an expression of type 'char [4]'}}
199*67e74705SXin Li int t4[sizeof t3 == 6 ? 1 : -1];
200*67e74705SXin Li }
201*67e74705SXin Li struct foo { int z; } w;
bar(void)202*67e74705SXin Li int bar (void) {
203*67e74705SXin Li struct foo z = { w }; //expected-error{{initializing 'int' with an expression of incompatible type 'struct foo'}}
204*67e74705SXin Li return z.z;
205*67e74705SXin Li }
206*67e74705SXin Li struct s3 {void (*a)(void);} t5 = {autoStructTest};
207*67e74705SXin Li struct {int a; int b[];} t6 = {1, {1, 2, 3}}; // expected-warning{{flexible array initialization is a GNU extension}} \
208*67e74705SXin Li // expected-note{{initialized flexible array member 'b' is here}}
209*67e74705SXin Li union {char a; int b;} t7[] = {1, 2, 3};
210*67e74705SXin Li int t8[sizeof t7 == (3*sizeof(int)) ? 1 : -1];
211*67e74705SXin Li
212*67e74705SXin Li struct bittest{int : 31, a, :21, :12, b;};
213*67e74705SXin Li struct bittest bittestvar = {1, 2, 3, 4}; //expected-warning{{excess elements in struct initializer}}
214*67e74705SXin Li
215*67e74705SXin Li // Not completely sure what should happen here...
216*67e74705SXin Li int u1 = {}; //expected-warning{{use of GNU empty initializer extension}} expected-error{{scalar initializer cannot be empty}}
217*67e74705SXin Li int u2 = {{3}}; //expected-warning{{too many braces around scalar initializer}}
218*67e74705SXin Li
219*67e74705SXin Li // PR2362
varArray()220*67e74705SXin Li void varArray() {
221*67e74705SXin Li int c[][x] = { 0 }; //expected-error{{variable-sized object may not be initialized}}
222*67e74705SXin Li }
223*67e74705SXin Li
224*67e74705SXin Li // PR2151
emptyInit()225*67e74705SXin Li void emptyInit() {struct {} x[] = {6};} //expected-warning{{empty struct is a GNU extension}} \
226*67e74705SXin Li // expected-error{{initializer for aggregate with no elements}}
227*67e74705SXin Li
noNamedInit()228*67e74705SXin Li void noNamedInit() {
229*67e74705SXin Li struct {int:5;} x[] = {6}; //expected-error{{initializer for aggregate with no elements}} \
230*67e74705SXin Li // expected-warning {{struct without named members is a GNU extension}}
231*67e74705SXin Li }
232*67e74705SXin Li struct {int a; int:5;} noNamedImplicit[] = {1,2,3};
233*67e74705SXin Li int noNamedImplicitCheck[sizeof(noNamedImplicit) == 3 * sizeof(*noNamedImplicit) ? 1 : -1];
234*67e74705SXin Li
235*67e74705SXin Li
236*67e74705SXin Li // ptrs are constant
237*67e74705SXin Li struct soft_segment_descriptor {
238*67e74705SXin Li long ssd_base;
239*67e74705SXin Li };
240*67e74705SXin Li static int dblfault_tss;
241*67e74705SXin Li
242*67e74705SXin Li union uniao { int ola; } xpto[1];
243*67e74705SXin Li
244*67e74705SXin Li struct soft_segment_descriptor gdt_segs[] = {
245*67e74705SXin Li {(long) &dblfault_tss},
246*67e74705SXin Li { (long)xpto},
247*67e74705SXin Li };
248*67e74705SXin Li
249*67e74705SXin Li static void sppp_ipv6cp_up();
250*67e74705SXin Li const struct {} ipcp = { sppp_ipv6cp_up }; //expected-warning{{empty struct is a GNU extension}} \
251*67e74705SXin Li // expected-warning{{excess elements in struct initializer}}
252*67e74705SXin Li
253*67e74705SXin Li struct _Matrix { union { float m[4][4]; }; }; //expected-warning{{anonymous unions are a C11 extension}}
254*67e74705SXin Li typedef struct _Matrix Matrix;
test_matrix()255*67e74705SXin Li void test_matrix() {
256*67e74705SXin Li const Matrix mat1 = {
257*67e74705SXin Li { { 1.0f, 2.0f, 3.0f, 4.0f,
258*67e74705SXin Li 5.0f, 6.0f, 7.0f, 8.0f,
259*67e74705SXin Li 9.0f, 10.0f, 11.0f, 12.0f,
260*67e74705SXin Li 13.0f, 14.0f, 15.0f, 16.0f } }
261*67e74705SXin Li };
262*67e74705SXin Li
263*67e74705SXin Li const Matrix mat2 = {
264*67e74705SXin Li 1.0f, 2.0f, 3.0f, 4.0f,
265*67e74705SXin Li 5.0f, 6.0f, 7.0f, 8.0f,
266*67e74705SXin Li 9.0f, 10.0f, 11.0f, 12.0f,
267*67e74705SXin Li 13.0f, 14.0f, 15.0f, 16.0f
268*67e74705SXin Li };
269*67e74705SXin Li }
270*67e74705SXin Li
271*67e74705SXin Li char badchararray[1] = { badchararray[0], "asdf" }; // expected-warning {{excess elements in array initializer}} expected-error {{initializer element is not a compile-time constant}}
272*67e74705SXin Li
273*67e74705SXin Li // Test the GNU extension for initializing an array from an array
274*67e74705SXin Li // compound literal. PR9261.
275*67e74705SXin Li typedef int int5[5];
276*67e74705SXin Li int 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}}
277*67e74705SXin Li int 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}}
278*67e74705SXin Li int 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}}
279*67e74705SXin Li int 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}}
280*67e74705SXin Li int 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*67e74705SXin Li
282*67e74705SXin Li int a6[5] = (int[]){1, 2, 3}; // expected-error{{cannot initialize array of type 'int [5]' with array of type 'int [3]'}}
283*67e74705SXin Li
284*67e74705SXin Li int nonconst_value();
285*67e74705SXin Li int a7[5] = (int[5]){ 1,
286*67e74705SXin Li 2,
287*67e74705SXin Li 3,
288*67e74705SXin Li 4,
289*67e74705SXin Li nonconst_value() // expected-error{{initializer element is not a compile-time constant}}
290*67e74705SXin Li };
291*67e74705SXin Li
292*67e74705SXin Li // <rdar://problem/10636946>
293*67e74705SXin Li __attribute__((weak)) const unsigned int test10_bound = 10;
294*67e74705SXin Li char test10_global[test10_bound]; // expected-error {{variable length array declaration not allowed at file scope}}
test10()295*67e74705SXin Li void test10() {
296*67e74705SXin Li char test10_local[test10_bound] = "help"; // expected-error {{variable-sized object may not be initialized}}
297*67e74705SXin Li }
298