r192345 - Sema: Taking the address of a dtor is illegal per C++ [class.dtor]p2.

Benjamin Kramer benny.kra at googlemail.com
Thu Oct 10 02:44:41 PDT 2013


Author: d0k
Date: Thu Oct 10 04:44:41 2013
New Revision: 192345

URL: http://llvm.org/viewvc/llvm-project?rev=192345&view=rev
Log:
Sema: Taking the address of a dtor is illegal per C++ [class.dtor]p2.

Emit a proper error instead of crashing in CodeGen. PR16892.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/SemaCXX/destructor.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=192345&r1=192344&r2=192345&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Oct 10 04:44:41 2013
@@ -4483,6 +4483,8 @@ def ext_typecheck_addrof_temporary : Ext
   InGroup<DiagGroup<"address-of-temporary">>, DefaultError;
 def err_typecheck_addrof_temporary : Error<
   "taking the address of a temporary object of type %0">;
+def err_typecheck_addrof_dtor : Error<
+  "taking the address of a destructor">;
 def err_typecheck_unary_expr : Error<
   "invalid argument type %0 to unary expression">;
 def err_typecheck_indirection_requires_pointer : Error<

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=192345&r1=192344&r2=192345&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Oct 10 04:44:41 2013
@@ -8709,6 +8709,10 @@ QualType Sema::CheckAddressOfOperand(Exp
       }
     }
 
+    // Taking the address of a dtor is illegal per C++ [class.dtor]p2.
+    if (isa<CXXDestructorDecl>(MD))
+      Diag(OpLoc, diag::err_typecheck_addrof_dtor) << op->getSourceRange();
+
     return Context.getMemberPointerType(op->getType(),
               Context.getTypeDeclType(MD->getParent()).getTypePtr());
   } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) {

Modified: cfe/trunk/test/SemaCXX/destructor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/destructor.cpp?rev=192345&r1=192344&r2=192345&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/destructor.cpp (original)
+++ cfe/trunk/test/SemaCXX/destructor.cpp Thu Oct 10 04:44:41 2013
@@ -363,3 +363,7 @@ namespace PR7900 {
     (&b)->~A(); // expected-error{{destructor type 'PR7900::A' in object destruction expression does not match the type 'PR7900::B' of the object being destroyed}}
   }
 }
+
+namespace PR16892 {
+  auto p = &A::~A; // expected-error{{taking the address of a destructor}}
+}





More information about the cfe-commits mailing list