diff options
author | Dan Gohman <gohman@apple.com> | 2010-08-20 00:48:10 +0000 |
---|---|---|
committer | Dan Gohman <gohman@apple.com> | 2010-08-20 00:48:10 +0000 |
commit | af0636f4d63e6ff2fd066d4594d8666459d1930d (patch) | |
tree | d32a8901c1ae44c37ab3055ec6b0db0759892c43 /lib | |
parent | f762fbe4fa421c91e20044ee009ddb57e25dd135 (diff) |
Introduce a new tool_output_file class, which extends raw_ostream with
functionality that most command-line tools need: ensuring that the
output file gets deleted if the tool is interrupted or encounters an
error.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@111595 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Support/FormattedStream.cpp | 3 | ||||
-rw-r--r-- | lib/Support/raw_ostream.cpp | 28 |
2 files changed, 31 insertions, 0 deletions
diff --git a/lib/Support/FormattedStream.cpp b/lib/Support/FormattedStream.cpp index c72b5a1751..77bdfab220 100644 --- a/lib/Support/FormattedStream.cpp +++ b/lib/Support/FormattedStream.cpp @@ -98,3 +98,6 @@ formatted_raw_ostream &llvm::fdbgs() { static formatted_raw_ostream S(dbgs()); return S; } + +/// ~formatted_tool_output_file - Out-of-line destructor. +formatted_tool_output_file::~formatted_tool_output_file() {} diff --git a/lib/Support/raw_ostream.cpp b/lib/Support/raw_ostream.cpp index 71a2ac5e09..81ffe4af84 100644 --- a/lib/Support/raw_ostream.cpp +++ b/lib/Support/raw_ostream.cpp @@ -19,6 +19,7 @@ #include "llvm/Config/config.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/System/Signals.h" #include "llvm/ADT/STLExtras.h" #include <cctype> #include <cerrno> @@ -669,3 +670,30 @@ void raw_null_ostream::write_impl(const char *Ptr, size_t Size) { uint64_t raw_null_ostream::current_pos() const { return 0; } + +//===----------------------------------------------------------------------===// +// tool_output_file +//===----------------------------------------------------------------------===// + +/// SetupRemoveOnSignal - This is a helper for tool_output_file's constructor +/// to allow the signal handlers to be installed before constructing the +/// base class raw_fd_ostream. +static const char *SetupRemoveOnSignal(const char *Filename) { + // Arrange for the file to be deleted if the process is killed. + if (strcmp(Filename, "-") != 0) + sys::RemoveFileOnSignal(sys::Path(Filename)); + return Filename; +} + +tool_output_file::tool_output_file(const char *filename, std::string &ErrorInfo, + unsigned Flags) + : raw_fd_ostream(SetupRemoveOnSignal(filename), ErrorInfo, Flags), + Filename(filename), + Keep(!ErrorInfo.empty() /* If open fails, no cleanup is needed. */) { +} + +tool_output_file::~tool_output_file() { + // Delete the file if the client hasn't told us not to. + if (!Keep && Filename != "-") + sys::Path(Filename).eraseFromDisk(); +} |