[Lldb-commits] [lldb] edc64d4 - [lldb] Add support for recognizing swift mangled names

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


Author: Alex Langford
Date: 2023-08-22T10:28:55-07:00
New Revision: edc64d49de92d33e709bcde1db5670ff430f5fad

URL: https://github.com/llvm/llvm-project/commit/edc64d49de92d33e709bcde1db5670ff430f5fad
DIFF: https://github.com/llvm/llvm-project/commit/edc64d49de92d33e709bcde1db5670ff430f5fad.diff

LOG: [lldb] Add support for recognizing swift mangled names

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 support to lldb.

Differential Revision: https://reviews.llvm.org/D158470

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Core/Mangled.h b/lldb/include/lldb/Core/Mangled.h
index 9892426161e439..165e67838e84bc 100644
--- a/lldb/include/lldb/Core/Mangled.h
+++ b/lldb/include/lldb/Core/Mangled.h
@@ -43,7 +43,8 @@ class Mangled {
     eManglingSchemeMSVC,
     eManglingSchemeItanium,
     eManglingSchemeRustV0,
-    eManglingSchemeD
+    eManglingSchemeD,
+    eManglingSchemeSwift,
   };
 
   /// Default constructor.

diff  --git a/lldb/source/Core/Mangled.cpp b/lldb/source/Core/Mangled.cpp
index d1973a9a8f7f63..4587119519e98e 100644
--- a/lldb/source/Core/Mangled.cpp
+++ b/lldb/source/Core/Mangled.cpp
@@ -58,6 +58,24 @@ Mangled::ManglingScheme Mangled::GetManglingScheme(llvm::StringRef const name) {
   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 @@ bool Mangled::GetRichManglingInfo(RichManglingContext &context,
 
   case eManglingSchemeRustV0:
   case eManglingSchemeD:
+  case eManglingSchemeSwift:
     // Rich demangling scheme is not supported
     return false;
   }
@@ -265,6 +284,10 @@ ConstString Mangled::GetDemangledName() const {
       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");
       }

diff  --git a/lldb/unittests/Core/MangledTest.cpp b/lldb/unittests/Core/MangledTest.cpp
index 284c2f21aadd61..4efc961d371d3f 100644
--- a/lldb/unittests/Core/MangledTest.cpp
+++ b/lldb/unittests/Core/MangledTest.cpp
@@ -89,6 +89,24 @@ TEST(MangledTest, EmptyForInvalidDLangName) {
   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");


        


More information about the lldb-commits mailing list