The attached patch should fix the TODO at lib/Sema/SemaExprCXX.cpp:1610

When compiling the following code

class Foo {
  private:
    ~Foo();
};

void do_foo() {
  Foo *f = new Foo;
  delete f;
}

clang now emits the error

testcase.cpp:8:10: error: calling a private destructor of class 'Foo *'
  delete f;
         ^
testcase.cpp:3:5: note: declared private here
    ~Foo();
    ^
1 error generated.

instead of compiling the code successfully.


If someone could point out how to turn the QualType instance representing
'Foo *' into just 'Foo', I'd appreciate it.

Thanks,
Alex Miller
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td	(revision 124005)
+++ include/clang/Basic/DiagnosticSemaKinds.td	(working copy)
@@ -559,6 +559,8 @@
 def err_access_ctor_field :
     Error<"field of type %1 has %select{private|protected}2 constructor">,
     NoSFINAE;
+def err_access_dtor : Error<
+  "calling a %select{private|protected}1 destructor of class %0">, NoSFINAE;
 def err_access_dtor_base :
     Error<"base class %0 has %select{private|protected}1 destructor">,
     NoSFINAE;
Index: lib/Sema/SemaExprCXX.cpp
===================================================================
--- lib/Sema/SemaExprCXX.cpp	(revision 124005)
+++ lib/Sema/SemaExprCXX.cpp	(working copy)
@@ -1607,7 +1607,15 @@
 
     MarkDeclarationReferenced(StartLoc, OperatorDelete);
 
-    // FIXME: Check access and ambiguity of operator delete and destructor.
+    // Check access and ambiguity of operator delete and destructor.
+    if (const RecordType *RT = PointeeElem->getAs<RecordType>()) {
+      CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
+        if (CXXDestructorDecl *Dtor = LookupDestructor(RD)) {
+            CheckDestructorAccess(Ex->getExprLoc(), Dtor, 
+                      PDiag(diag::err_access_dtor) << Type);
+        }
+    }
+
   }
 
   return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm,
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to