This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: GCC turns &~ into | due to undefined bit-shift without warning
- From: Vincent Lefevre <vincent+gcc at vinc17 dot org>
- To: Moritz Strübe <moritz dot struebe at redheads dot de>
- Cc: Jakub Jelinek <jakub at redhat dot com>, "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>, Nicolai Steinkamp <nicolai dot steinkamp at redheads dot de>
- Date: Mon, 11 Mar 2019 12:24:29 +0100
- Subject: Re: GCC turns &~ into | due to undefined bit-shift without warning
- References: <4af9e251-f4c3-a5a4-e33d-fb8750c87e36@redheads.de> <20190311091449.GB7611@tucnak> <9085342b-41a6-851c-28e3-08a40cc30103@redheads.de>
On 2019-03-11 11:06:37 +0000, Moritz Strübe wrote:
> On 11.03.2019 at 10:14 Jakub Jelinek wrote:
> > The fact that negative or >= bit precision shifts are UB is widely known,
[...]
And even in the case where the compiler maps the shift directly to
the asm shift (without optimizations), the behavior may depend on
the processor.
> Thanks for that explanation. None the less, a compile time warning
> would be nice.
It already does by default:
-Wshift-count-negative
Warn if shift count is negative. This warning is enabled
by default.
-Wshift-count-overflow
Warn if shift count >= width of type. This warning is
enabled by default.
Of course, if the compiler cannot guess that there will be such
an issue, it will not emit the warning. You certainly don't want
a warning for each non-trivial shift just because the compiler
cannot know whether the constraint on the shift count will be
satisfied.
--
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)