remove SectionFlags::Small: it is only used on Xcore, and we'll find
[oota-llvm.git] / include / llvm / Support / FormattedStream.h
index ecfa4b8ca8c2813b40e74b89103631283945b0df..dc30cdb5fe70dc049c3e63b7581df5bda4e49c53 100644 (file)
@@ -33,7 +33,8 @@ namespace llvm
     const static bool PRESERVE_STREAM = false;
     
   private:
-    /// TheStream - The real stream we output to.
+    /// TheStream - The real stream we output to. We set it to be
+    /// unbuffered, since we're already doing our own buffering.
     ///
     raw_ostream *TheStream;
     /// DeleteStream - Do we need to delete TheStream in the
@@ -46,7 +47,7 @@ namespace llvm
     ///
     unsigned Column;
 
-    virtual void write_impl(const char *Ptr, unsigned Size) {
+    virtual void write_impl(const char *Ptr, size_t Size) {
       ComputeColumn(Ptr, Size);
       TheStream->write(Ptr, Size);
     }
@@ -62,7 +63,7 @@ namespace llvm
     /// ComputeColumn - Examine the current output and figure out
     /// which column we end up in after output.
     ///
-    void ComputeColumn(const char *Ptr, unsigned Size);
+    void ComputeColumn(const char *Ptr, size_t Size);
 
   public:
     /// formatted_raw_ostream - Open the specified file for
@@ -70,23 +71,38 @@ namespace llvm
     /// put into ErrorInfo, and the stream should be immediately
     /// destroyed; the string will be empty if no error occurred.
     ///
-    /// \param Filename - The file to open. If this is "-" then the
-    /// stream will use stdout instead.
-    /// \param Binary - The file should be opened in binary mode on
-    /// platforms that support this distinction.
+    /// As a side effect, the given Stream is set to be Unbuffered.
+    /// This is because formatted_raw_ostream does its own buffering,
+    /// so it doesn't want another layer of buffering to be happening
+    /// underneath it.
+    ///
     formatted_raw_ostream(raw_ostream &Stream, bool Delete = false) 
-        : raw_ostream(), TheStream(&Stream), DeleteStream(Delete), Column(0) {}
-    explicit formatted_raw_ostream() 
+      : raw_ostream(), TheStream(0), DeleteStream(false), Column(0) {
+      setStream(Stream, Delete);
+    }
+    explicit formatted_raw_ostream()
       : raw_ostream(), TheStream(0), DeleteStream(false), Column(0) {}
 
     ~formatted_raw_ostream() {
       if (DeleteStream)
-        delete &TheStream;
+        delete TheStream;
     }
     
     void setStream(raw_ostream &Stream, bool Delete = false) {
+      if (DeleteStream)
+        delete TheStream;
+
       TheStream = &Stream;
       DeleteStream = Delete;
+
+      // This formatted_raw_ostream inherits from raw_ostream, so it'll do its
+      // own buffering, and it doesn't need or want TheStream to do another
+      // layer of buffering underneath. Resize the buffer to what TheStream
+      // had been using, and tell TheStream not to do its own buffering.
+      TheStream->flush();
+      if (size_t BufferSize = TheStream->GetNumBytesInBuffer())
+        SetBufferSize(BufferSize);
+      TheStream->SetUnbuffered();
     }
 
     /// PadToColumn - Align the output to some column number.