18 #include "llvm/Config/config.h"
31 #if defined(HAVE_FCNTL_H)
35 #if defined(HAVE_UNISTD_H)
38 #if defined(HAVE_SYS_UIO_H) && defined(HAVE_WRITEV)
42 #if defined(__CYGWIN__)
49 # define STDIN_FILENO 0
52 # define STDOUT_FILENO 1
55 # define STDERR_FILENO 2
64 assert(OutBufCur == OutBufStart &&
65 "raw_ostream destructor called with non-empty buffer!");
67 if (BufferMode == InternalBuffer)
68 delete [] OutBufStart;
72 void raw_ostream::handle() {}
88 void raw_ostream::SetBufferAndMode(
char *BufferStart,
size_t Size,
90 assert(((Mode == Unbuffered && BufferStart == 0 && Size == 0) ||
91 (Mode != Unbuffered && BufferStart && Size)) &&
92 "stream must be unbuffered or have at least one byte");
97 if (BufferMode == InternalBuffer)
98 delete [] OutBufStart;
99 OutBufStart = BufferStart;
100 OutBufEnd = OutBufStart+Size;
101 OutBufCur = OutBufStart;
104 assert(OutBufStart <= OutBufEnd &&
"Invalid size!");
112 char NumberBuffer[20];
113 char *EndPtr = NumberBuffer+
sizeof(NumberBuffer);
114 char *CurPtr = EndPtr;
117 *--CurPtr =
'0' + char(N % 10);
120 return write(CurPtr, EndPtr-CurPtr);
127 N = -(
unsigned long)N;
130 return this->operator<<(static_cast<unsigned long>(
N));
135 if (N == static_cast<unsigned long>(N))
136 return this->operator<<(static_cast<unsigned long>(
N));
138 char NumberBuffer[20];
139 char *EndPtr = NumberBuffer+
sizeof(NumberBuffer);
140 char *CurPtr = EndPtr;
143 *--CurPtr =
'0' + char(N % 10);
146 return write(CurPtr, EndPtr-CurPtr);
153 N = -(
unsigned long long)N;
156 return this->operator<<(static_cast<unsigned long long>(
N));
164 char NumberBuffer[20];
165 char *EndPtr = NumberBuffer+
sizeof(NumberBuffer);
166 char *CurPtr = EndPtr;
169 uintptr_t x = N % 16;
170 *--CurPtr = (x < 10 ?
'0' + x :
'a' + x - 10);
174 return write(CurPtr, EndPtr-CurPtr);
178 bool UseHexEscapes) {
179 for (
unsigned i = 0, e = Str.
size(); i != e; ++i) {
180 unsigned char c = Str[i];
184 *
this <<
'\\' <<
'\\';
187 *
this <<
'\\' <<
't';
190 *
this <<
'\\' <<
'n';
193 *
this <<
'\\' <<
'"';
196 if (std::isprint(c)) {
203 *
this <<
'\\' <<
'x';
204 *this << hexdigit((c >> 4 & 0xF));
205 *this << hexdigit((c >> 0) & 0xF);
209 *this << char('0' + ((c >> 6) & 7));
210 *this << char('0' + ((c >> 3) & 7));
211 *this << char('0' + ((c >> 0) & 7));
230 int fpcl = _fpclass(N);
233 if (fpcl == _FPCLASS_NZ)
234 return *
this <<
"-0.000000e+00";
238 len =
snprintf(buf,
sizeof(buf),
"%e", N);
239 if (len <=
sizeof(buf) - 2) {
240 if (len >= 5 && buf[len - 5] ==
'e' && buf[len - 3] ==
'0') {
241 int cs = buf[len - 4];
242 if (cs ==
'+' || cs ==
'-') {
243 int c1 = buf[len - 2];
244 int c0 = buf[len - 1];
245 if (
isdigit(static_cast<unsigned char>(c1)) &&
246 isdigit(static_cast<unsigned char>(c0))) {
262 void raw_ostream::flush_nonempty() {
263 assert(OutBufCur > OutBufStart &&
"Invalid call to flush_nonempty.");
264 size_t Length = OutBufCur - OutBufStart;
265 OutBufCur = OutBufStart;
266 write_impl(OutBufStart, Length);
273 if (BufferMode == Unbuffered) {
274 write_impl(reinterpret_cast<char*>(&C), 1);
293 if (BufferMode == Unbuffered) {
294 write_impl(Ptr, Size);
299 return write(Ptr, Size);
302 size_t NumBytes = OutBufEnd - OutBufCur;
308 size_t BytesToWrite = Size - (Size % NumBytes);
309 write_impl(Ptr, BytesToWrite);
310 size_t BytesRemaining = Size - BytesToWrite;
311 if (BytesRemaining >
size_t(OutBufEnd - OutBufCur)) {
313 return write(Ptr + BytesToWrite, BytesRemaining);
315 copy_to_buffer(Ptr + BytesToWrite, BytesRemaining);
321 copy_to_buffer(Ptr, NumBytes);
323 return write(Ptr + NumBytes, Size - NumBytes);
326 copy_to_buffer(Ptr, Size);
331 void raw_ostream::copy_to_buffer(
const char *Ptr,
size_t Size) {
332 assert(Size <=
size_t(OutBufEnd - OutBufCur) &&
"Buffer overrun!");
337 case 4: OutBufCur[3] = Ptr[3];
338 case 3: OutBufCur[2] = Ptr[2];
339 case 2: OutBufCur[1] = Ptr[1];
340 case 1: OutBufCur[0] = Ptr[0];
343 memcpy(OutBufCur, Ptr, Size);
354 size_t NextBufferSize = 127;
355 size_t BufferBytesLeft = OutBufEnd - OutBufCur;
356 if (BufferBytesLeft > 3) {
357 size_t BytesUsed = Fmt.
print(OutBufCur, BufferBytesLeft);
360 if (BytesUsed <= BufferBytesLeft) {
361 OutBufCur += BytesUsed;
367 NextBufferSize = BytesUsed;
379 size_t BytesUsed = Fmt.
print(V.
data(), NextBufferSize);
382 if (BytesUsed <= NextBufferSize)
386 assert(BytesUsed > NextBufferSize &&
"Didn't grow buffer!?");
387 NextBufferSize = BytesUsed;
393 static const char Spaces[] =
" "
399 return write(Spaces, NumSpaces);
402 unsigned NumToWrite = std::min(NumSpaces,
404 write(Spaces, NumToWrite);
405 NumSpaces -= NumToWrite;
430 assert(Filename != 0 &&
"Filename is null");
436 if (Filename[0] ==
'-' && Filename[1] == 0) {
450 ErrorInfo =
"Error opening output file '" + std::string(Filename) +
"': " +
468 if (fd == STDOUT_FILENO || fd == STDERR_FILENO)
469 setmode(fd, O_BINARY);
473 off_t loc = ::lseek(FD, 0, SEEK_CUR);
474 if (loc == (off_t)-1)
477 pos =
static_cast<uint64_t
>(loc);
484 while (::
close(FD) != 0)
485 if (errno != EINTR) {
508 void raw_fd_ostream::write_impl(
const char *Ptr,
size_t Size) {
509 assert(FD >= 0 &&
"File already closed.");
520 #if defined(HAVE_WRITEV)
521 const void *Addr =
static_cast<const void *
>(Ptr);
522 struct iovec IOV = {
const_cast<void *
>(Addr), Size };
523 ret = ::writev(FD, &IOV, 1);
538 if (errno == EINTR || errno == EAGAIN
540 || errno == EWOULDBLOCK
562 while (::
close(FD) != 0)
563 if (errno != EINTR) {
572 pos = ::lseek(FD, off, SEEK_SET);
578 size_t raw_fd_ostream::preferred_buffer_size()
const {
579 #if !defined(_MSC_VER) && !defined(__MINGW32__)
581 assert(FD >= 0 &&
"File not yet open!");
583 if (
fstat(FD, &statbuf) != 0)
589 if (S_ISCHR(statbuf.st_mode) && isatty(FD))
592 return statbuf.st_blksize;
602 const char *colorcode =
606 size_t len =
strlen(colorcode);
607 write(colorcode, len);
619 size_t len =
strlen(colorcode);
620 write(colorcode, len);
632 size_t len =
strlen(colorcode);
633 write(colorcode, len);
685 void raw_string_ostream::write_impl(
const char *Ptr,
size_t Size) {
686 OS.append(Ptr, Size);
724 void raw_svector_ostream::write_impl(
const char *Ptr,
size_t Size) {
728 if (Ptr == OS.
end()) {
729 assert(OS.
size() + Size <= OS.
capacity() &&
"Invalid write_impl() call!");
733 "Should be writing from buffer if some bytes in it");
746 uint64_t raw_svector_ostream::current_pos()
const {
768 void raw_null_ostream::write_impl(
const char *Ptr,
size_t Size) {
771 uint64_t raw_null_ostream::current_pos()
const {
void set_size(unsigned N)
virtual raw_ostream & resetColor() LLVM_OVERRIDE
size_t size() const
size - Get the string size.
int fstat(int fildes, struct stat *buf);
error_code openFileForWrite(const Twine &Name, int &ResultFD, OpenFlags Flags, unsigned Mode=0666)
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)
raw_null_ostream - A raw_ostream that discards all output.
uint64_t seek(uint64_t off)
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
static bool FileDescriptorHasColors(int fd)
static bool FileDescriptorIsDisplayed(int fd)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
static bool ColorNeedsFlush()
static const char * OutputReverse()
void SetBuffer(char *BufferStart, size_t Size)
raw_svector_ostream(SmallVectorImpl< char > &O)
format_object1< T > format(const char *Fmt, const T &Val)
size_t array_lengthof(T(&)[N])
Find the length of an array.
raw_ostream & write_hex(unsigned long long N)
write_hex - Output N in hexadecimal, without any prefix or padding.
raw_ostream & operator<<(char C)
virtual raw_ostream & changeColor(enum Colors colors, bool bold=false, bool bg=false) LLVM_OVERRIDE
raw_fd_ostream(const char *Filename, std::string &ErrorInfo, sys::fs::OpenFlags Flags=sys::fs::F_None)
int snprintf(char *s, size_t n, const char *format, ...);
void append(in_iter in_start, in_iter in_end)
void SetBufferSize(size_t Size)
int stat(const char *path, struct stat *buf);
raw_ostream & write(unsigned char C)
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
virtual bool has_colors() const LLVM_OVERRIDE
This function determines if this stream is displayed and supports colors.
virtual size_t preferred_buffer_size() const
size_t strlen(const char *s);
std::string message() const
pointer data()
data - Return a pointer to the vector's buffer, even if empty().
virtual raw_ostream & reverseColor() LLVM_OVERRIDE
Reverses the forground and background colors.
static const char * OutputBold(bool bg)
Same as OutputColor, but only enables the bold attribute.
raw_ostream & nulls()
nulls() - This returns a reference to a raw_ostream which discards output.
virtual bool is_displayed() const LLVM_OVERRIDE
error_code ChangeStdoutToBinary()
size_t GetNumBytesInBuffer() const