Bug ID: 88401
Summary: -Wshift-overflow only works for const variables
Assignee: unassigned at gcc dot gnu.org
Reporter: skvadrik at gmail dot com
Target Milestone: ---
Consider this program (1.cc):
void foo ()
int32_t x = -32768;
x << 31;
const int32_t y = -32768;
y << 31;
GCC detects shift overflow on y, but not on x:
$ g++ -c 1.cc -Wshift-overflow=2 --std=c++11 -O3
1.cc: In function ‘void foo()’:
1.cc:9:5: warning: result of ‘(-32768 << 31)’ requires 47 bits to represent,
but ‘int’ only has 32 bits [-Wshift-overflow=]
y << 31;
It seems that very basic constant propagation would deduce x = -32768.
--- Comment #2 from Ulya <skvadrik at gmail dot com> ---
(In reply to Jakub Jelinek from comment #1)
> It is a front-end warning, so there is no constant propagation possible.
> You can use -fsanitize=shift to detect this stuff at runtime.
Ok, understood. Maybe someday it can be moved to middle-end.
--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
That is not that easy, because what is considered invalid heavily depends on
the FE (and standard version), e.g. the above is completely valid in C++20.
Furthermore, warning too late in the middle-end could very well mean warning
about stuff that is propagated into statements after jump threading etc. and
that are dead and not something user actually wrote.
As I said, if you want accurate diagnostics, use the runtime one.