[cfe-commits] r67887 - in /cfe/trunk: lib/Sema/SemaAccess.cpp lib/Sema/SemaInherit.h test/SemaCXX/access-base-class.cpp

Anders Carlsson andersca at mac.com
Fri Mar 27 18:09:06 PDT 2009


Author: andersca
Date: Fri Mar 27 20:09:05 2009
New Revision: 67887

URL: http://llvm.org/viewvc/llvm-project?rev=67887&view=rev
Log:
Implement access checking for protected base classes.

Modified:
    cfe/trunk/lib/Sema/SemaAccess.cpp
    cfe/trunk/lib/Sema/SemaInherit.h
    cfe/trunk/test/SemaCXX/access-base-class.cpp

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaAccess.cpp (original)
+++ cfe/trunk/lib/Sema/SemaAccess.cpp Fri Mar 27 20:09:05 2009
@@ -55,7 +55,7 @@
   
   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 @@
         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) {

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaInherit.h (original)
+++ cfe/trunk/lib/Sema/SemaInherit.h Fri Mar 27 20:09:05 2009
@@ -40,7 +40,7 @@
     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 

Modified: cfe/trunk/test/SemaCXX/access-base-class.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/access-base-class.cpp?rev=67887&r1=67886&r2=67887&view=diff

==============================================================================
--- cfe/trunk/test/SemaCXX/access-base-class.cpp (original)
+++ cfe/trunk/test/SemaCXX/access-base-class.cpp Fri Mar 27 20:09:05 2009
@@ -80,3 +80,35 @@
     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 *'}}
+}
+
+}





More information about the cfe-commits mailing list