blob: b3cae60495c7d5762381c0dc73c3f74846fe0084 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
Richard Smith561e3c62012-09-20 01:02:05 +00002// RUN: %clang_cc1 -fsyntax-only -Wgnu -Wc11-extensions -verify %s
NAKAMURA Takumib774d732012-09-12 10:45:40 +00003// REQUIRES: LP64
Steve Narofff0090632007-09-02 02:04:30 +00004
Steve Naroff410e3e22007-09-12 20:13:48 +00005extern int foof() = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
6
Steve Naroff6f9f3072007-09-02 15:34:30 +00007static int x, y, z;
8
Chris Lattnerd8803632008-08-10 01:58:45 +00009static int ary[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}
10int ary2[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}
Steve Naroff6f9f3072007-09-02 15:34:30 +000011
12extern int fileScopeExtern[3] = { 1, 3, 5 }; // expected-warning{{'extern' variable has an initializer}}
13
Douglas Gregor08a41902010-04-09 17:53:29 +000014static long ary3[] = { 1, "abc", 3, 4 }; // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
Steve Naroff38374b02007-09-02 20:30:18 +000015
Steve Narofff0090632007-09-02 02:04:30 +000016void func() {
17 int x = 1;
18
Steve Naroff410e3e22007-09-12 20:13:48 +000019 typedef int TInt = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
20
Steve Naroffd35005e2007-09-03 01:24:23 +000021 int xComputeSize[] = { 1, 3, 5 };
Steve Narofff0090632007-09-02 02:04:30 +000022
Steve Naroff38374b02007-09-02 20:30:18 +000023 int x3[x] = { 1, 2 }; // expected-error{{variable-sized object may not be initialized}}
Steve Narofff0090632007-09-02 02:04:30 +000024
Eli Friedman759f2522009-05-16 11:45:48 +000025 int x4 = { 1, 2 }; // expected-warning{{excess elements in scalar initializer}}
Steve Narofff0090632007-09-02 02:04:30 +000026
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
Steve Naroffd35005e2007-09-03 01:24:23 +000037 int y3[4][3] = {
38 { 1, 3, 5 },
39 { 2, 4, 6 },
40 { 3, 5, 7 },
41 { 4, 6, 8 },
Douglas Gregor7c53ca62009-02-18 22:23:55 +000042 { 5 }, // expected-warning{{excess elements in array initializer}}
Steve Naroffd35005e2007-09-03 01:24:23 +000043 };
44
Steve Narofff0090632007-09-02 02:04:30 +000045 struct threeElements {
46 int a,b,c;
47 } z = { 1 };
48
Douglas Gregor08a41902010-04-09 17:53:29 +000049 struct threeElements *p = 7; // expected-warning{{incompatible integer to pointer conversion initializing 'struct threeElements *' with an expression of type 'int'}}
Steve Naroff6f9f3072007-09-02 15:34:30 +000050
51 extern int blockScopeExtern[3] = { 1, 3, 5 }; // expected-error{{'extern' variable cannot have an initializer}}
Steve Naroff38374b02007-09-02 20:30:18 +000052
David Blaikiee31b8fb2012-04-05 00:16:44 +000053 static long x2[3] = { 1.0,
54 "abc", // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
David Blaikiebe0ee872012-05-15 16:56:36 +000055 5.8 }; // expected-warning {{implicit conversion from 'double' to 'long' changes value from 5.8 to 5}}
Steve Narofff0090632007-09-02 02:04:30 +000056}
Steve Naroff371227d2007-09-04 02:20:04 +000057
58void test() {
59 int y1[3] = {
Eli Friedman0c706c22011-09-19 23:17:44 +000060 { 1, 2, 3 } // expected-warning{{excess elements in scalar initializer}}
Steve Naroff371227d2007-09-04 02:20:04 +000061 };
62 int y3[4][3] = {
63 { 1, 3, 5 },
64 { 2, 4, 6 },
65 { 3, 5, 7 },
66 { 4, 6, 8 },
Douglas Gregor7c53ca62009-02-18 22:23:55 +000067 { }, // expected-warning{{use of GNU empty initializer extension}} expected-warning{{excess elements in array initializer}}
Steve Naroff371227d2007-09-04 02:20:04 +000068 };
69 int y4[4][3] = {
Douglas Gregor7c53ca62009-02-18 22:23:55 +000070 { 1, 3, 5, 2 }, // expected-warning{{excess elements in array initializer}}
Steve Naroff371227d2007-09-04 02:20:04 +000071 { 4, 6 },
72 { 3, 5, 7 },
Steve Naroffa9960332008-01-25 00:51:06 +000073 { 4, 6, 8 },
Steve Naroff371227d2007-09-04 02:20:04 +000074 };
75}
76
77void 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
102void legal() {
103 short q[][3][2] = {
104 { 1 },
105 { 2, 3 },
106 { 4, 5, 6 }
107 };
Douglas Gregor4c678342009-01-28 21:54:33 +0000108 int q_sizecheck[(sizeof(q) / sizeof(short [3][2])) == 3? 1 : -1];
Steve Naroff371227d2007-09-04 02:20:04 +0000109}
110
Steve Narofffd8b4a42007-10-18 03:27:23 +0000111unsigned char asso_values[] = { 34 };
112int legal2() {
113 return asso_values[0];
114}
115
Steve Naroff371227d2007-09-04 02:20:04 +0000116void 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 };
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000134 int a[][] = { 1, 2 }; // expected-error{{array has incomplete element type 'int []'}}
Steve Naroff371227d2007-09-04 02:20:04 +0000135}
136
137typedef int AryT[];
138
139void testTypedef()
140{
141 AryT a = { 1, 2 }, b = { 3, 4, 5 };
Douglas Gregor4c678342009-01-28 21:54:33 +0000142 int a_sizecheck[(sizeof(a) / sizeof(int)) == 2? 1 : -1];
143 int b_sizecheck[(sizeof(b) / sizeof(int)) == 3? 1 : -1];
Steve Naroff371227d2007-09-04 02:20:04 +0000144}
145
Steve Naroff2fdc3742007-12-10 22:44:33 +0000146static char const xx[] = "test";
Douglas Gregor4c678342009-01-28 21:54:33 +0000147int xx_sizecheck[(sizeof(xx) / sizeof(char)) == 5? 1 : -1];
Steve Naroff2fdc3742007-12-10 22:44:33 +0000148static char const yy[5] = "test";
149static char const zz[3] = "test"; // expected-warning{{initializer-string for char array is too long}}
150
Mike Stump1eb44332009-09-09 15:08:12 +0000151void 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}}
Steve Naroff2fdc3742007-12-10 22:44:33 +0000156
157 char* cp[] = { "Hello" };
158
159 char c[] = { "Hello" };
160 int l[sizeof(c) == 6 ? 1 : -1];
161
Douglas Gregor08a41902010-04-09 17:53:29 +0000162 int i[] = { "Hello "}; // expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [7]'}}
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000163 char c2[] = { "Hello", "Good bye" }; //expected-warning{{excess elements in char array initializer}}
Steve Naroff2fdc3742007-12-10 22:44:33 +0000164
Douglas Gregor08a41902010-04-09 17:53:29 +0000165 int i2[1] = { "Hello" }; //expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [6]'}}
Steve Naroff2fdc3742007-12-10 22:44:33 +0000166 char c3[5] = { "Hello" };
167 char c4[4] = { "Hello" }; //expected-warning{{initializer-string for char array is too long}}
168
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000169 int i3[] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
Steve Naroff2fdc3742007-12-10 22:44:33 +0000170}
171
Steve Naroffca107302008-01-21 23:53:58 +0000172void variableArrayInit() {
173 int a = 4;
John McCall73076432012-01-05 00:13:19 +0000174 char strlit[a] = "foo"; //expected-error{{variable-sized object may not be initialized}}
Steve Naroffca107302008-01-21 23:53:58 +0000175 int b[a] = { 1, 2, 4 }; //expected-error{{variable-sized object may not be initialized}}
176}
Steve Naroffa9960332008-01-25 00:51:06 +0000177
178// Pure array tests
179float r1[10] = {{7}}; //expected-warning{{braces around scalar initializer}}
180float r2[] = {{8}}; //expected-warning{{braces around scalar initializer}}
181char r3[][5] = {1,2,3,4,5,6};
Douglas Gregor4c678342009-01-28 21:54:33 +0000182int r3_sizecheck[(sizeof(r3) / sizeof(char[5])) == 2? 1 : -1];
Steve Naroffa9960332008-01-25 00:51:06 +0000183char r3_2[sizeof r3 == 10 ? 1 : -1];
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000184float r4[1][2] = {1,{2},3,4}; //expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}}
Steve Naroffa9960332008-01-25 00:51:06 +0000185char r5[][5] = {"aa", "bbb", "ccccc"};
186char r6[sizeof r5 == 15 ? 1 : -1];
187const char r7[] = "zxcv";
188char r8[5] = "5char";
189char r9[5] = "6chars"; //expected-warning{{initializer-string for char array is too long}}
190
191int r11[0] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
192
193// Some struct tests
194void autoStructTest() {
195struct s1 {char a; char b;} t1;
196struct s2 {struct s1 c;} t2 = { t1 };
197// The following is a less than great diagnostic (though it's on par with EDG).
Douglas Gregor08a41902010-04-09 17:53:29 +0000198struct s1 t3[] = {t1, t1, "abc", 0}; //expected-warning{{incompatible pointer to integer conversion initializing 'char' with an expression of type 'char [4]'}}
Steve Naroffa9960332008-01-25 00:51:06 +0000199int t4[sizeof t3 == 6 ? 1 : -1];
200}
Steve Naroff578edc62008-01-28 02:00:41 +0000201struct foo { int z; } w;
202int bar (void) {
Douglas Gregor08a41902010-04-09 17:53:29 +0000203 struct foo z = { w }; //expected-error{{initializing 'int' with an expression of incompatible type 'struct foo'}}
Steve Naroff578edc62008-01-28 02:00:41 +0000204 return z.z;
205}
Steve Naroffa9960332008-01-25 00:51:06 +0000206struct s3 {void (*a)(void);} t5 = {autoStructTest};
Douglas Gregora6457962009-03-20 00:32:56 +0000207struct {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}}
Steve Naroffa9960332008-01-25 00:51:06 +0000209union {char a; int b;} t7[] = {1, 2, 3};
210int t8[sizeof t7 == (3*sizeof(int)) ? 1 : -1];
211
212struct bittest{int : 31, a, :21, :12, b;};
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000213struct bittest bittestvar = {1, 2, 3, 4}; //expected-warning{{excess elements in struct initializer}}
Steve Naroffa9960332008-01-25 00:51:06 +0000214
215// Not completely sure what should happen here...
Eli Friedmanc56c9772008-05-19 20:29:35 +0000216int u1 = {}; //expected-warning{{use of GNU empty initializer extension}} expected-error{{scalar initializer cannot be empty}}
Eli Friedman09865a92010-08-14 03:14:53 +0000217int u2 = {{3}}; //expected-warning{{too many braces around scalar initializer}}
Steve Naroffa9960332008-01-25 00:51:06 +0000218
Eli Friedman638e1442008-05-25 13:22:35 +0000219// PR2362
220void varArray() {
221 int c[][x] = { 0 }; //expected-error{{variable-sized object may not be initialized}}
222}
Eli Friedman402256f2008-05-25 13:49:22 +0000223
224// PR2151
Richard Smithd7c56e12011-12-29 21:57:33 +0000225void emptyInit() {struct {} x[] = {6};} //expected-warning{{empty struct is a GNU extension}} \
Douglas Gregor03332962010-07-29 14:29:34 +0000226// expected-error{{initializer for aggregate with no elements}}
Eli Friedman402256f2008-05-25 13:49:22 +0000227
Mike Stumpd1969d82009-07-22 00:43:08 +0000228void noNamedInit() {
229 struct {int:5;} x[] = {6}; //expected-error{{initializer for aggregate with no elements}}
Eli Friedmanf84eda32008-05-25 14:03:31 +0000230}
231struct {int a; int:5;} noNamedImplicit[] = {1,2,3};
232int noNamedImplicitCheck[sizeof(noNamedImplicit) == 3 * sizeof(*noNamedImplicit) ? 1 : -1];
233
Nuno Lopes62b6a652008-09-01 22:28:55 +0000234
235// ptrs are constant
236struct soft_segment_descriptor {
Mike Stump1eb44332009-09-09 15:08:12 +0000237 long ssd_base;
Nuno Lopes62b6a652008-09-01 22:28:55 +0000238};
239static int dblfault_tss;
240
241union uniao { int ola; } xpto[1];
242
243struct soft_segment_descriptor gdt_segs[] = {
Mike Stump1eb44332009-09-09 15:08:12 +0000244 {(long) &dblfault_tss},
245 { (long)xpto},
Nuno Lopes62b6a652008-09-01 22:28:55 +0000246};
247
Nuno Lopes73419bf2008-09-01 18:42:41 +0000248static void sppp_ipv6cp_up();
Richard Smithd7c56e12011-12-29 21:57:33 +0000249const struct {} ipcp = { sppp_ipv6cp_up }; //expected-warning{{empty struct is a GNU extension}} \
Douglas Gregor03332962010-07-29 14:29:34 +0000250// expected-warning{{excess elements in struct initializer}}
Douglas Gregor4c678342009-01-28 21:54:33 +0000251
Hans Wennborgacbabf12012-02-03 15:47:04 +0000252struct _Matrix { union { float m[4][4]; }; }; //expected-warning{{anonymous unions are a C11 extension}}
Douglas Gregor4c678342009-01-28 21:54:33 +0000253typedef struct _Matrix Matrix;
254void test_matrix() {
255 const Matrix mat1 = {
256 { { 1.0f, 2.0f, 3.0f, 4.0f,
257 5.0f, 6.0f, 7.0f, 8.0f,
258 9.0f, 10.0f, 11.0f, 12.0f,
259 13.0f, 14.0f, 15.0f, 16.0f } }
260 };
261
262 const Matrix mat2 = {
263 1.0f, 2.0f, 3.0f, 4.0f,
264 5.0f, 6.0f, 7.0f, 8.0f,
265 9.0f, 10.0f, 11.0f, 12.0f,
266 13.0f, 14.0f, 15.0f, 16.0f
267 };
268}
Eli Friedmane5408582009-05-29 20:20:05 +0000269
270char badchararray[1] = { badchararray[0], "asdf" }; // expected-warning {{excess elements in array initializer}} expected-error {{initializer element is not a compile-time constant}}
Douglas Gregorcd9ec3b2011-02-22 18:29:51 +0000271
272// Test the GNU extension for initializing an array from an array
273// compound literal. PR9261.
274typedef int int5[5];
275int 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}}
276int 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}}
277int 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}}
278int 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}}
279int 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}}
280
281int a6[5] = (int[]){1, 2, 3}; // expected-error{{cannot initialize array of type 'int [5]' with array of type 'int [3]'}}
282
283int nonconst_value();
284int a7[5] = (int[5]){ 1, 2, 3, 4, nonconst_value() }; // expected-error{{initializer element is not a compile-time constant}}
John McCall73076432012-01-05 00:13:19 +0000285
286// <rdar://problem/10636946>
287__attribute__((weak)) const unsigned int test10_bound = 10;
288char test10_global[test10_bound]; // expected-error {{variable length array declaration not allowed at file scope}}
289void test10() {
290 char test10_local[test10_bound] = "help"; // expected-error {{variable-sized object may not be initialized}}
291}