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