Debian Codegraph @

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

raw
1 /* Test GNU C11 support for empty initializers. */
2 /* { dg-do run } */
3 /* { dg-options "-std=gnu11 -fzero-init-padding-bits=unions" } */
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 /* Empty initializer in GNU C11 doesn't guarantee anything about
64 padding bits in the initializer directly, but padding in omitted members
65 is guaranteed to be zero initialized since C11. */
66 if (a->a != 0 || a->b != 0)
67 abort ();
68 if (b->a != 0 || b->b != 0)
69 abort ();
70 for (int i = 0; i < 3; ++i)
71 if (b->c[i].a != 0 || b->c[i].b != 0)
72 abort ();
73 else
74 check_A_padding (&b->c[i]);
75 /* In *c the padding between c->a and c->b is indeterminate, but
76 padding in c->c[0] (and 1 and 2) zero initialized (already since C11). */
77 if (c->a != 1 || c->b != 2)
78 abort ();
79 for (int i = 0; i < 3; ++i)
80 if (c->c[i].a != 0 || c->c[i].b != 0)
81 abort ();
82 else
83 check_A_padding (&c->c[i]);
84 /* In *d the padding between d->a and d->b is indeterminate, but
85 padding in d->c[0] (and 1) zero initialized (already since C11),
86 padding in d->c[2] again indeterminate. */
87 if (d->a != 2 || d->b != 1)
88 abort ();
89 for (int i = 0; i < 2; ++i)
90 if (d->c[i].a != 0 || d->c[i].b != 0)
91 abort ();
92 else
93 check_A_padding (&d->c[i]);
94 if (d->c[2].a != 3 || d->c[2].b != 4)
95 abort ();
96 /* In *e all padding is indeterminate. */
97 if (e->a != 1 || e->b != 2)
98 abort ();
99 for (int i = 0; i < 3; ++i)
100 if (e->c[i].a != 3 + 2 * i || e->c[i].b != 4 + 2 * i)
101 abort ();
102 /* In *f likewise. */
103 if (f->a != 1 || f->b != 2)
104 abort ();
105 for (int i = 0; i < 3; ++i)
106 if (f->c[i].a != 3 + 2 * i || f->c[i].b != 4 + 2 * i)
107 abort ();
108 /* In *g all padding is indeterminate. */
109 if (g->a != 1 || g->b != 2)
110 abort ();
111 for (int i = 0; i < 3; ++i)
112 if (g->c[i].a != 3 + 2 * i || g->c[i].b != 4 + 2 * i)
113 abort ();
114 /* In *h h->a is default initialized and padding indeterminate. */
115 if (h->a != 0)
116 abort ();
117 /* But -fzero-init-padding-bits=unions overrides that. */
118 check_U_padding (h);
119 /* In *i (and *j) i->a is initialized and padding indeterminate. */
120 if (i->a != 1 || j->a != 1)
121 abort ();
122 /* But -fzero-init-padding-bits=unions overrides that. */
123 check_U_padding (i);
124 check_U_padding (j);
125 /* In *k k->b is initialized and there is (likely) no padding. */
126 if (k->b != 1)
127 abort ();
128 /* Padding in omitted members is zero initialized since C11. */
129 if (l->a != 0 || l->b != 0 || l->c.a.a != 0 || l->c.a.b != 0)
130 abort ();
131 check_A_padding (&l->c.a);
132 /* In *m the padding between m->a and m->b is indeterminate, but
133 padding in m->c.a is zero initialized (already since C11). */
134 if (m->a != 1 || m->b != 2 || m->c.a.a != 0 || m->c.a.b != 0)
135 abort ();
136 check_A_padding (&m->c.a);
137 /* In *n the padding between n->a and n->b is indeterminate,
138 and padding in n->c.a too. */
139 if (n->a != 1 || n->b != 2 || n->c.a.a != 3 || n->c.a.b != 4)
140 abort ();
141 }
142
143 __attribute__((noipa)) void
144 test (void)
145 {
146 struct A a = {};
147 struct B b = {};
148 struct B c = { 1, 2 };
149 struct B d = { .b = 1, .a = 2, .c[2].a = 3, .c[2].b = 4 };
150 struct B e = { 1, 2, .c[2] = {}, .c[1] = { 9 }, .c[0] = {},
151 .c[0].a = 3, .c[0].b = 4, .c[1].a = 5, .c[1].b = 6,
152 .c[2].a = 7, .c[2].b = 8 };
153 struct B f = { 1, 2, {},
154 .c[0].a = 3, .c[0].b = 4, .c[1].a = 5, .c[1].b = 6,
155 .c[2].a = 7, .c[2].b = 8 };
156 struct B g = { 1, 2, .c[0].a = 3, .c[0].b = 4, .c[1].a = 5, .c[1].b = 6,
157 .c[2].a = 7, .c[2].b = 8 };
158 union U h = {};
159 union U i = { 1 };
160 union U j = { .a = 1 };
161 union U k = { .b = 1 };
162 struct D l = {};
163 struct D m = { 1, 2 };
164 struct D n = { 1, 2, {}, .c.a.a = 3, .c.a.b = 4 };
165 check (&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m, &n);
166 }
167
168 __attribute__((noipa)) void
169 set (struct A *a, struct B *b, struct B *c, struct B *d, struct B *e,
170 struct B *f, struct B *g, union U *h, union U *i, union U *j,
171 union U *k, struct D *l, struct D *m, struct D *n)
172 {
173 memset (a, ~0, sizeof (*a));
174 memset (b, ~0, sizeof (*b));
175 memset (c, ~0, sizeof (*c));
176 memset (d, ~0, sizeof (*d));
177 memset (e, ~0, sizeof (*e));
178 memset (f, ~0, sizeof (*f));
179 memset (g, ~0, sizeof (*g));
180 memset (h, ~0, sizeof (*h));
181 memset (i, ~0, sizeof (*i));
182 memset (j, ~0, sizeof (*j));
183 memset (k, ~0, sizeof (*k));
184 memset (l, ~0, sizeof (*l));
185 memset (m, ~0, sizeof (*m));
186 memset (n, ~0, sizeof (*n));
187 }
188
189 __attribute__((noipa)) void
190 prepare (void)
191 {
192 struct A a;
193 struct B b, c, d, e, f, g;
194 union U h, i, j, k;
195 struct D l, m, n;
196 set (&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m, &n);
197 }
198
199 int
200 main ()
201 {
202 prepare ();
203 test ();
204 }