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

Augusto Noronha via lldb-commits lldb-commits at lists.llvm.org
Tue Oct 22 14:35:02 PDT 2024


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

>From eab35bd50d89a16494d8f08421a12aef77356c43 Mon Sep 17 00:00:00 2001
From: Augusto Noronha <anoronha at apple.com>
Date: Tue, 22 Oct 2024 14:34:44 -0700
Subject: [PATCH] [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.
---
 lldb/include/lldb/Symbol/Type.h                  |  9 +++++++++
 .../Plugins/SymbolFile/CTF/SymbolFileCTF.cpp     | 16 +++++++++++-----
 .../Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp |  9 +++++++++
 .../SymbolFile/NativePDB/SymbolFileNativePDB.cpp |  5 ++++-
 .../Plugins/SymbolFile/PDB/SymbolFilePDB.cpp     |  5 ++++-
 5 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/lldb/include/lldb/Symbol/Type.h b/lldb/include/lldb/Symbol/Type.h
index 03d9f927997476..a8e5b81585e169 100644
--- a/lldb/include/lldb/Symbol/Type.h
+++ b/lldb/include/lldb/Symbol/Type.h
@@ -84,6 +84,9 @@ 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 +303,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..4d08c53f752098 100644
--- a/lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp
+++ b/lldb/source/Plugins/SymbolFile/CTF/SymbolFileCTF.cpp
@@ -1032,11 +1032,17 @@ 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)
+      continue;
+    auto type_name =
+        match.GetSearchByMangledName()
+            ? type_sp->GetForwardCompilerType().GetMangledTypeName()
+            : type_sp->GetName();
+    if (type_name != name)
+      continue;
+    results.InsertUnique(type_sp);
+    if (results.Done(match))
+      return;
   }
 }
 
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
index e5b8eee8d08c24..704c8d64dbfb2f 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
@@ -2758,6 +2758,15 @@ void SymbolFileDWARF::FindTypes(const TypeQuery &query, TypeResults &results) {
         return true; // Keep iterating over index types, language mismatch.
     }
 
+    // Since mangled names are unique, we only need to check if the names are
+    // the same.
+    if (query.GetSearchByMangledName()) {
+      if (die.GetMangledName() == query.GetTypeBasename().GetStringRef())
+        if (Type *matching_type = ResolveType(die, true, true))
+          results.InsertUnique(matching_type->shared_from_this());
+      return !results.Done(query); // Keep iterating if we aren't done.
+    }
+
     // Check the context matches
     std::vector<lldb_private::CompilerContext> die_context;
     if (query.GetModuleSearch())
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 4fc48b4d133382..0ea18d8f74baa3 100644
--- a/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
+++ b/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp
@@ -1561,7 +1561,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);



More information about the lldb-commits mailing list