From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 35783 invoked by alias); 15 Mar 2016 12:09:25 -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 35768 invoked by uid 89); 15 Mar 2016 12:09:25 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=Hx-languages-length:3629 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; Tue, 15 Mar 2016 12:09:23 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id 16E591E32 for ; Tue, 15 Mar 2016 12:09:22 +0000 (UTC) Received: from redhat.com (ovpn-204-101.brq.redhat.com [10.40.204.101]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u2FC9IGK002044 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Tue, 15 Mar 2016 08:09:20 -0400 Date: Tue, 15 Mar 2016 12:09:00 -0000 From: Marek Polacek To: Jakub Jelinek Cc: GCC Patches , Jason Merrill Subject: Re: C++ PATCH to fix missing warning (PR c++/70194) Message-ID: <20160315120918.GD10006@redhat.com> References: <20160315104120.GC10006@redhat.com> <20160315105618.GT3017@tucnak.redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20160315105618.GT3017@tucnak.redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-SW-Source: 2016-03/txt/msg00829.txt.bz2 On Tue, Mar 15, 2016 at 11:56:18AM +0100, Jakub Jelinek wrote: > From compile time perspective, I wonder if it wouldn't be better to do > the cheap tests early, like: > if (warn_address > && (complain & tf_warning) > && c_inhibit_evaluation_warnings == 0 > && !TREE_NO_WARNING (op0)) > { > tree cop0 = fold_non_dependent_expr (op0); > > if (TREE_CODE (cop0) == ADDR_EXPR > && decl_with_nonnull_addr_p (TREE_OPERAND (cop0, 0)) > && !TREE_NO_WARNING (cop0)) > warning (OPT_waddress, "the address of %qD will never be NULL", > TREE_OPERAND (cop0, 0)); > } > thus perform fold_non_dependent_expr only if it is needed. Ok, makes sense. > Furthermore, I wonder if it isn't preferrable to %qD the non-folded > expression (if it is ADDR_EXPR, that is), so perhaps: > TREE_OPERAND (TREE_CODE (op0) == ADDR_EXPR ? op0 : cop0, 0) > ? I tried this before but it gave the same output as with what I have now, so I left this unchanged in this version... Thanks. Bootstrapped/regtested on x86_64-linux, ok for trunk? 2016-03-15 Marek Polacek PR c++/70194 * typeck.c (cp_build_binary_op): Call fold_non_dependent_expr before warning about an address not being null. Check cheap stuff first. * g++.dg/warn/constexpr-70194.C: New test. diff --git gcc/cp/typeck.c gcc/cp/typeck.c index 20f0afc..5069e88 100644 --- gcc/cp/typeck.c +++ gcc/cp/typeck.c @@ -4520,14 +4520,18 @@ cp_build_binary_op (location_t location, else result_type = type0; - if (TREE_CODE (op0) == ADDR_EXPR - && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))) + if (warn_address + && (complain & tf_warning) + && c_inhibit_evaluation_warnings == 0 + && !TREE_NO_WARNING (op0)) { - if ((complain & tf_warning) - && c_inhibit_evaluation_warnings == 0 - && !TREE_NO_WARNING (op0)) + tree cop0 = fold_non_dependent_expr (op0); + + if (TREE_CODE (cop0) == ADDR_EXPR + && decl_with_nonnull_addr_p (TREE_OPERAND (cop0, 0)) + && !TREE_NO_WARNING (cop0)) warning (OPT_Waddress, "the address of %qD will never be NULL", - TREE_OPERAND (op0, 0)); + TREE_OPERAND (cop0, 0)); } if (CONVERT_EXPR_P (op0) @@ -4559,14 +4563,18 @@ cp_build_binary_op (location_t location, else result_type = type1; - if (TREE_CODE (op1) == ADDR_EXPR - && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))) + if (warn_address + && (complain & tf_warning) + && c_inhibit_evaluation_warnings == 0 + && !TREE_NO_WARNING (op1)) { - if ((complain & tf_warning) - && c_inhibit_evaluation_warnings == 0 - && !TREE_NO_WARNING (op1)) + tree cop1 = fold_non_dependent_expr (op1); + + if (TREE_CODE (cop1) == ADDR_EXPR + && decl_with_nonnull_addr_p (TREE_OPERAND (cop1, 0)) + && !TREE_NO_WARNING (cop1)) warning (OPT_Waddress, "the address of %qD will never be NULL", - TREE_OPERAND (op1, 0)); + TREE_OPERAND (cop1, 0)); } if (CONVERT_EXPR_P (op1) diff --git gcc/testsuite/g++.dg/warn/constexpr-70194.C gcc/testsuite/g++.dg/warn/constexpr-70194.C index e69de29..cdc56c0 100644 --- gcc/testsuite/g++.dg/warn/constexpr-70194.C +++ gcc/testsuite/g++.dg/warn/constexpr-70194.C @@ -0,0 +1,12 @@ +// PR c++/70194 +// { dg-do compile { target c++11 } } +// { dg-options "-Wall" } + +int i; + +const bool b0 = &i == 0; // { dg-warning "the address of .i. will never be NULL" } +constexpr int *p = &i; +const bool b1 = p == 0; // { dg-warning "the address of .i. will never be NULL" } +const bool b2 = 0 == p; // { dg-warning "the address of .i. will never be NULL" } +const bool b3 = p != 0; // { dg-warning "the address of .i. will never be NULL" } +const bool b4 = 0 != p; // { dg-warning "the address of .i. will never be NULL" } Marek