[clang] [clang] Introduce [[clang::lifetime_capture_by(X)]] (PR #111499)
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 24 01:27:34 PDT 2024
================
@@ -269,6 +271,40 @@ void Sema::inferLifetimeBoundAttribute(FunctionDecl *FD) {
}
}
+static bool IsPointerLikeType(QualType QT) {
+ QT = QT.getNonReferenceType();
+ // if (QT->isPointerType())
+ // return true;
+ auto *RD = QT->getAsCXXRecordDecl();
+ if (!RD)
+ return false;
+ RD = RD->getCanonicalDecl();
+ if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
+ RD = CTSD->getSpecializedTemplate()->getTemplatedDecl();
+ return RD->hasAttr<PointerAttr>();
+}
+
+void Sema::inferLifetimeCaptureByAttribute(FunctionDecl *FD) {
+ if (!FD)
+ return;
+ auto *MD = dyn_cast<CXXMethodDecl>(FD);
+ if (!MD || !MD->getIdentifier() || !MD->getParent()->isInStdNamespace())
+ return;
+ static const llvm::StringSet<> CapturingMethods{"insert", "push",
----------------
hokein wrote:
I think the `[]` operator for map-like container is an important case, code like below is probably not rare:
```
std::map<string_view, ...> m;
m[ReturnString(..)] = ...; // this leaves a dangling string_view key in the map.
```
https://github.com/llvm/llvm-project/pull/111499
More information about the cfe-commits
mailing list