xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/array-init.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -std=gnu99 -fsyntax-only -pedantic -verify %s
2*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -std=gnu99 -fsyntax-only -Wgnu -Wc11-extensions -verify %s
3f4a2713aSLionel Sambuc // REQUIRES: LP64
4f4a2713aSLionel Sambuc 
5f4a2713aSLionel Sambuc extern int foof() = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
6f4a2713aSLionel Sambuc 
7f4a2713aSLionel Sambuc static int x, y, z;
8f4a2713aSLionel Sambuc 
9f4a2713aSLionel Sambuc static int ary[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}
10f4a2713aSLionel Sambuc int ary2[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}
11f4a2713aSLionel Sambuc 
12f4a2713aSLionel Sambuc extern int fileScopeExtern[3] = { 1, 3, 5 }; // expected-warning{{'extern' variable has an initializer}}
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc static long ary3[] = { 1, "abc", 3, 4 }; // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
15f4a2713aSLionel Sambuc 
func()16f4a2713aSLionel Sambuc void func() {
17f4a2713aSLionel Sambuc   int x = 1;
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc   typedef int TInt = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
20f4a2713aSLionel Sambuc 
21f4a2713aSLionel Sambuc   int xComputeSize[] = { 1, 3, 5 };
22f4a2713aSLionel Sambuc 
23f4a2713aSLionel Sambuc   int x3[x] = { 1, 2 }; // expected-error{{variable-sized object may not be initialized}}
24f4a2713aSLionel Sambuc 
25f4a2713aSLionel Sambuc   int x4 = { 1, 2 }; // expected-warning{{excess elements in scalar initializer}}
26f4a2713aSLionel Sambuc 
27f4a2713aSLionel Sambuc   int y[4][3] = {
28f4a2713aSLionel Sambuc     { 1, 3, 5 },
29f4a2713aSLionel Sambuc     { 2, 4, 6 },
30f4a2713aSLionel Sambuc     { 3, 5, 7 },
31f4a2713aSLionel Sambuc   };
32f4a2713aSLionel Sambuc 
33f4a2713aSLionel Sambuc   int y2[4][3] = {
34f4a2713aSLionel Sambuc     1, 3, 5, 2, 4, 6, 3, 5, 7
35f4a2713aSLionel Sambuc   };
36f4a2713aSLionel Sambuc 
37f4a2713aSLionel Sambuc   int y3[4][3] = {
38f4a2713aSLionel Sambuc     { 1, 3, 5 },
39f4a2713aSLionel Sambuc     { 2, 4, 6 },
40f4a2713aSLionel Sambuc     { 3, 5, 7 },
41f4a2713aSLionel Sambuc     { 4, 6, 8 },
42f4a2713aSLionel Sambuc     { 5 }, // expected-warning{{excess elements in array initializer}}
43f4a2713aSLionel Sambuc   };
44f4a2713aSLionel Sambuc 
45f4a2713aSLionel Sambuc   struct threeElements {
46f4a2713aSLionel Sambuc     int a,b,c;
47f4a2713aSLionel Sambuc   } z = { 1 };
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc   struct threeElements *p = 7; // expected-warning{{incompatible integer to pointer conversion initializing 'struct threeElements *' with an expression of type 'int'}}
50f4a2713aSLionel Sambuc 
51f4a2713aSLionel Sambuc   extern int blockScopeExtern[3] = { 1, 3, 5 }; // expected-error{{'extern' variable cannot have an initializer}}
52f4a2713aSLionel Sambuc 
53f4a2713aSLionel Sambuc   static long x2[3] = { 1.0,
54f4a2713aSLionel Sambuc                         "abc", // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
55f4a2713aSLionel Sambuc                          5.8 }; // expected-warning {{implicit conversion from 'double' to 'long' changes value from 5.8 to 5}}
56f4a2713aSLionel Sambuc }
57f4a2713aSLionel Sambuc 
test()58f4a2713aSLionel Sambuc void test() {
59f4a2713aSLionel Sambuc   int y1[3] = {
60f4a2713aSLionel Sambuc     { 1, 2, 3 } // expected-warning{{excess elements in scalar initializer}}
61f4a2713aSLionel Sambuc   };
62f4a2713aSLionel Sambuc   int y3[4][3] = {
63f4a2713aSLionel Sambuc     { 1, 3, 5 },
64f4a2713aSLionel Sambuc     { 2, 4, 6 },
65f4a2713aSLionel Sambuc     { 3, 5, 7 },
66f4a2713aSLionel Sambuc     { 4, 6, 8 },
67f4a2713aSLionel Sambuc     {  }, // expected-warning{{use of GNU empty initializer extension}} expected-warning{{excess elements in array initializer}}
68f4a2713aSLionel Sambuc   };
69f4a2713aSLionel Sambuc   int y4[4][3] = {
70f4a2713aSLionel Sambuc     { 1, 3, 5, 2 }, // expected-warning{{excess elements in array initializer}}
71f4a2713aSLionel Sambuc     { 4, 6 },
72f4a2713aSLionel Sambuc     { 3, 5, 7 },
73f4a2713aSLionel Sambuc     { 4, 6, 8 },
74f4a2713aSLionel Sambuc   };
75f4a2713aSLionel Sambuc }
76f4a2713aSLionel Sambuc 
allLegalAndSynonymous()77f4a2713aSLionel Sambuc void allLegalAndSynonymous() {
78f4a2713aSLionel Sambuc   short q[4][3][2] = {
79f4a2713aSLionel Sambuc     { 1 },
80f4a2713aSLionel Sambuc     { 2, 3 },
81f4a2713aSLionel Sambuc     { 4, 5, 6 }
82f4a2713aSLionel Sambuc   };
83f4a2713aSLionel Sambuc   short q2[4][3][2] = {
84f4a2713aSLionel Sambuc     { 1, 0, 0, 0, 0, 0 },
85f4a2713aSLionel Sambuc     { 2, 3, 0, 0, 0, 0 },
86f4a2713aSLionel Sambuc     { 4, 5, 6 }
87f4a2713aSLionel Sambuc   };
88f4a2713aSLionel Sambuc   short q3[4][3][2] = {
89f4a2713aSLionel Sambuc     {
90f4a2713aSLionel Sambuc       { 1 },
91f4a2713aSLionel Sambuc     },
92f4a2713aSLionel Sambuc     {
93f4a2713aSLionel Sambuc       { 2, 3 },
94f4a2713aSLionel Sambuc     },
95f4a2713aSLionel Sambuc     {
96f4a2713aSLionel Sambuc       { 4, 5 },
97f4a2713aSLionel Sambuc       { 6 },
98f4a2713aSLionel Sambuc     },
99f4a2713aSLionel Sambuc   };
100f4a2713aSLionel Sambuc }
101f4a2713aSLionel Sambuc 
legal()102f4a2713aSLionel Sambuc void legal() {
103f4a2713aSLionel Sambuc   short q[][3][2] = {
104f4a2713aSLionel Sambuc     { 1 },
105f4a2713aSLionel Sambuc     { 2, 3 },
106f4a2713aSLionel Sambuc     { 4, 5, 6 }
107f4a2713aSLionel Sambuc   };
108f4a2713aSLionel Sambuc   int q_sizecheck[(sizeof(q) / sizeof(short [3][2])) == 3? 1 : -1];
109f4a2713aSLionel Sambuc }
110f4a2713aSLionel Sambuc 
111f4a2713aSLionel Sambuc unsigned char asso_values[] = { 34 };
legal2()112f4a2713aSLionel Sambuc int legal2() {
113f4a2713aSLionel Sambuc   return asso_values[0];
114f4a2713aSLionel Sambuc }
115f4a2713aSLionel Sambuc 
illegal()116f4a2713aSLionel Sambuc void illegal() {
117f4a2713aSLionel Sambuc   short q2[4][][2] = { // expected-error{{array has incomplete element type 'short [][2]'}}
118f4a2713aSLionel Sambuc     { 1, 0, 0, 0, 0, 0 },
119f4a2713aSLionel Sambuc     { 2, 3, 0, 0, 0, 0 },
120f4a2713aSLionel Sambuc     { 4, 5, 6 }
121f4a2713aSLionel Sambuc   };
122f4a2713aSLionel Sambuc   short q3[4][3][] = { // expected-error{{array has incomplete element type 'short []'}}
123f4a2713aSLionel Sambuc     {
124f4a2713aSLionel Sambuc       { 1 },
125f4a2713aSLionel Sambuc     },
126f4a2713aSLionel Sambuc     {
127f4a2713aSLionel Sambuc       { 2, 3 },
128f4a2713aSLionel Sambuc     },
129f4a2713aSLionel Sambuc     {
130f4a2713aSLionel Sambuc       { 4, 5 },
131f4a2713aSLionel Sambuc       { 6 },
132f4a2713aSLionel Sambuc     },
133f4a2713aSLionel Sambuc   };
134f4a2713aSLionel Sambuc   int a[][] = { 1, 2 }; // expected-error{{array has incomplete element type 'int []'}}
135f4a2713aSLionel Sambuc }
136f4a2713aSLionel Sambuc 
137f4a2713aSLionel Sambuc typedef int AryT[];
138f4a2713aSLionel Sambuc 
testTypedef()139f4a2713aSLionel Sambuc void testTypedef()
140f4a2713aSLionel Sambuc {
141f4a2713aSLionel Sambuc   AryT a = { 1, 2 }, b = { 3, 4, 5 };
142f4a2713aSLionel Sambuc   int a_sizecheck[(sizeof(a) / sizeof(int)) == 2? 1 : -1];
143f4a2713aSLionel Sambuc   int b_sizecheck[(sizeof(b) / sizeof(int)) == 3? 1 : -1];
144f4a2713aSLionel Sambuc }
145f4a2713aSLionel Sambuc 
146f4a2713aSLionel Sambuc static char const xx[] = "test";
147f4a2713aSLionel Sambuc int xx_sizecheck[(sizeof(xx) / sizeof(char)) == 5? 1 : -1];
148f4a2713aSLionel Sambuc static char const yy[5] = "test";
149f4a2713aSLionel Sambuc static char const zz[3] = "test"; // expected-warning{{initializer-string for char array is too long}}
150f4a2713aSLionel Sambuc 
charArrays()151f4a2713aSLionel Sambuc void charArrays() {
152f4a2713aSLionel Sambuc   static char const test[] = "test";
153f4a2713aSLionel Sambuc   int test_sizecheck[(sizeof(test) / sizeof(char)) == 5? 1 : -1];
154f4a2713aSLionel Sambuc   static char const test2[] = { "weird stuff" };
155f4a2713aSLionel Sambuc   static char const test3[] = { "test", "excess stuff" }; // expected-warning{{excess elements in char array initializer}}
156f4a2713aSLionel Sambuc 
157f4a2713aSLionel Sambuc   char* cp[] = { "Hello" };
158f4a2713aSLionel Sambuc 
159f4a2713aSLionel Sambuc   char c[] = { "Hello" };
160f4a2713aSLionel Sambuc   int l[sizeof(c) == 6 ? 1 : -1];
161f4a2713aSLionel Sambuc 
162f4a2713aSLionel Sambuc   int i[] = { "Hello "}; // expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [7]'}}
163f4a2713aSLionel Sambuc   char c2[] = { "Hello", "Good bye" }; //expected-warning{{excess elements in char array initializer}}
164f4a2713aSLionel Sambuc 
165f4a2713aSLionel Sambuc   int i2[1] = { "Hello" }; //expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [6]'}}
166f4a2713aSLionel Sambuc   char c3[5] = { "Hello" };
167f4a2713aSLionel Sambuc   char c4[4] = { "Hello" }; //expected-warning{{initializer-string for char array is too long}}
168f4a2713aSLionel Sambuc 
169f4a2713aSLionel Sambuc   int i3[] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
170f4a2713aSLionel Sambuc }
171f4a2713aSLionel Sambuc 
variableArrayInit()172f4a2713aSLionel Sambuc void variableArrayInit() {
173f4a2713aSLionel Sambuc   int a = 4;
174f4a2713aSLionel Sambuc   char strlit[a] = "foo"; //expected-error{{variable-sized object may not be initialized}}
175f4a2713aSLionel Sambuc   int b[a] = { 1, 2, 4 }; //expected-error{{variable-sized object may not be initialized}}
176f4a2713aSLionel Sambuc }
177f4a2713aSLionel Sambuc 
178f4a2713aSLionel Sambuc // Pure array tests
179f4a2713aSLionel Sambuc float r1[10] = {{7}}; //expected-warning{{braces around scalar initializer}}
180f4a2713aSLionel Sambuc float r2[] = {{8}}; //expected-warning{{braces around scalar initializer}}
181f4a2713aSLionel Sambuc char r3[][5] = {1,2,3,4,5,6};
182f4a2713aSLionel Sambuc int r3_sizecheck[(sizeof(r3) / sizeof(char[5])) == 2? 1 : -1];
183f4a2713aSLionel Sambuc char r3_2[sizeof r3 == 10 ? 1 : -1];
184f4a2713aSLionel Sambuc float r4[1][2] = {1,{2},3,4}; //expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}}
185f4a2713aSLionel Sambuc char r5[][5] = {"aa", "bbb", "ccccc"};
186f4a2713aSLionel Sambuc char r6[sizeof r5 == 15 ? 1 : -1];
187f4a2713aSLionel Sambuc const char r7[] = "zxcv";
188f4a2713aSLionel Sambuc char r8[5] = "5char";
189f4a2713aSLionel Sambuc char r9[5] = "6chars"; //expected-warning{{initializer-string for char array is too long}}
190f4a2713aSLionel Sambuc unsigned char r10[] = __extension__ (_Generic(0, int: (__extension__ "foo" )));
191f4a2713aSLionel Sambuc int r11[0] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
192f4a2713aSLionel Sambuc 
193f4a2713aSLionel Sambuc // Some struct tests
autoStructTest()194f4a2713aSLionel Sambuc void autoStructTest() {
195f4a2713aSLionel Sambuc struct s1 {char a; char b;} t1;
196f4a2713aSLionel Sambuc struct s2 {struct s1 c;} t2 = { t1 };
197f4a2713aSLionel Sambuc // The following is a less than great diagnostic (though it's on par with EDG).
198f4a2713aSLionel Sambuc struct s1 t3[] = {t1, t1, "abc", 0}; //expected-warning{{incompatible pointer to integer conversion initializing 'char' with an expression of type 'char [4]'}}
199f4a2713aSLionel Sambuc int t4[sizeof t3 == 6 ? 1 : -1];
200f4a2713aSLionel Sambuc }
201f4a2713aSLionel Sambuc struct foo { int z; } w;
bar(void)202f4a2713aSLionel Sambuc int bar (void) {
203f4a2713aSLionel Sambuc   struct foo z = { w }; //expected-error{{initializing 'int' with an expression of incompatible type 'struct foo'}}
204f4a2713aSLionel Sambuc   return z.z;
205f4a2713aSLionel Sambuc }
206f4a2713aSLionel Sambuc struct s3 {void (*a)(void);} t5 = {autoStructTest};
207f4a2713aSLionel Sambuc struct {int a; int b[];} t6 = {1, {1, 2, 3}}; // expected-warning{{flexible array initialization is a GNU extension}} \
208f4a2713aSLionel Sambuc // expected-note{{initialized flexible array member 'b' is here}}
209f4a2713aSLionel Sambuc union {char a; int b;} t7[] = {1, 2, 3};
210f4a2713aSLionel Sambuc int t8[sizeof t7 == (3*sizeof(int)) ? 1 : -1];
211f4a2713aSLionel Sambuc 
212f4a2713aSLionel Sambuc struct bittest{int : 31, a, :21, :12, b;};
213f4a2713aSLionel Sambuc struct bittest bittestvar = {1, 2, 3, 4}; //expected-warning{{excess elements in struct initializer}}
214f4a2713aSLionel Sambuc 
215f4a2713aSLionel Sambuc // Not completely sure what should happen here...
216f4a2713aSLionel Sambuc int u1 = {}; //expected-warning{{use of GNU empty initializer extension}} expected-error{{scalar initializer cannot be empty}}
217f4a2713aSLionel Sambuc int u2 = {{3}}; //expected-warning{{too many braces around scalar initializer}}
218f4a2713aSLionel Sambuc 
219f4a2713aSLionel Sambuc // PR2362
varArray()220f4a2713aSLionel Sambuc void varArray() {
221f4a2713aSLionel Sambuc   int c[][x] = { 0 }; //expected-error{{variable-sized object may not be initialized}}
222f4a2713aSLionel Sambuc }
223f4a2713aSLionel Sambuc 
224f4a2713aSLionel Sambuc // PR2151
emptyInit()225f4a2713aSLionel Sambuc void emptyInit() {struct {} x[] = {6};} //expected-warning{{empty struct is a GNU extension}} \
226f4a2713aSLionel Sambuc // expected-error{{initializer for aggregate with no elements}}
227f4a2713aSLionel Sambuc 
noNamedInit()228f4a2713aSLionel Sambuc void noNamedInit() {
229f4a2713aSLionel Sambuc   struct {int:5;} x[] = {6}; //expected-error{{initializer for aggregate with no elements}} \
230f4a2713aSLionel Sambuc // expected-warning {{struct without named members is a GNU extension}}
231f4a2713aSLionel Sambuc }
232f4a2713aSLionel Sambuc struct {int a; int:5;} noNamedImplicit[] = {1,2,3};
233f4a2713aSLionel Sambuc int noNamedImplicitCheck[sizeof(noNamedImplicit) == 3 * sizeof(*noNamedImplicit) ? 1 : -1];
234f4a2713aSLionel Sambuc 
235f4a2713aSLionel Sambuc 
236f4a2713aSLionel Sambuc // ptrs are constant
237f4a2713aSLionel Sambuc struct soft_segment_descriptor {
238f4a2713aSLionel Sambuc   long ssd_base;
239f4a2713aSLionel Sambuc };
240f4a2713aSLionel Sambuc static int dblfault_tss;
241f4a2713aSLionel Sambuc 
242f4a2713aSLionel Sambuc union uniao { int ola; } xpto[1];
243f4a2713aSLionel Sambuc 
244f4a2713aSLionel Sambuc struct soft_segment_descriptor gdt_segs[] = {
245f4a2713aSLionel Sambuc   {(long) &dblfault_tss},
246f4a2713aSLionel Sambuc   { (long)xpto},
247f4a2713aSLionel Sambuc };
248f4a2713aSLionel Sambuc 
249f4a2713aSLionel Sambuc static void sppp_ipv6cp_up();
250f4a2713aSLionel Sambuc const struct {} ipcp = { sppp_ipv6cp_up }; //expected-warning{{empty struct is a GNU extension}} \
251f4a2713aSLionel Sambuc // expected-warning{{excess elements in struct initializer}}
252f4a2713aSLionel Sambuc 
253f4a2713aSLionel Sambuc struct _Matrix { union { float m[4][4]; }; }; //expected-warning{{anonymous unions are a C11 extension}}
254f4a2713aSLionel Sambuc typedef struct _Matrix Matrix;
test_matrix()255f4a2713aSLionel Sambuc void test_matrix() {
256f4a2713aSLionel Sambuc   const Matrix mat1 = {
257f4a2713aSLionel Sambuc     { { 1.0f, 2.0f, 3.0f, 4.0f,
258f4a2713aSLionel Sambuc         5.0f, 6.0f, 7.0f, 8.0f,
259f4a2713aSLionel Sambuc         9.0f, 10.0f, 11.0f, 12.0f,
260f4a2713aSLionel Sambuc         13.0f, 14.0f, 15.0f, 16.0f } }
261f4a2713aSLionel Sambuc   };
262f4a2713aSLionel Sambuc 
263f4a2713aSLionel Sambuc   const Matrix mat2 = {
264f4a2713aSLionel Sambuc     1.0f, 2.0f, 3.0f, 4.0f,
265f4a2713aSLionel Sambuc     5.0f, 6.0f, 7.0f, 8.0f,
266f4a2713aSLionel Sambuc     9.0f, 10.0f, 11.0f, 12.0f,
267f4a2713aSLionel Sambuc     13.0f, 14.0f, 15.0f, 16.0f
268f4a2713aSLionel Sambuc   };
269f4a2713aSLionel Sambuc }
270f4a2713aSLionel Sambuc 
271f4a2713aSLionel Sambuc char badchararray[1] = { badchararray[0], "asdf" }; // expected-warning {{excess elements in array initializer}} expected-error {{initializer element is not a compile-time constant}}
272f4a2713aSLionel Sambuc 
273f4a2713aSLionel Sambuc // Test the GNU extension for initializing an array from an array
274f4a2713aSLionel Sambuc // compound literal. PR9261.
275f4a2713aSLionel Sambuc typedef int int5[5];
276f4a2713aSLionel Sambuc 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}}
277f4a2713aSLionel Sambuc 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}}
278f4a2713aSLionel Sambuc 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}}
279f4a2713aSLionel Sambuc 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}}
280f4a2713aSLionel Sambuc 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}}
281f4a2713aSLionel Sambuc 
282f4a2713aSLionel Sambuc int a6[5] = (int[]){1, 2, 3}; // expected-error{{cannot initialize array of type 'int [5]' with array of type 'int [3]'}}
283f4a2713aSLionel Sambuc 
284f4a2713aSLionel Sambuc int nonconst_value();
285*0a6a1f1dSLionel Sambuc int a7[5] = (int[5]){ 1,
286*0a6a1f1dSLionel Sambuc                       2,
287*0a6a1f1dSLionel Sambuc                       3,
288*0a6a1f1dSLionel Sambuc                       4,
289*0a6a1f1dSLionel Sambuc                       nonconst_value() // expected-error{{initializer element is not a compile-time constant}}
290*0a6a1f1dSLionel Sambuc };
291f4a2713aSLionel Sambuc 
292f4a2713aSLionel Sambuc // <rdar://problem/10636946>
293f4a2713aSLionel Sambuc __attribute__((weak)) const unsigned int test10_bound = 10;
294f4a2713aSLionel Sambuc char test10_global[test10_bound]; // expected-error {{variable length array declaration not allowed at file scope}}
test10()295f4a2713aSLionel Sambuc void test10() {
296f4a2713aSLionel Sambuc   char test10_local[test10_bound] = "help"; // expected-error {{variable-sized object may not be initialized}}
297f4a2713aSLionel Sambuc }
298