Hi,
this is what I finished testing and I'm submitting for review. In the
meanwhile I found another place, in build_ptrmemfunc, which requires
internally using nullptr_node, otherwise we have a duplicate diagnostics
for member function pointers; also, simplified a tad the
cp_build_binary_op bits, to just forward op1 (known to be a
null_ptr_cst_p at that point). Tested x86_86-linux.
Ok?
Thanks,
Paolo.
//////////////////////
/cp
2011-11-01 Paolo Carlini <paolo.carl...@oracle.com>
PR c++/44277
* cvt.c (cp_convert_to_pointer): Warn for zero as null pointer
constant.
* typeck.c (cp_truthvalue_conversion): Handle pointers and member
function pointers under c_inhibit_evaluation_warnings; use
nullptr_node for data member pointers.
(cp_build_binary_op): Tweak, just forward to cp_convert op1,
either a nullptr_node or an integer_zero_node.
(build_ptrmemfunc): Use nullptr_node.
* init.c (build_zero_init_1): Likewise.
/c-family
2011-11-01 Paolo Carlini <paolo.carl...@oracle.com>
PR c++/44277
* c.opt: Add Wzero-as-null-pointer-constant.
/gcc
2011-11-01 Paolo Carlini <paolo.carl...@oracle.com>
PR c++/44277
* doc/invoke.texi: Document -Wzero-as-null-pointer-constant.
/testsuite
2011-11-01 Paolo Carlini <paolo.carl...@oracle.com>
PR c++/44277
* g++.dg/warn/Wzero-as-null-pointer-constant-1.C: New.
* g++.dg/cpp0x/Wzero-as-null-pointer-constant-1.C: Likewise.
Index: doc/invoke.texi
===================================================================
--- doc/invoke.texi (revision 180733)
+++ doc/invoke.texi (working copy)
@@ -4142,6 +4142,12 @@ unsigned integers are disabled by default in C++ u
Do not warn for conversions between @code{NULL} and non-pointer
types. @option{-Wconversion-null} is enabled by default.
+@item -Wzero-as-null-pointer-constant @r{(C++ and Objective-C++ only)}
+@opindex Wzero-as-null-pointer-constant
+@opindex Wno-zero-as-null-pointer-constant
+Warn when a literal '0' is used as null pointer constant. This can
+be useful to facilitate the conversion to @code{nullptr} in C++11.
+
@item -Wempty-body
@opindex Wempty-body
@opindex Wno-empty-body
Index: c-family/c.opt
===================================================================
--- c-family/c.opt (revision 180733)
+++ c-family/c.opt (working copy)
@@ -689,6 +689,10 @@ Wpointer-sign
C ObjC Var(warn_pointer_sign) Init(-1) Warning
Warn when a pointer differs in signedness in an assignment
+Wzero-as-null-pointer-constant
+C++ ObjC++ Var(warn_zero_as_null_pointer_constant) Warning
+Warn when a literal '0' is used as null pointer
+
ansi
C ObjC C++ ObjC++
A synonym for -std=c89 (for C) or -std=c++98 (for C++)
Index: testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-1.C
===================================================================
--- testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-1.C (revision 0)
+++ testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-1.C (revision 0)
@@ -0,0 +1,100 @@
+// { dg-options "-Wzero-as-null-pointer-constant" }
+
+struct A;
+
+typedef int (A::*pointmemfun) (int);
+typedef int (A::*pointdmem);
+typedef int (*pointfun) (int);
+
+pointmemfun pmfs;
+pointdmem pdms;
+pointfun pfs;
+int* ps;
+
+void f()
+{
+ pointmemfun pmf(0); // { dg-warning "zero as null pointer" }
+ pointdmem pdm(0); // { dg-warning "zero as null pointer" }
+ pointfun pf(0); // { dg-warning "zero as null pointer" }
+ int* p(0); // { dg-warning "zero as null pointer" }
+
+ pmf = 0; // { dg-warning "zero as null pointer" }
+
+ pdm = 0; // { dg-warning "zero as null pointer" }
+
+ pf = 0; // { dg-warning "zero as null pointer" }
+
+ p = 0; // { dg-warning "zero as null pointer" }
+
+ if (pmf)
+ ;
+
+ if (pdm)
+ ;
+
+ if (pf)
+ ;
+
+ if (p)
+ ;
+
+ if (!pmf)
+ ;
+
+ if (!pdm)
+ ;
+
+ if (!pf)
+ ;
+
+ if (!p)
+ ;
+
+ if (pmf == 0) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (pdm == 0) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (pf == 0) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (p == 0) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (0 == pmf) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (0 == pdm) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (0 == pf) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (0 == p) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (pmf != 0) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (pdm != 0) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (pf != 0) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (p != 0) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (0 != pmf) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (0 != pdm) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (0 != pf) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (0 != p) // { dg-warning "zero as null pointer" }
+ ;
+}
Index: testsuite/g++.dg/cpp0x/Wzero-as-null-pointer-constant-1.C
===================================================================
--- testsuite/g++.dg/cpp0x/Wzero-as-null-pointer-constant-1.C (revision 0)
+++ testsuite/g++.dg/cpp0x/Wzero-as-null-pointer-constant-1.C (revision 0)
@@ -0,0 +1,161 @@
+// { dg-options "-std=c++0x -Wzero-as-null-pointer-constant" }
+
+struct A;
+
+typedef int (A::*pointmemfun) (int);
+typedef int (A::*pointdmem);
+typedef int (*pointfun) (int);
+
+pointmemfun pmfs;
+pointdmem pdms;
+pointfun pfs;
+int* ps;
+
+void f()
+{
+ pointmemfun pmf(0); // { dg-warning "zero as null pointer" }
+ pointdmem pdm(0); // { dg-warning "zero as null pointer" }
+ pointfun pf(0); // { dg-warning "zero as null pointer" }
+ int* p(0); // { dg-warning "zero as null pointer" }
+
+ pointmemfun pmfn(nullptr);
+ pointdmem pdmn(nullptr);
+ pointfun pfn(nullptr);
+ int* pn(nullptr);
+
+ pmf = 0; // { dg-warning "zero as null pointer" }
+
+ pdm = 0; // { dg-warning "zero as null pointer" }
+
+ pf = 0; // { dg-warning "zero as null pointer" }
+
+ p = 0; // { dg-warning "zero as null pointer" }
+
+ pmf = nullptr;
+
+ pdm = nullptr;
+
+ pf = nullptr;
+
+ p = nullptr;
+
+ if (pmf)
+ ;
+
+ if (pdm)
+ ;
+
+ if (pf)
+ ;
+
+ if (p)
+ ;
+
+ if (!pmf)
+ ;
+
+ if (!pdm)
+ ;
+
+ if (!pf)
+ ;
+
+ if (!p)
+ ;
+
+ if (pmf == 0) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (pdm == 0) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (pf == 0) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (p == 0) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (0 == pmf) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (0 == pdm) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (0 == pf) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (0 == p) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (pmf != 0) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (pdm != 0) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (pf != 0) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (p != 0) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (0 != pmf) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (0 != pdm) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (0 != pf) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (0 != p) // { dg-warning "zero as null pointer" }
+ ;
+
+ if (pmf == nullptr)
+ ;
+
+ if (pdm == nullptr)
+ ;
+
+ if (pf == nullptr)
+ ;
+
+ if (p == nullptr)
+ ;
+
+ if (nullptr == pmf)
+ ;
+
+ if (nullptr == pdm)
+ ;
+
+ if (nullptr == pf)
+ ;
+
+ if (nullptr == p)
+ ;
+
+ if (pmf != nullptr)
+ ;
+
+ if (pdm != nullptr)
+ ;
+
+ if (pf != nullptr)
+ ;
+
+ if (p != nullptr)
+ ;
+
+ if (nullptr != pmf)
+ ;
+
+ if (nullptr != pdm)
+ ;
+
+ if (nullptr != pf)
+ ;
+
+ if (nullptr != p)
+ ;
+}
Index: cp/typeck.c
===================================================================
--- cp/typeck.c (revision 180733)
+++ cp/typeck.c (working copy)
@@ -4058,7 +4058,7 @@ cp_build_binary_op (location_t location,
else
{
op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
- op1 = cp_convert (TREE_TYPE (op0), integer_zero_node);
+ op1 = cp_convert (TREE_TYPE (op0), op1);
}
result_type = TREE_TYPE (op0);
}
@@ -4668,7 +4668,17 @@ cp_truthvalue_conversion (tree expr)
tree type = TREE_TYPE (expr);
if (TYPE_PTRMEM_P (type))
return build_binary_op (EXPR_LOCATION (expr),
- NE_EXPR, expr, integer_zero_node, 1);
+ NE_EXPR, expr, nullptr_node, 1);
+ else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
+ {
+ /* With -Wzero-as-null-pointer-constant do not warn for an
+ 'if (p)' or a 'while (!p)', where p is a pointer. */
+ tree ret;
+ ++c_inhibit_evaluation_warnings;
+ ret = c_common_truthvalue_conversion (input_location, expr);
+ --c_inhibit_evaluation_warnings;
+ return ret;
+ }
else
return c_common_truthvalue_conversion (input_location, expr);
}
@@ -7148,7 +7158,7 @@ build_ptrmemfunc (tree type, tree pfn, int force,
/* Handle null pointer to member function conversions. */
if (null_ptr_cst_p (pfn))
{
- pfn = build_c_cast (input_location, type, integer_zero_node);
+ pfn = build_c_cast (input_location, type, nullptr_node);
return build_ptrmemfunc1 (to_type,
integer_zero_node,
pfn);
Index: cp/init.c
===================================================================
--- cp/init.c (revision 180733)
+++ cp/init.c (working copy)
@@ -176,6 +176,8 @@ build_zero_init_1 (tree type, tree nelts, bool sta
items with static storage duration that are not otherwise
initialized are initialized to zero. */
;
+ else if (TYPE_PTR_P (type) || TYPE_PTR_TO_MEMBER_P (type))
+ init = convert (type, nullptr_node);
else if (SCALAR_TYPE_P (type))
init = convert (type, integer_zero_node);
else if (CLASS_TYPE_P (type))
Index: cp/cvt.c
===================================================================
--- cp/cvt.c (revision 180733)
+++ cp/cvt.c (working copy)
@@ -198,6 +198,11 @@ cp_convert_to_pointer (tree type, tree expr)
if (null_ptr_cst_p (expr))
{
+ if (c_inhibit_evaluation_warnings == 0
+ && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
+ warning (OPT_Wzero_as_null_pointer_constant,
+ "zero as null pointer constant");
+
if (TYPE_PTRMEMFUNC_P (type))
return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
/*c_cast_p=*/false, tf_warning_or_error);