Make changes suggested by Chris and eliminate newly-added raw_ostream
authorDavid Greene <greened@obbligato.org>
Fri, 10 Jul 2009 21:14:44 +0000 (21:14 +0000)
committerDavid Greene <greened@obbligato.org>
Fri, 10 Jul 2009 21:14:44 +0000 (21:14 +0000)
hooks as they're no longer needed.

The major change with this patch is to make formatted_raw_ostream usable
by any client of raw_ostream.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75283 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/CodeGen/AsmFormatter.h [new file with mode: 0644]
include/llvm/CodeGen/AsmStream.h [deleted file]
include/llvm/Support/FormattedStream.h [new file with mode: 0644]
include/llvm/Support/raw_ostream.h
lib/CodeGen/AsmStream.cpp [deleted file]
lib/Support/FormattedStream.cpp [new file with mode: 0644]
lib/Support/raw_ostream.cpp

diff --git a/include/llvm/CodeGen/AsmFormatter.h b/include/llvm/CodeGen/AsmFormatter.h
new file mode 100644 (file)
index 0000000..7d446bc
--- /dev/null
@@ -0,0 +1,65 @@
+//===-- llvm/CodeGen/AsmFormatter.h - Formatted asm framework ---*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains various I/O manipulators to pretty-print asm.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/FormattedStream.h"
+#include "llvm/Target/TargetAsmInfo.h"
+
+namespace llvm 
+{
+  /// AsmComment - An I/O manipulator to output an end-of-line comment
+  ///
+  class AsmComment : public Column {
+  private:
+    /// CommentColumn - The column at which to output a comment
+    ///
+    static const int CommentColumn = 60;
+    /// Text - The comment to output
+    ///
+    std::string Text;
+    /// TAI - Target information from the code generator
+    ///
+    const TargetAsmInfo &TAI;
+    
+  public:
+    AsmComment(const TargetAsmInfo &T) 
+        : Column(CommentColumn), Text(""), TAI(T) {}
+
+    AsmComment(const std::string &Cmnt,
+            const TargetAsmInfo &T) 
+        : Column(CommentColumn), Text(Cmnt), TAI(T) {}
+
+    /// operator() - Store a comments tring for later processing.
+    ///
+    AsmComment &operator()(const std::string &Cmnt) {
+      Text = Cmnt;
+      return *this;
+    }
+
+    /// operator() - Make Comment a functor invoktable by a stream
+    /// output operator.  This intentially hides Column's operator().
+    ///
+    formatted_raw_ostream &operator()(formatted_raw_ostream &Out) const {
+      Column::operator()(Out);
+      Out << TAI.getCommentString() << " " << Text;
+      return(Out);
+    }
+  };
+
+  /// operator<< - Support comment formatting in formatted streams.
+  ///
+  inline formatted_raw_ostream &operator<<(formatted_raw_ostream &Out,
+                                           const AsmComment &Func)
+  {
+    return(Func(Out));
+  }
+}
diff --git a/include/llvm/CodeGen/AsmStream.h b/include/llvm/CodeGen/AsmStream.h
deleted file mode 100644 (file)
index 6b17c73..0000000
+++ /dev/null
@@ -1,179 +0,0 @@
-//===-- llvm/CodeGen/AsmStream.h - AsmStream Framework --------*- C++ -*-===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file contains raw_ostream implementations for ASM printers to
-// do things like pretty-print comments.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_CODEGEN_ASMSTREAM_H
-#define LLVM_CODEGEN_ASMSTREAM_H
-
-#include "llvm/Target/TargetAsmInfo.h"
-#include "llvm/Support/raw_ostream.h"
-
-namespace llvm 
-{
-  /// raw_asm_fd_ostream - Formatted raw_fd_ostream to handle
-  /// asm-specific constructs
-  ///
-  class raw_asm_fd_ostream : public raw_fd_ostream {
-  private:
-    bool formatted;
-    int column;
-
-  protected:
-    void ComputeColumn(void) {
-      if (formatted) {
-        // Keep track of the current column by scanning the string for
-        // special characters
-
-        // Find the last newline.  This is our column start.  If there
-        // is no newline, start with the current column.
-        char *nlpos = NULL;        
-        for (char *pos = CurBufPtr(), *epos = StartBufPtr(); pos > epos; --pos) {
-          if (*(pos-1) == '\n') {
-            nlpos = pos-1;
-            // The newline will be counted, setting this to zero.  We
-            // need to do it this way in case nlpos is CurBufPtr().
-            column = -1;
-            break;
-          }
-        }
-
-        if (nlpos == NULL) {
-          nlpos = StartBufPtr();
-        }
-
-        // Walk through looking for tabs and advance column as appropriate
-        for (char *pos = nlpos, *epos = CurBufPtr(); pos != epos; ++pos) {
-          ++column;
-          if (*pos == '\t') {
-            // Advance to next tab stop (every eight characters)
-            column += ((8 - (column & 0x7)) & 0x7);
-            assert(!(column & 0x3) && "Column out of alignment");
-          }
-        }
-      }
-    }
-
-    virtual void AboutToFlush(void) {
-      ComputeColumn();
-    }
-
-  public:
-    /// raw_asm_fd_ostream - Open the specified file for writing. If
-    /// an error occurs, information about the error is 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.
-    raw_asm_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo) 
-        : raw_fd_ostream(Filename, Binary, ErrorInfo),
-            formatted(!Binary), column(0) {}
-
-    /// raw_asm_fd_ostream ctor - FD is the file descriptor that this
-    /// writes to.  If ShouldClose is true, this closes the file when
-    /// the stream is destroyed.
-    raw_asm_fd_ostream(int fd, bool shouldClose, 
-                       bool unbuffered=false)
-        : raw_fd_ostream(fd, shouldClose, unbuffered),
-            formatted(true), column(0) {
-      if (unbuffered) {
-        assert(0 && "asm stream must be buffered");
-        // Force buffering
-        SetBufferSize();
-      }
-    }
-
-
-    /// SetColumn - Align the output to some column number
-    ///
-    void setColumn(int newcol, int minpad = 0) {
-      if (formatted) {
-        flush();
-
-        // Output spaces until we reach the desired column
-        int num = newcol - column;
-        if (num <= 0) {
-          num = minpad;
-        }
-
-        // TODO: Write a whole string at a time
-        while (num-- > 0) {
-          write(' ');
-        }
-      }
-    };
-  };
-
-  /// Column - An I/O manipulator to advance the output to a certain column
-  ///
-  class Column {
-  private:
-    int column;
-
-  public:
-    explicit Column(int c) 
-        : column(c) {}
-
-    raw_asm_fd_ostream &operator()(raw_asm_fd_ostream &out) const {
-      // Make at least one space before the comment
-      out.setColumn(column, 1);
-      return(out);
-    }
-  };
-
-  inline raw_asm_fd_ostream &operator<<(raw_asm_fd_ostream &out, const Column &column)
-  {
-    return(column(out));
-  }
-
-  /// Comment - An I/O manipulator to output an end-of-line comment
-  ///
-  class Comment 
-      : public Column {
-  private:
-    static const int CommentColumn = 60;
-    std::string text;
-    const TargetAsmInfo &TAI;
-    
-  public:
-    Comment(const std::string &comment,
-            const TargetAsmInfo &tai) 
-        : Column(CommentColumn), text(comment), TAI(tai) {}
-
-    raw_asm_fd_ostream &operator()(raw_asm_fd_ostream &out) const {
-      Column::operator()(out);
-      out << TAI.getCommentString() << " " << text;
-      return(out);
-    }
-  };
-
-  inline raw_asm_fd_ostream &operator<<(raw_asm_fd_ostream &out,
-                                        const Comment &comment)
-  {
-    return(comment(out));
-  }
-
-  /// Asm stream equivalent for std streams
-  ///
-
-  /// WARNING: Do NOT use these streams in the constructors of global
-  /// objects.  There is no mechanism to ensure they are initialized
-  /// before other global objects.
-  ///
-  extern raw_asm_fd_ostream asmouts;
-  extern raw_asm_fd_ostream asmerrs;
-}
-
-#endif
diff --git a/include/llvm/Support/FormattedStream.h b/include/llvm/Support/FormattedStream.h
new file mode 100644 (file)
index 0000000..fb8aa35
--- /dev/null
@@ -0,0 +1,106 @@
+//===-- llvm/CodeGen/FormattedStream.h - Formatted streams ------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains raw_ostream implementations for streams to do
+// things like pretty-print comments.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_ASMSTREAM_H
+#define LLVM_CODEGEN_ASMSTREAM_H
+
+#include "llvm/Support/raw_ostream.h"
+
+namespace llvm 
+{
+  /// raw_asm_fd_ostream - Formatted raw_fd_ostream to handle
+  /// asm-specific constructs
+  ///
+  class formatted_raw_ostream : public raw_ostream {
+  private:
+    /// TheStream - The real stream we output to
+    ///
+    raw_ostream &TheStream;
+
+    /// Column - The current output column of the stream
+    ///
+    unsigned Column;
+
+    virtual void write_impl(const char *Ptr, unsigned Size) {
+      ComputeColumn(Ptr, Size);
+      TheStream.write(Ptr, Size);
+    }
+
+    /// current_pos - Return the current position within the stream,
+    /// not counting the bytes currently in the buffer.
+    virtual uint64_t current_pos() { 
+      // This has the same effect as calling TheStream.current_pos(),
+      // but that interface is private.
+      return TheStream.tell() - TheStream.GetNumBytesInBuffer();
+    }
+
+    /// ComputeColumn - Examine the current output and figure out
+    /// which column we end up in after output.
+    ///
+    void ComputeColumn(const char *Ptr, unsigned Size);
+
+  public:
+    /// formatted_raw_ostream - Open the specified file for
+    /// writing. If an error occurs, information about the error is
+    /// 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.
+    formatted_raw_ostream(raw_ostream &Stream) 
+        : raw_ostream(), TheStream(Stream), Column(0) {}
+
+    /// PadToColumn - Align the output to some column number
+    ///
+    /// \param NewCol - The column to move to
+    /// \param MinPad - The minimum space to give after the most
+    /// recent I/O, even if the current column + minpad > newcol
+    ///
+    void PadToColumn(unsigned NewCol, unsigned MinPad = 0);
+  };
+
+  /// Column - An I/O manipulator to advance the output to a certain column
+  ///
+  class Column {
+  private:
+    /// Col - The column to move to
+    ///
+    unsigned int Col;
+
+  public:
+    explicit Column(unsigned int c) 
+        : Col(c) {}
+
+    /// operator() - Make Column a functor invokable by a stream
+    /// output operator.
+    ///
+    formatted_raw_ostream &operator()(formatted_raw_ostream &Out) const {
+      // Make at least one space before the next output
+      Out.PadToColumn(Col, 1);
+      return(Out);
+    }
+  };
+
+  /// operator<< - Support coulmn-setting in formatted streams.
+  ///
+  inline formatted_raw_ostream &operator<<(formatted_raw_ostream &Out,
+                                           const Column &Func)
+  {
+    return(Func(Out));
+  }
+}
+
+#endif
index 22fb38485d7aa8de6a5da2f2d9249dbd049ab710..8242f04e23ce4670fa2e3d8005b3c8c284adeff5 100644 (file)
@@ -44,22 +44,6 @@ private:
   char *OutBufStart, *OutBufEnd, *OutBufCur;
   bool Unbuffered;
 
