[clang] [LifetimeSafety] Analyze blocks that cannot reach the exit in liveness (PR #240890)
Gábor Horváth via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 11 03:56:18 PST 2026
https://github.com/Xazax-hun updated https://github.com/llvm/llvm-project/pull/240890
>From 2d2e6f7f57f7bb35c4314e3a9c6cd13b6ee4ca48 Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Tue, 10 Nov 2026 16:00:48 +0000
Subject: [PATCH] [LifetimeSafety] Analyze blocks that cannot reach the exit in
liveness
The backward liveness analysis was seeded only at the exit block, so
blocks that cannot reach it, e.g., the body of an infinite loop, were
never analyzed and uses in them were missed.
Fixes #222273
Assisted-by: Claude Opus 5.5
---
clang/lib/Analysis/LifetimeSafety/Dataflow.h | 20 +++++-
.../Sema/LifetimeSafety/invalidations.cpp | 10 +++
clang/test/Sema/LifetimeSafety/safety.cpp | 72 +++++++++++++++++++
.../unittests/Analysis/LifetimeSafetyTest.cpp | 65 +++++++++++++++++
4 files changed, 165 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Analysis/LifetimeSafety/Dataflow.h b/clang/lib/Analysis/LifetimeSafety/Dataflow.h
index beda379a6a32ca..1e96628185b65c 100644
--- a/clang/lib/Analysis/LifetimeSafety/Dataflow.h
+++ b/clang/lib/Analysis/LifetimeSafety/Dataflow.h
@@ -99,10 +99,26 @@ class DataflowAnalysis {
const CFGBlock *Start = isForward() ? &Cfg.getEntry() : &Cfg.getExit();
InStates[Start] = D.getInitialState();
W.enqueueBlock(Start);
+ drainWorklist(D, W);
+
+ // Walking predecessors from the exit only reaches blocks that can reach the
+ // exit, e.g., it misses the body of an infinite loop. Analyze those too.
+ // Their in-state is left unset rather than seeded, so that only the first
+ // block analyzed in each such cycle starts from the initial state.
+ if constexpr (!isForward()) {
+ for (const CFGBlock *B : Cfg)
+ if (!InStates.contains(B))
+ W.enqueueBlock(B);
+ drainWorklist(D, W);
+ }
+ }
+private:
+ template <typename WorklistT> void drainWorklist(Derived &D, WorklistT &W) {
while (const CFGBlock *B = W.dequeue()) {
- Lattice StateIn = *getInState(B);
- Lattice StateOut = transferBlock(B, StateIn);
+ std::optional<Lattice> StateIn = getInState(B);
+ Lattice StateOut =
+ transferBlock(B, StateIn ? *StateIn : D.getInitialState());
OutStates[B] = StateOut;
for (const CFGBlock *AdjacentB : isForward() ? B->succs() : B->preds()) {
if (!AdjacentB)
diff --git a/clang/test/Sema/LifetimeSafety/invalidations.cpp b/clang/test/Sema/LifetimeSafety/invalidations.cpp
index 20a59355e24dad..d54068c9e53f4b 100644
--- a/clang/test/Sema/LifetimeSafety/invalidations.cpp
+++ b/clang/test/Sema/LifetimeSafety/invalidations.cpp
@@ -146,6 +146,16 @@ void IteratorInvalidationInAWhileLoop(std::vector<int> v) {
}
}
+void PointerInvalidationBeforeInfiniteLoop() {
+ std::vector<int> v = {1, 2, 3};
+ int* p = &v[0]; // expected-warning {{local variable 'v' is later invalidated}} \
+ // expected-note {{result of call to 'operator[]' aliases the storage of local variable 'v'}}
+ v.push_back(4); // expected-note {{local variable 'v' is invalidated here}}
+ *p = 10; // expected-note {{later used here}}
+ for (;;) {
+ }
+}
+
void NoIteratorInvalidationInAWhileLoopErase(std::unordered_map<int, int> mp) {
auto it = mp.begin();
while (it != std::end(mp)) {
diff --git a/clang/test/Sema/LifetimeSafety/safety.cpp b/clang/test/Sema/LifetimeSafety/safety.cpp
index a287bfb57ecce9..1ff31bb4da6a3c 100644
--- a/clang/test/Sema/LifetimeSafety/safety.cpp
+++ b/clang/test/Sema/LifetimeSafety/safety.cpp
@@ -339,6 +339,78 @@ void loop_with_break_gsl(bool cond) {
v.use(); // expected-note {{later used here}}
}
+void infinite_for_loop() {
+ MyObj safe;
+ MyObj* p = &safe;
+ {
+ MyObj s;
+ p = &s; // expected-warning {{local variable 's' does not live long enough}}
+ } // expected-note {{local variable 's' is destroyed here}}
+ for (;;)
+ use(*p); // expected-note {{later used here}}
+}
+
+// GH222273
+void infinite_while_loop(bool cond) {
+ while (true) {
+ MyObj* p;
+ if (cond) {
+ MyObj s;
+ p = &s; // expected-warning {{local variable 's' does not live long enough}}
+ } // expected-note {{local variable 's' is destroyed here}}
+ use(*p); // expected-note {{later used here}}
+ }
+}
+
+void infinite_goto_loop() {
+ View v;
+ {
+ MyObj s;
+ v = s; // expected-warning {{local variable 's' does not live long enough}}
+ } // expected-note {{local variable 's' is destroyed here}}
+again:
+ v.use(); // expected-note {{later used here}}
+ goto again;
+}
+
+void infinite_loop_on_one_branch(bool cond) {
+ MyObj safe;
+ MyObj* p = &safe;
+ {
+ MyObj s;
+ p = &s; // expected-warning {{local variable 's' does not live long enough}}
+ } // expected-note {{local variable 's' is destroyed here}}
+ if (cond)
+ for (;;)
+ use(*p); // expected-note {{later used here}}
+}
+
+void infinite_loops_on_both_branches(bool cond) {
+ MyObj safe;
+ MyObj* p = &safe;
+ MyObj* q = &safe;
+ {
+ MyObj s, t;
+ p = &s; // expected-warning {{local variable 's' does not live long enough}}
+ q = &t; // expected-warning {{local variable 't' does not live long enough}}
+ } // expected-note {{local variable 's' is destroyed here}} \
+ // expected-note {{local variable 't' is destroyed here}}
+ if (cond) {
+ for (;;)
+ use(*p); // expected-note {{later used here}}
+ } else {
+ for (;;)
+ use(*q); // expected-note {{later used here}}
+ }
+}
+
+void safe_infinite_loop() {
+ MyObj safe;
+ MyObj* p = &safe;
+ for (;;)
+ use(*p);
+}
+
void multiple_expiry_of_same_loan(bool cond) {
// Choose the last expiry location for the loan (e.g., through scope-ends and break statements).
MyObj safe;
diff --git a/clang/unittests/Analysis/LifetimeSafetyTest.cpp b/clang/unittests/Analysis/LifetimeSafetyTest.cpp
index 2c39cd5b52d2fc..531fb85337f8d6 100644
--- a/clang/unittests/Analysis/LifetimeSafetyTest.cpp
+++ b/clang/unittests/Analysis/LifetimeSafetyTest.cpp
@@ -1410,6 +1410,71 @@ TEST_F(LifetimeAnalysisTest, LivenessOutsideLoop) {
EXPECT_THAT(Origins({"p"}), MaybeLiveAt("p1"));
}
+TEST_F(LifetimeAnalysisTest, LivenessInInfiniteLoop) {
+ SetupTest(R"(
+ void target() {
+ MyObj s;
+ MyObj* p = &s;
+ POINT(p1);
+ for (;;) {
+ use(*p);
+ POINT(p2);
+ }
+ }
+ )");
+ EXPECT_THAT(Origins({"p"}), MustBeLiveAt("p1"));
+ // The loop's latch block is analyzed before its successors, which demotes the
+ // liveness in the loop body.
+ EXPECT_THAT(Origins({"p"}), MaybeLiveAt("p2"));
+}
+
+TEST_F(LifetimeAnalysisTest, LivenessInInfiniteLoopConditional) {
+ SetupTest(R"(
+ void target(bool c) {
+ MyObj s;
+ MyObj* p = &s;
+ for (;;) {
+ POINT(p1);
+ if (c)
+ use(*p);
+ }
+ }
+ )");
+ EXPECT_THAT(Origins({"p"}), MaybeLiveAt("p1"));
+}
+
+TEST_F(LifetimeAnalysisTest, LivenessInInfiniteLoopOnOneBranch) {
+ SetupTest(R"(
+ void target(bool c) {
+ MyObj s;
+ MyObj* p = &s;
+ POINT(p1);
+ if (c)
+ for (;;)
+ use(*p);
+ }
+ )");
+ EXPECT_THAT(Origins({"p"}), MaybeLiveAt("p1"));
+}
+
+TEST_F(LifetimeAnalysisTest, LivenessInInfiniteLoopsOnBothBranches) {
+ SetupTest(R"(
+ void target(bool c) {
+ MyObj s;
+ MyObj* p = &s;
+ POINT(p1);
+ if (c) {
+ for (;;)
+ use(*p);
+ } else {
+ for (;;)
+ use(*p);
+ }
+ }
+ )");
+ EXPECT_THAT(Origins({"p"}), MustBeLiveAt("p1"));
+}
+
// A use is an access through an lvalue: an lvalue-to-rvalue conversion or a
// write through it. Taking an address or copying a pointer out of a variable
// is neither.
More information about the cfe-commits
mailing list