Commit 30b07d03 by Paolo Carlini Committed by Paolo Carlini

re PR c++/56373 (-Wzero-as-null-pointer-constant: does not catch issues with smart pointers)

/cp
2013-02-20  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/56373
	* tree.c (maybe_warn_zero_as_null_pointer_constant): Add.
	* cvt.c (ocp_convert): Use the latter.
	(cp_convert_to_pointer): Likewise.
	* decl.c (check_default_argument): Likewise.
	* typeck.c (cp_build_binary_op): Likewise.
	* cp-tree.h (maybe_warn_zero_as_null_pointer_constant): Declare.

/testsuite
2013-02-20  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/56373
	* g++.dg/cpp0x/Wzero-as-null-pointer-constant-2.C: New.

From-SVN: r196165
parent bbe4fb2c
2013-02-20 Paolo Carlini <paolo.carlini@oracle.com>
PR c++/56373
* tree.c (maybe_warn_zero_as_null_pointer_constant): Add.
* cvt.c (ocp_convert): Use the latter.
(cp_convert_to_pointer): Likewise.
* decl.c (check_default_argument): Likewise.
* typeck.c (cp_build_binary_op): Likewise.
* cp-tree.h (maybe_warn_zero_as_null_pointer_constant): Declare.
2013-02-15 Jonathan Wakely <jwakely.gcc@gmail.com> 2013-02-15 Jonathan Wakely <jwakely.gcc@gmail.com>
Paolo Carlini <paolo.carlini@oracle.com> Paolo Carlini <paolo.carlini@oracle.com>
......
...@@ -5834,6 +5834,7 @@ extern bool cast_valid_in_integral_constant_expression_p (tree); ...@@ -5834,6 +5834,7 @@ extern bool cast_valid_in_integral_constant_expression_p (tree);
extern bool cxx_type_hash_eq (const_tree, const_tree); extern bool cxx_type_hash_eq (const_tree, const_tree);
extern void cxx_print_statistics (void); extern void cxx_print_statistics (void);
extern bool maybe_warn_zero_as_null_pointer_constant (tree, location_t);
/* in ptree.c */ /* in ptree.c */
extern void cxx_print_xnode (FILE *, tree, int); extern void cxx_print_xnode (FILE *, tree, int);
......
...@@ -203,11 +203,8 @@ cp_convert_to_pointer (tree type, tree expr, tsubst_flags_t complain) ...@@ -203,11 +203,8 @@ cp_convert_to_pointer (tree type, tree expr, tsubst_flags_t complain)
if (null_ptr_cst_p (expr)) if (null_ptr_cst_p (expr))
{ {
if ((complain & tf_warning) if (complain & tf_warning)
&& c_inhibit_evaluation_warnings == 0 maybe_warn_zero_as_null_pointer_constant (expr, loc);
&& !NULLPTR_TYPE_P (TREE_TYPE (expr)))
warning_at (loc, OPT_Wzero_as_null_pointer_constant,
"zero as null pointer constant");
if (TYPE_PTRMEMFUNC_P (type)) if (TYPE_PTRMEMFUNC_P (type))
return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0, return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
...@@ -783,7 +780,11 @@ ocp_convert (tree type, tree expr, int convtype, int flags, ...@@ -783,7 +780,11 @@ ocp_convert (tree type, tree expr, int convtype, int flags,
return ignore_overflows (converted, e); return ignore_overflows (converted, e);
} }
if (NULLPTR_TYPE_P (type) && e && null_ptr_cst_p (e)) if (NULLPTR_TYPE_P (type) && e && null_ptr_cst_p (e))
return nullptr_node; {
if (complain & tf_warning)
maybe_warn_zero_as_null_pointer_constant (e, loc);
return nullptr_node;
}
if (POINTER_TYPE_P (type) || TYPE_PTRMEM_P (type)) if (POINTER_TYPE_P (type) || TYPE_PTRMEM_P (type))
return fold_if_not_in_template (cp_convert_to_pointer (type, e, complain)); return fold_if_not_in_template (cp_convert_to_pointer (type, e, complain));
if (code == VECTOR_TYPE) if (code == VECTOR_TYPE)
......
...@@ -10861,15 +10861,10 @@ check_default_argument (tree decl, tree arg) ...@@ -10861,15 +10861,10 @@ check_default_argument (tree decl, tree arg)
--cp_unevaluated_operand; --cp_unevaluated_operand;
if (warn_zero_as_null_pointer_constant if (warn_zero_as_null_pointer_constant
&& c_inhibit_evaluation_warnings == 0
&& TYPE_PTR_OR_PTRMEM_P (decl_type) && TYPE_PTR_OR_PTRMEM_P (decl_type)
&& null_ptr_cst_p (arg) && null_ptr_cst_p (arg)
&& !NULLPTR_TYPE_P (TREE_TYPE (arg))) && maybe_warn_zero_as_null_pointer_constant (arg, input_location))
{ return nullptr_node;
warning (OPT_Wzero_as_null_pointer_constant,
"zero as null pointer constant");
return nullptr_node;
}
/* [dcl.fct.default] /* [dcl.fct.default]
......
...@@ -3939,6 +3939,21 @@ cp_tree_operand_length (const_tree t) ...@@ -3939,6 +3939,21 @@ cp_tree_operand_length (const_tree t)
return TREE_OPERAND_LENGTH (t); return TREE_OPERAND_LENGTH (t);
} }
} }
/* Implement -Wzero_as_null_pointer_constant. Return true if the
conditions for the warning hold, false otherwise. */
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)))
{
warning_at (loc, OPT_Wzero_as_null_pointer_constant,
"zero as null pointer constant");
return true;
}
return false;
}
#if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007) #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
/* Complain that some language-specific thing hanging off a tree /* Complain that some language-specific thing hanging off a tree
......
...@@ -4293,12 +4293,9 @@ cp_build_binary_op (location_t location, ...@@ -4293,12 +4293,9 @@ cp_build_binary_op (location_t location,
delta0, delta0,
integer_one_node, integer_one_node,
complain); complain);
if ((complain & tf_warning) if (complain & tf_warning)
&& c_inhibit_evaluation_warnings == 0 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
&& !NULLPTR_TYPE_P (TREE_TYPE (op1)))
warning (OPT_Wzero_as_null_pointer_constant,
"zero as null pointer constant");
e2 = cp_build_binary_op (location, e2 = cp_build_binary_op (location,
EQ_EXPR, e2, integer_zero_node, EQ_EXPR, e2, integer_zero_node,
......
2013-02-20 Paolo Carlini <paolo.carlini@oracle.com>
PR c++/56373
* g++.dg/cpp0x/Wzero-as-null-pointer-constant-2.C: New.
2013-02-19 Richard Biener <rguenther@suse.de> 2013-02-19 Richard Biener <rguenther@suse.de>
PR tree-optimization/56384 PR tree-optimization/56384
......
// PR c++/56373
// { dg-options "-std=c++11 -Wzero-as-null-pointer-constant" }
struct shared_ptr
{
shared_ptr(decltype(nullptr));
};
void f()
{
shared_ptr a = 0; // { dg-warning "zero as null pointer" }
shared_ptr b(0); // { dg-warning "zero as null pointer" }
shared_ptr c{0}; // { dg-warning "zero as null pointer" }
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment