[clang] [analyzer] Skip LifetimeEnd nodes in getNextStmtForDiagnostics (PR #228045)
Benedek Kaibas via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 28 14:59:26 PST 2026
https://github.com/benedekaibas updated https://github.com/llvm/llvm-project/pull/228045
>From c7a035062abce927d8623817e421275d7bfb6feb Mon Sep 17 00:00:00 2001
From: benedekaibas <kaibas01 at allegheny.edu>
Date: Thu, 1 Oct 2026 13:29:01 +0200
Subject: [PATCH 1/6] [analyzer] Skip LifetimeEnd nodes in
getNextStmtForDiagnostics
---
.../lib/StaticAnalyzer/Core/ExplodedGraph.cpp | 3 ++
.../test/Analysis/lifetime-end-path-notes.cpp | 51 +++++++++++++++++++
2 files changed, 54 insertions(+)
create mode 100644 clang/test/Analysis/lifetime-end-path-notes.cpp
diff --git a/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp b/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp
index 50b341c13a4cc8..fe133ece043dfe 100644
--- a/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp
@@ -347,6 +347,9 @@ const Stmt *ExplodedNode::getNextStmtForDiagnostics() const {
for (const ExplodedNode *N = getFirstSucc(); N; N = N->getFirstSucc()) {
if (N->getLocation().isPurgeKind())
continue;
+ if (ProgramPoint P = N->getLocation(); P.getAs<LifetimeEnd>())
+ // P.dump();
+ continue;
if (const Stmt *S = N->getStmtForDiagnostics()) {
// Check if the statement is '?' or '&&'/'||'. These are "merges",
// not actual statement points.
diff --git a/clang/test/Analysis/lifetime-end-path-notes.cpp b/clang/test/Analysis/lifetime-end-path-notes.cpp
new file mode 100644
index 00000000000000..9fba6b7f845307
--- /dev/null
+++ b/clang/test/Analysis/lifetime-end-path-notes.cpp
@@ -0,0 +1,51 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection,alpha.core.DanglingPtrDeref -analyzer-config cfg-lifetime=true -analyzer-output=text -verify %s
+
+void clang_analyzer_warnIfReached();
+
+// Enabling CFG lifetime-end elements should not cause regression in the
+// path notes. The loop exit path note must point to the next executed
+// statmeent and not back to the loop.
+void testPathNotesWithLoopScopeEnd() {
+ int *p = nullptr;
+ for (int i = 0; i < 3; ++i) {
+ // expected-note at -1 3 {{Loop condition is true. Entering loop body}}
+ // expected-note at -2 {{The value 2 is assigned to 'i'}}
+ // expected-note at -3 {{Loop condition is false. Execution continues on line 17}}
+ p = &i; // expected-note {{Value assigned to 'p'}}
+ }
+ // expected-note at -1 {{'i' is destroyed here}}
+ *p = 4;
+ // expected-warning at -1 {{Use of 'i' after its lifetime ended}}
+ // expected-note at -2 {{Use of 'i' after its lifetime ended}}
+}
+
+void testPathNotesWithBlockScopeEnd() {
+ int *p = nullptr;
+ {
+ int n = 0;
+ while (n < 3) {
+ // expected-note at -1 3 {{Loop condition is true. Entering loop body}}
+ // expected-note at -2 {{Loop condition is false. Execution continues on line 34}}
+ p = &n; // expected-note {{Value assigned to 'p'}}
+ ++n; // expected-note {{The value 2 is assigned to 'n'}}
+ }
+ }
+ // expected-note at -1 {{'n' is destroyed here}}
+ *p = 4;
+ // expected-warning at -1 {{Use of 'n' after its lifetime ended}}
+ // expected-note at -2 {{Use of 'n' after its lifetime ended}}
+}
+
+void testPathNotesWithWarnIfReached() {
+ {
+ int i = 0;
+ while (i < 3) {
+ // expected-note at -1 3 {{Loop condition is true. Entering loop body}}
+ // expected-note at -2 {{Loop condition is false. Execution continues on line 48}}
+ ++i;
+ }
+ }
+ clang_analyzer_warnIfReached();
+ // expected-warning at -1 {{REACHABLE}}
+ // expected-note at -2 {{REACHABLE}}
+}
>From e2ef902d15744a4dd17d17e3e5e844b55fc041e0 Mon Sep 17 00:00:00 2001
From: benedekaibas <kaibas01 at allegheny.edu>
Date: Tue, 27 Oct 2026 21:20:13 +0100
Subject: [PATCH 2/6] Add changes back correctly.
---
clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp | 3 +--
clang/test/Analysis/lifetime-end-path-notes.cpp | 3 ++-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp b/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp
index fe133ece043dfe..d66ef10124acc4 100644
--- a/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp
@@ -347,8 +347,7 @@ const Stmt *ExplodedNode::getNextStmtForDiagnostics() const {
for (const ExplodedNode *N = getFirstSucc(); N; N = N->getFirstSucc()) {
if (N->getLocation().isPurgeKind())
continue;
- if (ProgramPoint P = N->getLocation(); P.getAs<LifetimeEnd>())
- // P.dump();
+ if (N->getLocation().getAs<LifetimeEnd>())
continue;
if (const Stmt *S = N->getStmtForDiagnostics()) {
// Check if the statement is '?' or '&&'/'||'. These are "merges",
diff --git a/clang/test/Analysis/lifetime-end-path-notes.cpp b/clang/test/Analysis/lifetime-end-path-notes.cpp
index 9fba6b7f845307..6d40818d3485f7 100644
--- a/clang/test/Analysis/lifetime-end-path-notes.cpp
+++ b/clang/test/Analysis/lifetime-end-path-notes.cpp
@@ -1,4 +1,5 @@
-// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection,alpha.core.DanglingPtrDeref -analyzer-config cfg-lifetime=true -analyzer-output=text -verify %s
+// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection,alpha.core.DanglingPtrDeref\
+// RUN: -analyzer-config cfg-lifetime=true -analyzer-output=text -verify %s
void clang_analyzer_warnIfReached();
>From ca155a39b6b46300e7f3699b4bff35bc29524493 Mon Sep 17 00:00:00 2001
From: benedekaibas <kaibas01 at allegheny.edu>
Date: Tue, 27 Oct 2026 21:24:43 +0100
Subject: [PATCH 3/6] Fix line numbers in expected notes.
---
clang/test/Analysis/lifetime-end-path-notes.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/test/Analysis/lifetime-end-path-notes.cpp b/clang/test/Analysis/lifetime-end-path-notes.cpp
index 6d40818d3485f7..aa30eb0f477548 100644
--- a/clang/test/Analysis/lifetime-end-path-notes.cpp
+++ b/clang/test/Analysis/lifetime-end-path-notes.cpp
@@ -11,7 +11,7 @@ void testPathNotesWithLoopScopeEnd() {
for (int i = 0; i < 3; ++i) {
// expected-note at -1 3 {{Loop condition is true. Entering loop body}}
// expected-note at -2 {{The value 2 is assigned to 'i'}}
- // expected-note at -3 {{Loop condition is false. Execution continues on line 17}}
+ // expected-note at -3 {{Loop condition is false. Execution continues on line 18}}
p = &i; // expected-note {{Value assigned to 'p'}}
}
// expected-note at -1 {{'i' is destroyed here}}
@@ -26,7 +26,7 @@ void testPathNotesWithBlockScopeEnd() {
int n = 0;
while (n < 3) {
// expected-note at -1 3 {{Loop condition is true. Entering loop body}}
- // expected-note at -2 {{Loop condition is false. Execution continues on line 34}}
+ // expected-note at -2 {{Loop condition is false. Execution continues on line 35}}
p = &n; // expected-note {{Value assigned to 'p'}}
++n; // expected-note {{The value 2 is assigned to 'n'}}
}
@@ -42,7 +42,7 @@ void testPathNotesWithWarnIfReached() {
int i = 0;
while (i < 3) {
// expected-note at -1 3 {{Loop condition is true. Entering loop body}}
- // expected-note at -2 {{Loop condition is false. Execution continues on line 48}}
+ // expected-note at -2 {{Loop condition is false. Execution continues on line 49}}
++i;
}
}
>From 4d4cc652710b90f9e11f63f89ec16c62900d5aa1 Mon Sep 17 00:00:00 2001
From: benedekaibas <kaibas01 at allegheny.edu>
Date: Tue, 27 Oct 2026 21:37:04 +0100
Subject: [PATCH 4/6] Correctly fix RUN lines, indentation, line numbering in
the test file.
---
clang/test/Analysis/lifetime-end-path-notes.cpp | 16 +++++++---------
1 file changed, 7 insertions(+), 9 deletions(-)
diff --git a/clang/test/Analysis/lifetime-end-path-notes.cpp b/clang/test/Analysis/lifetime-end-path-notes.cpp
index aa30eb0f477548..0e0e6e7599001b 100644
--- a/clang/test/Analysis/lifetime-end-path-notes.cpp
+++ b/clang/test/Analysis/lifetime-end-path-notes.cpp
@@ -11,10 +11,9 @@ void testPathNotesWithLoopScopeEnd() {
for (int i = 0; i < 3; ++i) {
// expected-note at -1 3 {{Loop condition is true. Entering loop body}}
// expected-note at -2 {{The value 2 is assigned to 'i'}}
- // expected-note at -3 {{Loop condition is false. Execution continues on line 18}}
+ // expected-note at -3 {{Loop condition is false. Execution continues on line 17}}
p = &i; // expected-note {{Value assigned to 'p'}}
- }
- // expected-note at -1 {{'i' is destroyed here}}
+ } // expected-note {{'i' is destroyed here}}
*p = 4;
// expected-warning at -1 {{Use of 'i' after its lifetime ended}}
// expected-note at -2 {{Use of 'i' after its lifetime ended}}
@@ -25,13 +24,12 @@ void testPathNotesWithBlockScopeEnd() {
{
int n = 0;
while (n < 3) {
- // expected-note at -1 3 {{Loop condition is true. Entering loop body}}
- // expected-note at -2 {{Loop condition is false. Execution continues on line 35}}
+ // expected-note at -1 3 {{Loop condition is true. Entering loop body}}
+ // expected-note at -2 {{Loop condition is false. Execution continues on line 33}}
p = &n; // expected-note {{Value assigned to 'p'}}
++n; // expected-note {{The value 2 is assigned to 'n'}}
}
- }
- // expected-note at -1 {{'n' is destroyed here}}
+ } // expected-note {{'n' is destroyed here}}
*p = 4;
// expected-warning at -1 {{Use of 'n' after its lifetime ended}}
// expected-note at -2 {{Use of 'n' after its lifetime ended}}
@@ -41,8 +39,8 @@ void testPathNotesWithWarnIfReached() {
{
int i = 0;
while (i < 3) {
- // expected-note at -1 3 {{Loop condition is true. Entering loop body}}
- // expected-note at -2 {{Loop condition is false. Execution continues on line 49}}
+ // expected-note at -1 3 {{Loop condition is true. Entering loop body}}
+ // expected-note at -2 {{Loop condition is false. Execution continues on line 47}}
++i;
}
}
>From 9e6109fe4bc15fdb847b7f106eab29652d5acafa Mon Sep 17 00:00:00 2001
From: benedekaibas <kaibas01 at allegheny.edu>
Date: Tue, 27 Oct 2026 23:51:28 +0100
Subject: [PATCH 5/6] Apply nits to the test file.
---
clang/test/Analysis/lifetime-end-path-notes.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang/test/Analysis/lifetime-end-path-notes.cpp b/clang/test/Analysis/lifetime-end-path-notes.cpp
index 0e0e6e7599001b..ac77804fbaeed8 100644
--- a/clang/test/Analysis/lifetime-end-path-notes.cpp
+++ b/clang/test/Analysis/lifetime-end-path-notes.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection,alpha.core.DanglingPtrDeref\
+// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection,alpha.core.DanglingPtrDeref \
// RUN: -analyzer-config cfg-lifetime=true -analyzer-output=text -verify %s
void clang_analyzer_warnIfReached();
@@ -9,9 +9,9 @@ void clang_analyzer_warnIfReached();
void testPathNotesWithLoopScopeEnd() {
int *p = nullptr;
for (int i = 0; i < 3; ++i) {
- // expected-note at -1 3 {{Loop condition is true. Entering loop body}}
- // expected-note at -2 {{The value 2 is assigned to 'i'}}
- // expected-note at -3 {{Loop condition is false. Execution continues on line 17}}
+ // expected-note at -1 3 {{Loop condition is true. Entering loop body}}
+ // expected-note at -2 {{The value 2 is assigned to 'i'}}
+ // expected-note at -3 {{Loop condition is false. Execution continues on line 17}}
p = &i; // expected-note {{Value assigned to 'p'}}
} // expected-note {{'i' is destroyed here}}
*p = 4;
>From 14f5342e2c90d9fedacb257b6c379db25fb75b46 Mon Sep 17 00:00:00 2001
From: benedekaibas <kaibas01 at allegheny.edu>
Date: Sat, 28 Nov 2026 23:58:38 +0100
Subject: [PATCH 6/6] Update the test file.
---
clang/test/Analysis/lifetime-end-path-notes.cpp | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/clang/test/Analysis/lifetime-end-path-notes.cpp b/clang/test/Analysis/lifetime-end-path-notes.cpp
index ac77804fbaeed8..d34286c43064b2 100644
--- a/clang/test/Analysis/lifetime-end-path-notes.cpp
+++ b/clang/test/Analysis/lifetime-end-path-notes.cpp
@@ -9,9 +9,9 @@ void clang_analyzer_warnIfReached();
void testPathNotesWithLoopScopeEnd() {
int *p = nullptr;
for (int i = 0; i < 3; ++i) {
- // expected-note at -1 3 {{Loop condition is true. Entering loop body}}
+ // expected-note at -1 3 {{Loop condition is true; entering loop body}}
// expected-note at -2 {{The value 2 is assigned to 'i'}}
- // expected-note at -3 {{Loop condition is false. Execution continues on line 17}}
+ // expected-note at -3 {{Loop condition is false; execution continues on line 17}}
p = &i; // expected-note {{Value assigned to 'p'}}
} // expected-note {{'i' is destroyed here}}
*p = 4;
@@ -24,8 +24,8 @@ void testPathNotesWithBlockScopeEnd() {
{
int n = 0;
while (n < 3) {
- // expected-note at -1 3 {{Loop condition is true. Entering loop body}}
- // expected-note at -2 {{Loop condition is false. Execution continues on line 33}}
+ // expected-note at -1 3 {{Loop condition is true; entering loop body}}
+ // expected-note at -2 {{Loop condition is false; execution continues on line 33}}
p = &n; // expected-note {{Value assigned to 'p'}}
++n; // expected-note {{The value 2 is assigned to 'n'}}
}
@@ -39,8 +39,8 @@ void testPathNotesWithWarnIfReached() {
{
int i = 0;
while (i < 3) {
- // expected-note at -1 3 {{Loop condition is true. Entering loop body}}
- // expected-note at -2 {{Loop condition is false. Execution continues on line 47}}
+ // expected-note at -1 3 {{Loop condition is true; entering loop body}}
+ // expected-note at -2 {{Loop condition is false; execution continues on line 47}}
++i;
}
}
More information about the cfe-commits
mailing list