Skip to content

Commit

Permalink
[Support] Make CleanupInstaller public (NFC) (llvm#86758)
Browse files Browse the repository at this point in the history
This can be used by others to automatically remove temp files.
  • Loading branch information
HaohaiWen authored Apr 9, 2024
1 parent d412047 commit 03ffb82
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
26 changes: 14 additions & 12 deletions llvm/include/llvm/Support/ToolOutputFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@

namespace llvm {

class CleanupInstaller {
public:
/// The name of the file.
std::string Filename;

/// The flag which indicates whether we should not delete the file.
bool Keep;

StringRef getFilename() { return Filename; }
explicit CleanupInstaller(StringRef Filename);
~CleanupInstaller();
};

/// This class contains a raw_fd_ostream and adds a few extra features commonly
/// needed for compiler-like tool output files:
/// - The file is automatically deleted if the process is killed.
Expand All @@ -28,18 +41,7 @@ class ToolOutputFile {
/// before the raw_fd_ostream is constructed and destructed after the
/// raw_fd_ostream is destructed. It installs cleanups in its constructor and
/// uninstalls them in its destructor.
class CleanupInstaller {
public:
/// The name of the file.
std::string Filename;

/// The flag which indicates whether we should not delete the file.
bool Keep;

StringRef getFilename() { return Filename; }
explicit CleanupInstaller(StringRef Filename);
~CleanupInstaller();
} Installer;
CleanupInstaller Installer;

/// Storage for the stream, if we're owning our own stream. This is
/// intentionally declared after Installer.
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Support/ToolOutputFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ using namespace llvm;

static bool isStdout(StringRef Filename) { return Filename == "-"; }

ToolOutputFile::CleanupInstaller::CleanupInstaller(StringRef Filename)
CleanupInstaller::CleanupInstaller(StringRef Filename)
: Filename(std::string(Filename)), Keep(false) {
// Arrange for the file to be deleted if the process is killed.
if (!isStdout(Filename))
sys::RemoveFileOnSignal(Filename);
}

ToolOutputFile::CleanupInstaller::~CleanupInstaller() {
CleanupInstaller::~CleanupInstaller() {
if (isStdout(Filename))
return;

Expand Down

0 comments on commit 03ffb82

Please sign in to comment.