re PR c++/69850 (unnecessary -Wnonnull-compare warning)
authorJakub Jelinek <jakub@redhat.com>
Fri, 19 Feb 2016 16:02:51 +0000 (17:02 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Fri, 19 Feb 2016 16:02:51 +0000 (17:02 +0100)
PR c++/69850
* init.c (build_vec_delete_1): Set TREE_NO_WARNING on the NE_EXPR
condition.
* cp-gimplify.c (cp_fold): Propagate TREE_NO_WARNING from binary
operators if folding preserved the binop, just with different
arguments.

* g++.dg/warn/Wnonnull-compare-2.C: New test.
* g++.dg/warn/Wnonnull-compare-3.C: New test.

From-SVN: r233561

gcc/cp/ChangeLog
gcc/cp/cp-gimplify.c
gcc/cp/init.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/Wnonnull-compare-2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wnonnull-compare-3.C [new file with mode: 0644]

index 12a3967..dbc6741 100644 (file)
@@ -1,5 +1,12 @@
 2016-02-19  Jakub Jelinek  <jakub@redhat.com>
 
+       PR c++/69850
+       * init.c (build_vec_delete_1): Set TREE_NO_WARNING on the NE_EXPR
+       condition.
+       * cp-gimplify.c (cp_fold): Propagate TREE_NO_WARNING from binary
+       operators if folding preserved the binop, just with different
+       arguments.
+
        PR c++/67767
        * parser.c (cp_parser_std_attribute_spec_seq): Don't assume
        attr_spec is always single element chain, chain all the attributes
index d83e9de..34bdc82 100644 (file)
@@ -2068,6 +2068,9 @@ cp_fold (tree x)
       else
        x = fold (x);
 
+      if (TREE_NO_WARNING (org_x)
+         && TREE_CODE (x) == TREE_CODE (org_x))
+       TREE_NO_WARNING (x) = 1;
       break;
 
     case VEC_COND_EXPR:
index 68cc1a7..19279e3 100644 (file)
@@ -3678,12 +3678,15 @@ build_vec_delete_1 (tree base, tree maxindex, tree type,
     body = integer_zero_node;
 
   /* Outermost wrapper: If pointer is null, punt.  */
+  tree cond
+    = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, base,
+                      fold_convert (TREE_TYPE (base), nullptr_node));
+  /* This is a compiler generated comparison, don't emit
+     e.g. -Wnonnull-compare warning for it.  */
+  if (TREE_CODE (cond) == NE_EXPR)
+    TREE_NO_WARNING (cond) = 1;
   body = fold_build3_loc (input_location, COND_EXPR, void_type_node,
-                     fold_build2_loc (input_location,
-                                  NE_EXPR, boolean_type_node, base,
-                                  fold_convert (TREE_TYPE (base),
-                                                nullptr_node)),
-                     body, integer_zero_node);
+                         cond, body, integer_zero_node);
   body = build1 (NOP_EXPR, void_type_node, body);
 
   if (controller)
index f916c9c..c60de26 100644 (file)
@@ -1,5 +1,9 @@
 2016-02-19  Jakub Jelinek  <jakub@redhat.com>
 
+       PR c++/69850
+       * g++.dg/warn/Wnonnull-compare-2.C: New test.
+       * g++.dg/warn/Wnonnull-compare-3.C: New test.
+
        PR c++/67767
        * g++.dg/cpp0x/pr67767.C: New test.
 
diff --git a/gcc/testsuite/g++.dg/warn/Wnonnull-compare-2.C b/gcc/testsuite/g++.dg/warn/Wnonnull-compare-2.C
new file mode 100644 (file)
index 0000000..641bb67
--- /dev/null
@@ -0,0 +1,27 @@
+// PR c++/69850
+// { dg-do compile }
+// { dg-options "-Wnonnull-compare" }
+
+struct D {
+  virtual ~D ();
+  void foo () const { delete this; }   // { dg-bogus "nonnull argument" }
+  template <typename> friend struct A;
+};
+template <typename T> struct A {
+  static void bar (T *x) { x->foo (); }
+};
+template <typename T> struct B {
+  T b;
+  void baz () { A<T>::bar (&b); }
+};
+class C {
+  class E : public D { ~E (); };
+  void baz ();
+  B<E> c;
+};
+
+void
+C::baz ()
+{
+  c.baz ();
+}
diff --git a/gcc/testsuite/g++.dg/warn/Wnonnull-compare-3.C b/gcc/testsuite/g++.dg/warn/Wnonnull-compare-3.C
new file mode 100644 (file)
index 0000000..f909a0f
--- /dev/null
@@ -0,0 +1,28 @@
+// PR c++/69850
+// { dg-do compile }
+// { dg-options "-Wnonnull-compare" }
+
+template <typename T>
+struct A {
+  static void foo (T *x) { x->bar (); }
+};
+template <typename T>
+struct B {
+  T b;
+  void operator= (B) { A<T>::foo (&b); }
+};
+struct C {
+  void bar () { delete[] this; }       // { dg-bogus "nonnull argument" }
+};
+struct D { B<C> d; };
+struct G {
+  D g[6];
+  void baz ();
+};
+int a;
+
+void
+G::baz ()
+{
+  g[a] = g[1];
+}