Introduce '-chain-include' option to specify headers that will be converted to chained PCHs in memory
without having to use multiple runs and intermediate files.

Intended for testing & debugging of chained PCH.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@127339 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Driver/CC1Options.td b/include/clang/Driver/CC1Options.td
index 12f2556..c0fb23d 100644
--- a/include/clang/Driver/CC1Options.td
+++ b/include/clang/Driver/CC1Options.td
@@ -560,6 +560,8 @@
   HelpText<"Include precompiled header file">;
 def include_pth : Separate<"-include-pth">, MetaVarName<"<file>">,
   HelpText<"Include file before parsing">;
+def chain_include : Separate<"-chain-include">, MetaVarName<"<file>">,
+  HelpText<"Include and chain a header file after turning it into PCH">;
 def preamble_bytes_EQ : Joined<"-preamble-bytes=">,
   HelpText<"Assume that the precompiled header is a precompiled preamble "
            "covering the first N bytes of the main file">;
diff --git a/include/clang/Frontend/ASTUnit.h b/include/clang/Frontend/ASTUnit.h
index 3138d7d..a0c99f1 100644
--- a/include/clang/Frontend/ASTUnit.h
+++ b/include/clang/Frontend/ASTUnit.h
@@ -540,7 +540,11 @@
   /// \brief A mapping from a file name to the memory buffer that stores the
   /// remapped contents of that file.
   typedef std::pair<std::string, FilenameOrMemBuf> RemappedFile;
-  
+
+  /// \brief Create a ASTUnit. Gets ownership of the passed CompilerInvocation. 
+  static ASTUnit *create(CompilerInvocation *CI,
+                         llvm::IntrusiveRefCntPtr<Diagnostic> Diags);
+
   /// \brief Create a ASTUnit from an AST file.
   ///
   /// \param Filename - The AST file to load.
@@ -656,6 +660,11 @@
   ///
   /// \returns True if an error occurred, false otherwise.
   bool Save(llvm::StringRef File);
+
+  /// \brief Serialize this translation unit with the given output stream.
+  ///
+  /// \returns True if an error occurred, false otherwise.
+  bool serialize(llvm::raw_ostream &OS);
 };
 
 } // namespace clang
diff --git a/include/clang/Frontend/PreprocessorOptions.h b/include/clang/Frontend/PreprocessorOptions.h
index 078b22e..e875ec1 100644
--- a/include/clang/Frontend/PreprocessorOptions.h
+++ b/include/clang/Frontend/PreprocessorOptions.h
@@ -44,6 +44,9 @@
   /// The implicit PCH included at the start of the translation unit, or empty.
   std::string ImplicitPCHInclude;
 
+  /// \brief Headers that will be converted to chained PCHs in memory.
+  std::vector<std::string> ChainedIncludes;
+
   /// \brief When true, disables most of the normal validation performed on
   /// precompiled headers.
   bool DisablePCHValidation;
diff --git a/include/clang/Serialization/ASTReader.h b/include/clang/Serialization/ASTReader.h
index 424e78c..835a0db 100644
--- a/include/clang/Serialization/ASTReader.h
+++ b/include/clang/Serialization/ASTReader.h
@@ -213,6 +213,10 @@
   /// \brief The AST consumer.
   ASTConsumer *Consumer;
 
+  /// \brief AST buffers for chained PCHs created and stored in memory.
+  /// First (not depending on another) PCH in chain is in front.
+  std::deque<llvm::MemoryBuffer *> ASTBuffers;
+
   /// \brief Information that is needed for every module.
   struct PerFileData {
     PerFileData(ASTFileType Ty);
@@ -886,6 +890,13 @@
   /// \brief Sets and initializes the given Context.
   void InitializeContext(ASTContext &Context);
 
+  /// \brief Set AST buffers for chained PCHs created and stored in memory.
+  /// First (not depending on another) PCH in chain is first in array.
+  void setASTMemoryBuffers(llvm::MemoryBuffer **bufs, unsigned numBufs) {
+    ASTBuffers.clear();
+    ASTBuffers.insert(ASTBuffers.begin(), bufs, bufs + numBufs);
+  }
+
   /// \brief Retrieve the name of the named (primary) AST file
   const std::string &getFileName() const { return Chain[0]->FileName; }
 
diff --git a/include/clang/Serialization/ChainedIncludesSource.h b/include/clang/Serialization/ChainedIncludesSource.h
new file mode 100644
index 0000000..90e74d9
--- /dev/null
+++ b/include/clang/Serialization/ChainedIncludesSource.h
@@ -0,0 +1,63 @@
+//===- ChainedIncludesSource.h - Chained PCHs in Memory ---------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines the ChainedIncludesSource class, which converts headers
+//  to chained PCHs in memory, mainly used for testing.
+//
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_CLANG_SERIALIZATION_CHAINEDINCLUDESSOURCE_H
+#define LLVM_CLANG_SERIALIZATION_CHAINEDINCLUDESSOURCE_H
+
+#include "clang/AST/ExternalASTSource.h"
+#include <vector>
+
+namespace clang {
+  class CompilerInstance;
+
+class ChainedIncludesSource : public ExternalASTSource {
+public:
+  virtual ~ChainedIncludesSource();
+
+  static ChainedIncludesSource *create(CompilerInstance &CI);
+
+private:
+  ExternalASTSource &getFinalReader() const { return *FinalReader; }
+
+  std::vector<CompilerInstance *> CIs;
+  llvm::OwningPtr<ExternalASTSource> FinalReader;
+
+  
+protected:
+
+//===----------------------------------------------------------------------===//
+// ExternalASTSource interface.
+//===----------------------------------------------------------------------===//
+
+  virtual Decl *GetExternalDecl(uint32_t ID);
+  virtual Selector GetExternalSelector(uint32_t ID);
+  virtual uint32_t GetNumExternalSelectors();
+  virtual Stmt *GetExternalDeclStmt(uint64_t Offset);
+  virtual CXXBaseSpecifier *GetExternalCXXBaseSpecifiers(uint64_t Offset);
+  virtual DeclContextLookupResult
+  FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name);
+  virtual void MaterializeVisibleDecls(const DeclContext *DC);
+  virtual bool FindExternalLexicalDecls(const DeclContext *DC,
+                                        bool (*isKindWeWant)(Decl::Kind),
+                                        llvm::SmallVectorImpl<Decl*> &Result);
+  virtual void CompleteType(TagDecl *Tag);
+  virtual void CompleteType(ObjCInterfaceDecl *Class);
+  virtual void StartedDeserializing();
+  virtual void FinishedDeserializing();
+  virtual void StartTranslationUnit(ASTConsumer *Consumer);
+  virtual void PrintStats();
+};
+
+}
+
+#endif