[cfe-commits] r97111 - in /cfe/trunk: include/clang/Frontend/CodeGenAction.h lib/Frontend/CodeGenAction.cpp

Daniel Dunbar daniel at zuster.org
Wed Feb 24 20:37:50 PST 2010


Author: ddunbar
Date: Wed Feb 24 22:37:50 2010
New Revision: 97111

URL: http://llvm.org/viewvc/llvm-project?rev=97111&view=rev
Log:
Frontend: Add CodeGenAction::takeModule().

Modified:
    cfe/trunk/include/clang/Frontend/CodeGenAction.h
    cfe/trunk/lib/Frontend/CodeGenAction.cpp

Modified: cfe/trunk/include/clang/Frontend/CodeGenAction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/CodeGenAction.h?rev=97111&r1=97110&r2=97111&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/CodeGenAction.h (original)
+++ cfe/trunk/include/clang/Frontend/CodeGenAction.h Wed Feb 24 22:37:50 2010
@@ -8,18 +8,31 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Frontend/FrontendAction.h"
+#include "llvm/ADT/OwningPtr.h"
+
+namespace llvm {
+  class Module;
+}
 
 namespace clang {
 
 class CodeGenAction : public ASTFrontendAction {
 private:
   unsigned Act;
+  llvm::OwningPtr<llvm::Module> TheModule;
 
 protected:
   CodeGenAction(unsigned _Act);
 
   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
                                          llvm::StringRef InFile);
+
+  virtual void EndSourceFileAction();
+
+public:
+  /// takeModule - Take the generated LLVM module, for use after the action has
+  /// been run. The result may be null on failure.
+  llvm::Module *takeModule();
 };
 
 class EmitAssemblyAction : public CodeGenAction {

Modified: cfe/trunk/lib/Frontend/CodeGenAction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CodeGenAction.cpp?rev=97111&r1=97110&r2=97111&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CodeGenAction.cpp (original)
+++ cfe/trunk/lib/Frontend/CodeGenAction.cpp Wed Feb 24 22:37:50 2010
@@ -62,7 +62,7 @@
 
     llvm::OwningPtr<CodeGenerator> Gen;
 
-    llvm::Module *TheModule;
+    llvm::OwningPtr<llvm::Module> TheModule;
     llvm::TargetData *TheTargetData;
 
     mutable FunctionPassManager *CodeGenPasses;
@@ -97,7 +97,7 @@
       LLVMIRGeneration("LLVM IR Generation Time"),
       CodeGenerationTime("Code Generation Time"),
       Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)),
-      TheModule(0), TheTargetData(0),
+      TheTargetData(0),
       CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) {
 
       if (AsmOutStream)
@@ -109,12 +109,13 @@
 
     ~BackendConsumer() {
       delete TheTargetData;
-      delete TheModule;
       delete CodeGenPasses;
       delete PerModulePasses;
       delete PerFunctionPasses;
     }
 
+    llvm::Module *takeModule() { return TheModule.take(); }
+
     virtual void Initialize(ASTContext &Ctx) {
       Context = &Ctx;
 
@@ -123,7 +124,7 @@
 
       Gen->Initialize(Ctx);
 
-      TheModule = Gen->GetModule();
+      TheModule.reset(Gen->GetModule());
       TheTargetData = new llvm::TargetData(Ctx.Target.getTargetDescription());
 
       if (llvm::TimePassesIsEnabled)
@@ -179,7 +180,7 @@
 
 FunctionPassManager *BackendConsumer::getCodeGenPasses() const {
   if (!CodeGenPasses) {
-    CodeGenPasses = new FunctionPassManager(TheModule);
+    CodeGenPasses = new FunctionPassManager(&*TheModule);
     CodeGenPasses->add(new TargetData(*TheTargetData));
   }
 
@@ -197,7 +198,7 @@
 
 FunctionPassManager *BackendConsumer::getPerFunctionPasses() const {
   if (!PerFunctionPasses) {
-    PerFunctionPasses = new FunctionPassManager(TheModule);
+    PerFunctionPasses = new FunctionPassManager(&*TheModule);
     PerFunctionPasses->add(new TargetData(*TheTargetData));
   }
 
@@ -391,11 +392,12 @@
   if (!M) {
     // The module has been released by IR gen on failures, do not
     // double free.
-    TheModule = 0;
+    TheModule.take();
     return;
   }
 
-  assert(TheModule == M && "Unexpected module change during IR generation");
+  assert(TheModule.get() == M &&
+         "Unexpected module change during IR generation");
 
   CreatePasses();
   if (!AddEmitPasses())
@@ -433,6 +435,22 @@
 
 CodeGenAction::CodeGenAction(unsigned _Act) : Act(_Act) {}
 
+void CodeGenAction::EndSourceFileAction() {
+  // If the consumer creation failed, do nothing.
+  if (!getCompilerInstance().hasASTConsumer())
+    return;
+
+  // Steal the module from the consumer.
+  BackendConsumer *Consumer = static_cast<BackendConsumer*>(
+    &getCompilerInstance().getASTConsumer());
+
+  TheModule.reset(Consumer->takeModule());
+}
+
+llvm::Module *CodeGenAction::takeModule() {
+  return TheModule.take();
+}
+
 ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
                                               llvm::StringRef InFile) {
   BackendAction BA = static_cast<BackendAction>(Act);





More information about the cfe-commits mailing list