1 //===--- raw_ostream.cpp - Implement the raw_ostream classes --------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This implements support for bulk buffered stream output.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/raw_ostream.h"
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/Duration.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/Format.h"
21 #include "llvm/Support/FormatVariadic.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/NativeFormatting.h"
24 #include "llvm/Support/Process.h"
25 #include "llvm/Support/Program.h"
31 // <fcntl.h> may provide O_BINARY.
32 #if defined(HAVE_FCNTL_H)
36 #if defined(HAVE_UNISTD_H)
40 #if defined(__CYGWIN__)
47 # define STDIN_FILENO 0
50 # define STDOUT_FILENO 1
53 # define STDERR_FILENO 2
58 #include "llvm/Support/ConvertUTF.h"
59 #include "llvm/Support/Signals.h"
60 #include "llvm/Support/Windows/WindowsSupport.h"
65 constexpr raw_ostream::Colors
raw_ostream::BLACK
;
66 constexpr raw_ostream::Colors
raw_ostream::RED
;
67 constexpr raw_ostream::Colors
raw_ostream::GREEN
;
68 constexpr raw_ostream::Colors
raw_ostream::YELLOW
;
69 constexpr raw_ostream::Colors
raw_ostream::BLUE
;
70 constexpr raw_ostream::Colors
raw_ostream::MAGENTA
;
71 constexpr raw_ostream::Colors
raw_ostream::CYAN
;
72 constexpr raw_ostream::Colors
raw_ostream::WHITE
;
73 constexpr raw_ostream::Colors
raw_ostream::SAVEDCOLOR
;
74 constexpr raw_ostream::Colors
raw_ostream::RESET
;
76 raw_ostream::~raw_ostream() {
77 // raw_ostream's subclasses should take care to flush the buffer
78 // in their destructors.
79 assert(OutBufCur
== OutBufStart
&&
80 "raw_ostream destructor called with non-empty buffer!");
82 if (BufferMode
== BufferKind::InternalBuffer
)
83 delete [] OutBufStart
;
86 size_t raw_ostream::preferred_buffer_size() const {
88 // On Windows BUFSIZ is only 512 which results in more calls to write. This
89 // overhead can cause significant performance degradation. Therefore use a
93 // BUFSIZ is intended to be a reasonable default.
98 void raw_ostream::SetBuffered() {
99 // Ask the subclass to determine an appropriate buffer size.
100 if (size_t Size
= preferred_buffer_size())
103 // It may return 0, meaning this stream should be unbuffered.
107 void raw_ostream::SetBufferAndMode(char *BufferStart
, size_t Size
,
109 assert(((Mode
== BufferKind::Unbuffered
&& !BufferStart
&& Size
== 0) ||
110 (Mode
!= BufferKind::Unbuffered
&& BufferStart
&& Size
!= 0)) &&
111 "stream must be unbuffered or have at least one byte");
112 // Make sure the current buffer is free of content (we can't flush here; the
113 // child buffer management logic will be in write_impl).
114 assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!");
116 if (BufferMode
== BufferKind::InternalBuffer
)
117 delete [] OutBufStart
;
118 OutBufStart
= BufferStart
;
119 OutBufEnd
= OutBufStart
+Size
;
120 OutBufCur
= OutBufStart
;
123 assert(OutBufStart
<= OutBufEnd
&& "Invalid size!");
126 raw_ostream
&raw_ostream::operator<<(unsigned long N
) {
127 write_integer(*this, static_cast<uint64_t>(N
), 0, IntegerStyle::Integer
);
131 raw_ostream
&raw_ostream::operator<<(long N
) {
132 write_integer(*this, static_cast<int64_t>(N
), 0, IntegerStyle::Integer
);
136 raw_ostream
&raw_ostream::operator<<(unsigned long long N
) {
137 write_integer(*this, static_cast<uint64_t>(N
), 0, IntegerStyle::Integer
);
141 raw_ostream
&raw_ostream::operator<<(long long N
) {
142 write_integer(*this, static_cast<int64_t>(N
), 0, IntegerStyle::Integer
);
146 raw_ostream
&raw_ostream::write_hex(unsigned long long N
) {
147 llvm::write_hex(*this, N
, HexPrintStyle::Lower
);
151 raw_ostream
&raw_ostream::operator<<(Colors C
) {
152 if (C
== Colors::RESET
)
159 raw_ostream
&raw_ostream::write_uuid(const uuid_t UUID
) {
160 for (int Idx
= 0; Idx
< 16; ++Idx
) {
161 *this << format("%02" PRIX32
, UUID
[Idx
]);
162 if (Idx
== 3 || Idx
== 5 || Idx
== 7 || Idx
== 9)
169 raw_ostream
&raw_ostream::write_escaped(StringRef Str
,
170 bool UseHexEscapes
) {
171 for (unsigned char c
: Str
) {
174 *this << '\\' << '\\';
177 *this << '\\' << 't';
180 *this << '\\' << 'n';
183 *this << '\\' << '"';
191 // Write out the escaped representation.
193 *this << '\\' << 'x';
194 *this << hexdigit((c
>> 4) & 0xF);
195 *this << hexdigit((c
>> 0) & 0xF);
197 // Always use a full 3-character octal escape.
199 *this << char('0' + ((c
>> 6) & 7));
200 *this << char('0' + ((c
>> 3) & 7));
201 *this << char('0' + ((c
>> 0) & 7));
209 raw_ostream
&raw_ostream::operator<<(const void *P
) {
210 llvm::write_hex(*this, (uintptr_t)P
, HexPrintStyle::PrefixLower
);
214 raw_ostream
&raw_ostream::operator<<(double N
) {
215 llvm::write_double(*this, N
, FloatStyle::Exponent
);
219 void raw_ostream::flush_nonempty() {
220 assert(OutBufCur
> OutBufStart
&& "Invalid call to flush_nonempty.");
221 size_t Length
= OutBufCur
- OutBufStart
;
222 OutBufCur
= OutBufStart
;
223 flush_tied_then_write(OutBufStart
, Length
);
226 raw_ostream
&raw_ostream::write(unsigned char C
) {
227 // Group exceptional cases into a single branch.
228 if (LLVM_UNLIKELY(OutBufCur
>= OutBufEnd
)) {
229 if (LLVM_UNLIKELY(!OutBufStart
)) {
230 if (BufferMode
== BufferKind::Unbuffered
) {
231 flush_tied_then_write(reinterpret_cast<char *>(&C
), 1);
234 // Set up a buffer and start over.
246 raw_ostream
&raw_ostream::write(const char *Ptr
, size_t Size
) {
247 // Group exceptional cases into a single branch.
248 if (LLVM_UNLIKELY(size_t(OutBufEnd
- OutBufCur
) < Size
)) {
249 if (LLVM_UNLIKELY(!OutBufStart
)) {
250 if (BufferMode
== BufferKind::Unbuffered
) {
251 flush_tied_then_write(Ptr
, Size
);
254 // Set up a buffer and start over.
256 return write(Ptr
, Size
);
259 size_t NumBytes
= OutBufEnd
- OutBufCur
;
261 // If the buffer is empty at this point we have a string that is larger
262 // than the buffer. Directly write the chunk that is a multiple of the
263 // preferred buffer size and put the remainder in the buffer.
264 if (LLVM_UNLIKELY(OutBufCur
== OutBufStart
)) {
265 assert(NumBytes
!= 0 && "undefined behavior");
266 size_t BytesToWrite
= Size
- (Size
% NumBytes
);
267 flush_tied_then_write(Ptr
, BytesToWrite
);
268 size_t BytesRemaining
= Size
- BytesToWrite
;
269 if (BytesRemaining
> size_t(OutBufEnd
- OutBufCur
)) {
270 // Too much left over to copy into our buffer.
271 return write(Ptr
+ BytesToWrite
, BytesRemaining
);
273 copy_to_buffer(Ptr
+ BytesToWrite
, BytesRemaining
);
277 // We don't have enough space in the buffer to fit the string in. Insert as
278 // much as possible, flush and start over with the remainder.
279 copy_to_buffer(Ptr
, NumBytes
);
281 return write(Ptr
+ NumBytes
, Size
- NumBytes
);
284 copy_to_buffer(Ptr
, Size
);
289 void raw_ostream::copy_to_buffer(const char *Ptr
, size_t Size
) {
290 assert(Size
<= size_t(OutBufEnd
- OutBufCur
) && "Buffer overrun!");
292 // Handle short strings specially, memcpy isn't very good at very short
295 case 4: OutBufCur
[3] = Ptr
[3]; [[fallthrough
]];
296 case 3: OutBufCur
[2] = Ptr
[2]; [[fallthrough
]];
297 case 2: OutBufCur
[1] = Ptr
[1]; [[fallthrough
]];
298 case 1: OutBufCur
[0] = Ptr
[0]; [[fallthrough
]];
301 memcpy(OutBufCur
, Ptr
, Size
);
308 void raw_ostream::flush_tied_then_write(const char *Ptr
, size_t Size
) {
311 write_impl(Ptr
, Size
);
315 raw_ostream
&raw_ostream::operator<<(const format_object_base
&Fmt
) {
316 // If we have more than a few bytes left in our output buffer, try
317 // formatting directly onto its end.
318 size_t NextBufferSize
= 127;
319 size_t BufferBytesLeft
= OutBufEnd
- OutBufCur
;
320 if (BufferBytesLeft
> 3) {
321 size_t BytesUsed
= Fmt
.print(OutBufCur
, BufferBytesLeft
);
323 // Common case is that we have plenty of space.
324 if (BytesUsed
<= BufferBytesLeft
) {
325 OutBufCur
+= BytesUsed
;
329 // Otherwise, we overflowed and the return value tells us the size to try
331 NextBufferSize
= BytesUsed
;
334 // If we got here, we didn't have enough space in the output buffer for the
335 // string. Try printing into a SmallVector that is resized to have enough
336 // space. Iterate until we win.
337 SmallVector
<char, 128> V
;
340 V
.resize(NextBufferSize
);
342 // Try formatting into the SmallVector.
343 size_t BytesUsed
= Fmt
.print(V
.data(), NextBufferSize
);
345 // If BytesUsed fit into the vector, we win.
346 if (BytesUsed
<= NextBufferSize
)
347 return write(V
.data(), BytesUsed
);
349 // Otherwise, try again with a new size.
350 assert(BytesUsed
> NextBufferSize
&& "Didn't grow buffer!?");
351 NextBufferSize
= BytesUsed
;
355 raw_ostream
&raw_ostream::operator<<(const formatv_object_base
&Obj
) {
360 raw_ostream
&raw_ostream::operator<<(const FormattedString
&FS
) {
361 unsigned LeftIndent
= 0;
362 unsigned RightIndent
= 0;
363 const ssize_t Difference
= FS
.Width
- FS
.Str
.size();
364 if (Difference
> 0) {
365 switch (FS
.Justify
) {
366 case FormattedString::JustifyNone
:
368 case FormattedString::JustifyLeft
:
369 RightIndent
= Difference
;
371 case FormattedString::JustifyRight
:
372 LeftIndent
= Difference
;
374 case FormattedString::JustifyCenter
:
375 LeftIndent
= Difference
/ 2;
376 RightIndent
= Difference
- LeftIndent
;
386 raw_ostream
&raw_ostream::operator<<(const FormattedNumber
&FN
) {
389 if (FN
.Upper
&& FN
.HexPrefix
)
390 Style
= HexPrintStyle::PrefixUpper
;
391 else if (FN
.Upper
&& !FN
.HexPrefix
)
392 Style
= HexPrintStyle::Upper
;
393 else if (!FN
.Upper
&& FN
.HexPrefix
)
394 Style
= HexPrintStyle::PrefixLower
;
396 Style
= HexPrintStyle::Lower
;
397 llvm::write_hex(*this, FN
.HexValue
, Style
, FN
.Width
);
399 llvm::SmallString
<16> Buffer
;
400 llvm::raw_svector_ostream
Stream(Buffer
);
401 llvm::write_integer(Stream
, FN
.DecValue
, 0, IntegerStyle::Integer
);
402 if (Buffer
.size() < FN
.Width
)
403 indent(FN
.Width
- Buffer
.size());
409 raw_ostream
&raw_ostream::operator<<(const FormattedBytes
&FB
) {
410 if (FB
.Bytes
.empty())
413 size_t LineIndex
= 0;
414 auto Bytes
= FB
.Bytes
;
415 const size_t Size
= Bytes
.size();
416 HexPrintStyle HPS
= FB
.Upper
? HexPrintStyle::Upper
: HexPrintStyle::Lower
;
417 uint64_t OffsetWidth
= 0;
418 if (FB
.FirstByteOffset
) {
419 // Figure out how many nibbles are needed to print the largest offset
420 // represented by this data set, so that we can align the offset field
421 // to the right width.
422 size_t Lines
= Size
/ FB
.NumPerLine
;
423 uint64_t MaxOffset
= *FB
.FirstByteOffset
+ Lines
* FB
.NumPerLine
;
426 Power
= llvm::Log2_64_Ceil(MaxOffset
);
427 OffsetWidth
= std::max
<uint64_t>(4, llvm::alignTo(Power
, 4) / 4);
430 // The width of a block of data including all spaces for group separators.
431 unsigned NumByteGroups
=
432 alignTo(FB
.NumPerLine
, FB
.ByteGroupSize
) / FB
.ByteGroupSize
;
433 unsigned BlockCharWidth
= FB
.NumPerLine
* 2 + NumByteGroups
- 1;
435 while (!Bytes
.empty()) {
436 indent(FB
.IndentLevel
);
438 if (FB
.FirstByteOffset
) {
439 uint64_t Offset
= *FB
.FirstByteOffset
;
440 llvm::write_hex(*this, Offset
+ LineIndex
, HPS
, OffsetWidth
);
444 auto Line
= Bytes
.take_front(FB
.NumPerLine
);
446 size_t CharsPrinted
= 0;
447 // Print the hex bytes for this line in groups
448 for (size_t I
= 0; I
< Line
.size(); ++I
, CharsPrinted
+= 2) {
449 if (I
&& (I
% FB
.ByteGroupSize
) == 0) {
453 llvm::write_hex(*this, Line
[I
], HPS
, 2);
457 // Print any spaces needed for any bytes that we didn't print on this
458 // line so that the ASCII bytes are correctly aligned.
459 assert(BlockCharWidth
>= CharsPrinted
);
460 indent(BlockCharWidth
- CharsPrinted
+ 2);
463 // Print the ASCII char values for each byte on this line
464 for (uint8_t Byte
: Line
) {
466 *this << static_cast<char>(Byte
);
473 Bytes
= Bytes
.drop_front(Line
.size());
474 LineIndex
+= Line
.size();
475 if (LineIndex
< Size
)
482 static raw_ostream
&write_padding(raw_ostream
&OS
, unsigned NumChars
) {
483 static const char Chars
[] = {C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
,
484 C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
,
485 C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
,
486 C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
,
487 C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
};
489 // Usually the indentation is small, handle it with a fastpath.
490 if (NumChars
< std::size(Chars
))
491 return OS
.write(Chars
, NumChars
);
494 unsigned NumToWrite
= std::min(NumChars
, (unsigned)std::size(Chars
) - 1);
495 OS
.write(Chars
, NumToWrite
);
496 NumChars
-= NumToWrite
;
501 /// indent - Insert 'NumSpaces' spaces.
502 raw_ostream
&raw_ostream::indent(unsigned NumSpaces
) {
503 return write_padding
<' '>(*this, NumSpaces
);
506 /// write_zeros - Insert 'NumZeros' nulls.
507 raw_ostream
&raw_ostream::write_zeros(unsigned NumZeros
) {
508 return write_padding
<'\0'>(*this, NumZeros
);
511 bool raw_ostream::prepare_colors() {
512 // Colors were explicitly disabled.
516 // Colors require changing the terminal but this stream is not going to a
518 if (sys::Process::ColorNeedsFlush() && !is_displayed())
521 if (sys::Process::ColorNeedsFlush())
527 raw_ostream
&raw_ostream::changeColor(enum Colors colors
, bool bold
, bool bg
) {
528 if (!prepare_colors())
531 const char *colorcode
=
532 (colors
== SAVEDCOLOR
)
533 ? sys::Process::OutputBold(bg
)
534 : sys::Process::OutputColor(static_cast<char>(colors
), bold
, bg
);
536 write(colorcode
, strlen(colorcode
));
540 raw_ostream
&raw_ostream::resetColor() {
541 if (!prepare_colors())
544 if (const char *colorcode
= sys::Process::ResetColor())
545 write(colorcode
, strlen(colorcode
));
549 raw_ostream
&raw_ostream::reverseColor() {
550 if (!prepare_colors())
553 if (const char *colorcode
= sys::Process::OutputReverse())
554 write(colorcode
, strlen(colorcode
));
558 void raw_ostream::anchor() {}
560 //===----------------------------------------------------------------------===//
562 //===----------------------------------------------------------------------===//
564 // Out of line virtual method.
565 void format_object_base::home() {
568 //===----------------------------------------------------------------------===//
570 //===----------------------------------------------------------------------===//
572 static int getFD(StringRef Filename
, std::error_code
&EC
,
573 sys::fs::CreationDisposition Disp
, sys::fs::FileAccess Access
,
574 sys::fs::OpenFlags Flags
) {
575 assert((Access
& sys::fs::FA_Write
) &&
576 "Cannot make a raw_ostream from a read-only descriptor!");
578 // Handle "-" as stdout. Note that when we do this, we consider ourself
579 // the owner of stdout and may set the "binary" flag globally based on Flags.
580 if (Filename
== "-") {
581 EC
= std::error_code();
582 // Change stdout's text/binary mode based on the Flags.
583 sys::ChangeStdoutMode(Flags
);
584 return STDOUT_FILENO
;
588 if (Access
& sys::fs::FA_Read
)
589 EC
= sys::fs::openFileForReadWrite(Filename
, FD
, Disp
, Flags
);
591 EC
= sys::fs::openFileForWrite(Filename
, FD
, Disp
, Flags
);
598 raw_fd_ostream::raw_fd_ostream(StringRef Filename
, std::error_code
&EC
)
599 : raw_fd_ostream(Filename
, EC
, sys::fs::CD_CreateAlways
, sys::fs::FA_Write
,
602 raw_fd_ostream::raw_fd_ostream(StringRef Filename
, std::error_code
&EC
,
603 sys::fs::CreationDisposition Disp
)
604 : raw_fd_ostream(Filename
, EC
, Disp
, sys::fs::FA_Write
, sys::fs::OF_None
) {}
606 raw_fd_ostream::raw_fd_ostream(StringRef Filename
, std::error_code
&EC
,
607 sys::fs::FileAccess Access
)
608 : raw_fd_ostream(Filename
, EC
, sys::fs::CD_CreateAlways
, Access
,
611 raw_fd_ostream::raw_fd_ostream(StringRef Filename
, std::error_code
&EC
,
612 sys::fs::OpenFlags Flags
)
613 : raw_fd_ostream(Filename
, EC
, sys::fs::CD_CreateAlways
, sys::fs::FA_Write
,
616 raw_fd_ostream::raw_fd_ostream(StringRef Filename
, std::error_code
&EC
,
617 sys::fs::CreationDisposition Disp
,
618 sys::fs::FileAccess Access
,
619 sys::fs::OpenFlags Flags
)
620 : raw_fd_ostream(getFD(Filename
, EC
, Disp
, Access
, Flags
), true) {}
622 /// FD is the file descriptor that this writes to. If ShouldClose is true, this
623 /// closes the file when the stream is destroyed.
624 raw_fd_ostream::raw_fd_ostream(int fd
, bool shouldClose
, bool unbuffered
,
626 : raw_pwrite_stream(unbuffered
, K
), FD(fd
), ShouldClose(shouldClose
) {
634 // Do not attempt to close stdout or stderr. We used to try to maintain the
635 // property that tools that support writing file to stdout should not also
636 // write informational output to stdout, but in practice we were never able to
637 // maintain this invariant. Many features have been added to LLVM and clang
638 // (-fdump-record-layouts, optimization remarks, etc) that print to stdout, so
639 // users must simply be aware that mixed output and remarks is a possibility.
640 if (FD
<= STDERR_FILENO
)
644 // Check if this is a console device. This is not equivalent to isatty.
646 ::GetFileType((HANDLE
)::_get_osfhandle(fd
)) == FILE_TYPE_CHAR
;
649 // Get the starting position.
650 off_t loc
= ::lseek(FD
, 0, SEEK_CUR
);
651 sys::fs::file_status Status
;
652 std::error_code EC
= status(FD
, Status
);
653 IsRegularFile
= Status
.type() == sys::fs::file_type::regular_file
;
655 // MSVCRT's _lseek(SEEK_CUR) doesn't return -1 for pipes.
656 SupportsSeeking
= !EC
&& IsRegularFile
;
658 SupportsSeeking
= !EC
&& loc
!= (off_t
)-1;
660 if (!SupportsSeeking
)
663 pos
= static_cast<uint64_t>(loc
);
666 raw_fd_ostream::~raw_fd_ostream() {
670 if (auto EC
= sys::Process::SafelyCloseFileDescriptor(FD
))
676 // On mingw, global dtors should not call exit().
677 // report_fatal_error() invokes exit(). We know report_fatal_error()
678 // might not write messages to stderr when any errors were detected
683 // If there are any pending errors, report them now. Clients wishing
684 // to avoid report_fatal_error calls should check for errors with
685 // has_error() and clear the error flag with clear_error() before
686 // destructing raw_ostream objects which may have errors.
688 report_fatal_error(Twine("IO failure on output stream: ") +
690 /*gen_crash_diag=*/false);
694 // The most reliable way to print unicode in a Windows console is with
695 // WriteConsoleW. To use that, first transcode from UTF-8 to UTF-16. This
696 // assumes that LLVM programs always print valid UTF-8 to the console. The data
697 // might not be UTF-8 for two major reasons:
698 // 1. The program is printing binary (-filetype=obj -o -), in which case it
699 // would have been gibberish anyway.
700 // 2. The program is printing text in a semi-ascii compatible codepage like
701 // shift-jis or cp1252.
703 // Most LLVM programs don't produce non-ascii text unless they are quoting
704 // user source input. A well-behaved LLVM program should either validate that
705 // the input is UTF-8 or transcode from the local codepage to UTF-8 before
706 // quoting it. If they don't, this may mess up the encoding, but this is still
707 // probably the best compromise we can make.
708 static bool write_console_impl(int FD
, StringRef Data
) {
709 SmallVector
<wchar_t, 256> WideText
;
711 // Fall back to ::write if it wasn't valid UTF-8.
712 if (auto EC
= sys::windows::UTF8ToUTF16(Data
, WideText
))
715 // On Windows 7 and earlier, WriteConsoleW has a low maximum amount of data
716 // that can be written to the console at a time.
717 size_t MaxWriteSize
= WideText
.size();
718 if (!RunningWindows8OrGreater())
719 MaxWriteSize
= 32767;
721 size_t WCharsWritten
= 0;
723 size_t WCharsToWrite
=
724 std::min(MaxWriteSize
, WideText
.size() - WCharsWritten
);
725 DWORD ActuallyWritten
;
727 ::WriteConsoleW((HANDLE
)::_get_osfhandle(FD
), &WideText
[WCharsWritten
],
728 WCharsToWrite
, &ActuallyWritten
,
729 /*Reserved=*/nullptr);
731 // The most likely reason for WriteConsoleW to fail is that FD no longer
732 // points to a console. Fall back to ::write. If this isn't the first loop
733 // iteration, something is truly wrong.
737 WCharsWritten
+= ActuallyWritten
;
738 } while (WCharsWritten
!= WideText
.size());
743 void raw_fd_ostream::write_impl(const char *Ptr
, size_t Size
) {
744 assert(FD
>= 0 && "File already closed.");
748 // If this is a Windows console device, try re-encoding from UTF-8 to UTF-16
749 // and using WriteConsoleW. If that fails, fall back to plain write().
750 if (IsWindowsConsole
)
751 if (write_console_impl(FD
, StringRef(Ptr
, Size
)))
755 // The maximum write size is limited to INT32_MAX. A write
756 // greater than SSIZE_MAX is implementation-defined in POSIX,
757 // and Windows _write requires 32 bit input.
758 size_t MaxWriteSize
= INT32_MAX
;
760 #if defined(__linux__)
761 // It is observed that Linux returns EINVAL for a very large write (>2G).
762 // Make it a reasonably small value.
763 MaxWriteSize
= 1024 * 1024 * 1024;
767 size_t ChunkSize
= std::min(Size
, MaxWriteSize
);
768 ssize_t ret
= ::write(FD
, Ptr
, ChunkSize
);
771 // If it's a recoverable error, swallow it and retry the write.
773 // Ideally we wouldn't ever see EAGAIN or EWOULDBLOCK here, since
774 // raw_ostream isn't designed to do non-blocking I/O. However, some
775 // programs, such as old versions of bjam, have mistakenly used
776 // O_NONBLOCK. For compatibility, emulate blocking semantics by
777 // spinning until the write succeeds. If you don't want spinning,
778 // don't use O_NONBLOCK file descriptors with raw_ostream.
779 if (errno
== EINTR
|| errno
== EAGAIN
781 || errno
== EWOULDBLOCK
787 // Windows equivalents of SIGPIPE/EPIPE.
788 DWORD WinLastError
= GetLastError();
789 if (WinLastError
== ERROR_BROKEN_PIPE
||
790 (WinLastError
== ERROR_NO_DATA
&& errno
== EINVAL
)) {
791 llvm::sys::CallOneShotPipeSignalHandler();
795 // Otherwise it's a non-recoverable error. Note it and quit.
796 error_detected(std::error_code(errno
, std::generic_category()));
800 // The write may have written some or all of the data. Update the
801 // size and buffer pointer to reflect the remainder that needs
802 // to be written. If there are no bytes left, we're done.
808 void raw_fd_ostream::close() {
812 if (auto EC
= sys::Process::SafelyCloseFileDescriptor(FD
))
817 uint64_t raw_fd_ostream::seek(uint64_t off
) {
818 assert(SupportsSeeking
&& "Stream does not support seeking!");
821 pos
= ::_lseeki64(FD
, off
, SEEK_SET
);
823 pos
= ::lseek(FD
, off
, SEEK_SET
);
825 if (pos
== (uint64_t)-1)
826 error_detected(std::error_code(errno
, std::generic_category()));
830 void raw_fd_ostream::pwrite_impl(const char *Ptr
, size_t Size
,
832 uint64_t Pos
= tell();
838 size_t raw_fd_ostream::preferred_buffer_size() const {
840 // Disable buffering for console devices. Console output is re-encoded from
841 // UTF-8 to UTF-16 on Windows, and buffering it would require us to split the
842 // buffer on a valid UTF-8 codepoint boundary. Terminal buffering is disabled
843 // below on most other OSs, so do the same thing on Windows and avoid that
845 if (IsWindowsConsole
)
847 return raw_ostream::preferred_buffer_size();
849 assert(FD
>= 0 && "File not yet open!");
851 if (fstat(FD
, &statbuf
) != 0)
854 // If this is a terminal, don't use buffering. Line buffering
855 // would be a more traditional thing to do, but it's not worth
857 if (S_ISCHR(statbuf
.st_mode
) && is_displayed())
859 // Return the preferred block size.
860 return statbuf
.st_blksize
;
864 bool raw_fd_ostream::is_displayed() const {
865 return sys::Process::FileDescriptorIsDisplayed(FD
);
868 bool raw_fd_ostream::has_colors() const {
870 HasColors
= sys::Process::FileDescriptorHasColors(FD
);
874 Expected
<sys::fs::FileLocker
> raw_fd_ostream::lock() {
875 std::error_code EC
= sys::fs::lockFile(FD
);
877 return sys::fs::FileLocker(FD
);
878 return errorCodeToError(EC
);
881 Expected
<sys::fs::FileLocker
>
882 raw_fd_ostream::tryLockFor(Duration
const& Timeout
) {
883 std::error_code EC
= sys::fs::tryLockFile(FD
, Timeout
.getDuration());
885 return sys::fs::FileLocker(FD
);
886 return errorCodeToError(EC
);
889 void raw_fd_ostream::anchor() {}
891 //===----------------------------------------------------------------------===//
892 // outs(), errs(), nulls()
893 //===----------------------------------------------------------------------===//
895 raw_fd_ostream
&llvm::outs() {
896 // Set buffer settings to model stdout behavior.
898 static raw_fd_ostream
S("-", EC
, sys::fs::OF_None
);
903 raw_fd_ostream
&llvm::errs() {
904 // Set standard error to be unbuffered and tied to outs() by default.
905 static raw_fd_ostream
S(STDERR_FILENO
, false, true);
909 /// nulls() - This returns a reference to a raw_ostream which discards output.
910 raw_ostream
&llvm::nulls() {
911 static raw_null_ostream S
;
915 //===----------------------------------------------------------------------===//
917 //===----------------------------------------------------------------------===//
919 raw_fd_stream::raw_fd_stream(StringRef Filename
, std::error_code
&EC
)
920 : raw_fd_ostream(getFD(Filename
, EC
, sys::fs::CD_CreateAlways
,
921 sys::fs::FA_Write
| sys::fs::FA_Read
,
923 true, false, OStreamKind::OK_FDStream
) {
927 if (!isRegularFile())
928 EC
= std::make_error_code(std::errc::invalid_argument
);
931 ssize_t
raw_fd_stream::read(char *Ptr
, size_t Size
) {
932 assert(get_fd() >= 0 && "File already closed.");
933 ssize_t Ret
= ::read(get_fd(), (void *)Ptr
, Size
);
937 error_detected(std::error_code(errno
, std::generic_category()));
941 bool raw_fd_stream::classof(const raw_ostream
*OS
) {
942 return OS
->get_kind() == OStreamKind::OK_FDStream
;
945 //===----------------------------------------------------------------------===//
946 // raw_string_ostream
947 //===----------------------------------------------------------------------===//
949 void raw_string_ostream::write_impl(const char *Ptr
, size_t Size
) {
950 OS
.append(Ptr
, Size
);
953 //===----------------------------------------------------------------------===//
954 // raw_svector_ostream
955 //===----------------------------------------------------------------------===//
957 uint64_t raw_svector_ostream::current_pos() const { return OS
.size(); }
959 void raw_svector_ostream::write_impl(const char *Ptr
, size_t Size
) {
960 OS
.append(Ptr
, Ptr
+ Size
);
963 void raw_svector_ostream::pwrite_impl(const char *Ptr
, size_t Size
,
965 memcpy(OS
.data() + Offset
, Ptr
, Size
);
968 //===----------------------------------------------------------------------===//
970 //===----------------------------------------------------------------------===//
972 raw_null_ostream::~raw_null_ostream() {
974 // ~raw_ostream asserts that the buffer is empty. This isn't necessary
975 // with raw_null_ostream, but it's better to have raw_null_ostream follow
976 // the rules than to change the rules just for raw_null_ostream.
981 void raw_null_ostream::write_impl(const char *Ptr
, size_t Size
) {
984 uint64_t raw_null_ostream::current_pos() const {
988 void raw_null_ostream::pwrite_impl(const char *Ptr
, size_t Size
,
991 void raw_pwrite_stream::anchor() {}
993 void buffer_ostream::anchor() {}
995 void buffer_unique_ostream::anchor() {}
997 Error
llvm::writeToOutput(StringRef OutputFileName
,
998 std::function
<Error(raw_ostream
&)> Write
) {
999 if (OutputFileName
== "-")
1000 return Write(outs());
1002 if (OutputFileName
== "/dev/null") {
1003 raw_null_ostream Out
;
1007 unsigned Mode
= sys::fs::all_read
| sys::fs::all_write
;
1008 Expected
<sys::fs::TempFile
> Temp
=
1009 sys::fs::TempFile::create(OutputFileName
+ ".temp-stream-%%%%%%", Mode
);
1011 return createFileError(OutputFileName
, Temp
.takeError());
1013 raw_fd_ostream
Out(Temp
->FD
, false);
1015 if (Error E
= Write(Out
)) {
1016 if (Error DiscardError
= Temp
->discard())
1017 return joinErrors(std::move(E
), std::move(DiscardError
));
1022 return Temp
->keep(OutputFileName
);