[driver] Associate a JobAction with each result file.  This enables the driver
to delete result files for only those commands that fail.
Part of rdar://12984531

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173361 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Driver/Compilation.cpp b/lib/Driver/Compilation.cpp
index 9c7100f..96e6048 100644
--- a/lib/Driver/Compilation.cpp
+++ b/lib/Driver/Compilation.cpp
@@ -199,39 +199,56 @@
   }
 }
 
+bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
+  llvm::sys::Path P(File);
+  std::string Error;
+
+  // Don't try to remove files which we don't have write access to (but may be
+  // able to remove), or non-regular files. Underlying tools may have
+  // intentionally not overwritten them.
+  if (!P.canWrite() || !P.isRegularFile())
+    return true;
+
+  if (P.eraseFromDisk(false, &Error)) {
+    // Failure is only failure if the file exists and is "regular". There is
+    // a race condition here due to the limited interface of
+    // llvm::sys::Path, we want to know if the removal gave ENOENT.
+    
+    // FIXME: Grumble, P.exists() is broken. PR3837.
+    struct stat buf;
+    if (::stat(P.c_str(), &buf) == 0 ? (buf.st_mode & S_IFMT) == S_IFREG :
+        (errno != ENOENT)) {
+      if (IssueErrors)
+        getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
+          << Error;
+      return false;
+    }
+  }
+  return true;
+}
+
 bool Compilation::CleanupFileList(const ArgStringList &Files,
                                   bool IssueErrors) const {
   bool Success = true;
-
   for (ArgStringList::const_iterator
+         it = Files.begin(), ie = Files.end(); it != ie; ++it)
+    Success &= CleanupFile(*it, IssueErrors);
+  return Success;
+}
+
+bool Compilation::CleanupFileMap(const ArgStringMap &Files,
+                                 const JobAction *JA,
+                                 bool IssueErrors) const {
+  bool Success = true;
+  for (ArgStringMap::const_iterator
          it = Files.begin(), ie = Files.end(); it != ie; ++it) {
 
-    llvm::sys::Path P(*it);
-    std::string Error;
-
-    // Don't try to remove files which we don't have write access to (but may be
-    // able to remove), or non-regular files. Underlying tools may have
-    // intentionally not overwritten them.
-    if (!P.canWrite() || !P.isRegularFile())
+    // If specified, only delete the files associated with the JobAction.
+    // Otherwise, delete all files in the map.
+    if (JA && it->first != JA)
       continue;
-
-    if (P.eraseFromDisk(false, &Error)) {
-      // Failure is only failure if the file exists and is "regular". There is
-      // a race condition here due to the limited interface of
-      // llvm::sys::Path, we want to know if the removal gave ENOENT.
-
-      // FIXME: Grumble, P.exists() is broken. PR3837.
-      struct stat buf;
-      if (::stat(P.c_str(), &buf) == 0 ? (buf.st_mode & S_IFMT) == S_IFREG :
-                                         (errno != ENOENT)) {
-        if (IssueErrors)
-          getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
-            << Error;
-        Success = false;
-      }
-    }
+    Success &= CleanupFile(it->second, IssueErrors);
   }
-
   return Success;
 }
 
diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp
index dcb3c5f..dda7099 100644
--- a/lib/Driver/Driver.cpp
+++ b/lib/Driver/Driver.cpp
@@ -485,8 +485,9 @@
       << "\n\n********************";
   } else {
     // Failure, remove preprocessed files.
-    if (!C.getArgs().hasArg(options::OPT_save_temps))
+    if (!C.getArgs().hasArg(options::OPT_save_temps)) {
       C.CleanupFileList(C.getTempFiles(), true);
+    }
 
     Diag(clang::diag::note_drv_command_failed_diag_msg)
       << "Error generating preprocessed source(s).";
@@ -516,11 +517,12 @@
 
   // Otherwise, remove result files as well.
   if (!C.getArgs().hasArg(options::OPT_save_temps)) {
-    C.CleanupFileList(C.getResultFiles(), true);
+    const JobAction *JA = cast<JobAction>(&FailingCommand->getSource());
+    C.CleanupFileMap(C.getResultFiles(), JA, true);
 
     // Failure result files are valid unless we crashed.
     if (Res < 0)
-      C.CleanupFileList(C.getFailureResultFiles(), true);
+      C.CleanupFileMap(C.getFailureResultFiles(), JA, true);
   }
 
   // Print extra information about abnormal failures, if possible.
@@ -1417,7 +1419,7 @@
   if (AtTopLevel && !isa<DsymutilJobAction>(JA) &&
       !isa<VerifyJobAction>(JA)) {
     if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
-      return C.addResultFile(FinalOutput->getValue());
+      return C.addResultFile(FinalOutput->getValue(), &JA);
   }
 
   // Default to writing to stdout?
@@ -1487,9 +1489,9 @@
       BasePath = NamedOutput;
     else
       llvm::sys::path::append(BasePath, NamedOutput);
-    return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()));
+    return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()), &JA);
   } else {
-    return C.addResultFile(NamedOutput);
+    return C.addResultFile(NamedOutput, &JA);
   }
 }
 
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index 309e149..69340e6 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -228,6 +228,7 @@
 }
 
 void Clang::AddPreprocessingOptions(Compilation &C,
+                                    const JobAction &JA,
                                     const Driver &D,
                                     const ArgList &Args,
                                     ArgStringList &CmdArgs,
@@ -248,7 +249,7 @@
     const char *DepFile;
     if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
       DepFile = MF->getValue();
-      C.addFailureResultFile(DepFile);
+      C.addFailureResultFile(DepFile, &JA);
     } else if (Output.getType() == types::TY_Dependencies) {
       DepFile = Output.getFilename();
     } else if (A->getOption().matches(options::OPT_M) ||
@@ -256,7 +257,7 @@
       DepFile = "-";
     } else {
       DepFile = getDependencyFileName(Args, Inputs);
-      C.addFailureResultFile(DepFile);
+      C.addFailureResultFile(DepFile, &JA);
     }
     CmdArgs.push_back("-dependency-file");
     CmdArgs.push_back(DepFile);
@@ -2300,7 +2301,7 @@
   //
   // FIXME: Support -fpreprocessed
   if (types::getPreprocessedType(InputType) != types::TY_INVALID)
-    AddPreprocessingOptions(C, D, Args, CmdArgs, Output, Inputs);
+    AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);
 
   // Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
   // that "The compiler can only warn and ignore the option if not recognized".
diff --git a/lib/Driver/Tools.h b/lib/Driver/Tools.h
index dcfd311..a2b3c07 100644
--- a/lib/Driver/Tools.h
+++ b/lib/Driver/Tools.h
@@ -40,6 +40,7 @@
 
   private:
     void AddPreprocessingOptions(Compilation &C,
+                                 const JobAction &JA,
                                  const Driver &D,
                                  const ArgList &Args,
                                  ArgStringList &CmdArgs,