When adding a friend class declaration to the lookup tables, use the access specifier
of any previous declaration in case we replace it in a class's declaration table.
Fixes bug 4858.  This sort of thing makes me reconsider putting friend declarations in
declaration lists.

llvm-svn: 80750
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 86222ff..113283a 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -4331,6 +4331,11 @@
   if (TUK == TUK_Friend) {
     // Friend tag decls are visible in fairly strange ways.
     if (!CurContext->isDependentContext()) {
+      // We might be replacing an existing declaration in the lookup tables;
+      // if so, borrow its access specifier.
+      if (PrevDecl)
+        New->setAccess(PrevDecl->getAccess());
+
       DeclContext *DC = New->getDeclContext()->getLookupContext();
       DC->makeDeclVisibleInContext(New, /* Recoverable = */ false);
       if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
diff --git a/clang/test/CXX/class/class.friend/p1.cpp b/clang/test/CXX/class/class.friend/p1.cpp
index 9ba0272..3f87788 100644
--- a/clang/test/CXX/class/class.friend/p1.cpp
+++ b/clang/test/CXX/class/class.friend/p1.cpp
@@ -61,6 +61,10 @@
 
   typedef void ftypedef();
   friend ftypedef typedeffed_function; // okay (because it's not declared as a member)
+
+  class facet;
+  friend class facet;  // should not assert
+  class facet {};
 };
 
 A::UndeclaredSoFar y; // expected-error {{ unknown type name 'UndeclaredSoFar' }}