From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 47420 invoked by alias); 30 Apr 2015 13:17:52 -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 46893 invoked by uid 89); 30 Apr 2015 13:17:51 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00,KAM_LAZY_DOMAIN_SECURITY,SPF_HELO_PASS,T_RP_MATCHES_RCVD autolearn=no version=3.3.2 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 (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 30 Apr 2015 13:17:50 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t3UDHnos012593 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Thu, 30 Apr 2015 09:17:49 -0400 Received: from redhat.com (ovpn-204-66.brq.redhat.com [10.40.204.66]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t3UDHjZN030232 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Thu, 30 Apr 2015 09:17:48 -0400 Date: Thu, 30 Apr 2015 13:37:00 -0000 From: Marek Polacek To: GCC Patches Subject: C/C++ PATCH to fix latest -Wbool-compare extension Message-ID: <20150430131745.GH3384@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-SW-Source: 2015-04/txt/msg02036.txt.bz2 The problem here was that the -Wbool-compare warning about always false/true comparisons with 0/1 was assuming that both operands are of a boolean type. That was wrong so check for that, but don't get confused about bools promoted to int. This bug is blocking aarch64 bootstrap, so I'm taking the liberty of committing it right away. Bootstrapped/regtested on x86_64-linux, applying to trunk. 2015-04-30 Marek Polacek * c-common.c (maybe_warn_bool_compare): When comparing with 0/1, require that the non-constant be of a boolean type. * c-c++-common/Wbool-compare-3.c: New test. diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c index 7d314f8..ada8e8a 100644 --- gcc/c-family/c-common.c +++ gcc/c-family/c-common.c @@ -11924,6 +11924,17 @@ maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0, } else if (integer_zerop (cst) || integer_onep (cst)) { + /* If the non-constant operand isn't of a boolean type, we + don't want to warn here. */ + tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0; + /* Handle booleans promoted to integers. */ + if (CONVERT_EXPR_P (noncst) + && TREE_TYPE (noncst) == integer_type_node + && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE) + /* Warn. */; + else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE + && !truth_value_p (TREE_CODE (noncst))) + return; /* Do some magic to get the right diagnostics. */ bool flag = TREE_CODE (op0) == INTEGER_CST; flag = integer_zerop (cst) ? flag : !flag; diff --git gcc/testsuite/c-c++-common/Wbool-compare-3.c gcc/testsuite/c-c++-common/Wbool-compare-3.c index e69de29..bac4f47 100644 --- gcc/testsuite/c-c++-common/Wbool-compare-3.c +++ gcc/testsuite/c-c++-common/Wbool-compare-3.c @@ -0,0 +1,30 @@ +/* { dg-do compile } */ +/* { dg-options "-Wbool-compare" } */ + +#ifndef __cplusplus +# define bool _Bool +#endif + +#define A 0 +#define B 1 + +int +foo (int i, bool b) +{ + int r = 0; + + r += i <= (A || B); + r += i <= b; + r += i <= A; + r += i < (A || B); + r += i < b; + r += i < A; + r += i > (A || B); + r += i > b; + r += i > A; + r += i >= (A || B); + r += i >= b; + r += i >= A; + + return r; +} Marek