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]

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


On 05/05/2010 11:49 PM, Joseph S. Myers wrote:
On Wed, 5 May 2010, Shujing Zhao wrote:

but would warn "comparison of distinct pointer types lacks a cast" for

extern void p()
if ( p > (void *)0)

since the types are not equivalent. My question is should the warnings to be
kept consistent to warn the "null pointer" for the latter test case?
If it is yes, besides warning inside the "comp_target_types" conditional, a
else if null_pointer_constant_p conditional would be needed before the "else"
conditional.

For the same reasons I have already explained it would be wrong to put any null_pointer_constant_p conditional (generating warnings) before the "else" cases that generate errors or pedwarns; those errors or pedwarns must always be generated when the relevant conditions on the types are satisfied, whether or not null pointer constants are involved. So if you give this warning it must be in addition to any existing errors or pedwarns, not replacing them. I do not believe it is worth giving it in the "else" cases where there is a more serious problem getting an error or pedwarn; avoiding unnecessary cascades of diagnostics seems like a good idea.


Ok. I got it. The attached four test cases indicate the warnings for ordered comparison of pointer with null pointer constant after applied the attached patch. Is it correct?

Thanks
Pearly
gcc/
2010-05-06  Shujing Zhao  <pearly.zhao@oracle.com>

	* c-typeck.c (build_binary_op): Warn ordered comparison of pointer
	with null pointer and also warn about ordered comparison of zero
	with pointer if -Wextra.

gcc/testsuite/
2010-05-06  Shujing Zhao  <pearly.zhao@oracle.com>

	* gcc.dg/ordered-comparison-1.c: New test.
	* gcc.dg/ordered-comparison-2.c: New test.
	* gcc.dg/ordered-comparison-3.c: New test.
	* gcc.dg/ordered-comparison-4.c: New test.

Index: c-typeck.c
===================================================================
--- c-typeck.c	(revision 158919)
+++ c-typeck.c	(working copy)
@@ -9625,6 +9625,11 @@ build_binary_op (location_t location, en
 	      else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
 		pedwarn (location, OPT_pedantic, "ISO C forbids "
 			 "ordered comparisons of pointers to functions");
+	      else if (null_pointer_constant_p (orig_op0)
+		       || null_pointer_constant_p (orig_op1))
+		warning_at (location, OPT_Wextra,
+			    "ordered comparison of pointer with null pointer");
+
 	    }
 	  else if (!addr_space_superset (as0, as1, &as_common))
 	    {
@@ -9649,13 +9654,17 @@ build_binary_op (location_t location, en
 		     "ordered comparison of pointer with integer zero");
 	  else if (extra_warnings)
 	    warning_at (location, OPT_Wextra,
-		     "ordered comparison of pointer with integer zero");
+			"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");
+	  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 (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
 	{
Index: testsuite/gcc.dg/ordered-comparison-1.c
===================================================================
--- testsuite/gcc.dg/ordered-comparison-1.c	(revision 0)
+++ testsuite/gcc.dg/ordered-comparison-1.c	(revision 0)
@@ -0,0 +1,21 @@
+/* Test warning for ordered comparison pointer with null pointer constant. */
+/* Tested with no warning option. */
+/* { dg-do compile } */
+/* { dg-options "" } */
+extern void z();
+void *p;
+
+void f() {
+ if (z >= 0)
+   z();
+ if (0 >= z)
+    z();
+ if (p >= (void*)0)
+    z();
+ if ((void*)0 >= p)
+    z();
+ if (z >= (void*)0) /* { dg-warning "distinct pointer types lacks a cast" } */
+    z();
+ if ((void*)0 >=z) /* { dg-warning "distinct pointer types lacks a cast" } */
+    z();
+}
Index: testsuite/gcc.dg/ordered-comparison-2.c
===================================================================
--- testsuite/gcc.dg/ordered-comparison-2.c	(revision 0)
+++ testsuite/gcc.dg/ordered-comparison-2.c	(revision 0)
@@ -0,0 +1,21 @@
+/* Test warning for ordered comparison pointer with null pointer constant. */
+/* Tested with -pedantic. */
+/* { dg-do compile } */
+/* { dg-options "-pedantic" } */
+extern void z();
+void *p;
+
+void f() {
+ if (z >= 0) /* { dg-warning "ordered comparison of pointer with" } */
+   z();
+ if (0 >= z) /* { dg-warning "ordered comparison of pointer with" } */
+    z();
+ if (p >= (void*)0)
+    z();
+ if ((void*)0 >= p)
+    z();
+ if (z >= (void*)0) /* { dg-warning "distinct pointer types lacks a cast" } */
+    z();
+ if ((void*)0 >=z) /* { dg-warning "distinct pointer types lacks a cast" } */
+    z();
+}
Index: testsuite/gcc.dg/ordered-comparison-3.c
===================================================================
--- testsuite/gcc.dg/ordered-comparison-3.c	(revision 0)
+++ testsuite/gcc.dg/ordered-comparison-3.c	(revision 0)
@@ -0,0 +1,21 @@
+/* Test warning for ordered comparison pointer with null pointer constant. */
+/* Test with -pedantic-errors. */
+/* { dg-do compile } */
+/* { dg-options "-pedantic-errors" } */
+extern void z();
+void *p;
+
+void f() {
+ if ( z >= 0 ) /* { dg-error "ordered comparison of pointer with" } */
+   z();
+ if ( 0 >= z) /* { dg-error "ordered comparison of pointer with" } */
+    z();
+ if ( p >= (void*)0 )
+    z();
+ if ( (void*)0 >= p)
+    z();
+ if (z >= (void*)0) /* { dg-error "distinct pointer types lacks a cast" } */
+    z();
+ if ((void*)0 >=z) /* { dg-error "distinct pointer types lacks a cast" } */
+    z(); 
+}
Index: testsuite/gcc.dg/ordered-comparison-4.c
===================================================================
--- testsuite/gcc.dg/ordered-comparison-4.c	(revision 0)
+++ testsuite/gcc.dg/ordered-comparison-4.c	(revision 0)
@@ -0,0 +1,21 @@
+/* Test warning for ordered comparison pointer with null pointer constant. */
+/* Test with -Wextra. */
+/* { dg-do compile } */
+/* { dg-options "-Wextra" } */
+extern void z();
+void *p;
+
+void f() {
+ if (z >= 0) /* { dg-warning "ordered comparison of pointer with" } */
+   z();
+ if (0 >= z) /* { dg-warning "ordered comparison of pointer with" } */
+    z();
+ if (p >= (void*)0) /* { dg-warning "ordered comparison of pointer with null pointer" } */
+    z();
+ if ((void*)0 >= p) /* { dg-warning "ordered comparison of pointer with null pointer" } */
+    z();
+ if (z >= (void*)0) /* { dg-warning "distinct pointer types lacks a cast" } */
+    z();
+ if ((void*)0 >=z) /* { dg-warning "distinct pointer types lacks a cast" } */
+    z();
+}

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