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