[clang] [analyzer] tryRearrange: don't "cancel out" symbol in BO_Add (PR #237160)

via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 30 03:13:39 PDT 2026


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 the special case when the same symbol appeared on both sides of the top-level operator (perhaps with some integer offsets), it was cancelled out... EVEN WHEN THE OPERATOR WAS ADDITION!

E.g. it was rearranging `(x + 1) + (x + 2)` to `3` and `x + x` to zero.

This change skips the "cancel out symbol" step for `BO_Add`, which ensures that e.g. `(x + 1) + (x + 2)` is rearranged to `(x + x) + 3`.

Perhaps later we should rearrange the `(x + x)` part to `x * 2`, but I don't see any concrete benefits from this.

Reported-by: Arseniy Zaostrovnykh <necto.ne@<!-- -->gmail.com>

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


2 Files Affected:

- (modified) clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp (+1-1) 
- (modified) clang/test/Analysis/svalbuilder-rearrange-comparisons.cpp (+9) 


``````````diff
diff --git a/clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp b/clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
index c89271af525b8e..881cff258c0ab2 100644
--- a/clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
+++ b/clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
@@ -346,7 +346,7 @@ static std::optional<NonLoc> tryRearrange(ProgramStateRef State,
   if (BinaryOperator::isComparisonOp(Op) && !InBounds(LInt, RInt, LSym, RSym))
     return std::nullopt;
 
-  if (LSym == RSym)
+  if (LSym == RSym && Op != BO_Add)
     return SVB
         .evalBinOpNN(State, Op, nonloc::ConcreteInt(BV.getValue(LInt)),
                      nonloc::ConcreteInt(BV.getValue(RInt)), ResultTy)
diff --git a/clang/test/Analysis/svalbuilder-rearrange-comparisons.cpp b/clang/test/Analysis/svalbuilder-rearrange-comparisons.cpp
index eca6ab0c71befe..95cdefa4b49df6 100644
--- a/clang/test/Analysis/svalbuilder-rearrange-comparisons.cpp
+++ b/clang/test/Analysis/svalbuilder-rearrange-comparisons.cpp
@@ -1026,3 +1026,12 @@ void unsigned_concrete_int_no_crash(void) {
   clang_analyzer_express(y); // expected-warning {{$y}}
   clang_analyzer_express(x == y); // expected-warning {{$x + 1U == $y + 1U}}
 }
+
+void add_shifted_same_symbol(void) {
+  int x = f();
+  clang_analyzer_denote(x, "$x");
+  clang_analyzer_express((x + 1) + (x + 2)); // expected-warning{{$x + $x + 3}}
+  clang_analyzer_express((x + 1) + x); // expected-warning{{$x + $x + 1}}
+  clang_analyzer_express((x - 1) + (x + 1)); // expected-warning{{$x + $x}}
+  clang_analyzer_express(x + x); // expected-warning{{$x + $x}}
+}

``````````

</details>


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


More information about the cfe-commits mailing list