]> granicus.if.org Git - clang/commitdiff
Split GenerateModuleAction into its own action, which will start
authorDouglas Gregor <dgregor@apple.com>
Tue, 15 Nov 2011 21:49:36 +0000 (21:49 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 15 Nov 2011 21:49:36 +0000 (21:49 +0000)
differing from GeneratePCHAction fairly soon.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@144703 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Frontend/FrontendActions.h
lib/Frontend/CompilerInstance.cpp
lib/Frontend/FrontendActions.cpp
lib/FrontendTool/ExecuteCompilerInvocation.cpp

index 72a3d908476a581874ff1f895bfd3788a42bca6c..ba1dbb8f2389a66b00c28b6c8dd27db5e9ba1807 100644 (file)
@@ -67,22 +67,40 @@ protected:
 };
 
 class GeneratePCHAction : public ASTFrontendAction {
-  bool MakeModule;
-  
 protected:
   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
                                          StringRef InFile);
 
   virtual TranslationUnitKind getTranslationUnitKind() { 
-    return MakeModule? TU_Module : TU_Prefix;
+    return TU_Prefix;
   }
 
   virtual bool hasASTFileSupport() const { return false; }
 
 public:
-  /// \brief Create a new action
-  explicit GeneratePCHAction(bool MakeModule) : MakeModule(MakeModule) { }
+  /// \brief Compute the AST consumer arguments that will be used to
+  /// create the PCHGenerator instance returned by CreateASTConsumer.
+  ///
+  /// \returns true if an error occurred, false otherwise.
+  static bool ComputeASTConsumerArguments(CompilerInstance &CI,
+                                          StringRef InFile,
+                                          std::string &Sysroot,
+                                          std::string &OutputFile,
+                                          raw_ostream *&OS);
+};
+
+class GenerateModuleAction : public ASTFrontendAction {
+protected:
+  virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
+                                         StringRef InFile);
+  
+  virtual TranslationUnitKind getTranslationUnitKind() { 
+    return TU_Module;
+  }
   
+  virtual bool hasASTFileSupport() const { return false; }
+  
+public:
   /// \brief Compute the AST consumer arguments that will be used to
   /// create the PCHGenerator instance returned by CreateASTConsumer.
   ///
index ea2c3bd6c6b86b65e25125f9010f297ab01300d1..0a32cb96f70d29d1b82e4f6605165bcd65a82b3b 100644 (file)
@@ -647,13 +647,14 @@ bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
     llvm::EnableStatistics();
 
   for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
-    const std::string &InFile = getFrontendOpts().Inputs[i].second;
-
+    InputKind InKind = getFrontendOpts().Inputs[i].first;
+    std::string InFile = getFrontendOpts().Inputs[i].second;
+    
     // Reset the ID tables if we are reusing the SourceManager.
     if (hasSourceManager())
       getSourceManager().clearIDTables();
 
-    if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) {
+    if (Act.BeginSourceFile(*this, InFile, InKind)) {
       Act.Execute();
       Act.EndSourceFile();
     }
@@ -698,7 +699,7 @@ static InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) {
 namespace {
   struct CompileModuleData {
     CompilerInstance &Instance;
-    GeneratePCHAction &CreateModuleAction;
+    GenerateModuleAction &CreateModuleAction;
   };
 }
 
@@ -1023,7 +1024,7 @@ static void compileModule(CompilerInstance &ImportingInstance,
                              /*ShouldCloneClient=*/true);
 
   // Construct a module-generating action.
-  GeneratePCHAction CreateModuleAction(true);
+  GenerateModuleAction CreateModuleAction;
 
   // Execute the action to actually build the module in-place. Use a separate
   // thread so that we get a stack large enough.
index 6f84da96300c7422ed4ed7d54791fc809d381afb..2fcbc67ef1189c433f35d7a461e3f55894814edd 100644 (file)
@@ -85,7 +85,7 @@ ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
 
   if (!CI.getFrontendOpts().RelocatablePCH)
     Sysroot.clear();
-  return new PCHGenerator(CI.getPreprocessor(), OutputFile, MakeModule, 
+  return new PCHGenerator(CI.getPreprocessor(), OutputFile, /*Module=*/false, 
                           Sysroot, OS);
 }
 
@@ -113,6 +113,44 @@ bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
   return false;
 }
 
+ASTConsumer *GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
+                                                     StringRef InFile) {
+  std::string Sysroot;
+  std::string OutputFile;
+  raw_ostream *OS = 0;
+  if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
+    return 0;
+  
+  if (!CI.getFrontendOpts().RelocatablePCH)
+    Sysroot.clear();
+  return new PCHGenerator(CI.getPreprocessor(), OutputFile, /*Module=*/true, 
+                          Sysroot, OS);
+}
+
+bool GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI,
+                                                       StringRef InFile,
+                                                       std::string &Sysroot,
+                                                       std::string &OutputFile,
+                                                       raw_ostream *&OS) {
+  Sysroot = CI.getHeaderSearchOpts().Sysroot;
+  if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
+    CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
+    return true;
+  }
+  
+  // We use createOutputFile here because this is exposed via libclang, and we
+  // must disable the RemoveFileOnSignal behavior.
+  // We use a temporary to avoid race conditions.
+  OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
+                           /*RemoveFileOnSignal=*/false, InFile,
+                           /*Extension=*/"", /*useTemporary=*/true);
+  if (!OS)
+    return true;
+  
+  OutputFile = CI.getFrontendOpts().OutputFile;
+  return false;
+}
+
 ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
                                                  StringRef InFile) {
   return new ASTConsumer();
index c9af3cc3ff1e04634d436cda402a771988f06928..ea50a76a39e286d0d764ed05e9a78347970345d3 100644 (file)
@@ -49,8 +49,8 @@ static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
   case EmitCodeGenOnly:        return new EmitCodeGenOnlyAction();
   case EmitObj:                return new EmitObjAction();
   case FixIt:                  return new FixItAction();
-  case GenerateModule:         return new GeneratePCHAction(true);
-  case GeneratePCH:            return new GeneratePCHAction(false);
+  case GenerateModule:         return new GenerateModuleAction();
+  case GeneratePCH:            return new GeneratePCHAction();
   case GeneratePTH:            return new GeneratePTHAction();
   case InitOnly:               return new InitOnlyAction();
   case ParseSyntaxOnly:        return new SyntaxOnlyAction();