def warn_delete_non_virtual_dtor : Warning<
"delete called on %0 that has virtual functions but non-virtual destructor">,
InGroup<DeleteNonVirtualDtor>, DefaultIgnore;
+def err_delete_abstract_non_virtual_dtor : Error<
+ "cannot delete %0, which is abstract and does not have a virtual destructor">;
def warn_overloaded_virtual : Warning<
"%q0 hides overloaded virtual %select{function|functions}1">,
InGroup<OverloadedVirtual>, DefaultIgnore;
DiagnoseUseOfDecl(Dtor, StartLoc);
}
+ // Deleting an abstract class with a non-virtual destructor is always
+ // undefined per [expr.delete]p3, and leads to strange-looking
+ // linker errors.
+ if (PointeeRD->isAbstract()) {
+ CXXDestructorDecl *dtor = PointeeRD->getDestructor();
+ if (dtor && !dtor->isVirtual()) {
+ Diag(StartLoc, diag::err_delete_abstract_non_virtual_dtor)
+ << PointeeElem;
+ return ExprError();
+ }
+ }
+
// C++ [expr.delete]p3:
// In the first alternative (delete object), if the static type of the
// object to be deleted is different from its dynamic type, the static
if (!ArrayForm && PointeeRD->isPolymorphic() &&
!PointeeRD->hasAttr<FinalAttr>()) {
CXXDestructorDecl *dtor = PointeeRD->getDestructor();
- if (!dtor || !dtor->isVirtual())
+ if (dtor && !dtor->isVirtual())
Diag(StartLoc, diag::warn_delete_non_virtual_dtor) << PointeeElem;
}
void f(A *x) { 1+delete x; } // expected-warning {{deleting pointer to incomplete type}} \
// expected-error {{invalid operands to binary expression}}
}
+
+namespace PR10504 {
+ struct A {
+ virtual void foo() = 0;
+ };
+ void f(A *x) { delete x; } // expected-error {{cannot delete 'PR10504::A', which is abstract and does not have a virtual destructor}}
+}