array-init.c revision 578edc6614236b224f63ac707acecaeb2a74d6b4
1// RUN: clang -fsyntax-only -verify -pedantic %s
2
3extern int foof() = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
4
5static int x, y, z;
6
7static int ary[] = { x, y, z }; // expected-error{{initializer element is not constant}}
8int ary2[] = { x, y, z }; // expected-error{{initializer element is not constant}}
9
10extern int fileScopeExtern[3] = { 1, 3, 5 }; // expected-warning{{'extern' variable has an initializer}}
11
12static int ary3[] = { 1, "abc", 3, 4 }; // expected-warning{{incompatible pointer to integer conversion initializing 'char *', expected 'int'}}
13
14void func() {
15  int x = 1;
16
17  typedef int TInt = 1; // expected-error{{illegal initializer (only variables can be initialized)}}
18
19  int xComputeSize[] = { 1, 3, 5 };
20
21  int x3[x] = { 1, 2 }; // expected-error{{variable-sized object may not be initialized}}
22
23  int x4 = { 1, 2 }; // expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}}
24
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
35  int y3[4][3] = {
36    { 1, 3, 5 },
37    { 2, 4, 6 },
38    { 3, 5, 7 },
39    { 4, 6, 8 },
40    { 5 }, // expected-warning{{excess elements in array initializer}}
41  };
42
43  struct threeElements {
44    int a,b,c;
45  } z = { 1 };
46
47  struct threeElements *p = 7; // expected-warning{{incompatible integer to pointer conversion initializing 'int', expected 'struct threeElements *'}}
48
49  extern int blockScopeExtern[3] = { 1, 3, 5 }; // expected-error{{'extern' variable cannot have an initializer}}
50
51  static int x2[3] = { 1.0, "abc" , 5.8 }; // expected-warning{{incompatible pointer to integer conversion initializing 'char *', expected 'int'}}
52}
53
54void test() {
55  int y1[3] = {
56    { 1, 2, 3 } // expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}}
57  };
58  int y3[4][3] = {
59    { 1, 3, 5 },
60    { 2, 4, 6 },
61    { 3, 5, 7 },
62    { 4, 6, 8 },
63    {  }, // expected-warning{{use of GNU empty initializer extension}} expected-warning{{excess elements in array initializer}}
64  };
65  int y4[4][3] = {
66    { 1, 3, 5, 2 }, // expected-warning{{excess elements in array initializer}}
67    { 4, 6 },
68    { 3, 5, 7 },
69    { 4, 6, 8 },
70  };
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  };
104}
105
106unsigned char asso_values[] = { 34 };
107int legal2() {
108  return asso_values[0];
109}
110
111void illegal() {
112  short q2[4][][2] = { // expected-error{{array has incomplete element type 'short [][2]'}}
113    { 1, 0, 0, 0, 0, 0 },
114    { 2, 3, 0, 0, 0, 0 },
115    { 4, 5, 6 }
116  };
117  short q3[4][3][] = { // expected-error{{array has incomplete element type 'short []'}}
118    {
119      { 1 },
120    },
121    {
122      { 2, 3 },
123    },
124    {
125      { 4, 5 },
126      { 6 },
127    },
128  };
129  // FIXME: the following two errors are redundant
130  int a[][] = { 1, 2 }; // expected-error{{array has incomplete element type 'int []'}} expected-error{{variable has incomplete type 'int []'}}
131}
132
133typedef int AryT[];
134
135void testTypedef()
136{
137  AryT a = { 1, 2 }, b = { 3, 4, 5 };
138}
139
140static char const xx[] = "test";
141static char const yy[5] = "test";
142static char const zz[3] = "test"; // expected-warning{{initializer-string for char array is too long}}
143
144void charArrays()
145{
146	static char const test[] = "test";
147	static char const test2[] = { "weird stuff" };
148	static char const test3[] = { "test", "excess stuff" }; // expected-error{{excess elements in char array initializer}}
149
150  char* cp[] = { "Hello" };
151
152  char c[] = { "Hello" };
153  int l[sizeof(c) == 6 ? 1 : -1];
154
155  int i[] = { "Hello "}; // expected-warning{{incompatible pointer to integer conversion initializing 'char *', expected 'int'}}
156  char c2[] = { "Hello", "Good bye" }; //expected-error{{excess elements in char array initializer}}
157
158  int i2[1] = { "Hello" }; //expected-warning{{incompatible pointer to integer conversion initializing 'char *', expected 'int'}}
159  char c3[5] = { "Hello" };
160  char c4[4] = { "Hello" }; //expected-warning{{initializer-string for char array is too long}}
161
162  int i3[] = {}; //expected-error{{at least one initializer value required to size array}} expected-error{{variable has incomplete type 'int []'}} expected-warning{{use of GNU empty initializer extension}}
163}
164
165void variableArrayInit() {
166  int a = 4;
167  char strlit[a] = "foo"; //expected-error{{variable-sized object may not be initialized}}
168  int b[a] = { 1, 2, 4 }; //expected-error{{variable-sized object may not be initialized}}
169}
170
171// Pure array tests
172float r1[10] = {{7}}; //expected-warning{{braces around scalar initializer}}
173float r2[] = {{8}}; //expected-warning{{braces around scalar initializer}}
174char r3[][5] = {1,2,3,4,5,6};
175char r3_2[sizeof r3 == 10 ? 1 : -1];
176float r4[1][2] = {1,{2},3,4}; //expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}}
177char r5[][5] = {"aa", "bbb", "ccccc"};
178char r6[sizeof r5 == 15 ? 1 : -1];
179const char r7[] = "zxcv";
180char r8[5] = "5char";
181char r9[5] = "6chars"; //expected-warning{{initializer-string for char array is too long}}
182
183int r11[0] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
184
185// Some struct tests
186void autoStructTest() {
187struct s1 {char a; char b;} t1;
188struct s2 {struct s1 c;} t2 = { t1 };
189// The following is a less than great diagnostic (though it's on par with EDG).
190struct s1 t3[] = {t1, t1, "abc", 0}; //expected-warning{{incompatible pointer to integer conversion initializing 'char *', expected 'char'}}
191int t4[sizeof t3 == 6 ? 1 : -1];
192}
193struct foo { int z; } w;
194int bar (void) {
195  struct foo z = { w }; //expected-error{{incompatible type initializing 'struct foo', expected 'int'}}
196  return z.z;
197}
198struct s3 {void (*a)(void);} t5 = {autoStructTest};
199// GCC extension; flexible array init. Once this is implemented, the warning should be removed.
200// Note that clang objc implementation depends on this extension.
201struct {int a; int b[];} t6 = {1, {1, 2, 3}}; //expected-warning{{excess elements in array initializer}}
202union {char a; int b;} t7[] = {1, 2, 3};
203int t8[sizeof t7 == (3*sizeof(int)) ? 1 : -1];
204
205struct bittest{int : 31, a, :21, :12, b;};
206struct bittest bittestvar = {1, 2, 3, 4}; //expected-warning{{excess elements in array initializer}}
207
208// Not completely sure what should happen here...
209int u1 = {}; //expected-warning{{use of GNU empty initializer extension}} expected-warning{{braces around scalar initializer}}
210int u2 = {{3}}; //expected-warning{{braces around scalar initializer}}
211
212