14 #ifndef LLVM_SUPPORT_RAW_OSTREAM_H 15 #define LLVM_SUPPORT_RAW_OSTREAM_H 17 #include "llvm/FileSystem.h" 18 #include "llvm/SmallVector.h" 19 #include "llvm/StringRef.h" 21 #include <system_error> 24 class format_object_base;
25 class FormattedString;
26 class FormattedNumber;
27 template <
typename T>
class SmallVectorImpl;
56 char *OutBufStart, *OutBufEnd, *OutBufCur;
79 : BufferMode(unbuffered ? Unbuffered : InternalBuffer) {
81 OutBufStart = OutBufEnd = OutBufCur =
nullptr;
87 uint64_t
tell()
const {
return current_pos() + GetNumBytesInBuffer(); }
100 SetBufferAndMode(
new char[Size], Size, InternalBuffer);
103 size_t GetBufferSize()
const {
106 if (BufferMode != Unbuffered && OutBufStart ==
nullptr)
110 return OutBufEnd - OutBufStart;
118 SetBufferAndMode(
nullptr, 0, Unbuffered);
121 size_t GetNumBytesInBuffer()
const {
122 return OutBufCur - OutBufStart;
130 if (OutBufCur != OutBufStart)
135 if (OutBufCur >= OutBufEnd)
142 if (OutBufCur >= OutBufEnd)
149 if (OutBufCur >= OutBufEnd)
157 size_t Size = Str.
size();
160 if (Size > (
size_t)(OutBufEnd - OutBufCur))
161 return write(Str.
data(), Size);
164 memcpy(OutBufCur, Str.
data(), Size);
179 return write(Str.data(), Str.length());
182 raw_ostream &operator<<(const llvm::SmallVectorImpl<char> &Str) {
183 return write(Str.data(), Str.size());
192 return this->operator<<(static_cast<unsigned long>(N));
196 return this->operator<<(static_cast<long>(N));
272 virtual void write_impl(
const char *Ptr,
size_t Size) = 0;
275 virtual void handle();
279 virtual uint64_t current_pos()
const = 0;
286 SetBufferAndMode(BufferStart, Size, ExternalBuffer);
301 void SetBufferAndMode(
char *BufferStart,
size_t Size, BufferKind Mode);
305 void flush_nonempty();
309 void copy_to_buffer(
const char *Ptr,
size_t Size);
316 virtual void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset) = 0;
321 void pwrite(
const char *Ptr,
size_t Size, uint64_t Offset) {
323 uint64_t Pos =
tell();
327 assert(Size + Offset <= Pos &&
"We don't support extending the stream");
329 pwrite_impl(Ptr, Size, Offset);
349 bool SupportsSeeking;
352 void write_impl(
const char *Ptr,
size_t Size)
override;
354 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
358 uint64_t current_pos()
const override {
return pos; }
364 void error_detected() { Error =
true; }
378 sys::fs::OpenFlags Flags);
390 bool supportsSeeking() {
return SupportsSeeking; }
394 uint64_t seek(uint64_t off);
439 void write_impl(
const char *Ptr,
size_t Size)
override;
443 uint64_t current_pos()
const override {
return OS.size(); }
466 void write_impl(
const char *Ptr,
size_t Size)
override;
468 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
471 uint64_t current_pos()
const override;
483 void flush() =
delete;
492 void write_impl(
const char *Ptr,
size_t size)
override;
493 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
497 uint64_t current_pos()
const override;
515 #endif // LLVM_SUPPORT_RAW_OSTREAM_H const char * getBufferStart() const
Return the beginning of the current stream buffer, or 0 if the stream is unbuffered.
Definition: raw_ostream.h:294
size_t size() const
size - Get the string size.
Definition: StringRef.h:149
A raw_ostream that discards all output.
Definition: raw_ostream.h:490
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
Definition: raw_ostream.h:252
virtual raw_ostream & changeColor(enum Colors Color, bool Bold=false, bool BG=false)
Changes the foreground color of text that will be output from this point forward. ...
Definition: raw_ostream.h:230
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
Definition: raw_ostream.cpp:475
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:462
bool has_error() const
Return the value of the flag in this raw_fd_ostream indicating whether an output error has been encou...
Definition: raw_ostream.h:400
void SetBuffer(char *BufferStart, size_t Size)
Use the provided buffer as the raw_ostream buffer.
Definition: raw_ostream.h:285
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:87
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:139
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
Definition: raw_ostream.cpp:179
virtual bool is_displayed() const
This function determines if this stream is connected to a "tty" or "console" window.
Definition: raw_ostream.h:249
void SetUnbuffered()
Set the stream to be unbuffered.
Definition: raw_ostream.h:116
virtual raw_ostream & reverseColor()
Reverses the foreground and background colors.
Definition: raw_ostream.h:244
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
Definition: raw_ostream.h:98
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
Definition: raw_ostream.h:451
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '', ' ', '"', and anything that doesn't satisfy std::isprint into an escape ...
Definition: raw_ostream.cpp:197
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:486
virtual size_t preferred_buffer_size() const
Return an efficient buffer size for the underlying output mechanism.
Definition: raw_ostream.cpp:94
void SetBuffered()
Set the stream to be buffered, with an automatically determined buffer size.
Definition: raw_ostream.cpp:99
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:339
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:134
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:315
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:435
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
Definition: raw_ostream.h:241
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:33
raw_svector_ostream(SmallVectorImpl< char > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:478
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42
Definition: raw_ostream.h:504
void clear_error()
Set the flag read by has_error() to false.
Definition: raw_ostream.h:413