https://github.com/usx95 updated https://github.com/llvm/llvm-project/pull/148712
>From 8f6394b2ab6183fd06f6e216cbaa70b823809e5d Mon Sep 17 00:00:00 2001 From: Utkarsh Saxena <u...@google.com> Date: Mon, 14 Jul 2025 19:32:35 +0000 Subject: [PATCH 1/2] users/usx95/lifetime-safety-add-loan-expiry >From 12e5b9a2dc200a9d8e710ef509bb39021fc92de0 Mon Sep 17 00:00:00 2001 From: Utkarsh Saxena <u...@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 | 61 +++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/clang/lib/Analysis/LifetimeSafety.cpp b/clang/lib/Analysis/LifetimeSafety.cpp index 0b4241cdf404c..300cc6ddd66af 100644 --- a/clang/lib/Analysis/LifetimeSafety.cpp +++ b/clang/lib/Analysis/LifetimeSafety.cpp @@ -769,6 +769,65 @@ class LoanPropagationAnalysis } }; +// ========================================================================= // +// Expired Loans Analysis +// ========================================================================= // + +/// The dataflow lattice for tracking the set of expired loans. +class ExpiredLattice { +public: + 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"; + } +}; + +/// The analysis that tracks which loans have expired. +class ExpiredLoansAnalysis + : public DataflowAnalysis<ExpiredLoansAnalysis, ExpiredLattice> { + + LoanSet::Factory &Factory; + +public: + ExpiredLoansAnalysis(const CFG &C, AnalysisDeclContext &AC, FactManager &F, + LifetimeFactory &SF) + : DataflowAnalysis(C, AC, F), Factory(SF.LoanSetFactory) {} + + using DataflowAnalysis<ExpiredLoansAnalysis, Lattice>::transfer; + + const char *getAnalysisName() const { return "ExpiredLoans"; } + + Lattice getInitialState() { return Lattice(Factory.getEmptySet()); } + + /// Merges two lattices by taking the union of the expired loan sets. + Lattice join(Lattice L1, Lattice L2) const { + return Lattice(utils::join(L1.Expired, L2.Expired, Factory)); + } + + Lattice transfer(Lattice In, const ExpireFact &F) { + return Lattice(Factory.add(In.Expired, F.getLoanID())); + } + + Lattice transfer(Lattice In, const IssueFact &F) { + return Lattice(Factory.remove(In.Expired, F.getLoanID())); + } +}; + // ========================================================================= // // TODO: // - Modifying loan propagation to answer `LoanSet getLoans(Origin O, Point P)` @@ -801,5 +860,7 @@ void runLifetimeSafetyAnalysis(const DeclContext &DC, const CFG &Cfg, LoanPropagationAnalysis LoanPropagation(Cfg, AC, FactMgr, Factory); LoanPropagation.run(); DEBUG_WITH_TYPE("LifetimeLoanPropagation", LoanPropagation.dump()); + + ExpiredLoansAnalysis ExpiredAnalysis(Cfg, AC, FactMgr, Factory); } } // namespace clang _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits