[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
Mon Aug 21 17:23:04 PDT 2023


bulbazord created this revision.
bulbazord added reviewers: JDevlieghere, aprantl, mib, jingham, augusto2112, kastiglione, fdeazeve.
Herald added a project: All.
bulbazord requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Apple maintains a downstream fork of lldb in order to support swift
debugging. Much of that support is isolated to its own plugins, but some
of it is exposed in more generic code. I would like to take some of
the swift support we have downstream and move it upstream to llvm.org in
an effort to 1) reduce downstream maintenance burden, and 2) work
towards solidifying the process of adding new language suppor to llvm.


Repository:
  rG LLVM Github Monorepo

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.552174.patch
Type: text/x-patch
Size: 3370 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20230822/35d6851b/attachment.bin>


More information about the lldb-commits mailing list