Add testcase for C++ chained PCH and fix the bugs it uncovered in name lookup.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@111882 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp
index 8167a0f..38b343f 100644
--- a/lib/Serialization/ASTReader.cpp
+++ b/lib/Serialization/ASTReader.cpp
@@ -3192,9 +3192,10 @@
 
   llvm::SmallVector<NamedDecl *, 64> Decls;
   // There might be visible decls in multiple parts of the chain, for the TU
-  // and namespaces.
+  // and namespaces. For any given name, the last available results replace
+  // all earlier ones. For this reason, we walk in reverse.
   DeclContextInfos &Infos = DeclContextOffsets[DC];
-  for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
+  for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
        I != E; ++I) {
     if (!I->NameLookupTableData)
       continue;
@@ -3208,6 +3209,7 @@
     ASTDeclContextNameLookupTrait::data_type Data = *Pos;
     for (; Data.first != Data.second; ++Data.first)
       Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
+    break;
   }
 
   ++NumVisibleDeclContextsRead;