blob: 2c835bd54f2e357a9df3aff191e6351197b2cbc5 [file] [log] [blame]
Douglas Gregor4c678342009-01-28 21:54:33 +00001// RUN: clang -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
Sebastian Redl1367ede2008-11-12 21:19:11 +000012static long ary3[] = { 1, "abc", 3, 4 }; // expected-warning{{incompatible pointer to integer conversion initializing 'char [4]', expected 'long'}}
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
Douglas Gregoreeb15d42009-02-04 22:46:25 +000023 int x4 = { 1, 2 }; // expected-warning{{braces around scalar initializer}} expected-error{{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 Gregorb574e562009-01-30 22:26:29 +000040 { 5 }, // expected-error{{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
Chris Lattnerb7b61152008-01-04 18:22:42 +000047 struct threeElements *p = 7; // expected-warning{{incompatible integer to pointer conversion initializing 'int', expected 'struct threeElements *'}}
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
Sebastian Redl1367ede2008-11-12 21:19:11 +000051 static long x2[3] = { 1.0, "abc" , 5.8 }; // expected-warning{{incompatible pointer to integer conversion initializing 'char [4]', expected 'long'}}
Steve Narofff0090632007-09-02 02:04:30 +000052}
Steve Naroff371227d2007-09-04 02:20:04 +000053
54void test() {
55 int y1[3] = {
Douglas Gregoreeb15d42009-02-04 22:46:25 +000056 { 1, 2, 3 } // expected-warning{{braces around scalar initializer}} expected-error{{excess elements in scalar initializer}}
Steve Naroff371227d2007-09-04 02:20:04 +000057 };
58 int y3[4][3] = {
59 { 1, 3, 5 },
60 { 2, 4, 6 },
61 { 3, 5, 7 },
62 { 4, 6, 8 },
Douglas Gregorb574e562009-01-30 22:26:29 +000063 { }, // expected-warning{{use of GNU empty initializer extension}} expected-error{{excess elements in array initializer}}
Steve Naroff371227d2007-09-04 02:20:04 +000064 };
65 int y4[4][3] = {
Douglas Gregorb574e562009-01-30 22:26:29 +000066 { 1, 3, 5, 2 }, // expected-error{{excess elements in array initializer}}
Steve Naroff371227d2007-09-04 02:20:04 +000067 { 4, 6 },
68 { 3, 5, 7 },
Steve Naroffa9960332008-01-25 00:51:06 +000069 { 4, 6, 8 },
Steve Naroff371227d2007-09-04 02:20:04 +000070 };
71}
72
73void allLegalAndSynonymous() {
74 short q[4][3][2] = {
75 { 1 },
76 { 2, 3 },
77 { 4, 5, 6 }
78 };
79 short q2[4][3][2] = {
80 { 1, 0, 0, 0, 0, 0 },
81 { 2, 3, 0, 0, 0, 0 },
82 { 4, 5, 6 }
83 };
84 short q3[4][3][2] = {
85 {
86 { 1 },
87 },
88 {
89 { 2, 3 },
90 },
91 {
92 { 4, 5 },
93 { 6 },
94 },
95 };
96}
97
98void legal() {
99 short q[][3][2] = {
100 { 1 },
101 { 2, 3 },
102 { 4, 5, 6 }
103 };
Douglas Gregor4c678342009-01-28 21:54:33 +0000104 int q_sizecheck[(sizeof(q) / sizeof(short [3][2])) == 3? 1 : -1];
Steve Naroff371227d2007-09-04 02:20:04 +0000105}
106
Steve Narofffd8b4a42007-10-18 03:27:23 +0000107unsigned char asso_values[] = { 34 };
108int legal2() {
109 return asso_values[0];
110}
111
Steve Naroff371227d2007-09-04 02:20:04 +0000112void illegal() {
113 short q2[4][][2] = { // expected-error{{array has incomplete element type 'short [][2]'}}
114 { 1, 0, 0, 0, 0, 0 },
115 { 2, 3, 0, 0, 0, 0 },
116 { 4, 5, 6 }
117 };
118 short q3[4][3][] = { // expected-error{{array has incomplete element type 'short []'}}
119 {
120 { 1 },
121 },
122 {
123 { 2, 3 },
124 },
125 {
126 { 4, 5 },
127 { 6 },
128 },
129 };
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000130 int a[][] = { 1, 2 }; // expected-error{{array has incomplete element type 'int []'}}
Steve Naroff371227d2007-09-04 02:20:04 +0000131}
132
133typedef int AryT[];
134
135void testTypedef()
136{
137 AryT a = { 1, 2 }, b = { 3, 4, 5 };
Douglas Gregor4c678342009-01-28 21:54:33 +0000138 int a_sizecheck[(sizeof(a) / sizeof(int)) == 2? 1 : -1];
139 int b_sizecheck[(sizeof(b) / sizeof(int)) == 3? 1 : -1];
Steve Naroff371227d2007-09-04 02:20:04 +0000140}
141
Steve Naroff2fdc3742007-12-10 22:44:33 +0000142static char const xx[] = "test";
Douglas Gregor4c678342009-01-28 21:54:33 +0000143int xx_sizecheck[(sizeof(xx) / sizeof(char)) == 5? 1 : -1];
Steve Naroff2fdc3742007-12-10 22:44:33 +0000144static char const yy[5] = "test";
145static char const zz[3] = "test"; // expected-warning{{initializer-string for char array is too long}}
146
147void charArrays()
148{
149 static char const test[] = "test";
Douglas Gregor4c678342009-01-28 21:54:33 +0000150 int test_sizecheck[(sizeof(test) / sizeof(char)) == 5? 1 : -1];
Steve Naroff2fdc3742007-12-10 22:44:33 +0000151 static char const test2[] = { "weird stuff" };
152 static char const test3[] = { "test", "excess stuff" }; // expected-error{{excess elements in char array initializer}}
153
154 char* cp[] = { "Hello" };
155
156 char c[] = { "Hello" };
157 int l[sizeof(c) == 6 ? 1 : -1];
158
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000159 int i[] = { "Hello "}; // expected-warning{{incompatible pointer to integer conversion initializing 'char [7]', expected 'int'}}
Steve Naroff2fdc3742007-12-10 22:44:33 +0000160 char c2[] = { "Hello", "Good bye" }; //expected-error{{excess elements in char array initializer}}
161
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000162 int i2[1] = { "Hello" }; //expected-warning{{incompatible pointer to integer conversion initializing 'char [6]', expected 'int'}}
Steve Naroff2fdc3742007-12-10 22:44:33 +0000163 char c3[5] = { "Hello" };
164 char c4[4] = { "Hello" }; //expected-warning{{initializer-string for char array is too long}}
165
Daniel Dunbar396f0bf2008-08-18 20:28:46 +0000166 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 +0000167}
168
Steve Naroffca107302008-01-21 23:53:58 +0000169void variableArrayInit() {
170 int a = 4;
171 char strlit[a] = "foo"; //expected-error{{variable-sized object may not be initialized}}
172 int b[a] = { 1, 2, 4 }; //expected-error{{variable-sized object may not be initialized}}
173}
Steve Naroffa9960332008-01-25 00:51:06 +0000174
175// Pure array tests
176float r1[10] = {{7}}; //expected-warning{{braces around scalar initializer}}
177float r2[] = {{8}}; //expected-warning{{braces around scalar initializer}}
178char r3[][5] = {1,2,3,4,5,6};
Douglas Gregor4c678342009-01-28 21:54:33 +0000179int r3_sizecheck[(sizeof(r3) / sizeof(char[5])) == 2? 1 : -1];
Steve Naroffa9960332008-01-25 00:51:06 +0000180char r3_2[sizeof r3 == 10 ? 1 : -1];
Douglas Gregorb574e562009-01-30 22:26:29 +0000181float r4[1][2] = {1,{2},3,4}; //expected-warning{{braces around scalar initializer}} expected-error{{excess elements in array initializer}}
Steve Naroffa9960332008-01-25 00:51:06 +0000182char r5[][5] = {"aa", "bbb", "ccccc"};
183char r6[sizeof r5 == 15 ? 1 : -1];
184const char r7[] = "zxcv";
185char r8[5] = "5char";
186char r9[5] = "6chars"; //expected-warning{{initializer-string for char array is too long}}
187
188int r11[0] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
189
190// Some struct tests
191void autoStructTest() {
192struct s1 {char a; char b;} t1;
193struct s2 {struct s1 c;} t2 = { t1 };
194// The following is a less than great diagnostic (though it's on par with EDG).
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000195struct s1 t3[] = {t1, t1, "abc", 0}; //expected-warning{{incompatible pointer to integer conversion initializing 'char [4]', expected 'char'}}
Steve Naroffa9960332008-01-25 00:51:06 +0000196int t4[sizeof t3 == 6 ? 1 : -1];
197}
Steve Naroff578edc62008-01-28 02:00:41 +0000198struct foo { int z; } w;
199int bar (void) {
200 struct foo z = { w }; //expected-error{{incompatible type initializing 'struct foo', expected 'int'}}
201 return z.z;
202}
Steve Naroffa9960332008-01-25 00:51:06 +0000203struct s3 {void (*a)(void);} t5 = {autoStructTest};
Steve Naroffa9960332008-01-25 00:51:06 +0000204// Note that clang objc implementation depends on this extension.
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000205struct {int a; int b[];} t6 = {1, {1, 2, 3}};
Steve Naroffa9960332008-01-25 00:51:06 +0000206union {char a; int b;} t7[] = {1, 2, 3};
207int t8[sizeof t7 == (3*sizeof(int)) ? 1 : -1];
208
209struct bittest{int : 31, a, :21, :12, b;};
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000210struct bittest bittestvar = {1, 2, 3, 4}; //expected-error{{excess elements in struct initializer}}
Steve Naroffa9960332008-01-25 00:51:06 +0000211
212// Not completely sure what should happen here...
Eli Friedmanc56c9772008-05-19 20:29:35 +0000213int u1 = {}; //expected-warning{{use of GNU empty initializer extension}} expected-error{{scalar initializer cannot be empty}}
214int u2 = {{3}}; //expected-error{{too many braces around scalar initializer}}
Steve Naroffa9960332008-01-25 00:51:06 +0000215
Eli Friedman638e1442008-05-25 13:22:35 +0000216// PR2362
217void varArray() {
218 int c[][x] = { 0 }; //expected-error{{variable-sized object may not be initialized}}
219}
Eli Friedman402256f2008-05-25 13:49:22 +0000220
221// PR2151
222int emptyInit() {struct {} x[] = {6};} //expected-warning{{empty struct extension}} expected-error{{initializer for aggregate with no elements}}
223
Eli Friedmanf84eda32008-05-25 14:03:31 +0000224int noNamedInit() {
225struct {int:5;} x[] = {6}; //expected-error{{initializer for aggregate with no elements}}
226}
227struct {int a; int:5;} noNamedImplicit[] = {1,2,3};
228int noNamedImplicitCheck[sizeof(noNamedImplicit) == 3 * sizeof(*noNamedImplicit) ? 1 : -1];
229
Nuno Lopes62b6a652008-09-01 22:28:55 +0000230
231// ptrs are constant
232struct soft_segment_descriptor {
Sebastian Redl1367ede2008-11-12 21:19:11 +0000233 long ssd_base;
Nuno Lopes62b6a652008-09-01 22:28:55 +0000234};
235static int dblfault_tss;
236
237union uniao { int ola; } xpto[1];
238
239struct soft_segment_descriptor gdt_segs[] = {
Sebastian Redl1367ede2008-11-12 21:19:11 +0000240 {(long) &dblfault_tss},
241 { (long)xpto},
Nuno Lopes62b6a652008-09-01 22:28:55 +0000242};
243
Nuno Lopes73419bf2008-09-01 18:42:41 +0000244static void sppp_ipv6cp_up();
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000245const struct {} ipcp = { sppp_ipv6cp_up }; //expected-warning{{empty struct extension}} expected-error{{excess elements in struct initializer}}
Douglas Gregor4c678342009-01-28 21:54:33 +0000246
247struct _Matrix { union { float m[4][4]; }; }; //expected-warning{{anonymous unions are a GNU extension in C}}
248typedef struct _Matrix Matrix;
249void test_matrix() {
250 const Matrix mat1 = {
251 { { 1.0f, 2.0f, 3.0f, 4.0f,
252 5.0f, 6.0f, 7.0f, 8.0f,
253 9.0f, 10.0f, 11.0f, 12.0f,
254 13.0f, 14.0f, 15.0f, 16.0f } }
255 };
256
257 const Matrix mat2 = {
258 1.0f, 2.0f, 3.0f, 4.0f,
259 5.0f, 6.0f, 7.0f, 8.0f,
260 9.0f, 10.0f, 11.0f, 12.0f,
261 13.0f, 14.0f, 15.0f, 16.0f
262 };
263}