Index: include/clang/Sema/DelayedDiagnostic.h
===================================================================
--- include/clang/Sema/DelayedDiagnostic.h	(revision 160854)
+++ include/clang/Sema/DelayedDiagnostic.h	(working copy)
@@ -44,10 +44,13 @@
                  MemberNonce _,
                  CXXRecordDecl *NamingClass,
                  DeclAccessPair FoundDecl,
-                 QualType BaseObjectType)
+                 QualType BaseObjectType,
+                 DeclContext *FromContext = 0)
     : Access(FoundDecl.getAccess()), IsMember(true),
       Target(FoundDecl.getDecl()), NamingClass(NamingClass),
-      BaseObjectType(BaseObjectType), Diag(0, Allocator) {
+      BaseObjectType(BaseObjectType),
+      FromContext(FromContext),
+      Diag(0, Allocator) {
   }
 
   AccessedEntity(PartialDiagnostic::StorageAllocator &Allocator,
@@ -58,6 +61,7 @@
     : Access(Access), IsMember(false),
       Target(BaseClass),
       NamingClass(DerivedClass),
+      FromContext(0),
       Diag(0, Allocator) {
   }
 
@@ -78,6 +82,10 @@
   /// Retrieves the base object type, important when accessing
   /// an instance member.
   QualType getBaseObjectType() const { return BaseObjectType; }
+  
+  /// Retrieves the specified DeclarationContext from which
+  /// we want to access the target.
+  DeclContext *getFromContext() const { return FromContext; }
 
   /// Sets a diagnostic to be performed.  The diagnostic is given
   /// four (additional) arguments:
@@ -105,6 +113,7 @@
   NamedDecl *Target;
   CXXRecordDecl *NamingClass;
   QualType BaseObjectType;
+  DeclContext *FromContext;
   PartialDiagnostic Diag;
 };
 
Index: include/clang/Sema/Sema.h
===================================================================
--- include/clang/Sema/Sema.h	(revision 160854)
+++ include/clang/Sema/Sema.h	(working copy)
@@ -4449,7 +4449,7 @@
                                     unsigned DiagID,
                                     bool ForceCheck = false,
                                     bool ForceUnprivileged = false);
-  void CheckLookupAccess(const LookupResult &R);
+  void CheckLookupAccess(const LookupResult &R, bool UseCurContext = false);
   bool IsSimplyAccessible(NamedDecl *decl, DeclContext *Ctx);
   bool isSpecialMemberAccessibleForDeletion(CXXMethodDecl *decl,
                                             AccessSpecifier access,
Index: lib/Sema/SemaAccess.cpp
===================================================================
--- lib/Sema/SemaAccess.cpp	(revision 160854)
+++ lib/Sema/SemaAccess.cpp	(working copy)
@@ -151,9 +151,10 @@
                MemberNonce _,
                CXXRecordDecl *NamingClass,
                DeclAccessPair FoundDecl,
-               QualType BaseObjectType)
+               QualType BaseObjectType,
+               DeclContext *FromContext = 0)
     : AccessedEntity(Context.getDiagAllocator(), Member, NamingClass,
-                     FoundDecl, BaseObjectType) {
+                     FoundDecl, BaseObjectType, FromContext) {
     initialize();
   }
 
@@ -1424,12 +1425,15 @@
   // of the declaration, just in case it's a friend of something.
   // However, this does not apply to local extern declarations.
 
-  DeclContext *DC = decl->getDeclContext();
-  if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) {
-    if (!DC->isFunctionOrMethod()) DC = fn;
-  } else if (FunctionTemplateDecl *fnt = dyn_cast<FunctionTemplateDecl>(decl)) {
-    // Never a local declaration.
-    DC = fnt->getTemplatedDecl();
+  DeclContext *DC = DD.getAccessData().getFromContext();
+  if (!DC) {
+    DC = decl->getDeclContext();
+    if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) {
+      if (!DC->isFunctionOrMethod()) DC = fn;
+    } else if (FunctionTemplateDecl *fnt = dyn_cast<FunctionTemplateDecl>(decl)) {
+      // Never a local declaration.
+      DC = fnt->getTemplatedDecl();
+    }
   }
 
   EffectiveContext EC(DC);
@@ -1752,7 +1756,7 @@
 }
 
 /// Checks access to all the declarations in the given result set.
-void Sema::CheckLookupAccess(const LookupResult &R) {
+void Sema::CheckLookupAccess(const LookupResult &R, bool UseCurContext) {
   assert(getLangOpts().AccessControl
          && "performing access check without access control");
   assert(R.getNamingClass() && "performing access check without naming class");
@@ -1761,7 +1765,8 @@
     if (I.getAccess() != AS_public) {
       AccessTarget Entity(Context, AccessedEntity::Member,
                           R.getNamingClass(), I.getPair(),
-                          R.getBaseObjectType());
+                          R.getBaseObjectType(),
+                          UseCurContext ? CurContext : 0);
       Entity.setDiag(diag::err_access);
       CheckAccess(*this, R.getNameLoc(), Entity);
     }
Index: lib/Sema/SemaDeclCXX.cpp
===================================================================
--- lib/Sema/SemaDeclCXX.cpp	(revision 160854)
+++ lib/Sema/SemaDeclCXX.cpp	(working copy)
@@ -10301,6 +10301,10 @@
     }
   }
 
+  if (Previous.getResultKind() == LookupResult::Found &&
+      Previous.getFoundDecl()->isCXXClassMember())
+    CheckLookupAccess(Previous);
+  
   // FIXME: This is an egregious hack to cope with cases where the scope stack
   // does not contain the declaration context, i.e., in an out-of-line 
   // definition of a class.
