1 //===--- raw_ostream.cpp - Implement the raw_ostream classes --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements support for bulk buffered stream output.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/raw_ostream.h"
15 #include "llvm/Support/Format.h"
16 #include "llvm/Support/Program.h"
17 #include "llvm/Support/Process.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/Config/config.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/ADT/STLExtras.h"
27 #include <sys/types.h>
29 #if defined(HAVE_UNISTD_H)
32 #if defined(HAVE_FCNTL_H)
35 #if defined(HAVE_SYS_UIO_H) && defined(HAVE_WRITEV)
39 #if defined(__CYGWIN__)
47 # define STDIN_FILENO 0
50 # define STDOUT_FILENO 1
53 # define STDERR_FILENO 2
59 raw_ostream::~raw_ostream() {
60 // raw_ostream's subclasses should take care to flush the buffer
61 // in their destructors.
62 assert(OutBufCur == OutBufStart &&
63 "raw_ostream destructor called with non-empty buffer!");
65 if (BufferMode == InternalBuffer)
66 delete [] OutBufStart;
69 // An out of line virtual method to provide a home for the class vtable.
70 void raw_ostream::handle() {}
72 size_t raw_ostream::preferred_buffer_size() const {
73 // BUFSIZ is intended to be a reasonable default.
77 void raw_ostream::SetBuffered() {
78 // Ask the subclass to determine an appropriate buffer size.
79 if (size_t Size = preferred_buffer_size())
82 // It may return 0, meaning this stream should be unbuffered.
86 void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size,
88 assert(((Mode == Unbuffered && BufferStart == 0 && Size == 0) ||
89 (Mode != Unbuffered && BufferStart && Size)) &&
90 "stream must be unbuffered or have at least one byte");
91 // Make sure the current buffer is free of content (we can't flush here; the
92 // child buffer management logic will be in write_impl).
93 assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!");
95 if (BufferMode == InternalBuffer)
96 delete [] OutBufStart;
97 OutBufStart = BufferStart;
98 OutBufEnd = OutBufStart+Size;
99 OutBufCur = OutBufStart;
102 assert(OutBufStart <= OutBufEnd && "Invalid size!");
105 raw_ostream &raw_ostream::operator<<(unsigned long N) {
106 // Zero is a special case.
110 char NumberBuffer[20];
111 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
112 char *CurPtr = EndPtr;
115 *--CurPtr = '0' + char(N % 10);
118 return write(CurPtr, EndPtr-CurPtr);
121 raw_ostream &raw_ostream::operator<<(long N) {
127 return this->operator<<(static_cast<unsigned long>(N));
130 raw_ostream &raw_ostream::operator<<(unsigned long long N) {
131 // Output using 32-bit div/mod when possible.
132 if (N == static_cast<unsigned long>(N))
133 return this->operator<<(static_cast<unsigned long>(N));
135 char NumberBuffer[20];
136 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
137 char *CurPtr = EndPtr;
140 *--CurPtr = '0' + char(N % 10);
143 return write(CurPtr, EndPtr-CurPtr);
146 raw_ostream &raw_ostream::operator<<(long long N) {
149 // Avoid undefined behavior on INT64_MIN with a cast.
150 N = -(unsigned long long)N;
153 return this->operator<<(static_cast<unsigned long long>(N));
156 raw_ostream &raw_ostream::write_hex(unsigned long long N) {
157 // Zero is a special case.
161 char NumberBuffer[20];
162 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
163 char *CurPtr = EndPtr;
166 uintptr_t x = N % 16;
167 *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10);
171 return write(CurPtr, EndPtr-CurPtr);
174 raw_ostream &raw_ostream::write_escaped(StringRef Str,
175 bool UseHexEscapes) {
176 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
177 unsigned char c = Str[i];
181 *this << '\\' << '\\';
184 *this << '\\' << 't';
187 *this << '\\' << 'n';
190 *this << '\\' << '"';
193 if (std::isprint(c)) {
198 // Write out the escaped representation.
200 *this << '\\' << 'x';
201 *this << hexdigit((c >> 4 & 0xF));
202 *this << hexdigit((c >> 0) & 0xF);
204 // Always use a full 3-character octal escape.
206 *this << char('0' + ((c >> 6) & 7));
207 *this << char('0' + ((c >> 3) & 7));
208 *this << char('0' + ((c >> 0) & 7));
216 raw_ostream &raw_ostream::operator<<(const void *P) {
219 return write_hex((uintptr_t) P);
222 raw_ostream &raw_ostream::operator<<(double N) {
223 return this->operator<<(format("%e", N));
228 void raw_ostream::flush_nonempty() {
229 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
230 size_t Length = OutBufCur - OutBufStart;
231 OutBufCur = OutBufStart;
232 write_impl(OutBufStart, Length);
235 raw_ostream &raw_ostream::write(unsigned char C) {
236 // Group exceptional cases into a single branch.
237 if (BUILTIN_EXPECT(OutBufCur >= OutBufEnd, false)) {
238 if (BUILTIN_EXPECT(!OutBufStart, false)) {
239 if (BufferMode == Unbuffered) {
240 write_impl(reinterpret_cast<char*>(&C), 1);
243 // Set up a buffer and start over.
255 raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
256 // Group exceptional cases into a single branch.
257 if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) {
258 if (BUILTIN_EXPECT(!OutBufStart, false)) {
259 if (BufferMode == Unbuffered) {
260 write_impl(Ptr, Size);
263 // Set up a buffer and start over.
265 return write(Ptr, Size);
268 // Write out the data in buffer-sized blocks until the remainder
269 // fits within the buffer.
271 size_t NumBytes = OutBufEnd - OutBufCur;
272 copy_to_buffer(Ptr, NumBytes);
276 } while (OutBufCur+Size > OutBufEnd);
279 copy_to_buffer(Ptr, Size);
284 void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) {
285 assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!");
287 // Handle short strings specially, memcpy isn't very good at very short
290 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
291 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
292 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
293 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
296 memcpy(OutBufCur, Ptr, Size);
304 raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
305 // If we have more than a few bytes left in our output buffer, try
306 // formatting directly onto its end.
307 size_t NextBufferSize = 127;
308 size_t BufferBytesLeft = OutBufEnd - OutBufCur;
309 if (BufferBytesLeft > 3) {
310 size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
312 // Common case is that we have plenty of space.
313 if (BytesUsed <= BufferBytesLeft) {
314 OutBufCur += BytesUsed;
318 // Otherwise, we overflowed and the return value tells us the size to try
320 NextBufferSize = BytesUsed;
323 // If we got here, we didn't have enough space in the output buffer for the
324 // string. Try printing into a SmallVector that is resized to have enough
325 // space. Iterate until we win.
326 SmallVector<char, 128> V;
329 V.resize(NextBufferSize);
331 // Try formatting into the SmallVector.
332 size_t BytesUsed = Fmt.print(V.data(), NextBufferSize);
334 // If BytesUsed fit into the vector, we win.
335 if (BytesUsed <= NextBufferSize)
336 return write(V.data(), BytesUsed);
338 // Otherwise, try again with a new size.
339 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
340 NextBufferSize = BytesUsed;
344 /// indent - Insert 'NumSpaces' spaces.
345 raw_ostream &raw_ostream::indent(unsigned NumSpaces) {
346 static const char Spaces[] = " "
350 // Usually the indentation is small, handle it with a fastpath.
351 if (NumSpaces < array_lengthof(Spaces))
352 return write(Spaces, NumSpaces);
355 unsigned NumToWrite = std::min(NumSpaces,
356 (unsigned)array_lengthof(Spaces)-1);
357 write(Spaces, NumToWrite);
358 NumSpaces -= NumToWrite;
364 //===----------------------------------------------------------------------===//
366 //===----------------------------------------------------------------------===//
368 // Out of line virtual method.
369 void format_object_base::home() {
372 //===----------------------------------------------------------------------===//
374 //===----------------------------------------------------------------------===//
376 /// raw_fd_ostream - Open the specified file for writing. If an error
377 /// occurs, information about the error is put into ErrorInfo, and the
378 /// stream should be immediately destroyed; the string will be empty
379 /// if no error occurred.
380 raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
382 : Error(false), UseAtomicWrites(false), pos(0)
384 assert(Filename != 0 && "Filename is null");
385 // Verify that we don't have both "append" and "excl".
386 assert((!(Flags & F_Excl) || !(Flags & F_Append)) &&
387 "Cannot specify both 'excl' and 'append' file creation flags!");
391 // Handle "-" as stdout. Note that when we do this, we consider ourself
392 // the owner of stdout. This means that we can do things like close the
393 // file descriptor when we're done and set the "binary" flag globally.
394 if (Filename[0] == '-' && Filename[1] == 0) {
396 // If user requested binary then put stdout into binary mode if
398 if (Flags & F_Binary)
399 sys::Program::ChangeStdoutToBinary();
400 // Close stdout when we're done, to detect any output errors.
405 int OpenFlags = O_WRONLY|O_CREAT;
407 if (Flags & F_Binary)
408 OpenFlags |= O_BINARY;
411 if (Flags & F_Append)
412 OpenFlags |= O_APPEND;
414 OpenFlags |= O_TRUNC;
418 while ((FD = open(Filename, OpenFlags, 0664)) < 0) {
419 if (errno != EINTR) {
420 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
426 // Ok, we successfully opened the file, so it'll need to be closed.
430 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
431 /// ShouldClose is true, this closes the file when the stream is destroyed.
432 raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered)
433 : raw_ostream(unbuffered), FD(fd),
434 ShouldClose(shouldClose), Error(false), UseAtomicWrites(false) {
436 // Setting STDOUT and STDERR to binary mode is necessary in Win32
437 // to avoid undesirable linefeed conversion.
438 if (fd == STDOUT_FILENO || fd == STDERR_FILENO)
439 setmode(fd, O_BINARY);
442 // Get the starting position.
443 off_t loc = ::lseek(FD, 0, SEEK_CUR);
444 if (loc == (off_t)-1)
447 pos = static_cast<uint64_t>(loc);
450 raw_fd_ostream::~raw_fd_ostream() {
454 while (::close(FD) != 0)
455 if (errno != EINTR) {
461 // If there are any pending errors, report them now. Clients wishing
462 // to avoid report_fatal_error calls should check for errors with
463 // has_error() and clear the error flag with clear_error() before
464 // destructing raw_ostream objects which may have errors.
466 report_fatal_error("IO failure on output stream.");
470 void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
471 assert(FD >= 0 && "File already closed.");
477 // Check whether we should attempt to use atomic writes.
478 if (BUILTIN_EXPECT(!UseAtomicWrites, true)) {
479 ret = ::write(FD, Ptr, Size);
481 // Use ::writev() where available.
482 #if defined(HAVE_WRITEV)
483 struct iovec IOV = { (void*) Ptr, Size };
484 ret = ::writev(FD, &IOV, 1);
486 ret = ::write(FD, Ptr, Size);
491 // If it's a recoverable error, swallow it and retry the write.
493 // Ideally we wouldn't ever see EAGAIN or EWOULDBLOCK here, since
494 // raw_ostream isn't designed to do non-blocking I/O. However, some
495 // programs, such as old versions of bjam, have mistakenly used
496 // O_NONBLOCK. For compatibility, emulate blocking semantics by
497 // spinning until the write succeeds. If you don't want spinning,
498 // don't use O_NONBLOCK file descriptors with raw_ostream.
499 if (errno == EINTR || errno == EAGAIN
501 || errno == EWOULDBLOCK
506 // Otherwise it's a non-recoverable error. Note it and quit.
511 // The write may have written some or all of the data. Update the
512 // size and buffer pointer to reflect the remainder that needs
513 // to be written. If there are no bytes left, we're done.
519 void raw_fd_ostream::close() {
523 while (::close(FD) != 0)
524 if (errno != EINTR) {
531 uint64_t raw_fd_ostream::seek(uint64_t off) {
533 pos = ::lseek(FD, off, SEEK_SET);
539 size_t raw_fd_ostream::preferred_buffer_size() const {
540 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__minix)
541 // Windows and Minix have no st_blksize.
542 assert(FD >= 0 && "File not yet open!");
544 if (fstat(FD, &statbuf) != 0)
547 // If this is a terminal, don't use buffering. Line buffering
548 // would be a more traditional thing to do, but it's not worth
550 if (S_ISCHR(statbuf.st_mode) && isatty(FD))
552 // Return the preferred block size.
553 return statbuf.st_blksize;
555 return raw_ostream::preferred_buffer_size();
559 raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
561 if (sys::Process::ColorNeedsFlush())
563 const char *colorcode =
564 (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
565 : sys::Process::OutputColor(colors, bold, bg);
567 size_t len = strlen(colorcode);
568 write(colorcode, len);
569 // don't account colors towards output characters
575 raw_ostream &raw_fd_ostream::resetColor() {
576 if (sys::Process::ColorNeedsFlush())
578 const char *colorcode = sys::Process::ResetColor();
580 size_t len = strlen(colorcode);
581 write(colorcode, len);
582 // don't account colors towards output characters
588 bool raw_fd_ostream::is_displayed() const {
589 return sys::Process::FileDescriptorIsDisplayed(FD);
592 //===----------------------------------------------------------------------===//
593 // outs(), errs(), nulls()
594 //===----------------------------------------------------------------------===//
596 /// outs() - This returns a reference to a raw_ostream for standard output.
597 /// Use it like: outs() << "foo" << "bar";
598 raw_ostream &llvm::outs() {
599 // Set buffer settings to model stdout behavior.
600 // Delete the file descriptor when the program exists, forcing error
601 // detection. If you don't want this behavior, don't use outs().
602 static raw_fd_ostream S(STDOUT_FILENO, true);
606 /// errs() - This returns a reference to a raw_ostream for standard error.
607 /// Use it like: errs() << "foo" << "bar";
608 raw_ostream &llvm::errs() {
609 // Set standard error to be unbuffered by default.
610 static raw_fd_ostream S(STDERR_FILENO, false, true);
614 /// nulls() - This returns a reference to a raw_ostream which discards output.
615 raw_ostream &llvm::nulls() {
616 static raw_null_ostream S;
621 //===----------------------------------------------------------------------===//
622 // raw_string_ostream
623 //===----------------------------------------------------------------------===//
625 raw_string_ostream::~raw_string_ostream() {
629 void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
630 OS.append(Ptr, Size);
633 //===----------------------------------------------------------------------===//
634 // raw_svector_ostream
635 //===----------------------------------------------------------------------===//
637 // The raw_svector_ostream implementation uses the SmallVector itself as the
638 // buffer for the raw_ostream. We guarantee that the raw_ostream buffer is
639 // always pointing past the end of the vector, but within the vector
640 // capacity. This allows raw_ostream to write directly into the correct place,
641 // and we only need to set the vector size when the data is flushed.
643 raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {
644 // Set up the initial external buffer. We make sure that the buffer has at
645 // least 128 bytes free; raw_ostream itself only requires 64, but we want to
646 // make sure that we don't grow the buffer unnecessarily on destruction (when
647 // the data is flushed). See the FIXME below.
648 OS.reserve(OS.size() + 128);
649 SetBuffer(OS.end(), OS.capacity() - OS.size());
652 raw_svector_ostream::~raw_svector_ostream() {
653 // FIXME: Prevent resizing during this flush().
657 /// resync - This is called when the SmallVector we're appending to is changed
658 /// outside of the raw_svector_ostream's control. It is only safe to do this
659 /// if the raw_svector_ostream has previously been flushed.
660 void raw_svector_ostream::resync() {
661 assert(GetNumBytesInBuffer() == 0 && "Didn't flush before mutating vector");
663 if (OS.capacity() - OS.size() < 64)
664 OS.reserve(OS.capacity() * 2);
665 SetBuffer(OS.end(), OS.capacity() - OS.size());
668 void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
669 // If we're writing bytes from the end of the buffer into the smallvector, we
670 // don't need to copy the bytes, just commit the bytes because they are
671 // already in the right place.
672 if (Ptr == OS.end()) {
673 assert(OS.size() + Size <= OS.capacity() && "Invalid write_impl() call!");
674 OS.set_size(OS.size() + Size);
676 assert(GetNumBytesInBuffer() == 0 &&
677 "Should be writing from buffer if some bytes in it");
678 // Otherwise, do copy the bytes.
679 OS.append(Ptr, Ptr+Size);
682 // Grow the vector if necessary.
683 if (OS.capacity() - OS.size() < 64)
684 OS.reserve(OS.capacity() * 2);
686 // Update the buffer position.
687 SetBuffer(OS.end(), OS.capacity() - OS.size());
690 uint64_t raw_svector_ostream::current_pos() const {
694 StringRef raw_svector_ostream::str() {
696 return StringRef(OS.begin(), OS.size());
699 //===----------------------------------------------------------------------===//
701 //===----------------------------------------------------------------------===//
703 raw_null_ostream::~raw_null_ostream() {
705 // ~raw_ostream asserts that the buffer is empty. This isn't necessary
706 // with raw_null_ostream, but it's better to have raw_null_ostream follow
707 // the rules than to change the rules just for raw_null_ostream.
712 void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
715 uint64_t raw_null_ostream::current_pos() const {