Skip to content

[clang] Fix Name Mangling Crashes #134486

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Mr-Anyone
Copy link

It appears that Clang currently mangles names incorrectly when handling lambda expressions in constraint (requires) clauses.

The issue likely stems from mangleLocalName being used, whereas mangleNestedName should be invoked instead. This is supported by the fact that GCC generates the following mangled name for the example below:

template<typename T>
auto a = [](T a) requires requires {
    [](T a){}(a);
} {
    return a;
};

// _ZNK1aIiEUliE_clEiQrqXcltlNS_IT_EUlS2_E_EEfL0p_EE
//                           ^
//                           |
//                         Nested Name
void func() {
    a<int>(1);
}

GCC produces this mangled name:

Example output from clang after this change with previously crashing code:

template<typename T>
auto a = [](T a)requires requires{
    [](T a){}(a);
}{
    return a;
};

//
void func(){
//_ZNK1aIiEMUliE_clEiQrqXclLN1aMUlT_E_clUlS3_E_EEfp_EE
    a<int>(1);
}

void func2() {
// _ZZ5func2vENK3$_0clIiEEDaT_QrqXclLNS_clUlTnivE_EEEE
    [](auto x) requires requires {
        []<int = 0>() {}();
    } {}(0);
}

It seems that something like this resolves these issues:
resolve #132925
resolve #131620
resolve #102169

Copy link

github-actions bot commented Apr 5, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Apr 5, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 5, 2025

@llvm/pr-subscribers-clang

Author: Vincent (Mr-Anyone)

Changes

It appears that Clang currently mangles names incorrectly when handling lambda expressions in constraint (requires) clauses.

The issue likely stems from mangleLocalName being used, whereas mangleNestedName should be invoked instead. This is supported by the fact that GCC generates the following mangled name for the example below:

template&lt;typename T&gt;
auto a = [](T a) requires requires {
    [](T a){}(a);
} {
    return a;
};

// _ZNK1aIiEUliE_clEiQrqXcltlNS_IT_EUlS2_E_EEfL0p_EE
//                           ^
//                           |
//                         Nested Name
void func() {
    a&lt;int&gt;(1);
}

GCC produces this mangled name:

Example output from clang after this change with previously crashing code:

template&lt;typename T&gt;
auto a = [](T a)requires requires{
    [](T a){}(a);
}{
    return a;
};

//
void func(){
//_ZNK1aIiEMUliE_clEiQrqXclLN1aMUlT_E_clUlS3_E_EEfp_EE
    a&lt;int&gt;(1);
}

void func2() {
// _ZZ5func2vENK3$_0clIiEEDaT_QrqXclLNS_clUlTnivE_EEEE
    [](auto x) requires requires {
        []&lt;int = 0&gt;() {}();
    } {}(0);
}

It seems that something like this resolves these issues:
resolve #132925
resolve #131620
resolve #102169


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

1 Files Affected:

  • (modified) clang/lib/AST/ItaniumMangle.cpp (+10)
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index fdd84d0bf7c5c..90d6255917654 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -1088,6 +1088,16 @@ void CXXNameMangler::mangleNameWithAbiTags(GlobalDecl GD,
     return;
   }
 
+  const RecordDecl *RD = GetLocalClassDecl(GD.getDecl());
+  if(RD){
+      const DeclContext *DC = Context.getEffectiveDeclContext(RD);
+      const FunctionDecl* FD = dyn_cast<FunctionDecl>(DC);
+      if(FD->getTrailingRequiresClause() && IsLambda){
+          mangleNestedName(GD, DC, AdditionalAbiTags);
+          return;
+      }
+  }
+
   if (isLocalContainerContext(DC)) {
     mangleLocalName(GD, AdditionalAbiTags);
     return;

Copy link
Contributor

@zyn0217 zyn0217 left a comment

Choose a reason for hiding this comment

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

This needs tests as well as release notes.

Also please run clang-format on the changes.

Given that @mizvekov is working on fixing unevaluated lambda contexts, I wonder if these issues could be resolved the other way around, especially since there seems to be a crash on the existing test.

@cor3ntin cor3ntin changed the title [clang] Fix Mame Mangling Crashes [clang] Fix Name Mangling Crashes Apr 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
3 participants