... this is another option: use an 'integer_zero_or_nullptr_node (tree)' defined differently for C and C++. Certainly the whole thing becomes smaller.

Paolo.

////////////////
Index: c-family/c.opt
===================================================================
--- c-family/c.opt      (revision 180690)
+++ c-family/c.opt      (working copy)
@@ -685,6 +685,9 @@ 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
+
 ansi
 C ObjC C++ ObjC++
 A synonym for -std=c89 (for C) or -std=c++98 (for C++)
Index: c-family/c-common.c
===================================================================
--- c-family/c-common.c (revision 180690)
+++ c-family/c-common.c (working copy)
@@ -4093,7 +4093,8 @@ c_common_truthvalue_conversion (location_t locatio
       return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
     }
   else
-    return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
+    return build_binary_op (location, NE_EXPR, expr,
+                           integer_zero_or_nullptr_node (expr), 1);
 
  ret:
   protected_set_expr_location (expr, location);
Index: c-family/c-common.h
===================================================================
--- c-family/c-common.h (revision 180690)
+++ c-family/c-common.h (working copy)
@@ -889,6 +889,8 @@ extern tree common_type (tree, tree);
 
 extern tree decl_constant_value (tree);
 
+extern tree integer_zero_or_nullptr_node (tree);
+
 /* Handle increment and decrement of boolean types.  */
 extern tree boolean_increment (enum tree_code, tree);
 
Index: cp/typeck.c
===================================================================
--- cp/typeck.c (revision 180690)
+++ cp/typeck.c (working copy)
@@ -4058,7 +4058,9 @@ 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),
+                               NULLPTR_TYPE_P (TREE_TYPE (op1))
+                               ? nullptr_node : integer_zero_node);
            }
          result_type = TREE_TYPE (op0);
        }
@@ -4658,6 +4660,16 @@ build_x_unary_op (enum tree_code code, tree xarg,
   return exp;
 }
 
+/*
+ */
+tree
+integer_zero_or_nullptr_node (tree expr)
+{
+  tree type = TREE_TYPE (expr);
+  return (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type)
+         ? nullptr_node : integer_zero_node);
+}
+
 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
    constants, where a null value is represented by an INTEGER_CST of
    -1.  */
@@ -4668,7 +4680,7 @@ 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
     return c_common_truthvalue_conversion (input_location, expr);
 }
Index: cp/init.c
===================================================================
--- cp/init.c   (revision 180690)
+++ 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 180690)
+++ cp/cvt.c    (working copy)
@@ -198,6 +198,10 @@ cp_convert_to_pointer (tree type, tree expr)
 
   if (null_ptr_cst_p (expr))
     {
+      if (!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: c-typeck.c
===================================================================
--- c-typeck.c  (revision 180690)
+++ c-typeck.c  (working copy)
@@ -9510,6 +9510,15 @@ scalar_to_vector (location_t loc, enum tree_code c
 
   return stv_nothing;
 }
+
+/*
+ */
+tree
+integer_zero_or_nullptr_node (tree type ATTRIBUTE_UNUSED)
+{
+  return integer_zero_node;
+}
+
 
 /* Build a binary-operation expression without default conversions.
    CODE is the kind of expression to build.

Reply via email to