Skip to content

Commit 57f99d0

Browse files
committed
[lldb] Reduce duplication in DWARFASTParserClang::CopyUniqueClassMethodTypes
Use lambdas to replace identical bits of code.
1 parent dc0ae8c commit 57f99d0

File tree

1 file changed

+55
-85
lines changed

1 file changed

+55
-85
lines changed

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

Lines changed: 55 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -3499,49 +3499,37 @@ bool DWARFASTParserClang::CopyUniqueClassMethodTypes(
34993499
// in "dst_cu" and "dst_class_die"
35003500
class_type->GetFullCompilerType();
35013501

3502-
DWARFDIE src_die;
3503-
DWARFDIE dst_die;
3502+
auto gather = [](DWARFDIE die, UniqueCStringMap<DWARFDIE> &map,
3503+
UniqueCStringMap<DWARFDIE> map_artificial) {
3504+
if (die.Tag() != DW_TAG_subprogram)
3505+
return;
3506+
// Make sure this is a declaration and not a concrete instance by looking
3507+
// for DW_AT_declaration set to 1. Sometimes concrete function instances are
3508+
// placed inside the class definitions and shouldn't be included in the list
3509+
// of things are are tracking here.
3510+
if (die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) != 1)
3511+
return;
3512+
3513+
if (const char *name = die.GetMangledName()) {
3514+
ConstString const_name(name);
3515+
if (die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
3516+
map_artificial.Append(const_name, die);
3517+
else
3518+
map.Append(const_name, die);
3519+
}
3520+
};
3521+
35043522
UniqueCStringMap<DWARFDIE> src_name_to_die;
35053523
UniqueCStringMap<DWARFDIE> dst_name_to_die;
35063524
UniqueCStringMap<DWARFDIE> src_name_to_die_artificial;
35073525
UniqueCStringMap<DWARFDIE> dst_name_to_die_artificial;
3508-
for (src_die = src_class_die.GetFirstChild(); src_die.IsValid();
3526+
for (DWARFDIE src_die = src_class_die.GetFirstChild(); src_die.IsValid();
35093527
src_die = src_die.GetSibling()) {
3510-
if (src_die.Tag() == DW_TAG_subprogram) {
3511-
// Make sure this is a declaration and not a concrete instance by looking
3512-
// for DW_AT_declaration set to 1. Sometimes concrete function instances
3513-
// are placed inside the class definitions and shouldn't be included in
3514-
// the list of things are are tracking here.
3515-
if (src_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) {
3516-
const char *src_name = src_die.GetMangledName();
3517-
if (src_name) {
3518-
ConstString src_const_name(src_name);
3519-
if (src_die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
3520-
src_name_to_die_artificial.Append(src_const_name, src_die);
3521-
else
3522-
src_name_to_die.Append(src_const_name, src_die);
3523-
}
3524-
}
3525-
}
3528+
gather(src_die, src_name_to_die, src_name_to_die_artificial);
35263529
}
3527-
for (dst_die = dst_class_die.GetFirstChild(); dst_die.IsValid();
3530+
for (DWARFDIE dst_die = dst_class_die.GetFirstChild(); dst_die.IsValid();
35283531
dst_die = dst_die.GetSibling()) {
3529-
if (dst_die.Tag() == DW_TAG_subprogram) {
3530-
// Make sure this is a declaration and not a concrete instance by looking
3531-
// for DW_AT_declaration set to 1. Sometimes concrete function instances
3532-
// are placed inside the class definitions and shouldn't be included in
3533-
// the list of things are are tracking here.
3534-
if (dst_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) {
3535-
const char *dst_name = dst_die.GetMangledName();
3536-
if (dst_name) {
3537-
ConstString dst_const_name(dst_name);
3538-
if (dst_die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
3539-
dst_name_to_die_artificial.Append(dst_const_name, dst_die);
3540-
else
3541-
dst_name_to_die.Append(dst_const_name, dst_die);
3542-
}
3543-
}
3544-
}
3532+
gather(dst_die, dst_name_to_die, dst_name_to_die_artificial);
35453533
}
35463534
const uint32_t src_size = src_name_to_die.GetSize();
35473535
const uint32_t dst_size = dst_name_to_die.GetSize();
@@ -3556,8 +3544,8 @@ bool DWARFASTParserClang::CopyUniqueClassMethodTypes(
35563544

35573545
if (fast_path) {
35583546
for (idx = 0; idx < src_size; ++idx) {
3559-
src_die = src_name_to_die.GetValueAtIndexUnchecked(idx);
3560-
dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3547+
DWARFDIE src_die = src_name_to_die.GetValueAtIndexUnchecked(idx);
3548+
DWARFDIE dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
35613549

35623550
if (src_die.Tag() != dst_die.Tag())
35633551
fast_path = false;
@@ -3575,28 +3563,29 @@ bool DWARFASTParserClang::CopyUniqueClassMethodTypes(
35753563

35763564
DWARFASTParserClang *src_dwarf_ast_parser =
35773565
static_cast<DWARFASTParserClang *>(
3578-
SymbolFileDWARF::GetDWARFParser(*src_die.GetCU()));
3566+
SymbolFileDWARF::GetDWARFParser(*src_class_die.GetCU()));
35793567
DWARFASTParserClang *dst_dwarf_ast_parser =
35803568
static_cast<DWARFASTParserClang *>(
3581-
SymbolFileDWARF::GetDWARFParser(*dst_die.GetCU()));
3569+
SymbolFileDWARF::GetDWARFParser(*dst_class_die.GetCU()));
3570+
auto link = [&](DWARFDIE src, DWARFDIE dst) {
3571+
SymbolFileDWARF::DIEToTypePtr &die_to_type =
3572+
dst_class_die.GetDWARF()->GetDIEToType();
3573+
clang::DeclContext *src_decl_ctx =
3574+
src_dwarf_ast_parser->m_die_to_decl_ctx[src.GetDIE()];
3575+
if (src_decl_ctx)
3576+
dst_dwarf_ast_parser->LinkDeclContextToDIE(src_decl_ctx, dst);
3577+
3578+
if (Type *src_child_type = die_to_type[src.GetDIE()])
3579+
die_to_type[dst.GetDIE()] = src_child_type;
3580+
};
35823581

35833582
// Now do the work of linking the DeclContexts and Types.
35843583
if (fast_path) {
35853584
// We can do this quickly. Just run across the tables index-for-index
35863585
// since we know each node has matching names and tags.
35873586
for (idx = 0; idx < src_size; ++idx) {
3588-
src_die = src_name_to_die.GetValueAtIndexUnchecked(idx);
3589-
dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3590-
3591-
clang::DeclContext *src_decl_ctx =
3592-
src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3593-
if (src_decl_ctx)
3594-
dst_dwarf_ast_parser->LinkDeclContextToDIE(src_decl_ctx, dst_die);
3595-
3596-
Type *src_child_type =
3597-
dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
3598-
if (src_child_type)
3599-
dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type;
3587+
link(src_name_to_die.GetValueAtIndexUnchecked(idx),
3588+
dst_name_to_die.GetValueAtIndexUnchecked(idx));
36003589
}
36013590
} else {
36023591
// We must do this slowly. For each member of the destination, look up a
@@ -3608,24 +3597,13 @@ bool DWARFASTParserClang::CopyUniqueClassMethodTypes(
36083597

36093598
for (idx = 0; idx < dst_size; ++idx) {
36103599
ConstString dst_name = dst_name_to_die.GetCStringAtIndex(idx);
3611-
dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3612-
src_die = src_name_to_die.Find(dst_name, DWARFDIE());
3613-
3614-
if (src_die && (src_die.Tag() == dst_die.Tag())) {
3615-
clang::DeclContext *src_decl_ctx =
3616-
src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3617-
if (src_decl_ctx)
3618-
dst_dwarf_ast_parser->LinkDeclContextToDIE(src_decl_ctx, dst_die);
3619-
3620-
Type *src_child_type =
3621-
dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
3622-
if (src_child_type) {
3623-
dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] =
3624-
src_child_type;
3625-
}
3626-
} else {
3600+
DWARFDIE dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3601+
DWARFDIE src_die = src_name_to_die.Find(dst_name, DWARFDIE());
3602+
3603+
if (src_die && (src_die.Tag() == dst_die.Tag()))
3604+
link(src_die, dst_die);
3605+
else
36273606
failures.push_back(dst_die);
3628-
}
36293607
}
36303608
}
36313609
}
@@ -3639,29 +3617,21 @@ bool DWARFASTParserClang::CopyUniqueClassMethodTypes(
36393617
for (idx = 0; idx < src_size_artificial; ++idx) {
36403618
ConstString src_name_artificial =
36413619
src_name_to_die_artificial.GetCStringAtIndex(idx);
3642-
src_die = src_name_to_die_artificial.GetValueAtIndexUnchecked(idx);
3643-
dst_die =
3620+
DWARFDIE src_die =
3621+
src_name_to_die_artificial.GetValueAtIndexUnchecked(idx);
3622+
DWARFDIE dst_die =
36443623
dst_name_to_die_artificial.Find(src_name_artificial, DWARFDIE());
36453624

3646-
if (dst_die) {
3647-
// Both classes have the artificial types, link them
3648-
clang::DeclContext *src_decl_ctx =
3649-
src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3650-
if (src_decl_ctx)
3651-
dst_dwarf_ast_parser->LinkDeclContextToDIE(src_decl_ctx, dst_die);
3652-
3653-
Type *src_child_type =
3654-
dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
3655-
if (src_child_type)
3656-
dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type;
3657-
}
3625+
// Both classes have the artificial types, link them
3626+
if (dst_die)
3627+
link(src_die, dst_die);
36583628
}
36593629
}
36603630

36613631
if (dst_size_artificial) {
36623632
for (idx = 0; idx < dst_size_artificial; ++idx) {
3663-
dst_die = dst_name_to_die_artificial.GetValueAtIndexUnchecked(idx);
3664-
failures.push_back(dst_die);
3633+
failures.push_back(
3634+
dst_name_to_die_artificial.GetValueAtIndexUnchecked(idx));
36653635
}
36663636
}
36673637

0 commit comments

Comments
 (0)