[Lldb-commits] [PATCH] D158470: [lldb] Add support for recognizing swift mangled names

Alex Langford via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Tue Aug 22 10:29:26 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rGedc64d49de92: [lldb] Add support for recognizing swift mangled names (authored by bulbazord).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158470/new/

https://reviews.llvm.org/D158470

Files:
  lldb/include/lldb/Core/Mangled.h
  lldb/source/Core/Mangled.cpp
  lldb/unittests/Core/MangledTest.cpp


Index: lldb/unittests/Core/MangledTest.cpp
===================================================================
--- lldb/unittests/Core/MangledTest.cpp
+++ lldb/unittests/Core/MangledTest.cpp
@@ -89,6 +89,24 @@
   EXPECT_STREQ("", the_demangled.GetCString());
 }
 
+TEST(MangledTest, RecognizeSwiftMangledNames) {
+  llvm::StringRef valid_swift_mangled_names[] = {
+      "_TtC4main7MyClass",   // Mangled objc class name
+      "_TtP4main3Foo_",      // Mangld objc protocol name
+      "$s4main3BarCACycfC",  // Mangled name
+      "_$s4main3BarCACycfC", // Mangled name with leading underscore
+      "$S4main3BarCACycfC",  // Older swift mangled name
+      "_$S4main3BarCACycfC", // Older swift mangled name
+                             // with leading underscore
+      // Mangled swift filename
+      "@__swiftmacro_4main16FunVariableNames9OptionSetfMm_.swift",
+  };
+
+  for (llvm::StringRef mangled : valid_swift_mangled_names)
+    EXPECT_EQ(Mangled::GetManglingScheme(mangled),
+              Mangled::eManglingSchemeSwift);
+}
+
 TEST(MangledTest, BoolConversionOperator) {
   {
     ConstString MangledName("_ZN1a1b1cIiiiEEvm");
Index: lldb/source/Core/Mangled.cpp
===================================================================
--- lldb/source/Core/Mangled.cpp
+++ lldb/source/Core/Mangled.cpp
@@ -58,6 +58,24 @@
   if (name.startswith("___Z"))
     return Mangled::eManglingSchemeItanium;
 
+  // Swift's older style of mangling used "_T" as a mangling prefix. This can
+  // lead to false positives with other symbols that just so happen to start
+  // with "_T". To minimize the chance of that happening, we only return true
+  // for select old-style swift mangled names. The known cases are ObjC classes
+  // and protocols. Classes are either prefixed with "_TtC" or "_TtGC".
+  // Protocols are prefixed with "_TtP".
+  if (name.startswith("_TtC") || name.startswith("_TtGC") ||
+      name.startswith("_TtP"))
+    return Mangled::eManglingSchemeSwift;
+
+  // Swift 4.2 used "$S" and "_$S".
+  // Swift 5 and onward uses "$s" and "_$s".
+  // Swift also uses "@__swiftmacro_" as a prefix for mangling filenames.
+  if (name.startswith("$S") || name.startswith("_$S") ||
+      name.startswith("$s") || name.startswith("_$s") ||
+      name.startswith("@__swiftmacro_"))
+    return Mangled::eManglingSchemeSwift;
+
   return Mangled::eManglingSchemeNone;
 }
 
@@ -228,6 +246,7 @@
 
   case eManglingSchemeRustV0:
   case eManglingSchemeD:
+  case eManglingSchemeSwift:
     // Rich demangling scheme is not supported
     return false;
   }
@@ -265,6 +284,10 @@
       case eManglingSchemeD:
         demangled_name = GetDLangDemangledStr(m_mangled);
         break;
+      case eManglingSchemeSwift:
+        // Demangling a swift name requires the swift compiler. This is
+        // explicitly unsupported on llvm.org.
+        break;
       case eManglingSchemeNone:
         llvm_unreachable("eManglingSchemeNone was handled already");
       }
Index: lldb/include/lldb/Core/Mangled.h
===================================================================
--- lldb/include/lldb/Core/Mangled.h
+++ lldb/include/lldb/Core/Mangled.h
@@ -43,7 +43,8 @@
     eManglingSchemeMSVC,
     eManglingSchemeItanium,
     eManglingSchemeRustV0,
-    eManglingSchemeD
+    eManglingSchemeD,
+    eManglingSchemeSwift,
   };
 
   /// Default constructor.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158470.552424.patch
Type: text/x-patch
Size: 3370 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20230822/9e524ef9/attachment.bin>


More information about the lldb-commits mailing list