diff options
author | Fariborz Jahanian <fjahanian@apple.com> | 2009-07-21 15:28:50 +0000 |
---|---|---|
committer | Fariborz Jahanian <fjahanian@apple.com> | 2009-07-21 15:28:50 +0000 |
commit | c19f959d7fa5303f2fff5fa7a4968361cb7ef068 (patch) | |
tree | d7dfdd1fa0f5b0aa4a2191d4926d4b4481bf90b5 | |
parent | 741dd9a7e1d63e4e385b657e4ce11c5d96d44f72 (diff) |
Diagnose when a destructor uses a unrelated class type as its name.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@76577 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang/Basic/DiagnosticSemaKinds.td | 2 | ||||
-rw-r--r-- | lib/Sema/SemaDecl.cpp | 10 | ||||
-rw-r--r-- | test/SemaCXX/destructor.cpp | 6 |
3 files changed, 18 insertions, 0 deletions
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index cfca62acaa..4de3a8489f 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/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/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 36cb656966..989e08ee9d 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -2456,6 +2456,16 @@ void Sema::CheckFunctionDeclaration(FunctionDecl *NewFD, NamedDecl *&PrevDecl, CheckConstructor(Constructor); } else if (isa<CXXDestructorDecl>(NewFD)) { CXXRecordDecl *Record = cast<CXXRecordDecl>(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/test/SemaCXX/destructor.cpp b/test/SemaCXX/destructor.cpp index e65a097132..790a401ae9 100644 --- a/test/SemaCXX/destructor.cpp +++ b/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}} +}; |