blob: 26c0b2418221d3372a01d48d4cc0c5755213ad92 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
Steve Narofff0090632007-09-02 02:04:30 +00002
Steve Naroff410e3e22007-09-12 20:13:48 +00003extern int foof() = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
4
Steve Naroff6f9f3072007-09-02 15:34:30 +00005static int x, y, z;
6
Chris Lattnerd8803632008-08-10 01:58:45 +00007static int ary[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}
8int ary2[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}
Steve Naroff6f9f3072007-09-02 15:34:30 +00009
10extern int fileScopeExtern[3] = { 1, 3, 5 }; // expected-warning{{'extern' variable has an initializer}}
11
Douglas Gregor08a41902010-04-09 17:53:29 +000012static 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 +000013
Steve Narofff0090632007-09-02 02:04:30 +000014void func() {
15 int x = 1;
16
Steve Naroff410e3e22007-09-12 20:13:48 +000017 typedef int TInt = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
18
Steve Naroffd35005e2007-09-03 01:24:23 +000019 int xComputeSize[] = { 1, 3, 5 };
Steve Narofff0090632007-09-02 02:04:30 +000020
Steve Naroff38374b02007-09-02 20:30:18 +000021 int x3[x] = { 1, 2 }; // expected-error{{variable-sized object may not be initialized}}
Steve Narofff0090632007-09-02 02:04:30 +000022
Eli Friedman759f2522009-05-16 11:45:48 +000023 int x4 = { 1, 2 }; // expected-warning{{excess elements in scalar initializer}}
Steve Narofff0090632007-09-02 02:04:30 +000024
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
Steve Naroffd35005e2007-09-03 01:24:23 +000035 int y3[4][3] = {
36 { 1, 3, 5 },
37 { 2, 4, 6 },
38 { 3, 5, 7 },
39 { 4, 6, 8 },
Douglas Gregor7c53ca62009-02-18 22:23:55 +000040 { 5 }, // expected-warning{{excess elements in array initializer}}
Steve Naroffd35005e2007-09-03 01:24:23 +000041 };
42
Steve Narofff0090632007-09-02 02:04:30 +000043 struct threeElements {
44 int a,b,c;
45 } z = { 1 };
46
Douglas Gregor08a41902010-04-09 17:53:29 +000047 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 +000048
49 extern int blockScopeExtern[3] = { 1, 3, 5 }; // expected-error{{'extern' variable cannot have an initializer}}
Steve Naroff38374b02007-09-02 20:30:18 +000050
David Blaikiee31b8fb2012-04-05 00:16:44 +000051 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 turns literal floating-point number into integer}}
Steve Narofff0090632007-09-02 02:04:30 +000054}
Steve Naroff371227d2007-09-04 02:20:04 +000055
56void test() {
57 int y1[3] = {
Eli Friedman0c706c22011-09-19 23:17:44 +000058 { 1, 2, 3 } // expected-warning{{excess elements in scalar initializer}}
Steve Naroff371227d2007-09-04 02:20:04 +000059 };
60 int y3[4][3] = {
61 { 1, 3, 5 },
62 { 2, 4, 6 },
63 { 3, 5, 7 },
64 { 4, 6, 8 },
Douglas Gregor7c53ca62009-02-18 22:23:55 +000065 { }, // expected-warning{{use of GNU empty initializer extension}} expected-warning{{excess elements in array initializer}}
Steve Naroff371227d2007-09-04 02:20:04 +000066 };
67 int y4[4][3] = {
Douglas Gregor7c53ca62009-02-18 22:23:55 +000068 { 1, 3, 5, 2 }, // expected-warning{{excess elements in array initializer}}
Steve Naroff371227d2007-09-04 02:20:04 +000069 { 4, 6 },
70 { 3, 5, 7 },
Steve Naroffa9960332008-01-25 00:51:06 +000071 { 4, 6, 8 },
Steve Naroff371227d2007-09-04 02:20:04 +000072 };
73}
74
75void 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
100void legal() {
101 short q[][3][2] = {
102 { 1 },
103 { 2, 3 },
104 { 4, 5, 6 }
105 };
Douglas Gregor4c678342009-01-28 21:54:33 +0000106 int q_sizecheck[(sizeof(q) / sizeof(short [3][2])) == 3? 1 : -1];
Steve Naroff371227d2007-09-04 02:20:04 +0000107}
108
Steve Narofffd8b4a42007-10-18 03:27:23 +0000109unsigned char asso_values[] = { 34 };
110int legal2() {
111 return asso_values[0];
112}
113
Steve Naroff371227d2007-09-04 02:20:04 +0000114void 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 };
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000132 int a[][] = { 1, 2 }; // expected-error{{array has incomplete element type 'int []'}}
Steve Naroff371227d2007-09-04 02:20:04 +0000133}
134
135typedef int AryT[];
136
137void testTypedef()
138{
139 AryT a = { 1, 2 }, b = { 3, 4, 5 };
Douglas Gregor4c678342009-01-28 21:54:33 +0000140 int a_sizecheck[(sizeof(a) / sizeof(int)) == 2? 1 : -1];
141 int b_sizecheck[(sizeof(b) / sizeof(int)) == 3? 1 : -1];
Steve Naroff371227d2007-09-04 02:20:04 +0000142}
143
Steve Naroff2fdc3742007-12-10 22:44:33 +0000144static char const xx[] = "test";
Douglas Gregor4c678342009-01-28 21:54:33 +0000145int xx_sizecheck[(sizeof(xx) / sizeof(char)) == 5? 1 : -1];
Steve Naroff2fdc3742007-12-10 22:44:33 +0000146static char const yy[5] = "test";
147static char const zz[3] = "test"; // expected-warning{{initializer-string for char array is too long}}
148
Mike Stump1eb44332009-09-09 15:08:12 +0000149void 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}}
Steve Naroff2fdc3742007-12-10 22:44:33 +0000154
155 char* cp[] = { "Hello" };
156
157 char c[] = { "Hello" };
158 int l[sizeof(c) == 6 ? 1 : -1];
159
Douglas Gregor08a41902010-04-09 17:53:29 +0000160 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 +0000161 char c2[] = { "Hello", "Good bye" }; //expected-warning{{excess elements in char array initializer}}
Steve Naroff2fdc3742007-12-10 22:44:33 +0000162
Douglas Gregor08a41902010-04-09 17:53:29 +0000163 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 +0000164 char c3[5] = { "Hello" };
165 char c4[4] = { "Hello" }; //expected-warning{{initializer-string for char array is too long}}
166
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000167 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 +0000168}
169
Steve Naroffca107302008-01-21 23:53:58 +0000170void variableArrayInit() {
171 int a = 4;
John McCall73076432012-01-05 00:13:19 +0000172 char strlit[a] = "foo"; //expected-error{{variable-sized object may not be initialized}}
Steve Naroffca107302008-01-21 23:53:58 +0000173 int b[a] = { 1, 2, 4 }; //expected-error{{variable-sized object may not be initialized}}
174}
Steve Naroffa9960332008-01-25 00:51:06 +0000175
176// Pure array tests
177float r1[10] = {{7}}; //expected-warning{{braces around scalar initializer}}
178float r2[] = {{8}}; //expected-warning{{braces around scalar initializer}}
179char r3[][5] = {1,2,3,4,5,6};
Douglas Gregor4c678342009-01-28 21:54:33 +0000180int r3_sizecheck[(sizeof(r3) / sizeof(char[5])) == 2? 1 : -1];
Steve Naroffa9960332008-01-25 00:51:06 +0000181char r3_2[sizeof r3 == 10 ? 1 : -1];
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000182float 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 +0000183char r5[][5] = {"aa", "bbb", "ccccc"};
184char r6[sizeof r5 == 15 ? 1 : -1];
185const char r7[] = "zxcv";
186char r8[5] = "5char";
187char r9[5] = "6chars"; //expected-warning{{initializer-string for char array is too long}}
188
189int r11[0] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
190
191// Some struct tests
192void autoStructTest() {
193struct s1 {char a; char b;} t1;
194struct s2 {struct s1 c;} t2 = { t1 };
195// The following is a less than great diagnostic (though it's on par with EDG).
Douglas Gregor08a41902010-04-09 17:53:29 +0000196struct 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 +0000197int t4[sizeof t3 == 6 ? 1 : -1];
198}
Steve Naroff578edc62008-01-28 02:00:41 +0000199struct foo { int z; } w;
200int bar (void) {
Douglas Gregor08a41902010-04-09 17:53:29 +0000201 struct foo z = { w }; //expected-error{{initializing 'int' with an expression of incompatible type 'struct foo'}}
Steve Naroff578edc62008-01-28 02:00:41 +0000202 return z.z;
203}
Steve Naroffa9960332008-01-25 00:51:06 +0000204struct s3 {void (*a)(void);} t5 = {autoStructTest};
Douglas Gregora6457962009-03-20 00:32:56 +0000205struct {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}}
Steve Naroffa9960332008-01-25 00:51:06 +0000207union {char a; int b;} t7[] = {1, 2, 3};
208int t8[sizeof t7 == (3*sizeof(int)) ? 1 : -1];
209
210struct bittest{int : 31, a, :21, :12, b;};
Douglas Gregor7c53ca62009-02-18 22:23:55 +0000211struct bittest bittestvar = {1, 2, 3, 4}; //expected-warning{{excess elements in struct initializer}}
Steve Naroffa9960332008-01-25 00:51:06 +0000212
213// Not completely sure what should happen here...
Eli Friedmanc56c9772008-05-19 20:29:35 +0000214int u1 = {}; //expected-warning{{use of GNU empty initializer extension}} expected-error{{scalar initializer cannot be empty}}
Eli Friedman09865a92010-08-14 03:14:53 +0000215int u2 = {{3}}; //expected-warning{{too many braces around scalar initializer}}
Steve Naroffa9960332008-01-25 00:51:06 +0000216
Eli Friedman638e1442008-05-25 13:22:35 +0000217// PR2362
218void varArray() {
219 int c[][x] = { 0 }; //expected-error{{variable-sized object may not be initialized}}
220}
Eli Friedman402256f2008-05-25 13:49:22 +0000221
222// PR2151
Richard Smithd7c56e12011-12-29 21:57:33 +0000223void emptyInit() {struct {} x[] = {6};} //expected-warning{{empty struct is a GNU extension}} \
Douglas Gregor03332962010-07-29 14:29:34 +0000224// expected-error{{initializer for aggregate with no elements}}
Eli Friedman402256f2008-05-25 13:49:22 +0000225
Mike Stumpd1969d82009-07-22 00:43:08 +0000226void noNamedInit() {
227 struct {int:5;} x[] = {6}; //expected-error{{initializer for aggregate with no elements}}
Eli Friedmanf84eda32008-05-25 14:03:31 +0000228}
229struct {int a; int:5;} noNamedImplicit[] = {1,2,3};
230int noNamedImplicitCheck[sizeof(noNamedImplicit) == 3 * sizeof(*noNamedImplicit) ? 1 : -1];
231
Nuno Lopes62b6a652008-09-01 22:28:55 +0000232
233// ptrs are constant
234struct soft_segment_descriptor {
Mike Stump1eb44332009-09-09 15:08:12 +0000235 long ssd_base;
Nuno Lopes62b6a652008-09-01 22:28:55 +0000236};
237static int dblfault_tss;
238
239union uniao { int ola; } xpto[1];
240
241struct soft_segment_descriptor gdt_segs[] = {
Mike Stump1eb44332009-09-09 15:08:12 +0000242 {(long) &dblfault_tss},
243 { (long)xpto},
Nuno Lopes62b6a652008-09-01 22:28:55 +0000244};
245
Nuno Lopes73419bf2008-09-01 18:42:41 +0000246static void sppp_ipv6cp_up();
Richard Smithd7c56e12011-12-29 21:57:33 +0000247const struct {} ipcp = { sppp_ipv6cp_up }; //expected-warning{{empty struct is a GNU extension}} \
Douglas Gregor03332962010-07-29 14:29:34 +0000248// expected-warning{{excess elements in struct initializer}}
Douglas Gregor4c678342009-01-28 21:54:33 +0000249
Hans Wennborgacbabf12012-02-03 15:47:04 +0000250struct _Matrix { union { float m[4][4]; }; }; //expected-warning{{anonymous unions are a C11 extension}}
Douglas Gregor4c678342009-01-28 21:54:33 +0000251typedef struct _Matrix Matrix;
252void 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}
Eli Friedmane5408582009-05-29 20:20:05 +0000267
268char 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 +0000269
270// Test the GNU extension for initializing an array from an array
271// compound literal. PR9261.
272typedef int int5[5];
273int 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}}
274int 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}}
275int 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}}
276int 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}}
277int 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
279int a6[5] = (int[]){1, 2, 3}; // expected-error{{cannot initialize array of type 'int [5]' with array of type 'int [3]'}}
280
281int nonconst_value();
282int 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 +0000283
284// <rdar://problem/10636946>
285__attribute__((weak)) const unsigned int test10_bound = 10;
286char test10_global[test10_bound]; // expected-error {{variable length array declaration not allowed at file scope}}
287void test10() {
288 char test10_local[test10_bound] = "help"; // expected-error {{variable-sized object may not be initialized}}
289}