Fix init bitfield padding with size 32/64
[tinycc.git] / tests / tests2 / 90_struct-init.c
blob6bff8ab47a363c0a3b1b333764fa88b4669a969a
1 typedef unsigned char u8;
2 typedef struct {} empty_s;
3 struct contains_empty {
4 u8 a;
5 empty_s empty;
6 u8 b;
7 };
8 struct contains_empty ce = { { (1) }, (empty_s){}, 022, };
9 /* The following decl of 'q' would demonstrate the TCC bug in init_putv when
10 handling copying compound literals. (Compound literals
11 aren't acceptable constant initializers in isoc99, but
12 we accept them like gcc, except for this case)
13 //char *q = (char *){ "trara" }; */
14 struct SS {u8 a[3], b; };
15 struct SS sinit16[] = { { 1 }, 2 };
16 struct S
18 u8 a,b;
19 u8 c[2];
22 struct T
24 u8 s[16];
25 u8 a;
28 struct U
30 u8 a;
31 struct S s;
32 u8 b;
33 struct T t;
36 struct V
38 struct S s;
39 struct T t;
40 u8 a;
43 struct W
45 struct V t;
46 struct S s[];
49 struct S gs = ((struct S){1, 2, 3, 4});
50 struct S gs2 = {1, 2, {3, 4}};
51 struct T gt = {"hello", 42};
52 struct U gu = {3, 5,6,7,8, 4, "huhu", 43};
53 struct U gu2 = {3, {5,6,7,8}, 4, {"huhu", 43}};
54 /* Optional braces around scalar initializers. Accepted, but with
55 a warning. */
56 struct U gu3 = { {3}, {5,6,7,8,}, 4, {"huhu", 43}};
57 /* Many superfluous braces and leaving out one initializer for U.s.c[1] */
58 struct U gu4 = { 3, {5,6,7,}, 5, { "bla", {44}} };
59 /* Superfluous braces and useless parens around values */
60 struct S gs3 = { (1), {(2)}, {(((3))), {4}}};
61 /* Superfluous braces, and leaving out braces for V.t, plus cast */
62 struct V gv = {{{3},4,{5,6}}, "haha", (u8)45, 46};
63 /* Compound literal */
64 struct V gv2 = {(struct S){7,8,{9,10}}, {"hihi", 47}, 48};
65 /* Parens around compound literal */
66 struct V gv3 = {((struct S){7,8,{9,10}}), {"hoho", 49}, 50};
67 /* Initialization of a flex array member (warns in GCC) */
68 struct W gw = {{1,2,3,4}, {1,2,3,4,5}};
70 union UU {
71 u8 a;
72 u8 b;
74 struct SU {
75 union UU u;
76 u8 c;
78 struct SU gsu = {5,6};
80 /* Unnamed struct/union members aren't ISO C, but it's a widely accepted
81 extension. See below for further extensions to that under -fms-extension.*/
82 union UV {
83 struct {u8 a,b;};
84 struct S s;
86 union UV guv = {{6,5}};
87 union UV guv2 = {{.b = 7, .a = 8}};
88 union UV guv3 = {.b = 8, .a = 7};
90 struct SSU {
91 int y;
92 struct { int x; };
94 struct SSU gssu1 = { .y = 5, .x = 3 };
95 struct SSU gssu2 = { 5, 3 };
97 /* Under -fms-extensions also the following is valid:
98 union UV2 {
99 struct Anon {u8 a,b;}; // unnamed member, but tagged struct, ...
100 struct S s;
102 struct Anon gan = { 10, 11 }; // ... which makes it available here.
103 union UV2 guv4 = {{4,3}}; // and the other inits from above as well
106 struct in6_addr {
107 union {
108 u8 u6_addr8[16];
109 unsigned short u6_addr16[8];
110 } u;
112 struct flowi6 {
113 struct in6_addr saddr, daddr;
115 struct pkthdr {
116 struct in6_addr daddr, saddr;
118 struct pkthdr phdr = { { { 6,5,4,3 } }, { { 9,8,7,6 } } };
120 struct Wrap {
121 void *func;
123 int global;
124 void inc_global (void)
126 global++;
129 struct Wrap global_wrap[] = {
130 ((struct Wrap) {inc_global}),
131 inc_global,
134 #include <stdio.h>
135 void print_ (const char *name, const u8 *p, long size)
137 printf ("%s:", name);
138 while (size--) {
139 printf (" %x", *p++);
141 printf ("\n");
143 #define print(x) print_(#x, (u8*)&x, sizeof (x))
144 #if 1
145 void foo (struct W *w, struct pkthdr *phdr_)
147 struct S ls = {1, 2, 3, 4};
148 struct S ls2 = {1, 2, {3, 4}};
149 struct T lt = {"hello", 42};
150 struct U lu = {3, 5,6,7,8, 4, "huhu", 43};
151 struct U lu1 = {3, ls, 4, {"huhu", 43}};
152 struct U lu2 = {3, (ls), 4, {"huhu", 43}};
153 const struct S *pls = &ls;
154 struct S ls21 = *pls;
155 struct U lu22 = {3, *pls, 4, {"huhu", 43}};
156 /* Incomplete bracing. */
157 struct U lu21 = {3, ls, 4, "huhu", 43};
158 /* Optional braces around scalar initializers. Accepted, but with
159 a warning. */
160 struct U lu3 = { 3, {5,6,7,8,}, 4, {"huhu", 43}};
161 /* Many superfluous braces and leaving out one initializer for U.s.c[1] */
162 struct U lu4 = { 3, {5,6,7,}, 5, { "bla", 44} };
163 /* Superfluous braces and useless parens around values */
164 struct S ls3 = { (1), (2), {(((3))), 4}};
165 /* Superfluous braces, and leaving out braces for V.t, plus cast */
166 struct V lv = {{3,4,{5,6}}, "haha", (u8)45, 46};
167 /* Compound literal */
168 struct V lv2 = {(struct S)w->t.s, {"hihi", 47}, 48};
169 /* Parens around compound literal */
170 struct V lv3 = {((struct S){7,8,{9,10}}), ((const struct W *)w)->t.t, 50};
171 const struct pkthdr *phdr = phdr_;
172 struct flowi6 flow = { .daddr = phdr->daddr, .saddr = phdr->saddr };
173 int elt = 0x42;
174 /* Range init, overlapping */
175 struct T lt2 = { { [1 ... 5] = 9, [6 ... 10] = elt, [4 ... 7] = elt+1 }, 1 };
176 struct SSU lssu1 = { 5, 3 };
177 struct SSU lssu2 = { .y = 5, .x = 3 };
178 /* designated initializers in GNU form */
179 #if defined(__GNUC__) || defined(__TINYC__)
180 struct S ls4 = {a: 1, b: 2, c: {3, 4}};
181 #else
182 struct S ls4 = {.a = 1, .b = 2, .c = {3, 4}};
183 #endif
184 print(ls);
185 print(ls2);
186 print(lt);
187 print(lu);
188 print(lu1);
189 print(lu2);
190 print(ls21);
191 print(lu21);
192 print(lu22);
193 print(lu3);
194 print(lu4);
195 print(ls3);
196 print(lv);
197 print(lv2);
198 print(lv3);
199 print(lt2);
200 print(lssu1);
201 print(lssu2);
202 print(flow);
203 print(ls4);
205 #endif
207 void test_compound_with_relocs (void)
209 struct Wrap local_wrap[] = {
210 ((struct Wrap) {inc_global}),
211 inc_global,
213 void (*p)(void);
214 p = global_wrap[0].func; p();
215 p = global_wrap[1].func; p();
216 p = local_wrap[0].func; p();
217 p = local_wrap[1].func; p();
220 void sys_ni(void) { printf("ni\n"); }
221 void sys_one(void) { printf("one\n"); }
222 void sys_two(void) { printf("two\n"); }
223 void sys_three(void) { printf("three\n"); }
224 void sys_four(void) { printf("four\n"); }
225 typedef void (*fptr)(void);
227 #define array_size(a) (sizeof a / sizeof a[0])
229 void test_multi_relocs(void)
231 int i;
233 static const fptr tabl1[4] = {
234 [0 ... 3] = &sys_ni,
235 [0] = sys_one,
236 [1] = sys_two,
237 [2] = sys_three,
238 sys_four,
239 [1 ... 2] = &sys_ni,
240 [1] = 0,
242 for (i = 0; i < array_size(tabl1); i++)
243 if (tabl1[i])
244 tabl1[i]();
245 else
246 printf("(0)\n");
248 const fptr tabl2[4] = {
249 [0 ... 3] = &sys_ni,
250 [0] = sys_one,
251 [1] = sys_two,
252 [2] = sys_three,
253 sys_four,
254 [1 ... 2] = &sys_ni,
255 [1] = 0,
257 for (i = 0; i < array_size(tabl2); i++)
258 if (tabl2[i])
259 tabl2[i]();
260 else
261 printf("(0)\n");
263 int c = 0;
264 int dd[] = {
265 [0 ... 1] = ++c,
266 [2 ... 3] = ++c
268 for (i = 0; i < array_size(dd); i++)
269 printf(" %d", dd[i]);
270 printf("\n");
272 /* multi-dimensional flex array with range initializers */
273 static char m1[][2][3] = {[0 ... 2]={{3,4,5},{6,7,8}},{{9},10},"abc"};
274 char m2[][2][3] = {[0 ... 2]={{3,4,5},{6,7,8}},{{9},10},"abc"};
275 int g, j, k;
276 for (g = 2; g-- > 0;) {
277 printf("mdfa %s: %d -", "locl\0glob" + g * 5, sizeof m1);
278 for (i = 0; i < array_size(m1); i++)
279 for (j = 0; j < array_size(m1[0]); j++)
280 for (k = 0; k < array_size(m1[0][0]); k++)
281 printf(" %d", (g ? m1:m2)[i][j][k]);
282 printf("\n");
286 void test_init_ranges(void) {
287 int i,c=0;
288 static void *gostring[] = {
289 [0 ... 31] = &&l_bad, [127] = &&l_bad,
290 [32 ... 126] = &&l_loop,
291 ['\\'] = &&l_esc, ['"'] = &&l_qdown,
292 [128 ... 191] = &&l_bad,
293 [192 ... 223] = &&l_utf8_2,
294 [224 ... 239] = &&l_utf8_3,
295 [240 ... 247] = &&l_utf8_4,
296 [248 ... 255] = &&l_bad
299 for (i = 0; i < 256; i++) {
300 goto *gostring[i];
301 l_bad: c++;
302 l_loop: c++;
303 l_esc: c++;
304 l_qdown: c++;
305 l_utf8_2: c++;
306 l_utf8_3: c++;
307 l_utf8_4: c++;
309 printf ("%d\n", c);
313 /* Following is from GCC gcc.c-torture/execute/20050613-1.c. */
315 struct SEA { int i; int j; int k; int l; };
316 struct SEB { struct SEA a; int r[1]; };
317 struct SEC { struct SEA a; int r[0]; };
318 struct SED { struct SEA a; int r[]; };
320 static void
321 test_correct_filling (struct SEA *x)
323 static int i;
324 if (x->i != 0 || x->j != 5 || x->k != 0 || x->l != 0)
325 printf("sea_fill%d: wrong\n", i);
326 else
327 printf("sea_fill%d: okay\n", i);
328 i++;
332 test_zero_init (void)
334 /* The peculiarity here is that only a.j is initialized. That
335 means that all other members must be zero initialized. TCC
336 once didn't do that for sub-level designators. */
337 struct SEB b = { .a.j = 5 };
338 struct SEC c = { .a.j = 5 };
339 struct SED d = { .a.j = 5 };
340 test_correct_filling (&b.a);
341 test_correct_filling (&c.a);
342 test_correct_filling (&d.a);
343 return 0;
346 void test_init_struct_from_struct(void)
348 int i = 0;
349 struct S {int x,y;}
350 a = {1,2},
351 b = {3,4},
352 c[] = {a,b},
353 d[] = {++i, ++i, ++i, ++i},
354 e[] = {b, (struct S){5,6}}
357 printf("%s: %d %d %d %d - %d %d %d %d - %d %d %d %d\n",
358 __FUNCTION__,
359 c[0].x,
360 c[0].y,
361 c[1].x,
362 c[1].y,
363 d[0].x,
364 d[0].y,
365 d[1].x,
366 d[1].y,
367 e[0].x,
368 e[0].y,
369 e[1].x,
370 e[1].y
374 typedef struct {
375 unsigned int a;
376 unsigned int : 32;
377 unsigned int b;
378 unsigned long : 64;
379 unsigned int c;
380 } tst_bf;
382 tst_bf arr[] = { { 1, 2, 3 } };
384 void
385 test_init_bf(void)
387 printf ("%s: %d %d %d\n", __FUNCTION__, arr[0].a, arr[0].b, arr[0].c);
391 int main()
393 print(ce);
394 print(gs);
395 print(gs2);
396 print(gt);
397 print(gu);
398 print(gu2);
399 print(gu3);
400 print(gu4);
401 print(gs3);
402 print(gv);
403 print(gv2);
404 print(gv3);
405 print(sinit16);
406 print(gw);
407 print(gsu);
408 print(guv);
409 print(guv.b);
410 print(guv2);
411 print(guv3);
412 print(gssu1);
413 print(gssu2);
414 print(phdr);
415 foo(&gw, &phdr);
416 //printf("q: %s\n", q);
417 test_compound_with_relocs();
418 test_multi_relocs();
419 test_zero_init();
420 test_init_ranges();
421 test_init_struct_from_struct();
422 test_init_bf();
423 return 0;