diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 34a0d5e8f07cc168487f6ff99b176aa89d8ef247..ec85f097c154e988018402fc62f4abcd4e92bc32 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2014-04-08 Nathan Sidwell <nathan@codesourcery.com> + + * doc/invoke (Wnon-virtual-dtor): Update to match implementation. + (Weffc++): Likewise. + 2014-04-07 Jan Hubicka <hubcika@ucw.cz> * ipa-devirt.c (maybe_record_node): When node is not recorded, diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 4a491cc022821c30e571e65a8442307e89b20cf8..60aa29f576e9f23d3baef8e1e35a9d1233148e6d 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2014-04-08 Nathan Sidwell <nathan@codesourcery.com> + + * class.c (check_bases_and_members): Warn about non-virtual dtors + in public bases only. Check warn_ecpp before complaining about + non-polymorphic bases. + 2014-04-04 Fabien Chêne <fabien@gcc.gnu.org> * decl.c (duplicate_decls): Check for the return of warning_at diff --git a/gcc/cp/class.c b/gcc/cp/class.c index 9d6d1260337ceb596845ed3a2a8bd93d4cc36e88..334bfd5eee7b54d969a2c4db09883ed6e7e3918f 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -5570,21 +5570,24 @@ check_bases_and_members (tree t) TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t); TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t); - /* Warn if a base of a polymorphic type has an accessible + /* Warn if a public base of a polymorphic type has an accessible non-virtual destructor. It is only now that we know the class is polymorphic. Although a polymorphic base will have a already been diagnosed during its definition, we warn on use too. */ if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor) { - tree binfo, base_binfo; + tree binfo = TYPE_BINFO (t); + vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo); + tree base_binfo; unsigned i; - for (binfo = TYPE_BINFO (t), i = 0; - BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) + for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) { tree basetype = TREE_TYPE (base_binfo); - if (accessible_nvdtor_p (basetype)) + if ((*accesses)[i] == access_public_node + && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp) + && accessible_nvdtor_p (basetype)) warning (OPT_Wnon_virtual_dtor, "base class %q#T has accessible non-virtual destructor", basetype); diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index cfc6d981237f1882d5371505319efc30dfab3d1a..f70f0d6e47cb250649f2bff834a1d504fa4c8bd6 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -2671,10 +2671,10 @@ the compiler to never throw an exception. @opindex Wnon-virtual-dtor @opindex Wno-non-virtual-dtor Warn when a class has virtual functions and an accessible non-virtual -destructor itself or in a base class, or has in which case it is -possible but unsafe to delete an instance of a derived class through a -pointer to the base class. This warning is automatically enabled if -@option{-Weffc++} is specified. +destructor itself or in an accessible polymorphic base class, in which +case it is possible but unsafe to delete an instance of a derived +class through a pointer to the class itself or base class. This +warning is automatically enabled if @option{-Weffc++} is specified. @item -Wreorder @r{(C++ and Objective-C++ only)} @opindex Wreorder @@ -2744,7 +2744,9 @@ Never overload @code{&&}, @code{||}, or @code{,}. @end itemize This option also enables @option{-Wnon-virtual-dtor}, which is also -one of the effective C++ recommendations. +one of the effective C++ recommendations. However, the check is +extended to warn about the lack of virtual destructor in accessible +non-polymorphic bases classes too. When selecting this option, be aware that the standard library headers do not obey all of these guidelines; use @samp{grep -v} diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 16f23c570e6dec01fd777da765438fe429c0fe25..04d964a6e974e045c1ed19904ab7ce784d2dd208 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2014-04-08 Nathan Sidwell <nathan@codesourcery.com> + + * g++.dg/warn/Wnvdtor-2.C: Add more cases. + * g++.dg/warn/Wnvdtor-3.C: Likewise. + * g++.dg/warn/Wnvdtor-4.C: Likewise. + 2014-04-07 Eric Botcazou <ebotcazou@adacore.com> * gnat.dg/test_raise_from_pure.adb: UnXFAIL for ARM. diff --git a/gcc/testsuite/g++.dg/warn/Wnvdtor-2.C b/gcc/testsuite/g++.dg/warn/Wnvdtor-2.C index de7c74bdfafb82936e1a9b0893264cfec8fae2db..9f2e4bea43b51959a7d23b4fce8a8fbf600bed7c 100644 --- a/gcc/testsuite/g++.dg/warn/Wnvdtor-2.C +++ b/gcc/testsuite/g++.dg/warn/Wnvdtor-2.C @@ -54,4 +54,23 @@ public: }; struct H {}; -struct I : H {}; + +struct I1 : H +{}; +struct I2 : private H +{}; + +struct J1 : H +{ virtual ~J1 ();}; +struct J2 : private H +{ virtual ~J2 ();}; + +struct K // { dg-warning "accessible non-virtual destructor" } +{ + virtual void k (); +}; + +struct L1 : K // { dg-warning "accessible non-virtual destructor" } +{virtual ~L1 ();}; +struct L2 : private K +{virtual ~L2 ();}; diff --git a/gcc/testsuite/g++.dg/warn/Wnvdtor-3.C b/gcc/testsuite/g++.dg/warn/Wnvdtor-3.C index 8ec81542ce28f301a212b39f863392d8dc7a3715..e83134b062bf83a44b3e057b8207359b7a15f3d7 100644 --- a/gcc/testsuite/g++.dg/warn/Wnvdtor-3.C +++ b/gcc/testsuite/g++.dg/warn/Wnvdtor-3.C @@ -53,4 +53,23 @@ public: }; struct H {}; -struct I : H {}; + +struct I1 : H +{}; +struct I2 : private H +{}; + +struct J1 : H // { dg-warning "accessible non-virtual destructor" } +{ virtual ~J1 ();}; +struct J2 : private H +{ virtual ~J2 ();}; + +struct K // { dg-warning "accessible non-virtual destructor" } +{ + virtual void k (); +}; + +struct L1 : K // { dg-warning "accessible non-virtual destructor" } +{virtual ~L1 ();}; +struct L2 : private K +{virtual ~L2 ();}; diff --git a/gcc/testsuite/g++.dg/warn/Wnvdtor-4.C b/gcc/testsuite/g++.dg/warn/Wnvdtor-4.C index f63ffdc07e6652ae8f544e500c1d56740a4a3051..dd6d9d7689ea175737c6c2c52d4b3663fc58a210 100644 --- a/gcc/testsuite/g++.dg/warn/Wnvdtor-4.C +++ b/gcc/testsuite/g++.dg/warn/Wnvdtor-4.C @@ -53,4 +53,23 @@ public: }; struct H {}; -struct I : H {}; + +struct I1 : H +{}; +struct I2 : private H +{}; + +struct J1 : H +{ virtual ~J1 ();}; +struct J2 : private H +{ virtual ~J2 ();}; + +struct K +{ + virtual void k (); +}; + +struct L1 : K +{virtual ~L1 ();}; +struct L2 : private K +{virtual ~L2 ();};