dstenb updated this revision to Diff 148577.
dstenb retitled this revision from "[Tooling] Clean up tmp files when creating 
a fixed compilation database" to "[Driver] Clean up tmp files when deleting 
Compilation objects".
dstenb edited the summary of this revision.
dstenb added a comment.

I have now updated the patch so that the files are removed when deleting 
Compilation objects.

Expect for the fact that we now also remove files created in 
stripPositionalArgs, the intention is that the removal behavior remains as 
before.


https://reviews.llvm.org/D45686

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


Index: lib/Driver/Driver.cpp
===================================================================
--- lib/Driver/Driver.cpp
+++ lib/Driver/Driver.cpp
@@ -1243,9 +1243,6 @@
 
   // 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;
@@ -1362,9 +1359,6 @@
 
   C.ExecuteJobs(C.getJobs(), FailingCommands);
 
-  // Remove temp files.
-  C.CleanupFileList(C.getTempFiles());
-
   // If the command succeeded, we are done.
   if (FailingCommands.empty())
     return 0;
Index: lib/Driver/Compilation.cpp
===================================================================
--- lib/Driver/Compilation.cpp
+++ lib/Driver/Compilation.cpp
@@ -38,13 +38,19 @@
                          InputArgList *_Args, DerivedArgList *_TranslatedArgs,
                          bool ContainsError)
     : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
-      TranslatedArgs(_TranslatedArgs), ContainsError(ContainsError) {
+      TranslatedArgs(_TranslatedArgs), ContainsError(ContainsError),
+      SaveTempsEnabled(D.isSaveTempsEnabled()) {
   // The offloading host toolchain is the default toolchain.
   OrderedOffloadingToolchains.insert(
       std::make_pair(Action::OFK_Host, &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 (!SaveTempsEnabled)
+    CleanupFileList(TempFiles);
+
   delete TranslatedArgs;
   delete Args;
 
@@ -245,6 +251,10 @@
   AllActions.clear();
   Jobs.clear();
 
+  // Remove temporary files.
+  if (!SaveTempsEnabled)
+    CleanupFileList(TempFiles);
+
   // Clear temporary/results file lists.
   TempFiles.clear();
   ResultFiles.clear();
@@ -262,6 +272,9 @@
 
   // Redirect stdout/stderr to /dev/null.
   Redirects = {None, {""}, {""}};
+
+  // Temporary files added by diagnostics should be kept.
+  SaveTempsEnabled = true;
 }
 
 StringRef Compilation::getSysRoot() const {
Index: include/clang/Driver/Compilation.h
===================================================================
--- include/clang/Driver/Compilation.h
+++ include/clang/Driver/Compilation.h
@@ -122,6 +122,9 @@
   /// Whether an error during the parsing of the input args.
   bool ContainsError;
 
+  /// Whether to save temporary files.
+  bool SaveTempsEnabled;
+
 public:
   Compilation(const Driver &D, const ToolChain &DefaultToolChain,
               llvm::opt::InputArgList *Args,


Index: lib/Driver/Driver.cpp
===================================================================
--- lib/Driver/Driver.cpp
+++ lib/Driver/Driver.cpp
@@ -1243,9 +1243,6 @@
 
   // 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;
@@ -1362,9 +1359,6 @@
 
   C.ExecuteJobs(C.getJobs(), FailingCommands);
 
-  // Remove temp files.
-  C.CleanupFileList(C.getTempFiles());
-
   // If the command succeeded, we are done.
   if (FailingCommands.empty())
     return 0;
Index: lib/Driver/Compilation.cpp
===================================================================
--- lib/Driver/Compilation.cpp
+++ lib/Driver/Compilation.cpp
@@ -38,13 +38,19 @@
                          InputArgList *_Args, DerivedArgList *_TranslatedArgs,
                          bool ContainsError)
     : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
-      TranslatedArgs(_TranslatedArgs), ContainsError(ContainsError) {
+      TranslatedArgs(_TranslatedArgs), ContainsError(ContainsError),
+      SaveTempsEnabled(D.isSaveTempsEnabled()) {
   // The offloading host toolchain is the default toolchain.
   OrderedOffloadingToolchains.insert(
       std::make_pair(Action::OFK_Host, &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 (!SaveTempsEnabled)
+    CleanupFileList(TempFiles);
+
   delete TranslatedArgs;
   delete Args;
 
@@ -245,6 +251,10 @@
   AllActions.clear();
   Jobs.clear();
 
+  // Remove temporary files.
+  if (!SaveTempsEnabled)
+    CleanupFileList(TempFiles);
+
   // Clear temporary/results file lists.
   TempFiles.clear();
   ResultFiles.clear();
@@ -262,6 +272,9 @@
 
   // Redirect stdout/stderr to /dev/null.
   Redirects = {None, {""}, {""}};
+
+  // Temporary files added by diagnostics should be kept.
+  SaveTempsEnabled = true;
 }
 
 StringRef Compilation::getSysRoot() const {
Index: include/clang/Driver/Compilation.h
===================================================================
--- include/clang/Driver/Compilation.h
+++ include/clang/Driver/Compilation.h
@@ -122,6 +122,9 @@
   /// Whether an error during the parsing of the input args.
   bool ContainsError;
 
+  /// Whether to save temporary files.
+  bool SaveTempsEnabled;
+
 public:
   Compilation(const Driver &D, const ToolChain &DefaultToolChain,
               llvm::opt::InputArgList *Args,
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to