public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Paolo Carlini <paolo.carlini@oracle.com>
To: Jason Merrill <jason@redhat.com>,
	       "gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>
Subject: Re: [C++ Patch/RFC] PR 43906
Date: Tue, 05 Aug 2014 11:49:00 -0000	[thread overview]
Message-ID: <53E0C4C4.3010101@oracle.com> (raw)
In-Reply-To: <53E03A36.5080203@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 1363 bytes --]

Hi,

On 08/05/2014 03:58 AM, Jason Merrill wrote:
> On 08/04/2014 07:01 PM, Paolo Carlini wrote:
>> In fact I wondered about that a few minutes after sending my message...
>> And this is what I figured out: normally we have hard errors from
>> composite_pointer_type (eg, try scalar types, class types), even for
>> null values. The only exception I have been able to find earlier today
>> is that of pointer to the same function type, eg:
>>
>> extern void z();
>> typedef void (*ptr)();
>> void i() { if ( z != (ptr)0 ); }
>>
>> but in this case the C front-end too doesn't warn.
> I don't see why we wouldn't want to warn in this case; it's still the 
> case thet the comparison will always be false.
In general, I agree of course. I was trying to understand if keeping the 
issue as minimally one of consistency with the C front-end simplified it.
> We can also see this situation for non-function pointers:
>
> void f()
> {
>   int i;
>   if (&i != (int*)0);
> }
Sure. Then, however, we must be careful about the actual pointer types, 
otherwise we change hard errors to warnings. And void* is an exception 
to the general rule. I tried using ptr_reasonably_similar to avoid the 
explicit call of comptypes + separate VOID_TYPE_P, but unfortunately it 
appears too loose about at least pointers to function types.

Thanks!
Paolo.

//////////////////////////

[-- Attachment #2: patch_43906_3 --]
[-- Type: text/plain, Size: 4330 bytes --]

Index: cp/typeck.c
===================================================================
--- cp/typeck.c	(revision 213631)
+++ cp/typeck.c	(working copy)
@@ -4353,12 +4353,14 @@ 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))
+	       && (null_ptr_cst_p (op1)
+		   /* Handle, eg, (void*)0 (c++/43906), and more.  */
+		   || (TYPE_PTR_P (type1) && integer_zerop (op1)
+		       && (VOID_TYPE_P (TREE_TYPE (type1))
+			   || comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (type0)),
+					 TYPE_MAIN_VARIANT (TREE_TYPE (type1)),
+					 COMPARE_BASE | COMPARE_DERIVED)))))
 	{
 	  if (TREE_CODE (op0) == ADDR_EXPR
 	      && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
@@ -4371,7 +4373,13 @@ cp_build_binary_op (location_t location,
 	  result_type = type0;
 	}
       else if ((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
-	       && null_ptr_cst_p (op0))
+	       && (null_ptr_cst_p (op0)
+		   /* Handle, eg, (void*)0 (c++/43906), and more.  */
+		   || (TYPE_PTR_P (type0) && integer_zerop (op0)
+		       && (VOID_TYPE_P (TREE_TYPE (type0))
+			   || comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (type0)),
+					 TYPE_MAIN_VARIANT (TREE_TYPE (type1)),
+					 COMPARE_BASE | COMPARE_DERIVED)))))
 	{
 	  if (TREE_CODE (op1) == ADDR_EXPR 
 	      && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
@@ -4383,6 +4391,10 @@ cp_build_binary_op (location_t location,
 	    }
 	  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,42 @@
+// PR c++/43906
+// { dg-options "-Waddress" }
+
+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" }
+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" }
+void pl() { if (z != (ptrfn)0) z(); }   // { dg-error "comparison" }
+void ql() { if (&d != (int*)0) z(); }   // { dg-error "comparison" }
+void rl() { if (&s != (U*)0) z(); }     // { dg-error "comparison" }
+
+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" }
+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" }
+void pr() { if ((ptrfn)0 != z) z(); }   // { dg-error "comparison" }
+void qr() { if ((int*)0 != &d) z(); }   // { dg-error "comparison" }
+void rr() { if ((U*)0 != &s) z(); }     // { dg-error "comparison" }

  reply	other threads:[~2014-08-05 11:49 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-04 16:24 Paolo Carlini
2014-08-04 20:45 ` Jason Merrill
2014-08-04 23:01   ` Paolo Carlini
2014-08-05  1:58     ` Jason Merrill
2014-08-05 11:49       ` Paolo Carlini [this message]
2014-08-05 12:10         ` Paolo Carlini
2014-08-05 12:33           ` Jason Merrill
2014-08-05 14:48             ` Paolo Carlini
2014-08-06 15:19               ` Jason Merrill
2014-08-06 17:07                 ` Paolo Carlini
2014-08-06 18:20                   ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=53E0C4C4.3010101@oracle.com \
    --to=paolo.carlini@oracle.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).