Apart from storing/retrieving the previous redeclaration from PCH, also store/retrieve the most recent
redeclaration. That way we are sure that the full redeclarations chain is loaded.

When using chained PCHs, first declarations point to the most recent redeclarations in the same PCH.
To address this use a REDECLS_UPDATE_LATEST record block to keep track of which first declarations need
to point to a most recent redeclaration in another PCH.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@110125 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/AST/Redeclarable.h b/include/clang/AST/Redeclarable.h
index 55e1f84..85c75b7 100644
--- a/include/clang/AST/Redeclarable.h
+++ b/include/clang/AST/Redeclarable.h
@@ -177,6 +177,9 @@
                                           static_cast<const decl_type*>(this)));
   }
   redecl_iterator redecls_end() const { return redecl_iterator(); }
+
+  friend class PCHDeclReader;
+  friend class PCHDeclWriter;
 };
 
 }
diff --git a/include/clang/Frontend/PCHBitCodes.h b/include/clang/Frontend/PCHBitCodes.h
index 109a78b..a07060f 100644
--- a/include/clang/Frontend/PCHBitCodes.h
+++ b/include/clang/Frontend/PCHBitCodes.h
@@ -245,8 +245,12 @@
       /// declarations.
       TU_UPDATE_LEXICAL = 28,
 
+      /// \brief Record code for an update to first decls pointing to the
+      /// latest redeclarations.
+      REDECLS_UPDATE_LATEST = 29,
+
       /// \brief Record code for declarations that Sema keeps references of.
-      SEMA_DECL_REFS = 29
+      SEMA_DECL_REFS = 30
     };
 
     /// \brief Record types used within a source manager block.
diff --git a/include/clang/Frontend/PCHReader.h b/include/clang/Frontend/PCHReader.h
index dda1567..e19669e 100644
--- a/include/clang/Frontend/PCHReader.h
+++ b/include/clang/Frontend/PCHReader.h
@@ -319,6 +319,11 @@
   /// DeclContext.
   DeclContextOffsetsMap DeclContextOffsets;
 
+  typedef llvm::DenseMap<pch::DeclID, pch::DeclID> FirstLatestDeclIDMap;
+  /// \brief Map of first declarations from a chained PCH that point to the
+  /// most recent declarations in another PCH.
+  FirstLatestDeclIDMap FirstLatestDeclIDs;
+
   /// \brief Read the records that describe the contents of declcontexts.
   bool ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
                               const std::pair<uint64_t, uint64_t> &Offsets,
@@ -561,7 +566,7 @@
   QualType ReadTypeRecord(unsigned Index);
   RecordLocation TypeCursorForIndex(unsigned Index);
   void LoadedDecl(unsigned Index, Decl *D);
-  Decl *ReadDeclRecord(unsigned Index);
+  Decl *ReadDeclRecord(unsigned Index, pch::DeclID ID);
   RecordLocation DeclCursorForIndex(unsigned Index);
 
   void PassInterestingDeclsToConsumer();
diff --git a/include/clang/Frontend/PCHWriter.h b/include/clang/Frontend/PCHWriter.h
index 92c7085..3403c6e 100644
--- a/include/clang/Frontend/PCHWriter.h
+++ b/include/clang/Frontend/PCHWriter.h
@@ -79,6 +79,7 @@
 public:
   typedef llvm::SmallVector<uint64_t, 64> RecordData;
 
+  friend class PCHDeclWriter;
 private:
   /// \brief The bitstream writer used to emit this precompiled header.
   llvm::BitstreamWriter &Stream;
@@ -195,6 +196,11 @@
   /// \brief Mapping from the macro definition indices in \c MacroDefinitions
   /// to the corresponding offsets within the preprocessor block.
   std::vector<uint32_t> MacroDefinitionOffsets;
+
+  typedef llvm::DenseMap<Decl *, Decl *> FirstLatestDeclMap;
+  /// \brief Map of first declarations from a chained PCH that point to the
+  /// most recent declarations in another PCH.
+  FirstLatestDeclMap FirstLatestDecls;
   
   /// \brief Declarations encountered that might be external
   /// definitions.