1 // RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
3 extern int foof() = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
7 static int ary
[] = { x
, y
, z
}; // expected-error{{initializer element is not a compile-time constant}}
8 int ary2
[] = { x
, y
, z
}; // expected-error{{initializer element is not a compile-time constant}}
10 extern int fileScopeExtern
[3] = { 1, 3, 5 }; // expected-warning{{'extern' variable has an initializer}}
12 static long ary3
[] = { 1, "abc", 3, 4 }; // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
17 typedef int TInt
= 1; // expected-error{{illegal initializer (only variables can be initialized)}}
19 int xComputeSize
[] = { 1, 3, 5 };
21 int x3
[x
] = { 1, 2 }; // expected-error{{variable-sized object may not be initialized}}
23 int x4
= { 1, 2 }; // expected-warning{{excess elements in scalar initializer}}
32 1, 3, 5, 2, 4, 6, 3, 5, 7
40 { 5 }, // expected-warning{{excess elements in array initializer}}
43 struct threeElements
{
47 struct threeElements
*p
= 7; // expected-warning{{incompatible integer to pointer conversion initializing 'struct threeElements *' with an expression of type 'int'}}
49 extern int blockScopeExtern
[3] = { 1, 3, 5 }; // expected-error{{'extern' variable cannot have an initializer}}
51 static long x2
[3] = { 1.0, "abc" , 5.8 }; // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
56 { 1, 2, 3 } // expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in scalar initializer}}
63 { }, // expected-warning{{use of GNU empty initializer extension}} expected-warning{{excess elements in array initializer}}
66 { 1, 3, 5, 2 }, // expected-warning{{excess elements in array initializer}}
73 void allLegalAndSynonymous() {
104 int q_sizecheck
[(sizeof(q
) / sizeof(short [3][2])) == 3? 1 : -1];
107 unsigned char asso_values
[] = { 34 };
109 return asso_values
[0];
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 },
118 short q3
[4][3][] = { // expected-error{{array has incomplete element type 'short []'}}
130 int a
[][] = { 1, 2 }; // expected-error{{array has incomplete element type 'int []'}}
137 AryT a
= { 1, 2 }, b
= { 3, 4, 5 };
138 int a_sizecheck
[(sizeof(a
) / sizeof(int)) == 2? 1 : -1];
139 int b_sizecheck
[(sizeof(b
) / sizeof(int)) == 3? 1 : -1];
142 static char const xx
[] = "test";
143 int xx_sizecheck
[(sizeof(xx
) / sizeof(char)) == 5? 1 : -1];
144 static char const yy
[5] = "test";
145 static char const zz
[3] = "test"; // expected-warning{{initializer-string for char array is too long}}
148 static char const test
[] = "test";
149 int test_sizecheck
[(sizeof(test
) / sizeof(char)) == 5? 1 : -1];
150 static char const test2
[] = { "weird stuff" };
151 static char const test3
[] = { "test", "excess stuff" }; // expected-warning{{excess elements in char array initializer}}
153 char* cp
[] = { "Hello" };
155 char c
[] = { "Hello" };
156 int l
[sizeof(c
) == 6 ? 1 : -1];
158 int i
[] = { "Hello "}; // expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [7]'}}
159 char c2
[] = { "Hello", "Good bye" }; //expected-warning{{excess elements in char array initializer}}
161 int i2
[1] = { "Hello" }; //expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [6]'}}
162 char c3
[5] = { "Hello" };
163 char c4
[4] = { "Hello" }; //expected-warning{{initializer-string for char array is too long}}
165 int i3
[] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
168 void variableArrayInit() {
170 char strlit
[a
] = "foo"; //expected-error{{array initializer must be an initializer list or string literal}}
171 int b
[a
] = { 1, 2, 4 }; //expected-error{{variable-sized object may not be initialized}}
175 float r1
[10] = {{7}}; //expected-warning{{braces around scalar initializer}}
176 float r2
[] = {{8}}; //expected-warning{{braces around scalar initializer}}
177 char r3
[][5] = {1,2,3,4,5,6};
178 int r3_sizecheck
[(sizeof(r3
) / sizeof(char[5])) == 2? 1 : -1];
179 char r3_2
[sizeof r3
== 10 ? 1 : -1];
180 float r4
[1][2] = {1,{2},3,4}; //expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}}
181 char r5
[][5] = {"aa", "bbb", "ccccc"};
182 char r6
[sizeof r5
== 15 ? 1 : -1];
183 const char r7
[] = "zxcv";
184 char r8
[5] = "5char";
185 char r9
[5] = "6chars"; //expected-warning{{initializer-string for char array is too long}}
187 int r11
[0] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
190 void autoStructTest() {
191 struct s1
{char a
; char b
;} t1
;
192 struct s2
{struct s1 c
;} t2
= { t1
};
193 // The following is a less than great diagnostic (though it's on par with EDG).
194 struct s1 t3
[] = {t1
, t1
, "abc", 0}; //expected-warning{{incompatible pointer to integer conversion initializing 'char' with an expression of type 'char [4]'}}
195 int t4
[sizeof t3
== 6 ? 1 : -1];
197 struct foo
{ int z
; } w
;
199 struct foo z
= { w
}; //expected-error{{initializing 'int' with an expression of incompatible type 'struct foo'}}
202 struct s3
{void (*a
)(void);} t5
= {autoStructTest
};
203 struct {int a
; int b
[];} t6
= {1, {1, 2, 3}}; // expected-warning{{flexible array initialization is a GNU extension}} \
204 // expected-note{{initialized flexible array member 'b' is here}}
205 union {char a
; int b
;} t7
[] = {1, 2, 3};
206 int t8
[sizeof t7
== (3*sizeof(int)) ? 1 : -1];
208 struct bittest
{int : 31, a
, :21, :12, b
;};
209 struct bittest bittestvar
= {1, 2, 3, 4}; //expected-warning{{excess elements in struct initializer}}
211 // Not completely sure what should happen here...
212 int u1
= {}; //expected-warning{{use of GNU empty initializer extension}} expected-error{{scalar initializer cannot be empty}}
213 int u2
= {{3}}; //expected-warning{{too many braces around scalar initializer}}
217 int c
[][x
] = { 0 }; //expected-error{{variable-sized object may not be initialized}}
221 void emptyInit() {struct {} x
[] = {6};} //expected-warning{{empty struct (accepted as an extension) has size 0 in C, size 1 in C++}} \
222 // expected-error{{initializer for aggregate with no elements}}
225 struct {int:5;} x
[] = {6}; //expected-error{{initializer for aggregate with no elements}}
227 struct {int a
; int:5;} noNamedImplicit
[] = {1,2,3};
228 int noNamedImplicitCheck
[sizeof(noNamedImplicit
) == 3 * sizeof(*noNamedImplicit
) ? 1 : -1];
232 struct soft_segment_descriptor
{
235 static int dblfault_tss
;
237 union uniao
{ int ola
; } xpto
[1];
239 struct soft_segment_descriptor gdt_segs
[] = {
240 {(long) &dblfault_tss
},
244 static void sppp_ipv6cp_up();
245 const struct {} ipcp
= { sppp_ipv6cp_up
}; //expected-warning{{empty struct (accepted as an extension) has size 0 in C, size 1 in C++}} \
246 // expected-warning{{excess elements in struct initializer}}
248 struct _Matrix
{ union { float m
[4][4]; }; }; //expected-warning{{anonymous unions are a GNU extension in C}}
249 typedef struct _Matrix Matrix
;
251 const Matrix mat1
= {
252 { { 1.0f
, 2.0f
, 3.0f
, 4.0f
,
253 5.0f
, 6.0f
, 7.0f
, 8.0f
,
254 9.0f
, 10.0f
, 11.0f
, 12.0f
,
255 13.0f
, 14.0f
, 15.0f
, 16.0f
} }
258 const Matrix mat2
= {
259 1.0f
, 2.0f
, 3.0f
, 4.0f
,
260 5.0f
, 6.0f
, 7.0f
, 8.0f
,
261 9.0f
, 10.0f
, 11.0f
, 12.0f
,
262 13.0f
, 14.0f
, 15.0f
, 16.0f
266 char badchararray
[1] = { badchararray
[0], "asdf" }; // expected-warning {{excess elements in array initializer}} expected-error {{initializer element is not a compile-time constant}}