[clang] [LifetimeSafety] Detect use-after-invalidation for STL containers (PR #179093)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 5 09:11:10 PST 2026


================
@@ -175,9 +179,66 @@ class LifetimeChecker {
     FinalWarningsMap[ExpiredLoan] = {/*ExpiryLoc=*/EF->getExpiryLoc(),
                                      /*BestCausingFact=*/BestCausingFact,
                                      /*MovedExpr=*/MovedExpr,
+                                     /*InvalidatedByExpr=*/nullptr,
                                      /*ConfidenceLevel=*/CurConfidence};
   }
 
+  // TODO: Doc.
+  void checkInvalidation(const InvalidateOriginFact *IOF) {
+    OriginID InvalidatedOrigin = IOF->getInvalidatedOrigin();
+    /// Get loans directly pointing to the invalidated container
+    LoanSet DirectlyInvalidatedLoans =
+        LoanPropagation.getLoans(InvalidatedOrigin, IOF);
+    llvm::DenseSet<LoanID> AllInvalidatedLoans;
+
+    auto GetAccessPath = [](const Loan *L) -> AccessPath {
+      if (auto *PL = dyn_cast<PathLoan>(L))
+        return PL->getAccessPath();
+      // TODO: Handle place holder loans.
+      // if (auto *PL = dyn_cast<PlaceholderLoan>(L))
+      //   return PL->getParmVarDecl();
+      return {};
+    };
+
+    // Build set of all loans that reference the invalidated container.
+    // We match loans by AccessPath because multiple IssueFacts may create
+    // loans to the same container.
+    FactMgr.forAllPredecessors(IOF, [&](const Fact *PF) {
----------------
usx95 wrote:

Now we iterate over live loans. Thanks for the suggestion.

https://github.com/llvm/llvm-project/pull/179093


More information about the cfe-commits mailing list