Debian Codegraph @

gcc/testsuite/gcc.dg/gnu11-empty-init-4.c

raw
1 /* Test GNU C11 support for empty initializers. */
2 /* { dg-do run } */
3 /* { dg-options "-std=gnu11 -fzero-init-padding-bits=all" } */
4
5 extern void abort (void);
6 extern void *memset (void *, int, __SIZE_TYPE__);
7 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
8
9 struct A { unsigned char a; long long b; };
10 struct B { unsigned char a; long long b; struct A c[3]; };
11 struct C { struct A a; };
12 struct D { unsigned char a; long long b; struct C c; };
13 union U { unsigned char a; long long b; };
14
15 __attribute__((noipa)) void
16 check_A_padding (struct A *p)
17 {
18 unsigned char *q = (unsigned char *) p;
19 unsigned char *r = (unsigned char *) p;
20 for (q += offsetof (struct A, a) + 1; q != r + offsetof (struct A, b); ++q)
21 if (*q != 0)
22 abort ();
23 }
24
25 __attribute__((noipa)) void
26 check_B_padding (struct B *p)
27 {
28 unsigned char *q = (unsigned char *) p;
29 unsigned char *r = (unsigned char *) p;
30 for (q += offsetof (struct B, a) + 1; q != r + offsetof (struct B, b); ++q)
31 if (*q != 0)
32 abort ();
33 for (int i = 0; i < 3; ++i)
34 check_A_padding (&p->c[i]);
35 }
36
37 __attribute__((noipa)) void
38 check_D_padding (struct D *p)
39 {
40 unsigned char *q = (unsigned char *) p;
41 unsigned char *r = (unsigned char *) p;
42 for (q += offsetof (struct D, a) + 1; q != r + offsetof (struct D, b); ++q)
43 if (*q != 0)
44 abort ();
45 check_A_padding (&p->c.a);
46 }
47
48 __attribute__((noipa)) void
49 check_U_padding (union U *p)
50 {
51 unsigned char *q = (unsigned char *) p;
52 unsigned char *r = (unsigned char *) p;
53 for (q += 1; q != r + sizeof (union U); ++q)
54 if (*q != 0)
55 abort ();
56 }
57
58 __attribute__((noipa)) void
59 check (struct A *a, struct B *b, struct B *c, struct B *d, struct B *e,
60 struct B *f, struct B *g, union U *h, union U *i, union U *j,
61 union U *k, struct D *l, struct D *m, struct D *n)
62 {
63 /* All padding bits are well defined with -fzero-init-padding-bits=all. */
64 if (a->a != 0 || a->b != 0)
65 abort ();
66 check_A_padding (a);
67 if (b->a != 0 || b->b != 0)
68 abort ();
69 for (int i = 0; i < 3; ++i)
70 if (b->c[i].a != 0 || b->c[i].b != 0)
71 abort ();
72 check_B_padding (b);
73 if (c->a != 1 || c->b != 2)
74 abort ();
75 for (int i = 0; i < 3; ++i)
76 if (c->c[i].a != 0 || c->c[i].b != 0)
77 abort ();
78 check_B_padding (c);
79 if (d->a != 2 || d->b != 1)
80 abort ();
81 for (int i = 0; i < 2; ++i)
82 if (d->c[i].a != 0 || d->c[i].b != 0)
83 abort ();
84 if (d->c[2].a != 3 || d->c[2].b != 4)
85 abort ();
86 check_B_padding (d);
87 if (e->a != 1 || e->b != 2)
88 abort ();
89 for (int i = 0; i < 3; ++i)
90 if (e->c[i].a != 3 + 2 * i || e->c[i].b != 4 + 2 * i)
91 abort ();
92 check_B_padding (e);
93 if (f->a != 1 || f->b != 2)
94 abort ();
95 for (int i = 0; i < 3; ++i)
96 if (f->c[i].a != 3 + 2 * i || f->c[i].b != 4 + 2 * i)
97 abort ();
98 check_B_padding (f);
99 if (g->a != 1 || g->b != 2)
100 abort ();
101 for (int i = 0; i < 3; ++i)
102 if (g->c[i].a != 3 + 2 * i || g->c[i].b != 4 + 2 * i)
103 abort ();
104 check_B_padding (g);
105 if (h->a != 0)
106 abort ();
107 check_U_padding (h);
108 if (i->a != 1 || j->a != 1)
109 abort ();
110 check_U_padding (i);
111 check_U_padding (j);
112 /* In *k k->b is initialized and there is (likely) no padding. */
113 if (k->b != 1)
114 abort ();
115 if (l->a != 0 || l->b != 0 || l->c.a.a != 0 || l->c.a.b != 0)
116 abort ();
117 check_D_padding (l);
118 if (m->a != 1 || m->b != 2 || m->c.a.a != 0 || m->c.a.b != 0)
119 abort ();
120 check_D_padding (m);
121 if (n->a != 1 || n->b != 2 || n->c.a.a != 3 || n->c.a.b != 4)
122 abort ();
123 check_D_padding (n);
124 }
125
126 __attribute__((noipa)) void
127 test (void)
128 {
129 struct A a = {};
130 struct B b = {};
131 struct B c = { 1, 2 };
132 struct B d = { .b = 1, .a = 2, .c[2].a = 3, .c[2].b = 4 };
133 struct B e = { 1, 2, .c[2] = {}, .c[1] = { 9 }, .c[0] = {},
134 .c[0].a = 3, .c[0].b = 4, .c[1].a = 5, .c[1].b = 6,
135 .c[2].a = 7, .c[2].b = 8 };
136 struct B f = { 1, 2, {},
137 .c[0].a = 3, .c[0].b = 4, .c[1].a = 5, .c[1].b = 6,
138 .c[2].a = 7, .c[2].b = 8 };
139 struct B g = { 1, 2, .c[0].a = 3, .c[0].b = 4, .c[1].a = 5, .c[1].b = 6,
140 .c[2].a = 7, .c[2].b = 8 };
141 union U h = {};
142 union U i = { 1 };
143 union U j = { .a = 1 };
144 union U k = { .b = 1 };
145 struct D l = {};
146 struct D m = { 1, 2 };
147 struct D n = { 1, 2, {}, .c.a.a = 3, .c.a.b = 4 };
148 check (&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m, &n);
149 }
150
151 __attribute__((noipa)) void
152 set (struct A *a, struct B *b, struct B *c, struct B *d, struct B *e,
153 struct B *f, struct B *g, union U *h, union U *i, union U *j,
154 union U *k, struct D *l, struct D *m, struct D *n)
155 {
156 memset (a, ~0, sizeof (*a));
157 memset (b, ~0, sizeof (*b));
158 memset (c, ~0, sizeof (*c));
159 memset (d, ~0, sizeof (*d));
160 memset (e, ~0, sizeof (*e));
161 memset (f, ~0, sizeof (*f));
162 memset (g, ~0, sizeof (*g));
163 memset (h, ~0, sizeof (*h));
164 memset (i, ~0, sizeof (*i));
165 memset (j, ~0, sizeof (*j));
166 memset (k, ~0, sizeof (*k));
167 memset (l, ~0, sizeof (*l));
168 memset (m, ~0, sizeof (*m));
169 memset (n, ~0, sizeof (*n));
170 }
171
172 __attribute__((noipa)) void
173 prepare (void)
174 {
175 struct A a;
176 struct B b, c, d, e, f, g;
177 union U h, i, j, k;
178 struct D l, m, n;
179 set (&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m, &n);
180 }
181
182 int
183 main ()
184 {
185 prepare ();
186 test ();
187 }