[clang] [clang][bytecode] Only check static lambda captures if we have to (PR #178452)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 28 07:50:06 PST 2026


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/178452

Call `getCaptureFields()` only if the function is static, because we only care about the captures in that case.

>From 45c43bcde110865847e56d5523153449ac41385b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 28 Jan 2026 16:24:21 +0100
Subject: [PATCH] static lambdas

---
 clang/lib/AST/ByteCode/Context.cpp | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Context.cpp b/clang/lib/AST/ByteCode/Context.cpp
index 208fcb2a2732e..5d5816eb02cd2 100644
--- a/clang/lib/AST/ByteCode/Context.cpp
+++ b/clang/lib/AST/ByteCode/Context.cpp
@@ -555,15 +555,15 @@ const Function *Context::getOrCreateFunction(const FunctionDecl *FuncDecl) {
       // the lambda captures.
       if (!MD->getParent()->isCompleteDefinition())
         return nullptr;
-      llvm::DenseMap<const ValueDecl *, FieldDecl *> LC;
-      FieldDecl *LTC;
+      if (MD->isStatic()) {
+        llvm::DenseMap<const ValueDecl *, FieldDecl *> LC;
+        FieldDecl *LTC;
 
-      MD->getParent()->getCaptureFields(LC, LTC);
-
-      if (MD->isStatic() && !LC.empty()) {
+        MD->getParent()->getCaptureFields(LC, LTC);
         // Static lambdas cannot have any captures. If this one does,
         // it has already been diagnosed and we can only ignore it.
-        return nullptr;
+        if (!LC.empty())
+          return nullptr;
       }
     }
   }



More information about the cfe-commits mailing list