aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorShujing Zhao <pearly.zhao@oracle.com>2010-05-07 08:18:06 +0000
committerShujing Zhao <pzhao@gcc.gnu.org>2010-05-07 08:18:06 +0000
commitd42ba3b80a51696ae5e68602d1170b05a56af27e (patch)
treefaabccf82b26d0837202f4853566831cd72abc7b /gcc
parentbb9c865802fd2b86e1de373af4cdf1fcde08000e (diff)
downloadgcc-d42ba3b80a51696ae5e68602d1170b05a56af27e.zip
gcc-d42ba3b80a51696ae5e68602d1170b05a56af27e.tar.gz
gcc-d42ba3b80a51696ae5e68602d1170b05a56af27e.tar.bz2
c-typeck.c (build_binary_op): Warn ordered comparison of pointer with null pointer and also warn about...
gcc/ 2010-05-07 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-07 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. From-SVN: r159145
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog6
-rw-r--r--gcc/c-typeck.c15
-rw-r--r--gcc/testsuite/ChangeLog7
-rw-r--r--gcc/testsuite/gcc.dg/ordered-comparison-1.c21
-rw-r--r--gcc/testsuite/gcc.dg/ordered-comparison-2.c21
-rw-r--r--gcc/testsuite/gcc.dg/ordered-comparison-3.c21
-rw-r--r--gcc/testsuite/gcc.dg/ordered-comparison-4.c21
7 files changed, 109 insertions, 3 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 5839779..f1885a9 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2010-05-07 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.
+
2010-05-05 Andreas Simbuerger <simbuerg@fim.uni-passau.de>
* graphite-blocking.c
diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c
index 96b2ab1..2f66d6a 100644
--- a/gcc/c-typeck.c
+++ b/gcc/c-typeck.c
@@ -9625,6 +9625,11 @@ build_binary_op (location_t location, enum tree_code code,
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, enum tree_code code,
"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)
{
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 1377bcb..50397ef 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,10 @@
+2010-05-07 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.
+
2010-05-06 Mike Stump <mikestump@comcast.net>
PR objc/35165
diff --git a/gcc/testsuite/gcc.dg/ordered-comparison-1.c b/gcc/testsuite/gcc.dg/ordered-comparison-1.c
new file mode 100644
index 0000000..0131c9d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ordered-comparison-1.c
@@ -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();
+}
diff --git a/gcc/testsuite/gcc.dg/ordered-comparison-2.c b/gcc/testsuite/gcc.dg/ordered-comparison-2.c
new file mode 100644
index 0000000..a99d9c3
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ordered-comparison-2.c
@@ -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();
+}
diff --git a/gcc/testsuite/gcc.dg/ordered-comparison-3.c b/gcc/testsuite/gcc.dg/ordered-comparison-3.c
new file mode 100644
index 0000000..2e4be49
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ordered-comparison-3.c
@@ -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();
+}
diff --git a/gcc/testsuite/gcc.dg/ordered-comparison-4.c b/gcc/testsuite/gcc.dg/ordered-comparison-4.c
new file mode 100644
index 0000000..ebe9edd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ordered-comparison-4.c
@@ -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();
+}