From patchwork Wed Aug 6 17:07:26 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 377085 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 8FAC914009C for ; Thu, 7 Aug 2014 03:07:43 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:subject:references :in-reply-to:content-type; q=dns; s=default; b=RE+G2xAimG3RBnHlE Iyz4/gJrN0e3y8VXYuRZgzelgD2HZ21lhtPf+6fIdopcsMpWSAa8aN8Z6QI4P86Y uw7DhFk9lYhTC1YnnjhWWvkubL2UTLzAAxDdeVILcZyntOoJK7AqwR8AzaTJAa+K dmocHhHFgMWcy+A19WCvFVvzMY= 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 :message-id:date:from:mime-version:to:subject:references :in-reply-to:content-type; s=default; bh=cb87vEL8fqOnP6Bao7BQWHD AQeE=; b=MWrcnkpea91NsU1/g/uWQdCKx3SQ4TWrf2vbBkfCkRZFevgBcW+eUfZ 6y3F/4ftyyeqM/0AvkNPdUB+Y5v/AG+XC3TgnQ4yDkCaxmhx/obgMF40GpWTQMcz Nemrr2I5DZlfZ3PwodnxnjZuOzg90jm5ufrSSptTcWoNBoOl6JG0= Received: (qmail 20248 invoked by alias); 6 Aug 2014 17:07:36 -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 20237 invoked by uid 89); 6 Aug 2014 17:07:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.6 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 X-HELO: aserp1040.oracle.com Received: from aserp1040.oracle.com (HELO aserp1040.oracle.com) (141.146.126.69) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Wed, 06 Aug 2014 17:07:34 +0000 Received: from acsinet21.oracle.com (acsinet21.oracle.com [141.146.126.237]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id s76H7VVK030079 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 6 Aug 2014 17:07:31 GMT Received: from aserz7022.oracle.com (aserz7022.oracle.com [141.146.126.231]) by acsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id s76H7UAF017748 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 6 Aug 2014 17:07:30 GMT Received: from abhmp0002.oracle.com (abhmp0002.oracle.com [141.146.116.8]) by aserz7022.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id s76H7TOm009768; Wed, 6 Aug 2014 17:07:29 GMT Received: from [192.168.1.4] (/79.33.191.31) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 06 Aug 2014 10:07:29 -0700 Message-ID: <53E260CE.3080604@oracle.com> Date: Wed, 06 Aug 2014 19:07:26 +0200 From: Paolo Carlini User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: Jason Merrill , "gcc-patches@gcc.gnu.org" Subject: Re: [C++ Patch/RFC] PR 43906 References: <53DFB3AE.1030706@oracle.com> <53DFF100.6010105@redhat.com> <53E010B2.6050703@oracle.com> <53E03A36.5080203@redhat.com> <53E0C4C4.3010101@oracle.com> <53E0C99A.4060003@oracle.com> <53E0CEF9.7020204@redhat.com> <53E0EEC1.2070605@oracle.com> <53E24768.5060002@redhat.com> In-Reply-To: <53E24768.5060002@redhat.com> X-IsSubscribed: yes Hi, On 08/06/2014 05:19 PM, Jason Merrill wrote: > On 08/05/2014 10:48 AM, Paolo Carlini wrote: >> + && (VOID_TYPE_P (TREE_TYPE (type1)) >> + || comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (type0)), >> + TYPE_MAIN_VARIANT (TREE_TYPE (type1)), >> + COMPARE_BASE | COMPARE_DERIVED)))) > > Can we drop this now that we're calling composite_pointer_type? Yes we can, sorry for not investigating that earlier. I only have to tweak a bit the testcase because then in the malformed cases we emit first the permerror and then the -Waddress warning too. I suppose it's Ok because after all those are in most of the cases permerrors and I don't think the additional verbosity should be that common, we are talking about comparing a "null" pointer of the wrong type, not a generic pointer. Otherwise we would have to tweak composite_pointer_type to precisely inform the caller when an actual error was emitted. Thanks, Paolo. ///////////////////// Index: cp/typeck.c =================================================================== --- cp/typeck.c (revision 213654) +++ cp/typeck.c (working copy) @@ -4353,13 +4353,18 @@ cp_build_binary_op (location_t location, && (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)) short_compare = 1; - else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE) - || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1))) - result_type = composite_pointer_type (type0, type1, op0, op1, - CPO_COMPARISON, complain); - else if ((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0)) - && null_ptr_cst_p (op1)) + else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0)) + && null_ptr_cst_p (op1)) + /* Handle, eg, (void*)0 (c++/43906), and more. */ + || (code0 == POINTER_TYPE + && TYPE_PTR_P (type1) && integer_zerop (op1))) { + if (TYPE_PTR_P (type1)) + result_type = composite_pointer_type (type0, type1, op0, op1, + CPO_COMPARISON, complain); + else + result_type = type0; + if (TREE_CODE (op0) == ADDR_EXPR && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))) { @@ -4368,11 +4373,19 @@ cp_build_binary_op (location_t location, warning (OPT_Waddress, "the address of %qD will never be NULL", TREE_OPERAND (op0, 0)); } - result_type = type0; } - else if ((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1)) - && null_ptr_cst_p (op0)) + else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1)) + && null_ptr_cst_p (op0)) + /* Handle, eg, (void*)0 (c++/43906), and more. */ + || (code1 == POINTER_TYPE + && TYPE_PTR_P (type0) && integer_zerop (op0))) { + if (TYPE_PTR_P (type0)) + result_type = composite_pointer_type (type0, type1, op0, op1, + CPO_COMPARISON, complain); + else + result_type = type1; + if (TREE_CODE (op1) == ADDR_EXPR && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))) { @@ -4381,8 +4394,11 @@ cp_build_binary_op (location_t location, warning (OPT_Waddress, "the address of %qD will never be NULL", TREE_OPERAND (op1, 0)); } - result_type = type1; } + else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE) + || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1))) + result_type = composite_pointer_type (type0, type1, op0, op1, + CPO_COMPARISON, complain); else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1)) /* One of the operands must be of nullptr_t type. */ result_type = TREE_TYPE (nullptr_node); Index: testsuite/g++.dg/warn/Waddress-1.C =================================================================== --- testsuite/g++.dg/warn/Waddress-1.C (revision 0) +++ testsuite/g++.dg/warn/Waddress-1.C (working copy) @@ -0,0 +1,50 @@ +// PR c++/43906 +// { dg-options "-Waddress -pedantic" } + +extern void z(); +typedef void (*ptrf) (); +typedef int (*ptrfn) (int); +int n; +const int m = 1; +struct S { }; +struct T : S { }; +struct U; +S s; +T t; +double d; + +void f() { if (z) z(); } // { dg-warning "address" } + +void gl() { if (z != 0) z(); } // { dg-warning "address" } +void hl() { if (z != (ptrf)0) z(); } // { dg-warning "address" } +void il() { if (z != (void*)0) z(); } // { dg-warning "address|comparison" } +void jl() { if (&n != (int*)0) z(); } // { dg-warning "address" } +void kl() { if (&m != (int*)0) z(); } // { dg-warning "address" } +void ll() { if (&s != (T*)0) z(); } // { dg-warning "address" } +void ml() { if (&t != (S*)0) z(); } // { dg-warning "address" } + +void nl() { if (z != (S*)0) z(); } // { dg-error "comparison" } +// { dg-warning "address" "" { target *-*-* } 26 } +void pl() { if (z != (ptrfn)0) z(); } // { dg-error "comparison" } +// { dg-warning "address" "" { target *-*-* } 28 } +void ql() { if (&d != (int*)0) z(); } // { dg-error "comparison" } +// { dg-warning "address" "" { target *-*-* } 30 } +void rl() { if (&s != (U*)0) z(); } // { dg-error "comparison" } +// { dg-warning "address" "" { target *-*-* } 32 } + +void gr() { if (0 != z) z(); } // { dg-warning "address" } +void hr() { if ((ptrf)0 != z) z(); } // { dg-warning "address" } +void ir() { if ((void*)0 != z) z(); } // { dg-warning "address|comparison" } +void jr() { if ((int*)0 != &n) z(); } // { dg-warning "address" } +void kr() { if ((int*)0 != &m) z(); } // { dg-warning "address" } +void lr() { if ((T*)0 != &s) z(); } // { dg-warning "address" } +void mr() { if ((S*)0 != &t) z(); } // { dg-warning "address" } + +void nr() { if ((S*)0 != z) z(); } // { dg-error "comparison" } +// { dg-warning "address" "" { target *-*-* } 43 } +void pr() { if ((ptrfn)0 != z) z(); } // { dg-error "comparison" } +// { dg-warning "address" "" { target *-*-* } 45 } +void qr() { if ((int*)0 != &d) z(); } // { dg-error "comparison" } +// { dg-warning "address" "" { target *-*-* } 47 } +void rr() { if ((U*)0 != &s) z(); } // { dg-error "comparison" } +// { dg-warning "address" "" { target *-*-* } 49 }