[clang] [LifetimeSafety] Add origin tracking for lambda captures (PR #185216)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 9 02:58:26 PDT 2026


================
@@ -51,7 +51,19 @@ class MissingOriginCollector
 } // namespace
 
 bool hasOrigins(QualType QT) {
-  return QT->isPointerOrReferenceType() || isGslPointerType(QT);
+  if (QT->isPointerOrReferenceType() || isGslPointerType(QT))
+    return true;
+  const auto *RD = QT->getAsCXXRecordDecl();
+  if (!RD)
+    return false;
+  // TODO: Limit to lambdas for now. This will be extended to user-defined
+  // structs with pointer-like fields.
+  if (!RD->isLambda())
+    return false;
+  for (const auto *FD : RD->fields())
+    if (hasOrigins(FD->getType()))
----------------
Xazax-hun wrote:

Please add a test case that has reference captures like:
```
int i;
auto lambda = [&]() { return i; };
```

Here, we capture only an integer but capture it by reference, so the lambda should not outlive the integer.

Do we get a reference field here? 

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


More information about the cfe-commits mailing list