History

struct-init: Implement initializing subaggregates

E.g. "struct { struct S s; int a;} = { others, 42 };" if 'others' is also a 'struct S'. Also when the value is a compound literal. See added testcases.

Michael Matz committed 10 years ago

Commit ed7d5461 parent 968bccd4 files · +226 −40Browse files

Showing the changes to tests (3 of 4 files). Show all

Unified · Split

3 files
  1. tests/tests2/86-struct-init.c +139 −0
  2. tests/tests2/86-struct-init.expect +27 −0
  3. tests/tests2/Makefile +1 −1
tests/tests2/86-struct-init.c+139−0
@@ -0,0 +1,139 @@
1typedef unsigned char u8;
2typedef struct {} empty_s;
3struct contains_empty {
4 u8 a;
5 empty_s empty;
6 u8 b;
7};
8struct 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" }; */
14struct SS {u8 a[3], b; };
15struct SS sinit16[] = { { 1 }, 2 };
16struct S
17{
18 u8 a,b;
19 u8 c[2];
20};
21
22struct T
23{
24 u8 s[16];
25 u8 a;
26};
27
28struct U
29{
30 u8 a;
31 struct S s;
32 u8 b;
33 struct T t;
34};
35
36struct V
37{
38 struct S s;
39 struct T t;
40 u8 a;
41};
42
43struct W
44{
45 struct V t;
46 struct S s[];
47};
48
49struct S gs = ((struct S){1, 2, 3, 4});
50struct S gs2 = {1, 2, {3, 4}};
51struct T gt = {"hello", 42};
52struct U gu = {3, 5,6,7,8, 4, "huhu", 43};
53struct U gu2 = {3, {5,6,7,8}, 4, {"huhu", 43}};
54/* Optional braces around scalar initializers. Accepted, but with
55 a warning. */
56struct U gu3 = { {3}, {5,6,7,8,}, 4, {"huhu", 43}};
57/* Many superfluous braces and leaving out one initializer for U.s.c[1] */
58struct U gu4 = { 3, {5,6,7,}, 5, { "bla", {44}} };
59/* Superfluous braces and useless parens around values */
60struct S gs3 = { (1), {(2)}, {(((3))), {4}}};
61/* Superfluous braces, and leaving out braces for V.t, plus cast */
62struct V gv = {{{3},4,{5,6}}, "haha", (u8)45, 46};
63/* Compund literal */
64struct V gv2 = {(struct S){7,8,{9,10}}, {"hihi", 47}, 48};
65/* Parens around compound literal */
66struct V gv3 = {((struct S){7,8,{9,10}}), {"hoho", 49}, 50};
67/* Initialization of a flex array member (warns in GCC) */
68struct W gw = {{1,2,3,4}, {1,2,3,4,5}};
69
70#include <stdio.h>
71void print_ (const char *name, const u8 *p, long size)
72{
73 printf ("%s:", name);
74 while (size--) {
75 printf (" %x", *p++);
76 }
77 printf ("\n");
78}
79#define print(x) print_(#x, (u8*)&x, sizeof (x))
80#if 1
81void foo (struct W *w)
82{
83 struct S ls = {1, 2, 3, 4};
84 struct S ls2 = {1, 2, {3, 4}};
85 struct T lt = {"hello", 42};
86 struct U lu = {3, 5,6,7,8, 4, "huhu", 43};
87 struct U lu1 = {3, ls, 4, {"huhu", 43}};
88 struct U lu2 = {3, (ls), 4, {"huhu", 43}};
89 /* Incomplete bracing. */
90 struct U lu21 = {3, ls, 4, "huhu", 43};
91 /* Optional braces around scalar initializers. Accepted, but with
92 a warning. */
93 struct U lu3 = { 3, {5,6,7,8,}, 4, {"huhu", 43}};
94 /* Many superfluous braces and leaving out one initializer for U.s.c[1] */
95 struct U lu4 = { 3, {5,6,7,}, 5, { "bla", 44} };
96 /* Superfluous braces and useless parens around values */
97 struct S ls3 = { (1), (2), {(((3))), 4}};
98 /* Superfluous braces, and leaving out braces for V.t, plus cast */
99 struct V lv = {{3,4,{5,6}}, "haha", (u8)45, 46};
100 /* Compund literal */
101 struct V lv2 = {(struct S)w->t.s, {"hihi", 47}, 48};
102 /* Parens around compound literal */
103 struct V lv3 = {((struct S){7,8,{9,10}}), w->t.t, 50};
104 print(ls);
105 print(ls2);
106 print(lt);
107 print(lu);
108 print(lu1);
109 print(lu2);
110 print(lu21);
111 print(lu3);
112 print(lu4);
113 print(ls3);
114 print(lv);
115 print(lv2);
116 print(lv3);
117}
118#endif
119
120int main()
121{
122 print(ce);
123 print(gs);
124 print(gs2);
125 print(gt);
126 print(gu);
127 print(gu2);
128 print(gu3);
129 print(gu4);
130 print(gs3);
131 print(gv);
132 print(gv2);
133 print(gv3);
134 print(sinit16);
135 print(gw);
136 foo(&gw);
137 //printf("q: %s\n", q);
138 return 0;
139}
tests/tests2/86-struct-init.expect+27−0
@@ -0,0 +1,27 @@
1ce: 1 12
2gs: 1 2 3 4
3gs2: 1 2 3 4
4gt: 68 65 6c 6c 6f 0 0 0 0 0 0 0 0 0 0 0 2a
5gu: 3 5 6 7 8 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b
6gu2: 3 5 6 7 8 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b
7gu3: 3 5 6 7 8 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b
8gu4: 3 5 6 7 0 5 62 6c 61 0 0 0 0 0 0 0 0 0 0 0 0 0 2c
9gs3: 1 2 3 4
10gv: 3 4 5 6 68 61 68 61 0 0 0 0 0 0 0 0 0 0 0 0 2d 2e
11gv2: 7 8 9 a 68 69 68 69 0 0 0 0 0 0 0 0 0 0 0 0 2f 30
12gv3: 7 8 9 a 68 6f 68 6f 0 0 0 0 0 0 0 0 0 0 0 0 31 32
13sinit16: 1 0 0 0 2 0 0 0
14gw: 1 2 3 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
15ls: 1 2 3 4
16ls2: 1 2 3 4
17lt: 68 65 6c 6c 6f 0 0 0 0 0 0 0 0 0 0 0 2a
18lu: 3 5 6 7 8 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b
19lu1: 3 1 2 3 4 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b
20lu2: 3 1 2 3 4 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b
21lu21: 3 1 2 3 4 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b
22lu3: 3 5 6 7 8 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b
23lu4: 3 5 6 7 0 5 62 6c 61 0 0 0 0 0 0 0 0 0 0 0 0 0 2c
24ls3: 1 2 3 4
25lv: 3 4 5 6 68 61 68 61 0 0 0 0 0 0 0 0 0 0 0 0 2d 2e
26lv2: 1 2 3 4 68 69 68 69 0 0 0 0 0 0 0 0 0 0 0 0 2f 30
27lv3: 7 8 9 a 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32
tests/tests2/Makefile+1−1
@@ -54,7 +54,7 @@ all test: $(filter-out $(SKIP),$(TESTS)) · 53 unmodified lines
54 54
55# automatically generate .expect files with gcc:55# automatically generate .expect files with gcc:
56%.expect :56%.expect :
57 (gcc $*.c -o a.exe && ./a.exe $(ARGS)) >$*.expect 2>&1; rm -f a.exe57 (gcc -w $*.c -o a.exe && ./a.exe $(ARGS)) >$*.expect 2>&1; rm -f a.exe
58 58
59# tell make not to delete59# tell make not to delete
60.PRECIOUS: %.expect60.PRECIOUS: %.expect

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t