Skip to content

Commit eb1f2bb

Browse files
committed
[lldb] Extend FindTypes to optionally search by mangled type name
Swift types have mangled type names. This adds functionality to look up those types through the FindTypes API by searching for the mangled type name instead of the regular name.
1 parent cc13d4f commit eb1f2bb

File tree

7 files changed

+58
-16
lines changed

7 files changed

+58
-16
lines changed

lldb/include/lldb/Symbol/Type.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ FLAGS_ENUM(TypeQueryOptions){
8484
/// matching type is found. When false, the type query should find all
8585
/// matching types.
8686
e_find_one = (1u << 4),
87+
// If set, treat TypeQuery::m_name as a mangled name that should be searched.
88+
e_search_by_mangled_name = (1u << 5),
8789
};
8890
LLDB_MARK_AS_BITMASK_ENUM(TypeQueryOptions)
8991

@@ -300,6 +302,12 @@ class TypeQuery {
300302
m_options &= ~e_find_one;
301303
}
302304

305+
/// Returns true if the type query is supposed to treat the name to be searched
306+
/// as a mangled name.
307+
bool GetSearchByMangledName() const {
308+
return (m_options & e_search_by_mangled_name) != 0;
309+
}
310+
303311
/// Access the internal compiler context array.
304312
///
305313
/// Clients can use this to populate the context manually.

lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,10 +1032,16 @@ void SymbolFileCTF::FindTypes(const lldb_private::TypeQuery &match,
10321032

10331033
ConstString name = match.GetTypeBasename();
10341034
for (TypeSP type_sp : GetTypeList().Types()) {
1035-
if (type_sp && type_sp->GetName() == name) {
1036-
results.InsertUnique(type_sp);
1037-
if (results.Done(match))
1038-
return;
1035+
if (type_sp) {
1036+
auto type_name =
1037+
match.GetSearchByMangledName()
1038+
? type_sp->GetForwardCompilerType().GetMangledTypeName()
1039+
: type_sp->GetName();
1040+
if (type_name == name) {
1041+
results.InsertUnique(type_sp);
1042+
if (results.Done(match))
1043+
return;
1044+
}
10391045
}
10401046
}
10411047
}

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "DWARFDeclContext.h"
1515
#include "DWARFUnit.h"
1616
#include "lldb/Symbol/Type.h"
17+
#include "lldb/lldb-private-enumerations.h"
1718

1819
#include "llvm/ADT/iterator.h"
1920
#include "llvm/BinaryFormat/Dwarf.h"
@@ -368,7 +369,7 @@ lldb_private::Type *DWARFDIE::ResolveTypeUID(const DWARFDIE &die) const {
368369
return nullptr;
369370
}
370371

371-
static void GetDeclContextImpl(DWARFDIE die,
372+
static void GetDeclContextImpl(DWARFDIE die, bool use_mangled_name,
372373
llvm::SmallSet<lldb::user_id_t, 4> &seen,
373374
std::vector<CompilerContext> &context) {
374375
// Stop if we hit a cycle.
@@ -383,6 +384,13 @@ static void GetDeclContextImpl(DWARFDIE die,
383384
auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
384385
context.push_back({kind, ConstString(name)});
385386
};
387+
388+
// Since mangled names are unique there's no need to build an entire context.
389+
if (use_mangled_name) {
390+
push_ctx(CompilerContextKind::AnyType, die.GetMangledName());
391+
return;
392+
}
393+
386394
switch (die.Tag()) {
387395
case DW_TAG_module:
388396
push_ctx(CompilerContextKind::Module, die.GetName());
@@ -417,15 +425,15 @@ static void GetDeclContextImpl(DWARFDIE die,
417425
}
418426
}
419427

420-
std::vector<CompilerContext> DWARFDIE::GetDeclContext() const {
428+
std::vector<CompilerContext> DWARFDIE::GetDeclContext(bool use_mangled_name) const {
421429
llvm::SmallSet<lldb::user_id_t, 4> seen;
422430
std::vector<CompilerContext> context;
423-
GetDeclContextImpl(*this, seen, context);
431+
GetDeclContextImpl(*this, use_mangled_name, seen, context);
424432
std::reverse(context.begin(), context.end());
425433
return context;
426434
}
427435

428-
static void GetTypeLookupContextImpl(DWARFDIE die,
436+
static void GetTypeLookupContextImpl(DWARFDIE die, bool use_mangled_name,
429437
llvm::SmallSet<lldb::user_id_t, 4> &seen,
430438
std::vector<CompilerContext> &context) {
431439
// Stop if we hit a cycle.
@@ -434,6 +442,19 @@ static void GetTypeLookupContextImpl(DWARFDIE die,
434442
auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
435443
context.push_back({kind, ConstString(name)});
436444
};
445+
446+
// Since mangled names are unique there's no need to build an entire context.
447+
if (use_mangled_name) {
448+
push_ctx(CompilerContextKind::AnyType, die.GetMangledName());
449+
return;
450+
}
451+
452+
// If there is no name, then there is no need to look anything up for this
453+
// DIE.
454+
const char *name = die.GetName();
455+
if (!name || !name[0])
456+
return;
457+
437458
switch (die.Tag()) {
438459
case DW_TAG_namespace:
439460
push_ctx(CompilerContextKind::Namespace, die.GetName());
@@ -453,7 +474,7 @@ static void GetTypeLookupContextImpl(DWARFDIE die,
453474
break;
454475
case DW_TAG_typedef:
455476
push_ctx(CompilerContextKind::Typedef, die.GetName());
456-
break;
477+
break;
457478
case DW_TAG_base_type:
458479
push_ctx(CompilerContextKind::Builtin, die.GetName());
459480
break;
@@ -477,10 +498,10 @@ static void GetTypeLookupContextImpl(DWARFDIE die,
477498
}
478499
}
479500

480-
std::vector<CompilerContext> DWARFDIE::GetTypeLookupContext() const {
501+
std::vector<CompilerContext> DWARFDIE::GetTypeLookupContext(bool use_mangled_name) const {
481502
llvm::SmallSet<lldb::user_id_t, 4> seen;
482503
std::vector<CompilerContext> context;
483-
GetTypeLookupContextImpl(*this, seen, context);
504+
GetTypeLookupContextImpl(*this, use_mangled_name, seen, context);
484505
std::reverse(context.begin(), context.end());
485506
return context;
486507
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class DWARFDIE : public DWARFBaseDIE {
7272
/// Return this DIE's decl context as it is needed to look up types
7373
/// in Clang modules. This context will include any modules or functions that
7474
/// the type is declared in so an exact module match can be efficiently made.
75-
std::vector<CompilerContext> GetDeclContext() const;
75+
std::vector<CompilerContext> GetDeclContext(bool use_mangled_name = false) const;
7676

7777
/// Get a context to a type so it can be looked up.
7878
///
@@ -84,7 +84,8 @@ class DWARFDIE : public DWARFBaseDIE {
8484
/// appropriate time, like either the translation unit or at a function
8585
/// context. This is designed to allow users to efficiently look for types
8686
/// using a full or partial CompilerContext array.
87-
std::vector<CompilerContext> GetTypeLookupContext() const;
87+
std::vector<CompilerContext>
88+
GetTypeLookupContext(bool use_mangled_name = false) const;
8889

8990
DWARFDeclContext GetDWARFDeclContext() const;
9091

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2762,7 +2762,7 @@ void SymbolFileDWARF::FindTypes(const TypeQuery &query, TypeResults &results) {
27622762
if (query.GetModuleSearch())
27632763
die_context = die.GetDeclContext();
27642764
else
2765-
die_context = die.GetTypeLookupContext();
2765+
die_context = die.GetTypeLookupContext(query.GetSearchByMangledName());
27662766
assert(!die_context.empty());
27672767
if (!query.ContextMatches(die_context))
27682768
return true; // Keep iterating over index types, context mismatch.

lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,10 @@ void SymbolFileNativePDB::FindTypes(const lldb_private::TypeQuery &query,
17351735
continue;
17361736

17371737
// We resolved a type. Get the fully qualified name to ensure it matches.
1738-
ConstString name = type_sp->GetQualifiedName();
1738+
ConstString name =
1739+
query.GetSearchByMangledName()
1740+
? type_sp->GetForwardCompilerType().GetMangledTypeName()
1741+
: type_sp->GetQualifiedName();
17391742
TypeQuery type_match(name.GetStringRef(), TypeQueryOptions::e_exact_match);
17401743
if (query.ContextMatches(type_match.GetContextRef())) {
17411744
results.InsertUnique(type_sp);

lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,10 @@ void SymbolFilePDB::FindTypes(const lldb_private::TypeQuery &query,
15621562
if (iter == m_types.end())
15631563
continue;
15641564
// We resolved a type. Get the fully qualified name to ensure it matches.
1565-
ConstString name = iter->second->GetQualifiedName();
1565+
ConstString name =
1566+
query.GetSearchByMangledName()
1567+
? iter->second->GetForwardCompilerType().GetMangledTypeName()
1568+
: iter->second->GetQualifiedName();
15661569
TypeQuery type_match(name.GetStringRef(), TypeQueryOptions::e_exact_match);
15671570
if (query.ContextMatches(type_match.GetContextRef())) {
15681571
type_results.InsertUnique(iter->second);

0 commit comments

Comments
 (0)