[clang] [llvm] [clang] Implement lifetime analysis for lifetime_capture_by(X) (PR #115921)
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 19 14:13:52 PST 2024
================
@@ -1412,17 +1438,34 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
return false;
};
+ bool HasReferenceBinding = Init->isGLValue();
llvm::SmallVector<IndirectLocalPathEntry, 8> Path;
- if (LK == LK_Assignment &&
- shouldRunGSLAssignmentAnalysis(SemaRef, *AEntity)) {
- Path.push_back(
- {isAssignmentOperatorLifetimeBound(AEntity->AssignmentOperator)
- ? IndirectLocalPathEntry::LifetimeBoundCall
- : IndirectLocalPathEntry::GslPointerAssignment,
- Init});
+ switch (LK) {
+ case LK_Assignment: {
+ if (shouldRunGSLAssignmentAnalysis(SemaRef, *AEntity))
+ Path.push_back(
+ {isAssignmentOperatorLifetimeBound(AEntity->AssignmentOperator)
+ ? IndirectLocalPathEntry::LifetimeBoundCall
+ : IndirectLocalPathEntry::GslPointerAssignment,
+ Init});
+ break;
+ }
+ case LK_LifetimeCapture: {
+ Path.push_back({IndirectLocalPathEntry::LifetimeCapture, Init});
+ if (isRecordWithAttr<PointerAttr>(Init->getType()))
+ HasReferenceBinding = false;
+ // Skip the top MTE if it is a temporary object of the pointer-like type
+ // itself.
+ if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Init);
+ MTE && isPointerLikeType(Init->getType()))
+ Init = MTE->getSubExpr();
----------------
hokein wrote:
The assert is being triggered because Path is empty. It seems that lifetime extension is only relevant in the `LK_Extended` case, and doesn't apply to other cases where we don’t check it. I think we can just remove the assert. (we should probably do the same thing for `GSLPointerAssignment`, I think this one can be merged with the `GSLPointerInit`).
Additionally, I’d prefer not to introduce a new `LifetimeCapture` kind unless necessary. Adding it would increase code complexity, requiring updates across multiple `switch (Kind)` statements.
https://github.com/llvm/llvm-project/pull/115921
More information about the cfe-commits
mailing list