From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 69485 invoked by alias); 2 Oct 2016 18:43:45 -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 69466 invoked by uid 89); 2 Oct 2016 18:43:44 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.4 required=5.0 tests=AWL,BAYES_00,RCVD_IN_SORBS_SPAM autolearn=no version=3.3.2 spammy=ordered, 2016-10-01 X-HELO: mail-oi0-f52.google.com Received: from mail-oi0-f52.google.com (HELO mail-oi0-f52.google.com) (209.85.218.52) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 02 Oct 2016 18:43:42 +0000 Received: by mail-oi0-f52.google.com with SMTP id w11so180281734oia.2 for ; Sun, 02 Oct 2016 11:43:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=oVOHrL15BlXwYRKN1c20K25YqRP4n8eOF8NPwYF7354=; b=SD1/UsM8q0nakvjmB73uDylN/X1zHhvuJTJg5FmzHmyk+2woUZpiIP82uaUpQQA/qv FewI59L9J85ClZCeFZw2u9rj+2zHPUsYVDAZBfCZAcy4Lyaz0b1ZhP9rrKnaQ+Z5liuH Ola59Lujw0h+9g0rFq1399kV0Oq+pUH79jh982chVpqRyBONGMmHqsOLG5y+B2pYr6JG eBdp94jWpuSb6S7be0TwAfQaxR6tdj0N64OrgCu4sgLpK2JemXOTlbeMdppAfxm4ib+A Q3V7kx3pp0qzkCtCpc+yjI5VrpDBdbJ71+03oclH6iYW43JqqIvh0uTaqvd9upanQLQm yO8A== X-Gm-Message-State: AA6/9RmthPCnj4jxnbrBtOcJaFYFMiJTsgxUrrswILw7C4Ijge5mbShZu/lCUH1PErm3PLFOGREDuMSlEwgegzhY X-Received: by 10.202.243.194 with SMTP id r185mr14417519oih.12.1475433820796; Sun, 02 Oct 2016 11:43:40 -0700 (PDT) MIME-Version: 1.0 Received: by 10.182.105.167 with HTTP; Sun, 2 Oct 2016 11:43:20 -0700 (PDT) In-Reply-To: <20161001141630.GJ3223@redhat.com> References: <20160910145843.GJ19950@redhat.com> <20160915121631.GY19950@redhat.com> <20160923131537.GB3223@redhat.com> <20160930164337.GE3223@redhat.com> <20161001141630.GJ3223@redhat.com> From: Jason Merrill Date: Sun, 02 Oct 2016 18:43:00 -0000 Message-ID: Subject: Re: C/C++ PATCH to implement -Wpointer-compare warning (PR c++/64767) To: Marek Polacek Cc: GCC Patches Content-Type: text/plain; charset=UTF-8 X-IsSubscribed: yes X-SW-Source: 2016-10/txt/msg00039.txt.bz2 OK, thanks. On Sat, Oct 1, 2016 at 10:16 AM, Marek Polacek wrote: > On Fri, Sep 30, 2016 at 05:48:03PM -0400, Jason Merrill wrote: >> On Fri, Sep 30, 2016 at 12:43 PM, Marek Polacek wrote: >> > On Fri, Sep 23, 2016 at 10:31:33AM -0400, Jason Merrill wrote: >> >> On Fri, Sep 23, 2016 at 9:15 AM, Marek Polacek wrote: >> >> > On Wed, Sep 21, 2016 at 03:52:09PM -0400, Jason Merrill wrote: >> >> >> On Mon, Sep 19, 2016 at 2:49 PM, Jason Merrill wrote: >> >> >> > I suppose that an INTEGER_CST of character type is necessarily a >> >> >> > character constant, so adding a check for !char_type_p ought to do the >> >> >> > trick. >> >> >> >> >> >> Indeed it does. I'm checking this in: >> >> > >> >> > Nice, thanks. What about the original patch? We still need to warn >> >> > (or error for C++11) for pointer comparisons. >> >> >> >> If we still accept pointer comparisons in C++, that's another bug with >> >> treating \0 as a null pointer constant. This seems to be because >> >> ocp_convert of \0 to int produces an INTEGER_CST indistinguishable >> >> from literal 0. >> > >> > I was trying to fix this in ocp_convert, by using NOP_EXPRs, but that wasn't >> > successful. But since we're interested in ==/!=, I think this can be fixed >> > easily in cp_build_binary_op. Actually, all that seems to be needed is using >> > orig_op as the argument to null_ptr_cst_p, but that wouldn't give the correct >> > diagnostics, so I did this. By checking orig_op we can see if the operands are >> > character literals or not, because orig_op is an operand before the default >> > conversions. >> >> What is wrong about the diagnostic from just using orig_op? "ISO C++ >> forbids comparison between pointer and integer" seems fine to me, and >> will help the user to realize that they need to index off the pointer. >> >> I see that some of the calls to null_ptr_cst_p in cp_build_binary_op >> have already been changed to check orig_op*, but not all. Let's >> update the remaining calls, that should do the trick without adding a >> new error. > > Here you go: > > Bootstrapped/regtested on x86_64-linux and ppc64-linux, ok for trunk? > > 2016-10-01 Marek Polacek > > Core 903 > * typeck.c (cp_build_binary_op): Pass original operands to > null_ptr_cst_p, not those after the default conversions. > > * g++.dg/cpp0x/nullptr37.C: New test. > > diff --git gcc/cp/typeck.c gcc/cp/typeck.c > index 617ca55..8b780be 100644 > --- gcc/cp/typeck.c > +++ gcc/cp/typeck.c > @@ -4573,7 +4573,7 @@ cp_build_binary_op (location_t location, > || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)) > short_compare = 1; > else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0)) > - && null_ptr_cst_p (op1)) > + && null_ptr_cst_p (orig_op1)) > /* Handle, eg, (void*)0 (c++/43906), and more. */ > || (code0 == POINTER_TYPE > && TYPE_PTR_P (type1) && integer_zerop (op1))) > @@ -4587,7 +4587,7 @@ cp_build_binary_op (location_t location, > warn_for_null_address (location, op0, complain); > } > else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1)) > - && null_ptr_cst_p (op0)) > + && null_ptr_cst_p (orig_op0)) > /* Handle, eg, (void*)0 (c++/43906), and more. */ > || (code1 == POINTER_TYPE > && TYPE_PTR_P (type0) && integer_zerop (op0))) > @@ -4604,7 +4604,7 @@ cp_build_binary_op (location_t location, > || (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)) > + else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)) > /* One of the operands must be of nullptr_t type. */ > result_type = TREE_TYPE (nullptr_node); > else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) > @@ -4623,7 +4623,7 @@ cp_build_binary_op (location_t location, > else > return error_mark_node; > } > - else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1)) > + else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1)) > { > if (TARGET_PTRMEMFUNC_VBIT_LOCATION > == ptrmemfunc_vbit_in_delta) > @@ -4664,7 +4664,7 @@ cp_build_binary_op (location_t location, > } > result_type = TREE_TYPE (op0); > } > - else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0)) > + else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0)) > return cp_build_binary_op (location, code, op1, op0, complain); > else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)) > { > @@ -4877,21 +4877,21 @@ cp_build_binary_op (location_t location, > else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE) > result_type = composite_pointer_type (type0, type1, op0, op1, > CPO_COMPARISON, complain); > - else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1)) > + else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1)) > { > result_type = type0; > if (extra_warnings && (complain & tf_warning)) > warning (OPT_Wextra, > "ordered comparison of pointer with integer zero"); > } > - else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0)) > + else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0)) > { > result_type = type1; > if (extra_warnings && (complain & tf_warning)) > warning (OPT_Wextra, > "ordered comparison of pointer with integer zero"); > } > - else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1)) > + else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)) > /* One of the operands must be of nullptr_t type. */ > result_type = TREE_TYPE (nullptr_node); > else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE) > diff --git gcc/testsuite/g++.dg/cpp0x/nullptr37.C gcc/testsuite/g++.dg/cpp0x/nullptr37.C > index e69de29..e746a28 100644 > --- gcc/testsuite/g++.dg/cpp0x/nullptr37.C > +++ gcc/testsuite/g++.dg/cpp0x/nullptr37.C > @@ -0,0 +1,78 @@ > +/* PR c++/64767 */ > +// { dg-do compile { target c++11 } } > + > +int > +f1 (int *p, int **q) > +{ > + int r = 0; > + > + r += p == '\0'; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += p == L'\0'; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += p == u'\0'; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += p == U'\0'; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += p != '\0'; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += p != L'\0'; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += p != u'\0'; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += p != U'\0'; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + > + r += '\0' == p; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += L'\0' == p; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += u'\0' == p; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += U'\0' == p; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += '\0' != p; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += L'\0' != p; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += u'\0' != p; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += U'\0' != p; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + > + r += q == '\0'; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += q == L'\0'; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += q == u'\0'; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += q == U'\0'; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += q != '\0'; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += q != L'\0'; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += q != u'\0'; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += q != U'\0'; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + > + r += '\0' == q; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += L'\0' == q; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += u'\0' == q; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += U'\0' == q; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += '\0' != q; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += L'\0' != q; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += u'\0' != q; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += U'\0' != q; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + > + return r; > +} > + > +int > +f2 (int *p) > +{ > + int r = 0; > + > + r += p == (void *) 0; > + r += p != (void *) 0; > + r += (void *) 0 == p; > + r += (void *) 0 != p; > + > + r += p == 0; > + r += p != 0; > + r += 0 == p; > + r += 0 != p; > + > + return r; > +} > + > +int > +f3 (int *p) > +{ > + int r = 0; > + > + r += p == (char) 0; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += p != (char) 0; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + > + r += (char) 0 == p; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + r += (char) 0 != p; // { dg-error "ISO C\\+\\+ forbids comparison between pointer and integer" } > + > + return r; > +} > > Marek