From: Jeff Law <law@redhat.com>
To: Bernd Edlinger <bernd.edlinger@hotmail.de>,
"gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>,
Joseph Myers <joseph@codesourcery.com>,
Jason Merrill <jason@redhat.com>
Subject: Re: [PATCH] Add a warning for suspicious use of conditional expressions in boolean context
Date: Mon, 12 Sep 2016 19:40:00 -0000 [thread overview]
Message-ID: <4fd68972-b48b-560a-b8e2-ae7d607b9b87@redhat.com> (raw)
In-Reply-To: <AM4PR0701MB21624AB3DECA1269E4BBB475E4E50@AM4PR0701MB2162.eurprd07.prod.outlook.com>
On 09/02/2016 12:53 PM, Bernd Edlinger wrote:
> Hi!
>
> As reported in PR77434 and PR77421 there should be a warning for
> suspicious uses of conditional expressions with non-boolean arguments.
>
> This warning triggers on conditional expressions in boolean context,
> when both possible results are non-zero integer constants, so that
> the resulting truth value does in fact not depend on the condition
> itself. Thus something like "if (a == b ? 1 : 2)" is always bogus,
> and was most likely meant to be "if (a == (b ? 1 : 2))".
>
>
> Boot-strap and reg-testing on x86_64-pc-linux-gnu without regressions.
> Is it OK for trunk.
>
>
> Thanks
> Bernd.
>
>
> changelog-pr77434.txt
>
>
> gcc:
> 2016-09-02 Bernd Edlinger <bernd.edlinger@hotmail.de>
>
> PR c++/77434
> * doc/invoke.texi: Document -Wcond-in-bool-context.
>
> PR middle-end/77421
> * dwarf2out.c (output_loc_operands): Fix assertion.
>
> c-family:
> 2016-09-02 Bernd Edlinger <bernd.edlinger@hotmail.de>
>
> PR c++/77434
> * c.opt (Wcond-in-bool-context): New warning.
> * c-common.c (c_common_truthvalue_conversion): Warn on integer
> constants in boolean context.
>
> testsuite:
> 2016-09-02 Bernd Edlinger <bernd.edlinger@hotmail.de>
>
> PR c++/77434
> * c-c++-common/Wcond-in-bool-context.c: New test.
For some reason the non-symmerty of the changes to
c_common_truthvalue_conversion caused me to have to think far more about
this than I probably should have.
Couldn't we have a new function
integer_zerop_or_onep
Then use
&& (!integer_zerop_or_onep (TREE_OPERAND (expr, 1))
|| !integer_zerop_or_onep (TREE_OPERAND (expr, 2)))
Ie, if they're both constants and either is not [0,1], then we warn.
With that cleanup, this is OK.
jeff
next prev parent reply other threads:[~2016-09-12 19:30 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-09-02 18:53 Bernd Edlinger
2016-09-04 8:45 ` [PATCHv2] " Bernd Edlinger
2016-09-05 11:41 ` Joseph Myers
2016-09-05 14:59 ` Bernd Edlinger
2016-09-05 16:57 ` Joseph Myers
[not found] ` <CAPWdEev7VW5LT47iPh-0EgAJz5ELEnoZ_snLtg-F5ZR+etLimg@mail.gmail.com>
2016-09-05 20:03 ` Bernd Edlinger
2016-09-12 19:40 ` Jeff Law [this message]
2016-09-12 20:02 ` [PATCH] " Bernd Edlinger
2016-09-12 20:18 ` Jeff Law
2016-09-12 21:28 ` Bernd Edlinger
2016-09-14 16:14 ` [PATCH, updated] " Bernd Edlinger
2016-09-14 16:50 ` Jason Merrill
2016-09-14 17:41 ` Bernd Edlinger
2016-09-14 19:04 ` Jason Merrill
[not found] ` <AM4PR0701MB2162B5B8246F8A10B4B6E42CE4F10@AM4PR0701MB2162.eurprd07.prod.outlook.com>
2016-09-14 20:17 ` [PATCHv3] " Bernd Edlinger
2016-09-15 15:52 ` [PATCH, updated] " Jeff Law
2016-09-15 16:20 ` Bernd Edlinger
2016-09-15 16:36 ` Jeff Law
2016-09-15 16:51 ` Bernd Edlinger
2016-09-15 19:21 ` Joseph Myers
2016-09-15 20:34 ` Jason Merrill
2016-09-15 20:45 ` Bernd Edlinger
2016-09-14 17:54 ` Steve Kargl
2016-09-14 17:56 ` Bernd Edlinger
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=4fd68972-b48b-560a-b8e2-ae7d607b9b87@redhat.com \
--to=law@redhat.com \
--cc=bernd.edlinger@hotmail.de \
--cc=gcc-patches@gcc.gnu.org \
--cc=jason@redhat.com \
--cc=joseph@codesourcery.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).