[clang] [LifetimeSafety] Implement multi-level origins (PR #168344)
Gábor Horváth via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 15 04:09:54 PST 2025
================
@@ -314,39 +474,34 @@ bool FactsGenerator::handleTestPoint(const CXXFunctionalCastExpr *FCE) {
return false;
}
-void FactsGenerator::handleAssignment(const Expr *LHSExpr,
- const Expr *RHSExpr) {
- if (!hasOrigin(LHSExpr))
- return;
- // Find the underlying variable declaration for the left-hand side.
- if (const auto *DRE_LHS =
- dyn_cast<DeclRefExpr>(LHSExpr->IgnoreParenImpCasts())) {
- markUseAsWrite(DRE_LHS);
- if (const auto *VD_LHS = dyn_cast<ValueDecl>(DRE_LHS->getDecl())) {
- // Kill the old loans of the destination origin and flow the new loans
- // from the source origin.
- killAndFlowOrigin(*VD_LHS, *RHSExpr);
- }
- }
-}
-
// A DeclRefExpr will be treated as a use of the referenced decl. It will be
// checked for use-after-free unless it is later marked as being written to
// (e.g. on the left-hand side of an assignment).
void FactsGenerator::handleUse(const DeclRefExpr *DRE) {
- if (isPointerType(DRE->getType())) {
- UseFact *UF = FactMgr.createFact<UseFact>(DRE, FactMgr.getOriginMgr());
- CurrentBlockFacts.push_back(UF);
- assert(!UseFacts.contains(DRE));
- UseFacts[DRE] = UF;
+ OriginList *List = getOriginsList(*DRE);
+ if (!List)
+ return;
+ // Remove the outer layer of origin which borrows from the decl directly. This
+ // is a use of the underlying decl.
+ List = getRValueOrigins(DRE, List);
+ // Skip if there is no inner origin (e.g., when it is not a pointer type).
+ if (!List)
+ return;
+ llvm::SmallVector<OriginID> UsedOrigins;
+ OriginList *L = List;
+ while (L) {
+ UsedOrigins.push_back(L->getOuterOriginID());
+ L = L->peelOuterOrigin();
}
+ UseFact *UF = FactMgr.createFact<UseFact>(DRE, std::move(UsedOrigins));
----------------
Xazax-hun wrote:
I wonder if the code would be simpler if we always had the same format everywhere for origin lists. E.g., if `UseFact` took an `OriginList` instead of a `SmallVector`. Might make it easier to switch to a tree in the future if we need it.
https://github.com/llvm/llvm-project/pull/168344
More information about the cfe-commits
mailing list