Skip to content

Commit 439b16e

Browse files
authored
[LLDB] Respect the DW_AT_alignment attribute. (#73307)
Part of fixes for #72913. clang emits `DW_AT_alignment` attribute, however LLDB didn't respect it, resulting in incorrect RecordDecls built by lldb. This only fixes non-inheritance cases. The inheritance case will be handled in a follow-up patch.
1 parent c66c15a commit 439b16e

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,10 @@ ParsedDWARFTypeAttributes::ParsedDWARFTypeAttributes(const DWARFDIE &die) {
355355
byte_size = form_value.Unsigned();
356356
break;
357357

358+
case DW_AT_alignment:
359+
alignment = form_value.Unsigned();
360+
break;
361+
358362
case DW_AT_byte_stride:
359363
byte_stride = form_value.Unsigned();
360364
break;
@@ -1921,17 +1925,21 @@ DWARFASTParserClang::ParseStructureLikeDIE(const SymbolContext &sc,
19211925
die.GetOffset(), attrs.name.GetCString());
19221926
}
19231927

1924-
// If the byte size of the record is specified then overwrite the size
1925-
// that would be computed by Clang. This is only needed as LLDB's
1926-
// TypeSystemClang is always in C++ mode, but some compilers such as
1927-
// GCC and Clang give empty structs a size of 0 in C mode (in contrast to
1928-
// the size of 1 for empty structs that would be computed in C++ mode).
1929-
if (attrs.byte_size) {
1928+
// Setting authority byte size and alignment for empty structures.
1929+
//
1930+
// If the byte size or alignmenet of the record is specified then
1931+
// overwrite the ones that would be computed by Clang.
1932+
// This is only needed as LLDB's TypeSystemClang is always in C++ mode,
1933+
// but some compilers such as GCC and Clang give empty structs a size of 0
1934+
// in C mode (in contrast to the size of 1 for empty structs that would be
1935+
// computed in C++ mode).
1936+
if (attrs.byte_size || attrs.alignment) {
19301937
clang::RecordDecl *record_decl =
19311938
TypeSystemClang::GetAsRecordDecl(clang_type);
19321939
if (record_decl) {
19331940
ClangASTImporter::LayoutInfo layout;
1934-
layout.bit_size = *attrs.byte_size * 8;
1941+
layout.bit_size = attrs.byte_size.value_or(0) * 8;
1942+
layout.alignment = attrs.alignment.value_or(0) * 8;
19351943
GetClangASTImporter().SetRecordLayout(record_decl, layout);
19361944
}
19371945
}
@@ -2270,6 +2278,9 @@ bool DWARFASTParserClang::CompleteRecordType(const DWARFDIE &die,
22702278
if (layout_info.bit_size == 0)
22712279
layout_info.bit_size =
22722280
die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8;
2281+
if (layout_info.alignment == 0)
2282+
layout_info.alignment =
2283+
die.GetAttributeValueAsUnsigned(llvm::dwarf::DW_AT_alignment, 0) * 8;
22732284

22742285
clang::CXXRecordDecl *record_decl =
22752286
m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());

lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ struct ParsedDWARFTypeAttributes {
456456
lldb_private::plugin::dwarf::DWARFFormValue type;
457457
lldb::LanguageType class_language = lldb::eLanguageTypeUnknown;
458458
std::optional<uint64_t> byte_size;
459+
std::optional<uint64_t> alignment;
459460
size_t calling_convention = llvm::dwarf::DW_CC_normal;
460461
uint32_t bit_stride = 0;
461462
uint32_t byte_stride = 0;

lldb/test/API/lang/cpp/alignas_base_class/TestAlignAsBaseClass.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ def test(self):
1212

1313
# The offset of f2 should be 8 because of `alignas(8)`.
1414
self.expect_expr("(intptr_t)&d3g.f2 - (intptr_t)&d3g", result_value="8")
15+
16+
# Verify specified class alignments.
17+
self.expect_expr("alignof(B2)", result_value="8")
18+
self.expect_expr("alignof(EmptyClassAlign8)", result_value="8")

lldb/test/API/lang/cpp/alignas_base_class/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ struct D : B1, B2 {};
1010

1111
D d3g;
1212

13+
struct alignas(8) EmptyClassAlign8 {
14+
} t;
15+
1316
int main() {}

0 commit comments

Comments
 (0)