[clang] Add support for lifetime capture_by (PR #196884)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Mon May 11 03:11:17 PDT 2026


================
@@ -864,6 +864,74 @@ void FactsGenerator::handleImplicitObjectFieldUses(const Expr *Call,
   });
 }
 
+void FactsGenerator::handleLifetimeCaptureBy(const FunctionDecl *FD,
+                                             ArrayRef<const Expr *> Args) {
+  bool isInstance = false;
+  if (const auto *Method = dyn_cast<CXXMethodDecl>(FD);
+      Method && Method->isInstance() && !isa<CXXConstructorDecl>(FD)) {
+    isInstance = true;
+  }
+  auto getArgCaptureBy = [FD,
+                          isInstance](unsigned I) -> LifetimeCaptureByAttr * {
+    const ParmVarDecl *PVD = nullptr;
+    // FIXME: Add support for capture_by on function declarations
+    if (isInstance) {
+      if (I > 0 && I - 1 < FD->getNumParams()) {
+        PVD = FD->getParamDecl(I - 1);
+      }
+    } else {
+      if (I < FD->getNumParams()) {
+        PVD = FD->getParamDecl(I);
+      }
+    }
+    return PVD ? PVD->getAttr<LifetimeCaptureByAttr>() : nullptr;
+  };
+  if (Args.empty())
----------------
Xazax-hun wrote:

Nit: move this early exist to the very beginning?

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


More information about the cfe-commits mailing list