]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/Sema/array-init.c
Vendor import of clang release_34 branch r197841 (effectively, 3.4 RC3):
[FreeBSD/FreeBSD.git] / test / Sema / array-init.c
1 // RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
2 // RUN: %clang_cc1 -fsyntax-only -Wgnu -Wc11-extensions -verify %s
3 // REQUIRES: LP64
4
5 extern int foof() = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
6
7 static int x, y, z;
8
9 static int ary[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}
10 int ary2[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}
11
12 extern int fileScopeExtern[3] = { 1, 3, 5 }; // expected-warning{{'extern' variable has an initializer}}
13
14 static long ary3[] = { 1, "abc", 3, 4 }; // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
15
16 void func() {
17   int x = 1;
18
19   typedef int TInt = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
20
21   int xComputeSize[] = { 1, 3, 5 };
22
23   int x3[x] = { 1, 2 }; // expected-error{{variable-sized object may not be initialized}}
24
25   int x4 = { 1, 2 }; // expected-warning{{excess elements in scalar initializer}}
26
27   int y[4][3] = { 
28     { 1, 3, 5 },
29     { 2, 4, 6 },
30     { 3, 5, 7 },
31   };
32
33   int y2[4][3] = {
34     1, 3, 5, 2, 4, 6, 3, 5, 7
35   };
36
37   int y3[4][3] = {  
38     { 1, 3, 5 },
39     { 2, 4, 6 },
40     { 3, 5, 7 },
41     { 4, 6, 8 },
42     { 5 }, // expected-warning{{excess elements in array initializer}}
43   };
44
45   struct threeElements {
46     int a,b,c;
47   } z = { 1 };
48
49   struct threeElements *p = 7; // expected-warning{{incompatible integer to pointer conversion initializing 'struct threeElements *' with an expression of type 'int'}}
50   
51   extern int blockScopeExtern[3] = { 1, 3, 5 }; // expected-error{{'extern' variable cannot have an initializer}}
52   
53   static long x2[3] = { 1.0,
54                         "abc", // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
55                          5.8 }; // expected-warning {{implicit conversion from 'double' to 'long' changes value from 5.8 to 5}}
56 }
57
58 void test() {
59   int y1[3] = { 
60     { 1, 2, 3 } // expected-warning{{excess elements in scalar initializer}}
61   };
62   int y3[4][3] = {  
63     { 1, 3, 5 },
64     { 2, 4, 6 },
65     { 3, 5, 7 },
66     { 4, 6, 8 },
67     {  }, // expected-warning{{use of GNU empty initializer extension}} expected-warning{{excess elements in array initializer}}
68   };
69   int y4[4][3] = {  
70     { 1, 3, 5, 2 }, // expected-warning{{excess elements in array initializer}}
71     { 4, 6 },
72     { 3, 5, 7 },
73     { 4, 6, 8 },
74   };
75 }
76
77 void allLegalAndSynonymous() {
78   short q[4][3][2] = {
79     { 1 },
80     { 2, 3 },
81     { 4, 5, 6 }
82   };
83   short q2[4][3][2] = {
84     { 1, 0, 0, 0, 0, 0 },
85     { 2, 3, 0, 0, 0, 0 },
86     { 4, 5, 6 }
87   };
88   short q3[4][3][2] = {
89     { 
90       { 1 },
91     },
92     { 
93       { 2, 3 },
94     },
95     { 
96       { 4, 5 },
97       { 6 },
98     },
99   };
100 }
101
102 void legal() {
103   short q[][3][2] = {
104     { 1 },
105     { 2, 3 },
106     { 4, 5, 6 }
107   };
108   int q_sizecheck[(sizeof(q) / sizeof(short [3][2])) == 3? 1 : -1];
109 }
110
111 unsigned char asso_values[] = { 34 };
112 int legal2() { 
113   return asso_values[0]; 
114 }
115
116 void illegal() {
117   short q2[4][][2] = { // expected-error{{array has incomplete element type 'short [][2]'}}
118     { 1, 0, 0, 0, 0, 0 },
119     { 2, 3, 0, 0, 0, 0 },
120     { 4, 5, 6 }
121   };
122   short q3[4][3][] = { // expected-error{{array has incomplete element type 'short []'}}
123     { 
124       { 1 },
125     },
126     { 
127       { 2, 3 },
128     },
129     { 
130       { 4, 5 },
131       { 6 },
132     },
133   };
134   int a[][] = { 1, 2 }; // expected-error{{array has incomplete element type 'int []'}}
135 }
136
137 typedef int AryT[];
138
139 void testTypedef()
140 {
141   AryT a = { 1, 2 }, b = { 3, 4, 5 };
142   int a_sizecheck[(sizeof(a) / sizeof(int)) == 2? 1 : -1];
143   int b_sizecheck[(sizeof(b) / sizeof(int)) == 3? 1 : -1];
144 }
145
146 static char const xx[] = "test";
147 int xx_sizecheck[(sizeof(xx) / sizeof(char)) == 5? 1 : -1];
148 static char const yy[5] = "test";
149 static char const zz[3] = "test"; // expected-warning{{initializer-string for char array is too long}}
150
151 void charArrays() {
152   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
172 void 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
179 float r1[10] = {{7}}; //expected-warning{{braces around scalar initializer}}
180 float r2[] = {{8}}; //expected-warning{{braces around scalar initializer}}
181 char r3[][5] = {1,2,3,4,5,6};
182 int r3_sizecheck[(sizeof(r3) / sizeof(char[5])) == 2? 1 : -1];
183 char r3_2[sizeof r3 == 10 ? 1 : -1];
184 float r4[1][2] = {1,{2},3,4}; //expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}}
185 char r5[][5] = {"aa", "bbb", "ccccc"};
186 char r6[sizeof r5 == 15 ? 1 : -1];
187 const char r7[] = "zxcv";
188 char r8[5] = "5char";
189 char r9[5] = "6chars"; //expected-warning{{initializer-string for char array is too long}}
190 unsigned char r10[] = __extension__ (_Generic(0, int: (__extension__ "foo" )));
191 int r11[0] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
192
193 // Some struct tests
194 void autoStructTest() {
195 struct s1 {char a; char b;} t1;
196 struct s2 {struct s1 c;} t2 = { t1 };
197 // The following is a less than great diagnostic (though it's on par with EDG).
198 struct s1 t3[] = {t1, t1, "abc", 0}; //expected-warning{{incompatible pointer to integer conversion initializing 'char' with an expression of type 'char [4]'}}
199 int t4[sizeof t3 == 6 ? 1 : -1];
200 }
201 struct foo { int z; } w;
202 int bar (void) { 
203   struct foo z = { w }; //expected-error{{initializing 'int' with an expression of incompatible type 'struct foo'}}
204   return z.z; 
205
206 struct s3 {void (*a)(void);} t5 = {autoStructTest};
207 struct {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}}
209 union {char a; int b;} t7[] = {1, 2, 3};
210 int t8[sizeof t7 == (3*sizeof(int)) ? 1 : -1];
211
212 struct bittest{int : 31, a, :21, :12, b;};
213 struct bittest bittestvar = {1, 2, 3, 4}; //expected-warning{{excess elements in struct initializer}}
214
215 // Not completely sure what should happen here...
216 int u1 = {}; //expected-warning{{use of GNU empty initializer extension}} expected-error{{scalar initializer cannot be empty}}
217 int u2 = {{3}}; //expected-warning{{too many braces around scalar initializer}}
218
219 // PR2362
220 void varArray() {
221   int c[][x] = { 0 }; //expected-error{{variable-sized object may not be initialized}}
222 }
223
224 // PR2151
225 void emptyInit() {struct {} x[] = {6};} //expected-warning{{empty struct is a GNU extension}} \
226 // expected-error{{initializer for aggregate with no elements}}
227
228 void 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 }
232 struct {int a; int:5;} noNamedImplicit[] = {1,2,3};
233 int noNamedImplicitCheck[sizeof(noNamedImplicit) == 3 * sizeof(*noNamedImplicit) ? 1 : -1];
234
235
236 // ptrs are constant
237 struct soft_segment_descriptor {
238   long ssd_base;
239 };
240 static int dblfault_tss;
241
242 union uniao { int ola; } xpto[1];
243
244 struct soft_segment_descriptor gdt_segs[] = {
245   {(long) &dblfault_tss},
246   { (long)xpto},
247 };
248
249 static void sppp_ipv6cp_up();
250 const struct {} ipcp = { sppp_ipv6cp_up }; //expected-warning{{empty struct is a GNU extension}} \
251 // expected-warning{{excess elements in struct initializer}}
252
253 struct _Matrix { union { float m[4][4]; }; }; //expected-warning{{anonymous unions are a C11 extension}}
254 typedef struct _Matrix Matrix;
255 void 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
271 char 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.
275 typedef int int5[5];
276 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 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 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 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 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
282 int a6[5] = (int[]){1, 2, 3}; // expected-error{{cannot initialize array of type 'int [5]' with array of type 'int [3]'}}
283
284 int nonconst_value();
285 int a7[5] = (int[5]){ 1, 2, 3, 4, nonconst_value() }; // expected-error{{initializer element is not a compile-time constant}}
286
287 // <rdar://problem/10636946>
288 __attribute__((weak)) const unsigned int test10_bound = 10;
289 char test10_global[test10_bound]; // expected-error {{variable length array declaration not allowed at file scope}}
290 void test10() {
291   char test10_local[test10_bound] = "help"; // expected-error {{variable-sized object may not be initialized}}
292 }