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">;
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 "
}
}
}
+
+ // 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,
-// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -Wnon-virtual-dtor -verify %s
class A {
public:
~A();
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();
+};
+}