[llvm-commits] [llvm] r111639 - in /llvm/trunk: include/llvm/Support/raw_ostream.h lib/Support/raw_ostream.cpp

Dan Gohman gohman at apple.com
Fri Aug 20 09:34:20 PDT 2010


Author: djg
Date: Fri Aug 20 11:34:20 2010
New Revision: 111639

URL: http://llvm.org/viewvc/llvm-project?rev=111639&view=rev
Log:
Move raw_ostream's Error flag into raw_fd_ostream, as that's the only
class which is using it.

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=111639&r1=111638&r2=111639&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/raw_ostream.h (original)
+++ llvm/trunk/include/llvm/Support/raw_ostream.h Fri Aug 20 11:34:20 2010
@@ -58,10 +58,6 @@
     ExternalBuffer
   } BufferMode;
 
-  /// 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 {
@@ -77,7 +73,7 @@
   };
 
   explicit raw_ostream(bool unbuffered=false)
-    : BufferMode(unbuffered ? Unbuffered : InternalBuffer), Error(false) {
+    : BufferMode(unbuffered ? Unbuffered : InternalBuffer) {
     // Start out ready to flush.
     OutBufStart = OutBufEnd = OutBufCur = 0;
   }
@@ -87,21 +83,6 @@
   /// tell - Return the current offset with the file.
   uint64_t tell() const { return current_pos() + GetNumBytesInBuffer(); }
 
-  /// has_error - Return the value of the flag in this raw_ostream indicating
-  /// whether an output error has been encountered.
-  /// This doesn't implicitly flush any pending output.
-  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,
-  /// report_fatal_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
   //===--------------------------------------------------------------------===//
@@ -285,10 +266,6 @@
   /// underlying output mechanism.
   virtual size_t preferred_buffer_size() const;
 
-  /// error_detected - Set the flag indicating that an output error has
-  /// been encountered.
-  void error_detected() { Error = true; }
-
   /// getBufferStart - Return the beginning of the current stream buffer, or 0
   /// if the stream is unbuffered.
   const char *getBufferStart() const { return OutBufStart; }
@@ -319,6 +296,11 @@
 class raw_fd_ostream : public raw_ostream {
   int FD;
   bool ShouldClose;
+
+  /// Error This flag is true if an error of any kind has been detected.
+  ///
+  bool Error;
+
   uint64_t pos;
 
   /// write_impl - See raw_ostream::write_impl.
@@ -331,6 +313,10 @@
   /// preferred_buffer_size - Determine an efficient buffer size.
   virtual size_t preferred_buffer_size() const;
 
+  /// error_detected - Set the flag indicating that an output error has
+  /// been encountered.
+  void error_detected() { Error = true; }
+
 public:
 
   enum {
@@ -365,7 +351,8 @@
   /// ShouldClose is true, this closes the file when the stream is destroyed.
   raw_fd_ostream(int fd, bool shouldClose,
                  bool unbuffered=false) : raw_ostream(unbuffered), FD(fd),
-                                          ShouldClose(shouldClose) {}
+                                          ShouldClose(shouldClose),
+                                          Error(false) {}
 
   ~raw_fd_ostream();
 
@@ -382,6 +369,21 @@
   virtual raw_ostream &resetColor();
 
   virtual bool is_displayed() const;
+
+  /// has_error - Return the value of the flag in this raw_fd_ostream indicating
+  /// whether an output error has been encountered.
+  /// This doesn't implicitly flush any pending output.
+  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,
+  /// report_fatal_error is called to report the error. Use clear_error()
+  /// after handling the error to avoid this behavior.
+  void clear_error() {
+    Error = false;
+  }
 };
 
 /// raw_stdout_ostream - This is a stream that always prints to stdout.

Modified: llvm/trunk/lib/Support/raw_ostream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/raw_ostream.cpp?rev=111639&r1=111638&r2=111639&view=diff
==============================================================================
--- llvm/trunk/lib/Support/raw_ostream.cpp (original)
+++ llvm/trunk/lib/Support/raw_ostream.cpp Fri Aug 20 11:34:20 2010
@@ -57,13 +57,6 @@
 
   if (BufferMode == InternalBuffer)
     delete [] OutBufStart;
-
-  // If there are any pending errors, report them now. Clients wishing
-  // to avoid report_fatal_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)
-    report_fatal_error("IO failure on output stream.");
 }
 
 // An out of line virtual method to provide a home for the class vtable.
@@ -370,7 +363,7 @@
 /// stream should be immediately destroyed; the string will be empty
 /// if no error occurred.
 raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
-                               unsigned Flags) : pos(0) {
+                               unsigned Flags) : Error(false), pos(0) {
   assert(Filename != 0 && "Filename is null");
   // Verify that we don't have both "append" and "excl".
   assert((!(Flags & F_Excl) || !(Flags & F_Append)) &&
@@ -418,14 +411,22 @@
 }
 
 raw_fd_ostream::~raw_fd_ostream() {
-  if (FD < 0) return;
-  flush();
-  if (ShouldClose)
-    while (::close(FD) != 0)
-      if (errno != EINTR) {
-        error_detected();
-        break;
-      }
+  if (FD >= 0) {
+    flush();
+    if (ShouldClose)
+      while (::close(FD) != 0)
+        if (errno != EINTR) {
+          error_detected();
+          break;
+        }
+  }
+
+  // If there are any pending errors, report them now. Clients wishing
+  // to avoid report_fatal_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 (has_error())
+    report_fatal_error("IO failure on output stream.");
 }
 
 





More information about the llvm-commits mailing list