================
@@ -207,6 +226,65 @@ class LifetimeChecker {
}
}
+ /// An entity that a parameter is captured by.
+ struct Capturer {
+ CapturingEntity Entity;
+ /// The index that a [[clang::lifetime_capture_by]] attribute uses for it.
+ int Index;
+ /// The field or global, if known.
+ const ValueDecl *CapturedBy;
+ };
+
+ /// Records \p PVD if it escapes through \p OEF into an entity that its
+ /// written [[clang::lifetime_capture_by]] attributes do not name. Inferred
+ /// attributes are not verified.
+ void checkCaptureBy(const ParmVarDecl &PVD, const OriginEscapesFact &OEF) {
+ std::optional<Capturer> C = getCapturer(OEF);
+ if (!C)
+ return;
+ // A lifetimebound constructor parameter is captured by the constructed
+ // object.
+ if (C->Entity == CapturingEntity::This && isa<CXXConstructorDecl>(FD) &&
+ PVD.hasAttr<LifetimeBoundAttr>())
+ return;
+ const LifetimeCaptureByAttr *FirstWritten = nullptr;
+ for (const auto *A : PVD.specific_attrs<LifetimeCaptureByAttr>()) {
+ if (A->isImplicit())
+ continue;
+ if (!FirstWritten)
+ FirstWritten = A;
+ // FIXME: The capturers of a lambda's parameters are never resolved, so
+ // they stay invalid and are not verified.
+ if (llvm::any_of(A->params(), [&](int Idx) {
+ return Idx == C->Index || Idx == LifetimeCaptureByAttr::Unknown ||
+ Idx == LifetimeCaptureByAttr::Invalid;
+ }))
+ return;
+ }
+ if (FirstWritten)
+ CaptureByViolations.try_emplace(
+ {&PVD, C->Entity}, CaptureByViolation{FirstWritten, C->CapturedBy});
+ }
+
+ /// Returns the entity that a parameter escaping through \p OEF is captured
+ /// by, if a [[clang::lifetime_capture_by]] attribute can name it.
+ std::optional<Capturer> getCapturer(const OriginEscapesFact &OEF) const {
+ if (isa<FieldEscapeFact, ThisEscapeFact>(OEF)) {
+ // Index 0 names `this` only with an implicit object parameter;
+ // otherwise it names the first parameter.
+ const auto *MD = dyn_cast<CXXMethodDecl>(FD);
+ if (!MD || !MD->isImplicitObjectMemberFunction())
+ return std::nullopt;
+ const auto *FieldEsc = dyn_cast<FieldEscapeFact>(&OEF);
+ return Capturer{CapturingEntity::This, LifetimeCaptureByAttr::This,
+ FieldEsc ? FieldEsc->getFieldDecl() : nullptr};
+ }
+ if (const auto *GlobalEsc = dyn_cast<GlobalEscapeFact>(&OEF))
+ return Capturer{CapturingEntity::Global, LifetimeCaptureByAttr::Global,
----------------
Xazax-hun wrote:
Yeah, the index was not needed. Also now I simplified the code a bit further,
reusing more from LifatimeCaptureByAttr instead of having a separate
CapturingEntity. The index would have been useful in the future for validating
parameters but let's cross that bridge once we are there :D
https://github.com/llvm/llvm-project/pull/207346
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits