[clang] [Clang] Unwrap RequiresExprBodyDecl for lambda mangling, numbering, linkage (PR #249314)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Dec 5 18:56:37 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Anshul Nigham (nigham)
<details>
<summary>Changes</summary>
Since commit 6d00c4297f67 (#<!-- -->147764), lambdas inside C++20 requires-expressions keep RequiresExprBodyDecl (a non-NamedDecl DeclContext) as their parent. That commit only unwrapped it late in Itanium's mangleNameWithAbiTags(). This commit addresses some missing handling:
- Itanium mangling misses local-class detection, crashes on cast<NamedDecl> in manglePrefix() (or SIGSEGVs without assertions), and bypasses getRedeclContext() for enclosing LinkageSpecDecls.
- Microsoft mangling drops the local scope discriminator, or emits duplicates for nested requires-expressions when stepping with DC->getParent().
- Separate requires-expressions in the same inline/templated function get distinct MangleNumberingContexts and colliding lambda numbers.
- getLVForClosure() gets null from dyn_cast<NamedDecl>(DC), giving lambdas in inline functions internal linkage instead of linkonce_odr.
Unwrap RequiresExprBodyDecl in both manglers' getEffectiveDeclContext() (and advance Microsoft's mangleNestedName() with it, dropping the late loop in mangleNameWithAbiTags()), in lambda mangling-number lookup, and in getLVForClosure().
Fixes #<!-- -->181933
LLM-aided
---
Full diff: https://github.com/llvm/llvm-project/pull/249314.diff
6 Files Affected:
- (modified) clang/lib/AST/Decl.cpp (+4-2)
- (modified) clang/lib/AST/ItaniumMangle.cpp (+2-5)
- (modified) clang/lib/AST/MicrosoftMangle.cpp (+3-3)
- (modified) clang/lib/Sema/SemaLambda.cpp (+4-4)
- (modified) clang/test/CodeGenCXX/mangle-requires.cpp (+65)
- (modified) clang/test/CodeGenCXX/ms-mangle-requires.cpp (+35)
``````````diff
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index a50c4a6375253a..93c19751b56431 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -1324,9 +1324,11 @@ LinkageInfo LinkageComputer::getLVForClosure(const DeclContext *DC,
LVComputationKind computation) {
// This lambda has its linkage/visibility determined by its owner.
const NamedDecl *Owner;
- if (!ContextDecl)
+ if (!ContextDecl) {
+ while (DC->isRequiresExprBody())
+ DC = DC->getParent()->getRedeclContext();
Owner = dyn_cast<NamedDecl>(DC);
- else if (isa<ParmVarDecl>(ContextDecl))
+ } else if (isa<ParmVarDecl>(ContextDecl))
Owner =
dyn_cast<NamedDecl>(ContextDecl->getDeclContext()->getRedeclContext());
else if (isa<ImplicitConceptSpecializationDecl>(ContextDecl)) {
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index 6ebbdbd5369245..48615e6c50af21 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -684,8 +684,8 @@ ItaniumMangleContextImpl::getEffectiveDeclContext(const Decl *D) {
}
const DeclContext *DC = D->getDeclContext();
- if (isa<CapturedDecl>(DC) || isa<OMPDeclareReductionDecl>(DC) ||
- isa<OMPDeclareMapperDecl>(DC)) {
+ if (isa<CapturedDecl, OMPDeclareReductionDecl, OMPDeclareMapperDecl,
+ RequiresExprBodyDecl>(DC)) {
return getEffectiveDeclContext(cast<Decl>(DC));
}
@@ -1094,9 +1094,6 @@ void CXXNameMangler::mangleNameWithAbiTags(
return;
}
- while (DC->isRequiresExprBody())
- DC = DC->getParent();
-
if (DC->isTranslationUnit() || isStdNamespace(DC)) {
// Check if we have a template.
const TemplateArgumentList *TemplateArgs = nullptr;
diff --git a/clang/lib/AST/MicrosoftMangle.cpp b/clang/lib/AST/MicrosoftMangle.cpp
index 38a0238df58b56..e2b49d78b1e0a2 100644
--- a/clang/lib/AST/MicrosoftMangle.cpp
+++ b/clang/lib/AST/MicrosoftMangle.cpp
@@ -115,8 +115,8 @@ static const DeclContext *getEffectiveDeclContext(const Decl *D) {
}
const DeclContext *DC = D->getDeclContext();
- if (isa<CapturedDecl>(DC) || isa<OMPDeclareReductionDecl>(DC) ||
- isa<OMPDeclareMapperDecl>(DC)) {
+ if (isa<CapturedDecl, OMPDeclareReductionDecl, OMPDeclareMapperDecl,
+ RequiresExprBodyDecl>(DC)) {
return getEffectiveDeclContext(cast<Decl>(DC));
}
@@ -1472,7 +1472,7 @@ void MicrosoftCXXNameMangler::mangleNestedName(GlobalDecl GD) {
}
}
}
- DC = DC->getParent();
+ DC = getEffectiveDeclContext(cast<Decl>(DC));
}
}
diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp
index 08526b7ec22b83..4c7f5235864453 100644
--- a/clang/lib/Sema/SemaLambda.cpp
+++ b/clang/lib/Sema/SemaLambda.cpp
@@ -361,8 +361,8 @@ Sema::getCurrentMangleNumberContext(const DeclContext *DC) {
if ((IsInNonspecializedTemplate &&
!(ManglingContextDecl && isa<ParmVarDecl>(ManglingContextDecl))) ||
IsInFunctionThatRequiresMangling(CurContext)) {
- while (auto *CD = dyn_cast<CapturedDecl>(DC))
- DC = CD->getParent();
+ while (isa<CapturedDecl, RequiresExprBodyDecl>(DC))
+ DC = DC->getParent();
return std::make_tuple(&Context.getManglingNumberContext(DC), nullptr);
}
@@ -500,8 +500,8 @@ void Sema::handleLambdaNumbering(
ASTContext::NeedExtraManglingDecl, ManglingContextDecl);
// Otherwise, from that lambda's decl context.
auto DC = Class->getDeclContext();
- while (auto *CD = dyn_cast<CapturedDecl>(DC))
- DC = CD->getParent();
+ while (isa<CapturedDecl, RequiresExprBodyDecl>(DC))
+ DC = DC->getParent();
return &Context.getManglingNumberContext(DC);
};
diff --git a/clang/test/CodeGenCXX/mangle-requires.cpp b/clang/test/CodeGenCXX/mangle-requires.cpp
index 7f2d0e5d219918..79c40e4a5df320 100644
--- a/clang/test/CodeGenCXX/mangle-requires.cpp
+++ b/clang/test/CodeGenCXX/mangle-requires.cpp
@@ -70,3 +70,68 @@ void test_dependent() {
}
// CHECK-LABEL: define internal void @"_ZZZN8GH10077414test_dependentEvENK3$_0clIiEEDaT_ENKUlS2_E_clIiEEDaS2_QrqXL_ZZZNS_14test_dependentEvENKS0_clES2_E1wEE"
}
+
+namespace GH181933 {
+
+template <typename F>
+struct Emit {
+ [[gnu::used]] static void run(F f) { f(0); }
+ static constexpr bool ok = true;
+};
+
+void foo() {
+ if constexpr (requires { requires Emit<decltype([](auto&&) {})>::ok; }) {}
+}
+// CHECK-DAG: define internal {{.*}}void @"_ZN8GH1819334EmitIZNS_3fooEvE3$_0E3runES1_"
+// CHECK-DAG: define internal {{.*}}void @"_ZZN8GH1819333fooEvENK3$_0clIiEEDaOT_"
+
+void overloaded(int) {
+ if constexpr (requires { requires Emit<decltype([](auto&&) {})>::ok; }) {}
+}
+void overloaded(double) {
+ if constexpr (requires { requires Emit<decltype([](auto&&) {})>::ok; }) {}
+}
+// CHECK-DAG: define internal {{.*}}void @"_ZZN8GH18193310overloadedEiENK3$_0clIiEEDaOT_"
+// CHECK-DAG: define internal {{.*}}void @"_ZZN8GH18193310overloadedEdENK3$_0clIiEEDaOT_"
+
+inline void inline_foo() {
+ if constexpr (requires { requires Emit<decltype([](auto&&) {})>::ok; }) {}
+ if constexpr (requires { requires Emit<decltype([](auto&&) {})>::ok; }) {}
+}
+void use_inline() { inline_foo(); }
+// CHECK-DAG: define linkonce_odr {{.*}}void @_ZN8GH1819334EmitIZNS_10inline_fooEvEUlOT_E_E3runES3_
+// CHECK-DAG: define linkonce_odr {{.*}}void @_ZZN8GH18193310inline_fooEvENKUlOT_E_clIiEEDaS1_
+// CHECK-DAG: define linkonce_odr {{.*}}void @_ZN8GH1819334EmitIZNS_10inline_fooEvEUlOT_E0_E3runES3_
+// CHECK-DAG: define linkonce_odr {{.*}}void @_ZZN8GH18193310inline_fooEvENKUlOT_E0_clIiEEDaS1_
+
+bool ns_b = requires { requires Emit<decltype([](auto&&) {})>::ok; };
+extern "C++" {
+bool ns_b2 = requires(int x) { requires Emit<decltype([](auto&&) {})>::ok; };
+}
+// CHECK-DAG: define internal {{.*}}void @"_ZNK8GH1819333$_5clIiEEDaOT_"
+// CHECK-DAG: define internal {{.*}}void @"_ZNK8GH1819333$_6clIiEEDaOT_"
+
+void nested() {
+ if constexpr (requires(int x) {
+ requires requires(int y) {
+ requires Emit<decltype([](auto&&) {})>::ok;
+ };
+ }) {}
+}
+// CHECK-DAG: define internal {{.*}}void @"_ZZN8GH1819336nestedEvENK3$_0clIiEEDaOT_"
+
+template <typename Predicate>
+void call_pred(Predicate pred) {
+ pred(42);
+}
+template <typename Predicate>
+auto bar(Predicate pred) {
+ call_pred(pred);
+}
+template <typename T>
+concept Baz = requires(const T& x) {
+ { bar([](const auto&) { return true; }) };
+};
+static_assert(Baz<int>);
+
+} // namespace GH181933
diff --git a/clang/test/CodeGenCXX/ms-mangle-requires.cpp b/clang/test/CodeGenCXX/ms-mangle-requires.cpp
index abd54fea87cdc4..536b30dc9a8a08 100644
--- a/clang/test/CodeGenCXX/ms-mangle-requires.cpp
+++ b/clang/test/CodeGenCXX/ms-mangle-requires.cpp
@@ -124,3 +124,38 @@ void caller() {
// CHECK: define {{.*}} @"??$testBar at UWithVariadic@GH191588@@V<lambda_0>@?0??caller at 2@YAXXZ@@GH191588@@YA?A?<decltype-auto>@@$$QEAUWithVariadic at 0@$$QEAV<lambda_0>@?0??caller at 0@YAXXZ@@Z"
}
+
+namespace GH181933 {
+
+template <typename F>
+struct Emit {
+ [[gnu::used]] static void run(F f) { f(0); }
+ static constexpr bool ok = true;
+};
+
+void foo() {
+ if constexpr (requires(int x) { requires Emit<decltype([](auto&&) {})>::ok; }) {}
+}
+// CHECK-DAG: define internal void @"?run@?$Emit at V<lambda_1>@?0??foo at GH181933@@YAXXZ@@GH181933@@SAXV<lambda_1>@?0??foo at 2@YAXXZ@@Z"
+// CHECK-DAG: define internal void @"??$?RH@<lambda_1>@?0??foo at GH181933@@YAXXZ at QEBA?A?<auto>@@$$QEAH at Z"
+
+inline void inline_foo() {
+ if constexpr (requires { requires Emit<decltype([](auto&&) {})>::ok; }) {}
+ if constexpr (requires { requires Emit<decltype([](auto&&) {})>::ok; }) {}
+}
+void use_inline() { inline_foo(); }
+// CHECK-DAG: define linkonce_odr dso_local void @"?run@?$Emit at V<lambda_1>@?0??inline_foo at GH181933@@YAXXZ@@GH181933@@SAXV<lambda_1>@?0??inline_foo at 2@YAXXZ@@Z"
+// CHECK-DAG: define linkonce_odr dso_local void @"??$?RH@<lambda_1>@?0??inline_foo at GH181933@@YAXXZ at QEBA?A?<auto>@@$$QEAH at Z"
+// CHECK-DAG: define linkonce_odr dso_local void @"?run@?$Emit at V<lambda_2>@?0??inline_foo at GH181933@@YAXXZ@@GH181933@@SAXV<lambda_2>@?0??inline_foo at 2@YAXXZ@@Z"
+// CHECK-DAG: define linkonce_odr dso_local void @"??$?RH@<lambda_2>@?0??inline_foo at GH181933@@YAXXZ at QEBA?A?<auto>@@$$QEAH at Z"
+
+void nested() {
+ if constexpr (requires(int x) {
+ requires requires(int y) {
+ requires Emit<decltype([](auto&&) {})>::ok;
+ };
+ }) {}
+}
+// CHECK-DAG: define internal void @"??$?RH@<lambda_2>@?0??nested at GH181933@@YAXXZ at QEBA?A?<auto>@@$$QEAH at Z"
+
+} // namespace GH181933
``````````
</details>
https://github.com/llvm/llvm-project/pull/249314
More information about the cfe-commits
mailing list