]> granicus.if.org Git - clang/commitdiff
[Driver] Clean up tmp files when deleting Compilation objects
authorDavid Stenberg <david.stenberg@ericsson.com>
Thu, 31 May 2018 09:05:22 +0000 (09:05 +0000)
committerDavid Stenberg <david.stenberg@ericsson.com>
Thu, 31 May 2018 09:05:22 +0000 (09:05 +0000)
Summary:
In rL327851 the createUniqueFile() and createTemporaryFile()
variants that do not return the file descriptors were changed to
create empty files, rather than only check if the paths are free.
This change was done in order to make the functions race-free.

That change led to clang-tidy (and possibly other tools) leaving
behind temporary assembly files, of the form placeholder-*, when
using a target that does not support the internal assembler.

The temporary files are created when building the Compilation
object in stripPositionalArgs(), as a part of creating the
compilation database for the arguments after the double-dash. The
files are created by Driver::GetNamedOutputPath().

Fix this issue by cleaning out temporary files at the deletion of
Compilation objects.

This fixes https://bugs.llvm.org/show_bug.cgi?id=37091.

Reviewers: klimek, sepavloff, arphaman, aaron.ballman, john.brawn, mehdi_amini, sammccall, bkramer, alexfh, JDevlieghere

Reviewed By: aaron.ballman, JDevlieghere

Subscribers: erichkeane, lebedev.ri, Ka-Ka, cfe-commits

Differential Revision: https://reviews.llvm.org/D45686

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

include/clang/Driver/Compilation.h
lib/Driver/Compilation.cpp
lib/Driver/Driver.cpp

index 0b4bb50059c9028b61042fb19a69b1025d81a93f..20eb07f6de8bd1871a979c4ed40a4cf446ac70e6 100644 (file)
@@ -122,6 +122,9 @@ class Compilation {
   /// Whether an error during the parsing of the input args.
   bool ContainsError;
 
+  /// Whether to keep temporary files regardless of -save-temps.
+  bool ForceKeepTempFiles = false;
+
 public:
   Compilation(const Driver &D, const ToolChain &DefaultToolChain,
               llvm::opt::InputArgList *Args,
index 2369999ce4cf4b556ad7dd1e4672361e798cec49..ca2525dd07fb901d503357156c6042d4626b8055 100644 (file)
@@ -45,6 +45,11 @@ Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
 }
 
 Compilation::~Compilation() {
+  // Remove temporary files. This must be done before arguments are freed, as
+  // the file names might be derived from the input arguments.
+  if (!TheDriver.isSaveTempsEnabled() && !ForceKeepTempFiles)
+    CleanupFileList(TempFiles);
+
   delete TranslatedArgs;
   delete Args;
 
@@ -245,6 +250,10 @@ void Compilation::initCompilationForDiagnostics() {
   AllActions.clear();
   Jobs.clear();
 
+  // Remove temporary files.
+  if (!TheDriver.isSaveTempsEnabled() && !ForceKeepTempFiles)
+    CleanupFileList(TempFiles);
+
   // Clear temporary/results file lists.
   TempFiles.clear();
   ResultFiles.clear();
@@ -262,6 +271,9 @@ void Compilation::initCompilationForDiagnostics() {
 
   // Redirect stdout/stderr to /dev/null.
   Redirects = {None, {""}, {""}};
+
+  // Temporary files added by diagnostics should be kept.
+  ForceKeepTempFiles = true;
 }
 
 StringRef Compilation::getSysRoot() const {
index 21e6e1601b7c9a924fe57f224e58f2de67952de3..c3ff9341a87ac8db895d059e8bec92c703842a23 100644 (file)
@@ -1253,9 +1253,6 @@ void Driver::generateCompilationDiagnostics(
 
   // If any of the preprocessing commands failed, clean up and exit.
   if (!FailingCommands.empty()) {
-    if (!isSaveTempsEnabled())
-      C.CleanupFileList(C.getTempFiles(), true);
-
     Diag(clang::diag::note_drv_command_failed_diag_msg)
         << "Error generating preprocessed source(s).";
     return;
@@ -1372,9 +1369,6 @@ int Driver::ExecuteCompilation(
 
   C.ExecuteJobs(C.getJobs(), FailingCommands);
 
-  // Remove temp files.
-  C.CleanupFileList(C.getTempFiles());
-
   // If the command succeeded, we are done.
   if (FailingCommands.empty())
     return 0;