]> granicus.if.org Git - clang/commitdiff
Pass the InitializedEntity to Sema::CheckConstructorAccess and use it to report diffe...
authorAnders Carlsson <andersca@mac.com>
Wed, 21 Apr 2010 18:47:17 +0000 (18:47 +0000)
committerAnders Carlsson <andersca@mac.com>
Wed, 21 Apr 2010 18:47:17 +0000 (18:47 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@102010 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/Sema.h
lib/Sema/SemaAccess.cpp
lib/Sema/SemaInit.cpp
test/CXX/class.access/p4.cpp

index bbabb46c115c18a70cb1e2047e9c04ae4ab2000a..9a2a388bc0b56134919dd0a5b688636503f2d9d8 100644 (file)
@@ -459,6 +459,9 @@ def err_access :
 def err_access_ctor :
     Error<"calling a %select{private|protected}0 constructor of class %2">,
     NoSFINAE;
+def err_access_ctor_base :
+    Error<"base class %0 has %select{private|protected}1 constructor">,
+    NoSFINAE;
 def err_access_dtor_base :
     Error<"base class %0 has %select{private|protected}1 destructor">,
     NoSFINAE;
index 636bb6a3d3e3d50a6a3a5b509d157d6c5c83aa9c..1506d8c2dc29b1ef5ec278e1f384f53efca824f9 100644 (file)
@@ -2644,6 +2644,7 @@ public:
                                      DeclAccessPair FoundDecl);
   AccessResult CheckConstructorAccess(SourceLocation Loc,
                                       CXXConstructorDecl *D,
+                                      const InitializedEntity &Entity,
                                       AccessSpecifier Access);
   AccessResult CheckDestructorAccess(SourceLocation Loc,
                                      CXXDestructorDecl *Dtor,
index 352477b859022139c58c2ae11407cfb650ce9da3..1658b699c6b8ff1def9156390e284893478bc994 100644 (file)
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "Sema.h"
+#include "SemaInit.h"
 #include "Lookup.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/CXXInheritance.h"
@@ -1127,18 +1128,28 @@ Sema::AccessResult Sema::CheckDestructorAccess(SourceLocation Loc,
 /// Checks access to a constructor.
 Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc,
                                   CXXConstructorDecl *Constructor,
+                                  const InitializedEntity &Entity,
                                   AccessSpecifier Access) {
   if (!getLangOptions().AccessControl ||
       Access == AS_public)
     return AR_accessible;
 
   CXXRecordDecl *NamingClass = Constructor->getParent();
-  AccessTarget Entity(Context, AccessTarget::Member, NamingClass,
-                      DeclAccessPair::make(Constructor, Access),
-                      QualType());
-  Entity.setDiag(diag::err_access_ctor);
+  AccessTarget AccessEntity(Context, AccessTarget::Member, NamingClass,
+                            DeclAccessPair::make(Constructor, Access),
+                            QualType());
+  switch (Entity.getKind()) {
+  default:
+    AccessEntity.setDiag(diag::err_access_ctor);
+    break;
+
+  case InitializedEntity::EK_Base:
+    AccessEntity.setDiag(PDiag(diag::err_access_ctor_base)
+                          << Entity.getBaseSpecifier()->getType());
+    break;
+  }
 
-  return CheckAccess(*this, UseLoc, Entity);
+  return CheckAccess(*this, UseLoc, AccessEntity);
 }
 
 /// Checks direct (i.e. non-inherited) access to an arbitrary class
index a99f513609b7532e87c9170b2966fb3a893797f6..fe7f79f9c5232df69f7db915945ab9d8664417aa 100644 (file)
@@ -3258,7 +3258,7 @@ static Sema::OwningExprResult CopyObject(Sema &S,
   ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(S);
   CurInit.release(); // Ownership transferred into MultiExprArg, below.
 
-  S.CheckConstructorAccess(Loc, Constructor,
+  S.CheckConstructorAccess(Loc, Constructor, Entity,
                            Best->FoundDecl.getAccess());
 
   if (IsExtraneousCopy) {
@@ -3521,7 +3521,7 @@ InitializationSequence::Perform(Sema &S,
         if (CurInit.isInvalid())
           return S.ExprError();
 
-        S.CheckConstructorAccess(Kind.getLocation(), Constructor,
+        S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity,
                                  FoundFn.getAccess());
         
         CastKind = CastExpr::CK_ConstructorConversion;
@@ -3647,7 +3647,7 @@ InitializationSequence::Perform(Sema &S,
         return S.ExprError();
 
       // Only check access if all of that succeeded.
-      S.CheckConstructorAccess(Loc, Constructor,
+      S.CheckConstructorAccess(Loc, Constructor, Entity,
                                Step->Function.FoundDecl.getAccess());
       
       if (shouldBindAsTemporary(Entity))
index 9bed7cd8fea0df5463213f4fd2f6e564725fda4d..3d1578a4fc8397633e73c816a3c2b4d4307897d7 100644 (file)
@@ -88,13 +88,25 @@ namespace test1 {
 namespace test2 {
   class A {
   private:
-    A(); // expected-note {{declared private here}}
+    A(); // expected-note {{declared private here}}
 
     static A foo;
   };
 
   A a; // expected-error {{calling a private constructor}}
   A A::foo; // okay
+  
+  class B : A { }; // expected-error {{base class 'test2::A' has private constructor}}
+  B b;
+  
+  class C : virtual A { 
+  public:
+    C();
+  };
+  
+  // FIXME: It would be better if this said something about A being an inherited virtual base.
+  class D : C { }; // expected-error {{base class 'test2::A' has private constructor}}
+  D d;
 }
 
 // Implicit destructor calls.