[clang] 00758de - [analyzer] tryRearrange: don't "cancel out" symbol in BO_Add (#237160)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 4 05:09:37 PST 2026
Author: DonĂ¡t Nagy
Date: 2026-11-04T14:09:32+01:00
New Revision: 00758deecd99b4f8296f19b4a741a9b67933228b
URL: https://github.com/llvm/llvm-project/commit/00758deecd99b4f8296f19b4a741a9b67933228b
DIFF: https://github.com/llvm/llvm-project/commit/00758deecd99b4f8296f19b4a741a9b67933228b.diff
LOG: [analyzer] tryRearrange: don't "cancel out" symbol in BO_Add (#237160)
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`.
Cancelling symbols is valid for all other supported operators
(subtraction and comparisons).
This bug was introduced in 2018 by
2bbccca9f75b6bce08d77cf19abfb206d0c3bc2e, but after spending 3 months in
trunk, `tryRearrange` was disabled by default for severe unrelated
(performance) issues. After that
`aggressive-binary-operation-simplification` was just one of the many
basically unsupported analyzer options, so it's not a surprise that
nobody looked at it and noticed this bug.
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 at gmail.com>
Added:
Modified:
clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
clang/test/Analysis/svalbuilder-rearrange-comparisons.cpp
Removed:
################################################################################
diff --git a/clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp b/clang/lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
index c89271af525b8..881cff258c0ab 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 eca6ab0c71bef..95cdefa4b49df 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}}
+}
More information about the cfe-commits
mailing list