13#ifndef LLVM_SUPPORT_RAW_OSTREAM_H
14#define LLVM_SUPPORT_RAW_OSTREAM_H
27#include <system_error>
33class formatv_object_base;
34class format_object_base;
38template <
class T>
class [[nodiscard]] Expected;
83 char *OutBufStart, *OutBufEnd, *OutBufCur;
84 bool ColorEnabled =
false;
86 enum class BufferKind {
115 static constexpr Colors BLACK = Colors::BLACK;
116 static constexpr Colors RED = Colors::RED;
117 static constexpr Colors GREEN = Colors::GREEN;
118 static constexpr Colors YELLOW = Colors::YELLOW;
119 static constexpr Colors BLUE = Colors::BLUE;
120 static constexpr Colors MAGENTA = Colors::MAGENTA;
121 static constexpr Colors CYAN = Colors::CYAN;
122 static constexpr Colors WHITE = Colors::WHITE;
123 static constexpr Colors BRIGHT_BLACK = Colors::BRIGHT_BLACK;
124 static constexpr Colors BRIGHT_RED = Colors::BRIGHT_RED;
125 static constexpr Colors BRIGHT_GREEN = Colors::BRIGHT_GREEN;
126 static constexpr Colors BRIGHT_YELLOW = Colors::BRIGHT_YELLOW;
127 static constexpr Colors BRIGHT_BLUE = Colors::BRIGHT_BLUE;
128 static constexpr Colors BRIGHT_MAGENTA = Colors::BRIGHT_MAGENTA;
129 static constexpr Colors BRIGHT_CYAN = Colors::BRIGHT_CYAN;
130 static constexpr Colors BRIGHT_WHITE = Colors::BRIGHT_WHITE;
131 static constexpr Colors SAVEDCOLOR = Colors::SAVEDCOLOR;
132 static constexpr Colors RESET = Colors::RESET;
136 : Kind(K), BufferMode(unbuffered ? BufferKind::Unbuffered
137 : BufferKind::InternalBuffer) {
139 OutBufStart = OutBufEnd = OutBufCur =
nullptr;
148 uint64_t tell()
const {
return current_pos() + GetNumBytesInBuffer(); }
170 SetBufferAndMode(
new char[
Size],
Size, BufferKind::InternalBuffer);
176 if (BufferMode != BufferKind::Unbuffered && OutBufStart ==
nullptr)
177 return preferred_buffer_size();
180 return OutBufEnd - OutBufStart;
188 SetBufferAndMode(
nullptr, 0, BufferKind::Unbuffered);
192 return OutBufCur - OutBufStart;
200 if (OutBufCur != OutBufStart)
205 if (OutBufCur >= OutBufEnd)
212 if (OutBufCur >= OutBufEnd)
219 if (OutBufCur >= OutBufEnd)
227 size_t Size = Str.size();
230 if (
Size > (
size_t)(OutBufEnd - OutBufCur))
234 memcpy(OutBufCur, Str.data(),
Size);
240#if defined(__cpp_char8_t)
263 return write(Str.data(), Str.length());
267 return write(Str.data(), Str.length());
271 return write(Str.data(), Str.size());
281 return this->operator<<(static_cast<unsigned long>(
N));
285 return this->operator<<(static_cast<long>(
N));
378 virtual void write_impl(
const char *
Ptr,
size_t Size) = 0;
382 virtual uint64_t current_pos()
const = 0;
389 SetBufferAndMode(BufferStart,
Size, BufferKind::ExternalBuffer);
393 virtual size_t preferred_buffer_size()
const;
404 void SetBufferAndMode(
char *BufferStart,
size_t Size, BufferKind
Mode);
408 void flush_nonempty();
412 void copy_to_buffer(
const char *
Ptr,
size_t Size);
416 bool prepare_colors();
418 virtual void anchor();
423template <
typename OStream,
typename T>
424std::enable_if_t<!std::is_reference_v<OStream> &&
425 std::is_base_of_v<raw_ostream, OStream>,
429 return std::move(
OS);
437 void anchor()
override;
464 bool SupportsSeeking =
false;
465 bool IsRegularFile =
false;
466 mutable std::optional<bool> HasColors;
475 bool IsWindowsConsole =
false;
483 void write_impl(
const char *
Ptr,
size_t Size)
override;
489 uint64_t current_pos()
const override {
return pos; }
492 size_t preferred_buffer_size()
const override;
494 void anchor()
override;
530 OStreamKind K = OStreamKind::OK_OStream);
546 bool is_displayed()
const override;
548 bool has_colors()
const override;
557 std::error_code
error()
const {
return EC; }
606 tryLockFor(
Duration const &Timeout);
666 void write_impl(
const char *
Ptr,
size_t Size)
override;
670 uint64_t current_pos()
const override {
return OS.size(); }
683 OS.reserve(tell() + ExtraSize);
696 void write_impl(
const char *
Ptr,
size_t Size)
override;
701 uint64_t current_pos()
const override;
725 OS.reserve(tell() + ExtraSize);
734 void write_impl(
const char *
Ptr,
size_t size)
override;
739 uint64_t current_pos()
const override;
750 void anchor()
override;
758 std::unique_ptr<raw_ostream>
OS;
761 void anchor()
override;
768 this->OS->SetUnbuffered();
840 std::function<Error(raw_ostream &)>
Write);
844template <typename T, typename = decltype(std::declval<raw_ostream &>()
845 << std::declval<const T &>())>
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
raw_ostream & operator<<(raw_ostream &OS, const binary_le_impl< value_type > &BLE)
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
static void write(bool isBE, void *P, T V)
This file defines the SmallVector class.
std::pair< llvm::MachO::Target, std::string > UUID
Tagged union holding either a T or a Error.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
buffer_ostream(raw_ostream &OS)
~buffer_ostream() override
buffer_unique_ostream(std::unique_ptr< raw_ostream > OS)
~buffer_unique_ostream() override
A raw_ostream that writes to a file descriptor.
bool has_error() const
Return the value of the flag in this raw_fd_ostream indicating whether an output error has been encou...
std::error_code error() const
void inc_pos(uint64_t Delta)
void tie(raw_ostream *TieTo)
Tie this stream to the specified stream.
bool supportsSeeking() const
void clear_error()
Set the flag read by has_error() to false.
bool isRegularFile() const
int get_fd() const
Return the file descriptor.
void error_detected(std::error_code EC)
Set the flag indicating that an output error has been encountered.
A raw_ostream of a file for reading/writing/seeking.
static LLVM_ABI bool classof(const raw_ostream *OS)
Check if OS is a pointer of type raw_fd_stream*.
LLVM_ABI ssize_t read(char *Ptr, size_t Size)
This reads the Size bytes into a buffer pointed by Ptr.
A raw_ostream that discards all output.
raw_null_ostream()=default
This class implements an extremely fast bulk output stream that can only output to a stream.
raw_ostream & operator<<(unsigned char C)
raw_ostream(bool unbuffered=false, OStreamKind K=OStreamKind::OK_OStream)
uint64_t tell() const
tell - Return the current offset with the file.
raw_ostream & operator<<(const std::string_view &Str)
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
size_t GetBufferSize() const
raw_ostream & operator<<(char C)
void SetBuffer(char *BufferStart, size_t Size)
Use the provided buffer as the raw_ostream buffer.
void SetUnbuffered()
Set the stream to be unbuffered.
virtual bool is_displayed() const
This function determines if this stream is connected to a "tty" or "console" window.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
const char * getBufferStart() const
Return the beginning of the current stream buffer, or 0 if the stream is unbuffered.
raw_ostream & operator<<(signed char C)
uint8_t[16] uuid_t
Output a formatted UUID with dash separators.
virtual void enable_colors(bool enable)
raw_ostream & operator<<(int N)
raw_ostream & operator<<(const char *Str)
raw_ostream(const raw_ostream &)=delete
void operator=(const raw_ostream &)=delete
raw_ostream & operator<<(const SmallVectorImpl< char > &Str)
raw_ostream & operator<<(StringRef Str)
virtual void reserveExtraSpace(uint64_t ExtraSize)
If possible, pre-allocate ExtraSize bytes for stream data.
size_t GetNumBytesInBuffer() const
raw_ostream & operator<<(const std::string &Str)
raw_ostream & operator<<(unsigned int N)
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
OStreamKind get_kind() const
bool colors_enabled() const
An abstract base class for streams implementations that also support a pwrite operation.
raw_pwrite_stream(bool Unbuffered=false, OStreamKind K=OStreamKind::OK_OStream)
void pwrite(const char *Ptr, size_t Size, uint64_t Offset)
A raw_ostream that writes to an std::string.
std::string & str()
Returns the string's reference.
void reserveExtraSpace(uint64_t ExtraSize) override
If possible, pre-allocate ExtraSize bytes for stream data.
raw_string_ostream(std::string &O)
A raw_ostream that writes to an SmallVector or SmallString.
~raw_svector_ostream() override=default
SmallVectorImpl< char > & buffer()
void reserveExtraSpace(uint64_t ExtraSize) override
If possible, pre-allocate ExtraSize bytes for stream data.
StringRef str() const
Return a StringRef for the vector contents.
raw_svector_ostream(SmallVectorImpl< char > &O)
Construct a new raw_svector_ostream.
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
LLVM_ABI Error writeToOutput(StringRef OutputFileName, std::function< Error(raw_ostream &)> Write)
This helper creates an output stream and then passes it to Write.
LLVM_ABI raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI void write_hex(raw_ostream &S, uint64_t N, HexPrintStyle Style, std::optional< size_t > Width=std::nullopt)
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Implement std::hash so that hash_code can be used in STL containers.
void operator+=(unsigned N)
void operator-=(unsigned N)
indent(unsigned NumIndents, unsigned Scale=1)
indent operator+(unsigned N) const
indent operator-(unsigned N) const
indent & operator=(unsigned N)