[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:52:34 PDT 2025
https://github.com/usx95 updated https://github.com/llvm/llvm-project/pull/148712
>From a9cb32152b8ba94a6a93c7923cce1abac43c9022 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 a50b00e1b5394dacd635fd1da3aef83eb54348e5 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 99bd7bc36faf5..1ac8f42318b8d 100644
--- a/clang/lib/Analysis/LifetimeSafety.cpp
+++ b/clang/lib/Analysis/LifetimeSafety.cpp
@@ -747,6 +747,65 @@ class LoanPropagationAnalysis
}
};
+// ========================================================================= //
+// 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)`
@@ -779,5 +838,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