[clang] [NFC][analyzer] Simplify CheckerContext::generateNonFatalErrorNode (PR #241346)
DonĂ¡t Nagy via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 11 06:55:29 PST 2026
https://github.com/NagyDonat created https://github.com/llvm/llvm-project/pull/241346
The three-parameter form of `CheckerContext::generateNonFatalErrorNode` had an early return statement which wasn't activated in any of the 6 call sites of this method, because each caller always passed a non-null state (the state of an existing exploded node).
By the way, this early return was also logically incorrect, so a downstream checker that relied on it would be already very buggy.
I also refactored four of the six call sites of this overload to use the simpler two-parameter overload (without passing any argument, using the defaults).
>From 5cd6c6b8af39ae1d9578c2547a2df6cb477a2fe7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <donat.nagy at ericsson.com>
Date: Wed, 11 Nov 2026 14:20:03 +0100
Subject: [PATCH 1/4] Remove unused early return from generateNonFatalErrorNode
The three-parameter form of `CheckerContext::generateNonFatalErrorNode`
had an early return statement which wasn't activated in any of the 6
call sites of this method, because each caller always passed a non-null
state (the state of an existing exploded node).
By the way, this early return was also logically incorrect and
counter-intuitive, so a downstream checker that relied on it would be
already very buggy.
---
.../Core/PathSensitive/CheckerContext.h | 12 +++---------
1 file changed, 3 insertions(+), 9 deletions(-)
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
index eaf62e8c8fbb3b..8d89df926a7471 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
@@ -273,11 +273,9 @@ class CheckerContext {
/// Creates a new ExplodedNode and adds it to the ExplodedGraph as a child of
/// the parameter \p Pred. Allows checkers to make a chain of nodes.
- /// The difference between this and 'addTransition()' should be that this
- /// always attempts to create a new node (even if there is no state change
- /// and no explicitly specified tag). Currently this logic is incorrect in
- /// addTransition and the difference is that this function lacks one of the
- /// two problematic early return statements.
+ /// The difference between this and 'addTransition()' is that this always
+ /// attempts to create a new node (even if there is no state change and no
+ /// explicitly specified tag).
/// Can return nullptr if the node creation fails (when it would recreate
/// an existing node or when the state is posteriorly overconstrained).
///
@@ -292,10 +290,6 @@ class CheckerContext {
generateNonFatalErrorNode(ProgramStateRef State,
ExplodedNode *Pred,
const ProgramPointTag *Tag = nullptr) {
- // FIXME: This can "jump back" to an ancestor of 'Pred'. We should return
- // either Pred or nullptr in this case.
- if (!State)
- return getPredecessor();
return addTransitionImpl(State, false, Pred, Tag);
}
>From 6ffb1e62fc9faffe91feb5f40851d2b49592d8af Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <donat.nagy at ericsson.com>
Date: Wed, 11 Nov 2026 14:28:01 +0100
Subject: [PATCH 2/4] Remove superfluous args of generateNonFatalErrorNode
These two checkers called the three-parameter overload of
`generateNonFatalErrorNode`, but they always passed `C.getState()` and
`C.getPredecessor()` (where `C` is the checker context; one checker
saved the state in a local before using it).
This is completely equivalent to calling `generateNonFatalErrorNode`
(more precisely its two-parameter overload) without passing any
arguments.
---
clang/lib/StaticAnalyzer/Checkers/ChrootChecker.cpp | 3 +--
clang/lib/StaticAnalyzer/Checkers/UseAfterLifetimeEnd.cpp | 3 +--
2 files changed, 2 insertions(+), 4 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Checkers/ChrootChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/ChrootChecker.cpp
index 61d35a0b12ec47..7b95eac102f3f3 100644
--- a/clang/lib/StaticAnalyzer/Checkers/ChrootChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/ChrootChecker.cpp
@@ -163,8 +163,7 @@ void ChrootChecker::checkPreCall(const CallEvent &Call,
return;
// Generate bug report.
- ExplodedNode *Err =
- C.generateNonFatalErrorNode(C.getState(), C.getPredecessor());
+ ExplodedNode *Err = C.generateNonFatalErrorNode();
if (!Err)
return;
diff --git a/clang/lib/StaticAnalyzer/Checkers/UseAfterLifetimeEnd.cpp b/clang/lib/StaticAnalyzer/Checkers/UseAfterLifetimeEnd.cpp
index f10da977172c07..c10107de66e68b 100644
--- a/clang/lib/StaticAnalyzer/Checkers/UseAfterLifetimeEnd.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/UseAfterLifetimeEnd.cpp
@@ -90,8 +90,7 @@ void UseAfterLifetimeEnd::checkEndFunction(const ReturnStmt *RS,
if (RetValRegion.empty())
return;
- if (ExplodedNode *N =
- C.generateNonFatalErrorNode(State, C.getPredecessor())) {
+ if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
for (const MemRegion *R : RetValRegion)
reportDanglingSource(R, RetVal, N, C);
}
>From 6fb3d0ccc4e1aaf1ec6a8f52494b5d52d5360708 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <donat.nagy at ericsson.com>
Date: Wed, 11 Nov 2026 14:39:30 +0100
Subject: [PATCH 3/4] Refactor leak node creation in StreamChecker
In the old code `reportLeaks` had an `ExplodedNode *` parameter and the
only call site always passed `C.getPredecessor()` -- and passing
anything else would be very fishy because `reportLeaks` always used
`C.getState()` as the state of the error node.
This change removes this pointless parameter and lets `reportLeaks` call
`C.getPredecessor()` directly.
Moreover, the check for `LeakedSyms.empty()` is moved into `reportLeaks`
to unite the logic in a single place (when I was reading `reportLeaks` I
was surprised to see that it unconditionally creates a node without
checking `LeakedSyms`).
---
.../StaticAnalyzer/Checkers/StreamChecker.cpp | 20 +++++++++----------
1 file changed, 9 insertions(+), 11 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
index 4ccdaf6c502132..9203500f193f27 100644
--- a/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
@@ -581,10 +581,10 @@ class StreamChecker : public Checker<check::PreCall, eval::Call,
ProgramStateRef State) const;
/// Emit resource leak warnings for the given symbols.
- /// Createn a non-fatal error node for these, and returns it (if any warnings
- /// were generated). Return value is non-null.
+ /// Returns either the non-fatal error node (where the warnings are emitted),
+ /// or 'C.getPredecessor()' when 'LeakedSyms' is empty or node creation fails.
ExplodedNode *reportLeaks(const SmallVector<SymbolRef, 2> &LeakedSyms,
- CheckerContext &C, ExplodedNode *Pred) const;
+ CheckerContext &C) const;
/// Find the description data of the function called by a call event.
/// Returns nullptr if no function is recognized.
@@ -2014,10 +2014,12 @@ void StreamChecker::reportFEofWarning(SymbolRef StreamSym, CheckerContext &C,
ExplodedNode *
StreamChecker::reportLeaks(const SmallVector<SymbolRef, 2> &LeakedSyms,
- CheckerContext &C, ExplodedNode *Pred) const {
- ExplodedNode *Err = C.generateNonFatalErrorNode(C.getState(), Pred);
+ CheckerContext &C) const {
+ if (LeakedSyms.empty())
+ return C.getPredecessor();
+ ExplodedNode *Err = C.generateNonFatalErrorNode();
if (!Err)
- return Pred;
+ return C.getPredecessor();
for (SymbolRef LeakSym : LeakedSyms) {
// Resource leaks can result in multiple warning that describe the same kind
@@ -2069,11 +2071,7 @@ void StreamChecker::checkDeadSymbols(SymbolReaper &SymReaper,
State = State->remove<StreamMap>(Sym);
}
- ExplodedNode *N = C.getPredecessor();
- if (!LeakedSyms.empty())
- N = reportLeaks(LeakedSyms, C, N);
-
- C.addTransition(State, N);
+ C.addTransition(State, reportLeaks(LeakedSyms, C));
}
ProgramStateRef StreamChecker::checkPointerEscape(
>From e835bc23c778d9e7d03247b518ddf406a72c163d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <donat.nagy at ericsson.com>
Date: Wed, 11 Nov 2026 14:51:12 +0100
Subject: [PATCH 4/4] Refactor leak node creation in FuchsiaHandleChecker
In the old code `reportLeaks` had an `ExplodedNode *` parameter and the
only call site always passed `C.getPredecessor()` -- and passing
anything else would be very fishy because `reportLeaks` always used
`C.getState()` as the state of the error node.
This change removes this pointless parameter and lets `reportLeaks` call
`C.getPredecessor()` directly.
Moreover, the check for `LeakedSyms.empty()` is moved into `reportLeaks`
to unite the logic in a single place.
This is very similar to the logic in StreamChecker, but note that this
`reportLeaks` can return `nullptr` (when `generateNonFatalErrorNode`
fails) which is not an error because `addTransition` can use
`C.getPredecessor()` when its node parameter is null. This behavior is
left unchanged.
---
.../Checkers/FuchsiaHandleChecker.cpp | 14 ++++++--------
1 file changed, 6 insertions(+), 8 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Checkers/FuchsiaHandleChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/FuchsiaHandleChecker.cpp
index 705598d5bc866a..94b1e9e49b27ae 100644
--- a/clang/lib/StaticAnalyzer/Checkers/FuchsiaHandleChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/FuchsiaHandleChecker.cpp
@@ -208,7 +208,7 @@ class FuchsiaHandleChecker
PointerEscapeKind Kind) const;
ExplodedNode *reportLeaks(ArrayRef<SymbolRef> LeakedHandles,
- CheckerContext &C, ExplodedNode *Pred) const;
+ CheckerContext &C) const;
void reportDoubleRelease(SymbolRef HandleSym, const SourceRange &Range,
CheckerContext &C) const;
@@ -519,11 +519,7 @@ void FuchsiaHandleChecker::checkDeadSymbols(SymbolReaper &SymReaper,
State = State->remove<HStateMap>(CurItem.first);
}
- ExplodedNode *N = C.getPredecessor();
- if (!LeakedSyms.empty())
- N = reportLeaks(LeakedSyms, C, N);
-
- C.addTransition(State, N);
+ C.addTransition(State, reportLeaks(LeakedSyms, C));
}
// Acquiring a handle is not always successful. In Fuchsia most functions
@@ -609,8 +605,10 @@ ProgramStateRef FuchsiaHandleChecker::checkPointerEscape(
ExplodedNode *
FuchsiaHandleChecker::reportLeaks(ArrayRef<SymbolRef> LeakedHandles,
- CheckerContext &C, ExplodedNode *Pred) const {
- ExplodedNode *ErrNode = C.generateNonFatalErrorNode(C.getState(), Pred);
+ CheckerContext &C) const {
+ if (LeakedHandles.empty())
+ return C.getPredecessor();
+ ExplodedNode *ErrNode = C.generateNonFatalErrorNode();
for (SymbolRef LeakedHandle : LeakedHandles) {
reportBug(LeakedHandle, ErrNode, C, nullptr, LeakBugType,
"Potential leak of handle");
More information about the cfe-commits
mailing list