18 #include "llvm/Config/config.h" 34 #include <system_error> 37 #if defined(HAVE_FCNTL_H) 41 #if defined(HAVE_UNISTD_H) 45 #if defined(__CYGWIN__) 52 # define STDIN_FILENO 0 55 # define STDOUT_FILENO 1 58 # define STDERR_FILENO 2 71 assert(OutBufCur == OutBufStart &&
72 "raw_ostream destructor called with non-empty buffer!");
74 if (BufferMode == InternalBuffer)
75 delete [] OutBufStart;
92 void raw_ostream::SetBufferAndMode(
char *BufferStart,
size_t Size,
94 assert(((Mode == Unbuffered && !BufferStart &&
Size == 0) ||
95 (Mode != Unbuffered && BufferStart &&
Size != 0)) &&
96 "stream must be unbuffered or have at least one byte");
101 if (BufferMode == InternalBuffer)
102 delete [] OutBufStart;
103 OutBufStart = BufferStart;
104 OutBufEnd = OutBufStart+
Size;
105 OutBufCur = OutBufStart;
108 assert(OutBufStart <= OutBufEnd &&
"Invalid size!");
137 for (
int Idx = 0; Idx < 16; ++Idx) {
138 *
this <<
format(
"%02" PRIX32, UUID[Idx]);
139 if (Idx == 3 || Idx == 5 || Idx == 7 || Idx == 9)
147 bool UseHexEscapes) {
148 for (
unsigned char c : Str) {
151 *
this <<
'\\' <<
'\\';
154 *
this <<
'\\' <<
't';
157 *
this <<
'\\' <<
'n';
160 *
this <<
'\\' <<
'"';
170 *
this <<
'\\' <<
'x';
171 *this << hexdigit((c >> 4 & 0xF));
172 *this << hexdigit((c >> 0) & 0xF);
176 *this << char('0' + ((c >> 6) & 7));
177 *this << char('0' + ((c >> 3) & 7));
178 *this << char('0' + ((c >> 0) & 7));
196 void raw_ostream::flush_nonempty() {
197 assert(OutBufCur > OutBufStart &&
"Invalid call to flush_nonempty.");
198 size_t Length = OutBufCur - OutBufStart;
199 OutBufCur = OutBufStart;
200 write_impl(OutBufStart, Length);
207 if (BufferMode == Unbuffered) {
208 write_impl(reinterpret_cast<char*>(&C), 1);
227 if (BufferMode == Unbuffered) {
228 write_impl(Ptr, Size);
233 return write(Ptr, Size);
236 size_t NumBytes = OutBufEnd - OutBufCur;
242 assert(NumBytes != 0 &&
"undefined behavior");
243 size_t BytesToWrite = Size - (Size % NumBytes);
244 write_impl(Ptr, BytesToWrite);
245 size_t BytesRemaining = Size - BytesToWrite;
246 if (BytesRemaining >
size_t(OutBufEnd - OutBufCur)) {
248 return write(Ptr + BytesToWrite, BytesRemaining);
250 copy_to_buffer(Ptr + BytesToWrite, BytesRemaining);
256 copy_to_buffer(Ptr, NumBytes);
258 return write(Ptr + NumBytes, Size - NumBytes);
261 copy_to_buffer(Ptr, Size);
266 void raw_ostream::copy_to_buffer(
const char *
Ptr,
size_t Size) {
267 assert(Size <=
size_t(OutBufEnd - OutBufCur) &&
"Buffer overrun!");
278 memcpy(OutBufCur, Ptr, Size);
289 size_t NextBufferSize = 127;
290 size_t BufferBytesLeft = OutBufEnd - OutBufCur;
291 if (BufferBytesLeft > 3) {
292 size_t BytesUsed = Fmt.
print(OutBufCur, BufferBytesLeft);
295 if (BytesUsed <= BufferBytesLeft) {
296 OutBufCur += BytesUsed;
302 NextBufferSize = BytesUsed;
314 size_t BytesUsed = Fmt.
print(V.
data(), NextBufferSize);
317 if (BytesUsed <= NextBufferSize)
321 assert(BytesUsed > NextBufferSize &&
"Didn't grow buffer!?");
322 NextBufferSize = BytesUsed;
337 const size_t Difference = FS.Width - FS.Str.
size();
338 switch (FS.Justify) {
348 int PadAmount = Difference / 2;
351 this->
indent(Difference - PadAmount);
363 if (FN.Upper && FN.HexPrefix)
365 else if (FN.Upper && !FN.HexPrefix)
367 else if (!FN.Upper && FN.HexPrefix)
376 if (Buffer.
size() < FN.Width)
384 if (FB.Bytes.
empty())
387 size_t LineIndex = 0;
388 auto Bytes = FB.Bytes;
389 const size_t Size = Bytes.size();
391 uint64_t OffsetWidth = 0;
392 if (FB.FirstByteOffset.
hasValue()) {
396 size_t Lines = Size / FB.NumPerLine;
397 uint64_t MaxOffset = *FB.FirstByteOffset + Lines * FB.NumPerLine;
401 OffsetWidth = std::max<uint64_t>(4,
llvm::alignTo(Power, 4) / 4);
405 unsigned NumByteGroups =
406 alignTo(FB.NumPerLine, FB.ByteGroupSize) / FB.ByteGroupSize;
407 unsigned BlockCharWidth = FB.NumPerLine * 2 + NumByteGroups - 1;
409 while (!Bytes.empty()) {
412 if (FB.FirstByteOffset.
hasValue()) {
418 auto Line = Bytes.take_front(FB.NumPerLine);
420 size_t CharsPrinted = 0;
422 for (
size_t I = 0;
I < Line.size(); ++
I, CharsPrinted += 2) {
423 if (
I && (
I % FB.ByteGroupSize) == 0) {
433 assert(BlockCharWidth >= CharsPrinted);
434 indent(BlockCharWidth - CharsPrinted + 2);
438 for (uint8_t Byte : Line) {
440 *this << static_cast<char>(Byte);
447 Bytes = Bytes.drop_front(Line.size());
448 LineIndex += Line.size();
449 if (LineIndex < Size)
457 static const char Chars[] = {
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
458 C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
459 C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
460 C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
461 C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C,
C, C};
465 return OS.
write(Chars, NumChars);
468 unsigned NumToWrite = std::min(NumChars,
470 OS.
write(Chars, NumToWrite);
471 NumChars -= NumToWrite;
478 return write_padding<' '>(*
this, NumSpaces);
483 return write_padding<'\0'>(*
this, NumZeros);
486 void raw_ostream::anchor() {}
504 "Cannot make a raw_ostream from a read-only descriptor!");
508 if (Filename ==
"-") {
509 EC = std::error_code();
514 return STDOUT_FILENO;
567 if (FD <= STDERR_FILENO)
571 off_t loc = ::lseek(FD, 0, SEEK_CUR);
575 std::error_code EC =
status(FD, Status);
578 SupportsSeeking = loc != (off_t)-1;
580 if (!SupportsSeeking)
583 pos =
static_cast<uint64_t
>(loc);
612 void raw_fd_ostream::write_impl(
const char *Ptr,
size_t Size) {
613 assert(FD >= 0 &&
"File already closed.");
619 size_t MaxWriteSize = SIZE_MAX >> 1;
621 #if defined(__linux__) 624 MaxWriteSize = 1024 * 1024 * 1024;
625 #elif defined(_WIN32) 630 MaxWriteSize = 32767;
634 size_t ChunkSize = std::min(Size, MaxWriteSize);
635 ssize_t ret =
::write(FD, Ptr, ChunkSize);
646 if (errno == EINTR || errno == EAGAIN
648 || errno == EWOULDBLOCK
654 error_detected(std::error_code(errno, std::generic_category()));
676 assert(SupportsSeeking &&
"Stream does not support seeking!");
679 pos = ::_lseeki64(FD, off, SEEK_SET);
680 #elif defined(HAVE_LSEEK64) 681 pos = ::lseek64(FD, off, SEEK_SET);
683 pos = ::lseek(FD, off, SEEK_SET);
685 if (pos == (uint64_t)-1)
686 error_detected(std::error_code(errno, std::generic_category()));
690 void raw_fd_ostream::pwrite_impl(
const char *Ptr,
size_t Size,
692 uint64_t Pos =
tell();
698 size_t raw_fd_ostream::preferred_buffer_size()
const {
699 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__minix) 701 assert(FD >= 0 &&
"File not yet open!");
703 if (fstat(FD, &statbuf) != 0)
709 if (S_ISCHR(statbuf.st_mode) && isatty(FD))
712 return statbuf.st_blksize;
722 const char *colorcode =
726 size_t len = strlen(colorcode);
727 write(colorcode, len);
739 size_t len = strlen(colorcode);
740 write(colorcode, len);
752 size_t len = strlen(colorcode);
753 write(colorcode, len);
768 void raw_fd_ostream::anchor() {}
806 void raw_string_ostream::write_impl(
const char *Ptr,
size_t Size) {
807 OS.append(Ptr, Size);
814 uint64_t raw_svector_ostream::current_pos()
const {
return OS.size(); }
816 void raw_svector_ostream::write_impl(
const char *Ptr,
size_t Size) {
817 OS.append(Ptr, Ptr + Size);
820 void raw_svector_ostream::pwrite_impl(
const char *Ptr,
size_t Size,
838 void raw_null_ostream::write_impl(
const char *Ptr,
size_t Size) {
841 uint64_t raw_null_ostream::current_pos()
const {
845 void raw_null_ostream::pwrite_impl(
const char *Ptr,
size_t Size,
848 void raw_pwrite_stream::anchor() {}
bool has_colors() const override
This function determines if this stream is displayed and supports colors.
bool is_displayed() const override
This function determines if this stream is connected to a "tty" or "console" window.
std::error_code openFileForReadWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp, OpenFlags Flags, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
uint8_t[16] uuid_t
Output a formatted UUID with dash separators.
bool isPrint(char C)
Checks whether character C is printable.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Compute iterated dominance frontiers using a linear time algorithm.
static const char * ResetColor()
Resets the terminals colors, or returns an escape sequence to do so.
static const char * OutputColor(char c, bool bold, bool bg)
This function returns the colorcode escape sequences.
std::error_code ChangeStdoutToBinary()
A raw_ostream that discards all output.
uint64_t seek(uint64_t off)
Flushes the stream and repositions the underlying file descriptor position to the offset specified fr...
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
~raw_string_ostream() override
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
static bool FileDescriptorHasColors(int fd)
This function determines if the given file descriptor is displayd and supports colors.
A raw_ostream that writes to an SmallVector or SmallString.
raw_fd_ostream(StringRef Filename, std::error_code &EC)
Open the specified file for writing.
static bool FileDescriptorIsDisplayed(int fd)
This function determines if the given file descriptor is connected to a "tty" or "console" window...
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
block Block Frequency true
Represents the result of a call to sys::fs::status().
std::error_code error() const
raw_ostream & write_uuid(const uuid_t UUID)
void write_double(raw_ostream &S, double D, FloatStyle Style, Optional< size_t > Precision=None)
static bool ColorNeedsFlush()
Whether changing colors requires the output to be flushed.
static const char * OutputReverse()
This function returns the escape sequence to reverse forground and background colors.
const T & getValue() const LLVM_LVALUE_FUNCTION
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
~raw_fd_ostream() override
The file should be opened in text mode on platforms that make this distinction.
raw_ostream & operator<<(char C)
static raw_ostream & write_padding(raw_ostream &OS, unsigned NumChars)
raw_ostream & outs()
This returns a reference to a raw_ostream for standard output.
raw_ostream & resetColor() override
Resets the colors to terminal defaults.
virtual size_t preferred_buffer_size() const
Return an efficient buffer size for the underlying output mechanism.
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
void SetUnbuffered()
Set the stream to be unbuffered.
#define LLVM_UNLIKELY(EXPR)
std::error_code openFileForWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp=CD_CreateAlways, OpenFlags Flags=OF_None, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
~raw_null_ostream() override
bool has_error() const
Return the value of the flag in this raw_fd_ostream indicating whether an output error has been encou...
bool RunningWindows8OrGreater()
Determines if the program is running on Windows 8 or newer.
raw_ostream & write(unsigned char C)
static int getFD(StringRef Filename, std::error_code &EC, sys::fs::CreationDisposition Disp, sys::fs::FileAccess Access, sys::fs::OpenFlags Flags)
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an escape...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
unsigned Log2_64_Ceil(uint64_t Value)
Return the ceil log base 2 of the specified value, 64 if the value is zero.
size_t GetNumBytesInBuffer() const
void SetBuffered()
Set the stream to be buffered, with an automatically determined buffer size.
Provides a library for accessing information about this process and other processes on the operating ...
void write_hex(raw_ostream &S, uint64_t N, HexPrintStyle Style, Optional< size_t > Width=None)
A raw_ostream that writes to a file descriptor.
pointer data()
Return a pointer to the vector's buffer, even if empty().
void close()
Manually flush the stream and close the file.
An abstract base class for streams implementations that also support a pwrite operation.
static const char * OutputBold(bool bg)
Same as OutputColor, but only enables the bold attribute.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
CD_CreateAlways - When opening a file:
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
raw_ostream & changeColor(enum Colors colors, bool bold=false, bool bg=false) override
Changes the foreground color of text that will be output from this point forward. ...
static std::error_code SafelyCloseFileDescriptor(int FD)
uint64_t tell() const
tell - Return the current offset with the file.
This class implements an extremely fast bulk output stream that can only output to a stream...
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
StringRef - Represent a constant reference to a string, i.e.
void write_integer(raw_ostream &S, unsigned int N, size_t MinDigits, IntegerStyle Style)
raw_ostream & reverseColor() override
Reverses the foreground and background colors.
bool empty() const
empty - Check if the array is empty.