From 815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Tue, 5 Aug 2008 18:50:11 +0000 Subject: 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 --- lib/CodeGen/ModuleBuilder.cpp | 49 +++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 20 deletions(-) (limited to 'lib/CodeGen/ModuleBuilder.cpp') diff --git a/lib/CodeGen/ModuleBuilder.cpp b/lib/CodeGen/ModuleBuilder.cpp index 201092f4d8..7329ed1d90 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 @@ using namespace clang; #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 TD; ASTContext *Ctx; const LangOptions &Features; bool GenerateDebugInfo; protected: - llvm::Module *&M; - CodeGen::CodeGenModule *Builder; + llvm::OwningPtr M; + llvm::OwningPtr 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)) {} + + virtual ~CodeGeneratorImpl() {} - ~CodeGenerator() { - delete Builder; - delete TD; + 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 @@ namespace { 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 @@ namespace { }; } -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); } - -- cgit v1.2.3-18-g5258