[clang] [clang] Don't use `VarDecl` of local variables as `ManglingContextDecl` for lambdas (PR #179035)

Jan Kokemüller via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 3 08:14:39 PST 2026


================
@@ -304,12 +304,24 @@ Sema::getCurrentMangleNumberContext(const DeclContext *DC) {
     //    Yeah, I think the only cases left where lambdas don't need a
     //    mangling are when they have (effectively) internal linkage or appear
     //    in a non-inline function in a non-module translation unit.
-    if (auto *ND = dyn_cast<NamedDecl>(ManglingContextDecl ? ManglingContextDecl
-                                                           : cast<Decl>(DC));
+
+    Decl *ManglingContextDeclForModule = [&]() {
+      if (!ManglingContextDecl || [&]() {
+            // If we must allocate mangling numbers but the
+            // `ManglingContextDecl` is a local variable, use the `DeclContext`
+            // containing the lambda expression instead.
+            VarDecl *Var = dyn_cast<VarDecl>(ManglingContextDecl);
+            return Var && Var->isLocalVarDecl();
+          }())
+        return const_cast<Decl *>(cast<Decl>(DC));
----------------
jiixyj wrote:

Hm, this doesn't quite work, sadly... It doesn't handle the case where `ManglingContextDecl` is set, but not a local `VarDecl`. Any other ideas?

https://github.com/llvm/llvm-project/pull/179035


More information about the cfe-commits mailing list