Refactored driver logic for CodeGen into LLVMCodeGenWriter.  This ASTConsumer layers on top of LLVMCodeGen (another existing ASTConsumer) to emit bitcode files to disk.  This layering takes this logic out of clang.cpp and puts it directly into the ASTConsumer interface.  The benefit is that now --emit-llvm works with both serialized ASTs and regular source files.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@54364 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp
index 5d85b7a..0c3b872 100644
--- a/lib/CodeGen/CodeGenModule.cpp
+++ b/lib/CodeGen/CodeGenModule.cpp
@@ -42,13 +42,15 @@
   Runtime = CreateObjCRuntime(*this);
 
   // If debug info generation is enabled, create the CGDebugInfo object.
-  if (GenerateDebugInfo)
-    DebugInfo = new CGDebugInfo(this);
-  else
-    DebugInfo = NULL;
+  DebugInfo = GenerateDebugInfo ? new CGDebugInfo(this) : 0;      
 }
 
 CodeGenModule::~CodeGenModule() {
+  delete Runtime;
+  delete DebugInfo;
+}
+
+void CodeGenModule::Release() {
   EmitStatics();
   llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction();
   if (ObjCInitFunction)
@@ -56,8 +58,6 @@
   EmitCtorList(GlobalCtors, "llvm.global_ctors");
   EmitCtorList(GlobalDtors, "llvm.global_dtors");
   EmitAnnotations();
-  delete Runtime;
-  delete DebugInfo;
   // Run the verifier to check that the generated code is consistent.
   assert(!verifyModule(TheModule));
 }
diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h
index 1fb2cf7..bb5de15 100644
--- a/lib/CodeGen/CodeGenModule.h
+++ b/lib/CodeGen/CodeGenModule.h
@@ -19,6 +19,7 @@
 #include "clang/AST/Attr.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/OwningPtr.h"
 
 namespace llvm {
   class Module;
@@ -62,8 +63,8 @@
   const llvm::TargetData &TheTargetData;
   Diagnostic &Diags;
   CodeGenTypes Types;
-  CGObjCRuntime *Runtime;
-  CGDebugInfo *DebugInfo;
+  CGObjCRuntime* Runtime;
+  CGDebugInfo* DebugInfo;
 
   llvm::Function *MemCpyFn;
   llvm::Function *MemMoveFn;
@@ -103,8 +104,12 @@
   CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M, 
                 const llvm::TargetData &TD, Diagnostic &Diags,
                 bool GenerateDebugInfo);
+
   ~CodeGenModule();
   
+  /// Release - Finalize LLVM code generation.
+  void Release();
+  
   CGObjCRuntime *getObjCRuntime() { return Runtime; }
   CGDebugInfo *getDebugInfo() { return DebugInfo; }
   ASTContext &getContext() const { return Context; }
diff --git a/lib/CodeGen/ModuleBuilder.cpp b/lib/CodeGen/ModuleBuilder.cpp
index 201092f..7329ed1 100644
--- a/lib/CodeGen/ModuleBuilder.cpp
+++ b/lib/CodeGen/ModuleBuilder.cpp
@@ -13,7 +13,6 @@
 
 #include "clang/CodeGen/ModuleBuilder.h"
 #include "CodeGenModule.h"
-#include "clang/AST/ASTConsumer.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Decl.h"
 using namespace clang;
@@ -27,26 +26,37 @@
 #include "llvm/Module.h"
 #include "llvm/Target/TargetData.h"
 #include "llvm/Target/TargetMachine.h"
+#include "llvm/Support/Compiler.h"
+#include "llvm/ADT/OwningPtr.h"
+
 
 namespace {
-  class CodeGenerator : public ASTConsumer {
+  class VISIBILITY_HIDDEN CodeGeneratorImpl : public CodeGenerator {
     Diagnostic &Diags;
-    const llvm::TargetData *TD;
+    llvm::OwningPtr<const llvm::TargetData> TD;
     ASTContext *Ctx;
     const LangOptions &Features;
     bool GenerateDebugInfo;
   protected:
-    llvm::Module *&M;
-    CodeGen::CodeGenModule *Builder;
+    llvm::OwningPtr<llvm::Module> M;
+    llvm::OwningPtr<CodeGen::CodeGenModule> Builder;
   public:
-    CodeGenerator(Diagnostic &diags, const LangOptions &LO,
-                  llvm::Module *&DestModule, bool DebugInfoFlag)
+    CodeGeneratorImpl(Diagnostic &diags, const LangOptions &LO,
+                      const std::string& ModuleName,
+                      bool DebugInfoFlag)
     : Diags(diags), Features(LO), GenerateDebugInfo(DebugInfoFlag),
-    M(DestModule) {}
+      M(new llvm::Module(ModuleName)) {}
     
-    ~CodeGenerator() {
-      delete Builder;
-      delete TD;
+    virtual ~CodeGeneratorImpl() {}
+    
+    virtual llvm::Module* ReleaseModule() {      
+      if (Diags.hasErrorOccurred())
+        return 0;
+      
+      if (Builder)
+        Builder->Release();
+      
+      return M.take();
     }
     
     virtual void Initialize(ASTContext &Context) {
@@ -54,9 +64,9 @@
       
       M->setTargetTriple(Ctx->Target.getTargetTriple());
       M->setDataLayout(Ctx->Target.getTargetDescription());
-      TD = new llvm::TargetData(Ctx->Target.getTargetDescription());
-      Builder = new CodeGen::CodeGenModule(Context, Features, *M, *TD, Diags,
-                                           GenerateDebugInfo);
+      TD.reset(new llvm::TargetData(Ctx->Target.getTargetDescription()));
+      Builder.reset(new CodeGen::CodeGenModule(Context, Features, *M, *TD,
+                                               Diags, GenerateDebugInfo));
     }
     
     virtual void HandleTopLevelDecl(Decl *D) {
@@ -128,10 +138,9 @@
   };
 }
 
-ASTConsumer *clang::CreateLLVMCodeGen(Diagnostic &Diags, 
-                                      const LangOptions &Features,
-                                      llvm::Module *&DestModule,
-                                      bool GenerateDebugInfo) {
-  return new CodeGenerator(Diags, Features, DestModule, GenerateDebugInfo);
+CodeGenerator *clang::CreateLLVMCodeGen(Diagnostic &Diags, 
+                                        const LangOptions &Features,
+                                        const std::string& ModuleName,
+                                        bool GenerateDebugInfo) {
+  return new CodeGeneratorImpl(Diags, Features, ModuleName, GenerateDebugInfo);
 }
-