Implemented -serialize-ast option for the driver.  This is not really tested
and is a work in progress.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44967 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/Driver/ASTConsumers.cpp b/Driver/ASTConsumers.cpp
index 790baf5..332f589 100644
--- a/Driver/ASTConsumers.cpp
+++ b/Driver/ASTConsumers.cpp
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "ASTConsumers.h"
+#include "TranslationUnit.h"
 #include "clang/AST/AST.h"
 #include "clang/AST/ASTConsumer.h"
 #include "clang/AST/CFG.h"
@@ -604,3 +605,40 @@
   return new LLVMEmitter(Diags, Features);
 }
 
+//===----------------------------------------------------------------------===//
+// AST Serializer
+
+namespace {
+  class ASTSerializer : public ASTConsumer {
+    Diagnostic &Diags;
+    TranslationUnit TU;
+    const llvm::sys::Path FName;
+  public:
+    ASTSerializer(const llvm::sys::Path& F, Diagnostic &diags,
+                  const LangOptions &LO)
+    : Diags(diags), TU(LO), FName(F) {}
+
+    
+    virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
+      TU.setContext(&Context);
+    }
+    
+    virtual void HandleTopLevelDecl(Decl *D) {
+      // If an error occurred, stop code generation, but continue parsing and
+      // semantic analysis (to ensure all warnings and errors are emitted).
+      if (Diags.hasErrorOccurred())
+        return;
+      
+      TU.AddTopLevelDecl(D);
+    }
+    
+    ~ASTSerializer() { TU.EmitBitcodeFile(FName); }
+  }; 
+} // end anonymous namespace
+
+
+ASTConsumer *clang::CreateASTSerializer(const llvm::sys::Path& FName,
+                                        Diagnostic &Diags,
+                                        const LangOptions &Features) {
+  return new ASTSerializer(FName, Diags, Features);
+}