forked from OSchip/llvm-project
Warn if the class has virtual methods but non-virtual destructor. Addresses rdar://8756445.
llvm-svn: 124582
This commit is contained in:
parent
f46cc65f44
commit
7f3986dc64
|
@ -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">;
|
||||
|
|
|
@ -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 "
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue