18 #include "llvm/Config/config.h"
29 #include <system_error>
32 #if defined(HAVE_FCNTL_H)
36 #if defined(HAVE_UNISTD_H)
39 #if defined(HAVE_SYS_UIO_H) && defined(HAVE_WRITEV)
43 #if defined(__CYGWIN__)
50 # define STDIN_FILENO 0
53 # define STDOUT_FILENO 1
56 # define STDERR_FILENO 2
65 assert(OutBufCur == OutBufStart &&
66 "raw_ostream destructor called with non-empty buffer!");
68 if (BufferMode == InternalBuffer)
69 delete [] OutBufStart;
73 void raw_ostream::handle() {}
89 void raw_ostream::SetBufferAndMode(
char *BufferStart,
size_t Size,
91 assert(((Mode == Unbuffered && !BufferStart && Size == 0) ||
92 (Mode != Unbuffered && BufferStart && Size != 0)) &&
93 "stream must be unbuffered or have at least one byte");
98 if (BufferMode == InternalBuffer)
99 delete [] OutBufStart;
100 OutBufStart = BufferStart;
101 OutBufEnd = OutBufStart+Size;
102 OutBufCur = OutBufStart;
105 assert(OutBufStart <= OutBufEnd &&
"Invalid size!");
113 char NumberBuffer[20];
114 char *EndPtr = NumberBuffer+
sizeof(NumberBuffer);
115 char *CurPtr = EndPtr;
118 *--CurPtr =
'0' + char(N % 10);
121 return write(CurPtr, EndPtr-CurPtr);
128 N = -(
unsigned long)N;
131 return this->operator<<(static_cast<unsigned long>(
N));
136 if (N == static_cast<unsigned long>(N))
137 return this->operator<<(static_cast<unsigned long>(
N));
139 char NumberBuffer[20];
140 char *EndPtr = NumberBuffer+
sizeof(NumberBuffer);
141 char *CurPtr = EndPtr;
144 *--CurPtr =
'0' + char(N % 10);
147 return write(CurPtr, EndPtr-CurPtr);
154 N = -(
unsigned long long)N;
157 return this->operator<<(static_cast<unsigned long long>(
N));
165 char NumberBuffer[20];
166 char *EndPtr = NumberBuffer+
sizeof(NumberBuffer);
167 char *CurPtr = EndPtr;
170 uintptr_t x = N % 16;
171 *--CurPtr = (x < 10 ?
'0' + x :
'a' + x - 10);
175 return write(CurPtr, EndPtr-CurPtr);
179 bool UseHexEscapes) {
180 for (
unsigned i = 0, e = Str.
size(); i != e; ++i) {
181 unsigned char c = Str[i];
185 *
this <<
'\\' <<
'\\';
188 *
this <<
'\\' <<
't';
191 *
this <<
'\\' <<
'n';
194 *
this <<
'\\' <<
'"';
197 if (std::isprint(c)) {
204 *
this <<
'\\' <<
'x';
205 *this << hexdigit((c >> 4 & 0xF));
206 *this << hexdigit((c >> 0) & 0xF);
210 *this << char('0' + ((c >> 6) & 7));
211 *this << char('0' + ((c >> 3) & 7));
212 *this << char('0' + ((c >> 0) & 7));
231 #if __cplusplus >= 201103L && defined(__MINGW32__)
233 if (N == 0.0 && std::signbit(N))
234 return *
this <<
"-0.000000e+00";
236 int fpcl = _fpclass(N);
239 if (fpcl == _FPCLASS_NZ)
240 return *
this <<
"-0.000000e+00";
245 len =
format(
"%e", N).snprint(buf,
sizeof(buf));
246 if (len <=
sizeof(buf) - 2) {
247 if (len >= 5 && buf[len - 5] ==
'e' && buf[len - 3] ==
'0') {
248 int cs = buf[len - 4];
249 if (cs ==
'+' || cs ==
'-') {
250 int c1 = buf[len - 2];
251 int c0 = buf[len - 1];
252 if (isdigit(static_cast<unsigned char>(c1)) &&
253 isdigit(static_cast<unsigned char>(c0))) {
269 void raw_ostream::flush_nonempty() {
270 assert(OutBufCur > OutBufStart &&
"Invalid call to flush_nonempty.");
271 size_t Length = OutBufCur - OutBufStart;
272 OutBufCur = OutBufStart;
273 write_impl(OutBufStart, Length);
280 if (BufferMode == Unbuffered) {
281 write_impl(reinterpret_cast<char*>(&C), 1);
300 if (BufferMode == Unbuffered) {
301 write_impl(Ptr, Size);
306 return write(Ptr, Size);
309 size_t NumBytes = OutBufEnd - OutBufCur;
315 assert(NumBytes != 0 &&
"undefined behavior");
316 size_t BytesToWrite = Size - (Size % NumBytes);
317 write_impl(Ptr, BytesToWrite);
318 size_t BytesRemaining = Size - BytesToWrite;
319 if (BytesRemaining >
size_t(OutBufEnd - OutBufCur)) {
321 return write(Ptr + BytesToWrite, BytesRemaining);
323 copy_to_buffer(Ptr + BytesToWrite, BytesRemaining);
329 copy_to_buffer(Ptr, NumBytes);
331 return write(Ptr + NumBytes, Size - NumBytes);
334 copy_to_buffer(Ptr, Size);
339 void raw_ostream::copy_to_buffer(
const char *Ptr,
size_t Size) {
340 assert(Size <=
size_t(OutBufEnd - OutBufCur) &&
"Buffer overrun!");
345 case 4: OutBufCur[3] = Ptr[3];
346 case 3: OutBufCur[2] = Ptr[2];
347 case 2: OutBufCur[1] = Ptr[1];
348 case 1: OutBufCur[0] = Ptr[0];
351 memcpy(OutBufCur, Ptr, Size);
362 size_t NextBufferSize = 127;
363 size_t BufferBytesLeft = OutBufEnd - OutBufCur;
364 if (BufferBytesLeft > 3) {
365 size_t BytesUsed = Fmt.
print(OutBufCur, BufferBytesLeft);
368 if (BytesUsed <= BufferBytesLeft) {
369 OutBufCur += BytesUsed;
375 NextBufferSize = BytesUsed;
387 size_t BytesUsed = Fmt.
print(V.
data(), NextBufferSize);
390 if (BytesUsed <= NextBufferSize)
394 assert(BytesUsed > NextBufferSize &&
"Didn't grow buffer!?");
395 NextBufferSize = BytesUsed;
400 unsigned Len = FS.Str.
size();
401 int PadAmount = FS.Width - Len;
402 if (FS.RightJustify && (PadAmount > 0))
405 if (!FS.RightJustify && (PadAmount > 0))
413 unsigned PrefixChars = FN.HexPrefix ? 2 : 0;
414 unsigned Width = std::max(FN.Width, Nibbles + PrefixChars);
416 char NumberBuffer[20] =
"0x0000000000000000";
418 NumberBuffer[1] =
'0';
419 char *EndPtr = NumberBuffer+Width;
420 char *CurPtr = EndPtr;
421 const char A = FN.Upper ?
'A' :
'a';
422 unsigned long long N = FN.HexValue;
424 uintptr_t x = N % 16;
425 *--CurPtr = (x < 10 ?
'0' + x : A + x - 10);
429 return write(NumberBuffer, Width);
432 if (FN.DecValue == 0) {
436 char NumberBuffer[32];
437 char *EndPtr = NumberBuffer+
sizeof(NumberBuffer);
438 char *CurPtr = EndPtr;
439 bool Neg = (FN.DecValue < 0);
440 uint64_t
N = Neg ? -
static_cast<uint64_t
>(FN.DecValue) : FN.DecValue;
442 *--CurPtr =
'0' + char(N % 10);
445 int Len = EndPtr - CurPtr;
446 int Pad = FN.Width - Len;
453 return write(CurPtr, Len);
460 static const char Spaces[] =
" "
466 return write(Spaces, NumSpaces);
469 unsigned NumToWrite =
std::min(NumSpaces,
471 write(Spaces, NumToWrite);
472 NumSpaces -= NumToWrite;
495 if (Filename ==
"-") {
496 EC = std::error_code();
501 return STDOUT_FILENO;
527 off_t loc = ::lseek(FD, 0, SEEK_CUR);
531 std::error_code EC =
status(FD, Status);
534 SupportsSeeking = loc != (off_t)-1;
536 if (!SupportsSeeking)
539 pos =
static_cast<uint64_t
>(loc);
566 void raw_fd_ostream::write_impl(
const char *Ptr,
size_t Size) {
567 assert(FD >= 0 &&
"File already closed.");
578 #if defined(HAVE_WRITEV)
579 const void *Addr =
static_cast<const void *
>(Ptr);
580 struct iovec IOV = {
const_cast<void *
>(Addr), Size };
581 ret = ::writev(FD, &IOV, 1);
596 if (errno == EINTR || errno == EAGAIN
598 || errno == EWOULDBLOCK
627 pos = ::lseek(FD, off, SEEK_SET);
628 if (pos == (uint64_t)-1)
633 void raw_fd_ostream::pwrite_impl(
const char *Ptr,
size_t Size,
635 uint64_t Pos =
tell();
641 size_t raw_fd_ostream::preferred_buffer_size()
const {
642 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__minix)
644 assert(FD >= 0 &&
"File not yet open!");
646 if (fstat(FD, &statbuf) != 0)
652 if (S_ISCHR(statbuf.st_mode) && isatty(FD))
655 return statbuf.st_blksize;
665 const char *colorcode =
669 size_t len = strlen(colorcode);
670 write(colorcode, len);
682 size_t len = strlen(colorcode);
683 write(colorcode, len);
695 size_t len = strlen(colorcode);
696 write(colorcode, len);
750 void raw_string_ostream::write_impl(
const char *Ptr,
size_t Size) {
751 OS.append(Ptr, Size);
785 void raw_svector_ostream::pwrite_impl(
const char *Ptr,
size_t Size,
788 memcpy(OS.
begin() + Offset, Ptr, Size);
802 void raw_svector_ostream::write_impl(
const char *Ptr,
size_t Size) {
803 if (Ptr == OS.
end()) {
805 size_t NewSize = OS.
size() + Size;
806 assert(NewSize <= OS.
capacity() &&
"Invalid write_impl() call!");
810 OS.
append(Ptr, Ptr + Size);
817 uint64_t raw_svector_ostream::current_pos()
const {
839 void raw_null_ostream::write_impl(
const char *Ptr,
size_t Size) {
842 uint64_t raw_null_ostream::current_pos()
const {
846 void raw_null_ostream::pwrite_impl(
const char *Ptr,
size_t Size,
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.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
size_t capacity() const
Return the total number of elements in the currently allocated buffer.
size_t size() const
size - Get the string size.
static const char * ResetColor()
Resets the terminals colors, or returns an escape sequence to do so.
#define LLVM_UNLIKELY(EXPR)
#define LLVM_LIKELY(EXPR)
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_string_ostream() override
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.
raw_svector_ostream(SmallVectorImpl< char > &O, unsigned)
static bool FileDescriptorIsDisplayed(int fd)
This function determines if the given file descriptor is connected to a "tty" or "console" window...
void reserve(size_type N)
file_status - Represents the result of a call to stat and friends.
std::size_t countLeadingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the most significant bit to the least stopping at the first 1...
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
static bool ColorNeedsFlush()
Whether changing colors requires the output to be flushed.
bool has_error() const
Return the value of the flag in this raw_fd_ostream indicating whether an output error has been encou...
static const char * OutputReverse()
This function returns the escape sequence to reverse forground and background colors.
Number of individual test Apply this number of consecutive mutations to each input exit after the first new interesting input is found the minimized corpus is saved into the first input directory Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
void SetBuffer(char *BufferStart, size_t Size)
Use the provided buffer as the raw_ostream buffer.
uint64_t tell() const
tell - Return the current offset with the file.
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
~raw_fd_ostream() override
raw_ostream & operator<<(char C)
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
LLVM_CONSTEXPR size_t array_lengthof(T(&)[N])
Find the length of an array.
raw_ostream & outs()
This returns a reference to a raw_ostream for standard output.
static int getFD(StringRef Filename, std::error_code &EC, sys::fs::OpenFlags Flags)
raw_ostream & resetColor() override
Resets the colors to terminal defaults.
void resync()
This is called when the SmallVector we're appending to is changed outside of the raw_svector_ostream'...
void SetUnbuffered()
Set the stream to be unbuffered.
~raw_svector_ostream() override
raw_fd_ostream(StringRef Filename, std::error_code &EC, sys::fs::OpenFlags Flags)
Open the specified file for writing.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
~raw_null_ostream() override
raw_ostream & write(unsigned char C)
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '', ' ', '"', and anything that doesn't satisfy std::isprint into an escape...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
virtual size_t preferred_buffer_size() const
Return an efficient buffer size for the underlying output mechanism.
The file should be opened in text mode on platforms that make this distinction.
void SetBuffered()
Set the stream to be buffered, with an automatically determined buffer size.
StringRef str()
Flushes the stream contents to the target vector and return a StringRef for the vector contents...
void set_size(size_type N)
Set the array size to N, which the current array must have enough capacity for.
Provides a library for accessing information about this process and other processes on the operating ...
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.
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)
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
std::error_code status(const Twine &path, file_status &result)
Get file status as if by POSIX stat().
raw_ostream & reverseColor() override
Reverses the forground and background colors.
size_t GetNumBytesInBuffer() const
std::error_code openFileForWrite(const Twine &Name, int &ResultFD, OpenFlags Flags, unsigned Mode=0666)