Handle zero-width bitfield member

This commit is contained in:
Rui Ueyama 2020-08-23 11:47:16 +09:00
parent ce74e67a28
commit 9eabd9516d
2 changed files with 9 additions and 1 deletions

View File

@ -2140,7 +2140,11 @@ static Type *struct_decl(Token **rest, Token *tok) {
int bits = 0;
for (Member *mem = ty->members; mem; mem = mem->next) {
if (mem->is_bitfield) {
if (mem->is_bitfield && mem->bit_width == 0) {
// Zero-width anonymous bitfield has a special meaning.
// It affects only alignment.
bits = align_to(bits, mem->ty->size * 8);
} else if (mem->is_bitfield) {
int sz = mem->ty->size;
if (bits / (sz * 8) != (bits + mem->bit_width - 1) / (sz * 8))
bits = align_to(bits, sz * 8);

View File

@ -48,6 +48,10 @@ int main() {
ASSERT(3, ({ T3 x={1,2,3}; ++x.b; }));
ASSERT(4, ({ T3 x={1,2,3}; ++x.c; }));
ASSERT(4, sizeof(struct {int a:3; int c:1; int c:5;}));
ASSERT(8, sizeof(struct {int a:3; int:0; int c:5;}));
ASSERT(4, sizeof(struct {int a:3; int:0;}));
printf("OK\n");
return 0;
}