[clang] c6b5a98 - [NFC][analyzer] Simplify CheckerContext::generateNonFatalErrorNode (#241346)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 12 01:24:56 PST 2026
Author: DonĂ¡t Nagy
Date: 2026-11-12T10:24:52+01:00
New Revision: c6b5a98c107ff7d69728cf706338c74c5f91bcba
URL: https://github.com/llvm/llvm-project/commit/c6b5a98c107ff7d69728cf706338c74c5f91bcba
DIFF: https://github.com/llvm/llvm-project/commit/c6b5a98c107ff7d69728cf706338c74c5f91bcba.diff
LOG: [NFC][analyzer] Simplify CheckerContext::generateNonFatalErrorNode (#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).
Added:
Modified:
clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
clang/lib/StaticAnalyzer/Checkers/ChrootChecker.cpp
clang/lib/StaticAnalyzer/Checkers/FuchsiaHandleChecker.cpp
clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
clang/lib/StaticAnalyzer/Checkers/UseAfterLifetimeEnd.cpp
Removed:
################################################################################
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
diff erence 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
diff erence is that this function lacks one of the
- /// two problematic early return statements.
+ /// The
diff erence 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);
}
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/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");
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(
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);
}
More information about the cfe-commits
mailing list