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