[llvm-commits] [llvm] r75857 - in /llvm/trunk: include/llvm/Support/raw_ostream.h lib/Support/raw_ostream.cpp
Dan Gohman
gohman at apple.com
Wed Jul 15 16:25:35 PDT 2009
Author: djg
Date: Wed Jul 15 18:25:33 2009
New Revision: 75857
URL: http://llvm.org/viewvc/llvm-project?rev=75857&view=rev
Log:
Change raw_ostream so that it doesn't call llvm_report_error
immediately on every output error. Instead, add a flag to
raw_ostream, and set the flag whenever an error is detected.
The flag can be queried and cleared from the public API. This
gives applications more flexibility to handling errors in
application-specific ways.
If the flag is not cleared when the raw_ostream is destructed,
llvm_report_error is called from the destructor. This ensures
that errors are not implicitly silenced, and provides
convenient default behavior for tools like llc and opt.
Clients wishing to avoid llvm_report_error calls from
raw_ostream should check for errors and clear the error flag.
Modified:
llvm/trunk/include/llvm/Support/raw_ostream.h
llvm/trunk/lib/Support/raw_ostream.cpp
Modified: llvm/trunk/include/llvm/Support/raw_ostream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/raw_ostream.h?rev=75857&r1=75856&r2=75857&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/raw_ostream.h (original)
+++ llvm/trunk/include/llvm/Support/raw_ostream.h Wed Jul 15 18:25:33 2009
@@ -44,6 +44,10 @@
char *OutBufStart, *OutBufEnd, *OutBufCur;
bool Unbuffered;
+ /// Error This flag is true if an error of any kind has been detected.
+ ///
+ bool Error;
+
public:
// color order matches ANSI escape sequence, don't change
enum Colors {
@@ -58,18 +62,31 @@
SAVEDCOLOR
};
- explicit raw_ostream(bool unbuffered=false) : Unbuffered(unbuffered) {
+ explicit raw_ostream(bool unbuffered=false)
+ : Unbuffered(unbuffered), Error(false) {
// Start out ready to flush.
OutBufStart = OutBufEnd = OutBufCur = 0;
}
- virtual ~raw_ostream() {
- delete [] OutBufStart;
- }
+ virtual ~raw_ostream();
/// tell - Return the current offset with the file.
uint64_t tell() { return current_pos() + GetNumBytesInBuffer(); }
+ /// has_error - Return the value of the flag in this raw_ostream indicating
+ /// whether an output error has been encountered.
+ bool has_error() const {
+ return Error;
+ };
+
+ /// clear_error - Set the flag read by has_error() to false. If the error
+ /// flag is set at the time when this raw_ostream's destructor is called,
+ /// llvm_report_error is called to report the error. Use clear_error()
+ /// after handling the error to avoid this behavior.
+ void clear_error() {
+ Error = false;
+ };
+
//===--------------------------------------------------------------------===//
// Configuration Interface
//===--------------------------------------------------------------------===//
@@ -213,6 +230,11 @@
/// counting the bytes currently in the buffer.
virtual uint64_t current_pos() = 0;
+protected:
+ /// error_detected - Set the flag indicating that an output error has
+ /// been encountered.
+ void error_detected() { Error = true; }
+
//===--------------------------------------------------------------------===//
// Private Interface
//===--------------------------------------------------------------------===//
@@ -311,8 +333,8 @@
//===----------------------------------------------------------------------===//
/// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
-/// simple adaptor class. It does not check for I/O errors; clients should use
-/// the underlying stream to detect errors.
+/// simple adaptor class. It does not check for output errors; clients should
+/// use the underlying stream to detect errors.
class raw_os_ostream : public raw_ostream {
std::ostream &OS;
@@ -332,7 +354,7 @@
};
/// raw_string_ostream - A raw_ostream that writes to an std::string. This is a
-/// simple adaptor class.
+/// simple adaptor class. This class does not encounter output errors.
class raw_string_ostream : public raw_ostream {
std::string &OS;
@@ -358,7 +380,8 @@
};
/// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
-/// SmallString. This is a simple adaptor class.
+/// SmallString. This is a simple adaptor class. This class does not
+/// encounter output errors.
class raw_svector_ostream : public raw_ostream {
SmallVectorImpl<char> &OS;
Modified: llvm/trunk/lib/Support/raw_ostream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/raw_ostream.cpp?rev=75857&r1=75856&r2=75857&view=diff
==============================================================================
--- llvm/trunk/lib/Support/raw_ostream.cpp (original)
+++ llvm/trunk/lib/Support/raw_ostream.cpp Wed Jul 15 18:25:33 2009
@@ -44,6 +44,16 @@
using namespace llvm;
+raw_ostream::~raw_ostream() {
+ delete [] OutBufStart;
+
+ // If there are any pending errors, report them now. Clients wishing
+ // to avoid llvm_report_error calls should check for errors with
+ // has_error() and clear the error flag with clear_error() before
+ // destructing raw_ostream objects which may have errors.
+ if (Error)
+ llvm_report_error("IO failure on output stream.");
+}
// An out of line virtual method to provide a home for the class vtable.
void raw_ostream::handle() {}
@@ -282,7 +292,7 @@
flush();
if (ShouldClose)
if (::close(FD) != 0)
- llvm_report_error("IO failure closing output stream.");
+ error_detected();
}
}
@@ -290,7 +300,7 @@
assert (FD >= 0 && "File already closed.");
pos += Size;
if (::write(FD, Ptr, Size) != (ssize_t) Size)
- llvm_report_error("IO failure writing to output stream.");
+ error_detected();
}
void raw_fd_ostream::close() {
@@ -298,7 +308,7 @@
ShouldClose = false;
flush();
if (::close(FD) != 0)
- llvm_report_error("IO failure closing output stream.");
+ error_detected();
FD = -1;
}
@@ -306,7 +316,7 @@
flush();
pos = ::lseek(FD, off, SEEK_SET);
if (pos != off)
- llvm_report_error("IO failure seeking on output stream.");
+ error_detected();
return pos;
}
More information about the llvm-commits
mailing list