1 // RUN: %clang_cc1 -std=gnu99 -fsyntax-only -pedantic -verify=expected,pedantic %s
2 // RUN: %clang_cc1 -std=gnu99 -fsyntax-only -Wgnu -Wc11-extensions -verify %s
5 extern int foof(void) = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
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}}
12 extern int fileScopeExtern
[3] = { 1, 3, 5 }; // expected-warning{{'extern' variable has an initializer}}
14 static long ary3
[] = { 1, "abc", 3, 4 }; // expected-error{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char[4]'}}
19 typedef int TInt
= 1; // expected-error{{illegal initializer (only variables can be initialized)}}
21 int xComputeSize
[] = { 1, 3, 5 };
23 int x3
[x
] = { 1, 2 }; // expected-error{{variable-sized object may not be initialized}}
25 int x4
= { 1, 2 }; // expected-warning{{excess elements in scalar initializer}}
34 1, 3, 5, 2, 4, 6, 3, 5, 7
42 { 5 }, // expected-warning{{excess elements in array initializer}}
45 struct threeElements
{
49 struct threeElements
*p
= 7; // expected-error{{incompatible integer to pointer conversion initializing 'struct threeElements *' with an expression of type 'int'}}
51 extern int blockScopeExtern
[3] = { 1, 3, 5 }; // expected-error{{declaration of block scope identifier with linkage cannot have an initializer}}
53 static long x2
[3] = { 1.0,
54 "abc", // expected-error{{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}}
60 { 1, 2, 3 } // expected-warning{{excess elements in scalar initializer}}
67 { }, // pedantic-warning{{use of an empty initializer is a C23 extension}} expected-warning{{excess elements in array initializer}}
70 { 1, 3, 5, 2 }, // expected-warning{{excess elements in array initializer}}
77 void allLegalAndSynonymous(void) {
108 int q_sizecheck
[(sizeof(q
) / sizeof(short [3][2])) == 3? 1 : -1];
111 unsigned char asso_values
[] = { 34 };
113 return asso_values
[0];
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 },
122 short q3
[4][3][] = { // expected-error{{array has incomplete element type 'short[]'}}
134 int a
[][] = { 1, 2 }; // expected-error{{array has incomplete element type 'int[]'}}
139 void testTypedef(void)
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];
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}}
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
156 void charArrays(void) {
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
166 char* cp
[] = { "Hello" };
168 char c
[] = { "Hello" };
169 int l
[sizeof(c
) == 6 ? 1 : -1];
171 int i
[] = { "Hello "}; // expected-error{{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}}
174 int i2
[1] = { "Hello" }; //expected-error{{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}}
178 int i3
[] = {}; //expected-warning{{zero size arrays are an extension}} pedantic-warning{{use of an empty initializer is a C23 extension}}
181 void variableArrayInit(void) {
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}}
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}} pedantic-warning{{use of an empty initializer is a C23 extension}}
203 void autoStructTest(void) {
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-error{{incompatible pointer to integer conversion initializing 'char' with an expression of type 'char[4]'}}
208 int t4
[sizeof t3
== 6 ? 1 : -1];
210 struct foo
{ int z
; } w
;
212 struct foo z
= { w
}; //expected-error{{initializing 'int' with an expression of incompatible type 'struct foo'}}
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];
221 struct bittest
{int : 31, a
, :21, :12, b
;};
222 struct bittest bittestvar
= {1, 2, 3, 4}; //expected-warning{{excess elements in struct initializer}}
224 int u1
= {}; //pedantic-warning{{use of an empty initializer is a C23 extension}}
225 int u2
= {{3}}; //expected-warning{{too many braces around scalar initializer}}
228 void varArray(void) {
229 int c
[][x
] = { 0 }; //expected-error{{variable-sized object may not be initialized}}
233 void emptyInit(void) {struct {} x
[] = {6};} //expected-warning{{empty struct is a GNU extension}} \
234 // expected-error{{initializer for aggregate with no elements}}
236 void noNamedInit(void) {
237 struct {int:5;} x
[] = {6}; //expected-error{{initializer for aggregate with no elements}} \
238 // expected-warning {{struct without named members is a GNU extension}}
240 struct {int a
; int:5;} noNamedImplicit
[] = {1,2,3};
241 int noNamedImplicitCheck
[sizeof(noNamedImplicit
) == 3 * sizeof(*noNamedImplicit
) ? 1 : -1];
245 struct soft_segment_descriptor
{
248 static int dblfault_tss
;
250 union uniao
{ int ola
; } xpto
[1];
252 struct soft_segment_descriptor gdt_segs
[] = {
253 {(long) &dblfault_tss
},
257 static void sppp_ipv6cp_up(void);
258 const struct {} ipcp
= { sppp_ipv6cp_up
}; //expected-warning{{empty struct is a GNU extension}} \
259 // expected-warning{{excess elements in struct initializer}}
261 struct _Matrix
{ union { float m
[4][4]; }; }; //expected-warning{{anonymous unions are a C11 extension}}
262 typedef struct _Matrix Matrix
;
263 void test_matrix(void) {
264 const Matrix mat1
= {
265 { { 1.0f
, 2.0f
, 3.0f
, 4.0f
,
266 5.0f
, 6.0f
, 7.0f
, 8.0f
,
267 9.0f
, 10.0f
, 11.0f
, 12.0f
,
268 13.0f
, 14.0f
, 15.0f
, 16.0f
} }
271 const Matrix mat2
= {
272 1.0f
, 2.0f
, 3.0f
, 4.0f
,
273 5.0f
, 6.0f
, 7.0f
, 8.0f
,
274 9.0f
, 10.0f
, 11.0f
, 12.0f
,
275 13.0f
, 14.0f
, 15.0f
, 16.0f
279 char badchararray
[1] = { badchararray
[0], "asdf" }; // expected-warning {{excess elements in array initializer}} expected-error {{initializer element is not a compile-time constant}}
281 // Test the GNU extension for initializing an array from an array
282 // compound literal. PR9261.
284 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}}
285 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}}
286 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}}
287 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}}
288 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 int a6
[5] = (int[]){1, 2, 3}; // expected-error{{cannot initialize array of type 'int[5]' with array of type 'int[3]'}}
292 int nonconst_value(void);
293 int a7
[5] = (int[5]){ 1,
297 nonconst_value() // expected-error{{initializer element is not a compile-time constant}}
300 __attribute__((weak
)) const unsigned int test10_bound
= 10;
301 char test10_global
[test10_bound
]; // expected-error {{variable length array declaration not allowed at file scope}}
303 char test10_local
[test10_bound
] = "help"; // expected-error {{variable-sized object may not be initialized}}