1 // RUN: %clang_cc1 -std=gnu99 -fsyntax-only -pedantic -verify %s
2 // RUN: %clang_cc1 -std=gnu99 -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
func()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
test()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
allLegalAndSynonymous()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
legal()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 };
legal2()112 int legal2() {
113 return asso_values[0];
114 }
115
illegal()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
testTypedef()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 #pragma clang diagnostic push
152 #pragma clang diagnostic ignored "-Wexcess-initializers"
153 static char const zz_quiet[3] = "test";
154 #pragma clang diagnostic pop
155
charArrays()156 void charArrays() {
157 static char const test[] = "test";
158 int test_sizecheck[(sizeof(test) / sizeof(char)) == 5? 1 : -1];
159 static char const test2[] = { "weird stuff" };
160 static char const test3[] = { "test", "excess stuff" }; // expected-warning{{excess elements in char array initializer}}
161 #pragma clang diagnostic push
162 #pragma clang diagnostic ignored "-Wexcess-initializers"
163 static char const test3_quiet[] = {"test", "excess stuff"};
164 #pragma clang diagnostic pop
165
166 char* cp[] = { "Hello" };
167
168 char c[] = { "Hello" };
169 int l[sizeof(c) == 6 ? 1 : -1];
170
171 int i[] = { "Hello "}; // expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [7]'}}
172 char c2[] = { "Hello", "Good bye" }; //expected-warning{{excess elements in char array initializer}}
173
174 int i2[1] = { "Hello" }; //expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [6]'}}
175 char c3[5] = { "Hello" };
176 char c4[4] = { "Hello" }; //expected-warning{{initializer-string for char array is too long}}
177
178 int i3[] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
179 }
180
variableArrayInit()181 void variableArrayInit() {
182 int a = 4;
183 char strlit[a] = "foo"; //expected-error{{variable-sized object may not be initialized}}
184 int b[a] = { 1, 2, 4 }; //expected-error{{variable-sized object may not be initialized}}
185 }
186
187 // Pure array tests
188 float r1[10] = {{7}}; //expected-warning{{braces around scalar initializer}}
189 float r2[] = {{8}}; //expected-warning{{braces around scalar initializer}}
190 char r3[][5] = {1,2,3,4,5,6};
191 int r3_sizecheck[(sizeof(r3) / sizeof(char[5])) == 2? 1 : -1];
192 char r3_2[sizeof r3 == 10 ? 1 : -1];
193 float r4[1][2] = {1,{2},3,4}; //expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}}
194 char r5[][5] = {"aa", "bbb", "ccccc"};
195 char r6[sizeof r5 == 15 ? 1 : -1];
196 const char r7[] = "zxcv";
197 char r8[5] = "5char";
198 char r9[5] = "6chars"; //expected-warning{{initializer-string for char array is too long}}
199 unsigned char r10[] = __extension__ (_Generic(0, int: (__extension__ "foo" )));
200 int r11[0] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
201
202 // Some struct tests
autoStructTest()203 void autoStructTest() {
204 struct s1 {char a; char b;} t1;
205 struct s2 {struct s1 c;} t2 = { t1 };
206 // The following is a less than great diagnostic (though it's on par with EDG).
207 struct s1 t3[] = {t1, t1, "abc", 0}; //expected-warning{{incompatible pointer to integer conversion initializing 'char' with an expression of type 'char [4]'}}
208 int t4[sizeof t3 == 6 ? 1 : -1];
209 }
210 struct foo { int z; } w;
bar(void)211 int bar (void) {
212 struct foo z = { w }; //expected-error{{initializing 'int' with an expression of incompatible type 'struct foo'}}
213 return z.z;
214 }
215 struct s3 {void (*a)(void);} t5 = {autoStructTest};
216 struct {int a; int b[];} t6 = {1, {1, 2, 3}}; // expected-warning{{flexible array initialization is a GNU extension}} \
217 // expected-note{{initialized flexible array member 'b' is here}}
218 union {char a; int b;} t7[] = {1, 2, 3};
219 int t8[sizeof t7 == (3*sizeof(int)) ? 1 : -1];
220
221 struct bittest{int : 31, a, :21, :12, b;};
222 struct bittest bittestvar = {1, 2, 3, 4}; //expected-warning{{excess elements in struct initializer}}
223
224 // Not completely sure what should happen here...
225 int u1 = {}; //expected-warning{{use of GNU empty initializer extension}} expected-error{{scalar initializer cannot be empty}}
226 int u2 = {{3}}; //expected-warning{{too many braces around scalar initializer}}
227
228 // PR2362
varArray()229 void varArray() {
230 int c[][x] = { 0 }; //expected-error{{variable-sized object may not be initialized}}
231 }
232
233 // PR2151
emptyInit()234 void emptyInit() {struct {} x[] = {6};} //expected-warning{{empty struct is a GNU extension}} \
235 // expected-error{{initializer for aggregate with no elements}}
236
noNamedInit()237 void noNamedInit() {
238 struct {int:5;} x[] = {6}; //expected-error{{initializer for aggregate with no elements}} \
239 // expected-warning {{struct without named members is a GNU extension}}
240 }
241 struct {int a; int:5;} noNamedImplicit[] = {1,2,3};
242 int noNamedImplicitCheck[sizeof(noNamedImplicit) == 3 * sizeof(*noNamedImplicit) ? 1 : -1];
243
244
245 // ptrs are constant
246 struct soft_segment_descriptor {
247 long ssd_base;
248 };
249 static int dblfault_tss;
250
251 union uniao { int ola; } xpto[1];
252
253 struct soft_segment_descriptor gdt_segs[] = {
254 {(long) &dblfault_tss},
255 { (long)xpto},
256 };
257
258 static void sppp_ipv6cp_up();
259 const struct {} ipcp = { sppp_ipv6cp_up }; //expected-warning{{empty struct is a GNU extension}} \
260 // expected-warning{{excess elements in struct initializer}}
261
262 struct _Matrix { union { float m[4][4]; }; }; //expected-warning{{anonymous unions are a C11 extension}}
263 typedef struct _Matrix Matrix;
test_matrix()264 void test_matrix() {
265 const Matrix mat1 = {
266 { { 1.0f, 2.0f, 3.0f, 4.0f,
267 5.0f, 6.0f, 7.0f, 8.0f,
268 9.0f, 10.0f, 11.0f, 12.0f,
269 13.0f, 14.0f, 15.0f, 16.0f } }
270 };
271
272 const Matrix mat2 = {
273 1.0f, 2.0f, 3.0f, 4.0f,
274 5.0f, 6.0f, 7.0f, 8.0f,
275 9.0f, 10.0f, 11.0f, 12.0f,
276 13.0f, 14.0f, 15.0f, 16.0f
277 };
278 }
279
280 char badchararray[1] = { badchararray[0], "asdf" }; // expected-warning {{excess elements in array initializer}} expected-error {{initializer element is not a compile-time constant}}
281
282 // Test the GNU extension for initializing an array from an array
283 // compound literal. PR9261.
284 typedef int int5[5];
285 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}}
286 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}}
287 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}}
288 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}}
289 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}}
290
291 int a6[5] = (int[]){1, 2, 3}; // expected-error{{cannot initialize array of type 'int [5]' with array of type 'int [3]'}}
292
293 int nonconst_value();
294 int a7[5] = (int[5]){ 1,
295 2,
296 3,
297 4,
298 nonconst_value() // expected-error{{initializer element is not a compile-time constant}}
299 };
300
301 // <rdar://problem/10636946>
302 __attribute__((weak)) const unsigned int test10_bound = 10;
303 char test10_global[test10_bound]; // expected-error {{variable length array declaration not allowed at file scope}}
test10()304 void test10() {
305 char test10_local[test10_bound] = "help"; // expected-error {{variable-sized object may not be initialized}}
306 }
307