OSDN Git Service

/cp
authorpaolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 1 Nov 2011 23:28:19 +0000 (23:28 +0000)
committerpaolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 1 Nov 2011 23:28:19 +0000 (23:28 +0000)
2011-11-01  Paolo Carlini  <paolo.carlini@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.carlini@oracle.com>

PR c++/44277
* c.opt: Add Wzero-as-null-pointer-constant.

/gcc
2011-11-01  Paolo Carlini  <paolo.carlini@oracle.com>

PR c++/44277
* doc/invoke.texi: Document -Wzero-as-null-pointer-constant.

/testsuite
2011-11-01  Paolo Carlini  <paolo.carlini@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.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@180750 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/c-family/ChangeLog
gcc/c-family/c.opt
gcc/cp/ChangeLog
gcc/cp/cvt.c
gcc/cp/init.c
gcc/cp/typeck.c
gcc/doc/invoke.texi
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp0x/Wzero-as-null-pointer-constant-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-1.C [new file with mode: 0644]

index 771a4a2..9c75318 100644 (file)
@@ -1,3 +1,8 @@
+2011-11-01  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       PR c++/44277
+       * doc/invoke.texi: Document -Wzero-as-null-pointer-constant.
+
 2011-11-01  Andrew Stubbs  <ams@codesourcery.com>
 
        * config/arm/bpabi.h (BE8_LINK_SPEC): Recognize generic-armv7 tuning.
index 028163c..472ed28 100644 (file)
@@ -1,3 +1,8 @@
+2011-11-01  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       PR c++/44277
+       * c.opt: Add Wzero-as-null-pointer-constant.
+
 2011-10-31  Jason Merrill  <jason@redhat.com>
 
        * c.opt (-fdeduce-init-list): Off by default.
index 6d50097..438b8b0 100644 (file)
@@ -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 1b40250..f7328db 100644 (file)
@@ -1,3 +1,16 @@
+2011-11-01  Paolo Carlini  <paolo.carlini@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.
+
 2011-11-01  Jason Merrill  <jason@redhat.com>
 
        PR c++/50500
index 3e618d3..8570e3d 100644 (file)
@@ -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);
index 9115df3..6b57eb6 100644 (file)
@@ -176,6 +176,8 @@ build_zero_init_1 (tree type, tree nelts, bool static_storage_p,
        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 ec14934..8d70df5 100644 (file)
@@ -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, bool c_cast_p,
   /* 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 2836b74..b68f607 100644 (file)
@@ -4142,6 +4142,12 @@ unsigned integers are disabled by default in C++ unless
 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 a107932..bdbdb8e 100644 (file)
@@ -1,3 +1,9 @@
+2011-11-01  Paolo Carlini  <paolo.carlini@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.
+
 2011-11-01  Tom de Vries  <tom@codesourcery.com>
 
        PR tree-optimization/50908
diff --git a/gcc/testsuite/g++.dg/cpp0x/Wzero-as-null-pointer-constant-1.C b/gcc/testsuite/g++.dg/cpp0x/Wzero-as-null-pointer-constant-1.C
new file mode 100644 (file)
index 0000000..aad2737
--- /dev/null
@@ -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)
+    ;
+}
diff --git a/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-1.C b/gcc/testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-1.C
new file mode 100644 (file)
index 0000000..d0f62b2
--- /dev/null
@@ -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" }
+    ;
+}