r175858 - Only suppress instance context if a member is actually

John McCall rjmccall at apple.com
Thu Feb 21 19:52:55 PST 2013


Author: rjmccall
Date: Thu Feb 21 21:52:55 2013
New Revision: 175858

URL: http://llvm.org/viewvc/llvm-project?rev=175858&view=rev
Log:
Only suppress instance context if a member is actually
accessible in its declaring class;  otherwise we might
fail to apply [class.protected] when considering
accessibility in derived classes.

Noticed by inspection; <rdar://13270329>.

I had an existing test wrong.  Here's why it's wrong:

Follow the rules (and notation) of [class.access]p5.
The naming class (N) is B and the context (R) is D::getX.
- 'x' as a member of B is protected, but R does not occur
  in a member or friend of a class derived from B.
- There does exist a base class of B, A, which is accessible
  from R, and 'x' is accessible at R when named in A because
  'x' as a member of A is protected and R occurs in a member
  of a class, D, that is derived from A;  however, by
  [class.protected], the class of the object expression must
  be equal to or derived from that class, and A does not
  derive from D.

Modified:
    cfe/trunk/lib/Sema/SemaAccess.cpp
    cfe/trunk/test/CXX/class.access/class.protected/p1.cpp

Modified: cfe/trunk/lib/Sema/SemaAccess.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaAccess.cpp?rev=175858&r1=175857&r2=175858&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaAccess.cpp (original)
+++ cfe/trunk/lib/Sema/SemaAccess.cpp Thu Feb 21 21:52:55 2013
@@ -1317,7 +1317,13 @@ static AccessResult IsAccessible(Sema &S
     FinalAccess = Target->getAccess();
     switch (HasAccess(S, EC, DeclaringClass, FinalAccess, Entity)) {
     case AR_accessible:
+      // Target is accessible at EC when named in its declaring class.
+      // We can now hill-climb and simply check whether the declaring
+      // class is accessible as a base of the naming class.  This is
+      // equivalent to checking the access of a notional public
+      // member with no instance context.
       FinalAccess = AS_public;
+      Entity.suppressInstanceContext();
       break;
     case AR_inaccessible: break;
     case AR_dependent: return AR_dependent; // see above
@@ -1325,8 +1331,6 @@ static AccessResult IsAccessible(Sema &S
 
     if (DeclaringClass == NamingClass)
       return (FinalAccess == AS_public ? AR_accessible : AR_inaccessible);
-
-    Entity.suppressInstanceContext();
   } else {
     FinalAccess = AS_public;
   }

Modified: cfe/trunk/test/CXX/class.access/class.protected/p1.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/class.access/class.protected/p1.cpp?rev=175858&r1=175857&r2=175858&view=diff
==============================================================================
--- cfe/trunk/test/CXX/class.access/class.protected/p1.cpp (original)
+++ cfe/trunk/test/CXX/class.access/class.protected/p1.cpp Thu Feb 21 21:52:55 2013
@@ -329,7 +329,7 @@ namespace test8 {
 
 namespace test9 {
   class A { // expected-note {{member is declared here}}
-  protected: int foo(); // expected-note 4 {{declared}} expected-note 2 {{can only access this member on an object of type}} expected-note {{member is declared here}}
+  protected: int foo(); // expected-note 4 {{declared}} expected-note 3 {{can only access this member on an object of type}} expected-note 2 {{member is declared here}}
   };
 
   class B : public A { // expected-note {{member is declared here}}
@@ -344,14 +344,15 @@ namespace test9 {
     static void test(A &a) {
       a.foo(); // expected-error {{'foo' is a protected member}}
       a.A::foo(); // expected-error {{'foo' is a protected member}}
-      a.B::foo();
+      a.B::foo(); // expected-error {{'foo' is a protected member}}
       a.C::foo(); // expected-error {{'foo' is a protected member}}
+      a.D::foo(); // expected-error {{'foo' is a protected member}}
     }
 
     static void test(B &b) {
       b.foo();
       b.A::foo();
-      b.B::foo();
+      b.B::foo(); // accessible as named in A
       b.C::foo(); // expected-error {{'foo' is a protected member}}
     }
 





More information about the cfe-commits mailing list