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 @@
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
17+{
18+ u8 a,b;
19+ u8 c[2];
20+};
21+
22+struct T
23+{
24+ u8 s[16];
25+ u8 a;
26+};
27+
28+struct U
29+{
30+ u8 a;
31+ struct S s;
32+ u8 b;
33+ struct T t;
34+};
35+
36+struct V
37+{
38+ struct S s;
39+ struct T t;
40+ u8 a;
41+};
42+
43+struct W
44+{
45+ struct V t;
46+ struct S s[];
47+};
48+
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+/* Compund 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}};
69+
70+#include <stdio.h>
71+void 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
81+void 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+
120+int 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 @@
1+ce: 1 12
2+gs: 1 2 3 4
3+gs2: 1 2 3 4
4+gt: 68 65 6c 6c 6f 0 0 0 0 0 0 0 0 0 0 0 2a
5+gu: 3 5 6 7 8 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b
6+gu2: 3 5 6 7 8 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b
7+gu3: 3 5 6 7 8 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b
8+gu4: 3 5 6 7 0 5 62 6c 61 0 0 0 0 0 0 0 0 0 0 0 0 0 2c
9+gs3: 1 2 3 4
10+gv: 3 4 5 6 68 61 68 61 0 0 0 0 0 0 0 0 0 0 0 0 2d 2e
11+gv2: 7 8 9 a 68 69 68 69 0 0 0 0 0 0 0 0 0 0 0 0 2f 30
12+gv3: 7 8 9 a 68 6f 68 6f 0 0 0 0 0 0 0 0 0 0 0 0 31 32
13+sinit16: 1 0 0 0 2 0 0 0
14+gw: 1 2 3 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
15+ls: 1 2 3 4
16+ls2: 1 2 3 4
17+lt: 68 65 6c 6c 6f 0 0 0 0 0 0 0 0 0 0 0 2a
18+lu: 3 5 6 7 8 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b
19+lu1: 3 1 2 3 4 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b
20+lu2: 3 1 2 3 4 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b
21+lu21: 3 1 2 3 4 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b
22+lu3: 3 5 6 7 8 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b
23+lu4: 3 5 6 7 0 5 62 6c 61 0 0 0 0 0 0 0 0 0 0 0 0 0 2c
24+ls3: 1 2 3 4
25+lv: 3 4 5 6 68 61 68 61 0 0 0 0 0 0 0 0 0 0 0 0 2d 2e
26+lv2: 1 2 3 4 68 69 68 69 0 0 0 0 0 0 0 0 0 0 0 0 2f 30
27+lv3: 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
5454
5555 # automatically generate .expect files with gcc:
5656 %.expect :
57− (gcc $*.c -o a.exe && ./a.exe $(ARGS)) >$*.expect 2>&1; rm -f a.exe
57+ (gcc -w $*.c -o a.exe && ./a.exe $(ARGS)) >$*.expect 2>&1; rm -f a.exe
5858
5959 # tell make not to delete
6060 .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