[clang] [Clang] Make `MangleContext::mangleMSGuidDecl()`, `MangleContext::mangleObjCMethodName()` and `MangleContext::mangleObjCMethodNameAsSourceName()` const methods (PR #130613)

Boaz Brickner via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 10 07:26:46 PDT 2025


https://github.com/bricknerb created https://github.com/llvm/llvm-project/pull/130613

Unfortunately, making `MangleContext::mangleName()` would require a lot of const escapes due to lazy initialization and id creations:

`Mangle.h`:
* https://github.com/llvm/llvm-project/blob/dffbc030e75b758e7512518abd499a0f680addbf/clang/include/clang/AST/Mangle.h#L82-L85
* https://github.com/llvm/llvm-project/blob/dffbc030e75b758e7512518abd499a0f680addbf/clang/include/clang/AST/Mangle.h#L97-L99

`ItaniumMangle.cpp`:
* https://github.com/llvm/llvm-project/blob/dffbc030e75b758e7512518abd499a0f680addbf/clang/lib/AST/ItaniumMangle.cpp#L158-L161
* https://github.com/llvm/llvm-project/blob/4508d6aa72b0f31056ae01aff0e8009a252e4683/clang/lib/AST/ItaniumMangle.cpp#L641

`MicrosoftMangle.cpp`:
* https://github.com/llvm/llvm-project/blob/dffbc030e75b758e7512518abd499a0f680addbf/clang/lib/AST/MicrosoftMangle.cpp#L243-L245
* https://github.com/llvm/llvm-project/blob/dffbc030e75b758e7512518abd499a0f680addbf/clang/lib/AST/MicrosoftMangle.cpp#L284

>From 6374b2477bcda7531c1820b5afed45d1f0692d39 Mon Sep 17 00:00:00 2001
From: Boaz Brickner <brickner at google.com>
Date: Mon, 10 Mar 2025 15:25:53 +0100
Subject: [PATCH] [Clang] Make `MangleContext::mangleMSGuidDecl()`,
 `MangleContext::mangleObjCMethodName()` and
 `MangleContext::mangleObjCMethodNameAsSourceName()` const methods
 Unfortunately, making `MangleContext::mangleName()` would require a lot of
 const escapes due to lazy initialization and id creations:

`Mangle.h`:
* https://github.com/llvm/llvm-project/blob/dffbc030e75b758e7512518abd499a0f680addbf/clang/include/clang/AST/Mangle.h#L82-L85
* https://github.com/llvm/llvm-project/blob/dffbc030e75b758e7512518abd499a0f680addbf/clang/include/clang/AST/Mangle.h#L97-L99

`ItaniumMangle.cpp`:
* https://github.com/llvm/llvm-project/blob/dffbc030e75b758e7512518abd499a0f680addbf/clang/lib/AST/ItaniumMangle.cpp#L158-L161
* https://github.com/llvm/llvm-project/blob/4508d6aa72b0f31056ae01aff0e8009a252e4683/clang/lib/AST/ItaniumMangle.cpp#L641

`MicrosoftMangle.cpp`:
* https://github.com/llvm/llvm-project/blob/dffbc030e75b758e7512518abd499a0f680addbf/clang/lib/AST/MicrosoftMangle.cpp#L243-L245
* https://github.com/llvm/llvm-project/blob/dffbc030e75b758e7512518abd499a0f680addbf/clang/lib/AST/MicrosoftMangle.cpp#L284
---
 clang/include/clang/AST/Mangle.h | 6 +++---
 clang/lib/AST/Mangle.cpp         | 7 ++++---
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/clang/include/clang/AST/Mangle.h b/clang/include/clang/AST/Mangle.h
index 6134a70c04bd3..9ed8895cbfff1 100644
--- a/clang/include/clang/AST/Mangle.h
+++ b/clang/include/clang/AST/Mangle.h
@@ -140,7 +140,7 @@ class MangleContext {
   virtual void mangleCXXRTTIName(QualType T, raw_ostream &,
                                  bool NormalizeIntegers = false) = 0;
   virtual void mangleStringLiteral(const StringLiteral *SL, raw_ostream &) = 0;
-  virtual void mangleMSGuidDecl(const MSGuidDecl *GD, raw_ostream &);
+  virtual void mangleMSGuidDecl(const MSGuidDecl *GD, raw_ostream &) const;
 
   void mangleGlobalBlock(const BlockDecl *BD, const NamedDecl *ID,
                          raw_ostream &Out);
@@ -153,9 +153,9 @@ class MangleContext {
 
   void mangleObjCMethodName(const ObjCMethodDecl *MD, raw_ostream &OS,
                             bool includePrefixByte = true,
-                            bool includeCategoryNamespace = true);
+                            bool includeCategoryNamespace = true) const;
   void mangleObjCMethodNameAsSourceName(const ObjCMethodDecl *MD,
-                                        raw_ostream &);
+                                        raw_ostream &) const;
 
   virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &) = 0;
 
diff --git a/clang/lib/AST/Mangle.cpp b/clang/lib/AST/Mangle.cpp
index 4c4f2038c51e6..b44ab23f1d0e1 100644
--- a/clang/lib/AST/Mangle.cpp
+++ b/clang/lib/AST/Mangle.cpp
@@ -240,7 +240,8 @@ void MangleContext::mangleName(GlobalDecl GD, raw_ostream &Out) {
   Out << ((DefaultPtrWidth / 8) * ArgWords);
 }
 
-void MangleContext::mangleMSGuidDecl(const MSGuidDecl *GD, raw_ostream &Out) {
+void MangleContext::mangleMSGuidDecl(const MSGuidDecl *GD,
+                                     raw_ostream &Out) const {
   // For now, follow the MSVC naming convention for GUID objects on all
   // targets.
   MSGuidDecl::Parts P = GD->getParts();
@@ -327,7 +328,7 @@ void MangleContext::mangleBlock(const DeclContext *DC, const BlockDecl *BD,
 void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
                                          raw_ostream &OS,
                                          bool includePrefixByte,
-                                         bool includeCategoryNamespace) {
+                                         bool includeCategoryNamespace) const {
   if (getASTContext().getLangOpts().ObjCRuntime.isGNUFamily()) {
     // This is the mangling we've always used on the GNU runtimes, but it
     // has obvious collisions in the face of underscores within class
@@ -382,7 +383,7 @@ void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
 }
 
 void MangleContext::mangleObjCMethodNameAsSourceName(const ObjCMethodDecl *MD,
-                                                     raw_ostream &Out) {
+                                                     raw_ostream &Out) const {
   SmallString<64> Name;
   llvm::raw_svector_ostream OS(Name);
 



More information about the cfe-commits mailing list