[llvm] r234460 - Add classof implementations to the raw_ostream classes.

Chandler Carruth chandlerc at google.com
Wed Apr 8 20:06:29 PDT 2015


Whoa, wait, what?

Why should we have RTTI-style stuff for ostreams? This seems really messed
up to me and not at all obvious. I don't really want *all* ostreams to pay
this cost.

On Wed, Apr 8, 2015 at 7:17 PM Rafael Espindola <rafael.espindola at gmail.com>
wrote:

> Author: rafael
> Date: Wed Apr  8 21:10:28 2015
> New Revision: 234460
>
> URL: http://llvm.org/viewvc/llvm-project?rev=234460&view=rev
> Log:
> Add classof implementations to the raw_ostream classes.
>
> More uses to follow in a another patch.
>
> Modified:
>     llvm/trunk/include/llvm/MC/MCLinkerOptimizationHint.h
>     llvm/trunk/include/llvm/Support/FormattedStream.h
>     llvm/trunk/include/llvm/Support/circular_raw_ostream.h
>     llvm/trunk/include/llvm/Support/raw_os_ostream.h
>     llvm/trunk/include/llvm/Support/raw_ostream.h
>     llvm/trunk/lib/Support/Debug.cpp
>     llvm/trunk/lib/Support/raw_ostream.cpp
>
> Modified: llvm/trunk/include/llvm/MC/MCLinkerOptimizationHint.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/
> MCLinkerOptimizationHint.h?rev=234460&r1=234459&r2=234460&view=diff
> ============================================================
> ==================
> --- llvm/trunk/include/llvm/MC/MCLinkerOptimizationHint.h (original)
> +++ llvm/trunk/include/llvm/MC/MCLinkerOptimizationHint.h Wed Apr  8
> 21:10:28 2015
> @@ -140,8 +140,12 @@ public:
>        uint64_t current_pos() const override { return Count; }
>
>      public:
> -      raw_counting_ostream() : Count(0) {}
> +      raw_counting_ostream() : raw_ostream(SK_COUNTING), Count(0) {}
>        ~raw_counting_ostream() { flush(); }
> +
> +      static bool classof(const raw_ostream *OS) {
> +        return OS->getKind() == SK_COUNTING;
> +      }
>      };
>
>      raw_counting_ostream OutStream;
>
> Modified: llvm/trunk/include/llvm/Support/FormattedStream.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/
> llvm/Support/FormattedStream.h?rev=234460&r1=234459&r2=234460&view=diff
> ============================================================
> ==================
> --- llvm/trunk/include/llvm/Support/FormattedStream.h (original)
> +++ llvm/trunk/include/llvm/Support/FormattedStream.h Wed Apr  8 21:10:28
> 2015
> @@ -84,15 +84,21 @@ public:
>    /// 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(nullptr), DeleteStream(false), Position(0,
> 0) {
> +  formatted_raw_ostream(raw_ostream &Stream, bool Delete = false)
> +      : raw_ostream(SK_FORMATTED), TheStream(nullptr),
> DeleteStream(false),
> +        Position(0, 0) {
>      setStream(Stream, Delete);
>    }
>    explicit formatted_raw_ostream()
> -    : raw_ostream(), TheStream(nullptr), DeleteStream(false), Position(0,
> 0) {
> +      : raw_ostream(SK_FORMATTED), TheStream(nullptr),
> DeleteStream(false),
> +        Position(0, 0) {
>      Scanned = nullptr;
>    }
>
> +  static bool classof(const raw_ostream *OS) {
> +    return OS->getKind() == SK_FORMATTED;
> +  }
> +
>    ~formatted_raw_ostream() {
>      flush();
>      releaseStream();
>
> Modified: llvm/trunk/include/llvm/Support/circular_raw_ostream.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/
> llvm/Support/circular_raw_ostream.h?rev=234460&r1=
> 234459&r2=234460&view=diff
> ============================================================
> ==================
> --- llvm/trunk/include/llvm/Support/circular_raw_ostream.h (original)
> +++ llvm/trunk/include/llvm/Support/circular_raw_ostream.h Wed Apr  8
> 21:10:28 2015
> @@ -107,14 +107,10 @@ namespace llvm
>      /// management of it, etc.
>      ///
>      circular_raw_ostream(raw_ostream &Stream, const char *Header,
> -                         size_t BuffSize = 0, bool Owns = REFERENCE_ONLY)
> -        : raw_ostream(/*unbuffered*/true),
> -            TheStream(nullptr),
> -            OwnsStream(Owns),
> -            BufferSize(BuffSize),
> -            BufferArray(nullptr),
> -            Filled(false),
> -            Banner(Header) {
> +                         size_t BuffSize = 0, bool Owns = REFERENCE_ONLY)
> +        : raw_ostream(SK_CIRCULAR, /*unbuffered*/ true),
> TheStream(nullptr),
> +          OwnsStream(Owns), BufferSize(BuffSize), BufferArray(nullptr),
> +          Filled(false), Banner(Header) {
>        if (BufferSize != 0)
>          BufferArray = new char[BufferSize];
>        Cur = BufferArray;
> @@ -144,6 +140,10 @@ namespace llvm
>      ///
>      void flushBufferWithBanner();
>
> +    static bool classof(const raw_ostream *OS) {
> +      return OS->getKind() == SK_CIRCULAR;
> +    }
> +
>    private:
>      /// releaseStream - Delete the held stream if needed. Otherwise,
>      /// transfer the buffer settings from this circular_raw_ostream
>
> Modified: llvm/trunk/include/llvm/Support/raw_os_ostream.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/
> llvm/Support/raw_os_ostream.h?rev=234460&r1=234459&r2=234460&view=diff
> ============================================================
> ==================
> --- llvm/trunk/include/llvm/Support/raw_os_ostream.h (original)
> +++ llvm/trunk/include/llvm/Support/raw_os_ostream.h Wed Apr  8 21:10:28
> 2015
> @@ -33,8 +33,11 @@ class raw_os_ostream : public raw_ostrea
>    uint64_t current_pos() const override;
>
>  public:
> -  raw_os_ostream(std::ostream &O) : OS(O) {}
> +  raw_os_ostream(std::ostream &O) : raw_ostream(SK_STD_OS), OS(O) {}
>    ~raw_os_ostream();
> +  static bool classof(const raw_ostream *OS) {
> +    return OS->getKind() == SK_STD_OS;
> +  }
>  };
>
>  } // end llvm namespace
>
> 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=234460&r1=234459&r2=234460&view=diff
> ============================================================
> ==================
> --- llvm/trunk/include/llvm/Support/raw_ostream.h (original)
> +++ llvm/trunk/include/llvm/Support/raw_ostream.h Wed Apr  8 21:10:28 2015
> @@ -16,6 +16,7 @@
>
>  #include "llvm/ADT/SmallVector.h"
>  #include "llvm/ADT/StringRef.h"
> +#include "llvm/Support/Casting.h"
>  #include "llvm/Support/DataTypes.h"
>  #include <system_error>
>
> @@ -67,6 +68,17 @@ private:
>    } BufferMode;
>
>  public:
> +  enum StreamKind {
> +    SK_FD,
> +    SK_STRING,
> +    SK_SVECTOR,
> +    SK_NULL,
> +    SK_STD_OS,
> +    SK_CIRCULAR,
> +    SK_FORMATTED,
> +    SK_COUNTING
> +  };
> +
>    // color order matches ANSI escape sequence, don't change
>    enum Colors {
>      BLACK=0,
> @@ -80,8 +92,8 @@ public:
>      SAVEDCOLOR
>    };
>
> -  explicit raw_ostream(bool unbuffered=false)
> -    : BufferMode(unbuffered ? Unbuffered : InternalBuffer) {
> +  explicit raw_ostream(StreamKind Kind, bool unbuffered = false)
> +      : BufferMode(unbuffered ? Unbuffered : InternalBuffer), Kind(Kind) {
>      // Start out ready to flush.
>      OutBufStart = OutBufEnd = OutBufCur = nullptr;
>    }
> @@ -259,7 +271,10 @@ public:
>    // Subclass Interface
>    //===-------------------------------------------------------
> -------------===//
>
> +  StreamKind getKind() const { return Kind; }
> +
>  private:
> +  StreamKind Kind;
>    /// The is the piece of the class that is implemented by subclasses.
> This
>    /// writes the \p Size bytes starting at
>    /// \p Ptr to the underlying stream.
> @@ -364,6 +379,8 @@ public:
>    /// this closes the file when the stream is destroyed.
>    raw_fd_ostream(int fd, bool shouldClose, bool unbuffered=false);
>
> +  static bool classof(const raw_ostream *OS) { return OS->getKind() ==
> SK_FD; }
> +
>    ~raw_fd_ostream();
>
>    /// Manually flush the stream and close the file. Note that this does
> not call
> @@ -443,9 +460,13 @@ class raw_string_ostream : public raw_os
>    /// currently in the buffer.
>    uint64_t current_pos() const override { return OS.size(); }
>  public:
> -  explicit raw_string_ostream(std::string &O) : OS(O) {}
> +  explicit raw_string_ostream(std::string &O) : raw_ostream(SK_STRING),
> OS(O) {}
>    ~raw_string_ostream();
>
> +  static bool classof(const raw_ostream *OS) {
> +    return OS->getKind() == SK_STRING;
> +  }
> +
>    /// Flushes the stream contents to the target string and returns  the
> string's
>    /// reference.
>    std::string& str() {
> @@ -473,6 +494,10 @@ public:
>    explicit raw_svector_ostream(SmallVectorImpl<char> &O);
>    ~raw_svector_ostream();
>
> +  static bool classof(const raw_ostream *OS) {
> +    return OS->getKind() == SK_SVECTOR;
> +  }
> +
>    /// This is called when the SmallVector we're appending to is changed
> outside
>    /// of the raw_svector_ostream's control.  It is only safe to do this
> if the
>    /// raw_svector_ostream has previously been flushed.
> @@ -493,8 +518,11 @@ class raw_null_ostream : public raw_ostr
>    uint64_t current_pos() const override;
>
>  public:
> -  explicit raw_null_ostream() {}
> +  explicit raw_null_ostream() : raw_ostream(SK_NULL) {}
>    ~raw_null_ostream();
> +  static bool classof(const raw_ostream *OS) {
> +    return OS->getKind() == SK_NULL;
> +  }
>  };
>
>  } // end llvm namespace
>
> Modified: llvm/trunk/lib/Support/Debug.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/
> Support/Debug.cpp?rev=234460&r1=234459&r2=234460&view=diff
> ============================================================
> ==================
> --- llvm/trunk/lib/Support/Debug.cpp (original)
> +++ llvm/trunk/lib/Support/Debug.cpp Wed Apr  8 21:10:28 2015
> @@ -114,9 +114,8 @@ static void debug_user_sig_handler(void
>    // know that debug mode is enabled and dbgs() really is a
>    // circular_raw_ostream.  If NDEBUG is defined, then dbgs() ==
>    // errs() but this will never be invoked.
> -  llvm::circular_raw_ostream *dbgout =
> -    static_cast<llvm::circular_raw_ostream *>(&llvm::dbgs());
> -  dbgout->flushBufferWithBanner();
> +  llvm::circular_raw_ostream &dbgout = cast<circular_raw_ostream>(
> llvm::dbgs());
> +  dbgout.flushBufferWithBanner();
>  }
>
>  /// dbgs - Return a circular-buffered debug stream.
>
> Modified: llvm/trunk/lib/Support/raw_ostream.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/
> Support/raw_ostream.cpp?rev=234460&r1=234459&r2=234460&view=diff
> ============================================================
> ==================
> --- llvm/trunk/lib/Support/raw_ostream.cpp (original)
> +++ llvm/trunk/lib/Support/raw_ostream.cpp Wed Apr  8 21:10:28 2015
> @@ -489,7 +489,7 @@ void format_object_base::home() {
>
>  raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
>                                 sys::fs::OpenFlags Flags)
> -    : Error(false), UseAtomicWrites(false), pos(0) {
> +    : raw_ostream(SK_FD), Error(false), UseAtomicWrites(false), pos(0) {
>    EC = std::error_code();
>    // Handle "-" as stdout. Note that when we do this, we consider ourself
>    // the owner of stdout. This means that we can do things like close the
> @@ -519,8 +519,8 @@ raw_fd_ostream::raw_fd_ostream(StringRef
>  /// raw_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_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered)
> -  : raw_ostream(unbuffered), FD(fd),
> -    ShouldClose(shouldClose), Error(false), UseAtomicWrites(false) {
> +    : raw_ostream(SK_FD, unbuffered), FD(fd), ShouldClose(shouldClose),
> +      Error(false), UseAtomicWrites(false) {
>  #ifdef O_BINARY
>    // Setting STDOUT to binary mode is necessary in Win32
>    // to avoid undesirable linefeed conversion.
> @@ -749,7 +749,8 @@ void raw_string_ostream::write_impl(cons
>  // capacity. This allows raw_ostream to write directly into the correct
> place,
>  // and we only need to set the vector size when the data is flushed.
>
> -raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O) :
> OS(O) {
> +raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O)
> +    : raw_ostream(SK_SVECTOR), OS(O) {
>    // Set up the initial external buffer. We make sure that the buffer has
> at
>    // least 128 bytes free; raw_ostream itself only requires 64, but we
> want to
>    // make sure that we don't grow the buffer unnecessarily on destruction
> (when
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150409/0ccf782c/attachment.html>


More information about the llvm-commits mailing list