https://github.com/usx95 updated 
https://github.com/llvm/llvm-project/pull/148712

>From 542bc99c1f5fb5fc28a3374f61b2fa3d059d9d75 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 6ce317303d599371fc61529eb1d3e7a1624aa8fb 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 | 62 +++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)

diff --git a/clang/lib/Analysis/LifetimeSafety.cpp 
b/clang/lib/Analysis/LifetimeSafety.cpp
index 9d1b662e3c589..52c6b18021d7a 100644
--- a/clang/lib/Analysis/LifetimeSafety.cpp
+++ b/clang/lib/Analysis/LifetimeSafety.cpp
@@ -763,6 +763,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)`
@@ -795,5 +854,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

Reply via email to