]> granicus.if.org Git - clang/commitdiff
Implement access checking for protected base classes.
authorAnders Carlsson <andersca@mac.com>
Sat, 28 Mar 2009 01:09:05 +0000 (01:09 +0000)
committerAnders Carlsson <andersca@mac.com>
Sat, 28 Mar 2009 01:09:05 +0000 (01:09 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67887 91177308-0d34-0410-b5e6-96231b3b80d8

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

index cc212434b794252a7cdbbb24d9914f0eaf684eca..e65b05050020ed72e9bebe1d2e6656de038c0df5 100644 (file)
@@ -55,7 +55,7 @@ bool Sema::CheckBaseClassAccess(QualType Derived, QualType Base,
   
   const CXXBaseSpecifier *InacessibleBase = 0;
 
-  const CXXRecordDecl* CurrentClassDecl = 0;
+  CXXRecordDecl* CurrentClassDecl = 0;
   if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(getCurFunctionDecl()))
     CurrentClassDecl = MD->getParent();
 
@@ -79,9 +79,21 @@ bool Sema::CheckBaseClassAccess(QualType Derived, QualType Base,
         if (CurrentClassDecl != Element->Class)
           FoundInaccessibleBase = true;
         break;
-      case AS_protected:  
-        // FIXME: Implement
-        break;
+      case AS_protected:
+        // FIXME: Check if the current function/class is a friend.
+        if (!CurrentClassDecl) {
+          FoundInaccessibleBase = true;
+          break;
+        }
+        
+        if (CurrentClassDecl != Element->Class) {
+          QualType CurrentClassType = Context.getTypeDeclType(CurrentClassDecl);
+          QualType ClassType = Context.getTypeDeclType(Element->Class);
+          
+          if (!IsDerivedFrom(CurrentClassType, ClassType))
+            FoundInaccessibleBase = true;
+          break;
+        }
       }
       
       if (FoundInaccessibleBase) {
index 6138685b2644fae276deb1da6d570924cc776650..a164d43e8ae30816e9481388712f432bce5a7417 100644 (file)
@@ -40,7 +40,7 @@ namespace clang {
     const CXXBaseSpecifier *Base;
 
     /// Class - The record decl of the class that the base is a base of.
-    const CXXRecordDecl *Class;
+    CXXRecordDecl *Class;
     
     /// SubobjectNumber - Identifies which base class subobject (of type
     /// @c Base->getType()) this base path element refers to. This 
index f98437695efe342f31a28e25f38a15b97fa90c5f..3e50b26e5a0812ac5d597d09aa88cc899dc2ee98 100644 (file)
@@ -80,3 +80,35 @@ namespace T6 {
     A *a = c;
   }
 }
+
+namespace T7 {
+  
+class C;
+class A { };
+class B : protected A { // expected-note {{'protected' inheritance specifier here}}
+  void f(C *);
+};
+
+class C : protected B { // expected-note {{'protected' inheritance specifier here}}
+  void f(C *c) {
+    A* a = c;
+  }
+};
+
+void B::f(C *c) {
+  A *a = c; // expected-error {{conversion from 'class T7::C' to inaccessible base class 'class T7::A'}} \
+               expected-error {{incompatible type initializing 'class T7::C *', expected 'class T7::A *'}}
+}
+
+class D : private C  {
+  void f(D *d) {
+    A *a = d;
+  }
+};
+
+void f(B* b) {
+  A *a = b; // expected-error {{conversion from 'class T7::B' to inaccessible base class 'class T7::A'}} \
+               expected-error {{incompatible type initializing 'class T7::B *', expected 'class T7::A *'}}
+}
+
+}