-protected:
-  /// CurBufPtr - Get a pointer to the current location in the buffer.
-  ///
-  char *CurBufPtr(void) { return OutBufCur; }
-  /// StartBufPtr - Get a pointer to the start of the buffer
-  ///
-  char *StartBufPtr(void) { return OutBufStart; }
-  /// EndBufPtr - Get a pointer to the end of the buffer
-  ///
-  char *EndBufPtr(void) { return OutBufEnd; }
-
-  /// AboutToFlush- Called when the buffer is about to be flushed,
-  /// allowing derived classes to take some action.
-  ///
-  virtual void AboutToFlush(void) {};
-
 public:
   // color order matches ANSI escape sequence, don't change
   enum Colors {
diff --git a/lib/CodeGen/AsmStream.cpp b/lib/CodeGen/AsmStream.cpp
deleted file mode 100644 (file)
index 363f9c1..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-//===-- llvm/CodeGen/AsmStream.cpp - AsmStream Framework --------*- C++ -*-===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file contains instantiations of "standard" AsmOStreams.
-//
-//===----------------------------------------------------------------------===//
-
-#include "llvm/CodeGen/AsmStream.h"
-
-namespace llvm {
-  raw_asm_fd_ostream asmouts(STDOUT_FILENO, false);
-  raw_asm_fd_ostream asmerrs(STDERR_FILENO, false);
-}
diff --git a/lib/Support/FormattedStream.cpp b/lib/Support/FormattedStream.cpp
new file mode 100644 (file)
index 0000000..d471d53
--- /dev/null
@@ -0,0 +1,74 @@
+//===-- llvm/CodeGen/AsmStream.cpp - AsmStream Framework --------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains instantiations of "standard" AsmOStreams.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/FormattedStream.h"
+
+namespace llvm {
+  /// ComputeColumn - Examine the current output and figure out which
+  /// column we end up in after output.
+  ///
+  void formatted_raw_ostream::ComputeColumn(const char *Ptr, unsigned Size)
+  {
+    // Keep track of the current column by scanning the string for
+    // special characters
+
+    // Find the last newline.  This is our column start.  If there
+    // is no newline, start with the current column.
+    const char *nlpos = NULL;
+    for (const char *pos = Ptr + Size, *epos = Ptr; pos > epos; --pos) {
+      if (*(pos-1) == '\n') {
+        nlpos = pos-1;
+        // The newline will be counted, setting this to zero.  We
+        // need to do it this way in case nlpos is Ptr.
+        Column = -1;
+        break;
+      }
+    }
+
+    if (nlpos == NULL) {
+      nlpos = Ptr;
+    }
+
+    // Walk through looking for tabs and advance column as appropriate
+    for (const char *pos = nlpos, *epos = Ptr + Size; pos != epos; ++pos) {
+      ++Column;
+      if (*pos == '\t') {
+        // Advance to next tab stop (every eight characters)
+        Column += ((8 - (Column & 0x7)) & 0x7);
+        assert(!(Column & 0x3) && "Column out of alignment");
+      }
+    }
+  }
+
+  /// PadToColumn - Align the output to some column number
+  ///
+  /// \param NewCol - The column to move to
+  /// \param MinPad - The minimum space to give after the most recent
+  /// I/O, even if the current column + minpad > newcol
+  ///
+  void formatted_raw_ostream::PadToColumn(unsigned NewCol, unsigned MinPad) 
+  {
+    flush();
+
+    // Output spaces until we reach the desired column
+    unsigned num = NewCol - Column;
+    if (NewCol < Column || num < MinPad) {
+      num = MinPad;
+    }
+
+    // TODO: Write a whole string at a time
+    while (num-- > 0) {
+      write(' ');
+    }
+  }
+}
index 403300f1bc46d88196a63aee6db9a2fe6b1e0501..42e6fda97baf03807413792a7a48077fe4213058 100644 (file)
@@ -120,7 +120,6 @@ raw_ostream &raw_ostream::operator<<(const void *P) {
 
 void raw_ostream::flush_nonempty() {
   assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
-  AboutToFlush();
   write_impl(OutBufStart, OutBufCur - OutBufStart);
   OutBufCur = OutBufStart;    
 }