[clang] [analyzer] Put PostVisit after EagerlyAssume (PR #239123)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 4 23:14:47 PST 2026


=?utf-8?q?Donát?= Nagy <donat.nagy at ericsson.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/239123 at github.com>


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-static-analyzer-1

Author: Donát Nagy (NagyDonat)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/239123.diff


2 Files Affected:

- (modified) clang/lib/StaticAnalyzer/Core/ExprEngine.cpp (+2-16) 
- (modified) clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp (+16-2) 


``````````diff
diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
index 1fe3dc0986b88..133a41223072b 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 d89c80ca01754..21a600871cd65 100644
--- a/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
@@ -160,8 +160,16 @@ void ExprEngine::VisitBinaryOperator(const BinaryOperator* B,
     }
   }
 
+  ExplodedNodeSet AssumedSet;
+  if (AMgr.options.ShouldEagerlyAssume &&
+      (B->isRelationalOp() || B->isEqualityOp())) {
+    evalEagerlyAssumeBifurcation(AssumedSet, Tmp2, B);
+  } else {
+    AssumedSet.insert(Tmp2);
+  }
   // FIXME: postvisits eventually go in ::Visit()
-  getCheckerManager().runCheckersForPostStmt(Dst, Tmp2, B, *this);
+  getCheckerManager().runCheckersForPostStmt(Dst, AssumedSet, B, *this);
+
 }
 
 void ExprEngine::VisitBlockExpr(const BlockExpr *BE, ExplodedNode *Pred,
@@ -969,7 +977,13 @@ void ExprEngine::VisitUnaryOperator(const UnaryOperator* U, ExplodedNode *Pred,
     }
   }
 
-  getCheckerManager().runCheckersForPostStmt(Dst, EvalSet, U, *this);
+  ExplodedNodeSet AssumedSet;
+  if (AMgr.options.ShouldEagerlyAssume && (U->getOpcode() == UO_LNot)) {
+    evalEagerlyAssumeBifurcation(AssumedSet, EvalSet, U);
+  } else {
+    AssumedSet.insert(EvalSet);
+  }
+  getCheckerManager().runCheckersForPostStmt(Dst, AssumedSet, U, *this);
 }
 
 void ExprEngine::VisitPseudoObjectExpr(const PseudoObjectExpr *PE,

``````````

</details>


https://github.com/llvm/llvm-project/pull/239123


More information about the cfe-commits mailing list