Split GenerateModuleAction into its own action, which will start
differing from GeneratePCHAction fairly soon.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@144703 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Frontend/CompilerInstance.cpp b/lib/Frontend/CompilerInstance.cpp
index ea2c3bd..0a32cb9 100644
--- a/lib/Frontend/CompilerInstance.cpp
+++ b/lib/Frontend/CompilerInstance.cpp
@@ -647,13 +647,14 @@
     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 @@
 namespace {
   struct CompileModuleData {
     CompilerInstance &Instance;
-    GeneratePCHAction &CreateModuleAction;
+    GenerateModuleAction &CreateModuleAction;
   };
 }
 
@@ -1023,7 +1024,7 @@
                              /*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.
diff --git a/lib/Frontend/FrontendActions.cpp b/lib/Frontend/FrontendActions.cpp
index 6f84da9..2fcbc67 100644
--- a/lib/Frontend/FrontendActions.cpp
+++ b/lib/Frontend/FrontendActions.cpp
@@ -85,7 +85,7 @@
 
   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 @@
   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();