[clang] [analyzer] Put PostVisit after EagerlyAssume (PR #239123)
DonĂ¡t Nagy via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 4 23:14:10 PST 2026
https://github.com/NagyDonat created https://github.com/llvm/llvm-project/pull/239123
In general, `PostVisit` is the last thing we do with a statement; `EagerlyAssume` has no good reason to run after it.
After this `PostStmt<UnaryOperator>` and `PostStmt<BinaryOperator>` callbacks will be able to see the effects of `EagerlyAssume`, which should be more useful than the status quo.
There were only three checkers with `PostStmt` callbacks for unary or binary operators:
- `ArrayBoundChecker.cpp` is unaffected, its `UnaryOperator` callback only cares for `UO_Deref` and ignores `UO_LNot`.
- In `IteratorModeling.cpp` the `UnaryOperator` callback is unaffected (only cares for increment and decrement), and the `BinaryOperator` callback is perturbed, but it is prepared to see either concrete or symbolic results from the equality checks, so should be fine.
- In `UndefResultChecker` the `BinaryOperator` callback is uanffected because it only cares for operator expressions with Undefined result, and those are not affected by the EagerlyAssume logic.
EagerlyAssume had a defensive check for the presence of a `PostStmt` node for the same expression, but evaluation of logical negation or comparison operators always creates a `PostStmt` node (which before this commit could be followed by `PostStmt` nodes added by checkers), so I think this defensive check was never relevant. (I'll try to remove it in a follow-up commit.)
After this the Pre/Post visit of unary operators can be handled in a regular way, but I'm postponing that for a follow-up change, because it will let us unindent the whole body of `VisitUnaryOperator`, and I don't want to touch so many lines in a non-NFC commit.
Pre/Post visit of binary operators is still irregular after this because logical operators are not Pre/Post visited at all.
>From 10e1955f47acc8b305eb1f56349bffb6e71434f2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <donat.nagy at ericsson.com>
Date: Thu, 5 Nov 2026 07:03:32 +0100
Subject: [PATCH 1/2] [NFC] Invoke EagerlyAssume from VisitXXXOperator
Previously `evalEagerlyAssumeBifurcation` was invoked by
`ExprEngine::Visit` immediately after certain `VisitUnaryOperator` and
`VisitBinaryOperator` calls (the ones where the operarator is a
comparison operator or a logical negation).
This commit replaces this with equivalent logic that calls it at the
very end of those `VisitUnaryOperator` or `VisitBinaryOperator` calls.
This prepares the ground for swapping the order of the `EagerlyAssume`
and the `PostVisit` steps.
---
clang/lib/StaticAnalyzer/Core/ExprEngine.cpp | 18 ++---------------
clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp | 20 ++++++++++++++++---
2 files changed, 19 insertions(+), 19 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
index 1fe3dc0986b886..133a41223072b4 100644
--- a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
@@ -2195,14 +2195,7 @@ void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred,
break;
}
- if (AMgr.options.ShouldEagerlyAssume &&
- (B->isRelationalOp() || B->isEqualityOp())) {
- ExplodedNodeSet Tmp;
- VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
- evalEagerlyAssumeBifurcation(Dst, Tmp, cast<Expr>(S));
- }
- else
- VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
+ VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
break;
}
@@ -2346,14 +2339,7 @@ void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred,
break;
case Stmt::UnaryOperatorClass: {
- const auto *U = cast<UnaryOperator>(S);
- if (AMgr.options.ShouldEagerlyAssume && (U->getOpcode() == UO_LNot)) {
- ExplodedNodeSet Tmp;
- VisitUnaryOperator(U, Pred, Tmp);
- evalEagerlyAssumeBifurcation(Dst, Tmp, U);
- }
- else
- VisitUnaryOperator(U, Pred, Dst);
+ VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst);
break;
}
diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
index d89c80ca01754f..ebd6f190d66c32 100644
--- a/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
@@ -159,9 +159,16 @@ void ExprEngine::VisitBinaryOperator(const BinaryOperator* B,
evalStore(Tmp2, B, LHS, N, State, LeftV, StoredInLeftV);
}
}
-
+ ExplodedNodeSet PostVisitedSet;
// FIXME: postvisits eventually go in ::Visit()
- getCheckerManager().runCheckersForPostStmt(Dst, Tmp2, B, *this);
+ getCheckerManager().runCheckersForPostStmt(PostVisitedSet, Tmp2, B, *this);
+
+ if (AMgr.options.ShouldEagerlyAssume &&
+ (B->isRelationalOp() || B->isEqualityOp())) {
+ evalEagerlyAssumeBifurcation(Dst, PostVisitedSet, B);
+ } else {
+ Dst.insert(PostVisitedSet);
+ }
}
void ExprEngine::VisitBlockExpr(const BlockExpr *BE, ExplodedNode *Pred,
@@ -969,7 +976,14 @@ void ExprEngine::VisitUnaryOperator(const UnaryOperator* U, ExplodedNode *Pred,
}
}
- getCheckerManager().runCheckersForPostStmt(Dst, EvalSet, U, *this);
+ ExplodedNodeSet PostVisitedSet;
+ getCheckerManager().runCheckersForPostStmt(PostVisitedSet, EvalSet, U, *this);
+
+ if (AMgr.options.ShouldEagerlyAssume && (U->getOpcode() == UO_LNot)) {
+ evalEagerlyAssumeBifurcation(Dst, PostVisitedSet, U);
+ } else {
+ Dst.insert(PostVisitedSet);
+ }
}
void ExprEngine::VisitPseudoObjectExpr(const PseudoObjectExpr *PE,
>From 6b07e401693130896e1eb84be9ea9bdc5da092e7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <donat.nagy at ericsson.com>
Date: Thu, 5 Nov 2026 07:35:06 +0100
Subject: [PATCH 2/2] Put PostVisit after EagerlyAssume
In general, PostVisit is the last thing we do with a statement;
EagerlyAssume has no reason to run after it.
After this `PostStmt<UnaryOperator>` and `PostStmt<BinaryOperator>`
callbacks will be able to see the effects of `EagerlyAssume`, which
should be more useful than the status quo.
There were only three checkers with `PostStmt` callbacks for unary or
binary operators:
- `ArrayBoundChecker.cpp` is unaffected, its `UnaryOperator` callback
only cares for `UO_Deref` and ignores `UO_LNot`.
- In `IteratorModeling.cpp` the `UnaryOperator` callback is unaffected
(only cares for increment and decrement), and the `BinaryOperator`
callback is perturbed, but it is prepared to see either concrete or
symbolic results from the equality checks, so should be fine.
- In `UndefResultChecker` the `BinaryOperator` callback is uanffected
because it only cares for operator expressions with Undefined result,
and those are not affected by the EagerlyAssume logic.
EagerlyAssume had a defensive check for the presence of a `PostStmt`
node for the same expression, but evaluation of logical negation or
comparison operators always creates a `PostStmt` node (which before this
commit could be followed by `PostStmt` nodes added by checkers), so I
think this check is never relevant.
---
clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp | 20 +++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
index ebd6f190d66c32..21a600871cd65b 100644
--- a/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
@@ -159,16 +159,17 @@ void ExprEngine::VisitBinaryOperator(const BinaryOperator* B,
evalStore(Tmp2, B, LHS, N, State, LeftV, StoredInLeftV);
}
}
- ExplodedNodeSet PostVisitedSet;
- // FIXME: postvisits eventually go in ::Visit()
- getCheckerManager().runCheckersForPostStmt(PostVisitedSet, Tmp2, B, *this);
+ ExplodedNodeSet AssumedSet;
if (AMgr.options.ShouldEagerlyAssume &&
(B->isRelationalOp() || B->isEqualityOp())) {
- evalEagerlyAssumeBifurcation(Dst, PostVisitedSet, B);
+ evalEagerlyAssumeBifurcation(AssumedSet, Tmp2, B);
} else {
- Dst.insert(PostVisitedSet);
+ AssumedSet.insert(Tmp2);
}
+ // FIXME: postvisits eventually go in ::Visit()
+ getCheckerManager().runCheckersForPostStmt(Dst, AssumedSet, B, *this);
+
}
void ExprEngine::VisitBlockExpr(const BlockExpr *BE, ExplodedNode *Pred,
@@ -976,14 +977,13 @@ void ExprEngine::VisitUnaryOperator(const UnaryOperator* U, ExplodedNode *Pred,
}
}
- ExplodedNodeSet PostVisitedSet;
- getCheckerManager().runCheckersForPostStmt(PostVisitedSet, EvalSet, U, *this);
-
+ ExplodedNodeSet AssumedSet;
if (AMgr.options.ShouldEagerlyAssume && (U->getOpcode() == UO_LNot)) {
- evalEagerlyAssumeBifurcation(Dst, PostVisitedSet, U);
+ evalEagerlyAssumeBifurcation(AssumedSet, EvalSet, U);
} else {
- Dst.insert(PostVisitedSet);
+ AssumedSet.insert(EvalSet);
}
+ getCheckerManager().runCheckersForPostStmt(Dst, AssumedSet, U, *this);
}
void ExprEngine::VisitPseudoObjectExpr(const PseudoObjectExpr *PE,
More information about the cfe-commits
mailing list