This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C++ PATCH] Some further -Wnonnull-compare fixes (PR c++/69850)


Hi!

As the testcases show, delete[] this; can also introduce artificial
comparison that -Wnonnull-compare incorrectly warns on, and the
folding can also drop TREE_NO_WARNING if optimizing the operands
of the comparison.
There is another case in rtti, but I'll need to still test a fix for that,
so will post it incrementally.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2016-02-18  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.

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

--- gcc/cp/init.c.jj	2016-02-17 23:26:29.000000000 +0100
+++ gcc/cp/init.c	2016-02-18 16:08:35.793958624 +0100
@@ -3678,12 +3678,15 @@ build_vec_delete_1 (tree base, tree maxi
     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)
--- gcc/cp/cp-gimplify.c.jj	2016-02-08 18:39:18.000000000 +0100
+++ gcc/cp/cp-gimplify.c	2016-02-18 11:20:08.491365580 +0100
@@ -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:
--- gcc/testsuite/g++.dg/warn/Wnonnull-compare-2.C.jj	2016-02-18 14:07:34.575846172 +0100
+++ gcc/testsuite/g++.dg/warn/Wnonnull-compare-2.C	2016-02-18 14:07:27.526942306 +0100
@@ -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 ();
+}
--- gcc/testsuite/g++.dg/warn/Wnonnull-compare-3.C.jj	2016-02-18 16:14:32.846084103 +0100
+++ gcc/testsuite/g++.dg/warn/Wnonnull-compare-3.C	2016-02-18 16:14:43.679936198 +0100
@@ -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];
+}

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]