This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH, C] Warn ordered comparison pointer with null pointer constant


Hi,
As Manuel's reply at http://gcc.gnu.org/ml/gcc-patches/2010-04/msg01425.html, this patch is to change the warning of (z >= (void *)0) as same as the warning of (z >= 0), if z is a pointer.


Tested on i686-pc-linux-gnu.
Is it ok for trunk?

Thanks
Pearly


2010-04-29  Shujing Zhao  <pearly.zhao@oracle.com>

	* c-typeck.c (build_binary_op): Move forward check for ordered
	comparison pointer with null pointer constant.

Index: c-typeck.c
===================================================================
--- c-typeck.c	(revision 158822)
+++ c-typeck.c	(working copy)
@@ -9609,6 +9609,22 @@ build_binary_op (location_t location, en
 	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
 	      || code1 == FIXED_POINT_TYPE))
 	short_compare = 1;
+      else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
+	{
+	  result_type = type0;
+	  if (pedantic)
+	    pedwarn (location, OPT_pedantic,
+		     "ordered comparison of pointer with integer zero");
+	  else if (extra_warnings)
+	    warning_at (location, OPT_Wextra,
+		     "ordered comparison of pointer with integer zero");
+	}
+      else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
+	{
+	  result_type = type1;
+	  pedwarn (location, OPT_pedantic,
+		   "ordered comparison of pointer with integer zero");
+	}
       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
 	{
 	  addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
@@ -9641,22 +9657,6 @@ build_binary_op (location_t location, en
 		       "comparison of distinct pointer types lacks a cast");
 	    }
 	}
-      else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
-	{
-	  result_type = type0;
-	  if (pedantic)
-	    pedwarn (location, OPT_pedantic,
-		     "ordered comparison of pointer with integer zero");
-	  else if (extra_warnings)
-	    warning_at (location, OPT_Wextra,
-		     "ordered comparison of pointer with integer zero");
-	}
-      else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
-	{
-	  result_type = type1;
-	  pedwarn (location, OPT_pedantic,
-		   "ordered comparison of pointer with integer zero");
-	}
       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
 	{
 	  result_type = type0;
Index: testsuite/gcc.dg/compare11.c
===================================================================
--- testsuite/gcc.dg/compare11.c	(revision 0)
+++ testsuite/gcc.dg/compare11.c	(revision 0)
@@ -0,0 +1,15 @@
+/* Test warning for ordered comparison pointer with null pointer constant. */
+/* { dg-do compile } */
+/* { dg-options "-pedantic" } */
+extern void z();
+
+void f() {
+ if ( z >= 0 ) /* { dg-warning "ordered comparison of pointer with" } */
+   z();
+ if ( z >= (void*)0 ) /* { dg-warning "ordered comparison of pointer with" } */
+    z();
+ if ( 0 >= z) /* { dg-warning "ordered comparison of pointer with" } */
+    z();
+ if ( (void*)0 >= z) /* { dg-warning "ordered comparison of pointer with" } */
+    z();
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]