aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Carlini <paolo.carlini@oracle.com>2014-08-06 19:09:08 +0000
committerPaolo Carlini <paolo@gcc.gnu.org>2014-08-06 19:09:08 +0000
commit81caef48c80b4f9a4daa8a4cbe429660e846f793 (patch)
treed997cf661a8a66d9f959ea6a8cf0dd60c37a5013
parentc5ff29dae8f2fff20b848df80ab9fa911bc1a2f1 (diff)
downloadgcc-81caef48c80b4f9a4daa8a4cbe429660e846f793.zip
gcc-81caef48c80b4f9a4daa8a4cbe429660e846f793.tar.gz
gcc-81caef48c80b4f9a4daa8a4cbe429660e846f793.tar.bz2
re PR c++/43906 (missing warnings the comparison between an address with a null pointer constant)
/cp 2014-08-06 Paolo Carlini <paolo.carlini@oracle.com> PR c++/43906 * typeck.c (cp_build_binary_op): Extend to more cases the -Waddress warning. /testsuite 2014-08-06 Paolo Carlini <paolo.carlini@oracle.com> PR c++/43906 * g++.dg/warn/Waddress-1.C: New. From-SVN: r213682
-rw-r--r--gcc/cp/ChangeLog6
-rw-r--r--gcc/cp/typeck.c38
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/g++.dg/warn/Waddress-1.C50
4 files changed, 88 insertions, 11 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index d9ad73c..e4f12b1 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,9 @@
+2014-08-06 Paolo Carlini <paolo.carlini@oracle.com>
+
+ PR c++/43906
+ * typeck.c (cp_build_binary_op): Extend to more cases the
+ -Waddress warning.
+
2014-08-01 Braden Obrzut <admin@maniacsvault.net>
Implement constexpr variable templates
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 735cd58..353d921 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -4353,13 +4353,18 @@ 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))
- {
+ else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
+ && null_ptr_cst_p (op1))
+ /* Handle, eg, (void*)0 (c++/43906), and more. */
+ || (code0 == POINTER_TYPE
+ && TYPE_PTR_P (type1) && integer_zerop (op1)))
+ {
+ if (TYPE_PTR_P (type1))
+ result_type = composite_pointer_type (type0, type1, op0, op1,
+ CPO_COMPARISON, complain);
+ else
+ result_type = type0;
+
if (TREE_CODE (op0) == ADDR_EXPR
&& decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
{
@@ -4368,11 +4373,19 @@ cp_build_binary_op (location_t location,
warning (OPT_Waddress, "the address of %qD will never be NULL",
TREE_OPERAND (op0, 0));
}
- result_type = type0;
}
- else if ((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
- && null_ptr_cst_p (op0))
+ else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
+ && null_ptr_cst_p (op0))
+ /* Handle, eg, (void*)0 (c++/43906), and more. */
+ || (code1 == POINTER_TYPE
+ && TYPE_PTR_P (type0) && integer_zerop (op0)))
{
+ if (TYPE_PTR_P (type0))
+ result_type = composite_pointer_type (type0, type1, op0, op1,
+ CPO_COMPARISON, complain);
+ else
+ result_type = type1;
+
if (TREE_CODE (op1) == ADDR_EXPR
&& decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
{
@@ -4381,8 +4394,11 @@ cp_build_binary_op (location_t location,
warning (OPT_Waddress, "the address of %qD will never be NULL",
TREE_OPERAND (op1, 0));
}
- 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);
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index ce1003c..0c5c386 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2014-08-06 Paolo Carlini <paolo.carlini@oracle.com>
+
+ PR c++/43906
+ * g++.dg/warn/Waddress-1.C: New.
+
2014-08-06 Maciej W. Rozycki <macro@codesourcery.com>
* gcc.dg/pr44194-1.c: Remove an extraneous brace.
diff --git a/gcc/testsuite/g++.dg/warn/Waddress-1.C b/gcc/testsuite/g++.dg/warn/Waddress-1.C
new file mode 100644
index 0000000..8723ac9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Waddress-1.C
@@ -0,0 +1,50 @@
+// PR c++/43906
+// { dg-options "-Waddress -pedantic" }
+
+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|comparison" }
+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" }
+// { dg-warning "address" "" { target *-*-* } 26 }
+void pl() { if (z != (ptrfn)0) z(); } // { dg-error "comparison" }
+// { dg-warning "address" "" { target *-*-* } 28 }
+void ql() { if (&d != (int*)0) z(); } // { dg-error "comparison" }
+// { dg-warning "address" "" { target *-*-* } 30 }
+void rl() { if (&s != (U*)0) z(); } // { dg-error "comparison" }
+// { dg-warning "address" "" { target *-*-* } 32 }
+
+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|comparison" }
+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" }
+// { dg-warning "address" "" { target *-*-* } 43 }
+void pr() { if ((ptrfn)0 != z) z(); } // { dg-error "comparison" }
+// { dg-warning "address" "" { target *-*-* } 45 }
+void qr() { if ((int*)0 != &d) z(); } // { dg-error "comparison" }
+// { dg-warning "address" "" { target *-*-* } 47 }
+void rr() { if ((U*)0 != &s) z(); } // { dg-error "comparison" }
+// { dg-warning "address" "" { target *-*-* } 49 }