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 | b87ad69350fecaf9f5ff31935417e267523f5c3e (patch) | |
| tree | 16604b23f2f27b89768fa74e4713cb4b819f107c /llvm/lib/Support/raw_ostream.cpp | |
| parent | 985d9e4ea8879bc173740eaab848a8b50ee626d6 (diff) | |
| download | llvm-b87ad69350fecaf9f5ff31935417e267523f5c3e.zip llvm-b87ad69350fecaf9f5ff31935417e267523f5c3e.tar.gz llvm-b87ad69350fecaf9f5ff31935417e267523f5c3e.tar.bz2 | |
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.
llvm-svn: 111595
Diffstat (limited to 'llvm/lib/Support/raw_ostream.cpp')
| -rw-r--r-- | llvm/lib/Support/raw_ostream.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp index 71a2ac5..81ffe4a 100644 --- a/llvm/lib/Support/raw_ostream.cpp +++ b/llvm/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(); +} |
