Commit 43f14744 by Pawel Sikora Committed by Jason Merrill

re PR c++/7302 (-Wnon-virtual-dtor should't complain of protected dtor)

        PR c++/7302
        * cp/class.c (finish_struct_1): Warn when a class has virtual
        functions and accessible non-virtual destructor.

        * doc/invoke.texi (-Wnon-virtual-dtor): Update documentation.

        * g++.dg/warn/Wnvdtor-2.C: New testcase.

From-SVN: r127649
parent 01a2a2f5
2007-08-20 Pawel Sikora <pluto@pld-linux.org>
* doc/invoke.texi (-Wnon-virtual-dtor): Update documentation.
2007-08-20 David Edelsohn <edelsohn@gnu.org> 2007-08-20 David Edelsohn <edelsohn@gnu.org>
* dwarf2out.c (text_section_used): Move declaration outside ifdef * dwarf2out.c (text_section_used): Move declaration outside ifdef
......
2007-08-20 Pawel Sikora <pluto@pld-linux.org>
PR c++/7302
* class.c (finish_struct_1): Warn when a class has virtual
functions and accessible non-virtual destructor.
2007-08-20 Richard Guenther <rguenther@suse.de> 2007-08-20 Richard Guenther <rguenther@suse.de>
PR c++/22369 PR c++/22369
......
...@@ -5120,17 +5120,19 @@ finish_struct_1 (tree t) ...@@ -5120,17 +5120,19 @@ finish_struct_1 (tree t)
tree dtor; tree dtor;
dtor = CLASSTYPE_DESTRUCTORS (t); dtor = CLASSTYPE_DESTRUCTORS (t);
/* Warn only if the dtor is non-private or the class has
friends. */
if (/* An implicitly declared destructor is always public. And, if (/* An implicitly declared destructor is always public. And,
if it were virtual, we would have created it by now. */ if it were virtual, we would have created it by now. */
!dtor !dtor
|| (!DECL_VINDEX (dtor) || (!DECL_VINDEX (dtor)
&& (!TREE_PRIVATE (dtor) && (/* public non-virtual */
|| CLASSTYPE_FRIEND_CLASSES (t) (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
|| DECL_FRIENDLIST (TYPE_MAIN_DECL (t))))) || (/* non-public non-virtual with friends */
warning (0, "%q#T has virtual functions but non-virtual destructor", (TREE_PRIVATE (dtor) || TREE_PROTECTED (dtor))
t); && (CLASSTYPE_FRIEND_CLASSES (t)
|| DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))))))
warning (OPT_Wnon_virtual_dtor,
"%q#T has virtual functions and accessible"
" non-virtual destructor", t);
} }
complete_vars (t); complete_vars (t);
......
...@@ -2011,9 +2011,10 @@ public static member functions. ...@@ -2011,9 +2011,10 @@ public static member functions.
@item -Wnon-virtual-dtor @r{(C++ only)} @item -Wnon-virtual-dtor @r{(C++ only)}
@opindex Wnon-virtual-dtor @opindex Wnon-virtual-dtor
Warn when a class appears to be polymorphic, thereby requiring a virtual Warn when a class has virtual functions and accessible non-virtual
destructor, yet it declares a non-virtual one. This warning is also destructor, in which case it would be possible but unsafe to delete
enabled if -Weffc++ is specified. an instance of a derived class through a pointer to the base class.
This warning is also enabled if -Weffc++ is specified.
@item -Wreorder @r{(C++ only)} @item -Wreorder @r{(C++ only)}
@opindex Wreorder @opindex Wreorder
......
2007-03-20 Pawel Sikora <pluto@pld-linux.org>
* g++.dg/warn/Wnvdtor-2.C: New testcase.
2007-08-20 Dorit Nuzman <dorit@il.ibm.com> 2007-08-20 Dorit Nuzman <dorit@il.ibm.com>
* gcc.dg/vect/costmodel/ppc/costmodel-vect-outer-fir.c: Add missing }. * gcc.dg/vect/costmodel/ppc/costmodel-vect-outer-fir.c: Add missing }.
// PR c++/7302
// { dg-do compile }
// { dg-options "-Wnon-virtual-dtor" }
// Warn when a class has virtual functions and accessible non-virtual
// destructor, in which case it would be possible but unsafe to delete
// an instance of a derived class through a pointer to the base class.
struct A
{ // { dg-bogus "non-virtual destructor" }
protected:
~A();
public:
virtual void f() = 0;
};
struct B
{ // { dg-bogus "non-virtual destructor" }
private:
~B();
public:
virtual void f() = 0;
};
struct C
{ // { dg-warning "non-virtual destructor" }
virtual void f() = 0;
};
struct D
{ // { dg-warning "non-virtual destructor" }
~D();
virtual void f() = 0;
};
struct E;
struct F
{ // { dg-warning "non-virtual destructor" }
protected:
friend class E;
~F();
public:
virtual void f() = 0;
};
struct G
{ // { dg-warning "non-virtual destructor" }
private:
friend class E;
~G();
public:
virtual void f() = 0;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment