[PCH] When we are replacing a decl in a chained PCH that is also a DeclContext,
make sure to fully load its external lexical and visible declarations before
re-writing it.

rdar://10914192

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@153254 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp
index 4c6c9dc..9436bfb 100644
--- a/lib/Serialization/ASTReader.cpp
+++ b/lib/Serialization/ASTReader.cpp
@@ -4972,6 +4972,51 @@
   return const_cast<DeclContext*>(DC)->lookup(Name);
 }
 
+namespace {
+  /// \brief ModuleFile visitor used to complete the visible decls map of a
+  /// declaration context.
+  class DeclContextVisibleDeclMapVisitor {
+    ASTReader &Reader;
+    DeclContext *DC;
+
+  public:
+    DeclContextVisibleDeclMapVisitor(ASTReader &Reader, DeclContext *DC)
+      : Reader(Reader), DC(DC) { }
+
+    static bool visit(ModuleFile &M, void *UserData) {
+      return static_cast<DeclContextVisibleDeclMapVisitor*>(UserData)->visit(M);
+    }
+
+    bool visit(ModuleFile &M) {
+      // Check whether we have any visible declaration information for
+      // this context in this module.
+      ModuleFile::DeclContextInfosMap::iterator
+        Info = M.DeclContextInfos.find(DC);
+      if (Info == M.DeclContextInfos.end() || 
+          !Info->second.NameLookupTableData)
+        return false;
+      
+      // Look for this name within this module.
+      ASTDeclContextNameLookupTable *LookupTable =
+        (ASTDeclContextNameLookupTable*)Info->second.NameLookupTableData;
+      for (ASTDeclContextNameLookupTable::key_iterator
+             I = LookupTable->key_begin(),
+             E = LookupTable->key_end(); I != E; ++I) {
+        DC->lookup(*I); // Force loading of the visible decls for the decl name.
+      }
+
+      return false;
+    }
+  };
+}
+
+void ASTReader::completeVisibleDeclsMap(DeclContext *DC) {
+  if (!DC->hasExternalVisibleStorage())
+    return;
+  DeclContextVisibleDeclMapVisitor Visitor(*this, DC);
+  ModuleMgr.visit(&DeclContextVisibleDeclMapVisitor::visit, &Visitor);
+}
+
 /// \brief Under non-PCH compilation the consumer receives the objc methods
 /// before receiving the implementation, and codegen depends on this.
 /// We simulate this by deserializing and passing to consumer the methods of the