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/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Config/config.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/FormatVariadic.h"
24 #include "llvm/Support/MathExtras.h"
25 #include "llvm/Support/NativeFormatting.h"
26 #include "llvm/Support/Process.h"
27 #include "llvm/Support/Program.h"
34 #include <system_error>
36 // <fcntl.h> may provide O_BINARY.
37 #if defined(HAVE_FCNTL_H)
41 #if defined(HAVE_UNISTD_H)
44 #if defined(HAVE_SYS_UIO_H) && defined(HAVE_WRITEV)
48 #if defined(__CYGWIN__)
55 # define STDIN_FILENO 0
58 # define STDOUT_FILENO 1
61 # define STDERR_FILENO 2
66 #include "Windows/WindowsSupport.h"
71 raw_ostream::~raw_ostream() {
72 // raw_ostream's subclasses should take care to flush the buffer
73 // in their destructors.
74 assert(OutBufCur == OutBufStart &&
75 "raw_ostream destructor called with non-empty buffer!");
77 if (BufferMode == InternalBuffer)
78 delete [] OutBufStart;
81 // An out of line virtual method to provide a home for the class vtable.
82 void raw_ostream::handle() {}
84 size_t raw_ostream::preferred_buffer_size() const {
85 // BUFSIZ is intended to be a reasonable default.
89 void raw_ostream::SetBuffered() {
90 // Ask the subclass to determine an appropriate buffer size.
91 if (size_t Size = preferred_buffer_size())
94 // It may return 0, meaning this stream should be unbuffered.
98 void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size,
100 assert(((Mode == Unbuffered && !BufferStart && Size == 0) ||
101 (Mode != Unbuffered && BufferStart && Size != 0)) &&
102 "stream must be unbuffered or have at least one byte");
103 // Make sure the current buffer is free of content (we can't flush here; the
104 // child buffer management logic will be in write_impl).
105 assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!");
107 if (BufferMode == InternalBuffer)
108 delete [] OutBufStart;
109 OutBufStart = BufferStart;
110 OutBufEnd = OutBufStart+Size;
111 OutBufCur = OutBufStart;
114 assert(OutBufStart <= OutBufEnd && "Invalid size!");
117 raw_ostream &raw_ostream::operator<<(unsigned long N) {
118 write_integer(*this, static_cast<uint64_t>(N), 0, IntegerStyle::Integer);
122 raw_ostream &raw_ostream::operator<<(long N) {
123 write_integer(*this, static_cast<int64_t>(N), 0, IntegerStyle::Integer);
127 raw_ostream &raw_ostream::operator<<(unsigned long long N) {
128 write_integer(*this, static_cast<uint64_t>(N), 0, IntegerStyle::Integer);
132 raw_ostream &raw_ostream::operator<<(long long N) {
133 write_integer(*this, static_cast<int64_t>(N), 0, IntegerStyle::Integer);
137 raw_ostream &raw_ostream::write_hex(unsigned long long N) {
138 llvm::write_hex(*this, N, HexPrintStyle::Lower);
142 raw_ostream &raw_ostream::write_uuid(const uuid_t UUID) {
143 for (int Idx = 0; Idx < 16; ++Idx) {
144 *this << format("%02" PRIX32, UUID[Idx]);
145 if (Idx == 3 || Idx == 5 || Idx == 7 || Idx == 9)
152 raw_ostream &raw_ostream::write_escaped(StringRef Str,
153 bool UseHexEscapes) {
154 for (unsigned char c : Str) {
157 *this << '\\' << '\\';
160 *this << '\\' << 't';
163 *this << '\\' << 'n';
166 *this << '\\' << '"';
169 if (std::isprint(c)) {
174 // Write out the escaped representation.
176 *this << '\\' << 'x';
177 *this << hexdigit((c >> 4 & 0xF));
178 *this << hexdigit((c >> 0) & 0xF);
180 // Always use a full 3-character octal escape.
182 *this << char('0' + ((c >> 6) & 7));
183 *this << char('0' + ((c >> 3) & 7));
184 *this << char('0' + ((c >> 0) & 7));
192 raw_ostream &raw_ostream::operator<<(const void *P) {
193 llvm::write_hex(*this, (uintptr_t)P, HexPrintStyle::PrefixLower);
197 raw_ostream &raw_ostream::operator<<(double N) {
198 llvm::write_double(*this, N, FloatStyle::Exponent);
202 void raw_ostream::flush_nonempty() {
203 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
204 size_t Length = OutBufCur - OutBufStart;
205 OutBufCur = OutBufStart;
206 write_impl(OutBufStart, Length);
209 raw_ostream &raw_ostream::write(unsigned char C) {
210 // Group exceptional cases into a single branch.
211 if (LLVM_UNLIKELY(OutBufCur >= OutBufEnd)) {
212 if (LLVM_UNLIKELY(!OutBufStart)) {
213 if (BufferMode == Unbuffered) {
214 write_impl(reinterpret_cast<char*>(&C), 1);
217 // Set up a buffer and start over.
229 raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
230 // Group exceptional cases into a single branch.
231 if (LLVM_UNLIKELY(size_t(OutBufEnd - OutBufCur) < Size)) {
232 if (LLVM_UNLIKELY(!OutBufStart)) {
233 if (BufferMode == Unbuffered) {
234 write_impl(Ptr, Size);
237 // Set up a buffer and start over.
239 return write(Ptr, Size);
242 size_t NumBytes = OutBufEnd - OutBufCur;
244 // If the buffer is empty at this point we have a string that is larger
245 // than the buffer. Directly write the chunk that is a multiple of the
246 // preferred buffer size and put the remainder in the buffer.
247 if (LLVM_UNLIKELY(OutBufCur == OutBufStart)) {
248 assert(NumBytes != 0 && "undefined behavior");
249 size_t BytesToWrite = Size - (Size % NumBytes);
250 write_impl(Ptr, BytesToWrite);
251 size_t BytesRemaining = Size - BytesToWrite;
252 if (BytesRemaining > size_t(OutBufEnd - OutBufCur)) {
253 // Too much left over to copy into our buffer.
254 return write(Ptr + BytesToWrite, BytesRemaining);
256 copy_to_buffer(Ptr + BytesToWrite, BytesRemaining);
260 // We don't have enough space in the buffer to fit the string in. Insert as
261 // much as possible, flush and start over with the remainder.
262 copy_to_buffer(Ptr, NumBytes);
264 return write(Ptr + NumBytes, Size - NumBytes);
267 copy_to_buffer(Ptr, Size);
272 void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) {
273 assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!");
275 // Handle short strings specially, memcpy isn't very good at very short
278 case 4: OutBufCur[3] = Ptr[3]; LLVM_FALLTHROUGH;
279 case 3: OutBufCur[2] = Ptr[2]; LLVM_FALLTHROUGH;
280 case 2: OutBufCur[1] = Ptr[1]; LLVM_FALLTHROUGH;
281 case 1: OutBufCur[0] = Ptr[0]; LLVM_FALLTHROUGH;
284 memcpy(OutBufCur, Ptr, Size);
292 raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
293 // If we have more than a few bytes left in our output buffer, try
294 // formatting directly onto its end.
295 size_t NextBufferSize = 127;
296 size_t BufferBytesLeft = OutBufEnd - OutBufCur;
297 if (BufferBytesLeft > 3) {
298 size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
300 // Common case is that we have plenty of space.
301 if (BytesUsed <= BufferBytesLeft) {
302 OutBufCur += BytesUsed;
306 // Otherwise, we overflowed and the return value tells us the size to try
308 NextBufferSize = BytesUsed;
311 // If we got here, we didn't have enough space in the output buffer for the
312 // string. Try printing into a SmallVector that is resized to have enough
313 // space. Iterate until we win.
314 SmallVector<char, 128> V;
317 V.resize(NextBufferSize);
319 // Try formatting into the SmallVector.
320 size_t BytesUsed = Fmt.print(V.data(), NextBufferSize);
322 // If BytesUsed fit into the vector, we win.
323 if (BytesUsed <= NextBufferSize)
324 return write(V.data(), BytesUsed);
326 // Otherwise, try again with a new size.
327 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
328 NextBufferSize = BytesUsed;
332 raw_ostream &raw_ostream::operator<<(const formatv_object_base &Obj) {
338 raw_ostream &raw_ostream::operator<<(const FormattedString &FS) {
339 if (FS.Str.size() >= FS.Width || FS.Justify == FormattedString::JustifyNone) {
340 this->operator<<(FS.Str);
343 const size_t Difference = FS.Width - FS.Str.size();
344 switch (FS.Justify) {
345 case FormattedString::JustifyLeft:
346 this->operator<<(FS.Str);
347 this->indent(Difference);
349 case FormattedString::JustifyRight:
350 this->indent(Difference);
351 this->operator<<(FS.Str);
353 case FormattedString::JustifyCenter: {
354 int PadAmount = Difference / 2;
355 this->indent(PadAmount);
356 this->operator<<(FS.Str);
357 this->indent(Difference - PadAmount);
361 llvm_unreachable("Bad Justification");
366 raw_ostream &raw_ostream::operator<<(const FormattedNumber &FN) {
369 if (FN.Upper && FN.HexPrefix)
370 Style = HexPrintStyle::PrefixUpper;
371 else if (FN.Upper && !FN.HexPrefix)
372 Style = HexPrintStyle::Upper;
373 else if (!FN.Upper && FN.HexPrefix)
374 Style = HexPrintStyle::PrefixLower;
376 Style = HexPrintStyle::Lower;
377 llvm::write_hex(*this, FN.HexValue, Style, FN.Width);
379 llvm::SmallString<16> Buffer;
380 llvm::raw_svector_ostream Stream(Buffer);
381 llvm::write_integer(Stream, FN.DecValue, 0, IntegerStyle::Integer);
382 if (Buffer.size() < FN.Width)
383 indent(FN.Width - Buffer.size());
389 raw_ostream &raw_ostream::operator<<(const FormattedBytes &FB) {
390 if (FB.Bytes.empty())
393 size_t LineIndex = 0;
394 auto Bytes = FB.Bytes;
395 const size_t Size = Bytes.size();
396 HexPrintStyle HPS = FB.Upper ? HexPrintStyle::Upper : HexPrintStyle::Lower;
397 uint64_t OffsetWidth = 0;
398 if (FB.FirstByteOffset.hasValue()) {
399 // Figure out how many nibbles are needed to print the largest offset
400 // represented by this data set, so that we can align the offset field
401 // to the right width.
402 size_t Lines = Size / FB.NumPerLine;
403 uint64_t MaxOffset = *FB.FirstByteOffset + Lines * FB.NumPerLine;
406 Power = llvm::Log2_64_Ceil(MaxOffset);
407 OffsetWidth = std::max<uint64_t>(4, llvm::alignTo(Power, 4) / 4);
410 // The width of a block of data including all spaces for group separators.
411 unsigned NumByteGroups =
412 alignTo(FB.NumPerLine, FB.ByteGroupSize) / FB.ByteGroupSize;
413 unsigned BlockCharWidth = FB.NumPerLine * 2 + NumByteGroups - 1;
415 while (!Bytes.empty()) {
416 indent(FB.IndentLevel);
418 if (FB.FirstByteOffset.hasValue()) {
419 uint64_t Offset = FB.FirstByteOffset.getValue();
420 llvm::write_hex(*this, Offset + LineIndex, HPS, OffsetWidth);
424 auto Line = Bytes.take_front(FB.NumPerLine);
426 size_t CharsPrinted = 0;
427 // Print the hex bytes for this line in groups
428 for (size_t I = 0; I < Line.size(); ++I, CharsPrinted += 2) {
429 if (I && (I % FB.ByteGroupSize) == 0) {
433 llvm::write_hex(*this, Line[I], HPS, 2);
437 // Print any spaces needed for any bytes that we didn't print on this
438 // line so that the ASCII bytes are correctly aligned.
439 assert(BlockCharWidth >= CharsPrinted);
440 indent(BlockCharWidth - CharsPrinted + 2);
443 // Print the ASCII char values for each byte on this line
444 for (uint8_t Byte : Line) {
446 *this << static_cast<char>(Byte);
453 Bytes = Bytes.drop_front(Line.size());
454 LineIndex += Line.size();
455 if (LineIndex < Size)
461 /// indent - Insert 'NumSpaces' spaces.
462 raw_ostream &raw_ostream::indent(unsigned NumSpaces) {
463 static const char Spaces[] = " "
467 // Usually the indentation is small, handle it with a fastpath.
468 if (NumSpaces < array_lengthof(Spaces))
469 return write(Spaces, NumSpaces);
472 unsigned NumToWrite = std::min(NumSpaces,
473 (unsigned)array_lengthof(Spaces)-1);
474 write(Spaces, NumToWrite);
475 NumSpaces -= NumToWrite;
480 //===----------------------------------------------------------------------===//
482 //===----------------------------------------------------------------------===//
484 // Out of line virtual method.
485 void format_object_base::home() {
488 //===----------------------------------------------------------------------===//
490 //===----------------------------------------------------------------------===//
492 static int getFD(StringRef Filename, std::error_code &EC,
493 sys::fs::OpenFlags Flags) {
494 // Handle "-" as stdout. Note that when we do this, we consider ourself
495 // the owner of stdout and may set the "binary" flag globally based on Flags.
496 if (Filename == "-") {
497 EC = std::error_code();
498 // If user requested binary then put stdout into binary mode if
500 if (!(Flags & sys::fs::F_Text))
501 sys::ChangeStdoutToBinary();
502 return STDOUT_FILENO;
506 EC = sys::fs::openFileForWrite(Filename, FD, Flags);
513 raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
514 sys::fs::OpenFlags Flags)
515 : raw_fd_ostream(getFD(Filename, EC, Flags), true) {}
517 /// FD is the file descriptor that this writes to. If ShouldClose is true, this
518 /// closes the file when the stream is destroyed.
519 raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered)
520 : raw_pwrite_stream(unbuffered), FD(fd), ShouldClose(shouldClose) {
526 // Do not attempt to close stdout or stderr. We used to try to maintain the
527 // property that tools that support writing file to stdout should not also
528 // write informational output to stdout, but in practice we were never able to
529 // maintain this invariant. Many features have been added to LLVM and clang
530 // (-fdump-record-layouts, optimization remarks, etc) that print to stdout, so
531 // users must simply be aware that mixed output and remarks is a possibility.
532 if (FD <= STDERR_FILENO)
535 // Get the starting position.
536 off_t loc = ::lseek(FD, 0, SEEK_CUR);
538 // MSVCRT's _lseek(SEEK_CUR) doesn't return -1 for pipes.
539 sys::fs::file_status Status;
540 std::error_code EC = status(FD, Status);
541 SupportsSeeking = !EC && Status.type() == sys::fs::file_type::regular_file;
543 SupportsSeeking = loc != (off_t)-1;
545 if (!SupportsSeeking)
548 pos = static_cast<uint64_t>(loc);
551 raw_fd_ostream::~raw_fd_ostream() {
555 if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
561 // On mingw, global dtors should not call exit().
562 // report_fatal_error() invokes exit(). We know report_fatal_error()
563 // might not write messages to stderr when any errors were detected
568 // If there are any pending errors, report them now. Clients wishing
569 // to avoid report_fatal_error calls should check for errors with
570 // has_error() and clear the error flag with clear_error() before
571 // destructing raw_ostream objects which may have errors.
573 report_fatal_error("IO failure on output stream: " + error().message(),
574 /*GenCrashDiag=*/false);
577 void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
578 assert(FD >= 0 && "File already closed.");
581 // The maximum write size is limited to SSIZE_MAX because a write
582 // greater than SSIZE_MAX is implementation-defined in POSIX.
583 // Since SSIZE_MAX is not portable, we use SIZE_MAX >> 1 instead.
584 size_t MaxWriteSize = SIZE_MAX >> 1;
586 #if defined(__linux__)
587 // It is observed that Linux returns EINVAL for a very large write (>2G).
588 // Make it a reasonably small value.
589 MaxWriteSize = 1024 * 1024 * 1024;
590 #elif defined(LLVM_ON_WIN32)
591 // Writing a large size of output to Windows console returns ENOMEM. It seems
592 // that, prior to Windows 8, WriteFile() is redirecting to WriteConsole(), and
593 // the latter has a size limit (66000 bytes or less, depending on heap usage).
594 if (::_isatty(FD) && !RunningWindows8OrGreater())
595 MaxWriteSize = 32767;
599 size_t ChunkSize = std::min(Size, MaxWriteSize);
600 ssize_t ret = ::write(FD, Ptr, ChunkSize);
603 // If it's a recoverable error, swallow it and retry the write.
605 // Ideally we wouldn't ever see EAGAIN or EWOULDBLOCK here, since
606 // raw_ostream isn't designed to do non-blocking I/O. However, some
607 // programs, such as old versions of bjam, have mistakenly used
608 // O_NONBLOCK. For compatibility, emulate blocking semantics by
609 // spinning until the write succeeds. If you don't want spinning,
610 // don't use O_NONBLOCK file descriptors with raw_ostream.
611 if (errno == EINTR || errno == EAGAIN
613 || errno == EWOULDBLOCK
618 // Otherwise it's a non-recoverable error. Note it and quit.
619 error_detected(std::error_code(errno, std::generic_category()));
623 // The write may have written some or all of the data. Update the
624 // size and buffer pointer to reflect the remainder that needs
625 // to be written. If there are no bytes left, we're done.
631 void raw_fd_ostream::close() {
635 if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
640 uint64_t raw_fd_ostream::seek(uint64_t off) {
641 assert(SupportsSeeking && "Stream does not support seeking!");
644 pos = ::_lseeki64(FD, off, SEEK_SET);
645 #elif defined(HAVE_LSEEK64)
646 pos = ::lseek64(FD, off, SEEK_SET);
648 pos = ::lseek(FD, off, SEEK_SET);
650 if (pos == (uint64_t)-1)
651 error_detected(std::error_code(errno, std::generic_category()));
655 void raw_fd_ostream::pwrite_impl(const char *Ptr, size_t Size,
657 uint64_t Pos = tell();
663 size_t raw_fd_ostream::preferred_buffer_size() const {
664 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__minix)
665 // Windows and Minix have no st_blksize.
666 assert(FD >= 0 && "File not yet open!");
668 if (fstat(FD, &statbuf) != 0)
671 // If this is a terminal, don't use buffering. Line buffering
672 // would be a more traditional thing to do, but it's not worth
674 if (S_ISCHR(statbuf.st_mode) && isatty(FD))
676 // Return the preferred block size.
677 return statbuf.st_blksize;
679 return raw_ostream::preferred_buffer_size();
683 raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
685 if (sys::Process::ColorNeedsFlush())
687 const char *colorcode =
688 (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
689 : sys::Process::OutputColor(colors, bold, bg);
691 size_t len = strlen(colorcode);
692 write(colorcode, len);
693 // don't account colors towards output characters
699 raw_ostream &raw_fd_ostream::resetColor() {
700 if (sys::Process::ColorNeedsFlush())
702 const char *colorcode = sys::Process::ResetColor();
704 size_t len = strlen(colorcode);
705 write(colorcode, len);
706 // don't account colors towards output characters
712 raw_ostream &raw_fd_ostream::reverseColor() {
713 if (sys::Process::ColorNeedsFlush())
715 const char *colorcode = sys::Process::OutputReverse();
717 size_t len = strlen(colorcode);
718 write(colorcode, len);
719 // don't account colors towards output characters
725 bool raw_fd_ostream::is_displayed() const {
726 return sys::Process::FileDescriptorIsDisplayed(FD);
729 bool raw_fd_ostream::has_colors() const {
730 return sys::Process::FileDescriptorHasColors(FD);
733 //===----------------------------------------------------------------------===//
734 // outs(), errs(), nulls()
735 //===----------------------------------------------------------------------===//
737 /// outs() - This returns a reference to a raw_ostream for standard output.
738 /// Use it like: outs() << "foo" << "bar";
739 raw_ostream &llvm::outs() {
740 // Set buffer settings to model stdout behavior.
742 static raw_fd_ostream S("-", EC, sys::fs::F_None);
747 /// errs() - This returns a reference to a raw_ostream for standard error.
748 /// Use it like: errs() << "foo" << "bar";
749 raw_ostream &llvm::errs() {
750 // Set standard error to be unbuffered by default.
751 static raw_fd_ostream S(STDERR_FILENO, false, true);
755 /// nulls() - This returns a reference to a raw_ostream which discards output.
756 raw_ostream &llvm::nulls() {
757 static raw_null_ostream S;
761 //===----------------------------------------------------------------------===//
762 // raw_string_ostream
763 //===----------------------------------------------------------------------===//
765 raw_string_ostream::~raw_string_ostream() {
769 void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
770 OS.append(Ptr, Size);
773 //===----------------------------------------------------------------------===//
774 // raw_svector_ostream
775 //===----------------------------------------------------------------------===//
777 uint64_t raw_svector_ostream::current_pos() const { return OS.size(); }
779 void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
780 OS.append(Ptr, Ptr + Size);
783 void raw_svector_ostream::pwrite_impl(const char *Ptr, size_t Size,
785 memcpy(OS.data() + Offset, Ptr, Size);
788 //===----------------------------------------------------------------------===//
790 //===----------------------------------------------------------------------===//
792 raw_null_ostream::~raw_null_ostream() {
794 // ~raw_ostream asserts that the buffer is empty. This isn't necessary
795 // with raw_null_ostream, but it's better to have raw_null_ostream follow
796 // the rules than to change the rules just for raw_null_ostream.
801 void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
804 uint64_t raw_null_ostream::current_pos() const {
808 void raw_null_ostream::pwrite_impl(const char *Ptr, size_t Size,