]> granicus.if.org Git - clang/commitdiff
Diagnose when a destructor uses a unrelated class type as its name.
authorFariborz Jahanian <fjahanian@apple.com>
Tue, 21 Jul 2009 15:28:50 +0000 (15:28 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Tue, 21 Jul 2009 15:28:50 +0000 (15:28 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@76577 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDecl.cpp
test/SemaCXX/destructor.cpp

index cfca62acaa4ab747ff37c063a166bd6f6f6f2aa8..4de3a8489f1c3910397196fa60e073561f5feaae 100644 (file)
@@ -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">;
index 36cb656966f575d49b9e5417b3513b9a6c89aed8..989e08ee9dac941155fbdee46484c232aabc350a 100644 (file)
@@ -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.
index e65a09713278b62b71d4d0f84cd1d957618bc390..790a401ae99c4e496131afcc20bb606d8bcd595f 100644 (file)
@@ -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}}
+};