| 1 |
/* Test GNU C11 support for empty initializers. */ |
| 2 |
/* { dg-do run } */ |
| 3 |
/* { dg-options "-std=gnu11" } */ |
| 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 |
/* In *i (and *j) i->a is initialized and padding indeterminate. */ |
| 118 |
if (i->a != 1 || j->a != 1) |
| 119 |
abort (); |
| 120 |
/* In *k k->b is initialized and there is (likely) no padding. */ |
| 121 |
if (k->b != 1) |
| 122 |
abort (); |
| 123 |
/* Padding in omitted members is zero initialized since C11. */ |
| 124 |
if (l->a != 0 || l->b != 0 || l->c.a.a != 0 || l->c.a.b != 0) |
| 125 |
abort (); |
| 126 |
check_A_padding (&l->c.a); |
| 127 |
/* In *m the padding between m->a and m->b is indeterminate, but |
| 128 |
padding in m->c.a is zero initialized (already since C11). */ |
| 129 |
if (m->a != 1 || m->b != 2 || m->c.a.a != 0 || m->c.a.b != 0) |
| 130 |
abort (); |
| 131 |
check_A_padding (&m->c.a); |
| 132 |
/* In *n the padding between n->a and n->b is indeterminate, |
| 133 |
and padding in n->c.a too. */ |
| 134 |
if (n->a != 1 || n->b != 2 || n->c.a.a != 3 || n->c.a.b != 4) |
| 135 |
abort (); |
| 136 |
} |
| 137 |
|
| 138 |
__attribute__((noipa)) void |
| 139 |
test (void) |
| 140 |
{ |
| 141 |
struct A a = {}; |
| 142 |
struct B b = {}; |
| 143 |
struct B c = { 1, 2 }; |
| 144 |
struct B d = { .b = 1, .a = 2, .c[2].a = 3, .c[2].b = 4 }; |
| 145 |
struct B e = { 1, 2, .c[2] = {}, .c[1] = { 9 }, .c[0] = {}, |
| 146 |
.c[0].a = 3, .c[0].b = 4, .c[1].a = 5, .c[1].b = 6, |
| 147 |
.c[2].a = 7, .c[2].b = 8 }; |
| 148 |
struct B f = { 1, 2, {}, |
| 149 |
.c[0].a = 3, .c[0].b = 4, .c[1].a = 5, .c[1].b = 6, |
| 150 |
.c[2].a = 7, .c[2].b = 8 }; |
| 151 |
struct B g = { 1, 2, .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 |
union U h = {}; |
| 154 |
union U i = { 1 }; |
| 155 |
union U j = { .a = 1 }; |
| 156 |
union U k = { .b = 1 }; |
| 157 |
struct D l = {}; |
| 158 |
struct D m = { 1, 2 }; |
| 159 |
struct D n = { 1, 2, {}, .c.a.a = 3, .c.a.b = 4 }; |
| 160 |
check (&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m, &n); |
| 161 |
} |
| 162 |
|
| 163 |
__attribute__((noipa)) void |
| 164 |
set (struct A *a, struct B *b, struct B *c, struct B *d, struct B *e, |
| 165 |
struct B *f, struct B *g, union U *h, union U *i, union U *j, |
| 166 |
union U *k, struct D *l, struct D *m, struct D *n) |
| 167 |
{ |
| 168 |
memset (a, ~0, sizeof (*a)); |
| 169 |
memset (b, ~0, sizeof (*b)); |
| 170 |
memset (c, ~0, sizeof (*c)); |
| 171 |
memset (d, ~0, sizeof (*d)); |
| 172 |
memset (e, ~0, sizeof (*e)); |
| 173 |
memset (f, ~0, sizeof (*f)); |
| 174 |
memset (g, ~0, sizeof (*g)); |
| 175 |
memset (h, ~0, sizeof (*h)); |
| 176 |
memset (i, ~0, sizeof (*i)); |
| 177 |
memset (j, ~0, sizeof (*j)); |
| 178 |
memset (k, ~0, sizeof (*k)); |
| 179 |
memset (l, ~0, sizeof (*l)); |
| 180 |
memset (m, ~0, sizeof (*m)); |
| 181 |
memset (n, ~0, sizeof (*n)); |
| 182 |
} |
| 183 |
|
| 184 |
__attribute__((noipa)) void |
| 185 |
prepare (void) |
| 186 |
{ |
| 187 |
struct A a; |
| 188 |
struct B b, c, d, e, f, g; |
| 189 |
union U h, i, j, k; |
| 190 |
struct D l, m, n; |
| 191 |
set (&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m, &n); |
| 192 |
} |
| 193 |
|
| 194 |
int |
| 195 |
main () |
| 196 |
{ |
| 197 |
prepare (); |
| 198 |
test (); |
| 199 |
} |