From 752e7593b0f19af233a0b7e72daab8413662b605 Mon Sep 17 00:00:00 2001 From: Ville Voutilainen Date: Wed, 30 May 2018 22:33:38 +0300 Subject: [PATCH] Do not warn about zero-as-null when NULL is used. gcc/cp/ Do not warn about zero-as-null when NULL is used. * call.c (conversion_null_warnings): Check for pointer types converted from zero constants. (convert_like_real): Add a warning sentinel at the end. * tree.c (maybe_warn_zero_as_null_pointer_constant): Also check null_node_p. testsuite/ Do not warn about zero-as-null when NULL is used. * g++.dg/warn/Wzero-as-null-pointer-constant-7.C: New. From-SVN: r260973 --- gcc/cp/ChangeLog | 9 +++++++++ gcc/cp/call.c | 11 +++++++++++ gcc/cp/tree.c | 2 +- .../g++.dg/warn/Wzero-as-null-pointer-constant-7.C | 13 +++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-7.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index ec5ee7eb9dd..70554be6f87 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,12 @@ +2018-05-30 Ville Voutilainen + + Do not warn about zero-as-null when NULL is used. + * call.c (conversion_null_warnings): Check for pointer + types converted from zero constants. + (convert_like_real): Add a warning sentinel at the end. + * tree.c (maybe_warn_zero_as_null_pointer_constant): Also + check null_node_p. + 2018-05-24 Jason Merrill PR c++/85807 - ICE with call in template NSDMI. diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 67e404d1cb2..98319f98c7d 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -6517,6 +6517,7 @@ build_temp (tree expr, tree type, int flags, } /* Perform warnings about peculiar, but valid, conversions from/to NULL. + Also handle a subset of zero as null warnings. EXPR is implicitly converted to type TOTYPE. FN and ARGNUM are used for diagnostics. */ @@ -6551,6 +6552,15 @@ conversion_null_warnings (tree totype, tree expr, tree fn, int argnum) warning_at (input_location, OPT_Wconversion_null, "converting % to pointer type %qT", totype); } + /* Handle zero as null pointer warnings for cases other + than EQ_EXPR and NE_EXPR */ + else if (null_ptr_cst_p (expr) && + (TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype))) + { + source_location loc = + expansion_point_location_if_in_system_header (input_location); + maybe_warn_zero_as_null_pointer_constant (expr, loc); + } } /* We gave a diagnostic during a conversion. If this was in the second @@ -7101,6 +7111,7 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum, && !check_narrowing (totype, expr, complain)) return error_mark_node; + warning_sentinel w (warn_zero_as_null_pointer_constant); if (issue_conversion_warnings) expr = cp_convert_and_check (totype, expr, complain); else diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c index 4bb2879dc59..c5b6e9689b6 100644 --- a/gcc/cp/tree.c +++ b/gcc/cp/tree.c @@ -5432,7 +5432,7 @@ bool maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc) { if (c_inhibit_evaluation_warnings == 0 - && !NULLPTR_TYPE_P (TREE_TYPE (expr))) + && !null_node_p (expr) && !NULLPTR_TYPE_P (TREE_TYPE (expr))) { warning_at (loc, OPT_Wzero_as_null_pointer_constant, "zero as null pointer constant"); 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 00000000000..0d06dbf0ec7 --- /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 + +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" } +} -- 2.30.2