Warn if the class has virtual methods but non-virtual destructor. Addresses rdar://8756445.

llvm-svn: 124582
This commit is contained in:
Argyrios Kyrtzidis 2011-01-31 07:05:00 +00:00
parent f46cc65f44
commit 7f3986dc64
4 changed files with 41 additions and 2 deletions

View File

@ -80,7 +80,7 @@ def NullDereference : DiagGroup<"null-dereference">;
def InitializerOverrides : DiagGroup<"initializer-overrides">;
def NonNull : DiagGroup<"nonnull">;
def : DiagGroup<"nonportable-cfstrings">;
def : DiagGroup<"non-virtual-dtor">;
def NonVirtualDtor : DiagGroup<"non-virtual-dtor">;
def : DiagGroup<"old-style-cast">;
def : DiagGroup<"old-style-definition">;
def OutOfLineDeclaration : DiagGroup<"out-of-line-declaration">;

View File

@ -2756,6 +2756,9 @@ def err_bad_memptr_lhs : Error<
def warn_exception_caught_by_earlier_handler : Warning<
"exception of type %0 will be caught by earlier handler">;
def note_previous_exception_handler : Note<"for type %0">;
def warn_non_virtual_dtor : Warning<
"%0 has virtual functions but non-virtual destructor">,
InGroup<NonVirtualDtor>, DefaultIgnore;
def err_conditional_void_nonvoid : Error<
"%select{left|right}1 operand to ? is void, but %select{right|left}1 operand "

View File

@ -2769,6 +2769,14 @@ void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) {
}
}
}
// Warn if the class has virtual methods but non-virtual destructor.
if (Record->isDynamicClass()) {
CXXDestructorDecl *dtor = Record->getDestructor();
if (!(dtor && dtor->isVirtual()))
Diag(dtor ? dtor->getLocation() : Record->getLocation(),
diag::warn_non_virtual_dtor) << Context.getRecordType(Record);
}
}
void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc,

View File

@ -1,4 +1,4 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s
// RUN: %clang_cc1 -fsyntax-only -Wnon-virtual-dtor -verify %s
class A {
public:
~A();
@ -120,3 +120,31 @@ namespace test7 {
b->~B();
}
}
namespace nonvirtualdtor {
struct S1 { // expected-warning {{has virtual functions but non-virtual destructor}}
virtual void m();
};
struct S2 {
~S2(); // expected-warning {{has virtual functions but non-virtual destructor}}
virtual void m();
};
struct S3 : public S1 { // expected-warning {{has virtual functions but non-virtual destructor}}
virtual void m();
};
struct S4 : public S2 { // expected-warning {{has virtual functions but non-virtual destructor}}
virtual void m();
};
struct B {
virtual ~B();
virtual void m();
};
struct S5 : public B {
virtual void m();
};
}