Open batneil opened 2 years ago
@llvm/issue-subscribers-clang-static-analyzer
I believe, this false report is because we do not handle integer casts properly. We are in the middle of implementing them. However, that seems to be a nontrivial problem that will take some time to tackle. We have high hopes for https://reviews.llvm.org/D103096 (@ASDenysPetrov).
This is the same issue that we have with (@vabridgers):
1
2 #include <assert.h>
3 void bar(short k) {
4 ++k; // k1 = k0 + 1
5 assert(k == 1); // k1 == 1 --> k0 == 0
6 (long)k << 16; // k0 + 1 << 16
7 }
$ clang --analyze -Xclang -analyzer-checker=debug.ExprInspection go.c
go.c:6:11: warning: The result of the left shift is undefined due to shifting '1' by '16', which is unrepresentable in the unsigned version of the return type 'long' [core.UndefinedBinaryOperatorResult]
(long)k << 16; // k0 + 1 << 16
~~~~~~~ ^
1 warning generated.
We are also seeing this:
error: The result of the left shift is undefined due to shifting '0' by '4', which is unrepresentable in the unsigned version of the return type 'uint64' [clang-analyzer-core.UndefinedBinaryOperatorResult,-warnings-as-errors]
With clang-tidy 14.0.5, compiling this test program produces a warning for shift of 0:
Compiling with
clang-tidy --checks=-*,clang-analyzer-core.UndefinedBinaryOperatorResult bug.cpp -- -std=c++17
results in:Perhaps I'm missing something, but the shift seems valid. The same code doesn't produce any warnings in version 13.0. Also, for what it's worth if I change the code so that bitf_mode comes from an extern bool rather than the struct member, I don't see the warning anymore.