[llvm-branch-commits] [clang] [LifetimeSafety] Add loan expiry analysis (PR #148712)
Utkarsh Saxena via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Jul 14 12:49:30 PDT 2025
https://github.com/usx95 updated https://github.com/llvm/llvm-project/pull/148712
>From 7c2838035392804a2cd9bda1d0751e3633d2a31f Mon Sep 17 00:00:00 2001
From: Utkarsh Saxena <usx at google.com>
Date: Mon, 14 Jul 2025 19:32:35 +0000
Subject: [PATCH 1/2] users/usx95/lifetime-safety-add-loan-expiry
>From ed73e3b1914a25c525bbb8f06fa0112cc4eb4bd0 Mon Sep 17 00:00:00 2001
From: Utkarsh Saxena <usx at google.com>
Date: Mon, 14 Jul 2025 19:37:49 +0000
Subject: [PATCH 2/2] [LifetimeSafety] Add loan expiry analysis
---
clang/lib/Analysis/LifetimeSafety.cpp | 62 +++++++++++++++++++++++++++
1 file changed, 62 insertions(+)
diff --git a/clang/lib/Analysis/LifetimeSafety.cpp b/clang/lib/Analysis/LifetimeSafety.cpp
index c1bd4e82f4327..1aba655a6ead1 100644
--- a/clang/lib/Analysis/LifetimeSafety.cpp
+++ b/clang/lib/Analysis/LifetimeSafety.cpp
@@ -746,6 +746,65 @@ class LoanPropagationAnalysis
return Factory.LoanSetFact.getEmptySet();
}
};
+// ========================================================================= //
+// Expired Loans Analysis
+// ========================================================================= //
+
+/// The lattice for tracking expired loans. It is a set of loan IDs.
+struct ExpiredLattice {
+ LoanSet Expired;
+
+ ExpiredLattice() : Expired(nullptr) {};
+ explicit ExpiredLattice(LoanSet S) : Expired(S) {}
+
+ bool operator==(const ExpiredLattice &Other) const {
+ return Expired == Other.Expired;
+ }
+ bool operator!=(const ExpiredLattice &Other) const {
+ return !(*this == Other);
+ }
+
+ void dump(llvm::raw_ostream &OS) const {
+ OS << "ExpiredLattice State:\n";
+ if (Expired.isEmpty())
+ OS << " <empty>\n";
+ for (const LoanID &LID : Expired)
+ OS << " Loan " << LID << " is expired\n";
+ }
+};
+
+class ExpiredLoansAnalysis
+ : public DataflowAnalysis<ExpiredLoansAnalysis, ExpiredLattice> {
+
+ LoanSet::Factory &SetFactory;
+
+public:
+ ExpiredLoansAnalysis(const CFG &C, AnalysisDeclContext &AC, FactManager &F,
+ LoanSet::Factory &SF)
+ : DataflowAnalysis(C, AC, F), SetFactory(SF) {}
+
+ using DataflowAnalysis<ExpiredLoansAnalysis, Lattice>::transfer;
+
+ const char *getAnalysisName() const { return "ExpiredLoans"; }
+
+ Lattice getInitialState() { return Lattice(SetFactory.getEmptySet()); }
+
+ Lattice join(Lattice L1, Lattice L2) const {
+ LoanSet JoinedSet = L1.Expired;
+ for (LoanID LID : L2.Expired)
+ JoinedSet = SetFactory.add(JoinedSet, LID);
+ return Lattice(JoinedSet);
+ }
+
+ Lattice transfer(Lattice In, const ExpireFact &F) {
+ return Lattice(SetFactory.add(In.Expired, F.getLoanID()));
+ }
+
+ Lattice transfer(Lattice In, const IssueFact &F) {
+ return Lattice(SetFactory.remove(In.Expired, F.getLoanID()));
+ }
+};
+
// ========================================================================= //
// TODO:
// - Modifying loan propagation to answer `LoanSet getLoans(Origin O, Point P)`
@@ -778,5 +837,8 @@ void runLifetimeSafetyAnalysis(const DeclContext &DC, const CFG &Cfg,
LoanPropagationAnalysis LoanPropagation(Cfg, AC, FactMgr, LifetimeFact);
LoanPropagation.run();
DEBUG_WITH_TYPE("LifetimeLoanPropagation", LoanPropagation.dump());
+
+ ExpiredLoansAnalysis ExpiredAnalysis(Cfg, AC, FactMgr,
+ LifetimeFact.LoanSetFact);
}
} // namespace clang
More information about the llvm-branch-commits
mailing list