13#ifndef LLVM_SUPPORT_RAW_OSTREAM_H
14#define LLVM_SUPPORT_RAW_OSTREAM_H
26#include <system_error>
37template <
class T>
class [[nodiscard]]
Expected;
82 char *OutBufStart, *OutBufEnd, *OutBufCur;
83 bool ColorEnabled =
false;
85 enum class BufferKind {
135 : Kind(K), BufferMode(unbuffered ? BufferKind::Unbuffered
136 : BufferKind::InternalBuffer) {
138 OutBufStart = OutBufEnd = OutBufCur =
nullptr;
169 SetBufferAndMode(
new char[
Size],
Size, BufferKind::InternalBuffer);
175 if (BufferMode != BufferKind::Unbuffered && OutBufStart ==
nullptr)
179 return OutBufEnd - OutBufStart;
187 SetBufferAndMode(
nullptr, 0, BufferKind::Unbuffered);
191 return OutBufCur - OutBufStart;
199 if (OutBufCur != OutBufStart)
204 if (OutBufCur >= OutBufEnd)
211 if (OutBufCur >= OutBufEnd)
218 if (OutBufCur >= OutBufEnd)
226 size_t Size = Str.size();
229 if (
Size > (
size_t)(OutBufEnd - OutBufCur))
233 memcpy(OutBufCur, Str.data(),
Size);
239#if defined(__cpp_char8_t)
262 return write(Str.data(), Str.length());
266 return write(Str.data(), Str.length());
270 return write(Str.data(), Str.size());
280 return this->operator<<(static_cast<unsigned long>(
N));
284 return this->operator<<(static_cast<long>(
N));
377 virtual void write_impl(
const char *
Ptr,
size_t Size) = 0;
381 virtual uint64_t current_pos()
const = 0;
388 SetBufferAndMode(BufferStart,
Size, BufferKind::ExternalBuffer);
403 void SetBufferAndMode(
char *BufferStart,
size_t Size, BufferKind
Mode);
407 void flush_nonempty();
411 void copy_to_buffer(
const char *
Ptr,
size_t Size);
415 bool prepare_colors();
417 virtual void anchor();
422template <
typename OStream,
typename T>
423std::enable_if_t<!std::is_reference_v<OStream> &&
424 std::is_base_of_v<raw_ostream, OStream>,
428 return std::move(
OS);
436 void anchor()
override;
463 bool SupportsSeeking =
false;
464 bool IsRegularFile =
false;
465 mutable std::optional<bool> HasColors;
474 bool IsWindowsConsole =
false;
482 void write_impl(
const char *
Ptr,
size_t Size)
override;
488 uint64_t current_pos()
const override {
return pos; }
491 size_t preferred_buffer_size()
const override;
493 void anchor()
override;
556 std::error_code
error()
const {
return EC; }
610raw_fd_ostream &
outs();
617raw_fd_ostream &
errs();
665 void write_impl(
const char *
Ptr,
size_t Size)
override;
669 uint64_t current_pos()
const override {
return OS.size(); }
682 OS.reserve(
tell() + ExtraSize);
695 void write_impl(
const char *
Ptr,
size_t Size)
override;
700 uint64_t current_pos()
const override;
724 OS.reserve(
tell() + ExtraSize);
733 void write_impl(
const char *
Ptr,
size_t size)
override;
738 uint64_t current_pos()
const override;
749 void anchor()
override;
757 std::unique_ptr<raw_ostream> OS;
760 void anchor()
override;
767 this->OS->SetUnbuffered();
781 std::function<Error(raw_ostream &)>
Write);
783raw_ostream &
operator<<(raw_ostream &
OS, std::nullopt_t);
785template <typename T, typename = decltype(std::declval<raw_ostream &>()
786 << std::declval<const T &>())>
static cl::opt< RegAllocEvictionAdvisorAnalysis::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Development, "development", "for training")))
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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 is_displayed() const override
This function determines if this stream is connected to a "tty" or "console" window.
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 close()
Manually flush the stream and close the file.
void inc_pos(uint64_t Delta)
Expected< sys::fs::FileLocker > lock()
Locks the underlying file.
void tie(raw_ostream *TieTo)
Tie this stream to the specified stream.
bool supportsSeeking() const
~raw_fd_ostream() override
void clear_error()
Set the flag read by has_error() to false.
bool has_colors() const override
This function determines if this stream is displayed and supports colors.
bool isRegularFile() const
uint64_t seek(uint64_t off)
Flushes the stream and repositions the underlying file descriptor position to the offset specified fr...
int get_fd() const
Return the file descriptor.
Expected< sys::fs::FileLocker > tryLockFor(Duration const &Timeout)
Tries to lock the underlying file within the specified period.
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 bool classof(const raw_ostream *OS)
Check if OS is a pointer of type raw_fd_stream*.
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() override
raw_null_ostream()=default
This class implements an extremely fast bulk output stream that can only output to a stream.
static constexpr Colors YELLOW
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
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)
static constexpr Colors BRIGHT_RED
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
static constexpr Colors CYAN
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.
size_t GetBufferSize() const
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
raw_ostream & write_uuid(const uuid_t UUID)
raw_ostream & operator<<(char C)
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
virtual raw_ostream & reverseColor()
Reverses the foreground and background colors.
void SetBuffer(char *BufferStart, size_t Size)
Use the provided buffer as the raw_ostream buffer.
static constexpr Colors BLUE
virtual size_t preferred_buffer_size() const
Return an efficient buffer size for the underlying output mechanism.
raw_ostream & write(unsigned char C)
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.
static constexpr Colors RESET
raw_ostream & operator<<(signed char C)
static constexpr Colors BRIGHT_MAGENTA
uint8_t[16] uuid_t
Output a formatted UUID with dash separators.
static constexpr Colors MAGENTA
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
static constexpr Colors BRIGHT_YELLOW
raw_ostream & operator<<(const SmallVectorImpl< char > &Str)
raw_ostream & operator<<(StringRef Str)
static constexpr Colors BRIGHT_CYAN
static constexpr Colors SAVEDCOLOR
virtual void reserveExtraSpace(uint64_t ExtraSize)
If possible, pre-allocate ExtraSize bytes for stream data.
size_t GetNumBytesInBuffer() const
static constexpr Colors BLACK
raw_ostream & operator<<(const std::string &Str)
raw_ostream & operator<<(unsigned int N)
static constexpr Colors BRIGHT_BLACK
static constexpr Colors BRIGHT_WHITE
static constexpr Colors GREEN
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
static constexpr Colors RED
static constexpr Colors BRIGHT_GREEN
static constexpr Colors BRIGHT_BLUE
OStreamKind get_kind() const
void SetBuffered()
Set the stream to be buffered, with an automatically determined buffer size.
static constexpr Colors WHITE
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.
static bool classof(const raw_ostream *OS)
~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.
@ C
The default llvm calling convention, compatible with C.
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.
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
Error writeToOutput(StringRef OutputFileName, std::function< Error(raw_ostream &)> Write)
This helper creates an output stream and then passes it to Write.
raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
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.