Skip to content

Commit da79304

Browse files
author
Théo De Magalhaes
committed
fix: changed the empty struct padding handling to take into account structs with required alignments
1 parent 8a933e5 commit da79304

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

clang/lib/AST/RecordLayoutBuilder.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3231,6 +3231,11 @@ void MicrosoftRecordLayoutBuilder::finalizeLayout(const RecordDecl *RD) {
32313231
uint64_t UnpaddedSizeInBits = Context.toBits(DataSize);
32323232
UnpaddedSizeInBits -= RemainingBitsInField;
32333233

3234+
// MS ABI allocates 1 byte for empty class
3235+
// (not padding)
3236+
if (Size.isZero())
3237+
UnpaddedSizeInBits += 8;
3238+
32343239
// Respect required alignment. Note that in 32-bit mode Required alignment
32353240
// may be 0 and cause size not to be updated.
32363241
DataSize = Size;
@@ -3262,9 +3267,10 @@ void MicrosoftRecordLayoutBuilder::finalizeLayout(const RecordDecl *RD) {
32623267
return;
32633268
}
32643269
unsigned CharBitNum = Context.getTargetInfo().getCharWidth();
3265-
uint64_t DataSizeInBits = Context.toBits(DataSize);
3266-
if (DataSizeInBits > UnpaddedSizeInBits) {
3267-
unsigned int PadSize = DataSizeInBits - UnpaddedSizeInBits;
3270+
uint64_t SizeInBits = Context.toBits(Size);
3271+
3272+
if (SizeInBits > UnpaddedSizeInBits) {
3273+
unsigned int PadSize = SizeInBits - UnpaddedSizeInBits;
32683274
bool InBits = true;
32693275
if (PadSize % CharBitNum == 0) {
32703276
PadSize = PadSize / CharBitNum;

clang/test/SemaCXX/windows-Wpadded.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,26 @@ struct __attribute__((ms_struct)) StructWithUnion { // expected-warning {{paddin
3232
short i;
3333
};
3434

35-
struct __attribute__((ms_struct)) EmptyStruct {
35+
struct __attribute__((ms_struct)) EmptyStruct {};
3636

37+
struct __attribute__((ms_struct)) AlignedMemberStruct { // expected-warning {{padding size of 'AlignedMemberStruct' with 28 bytes to alignment boundary}}
38+
alignas(32) int x;
3739
};
3840

41+
struct alignas(32) __attribute__((ms_struct)) AlignedNonEmptyStruct { // expected-warning {{padding size of 'AlignedNonEmptyStruct' with 28 bytes to alignment boundary}}
42+
int x;
43+
};
44+
45+
46+
struct alignas(16) __attribute__((ms_struct)) AlignedEmptyStruct {}; // expected-warning {{padding size of 'AlignedEmptyStruct' with 15 bytes to alignment boundary}}
47+
3948
int main() {
4049
Foo f;
4150
AlignedStruct a;
4251
Derived d;
4352
StructWithUnion swu;
4453
EmptyStruct e;
54+
AlignedNonEmptyStruct anes;
55+
AlignedMemberStruct ams;
56+
AlignedEmptyStruct aes;
4557
}

0 commit comments

Comments
 (0)