From: Benjamin Kramer Date: Thu, 10 Oct 2013 09:44:41 +0000 (+0000) Subject: Sema: Taking the address of a dtor is illegal per C++ [class.dtor]p2. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6e04a849fec62c15968f8a1c94ac380f5eae7b99;p=clang Sema: Taking the address of a dtor is illegal per C++ [class.dtor]p2. Emit a proper error instead of crashing in CodeGen. PR16892. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@192345 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 868a5dd7d6..1e01da1991 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -4483,6 +4483,8 @@ def ext_typecheck_addrof_temporary : ExtWarn< InGroup>, 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< diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index db231e55d6..690661df1c 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -8709,6 +8709,10 @@ QualType Sema::CheckAddressOfOperand(ExprResult &OrigOp, SourceLocation OpLoc) { } } + // Taking the address of a dtor is illegal per C++ [class.dtor]p2. + if (isa(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) { diff --git a/test/SemaCXX/destructor.cpp b/test/SemaCXX/destructor.cpp index f3c6ab0b73..e511be0116 100644 --- a/test/SemaCXX/destructor.cpp +++ b/test/SemaCXX/destructor.cpp @@ -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}} +}