Hi,
On 08/05/2014 02:32 PM, Jason Merrill wrote:
On 08/05/2014 08:10 AM, Paolo Carlini wrote:
.. a clarification. As I tried to briefly explain yesterday, this kind
of change means that:
extern void z();
void il() { if (z != (void*)0) z(); }
doesn't trigger anymore the pedwarn at beginning of
composite_pointer_type about the comparison itself, for the simple
reason that we don't call it anymore.
So let's keep calling it when the RHS is also a pointer?
Indeed ;) Then I'm finishing testing the below.
Note: I also rearranged the conditionals, splitting out the
TYPE_PTRDATAMEM_P case, which was causing confusion in some cases: we
were feeding a TYPE_PTRDATAMEM_P and a TYPE_PTR_P to
composite_pointer_type, thus obtaining immediately verbose diagnostic,
instead of the expected clean one talking about invalid operands to
operator!=.
Thanks,
Paolo.
/////////////////////
Index: cp/typeck.c
===================================================================
--- cp/typeck.c (revision 213631)
+++ cp/typeck.c (working copy)
@@ -4353,13 +4353,22 @@ 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)
+ && (VOID_TYPE_P (TREE_TYPE (type1))
+ || comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (type0)),
+ TYPE_MAIN_VARIANT (TREE_TYPE (type1)),
+ COMPARE_BASE | COMPARE_DERIVED))))
{
+ 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 +4377,23 @@ 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)
+ && (VOID_TYPE_P (TREE_TYPE (type0))
+ || comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (type0)),
+ TYPE_MAIN_VARIANT (TREE_TYPE (type1)),
+ COMPARE_BASE | COMPARE_DERIVED))))
{
+ 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 +4402,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);
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 -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" }
+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|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" }
+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" }