PATCH: fix PR 15214

Tom Marshall tmarshall@real.com
Tue May 11 17:36:00 GMT 2004


From the PR:

  It seems to me that it is safe to have virtual functions and a non-virtual
  dtor if the dtor can only be invoked by methods within the class.  That
  is, the dtor must be private and the class must not declare any friends.

Andrew Pinski replied and suggested that non-public access (eg. protected)
is sufficient.  I don't think that is correct.  Consider the test code in
the PR.  Make the dtor protected and derive class B with its own protected
non-virtual dtor.  B::dtor will not be invoked from A::release.

PR 7302 was marked as a duplicate of PR 15214 and I also believe this to be
incorrect.  After more consideration, it seems to me that PR 7302 is not a
bug.  The fact that the class has only pure virtual functions and no members
is not a valid consideration.  The dtor may still do something useful (such
as poke some hardware, for example).  The only reason for the compiler to
avoid displaying the warning is by observing that the dtor is empty.  And if
the dtor is empty, why declare it in the first place?

I've attempted to submit this patch in compliance with contribute.html.
Unfortunately, it seems that I cannot convince my Debian Testing machine to
produce a successful "make -k check" for the GCC 3.4.0 release.  It fails in
boehm-gc.  I've tried the commandlines in contribute.html verbatim.  I've
tried installing after I ran "make bootstrap".  I've tried setting PATH and
LD_LIBRARY_PATH.  Nothing seems to work.  I would appreciate if someone
could tell me how to get the test to succeed, perform the test for me, or
just accept the patch as being Obviously Correct (tm).

Finally, please cc: me on any replies.  I am not subscribed to this list.

The author assigns the copyright for the following patch to the FSF, with
employer approval.  This is against GCC 3.4.0 release.

--- gcc/cp/class.c.orig 2004-03-08 23:27:23.000000000 -0800
+++ gcc/cp/class.c      2004-05-06 09:30:14.000000000 -0700
@@ -5102,7 +5102,17 @@
 
   if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
       && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
-    warning ("`%#T' has virtual functions but non-virtual destructor", t);
+    {
+      tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
+
+      /* Warn only if the dtor is non-private or the class has friends */
+      if (!TREE_PRIVATE (dtor) ||
+          (CLASSTYPE_FRIEND_CLASSES (t) ||
+           DECL_FRIENDLIST (TYPE_MAIN_DECL (t))))
+      {
+        warning ("%#T' has virtual functions but non-virtual destructor", t);
+      }
+    }
 
   complete_vars (t);
 

-- 
Never argue with an idiot.  They drag you down to their level, then beat you
with experience.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20040511/3b001d37/attachment.sig>


More information about the Gcc-patches mailing list