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

Chris Lattner sabre at nondot.org
Sat Aug 16 21:13:37 PDT 2008


Author: lattner
Date: Sat Aug 16 23:13:37 2008
New Revision: 54865

URL: http://llvm.org/viewvc/llvm-project?rev=54865&view=rev
Log:
add support for a cout/cerr analog (outs()/errs()) as well as
a simple adaptor class to give raw output capabilities to 
something that wants to write to an ostream.

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=54865&r1=54864&r2=54865&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Support/raw_ostream.h (original)
+++ llvm/trunk/include/llvm/Support/raw_ostream.h Sat Aug 16 23:13:37 2008
@@ -1,4 +1,4 @@
-//===--- raw_ostream.h - Raw output stream ---------------------------------===//
+//===--- raw_ostream.h - Raw output stream --------------------------------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -15,6 +15,7 @@
 #define LLVM_SUPPORT_RAW_OSTREAM_H
 
 #include <string>
+#include <iosfwd>
 
 namespace llvm {
 
@@ -151,6 +152,8 @@
   virtual void flush_impl();
 };
   
+/// raw_stdout_ostream - This is a stream that always prints to stdout.
+///
 class raw_stdout_ostream : public raw_fd_ostream {
   // An out of line virtual method to provide a home for the class vtable.
   virtual void handle();
@@ -158,6 +161,8 @@
   raw_stdout_ostream();
 };
 
+/// raw_stderr_ostream - This is a stream that always prints to stderr.
+///
 class raw_stderr_ostream : public raw_fd_ostream {
   // An out of line virtual method to provide a home for the class vtable.
   virtual void handle();
@@ -165,6 +170,28 @@
   raw_stderr_ostream();
 };
   
+/// outs() - This returns a reference to a raw_ostream for standard output.
+/// Use it like: outs() << "foo" << "bar";
+raw_ostream &outs();
+
+/// errs() - This returns a reference to a raw_ostream for standard error.
+/// Use it like: errs() << "foo" << "bar";
+raw_ostream &errs();
+  
+  
+/// raw_os_ostream - A raw_ostream that writes to an std::ostream.  This is a
+/// simple adaptor class.
+class raw_os_ostream : public raw_ostream {
+  std::ostream &OS;
+public:
+  raw_os_ostream(std::ostream &O) : OS(O) {}
+  
+  /// flush_impl - The is the piece of the class that is implemented by
+  /// subclasses.  This outputs the currently buffered data and resets the
+  /// buffer to empty.
+  virtual void flush_impl();
+};
+  
 } // end llvm namespace
 
 #endif

Modified: llvm/trunk/lib/Support/raw_ostream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/raw_ostream.cpp?rev=54865&r1=54864&r2=54865&view=diff

==============================================================================
--- llvm/trunk/lib/Support/raw_ostream.cpp (original)
+++ llvm/trunk/lib/Support/raw_ostream.cpp Sat Aug 16 23:13:37 2008
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/raw_ostream.h"
+#include <ostream>
 using namespace llvm;
 
 #if !defined(_MSC_VER)
@@ -62,6 +63,9 @@
   HandleFlush();
 }
 
+//===----------------------------------------------------------------------===//
+//  raw_stdout/err_ostream
+//===----------------------------------------------------------------------===//
 
 raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
 raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false) {}
@@ -69,3 +73,30 @@
 // An out of line virtual method to provide a home for the class vtable.
 void raw_stdout_ostream::handle() {}
 void raw_stderr_ostream::handle() {}
+
+/// outs() - This returns a reference to a raw_ostream for standard output.
+/// Use it like: outs() << "foo" << "bar";
+raw_ostream &outs() {
+  static raw_stdout_ostream S;
+  return S;
+}
+
+/// errs() - This returns a reference to a raw_ostream for standard error.
+/// Use it like: errs() << "foo" << "bar";
+raw_ostream &errs() {
+  static raw_stderr_ostream S;
+  return S;
+}
+
+//===----------------------------------------------------------------------===//
+//  raw_os_ostream
+//===----------------------------------------------------------------------===//
+
+/// flush_impl - The is the piece of the class that is implemented by
+/// subclasses.  This outputs the currently buffered data and resets the
+/// buffer to empty.
+void raw_os_ostream::flush_impl() {
+  if (OutBufCur-OutBufStart)
+    OS.write(OutBufStart, OutBufCur-OutBufStart);
+  HandleFlush();
+}





More information about the llvm-commits mailing list