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]

[patch] fix c++/49605: -Wdelete-non-virtual-dtor is not picky enough


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49605

There's a spurious warning because I didn't realise that build_delete
is used for destroying automatic variables as well as for delete
expression, this fixes it by checking for sfk_deleting_destructor, is
that the right thing to do?

Tested x86_64-linux, ok for trunk?

Or would it be better to move this warning further down, with these
other things that only apply to sfk_deleting_destrutor?

      if (use_global_delete && auto_delete == sfk_deleting_destructor)
        ...
      else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type))
               && auto_delete == sfk_deleting_destructor)
        ...
      else if (auto_delete == sfk_deleting_destructor
               && TYPE_GETS_REG_DELETE (type))


cp/ChangeLog

        PR c++/49065
        * init.c (build_delete): Only warn for sfk_deleting_destructor.

testsuite/ChangeLog

        PR c++/49065
        * g++.dg/warn/delete-non-virtual-dtor.C: Adjust.
Index: cp/init.c
===================================================================
--- cp/init.c	(revision 175752)
+++ cp/init.c	(working copy)
@@ -3467,8 +3467,9 @@
 		}
 	      complete_p = false;
 	    }
-	  else if (warn_delnonvdtor && MAYBE_CLASS_TYPE_P (type)
-		   && !CLASSTYPE_FINAL (type) && TYPE_POLYMORPHIC_P (type))
+	  else if (auto_delete == sfk_deleting_destructor && warn_delnonvdtor
+	           && MAYBE_CLASS_TYPE_P (type) && !CLASSTYPE_FINAL (type)
+		   && TYPE_POLYMORPHIC_P (type))
 	    {
 	      tree dtor;
 	      dtor = CLASSTYPE_DESTRUCTORS (type);
Index: testsuite/g++.dg/warn/delete-non-virtual-dtor.C
===================================================================
--- testsuite/g++.dg/warn/delete-non-virtual-dtor.C	(revision 175752)
+++ testsuite/g++.dg/warn/delete-non-virtual-dtor.C	(working copy)
@@ -5,6 +5,7 @@
 
 void f(polyBase* p, polyBase* arr)
 {
+  polyBase pb;
   delete p;      // { dg-warning "non-virtual destructor might" }
   delete [] arr;
 }
@@ -13,6 +14,7 @@
 
 void f(polyDerived* p, polyDerived* arr)
 {
+  polyDerived pd;
   delete p;      // { dg-warning "non-virtual destructor might" }
   delete [] arr;
 }
@@ -29,6 +31,7 @@
 
 void f(finalDerived* p, finalDerived* arr)
 {
+  finalDerived fd;
   delete p;      // no error for final classes
   delete [] arr;
 }
@@ -38,7 +41,26 @@
 
 void f(safeDerived* p, safeDerived* arr)
 {
+  safeDerived sd;
   delete p;      // no error because base has virtual dtor
   delete [] arr;
 }
 
+struct polyBaseNonTrivial { ~polyBaseNonTrivial(); virtual void f(); };
+
+void f(polyBaseNonTrivial* p, polyBaseNonTrivial* arr)
+{
+  polyBaseNonTrivial pbnt;
+  delete p;      // { dg-warning "non-virtual destructor might" }
+  delete [] arr;
+}
+
+struct polyDerivedNT : polyBaseNonTrivial { ~polyDerivedNT(); };
+
+void f(polyDerivedNT* p, polyDerivedNT* arr)
+{
+  polyDerivedNT pdnt;
+  delete p;      // { dg-warning "non-virtual destructor might" }
+  delete [] arr;
+}
+

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