From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 31929 invoked by alias); 12 Sep 2016 19:30:41 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 31910 invoked by uid 89); 12 Sep 2016 19:30:40 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=dwarf2out.c, dwarf2outc, UD:dwarf2out.c, 0,1 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 12 Sep 2016 19:30:30 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 53FBD80F7C; Mon, 12 Sep 2016 19:30:27 +0000 (UTC) Received: from localhost.localdomain (ovpn-116-111.phx2.redhat.com [10.3.116.111]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u8CJUQl8006878; Mon, 12 Sep 2016 15:30:26 -0400 Subject: Re: [PATCH] Add a warning for suspicious use of conditional expressions in boolean context To: Bernd Edlinger , "gcc-patches@gcc.gnu.org" , Joseph Myers , Jason Merrill References: From: Jeff Law Message-ID: <4fd68972-b48b-560a-b8e2-ae7d607b9b87@redhat.com> Date: Mon, 12 Sep 2016 19:40:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2016-09/txt/msg00659.txt.bz2 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 > > 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 > > 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 > > 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