diff options
-rw-r--r-- | clang-tools-extra/test/clang-tidy/pr37091.cpp | 10 | ||||
-rw-r--r-- | clang/include/clang/Driver/Compilation.h | 3 | ||||
-rw-r--r-- | clang/lib/Driver/Compilation.cpp | 12 | ||||
-rw-r--r-- | clang/lib/Driver/Driver.cpp | 6 |
4 files changed, 25 insertions, 6 deletions
diff --git a/clang-tools-extra/test/clang-tidy/pr37091.cpp b/clang-tools-extra/test/clang-tidy/pr37091.cpp new file mode 100644 index 0000000..e56115a --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/pr37091.cpp @@ -0,0 +1,10 @@ +// REQUIRES: shell +// RUN: rm -rf %t +// RUN: mkdir -p %t + +// This is a reproducer for PR37091. +// +// Verify that no temporary files are left behind by the clang-tidy invocation. + +// RUN: env TMPDIR=%t TEMP=%t TMP=%t clang-tidy %s -- --target=mips64 +// RUN: rmdir %t diff --git a/clang/include/clang/Driver/Compilation.h b/clang/include/clang/Driver/Compilation.h index 0b4bb500..20eb07f 100644 --- a/clang/include/clang/Driver/Compilation.h +++ b/clang/include/clang/Driver/Compilation.h @@ -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, diff --git a/clang/lib/Driver/Compilation.cpp b/clang/lib/Driver/Compilation.cpp index 2369999..ca2525d 100644 --- a/clang/lib/Driver/Compilation.cpp +++ b/clang/lib/Driver/Compilation.cpp @@ -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 { diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 21e6e16..c3ff934 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -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; |