]> granicus.if.org Git - clang/commitdiff
It is OK to cast to a private base class if the current member belongs to the class...
authorAnders Carlsson <andersca@mac.com>
Fri, 27 Mar 2009 19:01:12 +0000 (19:01 +0000)
committerAnders Carlsson <andersca@mac.com>
Fri, 27 Mar 2009 19:01:12 +0000 (19:01 +0000)
class A {};
class B : private A {
  void f(B *b) { A* a = b; }
};

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67860 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaAccess.cpp
test/SemaCXX/access-base-class.cpp

index 85e1c2ed883b497464ca47fcf7125dc53c173378..cc212434b794252a7cdbbb24d9914f0eaf684eca 100644 (file)
@@ -55,6 +55,10 @@ bool Sema::CheckBaseClassAccess(QualType Derived, QualType Base,
   
   const CXXBaseSpecifier *InacessibleBase = 0;
 
+  const CXXRecordDecl* CurrentClassDecl = 0;
+  if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(getCurFunctionDecl()))
+    CurrentClassDecl = MD->getParent();
+
   for (BasePaths::paths_iterator Path = Paths.begin(), PathsEnd = Paths.end(); 
       Path != PathsEnd; ++Path) {
     
@@ -71,8 +75,9 @@ bool Sema::CheckBaseClassAccess(QualType Derived, QualType Base,
         // Nothing to do.
         break;
       case AS_private:
-        // FIXME: Check if the current function is a member or friend.
-        FoundInaccessibleBase = true;
+        // FIXME: Check if the current function/class is a friend.
+        if (CurrentClassDecl != Element->Class)
+          FoundInaccessibleBase = true;
         break;
       case AS_protected:  
         // FIXME: Implement
index 29586759b04e9e1aa93220c3f292cbbfea9a6d12..f98437695efe342f31a28e25f38a15b97fa90c5f 100644 (file)
@@ -49,3 +49,34 @@ void f(D *d) {
 }
 
 }
+
+namespace T5 {
+  class A {};
+    
+  class B : private A {
+    void f(B *b) {
+      A *a = b;
+    }
+  };    
+}
+
+namespace T6 {
+  class C;
+  
+  class A {};
+  
+  class B : private A { // expected-note {{'private' inheritance specifier here}}
+    void f(C* c);
+  };
+  
+  class C : public B { 
+    void f(C *c) {
+      A* a = c; // expected-error {{conversion from 'class T6::C' to inaccessible base class 'class T6::A'}} \
+                   expected-error {{incompatible type initializing 'class T6::C *', expected 'class T6::A *'}}
+    }
+  };
+  
+  void B::f(C *c) {
+    A *a = c;
+  }
+}