[Lldb-commits] [lldb] [lldb] Extend FindTypes to optionally search by mangled type name (PR #113007)

via lldb-commits lldb-commits at lists.llvm.org
Fri Oct 18 17:05:33 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Augusto Noronha (augusto2112)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/113007.diff


7 Files Affected:

- (modified) lldb/include/lldb/Symbol/Type.h (+8) 
- (modified) lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp (+10-4) 
- (modified) lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp (+28-7) 
- (modified) lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.h (+3-2) 
- (modified) lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp (+1-1) 
- (modified) lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp (+4-1) 
- (modified) lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp (+4-1) 


``````````diff
diff --git a/lldb/include/lldb/Symbol/Type.h b/lldb/include/lldb/Symbol/Type.h
index 03d9f927997476..b90cfc44e907b9 100644
--- a/lldb/include/lldb/Symbol/Type.h
+++ b/lldb/include/lldb/Symbol/Type.h
@@ -84,6 +84,8 @@ FLAGS_ENUM(TypeQueryOptions){
     /// matching type is found. When false, the type query should find all
     /// matching types.
     e_find_one = (1u << 4),
+    // If set, treat TypeQuery::m_name as a mangled name that should be searched.
+    e_search_by_mangled_name = (1u << 5),
 };
 LLDB_MARK_AS_BITMASK_ENUM(TypeQueryOptions)
 
@@ -300,6 +302,12 @@ class TypeQuery {
       m_options &= ~e_find_one;
   }
 
+  /// Returns true if the type query is supposed to treat the name to be searched
+  /// as a mangled name.
+  bool GetSearchByMangledName() const {
+    return (m_options & e_search_by_mangled_name) != 0;
+  }
+
   /// Access the internal compiler context array.
   ///
   /// Clients can use this to populate the context manually.
diff --git a/lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp b/lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp
index bb738c3dcc54a0..ee63c21663128a 100644
--- a/lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp
+++ b/lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp
@@ -1032,10 +1032,16 @@ void SymbolFileCTF::FindTypes(const lldb_private::TypeQuery &match,
 
   ConstString name = match.GetTypeBasename();
   for (TypeSP type_sp : GetTypeList().Types()) {
-    if (type_sp && type_sp->GetName() == name) {
-      results.InsertUnique(type_sp);
-      if (results.Done(match))
-        return;
+    if (type_sp) {
+      auto type_name =
+          match.GetSearchByMangledName()
+              ? type_sp->GetForwardCompilerType().GetMangledTypeName()
+              : type_sp->GetName();
+      if (type_name == name) {
+        results.InsertUnique(type_sp);
+        if (results.Done(match))
+          return;
+      }
     }
   }
 }
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
index d83740f8e2113b..d4604d72b3a3b6 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
@@ -14,6 +14,7 @@
 #include "DWARFDeclContext.h"
 #include "DWARFUnit.h"
 #include "lldb/Symbol/Type.h"
+#include "lldb/lldb-private-enumerations.h"
 
 #include "llvm/ADT/iterator.h"
 #include "llvm/BinaryFormat/Dwarf.h"
@@ -368,7 +369,7 @@ lldb_private::Type *DWARFDIE::ResolveTypeUID(const DWARFDIE &die) const {
   return nullptr;
 }
 
-static void GetDeclContextImpl(DWARFDIE die,
+static void GetDeclContextImpl(DWARFDIE die, bool use_mangled_name,
                                llvm::SmallSet<lldb::user_id_t, 4> &seen,
                                std::vector<CompilerContext> &context) {
   // Stop if we hit a cycle.
@@ -383,6 +384,13 @@ static void GetDeclContextImpl(DWARFDIE die,
     auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
       context.push_back({kind, ConstString(name)});
     };
+
+    // Since mangled names are unique there's no need to build an entire context.
+    if (use_mangled_name) {
+      push_ctx(CompilerContextKind::AnyType, die.GetMangledName());
+      return;
+    }
+
     switch (die.Tag()) {
     case DW_TAG_module:
       push_ctx(CompilerContextKind::Module, die.GetName());
@@ -417,15 +425,15 @@ static void GetDeclContextImpl(DWARFDIE die,
   }
 }
 
-std::vector<CompilerContext> DWARFDIE::GetDeclContext() const {
+std::vector<CompilerContext> DWARFDIE::GetDeclContext(bool use_mangled_name) const {
   llvm::SmallSet<lldb::user_id_t, 4> seen;
   std::vector<CompilerContext> context;
-  GetDeclContextImpl(*this, seen, context);
+  GetDeclContextImpl(*this, use_mangled_name, seen, context);
   std::reverse(context.begin(), context.end());
   return context;
 }
 
-static void GetTypeLookupContextImpl(DWARFDIE die,
+static void GetTypeLookupContextImpl(DWARFDIE die, bool use_mangled_name,
                                      llvm::SmallSet<lldb::user_id_t, 4> &seen,
                                      std::vector<CompilerContext> &context) {
   // Stop if we hit a cycle.
@@ -434,6 +442,19 @@ static void GetTypeLookupContextImpl(DWARFDIE die,
     auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
       context.push_back({kind, ConstString(name)});
     };
+
+    // Since mangled names are unique there's no need to build an entire context.
+    if (use_mangled_name) {
+      push_ctx(CompilerContextKind::AnyType, die.GetMangledName());
+      return;
+    }
+
+    // If there is no name, then there is no need to look anything up for this
+    // DIE.
+       const char *name = die.GetName();
+    if (!name || !name[0])
+      return;
+
     switch (die.Tag()) {
     case DW_TAG_namespace:
       push_ctx(CompilerContextKind::Namespace, die.GetName());
@@ -453,7 +474,7 @@ static void GetTypeLookupContextImpl(DWARFDIE die,
       break;
     case DW_TAG_typedef:
       push_ctx(CompilerContextKind::Typedef, die.GetName());
-      break;
+   break;
     case DW_TAG_base_type:
       push_ctx(CompilerContextKind::Builtin, die.GetName());
       break;
@@ -477,10 +498,10 @@ static void GetTypeLookupContextImpl(DWARFDIE die,
   }
 }
 
-std::vector<CompilerContext> DWARFDIE::GetTypeLookupContext() const {
+std::vector<CompilerContext> DWARFDIE::GetTypeLookupContext(bool use_mangled_name) const {
   llvm::SmallSet<lldb::user_id_t, 4> seen;
   std::vector<CompilerContext> context;
-  GetTypeLookupContextImpl(*this, seen, context);
+  GetTypeLookupContextImpl(*this, use_mangled_name, seen, context);
   std::reverse(context.begin(), context.end());
   return context;
 }
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.h b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.h
index e1318953a384cd..109070bdcb6e95 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.h
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.h
@@ -72,7 +72,7 @@ class DWARFDIE : public DWARFBaseDIE {
   /// Return this DIE's decl context as it is needed to look up types
   /// in Clang modules. This context will include any modules or functions that
   /// the type is declared in so an exact module match can be efficiently made.
-  std::vector<CompilerContext> GetDeclContext() const;
+  std::vector<CompilerContext> GetDeclContext(bool use_mangled_name = false) const;
 
   /// Get a context to a type so it can be looked up.
   ///
@@ -84,7 +84,8 @@ class DWARFDIE : public DWARFBaseDIE {
   /// appropriate time, like either the translation unit or at a function
   /// context. This is designed to allow users to efficiently look for types
   /// using a full or partial CompilerContext array.
-  std::vector<CompilerContext> GetTypeLookupContext() const;
+  std::vector<CompilerContext>
+  GetTypeLookupContext(bool use_mangled_name = false) const;
 
   DWARFDeclContext GetDWARFDeclContext() const;
 
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
index 9287d4baf19e9c..f1956f006de84d 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
@@ -2762,7 +2762,7 @@ void SymbolFileDWARF::FindTypes(const TypeQuery &query, TypeResults &results) {
     if (query.GetModuleSearch())
       die_context = die.GetDeclContext();
     else
-      die_context = die.GetTypeLookupContext();
+      die_context = die.GetTypeLookupContext(query.GetSearchByMangledName());
     assert(!die_context.empty());
     if (!query.ContextMatches(die_context))
       return true; // Keep iterating over index types, context mismatch.
diff --git a/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp b/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp
index 7fded6a31a3af5..b55518a136c997 100644
--- a/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp
+++ b/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp
@@ -1735,7 +1735,10 @@ void SymbolFileNativePDB::FindTypes(const lldb_private::TypeQuery &query,
       continue;
 
     // We resolved a type. Get the fully qualified name to ensure it matches.
-    ConstString name = type_sp->GetQualifiedName();
+    ConstString name =
+        query.GetSearchByMangledName()
+            ? type_sp->GetForwardCompilerType().GetMangledTypeName()
+            : type_sp->GetQualifiedName();
     TypeQuery type_match(name.GetStringRef(), TypeQueryOptions::e_exact_match);
     if (query.ContextMatches(type_match.GetContextRef())) {
       results.InsertUnique(type_sp);
diff --git a/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp b/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
index 584c2115459c66..0179c52df9e563 100644
--- a/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
+++ b/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
@@ -1562,7 +1562,10 @@ void SymbolFilePDB::FindTypes(const lldb_private::TypeQuery &query,
     if (iter == m_types.end())
       continue;
     // We resolved a type. Get the fully qualified name to ensure it matches.
-    ConstString name = iter->second->GetQualifiedName();
+    ConstString name =
+        query.GetSearchByMangledName()
+            ? iter->second->GetForwardCompilerType().GetMangledTypeName()
+            : iter->second->GetQualifiedName();
     TypeQuery type_match(name.GetStringRef(), TypeQueryOptions::e_exact_match);
     if (query.ContextMatches(type_match.GetContextRef())) {
       type_results.InsertUnique(iter->second);

``````````

</details>


https://github.com/llvm/llvm-project/pull/113007


More information about the lldb-commits mailing list