I smacked my head against conversion_null_warnings for a while, and then I realized that we could just stop convert_like_real from changing the node type for null_node.
Tested manually on Linux-x64, running full suite on Linux-PPC64, ok for trunk? 2018-05-25 Ville Voutilainen <ville.voutilai...@gmail.com> gcc/cp/ Do not warn about zero-as-null when NULL is used. * call.c (convert_like_real): Don't turn a null_node into integer_cst. * cvt.c (cp_convert_to_pointer): Don't warn about null_nodes. testsuite/ Do not warn about zero-as-null when NULL is used. * g++.dg/warn/Wzero-as-null-pointer-constant-7.C: New.
diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 7aadd64..cb07bb7 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -6799,12 +6799,6 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum, if (type_unknown_p (expr)) expr = instantiate_type (totype, expr, complain); - if (expr == null_node - && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype)) - /* If __null has been converted to an integer type, we do not want to - continue to warn about uses of EXPR as an integer, rather than as a - pointer. */ - expr = build_int_cst (totype, 0); return expr; case ck_ambig: /* We leave bad_p off ck_ambig because overload resolution considers diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c index f29dacd..36529f9 100644 --- a/gcc/cp/cvt.c +++ b/gcc/cp/cvt.c @@ -208,8 +208,8 @@ cp_convert_to_pointer (tree type, tree expr, bool dofold, return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0, /*c_cast_p=*/false, complain); - if (complain & tf_warning) - maybe_warn_zero_as_null_pointer_constant (expr, loc); + if (!null_node_p (expr) && (complain & tf_warning)) + maybe_warn_zero_as_null_pointer_constant (expr, loc); /* A NULL pointer-to-data-member is represented by -1, not by zero. */ diff --git a/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-7.C b/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-7.C new file mode 100644 index 0000000..0d06dbf --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-7.C @@ -0,0 +1,13 @@ +// { dg-options "-Wzero-as-null-pointer-constant" } +// { dg-do compile { target c++11 } } + +#include <cstddef> + +void test01() +{ + char* x(NULL); + char* x2{NULL}; + char* x3 = NULL; + char* x4(0); // { dg-warning "zero as null pointer" } + char* x5 = 0; // { dg-warning "zero as null pointer" } +}