From patchwork Wed Mar 16 14:45:53 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 598442 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3qQDnZ2JFCz9ssM for ; Thu, 17 Mar 2016 01:46:09 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=vFXuGRTE; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; q=dns; s=default; b=swwbhqRKHI6rUNbKy HgdAcPabJF1Gwr7T64cZ7AQdNTc/06oh4HoI9N+9Y43qQ/Z+eVvfmFCgzEWraE4H VZcwyhy1SlxTSClnpiRemzUYLnBxpdX7e9NuPzflFyFtFWf7QjAmv+ISYjDCTltq HKGP4FPWAUIA4+N1IsrvX63aQ4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=default; bh=4UFQW5lc+iOqf6G7XvuuF9k be+Q=; b=vFXuGRTEbXBujwKQJEpSRHuM9cjB4EtXdMpg7AQaS4rHWG84rK0KZHJ jg0E9gYmgyxfVMV/H5Ih+xHYUxcbL5PQOtKHHKnxeg+83OQOt6cOhpvximarMXFg l/1B0AovrD9oPqx9xZNvHzc6n8BhX+OUGEVpvNxA9Sz01Rq/xqEc= Received: (qmail 87799 invoked by alias); 16 Mar 2016 14:46:00 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 87774 invoked by uid 89); 16 Mar 2016 14:45:59 -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= 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; Wed, 16 Mar 2016 14:45:58 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id 100366030 for ; Wed, 16 Mar 2016 14:45:57 +0000 (UTC) Received: from redhat.com (ovpn-204-101.brq.redhat.com [10.40.204.101]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u2GEjrQA023913 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 16 Mar 2016 10:45:55 -0400 Date: Wed, 16 Mar 2016 15:45:53 +0100 From: Marek Polacek To: Jason Merrill Cc: Jakub Jelinek , GCC Patches Subject: Re: C++ PATCH to fix missing warning (PR c++/70194) Message-ID: <20160316144552.GG10006@redhat.com> References: <20160315104120.GC10006@redhat.com> <20160315105618.GT3017@tucnak.redhat.com> <20160315120918.GD10006@redhat.com> <56E86580.70800@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <56E86580.70800@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) On Tue, Mar 15, 2016 at 03:41:52PM -0400, Jason Merrill wrote: > Let's factor out that duplicated code into a separate function. Sure. It also allowed me to hoist the cheap tests for both warnings, and while at it, I used 'location' for the first warning. Bootstrapped/regtested on x86_64-linux, ok for trunk? 2016-03-16 Marek Polacek PR c++/70194 * typeck.c (warn_for_null_address): New function. (cp_build_binary_op): Call it. * g++.dg/warn/constexpr-70194.C: New test. Marek diff --git gcc/cp/typeck.c gcc/cp/typeck.c index 20f0afc..447006c 100644 --- gcc/cp/typeck.c +++ gcc/cp/typeck.c @@ -3974,6 +3974,38 @@ build_vec_cmp (tree_code code, tree type, return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec); } +/* Possibly warn about an address never being NULL. */ + +static void +warn_for_null_address (location_t location, tree op, tsubst_flags_t complain) +{ + if (!warn_address + || (complain & tf_warning) == 0 + || c_inhibit_evaluation_warnings != 0 + || TREE_NO_WARNING (op)) + return; + + tree cop = fold_non_dependent_expr (op); + + if (TREE_CODE (cop) == ADDR_EXPR + && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0)) + && !TREE_NO_WARNING (cop)) + warning_at (location, OPT_Waddress, "the address of %qD will never " + "be NULL", TREE_OPERAND (cop, 0)); + + if (CONVERT_EXPR_P (op) + && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == REFERENCE_TYPE) + { + tree inner_op = op; + STRIP_NOPS (inner_op); + + if (DECL_P (inner_op)) + warning_at (location, OPT_Waddress, + "the compiler can assume that the address of " + "%qD will never be NULL", inner_op); + } +} + /* Build a binary-operation expression without default conversions. CODE is the kind of expression to build. LOCATION is the location_t of the operator in the source code. @@ -4520,32 +4552,7 @@ 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 ((complain & tf_warning) - && c_inhibit_evaluation_warnings == 0 - && !TREE_NO_WARNING (op0)) - warning (OPT_Waddress, "the address of %qD will never be NULL", - TREE_OPERAND (op0, 0)); - } - - if (CONVERT_EXPR_P (op0) - && TREE_CODE (TREE_TYPE (TREE_OPERAND (op0, 0))) - == REFERENCE_TYPE) - { - tree inner_op0 = op0; - STRIP_NOPS (inner_op0); - - if ((complain & tf_warning) - && c_inhibit_evaluation_warnings == 0 - && !TREE_NO_WARNING (op0) - && DECL_P (inner_op0)) - warning_at (location, OPT_Waddress, - "the compiler can assume that the address of " - "%qD will never be NULL", - inner_op0); - } + warn_for_null_address (location, op0, complain); } else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1)) && null_ptr_cst_p (op0)) @@ -4559,32 +4566,7 @@ 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 ((complain & tf_warning) - && c_inhibit_evaluation_warnings == 0 - && !TREE_NO_WARNING (op1)) - warning (OPT_Waddress, "the address of %qD will never be NULL", - TREE_OPERAND (op1, 0)); - } - - if (CONVERT_EXPR_P (op1) - && TREE_CODE (TREE_TYPE (TREE_OPERAND (op1, 0))) - == REFERENCE_TYPE) - { - tree inner_op1 = op1; - STRIP_NOPS (inner_op1); - - if ((complain & tf_warning) - && c_inhibit_evaluation_warnings == 0 - && !TREE_NO_WARNING (op1) - && DECL_P (inner_op1)) - warning_at (location, OPT_Waddress, - "the compiler can assume that the address of " - "%qD will never be NULL", - inner_op1); - } + warn_for_null_address (location, op1, complain); } else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE) || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1))) 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" }