[cfe-commits] r95078 - in /cfe/trunk: lib/Sema/Sema.h lib/Sema/SemaAccess.cpp lib/Sema/SemaChecking.cpp lib/Sema/SemaDeclCXX.cpp test/CXX/class.access/p4.cpp

John McCall rjmccall at apple.com
Tue Feb 2 00:45:54 PST 2010


Author: rjmccall
Date: Tue Feb  2 02:45:54 2010
New Revision: 95078

URL: http://llvm.org/viewvc/llvm-project?rev=95078&view=rev
Log:
Access control for implicit destructor calls.  Diagnostic could be orders of
magnitude clearer.


Modified:
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaAccess.cpp
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/test/CXX/class.access/p4.cpp

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=95078&r1=95077&r2=95078&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Tue Feb  2 02:45:54 2010
@@ -2417,6 +2417,7 @@
                                    AccessSpecifier Access);
   bool CheckConstructorAccess(SourceLocation Loc, CXXConstructorDecl *D,
                               AccessSpecifier Access);
+  bool CheckDestructorAccess(SourceLocation Loc, QualType T);
   bool CheckMemberOperatorAccess(SourceLocation Loc, Expr *ObjectExpr,
                                  NamedDecl *D, AccessSpecifier Access);
   bool CheckAccess(const LookupResult &R, NamedDecl *D, AccessSpecifier Access);

Modified: cfe/trunk/lib/Sema/SemaAccess.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaAccess.cpp?rev=95078&r1=95077&r2=95078&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaAccess.cpp (original)
+++ cfe/trunk/lib/Sema/SemaAccess.cpp Tue Feb  2 02:45:54 2010
@@ -306,6 +306,31 @@
   return false;
 }
 
+bool Sema::CheckDestructorAccess(SourceLocation Loc,
+                                 QualType T) {
+  if (!getLangOptions().AccessControl)
+    return false;
+
+  const RecordType *Record = T->getAs<RecordType>();
+  if (!Record)
+    return false;
+
+  CXXRecordDecl *NamingClass = cast<CXXRecordDecl>(Record->getDecl());
+  CXXDestructorDecl *Dtor = NamingClass->getDestructor(Context);
+
+  AccessSpecifier Access = Dtor->getAccess();
+  if (Access == AS_public)
+    return false;
+
+  LookupResult R(*this, Dtor->getDeclName(), Loc, LookupOrdinaryName);
+  R.suppressDiagnostics();
+
+  R.setNamingClass(NamingClass);
+  return CheckAccess(R, Dtor, Access);
+
+  // FIXME: protected check
+}
+
 /// Checks access to a constructor.
 bool Sema::CheckConstructorAccess(SourceLocation UseLoc,
                                   CXXConstructorDecl *Constructor,

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=95078&r1=95077&r2=95078&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Tue Feb  2 02:45:54 2010
@@ -2637,6 +2637,9 @@
         Diag(Param->getLocation(), diag::err_array_star_in_function_definition);
       }
     }
+
+    if (getLangOptions().AccessControl)
+      CheckDestructorAccess(Param->getLocation(), Param->getType());
   }
 
   return HasInvalidParm;

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=95078&r1=95077&r2=95078&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Tue Feb  2 02:45:54 2010
@@ -3985,10 +3985,11 @@
 void Sema::FinalizeVarWithDestructor(VarDecl *VD, QualType DeclInitType) {
   CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(
                                   DeclInitType->getAs<RecordType>()->getDecl());
-  if (!ClassDecl->hasTrivialDestructor())
-    if (CXXDestructorDecl *Destructor =
-        const_cast<CXXDestructorDecl*>(ClassDecl->getDestructor(Context)))
-      MarkDeclarationReferenced(VD->getLocation(), Destructor);
+  if (!ClassDecl->hasTrivialDestructor()) {
+    CXXDestructorDecl *Destructor = ClassDecl->getDestructor(Context);
+    MarkDeclarationReferenced(VD->getLocation(), Destructor);
+    CheckDestructorAccess(VD->getLocation(), VD->getType());
+  }
 }
 
 /// AddCXXDirectInitializerToDecl - This action is called immediately after

Modified: cfe/trunk/test/CXX/class.access/p4.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/class.access/p4.cpp?rev=95078&r1=95077&r2=95078&view=diff

==============================================================================
--- cfe/trunk/test/CXX/class.access/p4.cpp (original)
+++ cfe/trunk/test/CXX/class.access/p4.cpp Tue Feb  2 02:45:54 2010
@@ -83,3 +83,32 @@
     ca(priv); // expected-error {{access to private member}}
   }
 }
+
+// Implicit constructor calls.
+namespace test2 {
+  class A {
+  private:
+    A(); // expected-note {{declared private here}}
+
+    static A foo;
+  };
+
+  A a; // expected-error {{access to private member}}
+  A A::foo; // okay
+}
+
+// Implicit destructor calls.
+namespace test3 {
+  class A{
+  private:
+    ~A(); // expected-note 3 {{declared private here}}
+    static A foo;
+  };
+
+  A a; // expected-error {{access to private member}}
+  A A::foo;
+
+  void foo(A param) { // expected-error {{access to private member}}
+    A local; // expected-error {{access to private member}}
+  }
+}





More information about the cfe-commits mailing list