https://github.com/usx95 updated https://github.com/llvm/llvm-project/pull/148712
>From 111163fc65843dfd135d60bb45b1311a21d60338 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 5f0991338d17813d9af276a0cea2d30875a9aa23 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 | 64 +++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/clang/lib/Analysis/LifetimeSafety.cpp b/clang/lib/Analysis/LifetimeSafety.cpp index f9a7093987896..217b02b416c1a 100644 --- a/clang/lib/Analysis/LifetimeSafety.cpp +++ b/clang/lib/Analysis/LifetimeSafety.cpp @@ -518,6 +518,8 @@ enum class Direction { Forward, Backward }; /// analysis. /// \tparam LatticeType The dataflow lattice used by the analysis. /// \tparam Dir The direction of the analysis (Forward or Backward). +/// TODO: Maybe use the dataflow framework! The framework might need changes +/// to support the current comparison done at block-entry. template <typename Derived, typename LatticeType, Direction Dir> class DataflowAnalysis { public: @@ -775,6 +777,65 @@ class LoanPropagationAnalysis } }; +// ========================================================================= // +// Expired Loans Analysis +// ========================================================================= // + +/// The dataflow lattice for tracking the set of expired loans. +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"; + } +}; + +/// The analysis that tracks which loans have expired. +class ExpiredLoansAnalysis + : public DataflowAnalysis<ExpiredLoansAnalysis, ExpiredLattice, + Direction::Forward> { + + LoanSet::Factory &Factory; + +public: + ExpiredLoansAnalysis(const CFG &C, AnalysisDeclContext &AC, FactManager &F, + LifetimeFactory &SF) + : DataflowAnalysis(C, AC, F), Factory(SF.LoanSetFactory) {} + + using Base::transfer; + + StringRef 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)` @@ -807,5 +868,8 @@ void runLifetimeSafetyAnalysis(const DeclContext &DC, const CFG &Cfg, LoanPropagationAnalysis LoanPropagation(Cfg, AC, FactMgr, Factory); LoanPropagation.run(); DEBUG_WITH_TYPE("LifetimeLoanPropagation", LoanPropagation.dump()); + + ExpiredLoansAnalysis ExpiredLoans(Cfg, AC, FactMgr, Factory); + ExpiredLoans.run(); } } // namespace clang _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits