[analyzer] Do not emit redundant SymbolCasts
authorGabor Marton <gabor.marton@ericsson.com>
Fri, 17 Jun 2022 16:19:55 +0000 (18:19 +0200)
committerGabor Marton <gabor.marton@ericsson.com>
Tue, 5 Jul 2022 16:42:34 +0000 (18:42 +0200)
commit5d7fa481cf4d36e14345b1c3ebc951edda0b40b1
treec60d47b246988eec892feccfc2c4f1d7cc9a7f5d
parent7b1ff859feaaf1316932d0be113d762626009fd6
[analyzer] Do not emit redundant SymbolCasts

In `RegionStore::getBinding` we call `evalCast` unconditionally to align
the stored value's type to the one that is being queried. However, the
stored type might be the same, so we may end up having redundant
`SymbolCasts` emitted.

The solution is to check whether the `to` and `from` type are the same
in `makeNonLoc`.

Note, we can't just do type equivalence check at the beginning of `evalCast`
because when `evalCast` is called from `getBinding` then the original type
(`OriginalTy`) is not set, so one operand is missing for the comparison. In
`evalCastSubKind(nonloc::SymbolVal)` when the original type is not set,
we get the `from` type via `SymbolVal::getType()`.

Differential Revision: https://reviews.llvm.org/D128068
clang/lib/StaticAnalyzer/Core/SValBuilder.cpp
clang/test/Analysis/symbolcast-floatingpoint.cpp [new file with mode: 0644]