Skip to content

Commit 3ca2fa7

Browse files
authored
[CLANG][MS-STRUCT] bitfield padding warning presents padding to exact bit count (#136062)
Aims to fix #131647.
1 parent 6d765e1 commit 3ca2fa7

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

clang/lib/AST/RecordLayoutBuilder.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,7 @@ void ItaniumRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
15391539
uint64_t StorageUnitSize = FieldInfo.Width;
15401540
unsigned FieldAlign = FieldInfo.Align;
15411541
bool AlignIsRequired = FieldInfo.isAlignRequired();
1542+
unsigned char PaddingInLastUnit = 0;
15421543

15431544
// UnfilledBitsInLastUnit is the difference between the end of the
15441545
// last allocated bitfield (i.e. the first bit offset available for
@@ -1611,6 +1612,7 @@ void ItaniumRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
16111612
if (!LastBitfieldStorageUnitSize && !FieldSize)
16121613
FieldAlign = 1;
16131614

1615+
PaddingInLastUnit = UnfilledBitsInLastUnit;
16141616
UnfilledBitsInLastUnit = 0;
16151617
LastBitfieldStorageUnitSize = 0;
16161618
}
@@ -1707,7 +1709,7 @@ void ItaniumRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
17071709
// For purposes of diagnostics, we're going to simultaneously
17081710
// compute the field offsets that we would have used if we weren't
17091711
// adding any alignment padding or if the field weren't packed.
1710-
uint64_t UnpaddedFieldOffset = FieldOffset;
1712+
uint64_t UnpaddedFieldOffset = FieldOffset - PaddingInLastUnit;
17111713
uint64_t UnpackedFieldOffset = FieldOffset;
17121714

17131715
// Check if we need to add padding to fit the bitfield within an

clang/test/SemaCXX/windows-Wpadded-bitfield.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -triple x86_64-windows-msvc -fsyntax-only -verify -Wpadded %s
2+
// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsyntax-only -verify -Wpadded %s
23

34
struct __attribute__((ms_struct)) BitfieldStruct { // expected-warning {{padding size of 'BitfieldStruct' with 3 bytes to alignment boundary}}
45
char c : 1;
@@ -24,9 +25,23 @@ struct __attribute__((ms_struct)) DifferentUnitSizeBitfield { // expected-warnin
2425
char i; // expected-warning {{padding struct 'DifferentUnitSizeBitfield' with 31 bits to align 'i'}}
2526
};
2627

28+
struct __attribute__((ms_struct)) BitfieldBigPadding { // expected-warning {{padding size of 'BitfieldBigPadding' with 63 bits to alignment boundary}}
29+
long long x;
30+
char a : 1;
31+
long long b : 1; // expected-warning {{padding struct 'BitfieldBigPadding' with 63 bits to align 'b'}}
32+
};
33+
34+
struct __attribute__((ms_struct)) SameUnitSizeMultiple { // expected-warning {{padding size of 'SameUnitSizeMultiple' with 2 bits to alignment boundary}}
35+
char c : 1;
36+
char cc : 2;
37+
char ccc : 3;
38+
};
39+
2740
int main() {
2841
BitfieldStruct b;
2942
SevenBitfieldStruct s;
3043
SameUnitSizeBitfield su;
3144
DifferentUnitSizeBitfield du;
45+
BitfieldBigPadding bbp;
46+
SameUnitSizeMultiple susm;
3247
}

0 commit comments

Comments
 (0)