Skip to content

[Clang] Fix the mangling of lambdas #89204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 19, 2024
Merged

[Clang] Fix the mangling of lambdas #89204

merged 3 commits into from
Apr 19, 2024

Conversation

cor3ntin
Copy link
Contributor

Lambdas used in the initializer of a local class were not mangling the name of the member.

Fixes #88906

Lambdas used in the initializer of a local class were not mangling
the name of the member.

Fixes llvm#88906
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Apr 18, 2024
@llvmbot
Copy link
Member

llvmbot commented Apr 18, 2024

@llvm/pr-subscribers-clang

Author: cor3ntin (cor3ntin)

Changes

Lambdas used in the initializer of a local class were not mangling the name of the member.

Fixes #88906


Full diff: https://github.com/llvm/llvm-project/pull/89204.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/AST/ItaniumMangle.cpp (+8-10)
  • (added) clang/test/CodeGenCXX/mangle-lambdas-gh88906.cpp (+27)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6c51c2d1f483ce..0ad301b24e6b73 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -538,6 +538,7 @@ Bug Fixes to C++ Support
 - Fix a crash in requires expression with templated base class member function. Fixes (#GH84020).
 - Fix a crash caused by defined struct in a type alias template when the structure
   has fields with dependent type. Fixes (#GH75221).
+- Fix the Itanium mangling of lambdas defined in a member of a local class (#GH88906)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index c3b98d2d2149cb..53ef022c710d8d 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -1070,14 +1070,7 @@ void CXXNameMangler::mangleNameWithAbiTags(GlobalDecl GD,
   if (isLocalContainerContext(DC) && ND->hasLinkage() && !isLambda(ND))
     while (!DC->isNamespace() && !DC->isTranslationUnit())
       DC = Context.getEffectiveParentContext(DC);
-  else if (GetLocalClassDecl(ND)) {
-    mangleLocalName(GD, AdditionalAbiTags);
-    return;
-  }
-
-  assert(!isa<LinkageSpecDecl>(DC) && "context cannot be LinkageSpecDecl");
-
-  if (isLocalContainerContext(DC)) {
+  else if (GetLocalClassDecl(ND) && !isLambda(ND)) {
     mangleLocalName(GD, AdditionalAbiTags);
     return;
   }
@@ -1089,6 +1082,13 @@ void CXXNameMangler::mangleNameWithAbiTags(GlobalDecl GD,
     return;
   }
 
+  assert(!isa<LinkageSpecDecl>(DC) && "context cannot be LinkageSpecDecl");
+
+  if (isLocalContainerContext(DC)) {
+    mangleLocalName(GD, AdditionalAbiTags);
+    return;
+  }
+
   if (DC->isTranslationUnit() || isStdNamespace(DC)) {
     // Check if we have a template.
     const TemplateArgumentList *TemplateArgs = nullptr;
@@ -2201,8 +2201,6 @@ void CXXNameMangler::manglePrefix(const DeclContext *DC, bool NoFunction) {
   if (NoFunction && isLocalContainerContext(DC))
     return;
 
-  assert(!isLocalContainerContext(DC));
-
   const NamedDecl *ND = cast<NamedDecl>(DC);
   if (mangleSubstitution(ND))
     return;
diff --git a/clang/test/CodeGenCXX/mangle-lambdas-gh88906.cpp b/clang/test/CodeGenCXX/mangle-lambdas-gh88906.cpp
new file mode 100644
index 00000000000000..61f340c848ebe4
--- /dev/null
+++ b/clang/test/CodeGenCXX/mangle-lambdas-gh88906.cpp
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu %s -emit-llvm -o - | FileCheck %s
+
+class func {
+public:
+    template <typename T>
+    func(T){};
+    template <typename T, typename U>
+    func(T, U){};
+};
+
+void GH88906(){
+  class Test{
+    public:
+    func a{[]{ }, []{ }};
+    func b{[]{ }};
+    func c{[]{ }};
+  } test;
+}
+
+// CHECK-LABEL: define internal void @_ZZ7GH88906vEN4TestC2Ev
+// CHECK: call void @_ZN4funcC2IN7GH889064Test1aMUlvE_ENS3_UlvE0_EEET_T0_
+// CHECK: call void @_ZN4funcC2IN7GH889064Test1bMUlvE_EEET_
+// CHECK: call void @_ZN4funcC2IN7GH889064Test1cMUlvE_EEET_
+
+// CHECK-LABEL: define internal void @_ZN4funcC2IN7GH889064Test1aMUlvE_ENS3_UlvE0_EEET_T0_
+// CHECK-LABEL: define internal void @_ZN4funcC2IN7GH889064Test1bMUlvE_EEET_
+// CHECK-LABEL: define internal void @_ZN4funcC2IN7GH889064Test1cMUlvE_EEET_

@AaronBallman AaronBallman requested a review from rjmccall April 18, 2024 11:29
@AaronBallman AaronBallman added the ABI Application Binary Interface label Apr 18, 2024
@@ -538,6 +538,7 @@ Bug Fixes to C++ Support
- Fix a crash in requires expression with templated base class member function. Fixes (#GH84020).
- Fix a crash caused by defined struct in a type alias template when the structure
has fields with dependent type. Fixes (#GH75221).
- Fix the Itanium mangling of lambdas defined in a member of a local class (#GH88906)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rjmccall do you think this should come with an ABI tag to get the old mangling behavior?

@cor3ntin cor3ntin merged commit 64cc3fa into llvm:main Apr 19, 2024
aniplcc pushed a commit to aniplcc/llvm-project that referenced this pull request Apr 21, 2024
Lambdas used in the initializer of a local class were not mangling the
name of the member.

Fixes llvm#88906
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ABI Application Binary Interface clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang] name mangling for lambdas in local classes broken since clang-13
4 participants