Hi,

more spurious -Wzero-as-null.. warnings, this one is about dynamic_casts, which I completely overlooked. In order to fix the original testcase, which involves pointers, it's enough to use nullptr_node in ifnnonnull; in order to fix a version I added for references, we have to use here too the c_inhibit_evaluation_warnings trick, because result in that case, a SAVE_EXPR, is of type POINTER_TYPE.

Tested x86_64-linux, Ok?

Thanks,
Paolo.

/////////////////
/cp
2011-11-25  Paolo Carlini  <paolo.carl...@oracle.com>

        PR c++/51299
        * rtti.c (ifnonnull): Use nullptr_node.
        (build_dynamic_cast_1): Inhibit evaluation warnings for the
        c_common_truthvalue_conversion call.

/testsuite
2011-11-25  Paolo Carlini  <paolo.carl...@oracle.com>

        PR c++/51299
        * g++.dg/warn/Wzero-as-null-pointer-constant-4.C: New.

Index: testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-4.C
===================================================================
--- testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-4.C    (revision 0)
+++ testsuite/g++.dg/warn/Wzero-as-null-pointer-constant-4.C    (revision 0)
@@ -0,0 +1,22 @@
+// PR c++/51299
+// { dg-options "-Wzero-as-null-pointer-constant" }
+
+class Base
+{
+  public:
+  virtual ~Base();
+};
+
+class Derived : public Base
+{
+};
+
+void foo(Base* b)
+{
+  Derived* d = dynamic_cast<Derived*>(b);
+}
+
+void bar(Base& b)
+{
+  Derived& d = dynamic_cast<Derived&>(b);
+}
Index: cp/rtti.c
===================================================================
--- cp/rtti.c   (revision 181706)
+++ cp/rtti.c   (working copy)
@@ -503,8 +503,8 @@ ifnonnull (tree test, tree result)
 {
   return build3 (COND_EXPR, TREE_TYPE (result),
                 build2 (EQ_EXPR, boolean_type_node, test,
-                        cp_convert (TREE_TYPE (test), integer_zero_node)),
-                cp_convert (TREE_TYPE (result), integer_zero_node),
+                        cp_convert (TREE_TYPE (test), nullptr_node)),
+                cp_convert (TREE_TYPE (result), nullptr_node),
                 result);
 }
 
@@ -747,7 +747,10 @@ build_dynamic_cast_1 (tree type, tree expr, tsubst
              tree neq;
 
              result = save_expr (result);
+             /* Avoid -Wzero-as-null-pointer-constant warnings.  */
+             ++c_inhibit_evaluation_warnings;
              neq = c_common_truthvalue_conversion (input_location, result);
+             --c_inhibit_evaluation_warnings;
              return cp_convert (type,
                                 build3 (COND_EXPR, TREE_TYPE (result),
                                         neq, result, bad));

Reply via email to