llvm.org GIT mirror llvm / af0636f
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 Dan Gohman 10 years ago
4 changed file(s) with 75 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
1818
1919 namespace llvm
2020 {
21 class formatted_tool_output_file;
22
2123 /// formatted_raw_ostream - Formatted raw_fd_ostream to handle
2224 /// asm-specific constructs.
2325 ///
2426 class formatted_raw_ostream : public raw_ostream {
27 friend class formatted_tool_output_file;
28
2529 public:
2630 /// DELETE_STREAM - Tell the destructor to delete the held stream.
2731 ///
135139 }
136140 };
137141
142 /// formatted_tool_output_file - This is a subclass of formatted_raw_ostream
143 /// for use when the underlying stream is a tool_output_file. It exposes
144 /// the keep() member function.
145 class formatted_tool_output_file : public formatted_raw_ostream {
146 public:
147 formatted_tool_output_file(tool_output_file &Stream, bool Delete = false)
148 : formatted_raw_ostream(Stream, Delete) {}
149
150 formatted_tool_output_file() {}
151
152 ~formatted_tool_output_file();
153
154 void setStream(tool_output_file &Stream, bool Delete = false) {
155 return formatted_raw_ostream::setStream(Stream, Delete);
156 }
157
158 void keep() { return static_cast(TheStream)->keep(); }
159 };
160
138161 /// fouts() - This returns a reference to a formatted_raw_ostream for
139162 /// standard output. Use it like: fouts() << "foo" << "bar";
140163 formatted_raw_ostream &fouts();
369369 ~raw_fd_ostream();
370370
371371 /// close - Manually flush the stream and close the file.
372 /// Note that this does not call fsync.
372373 void close();
373374
374375 /// seek - Flushes the stream and repositions the underlying file descriptor
375 /// positition to the offset specified from the beginning of the file.
376 /// positition to the offset specified from the beginning of the file.
376377 uint64_t seek(uint64_t off);
377378
378379 virtual raw_ostream &changeColor(enum Colors colors, bool bold=false,
483484 ~raw_null_ostream();
484485 };
485486
487 /// tool_output_file - This class behaves like a raw_fd_ostream but adds a
488 /// few extra features commonly needed for compiler-like tool output files:
489 /// - The file is automatically deleted if the process is killed.
490 /// - The file is automatically deleted when the tool_output_file
491 /// object is destroyed unless the client calls keep().
492 class tool_output_file : public raw_fd_ostream {
493 std::string Filename;
494 bool Keep;
495 public:
496 tool_output_file(const char *filename, std::string &ErrorInfo,
497 unsigned Flags = 0);
498
499 ~tool_output_file();
500
501 /// keep - Indicate that the tool's job wrt this output file has been
502 /// successful and the file should not be deleted.
503 void keep() { Keep = true; }
504 };
505
486506 } // end llvm namespace
487507
488508 #endif
9797 static formatted_raw_ostream S(dbgs());
9898 return S;
9999 }
100
101 /// ~formatted_tool_output_file - Out-of-line destructor.
102 formatted_tool_output_file::~formatted_tool_output_file() {}
1818 #include "llvm/Config/config.h"
1919 #include "llvm/Support/Compiler.h"
2020 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/System/Signals.h"
2122 #include "llvm/ADT/STLExtras.h"
2223 #include
2324 #include
668669 uint64_t raw_null_ostream::current_pos() const {
669670 return 0;
670671 }
672
673 //===----------------------------------------------------------------------===//
674 // tool_output_file
675 //===----------------------------------------------------------------------===//
676
677 /// SetupRemoveOnSignal - This is a helper for tool_output_file's constructor
678 /// to allow the signal handlers to be installed before constructing the
679 /// base class raw_fd_ostream.
680 static const char *SetupRemoveOnSignal(const char *Filename) {
681 // Arrange for the file to be deleted if the process is killed.
682 if (strcmp(Filename, "-") != 0)
683 sys::RemoveFileOnSignal(sys::Path(Filename));
684 return Filename;
685 }
686
687 tool_output_file::tool_output_file(const char *filename, std::string &ErrorInfo,
688 unsigned Flags)
689 : raw_fd_ostream(SetupRemoveOnSignal(filename), ErrorInfo, Flags),
690 Filename(filename),
691 Keep(!ErrorInfo.empty() /* If open fails, no cleanup is needed. */) {
692 }
693
694 tool_output_file::~tool_output_file() {
695 // Delete the file if the client hasn't told us not to.
696 if (!Keep && Filename != "-")
697 sys::Path(Filename).eraseFromDisk();
698 }