Skip to content

Commit b09ba42

Browse files
committed
Bug 51277: [DWARF] DW_AT_alignment incorrect when
attribute((__aligned__)) is present but ignored` In the original code, the 'getDeclAlignIfRequired' function is used. The 'getDeclAlignIfRequired' function will return the max alignment of all aligned attributes if the type has aligned attributes. The function doesn't consider the type at all. The 'getTypeAlignIfRequired' function uses the type's alignment value, which also used by the 'alignof' function. I think we should use the function of 'getTypeAlignIfRequired'. Reviewed By: dblaikie, jmorse, wolfgangp Differential Revision: https://reviews.llvm.org/D124006
1 parent ba46ae7 commit b09ba42

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3561,7 +3561,11 @@ llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
35613561
return getOrCreateRecordFwdDecl(Ty, RDContext);
35623562

35633563
uint64_t Size = CGM.getContext().getTypeSize(Ty);
3564-
auto Align = getDeclAlignIfRequired(D, CGM.getContext());
3564+
// __attribute__((aligned)) can increase or decrease alignment *except* on a
3565+
// struct or struct member, where it only increases alignment unless 'packed'
3566+
// is also specified. To handle this case, the `getTypeAlignIfRequired` needs
3567+
// to be used.
3568+
auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
35653569

35663570
SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
35673571

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Test for debug info related to DW_AT_alignment attribute in the struct type.
2+
// RUN: %clang_cc1 -dwarf-version=5 -debug-info-kind=standalone -S -emit-llvm %s -o - | FileCheck %s
3+
4+
// CHECK-DAG: DICompositeType(tag: DW_TAG_structure_type, name: "MyType", {{.*}}, align: 32
5+
// CHECK-DAG: DICompositeType(tag: DW_TAG_structure_type, name: "MyType1", {{.*}}, align: 8
6+
// CHECK-DAG: DICompositeType(tag: DW_TAG_structure_type, name: "MyType2", {{.*}}, align: 8
7+
8+
struct MyType {
9+
int m;
10+
} __attribute__((aligned(1)));
11+
MyType mt;
12+
13+
static_assert(alignof(MyType) == 4, "alignof MyType is wrong");
14+
15+
struct MyType1 {
16+
int m;
17+
} __attribute__((packed, aligned(1)));
18+
MyType1 mt1;
19+
20+
static_assert(alignof(MyType1) == 1, "alignof MyType1 is wrong");
21+
22+
struct MyType2 {
23+
__attribute__((packed)) int m;
24+
} __attribute__((aligned(1)));
25+
MyType2 mt2;
26+
27+
static_assert(alignof(MyType2) == 1, "alignof MyType2 is wrong");

0 commit comments

Comments
 (0)