From: Florian Weimer <fw@deneb.enyo.de>
To: Jason Merrill <jason@redhat.com>
Cc: Bernd Edlinger <bernd.edlinger@hotmail.de>,
"gcc-patches\@gcc.gnu.org" <gcc-patches@gcc.gnu.org>,
Jeff Law <law@redhat.com>
Subject: Re: [PATCH] Make -Wint-in-bool-context warn on suspicious shift ops
Date: Tue, 27 Sep 2016 12:58:00 -0000 [thread overview]
Message-ID: <87shslv769.fsf@mid.deneb.enyo.de> (raw)
In-Reply-To: <CADzB+2mYhkUgYHcJM9+f15psM=7GyasK5Af56A1W9cjXYFsmHQ@mail.gmail.com> (Jason Merrill's message of "Tue, 27 Sep 2016 08:42:18 -0400")
* Jason Merrill:
> On Sun, Sep 25, 2016 at 3:46 AM, Bernd Edlinger
> <bernd.edlinger@hotmail.de> wrote:
>> This patch makes -Wint-in-bool-context warn on suspicious integer left
>> shifts, when the integer is signed, which is most likely some kind of
>> programming error, for instance using "<<" instead of "<".
>>
>> The warning is motivated by the fact, that an overflow on integer shift
>> left is undefined behavior, even if gcc won't optimize the shift based
>> on the undefined behavior.
>>
>> So in absence of undefined behavior the boolean result does not depend
>> on the shift value, thus the whole shifting is pointless.
>
> It's pointless for unsigned integers, too; why not warn for them as
> well? And why not warn for 0 << 0 and 1 << 0, which are just as
> pointless?
“1 << 0“ is often used in a sequence of flag mask definitions. This
example is from <bits/termios.h>:
| /* Terminal control structure. */
| struct termios
| {
| /* Input modes. */
| tcflag_t c_iflag;
| #define IGNBRK (1 << 0) /* Ignore break condition. */
| #define BRKINT (1 << 1) /* Signal interrupt on break. */
| #define IGNPAR (1 << 2) /* Ignore characters with parity errors. */
| #define PARMRK (1 << 3) /* Mark parity and framing errors. */
“0 << 0” is used in a similar context, to create a zero constant for a
multi-bit subfield of an integer.
This example comes from GDB, in bfd/elf64-alpha.c:
| insn = INSN_ADDQ | (16 << 21) | (0 << 16) | (0 << 0);
next prev parent reply other threads:[~2016-09-27 12:50 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-09-25 9:14 Bernd Edlinger
2016-09-27 12:45 ` Jason Merrill
2016-09-27 12:58 ` Florian Weimer [this message]
2016-09-27 13:56 ` Bernd Edlinger
2016-09-27 14:34 ` Florian Weimer
2016-09-27 14:42 ` Bernd Edlinger
2016-09-27 14:51 ` Jason Merrill
2016-09-27 15:19 ` Bernd Edlinger
2016-09-28 14:44 ` Jason Merrill
2016-09-28 16:17 ` Bernd Edlinger
2016-09-29 18:10 ` Jason Merrill
2016-09-29 19:07 ` Bernd Edlinger
2016-09-29 20:08 ` Bernd Edlinger
2016-09-29 20:53 ` Jason Merrill
2016-09-30 7:05 ` Bernd Edlinger
2016-10-02 18:38 ` Jason Merrill
2016-10-08 17:40 ` Jason Merrill
2016-10-08 20:05 ` Bernd Edlinger
2016-10-09 2:42 ` Jason Merrill
2016-10-17 15:23 ` Markus Trippelsdorf
2016-10-17 16:51 ` Bernd Edlinger
2016-10-17 17:11 ` Markus Trippelsdorf
2016-10-17 17:30 ` Bernd Edlinger
2016-10-17 17:44 ` Markus Trippelsdorf
2016-10-18 17:04 ` Bernd Edlinger
2016-10-18 17:05 ` Joseph Myers
2016-10-18 18:14 ` Bernd Edlinger
2016-10-19 20:13 ` Jeff Law
2016-10-20 8:05 ` Markus Trippelsdorf
2016-10-20 14:00 ` Bernd Edlinger
2016-09-27 13:48 ` Michael Matz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87shslv769.fsf@mid.deneb.enyo.de \
--to=fw@deneb.enyo.de \
--cc=bernd.edlinger@hotmail.de \
--cc=gcc-patches@gcc.gnu.org \
--cc=jason@redhat.com \
--cc=law@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).