https://github.com/DataCorrupted updated 
https://github.com/llvm/llvm-project/pull/137884

>From f6166a6c2ab45edaa0ff273e2ba917fff778a243 Mon Sep 17 00:00:00 2001
From: Peter Rong <peterr...@meta.com>
Date: Tue, 29 Apr 2025 14:24:49 -0700
Subject: [PATCH 1/2] [clang] change mangling API to allow calls from
 swift-frontend

Signed-off-by: Peter Rong <peterr...@meta.com>
---
 clang/include/clang/AST/Mangle.h |  7 ++++++
 clang/lib/AST/Mangle.cpp         | 37 +++++++++++++++++++++++---------
 2 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/clang/include/clang/AST/Mangle.h b/clang/include/clang/AST/Mangle.h
index a0162fb7125fe..1afbf80df40cf 100644
--- a/clang/include/clang/AST/Mangle.h
+++ b/clang/include/clang/AST/Mangle.h
@@ -40,6 +40,13 @@ struct ThisAdjustment;
 struct ThunkInfo;
 class VarDecl;
 
+/// Extract mangling function name from MangleContext such that swift can call
+/// it to prepare for ObjCDirect in swift.
+void mangleObjCMethodName(raw_ostream &OS, bool includePrefixByte,
+  bool isInstanceMethod, StringRef ClassName,
+  std::optional<StringRef> CategoryName,
+  StringRef MethodName);
+
 /// MangleContext - Context for tracking state which persists across multiple
 /// calls to the C++ name mangler.
 class MangleContext {
diff --git a/clang/lib/AST/Mangle.cpp b/clang/lib/AST/Mangle.cpp
index 741c031a40385..9652fdbc4e125 100644
--- a/clang/lib/AST/Mangle.cpp
+++ b/clang/lib/AST/Mangle.cpp
@@ -29,6 +29,23 @@
 
 using namespace clang;
 
+void clang::mangleObjCMethodName(raw_ostream &OS, bool includePrefixByte,
+                                 bool isInstanceMethod, StringRef ClassName,
+                                 std::optional<StringRef> CategoryName,
+                                 StringRef MethodName) {
+  // \01+[ContainerName(CategoryName) SelectorName]
+  if (includePrefixByte)
+    OS << "\01";
+  OS << (isInstanceMethod ? '-' : '+');
+  OS << '[';
+  OS << ClassName;
+  if (CategoryName)
+    OS << "(" << *CategoryName << ")";
+  OS << " ";
+  OS << MethodName;
+  OS << ']';
+}
+
 // FIXME: For blocks we currently mimic GCC's mangling scheme, which leaves
 // much to be desired. Come up with a better mangling scheme.
 
@@ -362,26 +379,26 @@ void MangleContext::mangleObjCMethodName(const 
ObjCMethodDecl *MD,
   }
 
   // \01+[ContainerName(CategoryName) SelectorName]
-  if (includePrefixByte) {
-    OS << '\01';
-  }
-  OS << (MD->isInstanceMethod() ? '-' : '+') << '[';
+  auto CategoryName = std::optional<StringRef>();
+  StringRef ClassName = "";
   if (const auto *CID = MD->getCategory()) {
     if (const auto *CI = CID->getClassInterface()) {
-      OS << CI->getName();
+      ClassName = CI->getName();
       if (includeCategoryNamespace) {
-        OS << '(' << *CID << ')';
+        CategoryName = CID->getName();
       }
     }
   } else if (const auto *CD =
                  dyn_cast<ObjCContainerDecl>(MD->getDeclContext())) {
-    OS << CD->getName();
+    ClassName = CD->getName();
   } else {
     llvm_unreachable("Unexpected ObjC method decl context");
   }
-  OS << ' ';
-  MD->getSelector().print(OS);
-  OS << ']';
+  std::string MethodName;
+  llvm::raw_string_ostream MethodNameOS(MethodName);
+  MD->getSelector().print(MethodNameOS);
+  clang::mangleObjCMethodName(OS, includePrefixByte, MD->isInstanceMethod(),
+                              ClassName, CategoryName, MethodName);
 }
 
 void MangleContext::mangleObjCMethodNameAsSourceName(const ObjCMethodDecl *MD,

>From 165f5119fd701ee42ca205ee259d8f65a10a0925 Mon Sep 17 00:00:00 2001
From: Peter Rong <peterr...@meta.com>
Date: Tue, 29 Apr 2025 14:52:14 -0700
Subject: [PATCH 2/2] Format

---
 clang/include/clang/AST/Mangle.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/AST/Mangle.h b/clang/include/clang/AST/Mangle.h
index 1afbf80df40cf..ca72dcfd4483d 100644
--- a/clang/include/clang/AST/Mangle.h
+++ b/clang/include/clang/AST/Mangle.h
@@ -43,9 +43,9 @@ class VarDecl;
 /// Extract mangling function name from MangleContext such that swift can call
 /// it to prepare for ObjCDirect in swift.
 void mangleObjCMethodName(raw_ostream &OS, bool includePrefixByte,
-  bool isInstanceMethod, StringRef ClassName,
-  std::optional<StringRef> CategoryName,
-  StringRef MethodName);
+                          bool isInstanceMethod, StringRef ClassName,
+                          std::optional<StringRef> CategoryName,
+                          StringRef MethodName);
 
 /// MangleContext - Context for tracking state which persists across multiple
 /// calls to the C++ name mangler.

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to