Diagnose trying to delete a pointer to an abstract class with a non-virtual destructor. PR10504.

I'm not completely sure the standard allows us to reject this, but if it doesn't, it should. :)



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@136172 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index 20332dc..264f7a9 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3406,6 +3406,8 @@
 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;
diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp
index a3e3c11..86a4ecc 100644
--- a/lib/Sema/SemaExprCXX.cpp
+++ b/lib/Sema/SemaExprCXX.cpp
@@ -1913,6 +1913,18 @@
           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
@@ -1924,7 +1936,7 @@
       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;
       }
 
diff --git a/test/SemaCXX/new-delete.cpp b/test/SemaCXX/new-delete.cpp
index 850229c..7545b7c 100644
--- a/test/SemaCXX/new-delete.cpp
+++ b/test/SemaCXX/new-delete.cpp
@@ -409,3 +409,10 @@
   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}}
+}