Skip to content

Commit 9a020d8

Browse files
author
jeffreytan81
committed
Improve namespace lookup
1 parent 3f92956 commit 9a020d8

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,19 @@ bool DWARFIndex::ProcessTypeDieMatchQuery(
151151
return true;
152152
return callback(die);
153153
}
154+
155+
void DWARFIndex::GetNamespacesWithParents(
156+
ConstString name, const CompilerDeclContext &parent_decl_ctx,
157+
llvm::function_ref<bool(DWARFDIE die)> callback) {
158+
GetNamespaces(name, [&](DWARFDIE die) {
159+
return ProcessNamespaceDieMatchParents(parent_decl_ctx, die, callback);
160+
});
161+
}
162+
163+
bool DWARFIndex::ProcessNamespaceDieMatchParents(
164+
const CompilerDeclContext &parent_decl_ctx, DWARFDIE die,
165+
llvm::function_ref<bool(DWARFDIE die)> callback) {
166+
if (!SymbolFileDWARF::DIEInDeclContext(parent_decl_ctx, die))
167+
return true;
168+
return callback(die);
169+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ class DWARFIndex {
7171
virtual void
7272
GetTypesWithQuery(TypeQuery &query,
7373
llvm::function_ref<bool(DWARFDIE die)> callback);
74+
/// Get namespace DIEs whose base name match \param name with \param
75+
/// parent_decl_ctx in its decl parent chain. A base implementation
76+
/// is provided. Specializations should override this if they are able to
77+
/// provide a faster implementation.
78+
virtual void
79+
GetNamespacesWithParents(ConstString name,
80+
const CompilerDeclContext &parent_decl_ctx,
81+
llvm::function_ref<bool(DWARFDIE die)> callback);
7482
virtual void
7583
GetFunctions(const Module::LookupInfo &lookup_info, SymbolFileDWARF &dwarf,
7684
const CompilerDeclContext &parent_decl_ctx,
@@ -127,6 +135,9 @@ class DWARFIndex {
127135
bool
128136
ProcessTypeDieMatchQuery(TypeQuery &query, DWARFDIE die,
129137
llvm::function_ref<bool(DWARFDIE die)> callback);
138+
bool ProcessNamespaceDieMatchParents(
139+
const CompilerDeclContext &parent_decl_ctx, DWARFDIE die,
140+
llvm::function_ref<bool(DWARFDIE die)> callback);
130141
};
131142
} // namespace dwarf
132143
} // namespace lldb_private::plugin

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,48 @@ void DebugNamesDWARFIndex::GetTypesWithQuery(
565565
m_fallback.GetTypesWithQuery(query, callback);
566566
}
567567

568+
void DebugNamesDWARFIndex::GetNamespacesWithParents(
569+
ConstString name, const CompilerDeclContext &parent_decl_ctx,
570+
llvm::function_ref<bool(DWARFDIE die)> callback) {
571+
std::vector<lldb_private::CompilerContext> parent_contexts =
572+
parent_decl_ctx.GetCompilerContext();
573+
if (parent_contexts.empty())
574+
return GetNamespaces(name, callback);
575+
576+
llvm::SmallVector<CompilerContext> parent_named_contexts;
577+
std::copy_if(parent_contexts.rbegin(), parent_contexts.rend(),
578+
std::back_inserter(parent_named_contexts),
579+
[](const CompilerContext &ctx) { return !ctx.name.IsEmpty(); });
580+
for (const DebugNames::Entry &entry :
581+
m_debug_names_up->equal_range(name.GetStringRef())) {
582+
lldb_private::dwarf::Tag entry_tag = entry.tag();
583+
if (entry_tag == DW_TAG_namespace ||
584+
entry_tag == DW_TAG_imported_declaration) {
585+
std::optional<llvm::SmallVector<Entry, 4>> parent_chain =
586+
getParentChain(entry);
587+
if (!parent_chain) {
588+
// Fallback: use the base class implementation.
589+
if (!ProcessEntry(entry, [&](DWARFDIE die) {
590+
return ProcessNamespaceDieMatchParents(parent_decl_ctx, die,
591+
callback);
592+
}))
593+
return;
594+
continue;
595+
}
596+
597+
if (WithinParentChain(parent_named_contexts, *parent_chain) &&
598+
!ProcessEntry(entry, [&](DWARFDIE die) {
599+
// After .debug_names filtering still sending to base class for
600+
// further filtering before calling the callback.
601+
return ProcessNamespaceDieMatchParents(parent_decl_ctx, die,
602+
callback);
603+
}))
604+
return;
605+
}
606+
}
607+
m_fallback.GetNamespacesWithParents(name, parent_decl_ctx, callback);
608+
}
609+
568610
void DebugNamesDWARFIndex::GetFunctions(
569611
const Module::LookupInfo &lookup_info, SymbolFileDWARF &dwarf,
570612
const CompilerDeclContext &parent_decl_ctx,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ class DebugNamesDWARFIndex : public DWARFIndex {
5555
void
5656
GetTypesWithQuery(TypeQuery &query,
5757
llvm::function_ref<bool(DWARFDIE die)> callback) override;
58-
58+
void GetNamespacesWithParents(
59+
ConstString name, const CompilerDeclContext &parent_decl_ctx,
60+
llvm::function_ref<bool(DWARFDIE die)> callback) override;
5961
void GetFunctions(const Module::LookupInfo &lookup_info,
6062
SymbolFileDWARF &dwarf,
6163
const CompilerDeclContext &parent_decl_ctx,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2899,7 +2899,7 @@ SymbolFileDWARF::FindNamespace(ConstString name,
28992899
if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
29002900
return namespace_decl_ctx;
29012901

2902-
m_index->GetNamespaces(name, [&](DWARFDIE die) {
2902+
m_index->GetNamespacesWithParents(name, parent_decl_ctx, [&](DWARFDIE die) {
29032903
if (!DIEInDeclContext(parent_decl_ctx, die, only_root_namespaces))
29042904
return true; // The containing decl contexts don't match
29052905

0 commit comments

Comments
 (0)