diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index cfca62acaa4a..4de3a8489f1c 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -385,6 +385,8 @@ def err_destructor_with_params : Error<"destructor cannot have any parameters">; def err_destructor_variadic : Error<"destructor cannot be variadic">; def err_destructor_typedef_name : Error< "destructor cannot be declared using a typedef %0 of the class name">; +def err_destructor_name : Error< + "expected the class name after '~' to name the enclosing class">; // C++ initialization def err_lvalue_to_rvalue_ref : Error<"rvalue reference cannot bind to lvalue">; diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 36cb656966f5..989e08ee9dac 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -2456,6 +2456,16 @@ void Sema::CheckFunctionDeclaration(FunctionDecl *NewFD, NamedDecl *&PrevDecl, CheckConstructor(Constructor); } else if (isa(NewFD)) { CXXRecordDecl *Record = cast(NewFD->getParent()); + QualType ClassType = Context.getTypeDeclType(Record); + if (!ClassType->isDependentType()) { + ClassType = Context.getCanonicalType(ClassType); + DeclarationName Name + = Context.DeclarationNames.getCXXDestructorName(ClassType); + if (NewFD->getDeclName() != Name) { + Diag(NewFD->getLocation(), diag::err_destructor_name); + return NewFD->setInvalidDecl(); + } + } Record->setUserDeclaredDestructor(true); // C++ [class]p4: A POD-struct is an aggregate class that has [...] no // user-defined destructor. diff --git a/clang/test/SemaCXX/destructor.cpp b/clang/test/SemaCXX/destructor.cpp index e65a09713278..790a401ae99c 100644 --- a/clang/test/SemaCXX/destructor.cpp +++ b/clang/test/SemaCXX/destructor.cpp @@ -55,3 +55,9 @@ G::~G() { } struct H { ~H(void) { } }; + +struct X {}; + +struct Y { + ~X(); // expected-error {{expected the class name after '~' to name the enclosing class}} +};