Skip to content

Commit 66df614

Browse files
authored
Fix #pragma (packed, n) not emitting the alignment in debug info (#94673)
Debug info generation won't emit the alignment of types that have a standard alignment. It was not taking into account the that case. rdar://127785973
1 parent 6f2c610 commit 66df614

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,16 @@ using namespace clang::CodeGen;
5858

5959
static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) {
6060
auto TI = Ctx.getTypeInfo(Ty);
61-
return TI.isAlignRequired() ? TI.Align : 0;
61+
if (TI.isAlignRequired())
62+
return TI.Align;
63+
64+
// MaxFieldAlignmentAttr is the attribute added to types
65+
// declared after #pragma pack(n).
66+
if (auto *Decl = Ty->getAsRecordDecl())
67+
if (Decl->hasAttr<MaxFieldAlignmentAttr>())
68+
return TI.Align;
69+
70+
return 0;
6271
}
6372

6473
static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) {

clang/test/CodeGen/debug-info-packed-struct.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct layout2 {
5959
#pragma pack()
6060
// CHECK: l2_ofs0
6161
// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "l2_ofs1",
62-
// CHECK-SAME: {{.*}}size: 64, offset: 8)
62+
// CHECK-SAME: {{.*}}size: 64, align: 8, offset: 8)
6363
// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "l2_ofs9",
6464
// CHECK-SAME: {{.*}}size: 1, offset: 72, flags: DIFlagBitField, extraData: i64 72)
6565

@@ -81,7 +81,7 @@ struct layout3 {
8181
#pragma pack()
8282
// CHECK: l3_ofs0
8383
// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "l3_ofs4",
84-
// CHECK-SAME: {{.*}}size: 64, offset: 32)
84+
// CHECK-SAME: {{.*}}size: 64, align: 32, offset: 32)
8585
// CHECK: !DIDerivedType(tag: DW_TAG_member, name: "l3_ofs12",
8686
// CHECK-SAME: {{.*}}size: 1, offset: 96, flags: DIFlagBitField, extraData: i64 96)
8787

clang/test/CodeGenCXX/debug-info-struct-align.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ struct MyType2 {
2525
MyType2 mt2;
2626

2727
static_assert(alignof(MyType2) == 1, "alignof MyType2 is wrong");
28+
29+
#pragma pack(1)
30+
struct MyType3 {
31+
int m;
32+
};
33+
MyType3 mt3;
34+
35+
static_assert(alignof(MyType3) == 1, "alignof MyType3 is wrong");

0 commit comments

Comments
 (0)