Pad bit-fields only to the next byte
Fixes #1054. Previously, bit-fields followed by another field were aligned to two bytes. Bit-fields ending the struct were (and continue to be) aligned only to a single byte. ``` struct s { unsigned int x : 4; }; struct t { unsigned int x : 4; unsigned int y; }; ``` Before: `sizeof(struct s) == 1`, sizeof(struct t) == 4` After: `sizeof(struct s) == 1` sizeof(struct t) == 3`
This commit is contained in:
committed by
Oliver Schmidt
parent
9e881a497e
commit
9858e47dfd
@@ -772,17 +772,19 @@ static SymEntry* ParseStructDecl (const char* Name)
|
|||||||
*/
|
*/
|
||||||
if (BitOffs > 0) {
|
if (BitOffs > 0) {
|
||||||
if (FieldWidth <= 0 || (BitOffs + FieldWidth) > INT_BITS) {
|
if (FieldWidth <= 0 || (BitOffs + FieldWidth) > INT_BITS) {
|
||||||
|
/* Bits needed to byte-align the next field. */
|
||||||
|
unsigned PaddingBitWidth = -BitOffs % CHAR_BITS;
|
||||||
|
|
||||||
/* We need an anonymous name */
|
/* We need an anonymous name */
|
||||||
AnonName (Ident, "bit-field");
|
AnonName (Ident, "bit-field");
|
||||||
|
|
||||||
/* Add an anonymous bit-field that aligns to the next
|
/* Add an anonymous bit-field that aligns to the next
|
||||||
** storage unit.
|
** byte.
|
||||||
*/
|
*/
|
||||||
AddBitField (Ident, StructSize, BitOffs, INT_BITS - BitOffs);
|
AddBitField (Ident, StructSize, BitOffs, PaddingBitWidth);
|
||||||
|
|
||||||
/* No bits left */
|
/* No bits left */
|
||||||
StructSize += SIZEOF_INT;
|
StructSize += (BitOffs + PaddingBitWidth) / CHAR_BITS;
|
||||||
BitOffs = 0;
|
BitOffs = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user