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/AutoConvert.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/Duration.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/FormatVariadic.h"
23 #include "llvm/Support/MathExtras.h"
24 #include "llvm/Support/NativeFormatting.h"
25 #include "llvm/Support/Process.h"
26 #include "llvm/Support/Program.h"
32 // <fcntl.h> may provide O_BINARY.
33 #if defined(HAVE_FCNTL_H)
37 #if defined(HAVE_UNISTD_H)
41 #if defined(__CYGWIN__)
48 # define STDIN_FILENO 0
51 # define STDOUT_FILENO 1
54 # define STDERR_FILENO 2
59 #include "llvm/Support/ConvertUTF.h"
60 #include "llvm/Support/Signals.h"
61 #include "llvm/Support/Windows/WindowsSupport.h"
66 constexpr raw_ostream::Colors
raw_ostream::BLACK
;
67 constexpr raw_ostream::Colors
raw_ostream::RED
;
68 constexpr raw_ostream::Colors
raw_ostream::GREEN
;
69 constexpr raw_ostream::Colors
raw_ostream::YELLOW
;
70 constexpr raw_ostream::Colors
raw_ostream::BLUE
;
71 constexpr raw_ostream::Colors
raw_ostream::MAGENTA
;
72 constexpr raw_ostream::Colors
raw_ostream::CYAN
;
73 constexpr raw_ostream::Colors
raw_ostream::WHITE
;
74 constexpr raw_ostream::Colors
raw_ostream::SAVEDCOLOR
;
75 constexpr raw_ostream::Colors
raw_ostream::RESET
;
77 raw_ostream::~raw_ostream() {
78 // raw_ostream's subclasses should take care to flush the buffer
79 // in their destructors.
80 assert(OutBufCur
== OutBufStart
&&
81 "raw_ostream destructor called with non-empty buffer!");
83 if (BufferMode
== BufferKind::InternalBuffer
)
84 delete [] OutBufStart
;
87 size_t raw_ostream::preferred_buffer_size() const {
89 // On Windows BUFSIZ is only 512 which results in more calls to write. This
90 // overhead can cause significant performance degradation. Therefore use a
94 // BUFSIZ is intended to be a reasonable default.
99 void raw_ostream::SetBuffered() {
100 // Ask the subclass to determine an appropriate buffer size.
101 if (size_t Size
= preferred_buffer_size())
104 // It may return 0, meaning this stream should be unbuffered.
108 void raw_ostream::SetBufferAndMode(char *BufferStart
, size_t Size
,
110 assert(((Mode
== BufferKind::Unbuffered
&& !BufferStart
&& Size
== 0) ||
111 (Mode
!= BufferKind::Unbuffered
&& BufferStart
&& Size
!= 0)) &&
112 "stream must be unbuffered or have at least one byte");
113 // Make sure the current buffer is free of content (we can't flush here; the
114 // child buffer management logic will be in write_impl).
115 assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!");
117 if (BufferMode
== BufferKind::InternalBuffer
)
118 delete [] OutBufStart
;
119 OutBufStart
= BufferStart
;
120 OutBufEnd
= OutBufStart
+Size
;
121 OutBufCur
= OutBufStart
;
124 assert(OutBufStart
<= OutBufEnd
&& "Invalid size!");
127 raw_ostream
&raw_ostream::operator<<(unsigned long N
) {
128 write_integer(*this, static_cast<uint64_t>(N
), 0, IntegerStyle::Integer
);
132 raw_ostream
&raw_ostream::operator<<(long N
) {
133 write_integer(*this, static_cast<int64_t>(N
), 0, IntegerStyle::Integer
);
137 raw_ostream
&raw_ostream::operator<<(unsigned long long N
) {
138 write_integer(*this, static_cast<uint64_t>(N
), 0, IntegerStyle::Integer
);
142 raw_ostream
&raw_ostream::operator<<(long long N
) {
143 write_integer(*this, static_cast<int64_t>(N
), 0, IntegerStyle::Integer
);
147 raw_ostream
&raw_ostream::write_hex(unsigned long long N
) {
148 llvm::write_hex(*this, N
, HexPrintStyle::Lower
);
152 raw_ostream
&raw_ostream::operator<<(Colors C
) {
153 if (C
== Colors::RESET
)
160 raw_ostream
&raw_ostream::write_uuid(const uuid_t UUID
) {
161 for (int Idx
= 0; Idx
< 16; ++Idx
) {
162 *this << format("%02" PRIX32
, UUID
[Idx
]);
163 if (Idx
== 3 || Idx
== 5 || Idx
== 7 || Idx
== 9)
170 raw_ostream
&raw_ostream::write_escaped(StringRef Str
,
171 bool UseHexEscapes
) {
172 for (unsigned char c
: Str
) {
175 *this << '\\' << '\\';
178 *this << '\\' << 't';
181 *this << '\\' << 'n';
184 *this << '\\' << '"';
192 // Write out the escaped representation.
194 *this << '\\' << 'x';
195 *this << hexdigit((c
>> 4) & 0xF);
196 *this << hexdigit((c
>> 0) & 0xF);
198 // Always use a full 3-character octal escape.
200 *this << char('0' + ((c
>> 6) & 7));
201 *this << char('0' + ((c
>> 3) & 7));
202 *this << char('0' + ((c
>> 0) & 7));
210 raw_ostream
&raw_ostream::operator<<(const void *P
) {
211 llvm::write_hex(*this, (uintptr_t)P
, HexPrintStyle::PrefixLower
);
215 raw_ostream
&raw_ostream::operator<<(double N
) {
216 llvm::write_double(*this, N
, FloatStyle::Exponent
);
220 void raw_ostream::flush_nonempty() {
221 assert(OutBufCur
> OutBufStart
&& "Invalid call to flush_nonempty.");
222 size_t Length
= OutBufCur
- OutBufStart
;
223 OutBufCur
= OutBufStart
;
224 flush_tied_then_write(OutBufStart
, Length
);
227 raw_ostream
&raw_ostream::write(unsigned char C
) {
228 // Group exceptional cases into a single branch.
229 if (LLVM_UNLIKELY(OutBufCur
>= OutBufEnd
)) {
230 if (LLVM_UNLIKELY(!OutBufStart
)) {
231 if (BufferMode
== BufferKind::Unbuffered
) {
232 flush_tied_then_write(reinterpret_cast<char *>(&C
), 1);
235 // Set up a buffer and start over.
247 raw_ostream
&raw_ostream::write(const char *Ptr
, size_t Size
) {
248 // Group exceptional cases into a single branch.
249 if (LLVM_UNLIKELY(size_t(OutBufEnd
- OutBufCur
) < Size
)) {
250 if (LLVM_UNLIKELY(!OutBufStart
)) {
251 if (BufferMode
== BufferKind::Unbuffered
) {
252 flush_tied_then_write(Ptr
, Size
);
255 // Set up a buffer and start over.
257 return write(Ptr
, Size
);
260 size_t NumBytes
= OutBufEnd
- OutBufCur
;
262 // If the buffer is empty at this point we have a string that is larger
263 // than the buffer. Directly write the chunk that is a multiple of the
264 // preferred buffer size and put the remainder in the buffer.
265 if (LLVM_UNLIKELY(OutBufCur
== OutBufStart
)) {
266 assert(NumBytes
!= 0 && "undefined behavior");
267 size_t BytesToWrite
= Size
- (Size
% NumBytes
);
268 flush_tied_then_write(Ptr
, BytesToWrite
);
269 size_t BytesRemaining
= Size
- BytesToWrite
;
270 if (BytesRemaining
> size_t(OutBufEnd
- OutBufCur
)) {
271 // Too much left over to copy into our buffer.
272 return write(Ptr
+ BytesToWrite
, BytesRemaining
);
274 copy_to_buffer(Ptr
+ BytesToWrite
, BytesRemaining
);
278 // We don't have enough space in the buffer to fit the string in. Insert as
279 // much as possible, flush and start over with the remainder.
280 copy_to_buffer(Ptr
, NumBytes
);
282 return write(Ptr
+ NumBytes
, Size
- NumBytes
);
285 copy_to_buffer(Ptr
, Size
);
290 void raw_ostream::copy_to_buffer(const char *Ptr
, size_t Size
) {
291 assert(Size
<= size_t(OutBufEnd
- OutBufCur
) && "Buffer overrun!");
293 // Handle short strings specially, memcpy isn't very good at very short
296 case 4: OutBufCur
[3] = Ptr
[3]; [[fallthrough
]];
297 case 3: OutBufCur
[2] = Ptr
[2]; [[fallthrough
]];
298 case 2: OutBufCur
[1] = Ptr
[1]; [[fallthrough
]];
299 case 1: OutBufCur
[0] = Ptr
[0]; [[fallthrough
]];
302 memcpy(OutBufCur
, Ptr
, Size
);
309 void raw_ostream::flush_tied_then_write(const char *Ptr
, size_t Size
) {
312 write_impl(Ptr
, Size
);
316 raw_ostream
&raw_ostream::operator<<(const format_object_base
&Fmt
) {
317 // If we have more than a few bytes left in our output buffer, try
318 // formatting directly onto its end.
319 size_t NextBufferSize
= 127;
320 size_t BufferBytesLeft
= OutBufEnd
- OutBufCur
;
321 if (BufferBytesLeft
> 3) {
322 size_t BytesUsed
= Fmt
.print(OutBufCur
, BufferBytesLeft
);
324 // Common case is that we have plenty of space.
325 if (BytesUsed
<= BufferBytesLeft
) {
326 OutBufCur
+= BytesUsed
;
330 // Otherwise, we overflowed and the return value tells us the size to try
332 NextBufferSize
= BytesUsed
;
335 // If we got here, we didn't have enough space in the output buffer for the
336 // string. Try printing into a SmallVector that is resized to have enough
337 // space. Iterate until we win.
338 SmallVector
<char, 128> V
;
341 V
.resize(NextBufferSize
);
343 // Try formatting into the SmallVector.
344 size_t BytesUsed
= Fmt
.print(V
.data(), NextBufferSize
);
346 // If BytesUsed fit into the vector, we win.
347 if (BytesUsed
<= NextBufferSize
)
348 return write(V
.data(), BytesUsed
);
350 // Otherwise, try again with a new size.
351 assert(BytesUsed
> NextBufferSize
&& "Didn't grow buffer!?");
352 NextBufferSize
= BytesUsed
;
356 raw_ostream
&raw_ostream::operator<<(const formatv_object_base
&Obj
) {
361 raw_ostream
&raw_ostream::operator<<(const FormattedString
&FS
) {
362 unsigned LeftIndent
= 0;
363 unsigned RightIndent
= 0;
364 const ssize_t Difference
= FS
.Width
- FS
.Str
.size();
365 if (Difference
> 0) {
366 switch (FS
.Justify
) {
367 case FormattedString::JustifyNone
:
369 case FormattedString::JustifyLeft
:
370 RightIndent
= Difference
;
372 case FormattedString::JustifyRight
:
373 LeftIndent
= Difference
;
375 case FormattedString::JustifyCenter
:
376 LeftIndent
= Difference
/ 2;
377 RightIndent
= Difference
- LeftIndent
;
387 raw_ostream
&raw_ostream::operator<<(const FormattedNumber
&FN
) {
390 if (FN
.Upper
&& FN
.HexPrefix
)
391 Style
= HexPrintStyle::PrefixUpper
;
392 else if (FN
.Upper
&& !FN
.HexPrefix
)
393 Style
= HexPrintStyle::Upper
;
394 else if (!FN
.Upper
&& FN
.HexPrefix
)
395 Style
= HexPrintStyle::PrefixLower
;
397 Style
= HexPrintStyle::Lower
;
398 llvm::write_hex(*this, FN
.HexValue
, Style
, FN
.Width
);
400 llvm::SmallString
<16> Buffer
;
401 llvm::raw_svector_ostream
Stream(Buffer
);
402 llvm::write_integer(Stream
, FN
.DecValue
, 0, IntegerStyle::Integer
);
403 if (Buffer
.size() < FN
.Width
)
404 indent(FN
.Width
- Buffer
.size());
410 raw_ostream
&raw_ostream::operator<<(const FormattedBytes
&FB
) {
411 if (FB
.Bytes
.empty())
414 size_t LineIndex
= 0;
415 auto Bytes
= FB
.Bytes
;
416 const size_t Size
= Bytes
.size();
417 HexPrintStyle HPS
= FB
.Upper
? HexPrintStyle::Upper
: HexPrintStyle::Lower
;
418 uint64_t OffsetWidth
= 0;
419 if (FB
.FirstByteOffset
) {
420 // Figure out how many nibbles are needed to print the largest offset
421 // represented by this data set, so that we can align the offset field
422 // to the right width.
423 size_t Lines
= Size
/ FB
.NumPerLine
;
424 uint64_t MaxOffset
= *FB
.FirstByteOffset
+ Lines
* FB
.NumPerLine
;
427 Power
= llvm::Log2_64_Ceil(MaxOffset
);
428 OffsetWidth
= std::max
<uint64_t>(4, llvm::alignTo(Power
, 4) / 4);
431 // The width of a block of data including all spaces for group separators.
432 unsigned NumByteGroups
=
433 alignTo(FB
.NumPerLine
, FB
.ByteGroupSize
) / FB
.ByteGroupSize
;
434 unsigned BlockCharWidth
= FB
.NumPerLine
* 2 + NumByteGroups
- 1;
436 while (!Bytes
.empty()) {
437 indent(FB
.IndentLevel
);
439 if (FB
.FirstByteOffset
) {
440 uint64_t Offset
= *FB
.FirstByteOffset
;
441 llvm::write_hex(*this, Offset
+ LineIndex
, HPS
, OffsetWidth
);
445 auto Line
= Bytes
.take_front(FB
.NumPerLine
);
447 size_t CharsPrinted
= 0;
448 // Print the hex bytes for this line in groups
449 for (size_t I
= 0; I
< Line
.size(); ++I
, CharsPrinted
+= 2) {
450 if (I
&& (I
% FB
.ByteGroupSize
) == 0) {
454 llvm::write_hex(*this, Line
[I
], HPS
, 2);
458 // Print any spaces needed for any bytes that we didn't print on this
459 // line so that the ASCII bytes are correctly aligned.
460 assert(BlockCharWidth
>= CharsPrinted
);
461 indent(BlockCharWidth
- CharsPrinted
+ 2);
464 // Print the ASCII char values for each byte on this line
465 for (uint8_t Byte
: Line
) {
467 *this << static_cast<char>(Byte
);
474 Bytes
= Bytes
.drop_front(Line
.size());
475 LineIndex
+= Line
.size();
476 if (LineIndex
< Size
)
483 static raw_ostream
&write_padding(raw_ostream
&OS
, unsigned NumChars
) {
484 static const char Chars
[] = {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
,
488 C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
, C
};
490 // Usually the indentation is small, handle it with a fastpath.
491 if (NumChars
< std::size(Chars
))
492 return OS
.write(Chars
, NumChars
);
495 unsigned NumToWrite
= std::min(NumChars
, (unsigned)std::size(Chars
) - 1);
496 OS
.write(Chars
, NumToWrite
);
497 NumChars
-= NumToWrite
;
502 /// indent - Insert 'NumSpaces' spaces.
503 raw_ostream
&raw_ostream::indent(unsigned NumSpaces
) {
504 return write_padding
<' '>(*this, NumSpaces
);
507 /// write_zeros - Insert 'NumZeros' nulls.
508 raw_ostream
&raw_ostream::write_zeros(unsigned NumZeros
) {
509 return write_padding
<'\0'>(*this, NumZeros
);
512 bool raw_ostream::prepare_colors() {
513 // Colors were explicitly disabled.
517 // Colors require changing the terminal but this stream is not going to a
519 if (sys::Process::ColorNeedsFlush() && !is_displayed())
522 if (sys::Process::ColorNeedsFlush())
528 raw_ostream
&raw_ostream::changeColor(enum Colors colors
, bool bold
, bool bg
) {
529 if (!prepare_colors())
532 const char *colorcode
=
533 (colors
== SAVEDCOLOR
)
534 ? sys::Process::OutputBold(bg
)
535 : sys::Process::OutputColor(static_cast<char>(colors
), bold
, bg
);
537 write(colorcode
, strlen(colorcode
));
541 raw_ostream
&raw_ostream::resetColor() {
542 if (!prepare_colors())
545 if (const char *colorcode
= sys::Process::ResetColor())
546 write(colorcode
, strlen(colorcode
));
550 raw_ostream
&raw_ostream::reverseColor() {
551 if (!prepare_colors())
554 if (const char *colorcode
= sys::Process::OutputReverse())
555 write(colorcode
, strlen(colorcode
));
559 void raw_ostream::anchor() {}
561 //===----------------------------------------------------------------------===//
563 //===----------------------------------------------------------------------===//
565 // Out of line virtual method.
566 void format_object_base::home() {
569 //===----------------------------------------------------------------------===//
571 //===----------------------------------------------------------------------===//
573 static int getFD(StringRef Filename
, std::error_code
&EC
,
574 sys::fs::CreationDisposition Disp
, sys::fs::FileAccess Access
,
575 sys::fs::OpenFlags Flags
) {
576 assert((Access
& sys::fs::FA_Write
) &&
577 "Cannot make a raw_ostream from a read-only descriptor!");
579 // Handle "-" as stdout. Note that when we do this, we consider ourself
580 // the owner of stdout and may set the "binary" flag globally based on Flags.
581 if (Filename
== "-") {
582 EC
= std::error_code();
583 // Change stdout's text/binary mode based on the Flags.
584 sys::ChangeStdoutMode(Flags
);
585 return STDOUT_FILENO
;
589 if (Access
& sys::fs::FA_Read
)
590 EC
= sys::fs::openFileForReadWrite(Filename
, FD
, Disp
, Flags
);
592 EC
= sys::fs::openFileForWrite(Filename
, FD
, Disp
, Flags
);
599 raw_fd_ostream::raw_fd_ostream(StringRef Filename
, std::error_code
&EC
)
600 : raw_fd_ostream(Filename
, EC
, sys::fs::CD_CreateAlways
, sys::fs::FA_Write
,
603 raw_fd_ostream::raw_fd_ostream(StringRef Filename
, std::error_code
&EC
,
604 sys::fs::CreationDisposition Disp
)
605 : raw_fd_ostream(Filename
, EC
, Disp
, sys::fs::FA_Write
, sys::fs::OF_None
) {}
607 raw_fd_ostream::raw_fd_ostream(StringRef Filename
, std::error_code
&EC
,
608 sys::fs::FileAccess Access
)
609 : raw_fd_ostream(Filename
, EC
, sys::fs::CD_CreateAlways
, Access
,
612 raw_fd_ostream::raw_fd_ostream(StringRef Filename
, std::error_code
&EC
,
613 sys::fs::OpenFlags Flags
)
614 : raw_fd_ostream(Filename
, EC
, sys::fs::CD_CreateAlways
, sys::fs::FA_Write
,
617 raw_fd_ostream::raw_fd_ostream(StringRef Filename
, std::error_code
&EC
,
618 sys::fs::CreationDisposition Disp
,
619 sys::fs::FileAccess Access
,
620 sys::fs::OpenFlags Flags
)
621 : raw_fd_ostream(getFD(Filename
, EC
, Disp
, Access
, Flags
), true) {}
623 /// FD is the file descriptor that this writes to. If ShouldClose is true, this
624 /// closes the file when the stream is destroyed.
625 raw_fd_ostream::raw_fd_ostream(int fd
, bool shouldClose
, bool unbuffered
,
627 : raw_pwrite_stream(unbuffered
, K
), FD(fd
), ShouldClose(shouldClose
) {
635 // Do not attempt to close stdout or stderr. We used to try to maintain the
636 // property that tools that support writing file to stdout should not also
637 // write informational output to stdout, but in practice we were never able to
638 // maintain this invariant. Many features have been added to LLVM and clang
639 // (-fdump-record-layouts, optimization remarks, etc) that print to stdout, so
640 // users must simply be aware that mixed output and remarks is a possibility.
641 if (FD
<= STDERR_FILENO
)
645 // Check if this is a console device. This is not equivalent to isatty.
647 ::GetFileType((HANDLE
)::_get_osfhandle(fd
)) == FILE_TYPE_CHAR
;
650 // Get the starting position.
651 off_t loc
= ::lseek(FD
, 0, SEEK_CUR
);
652 sys::fs::file_status Status
;
653 std::error_code EC
= status(FD
, Status
);
654 IsRegularFile
= Status
.type() == sys::fs::file_type::regular_file
;
656 // MSVCRT's _lseek(SEEK_CUR) doesn't return -1 for pipes.
657 SupportsSeeking
= !EC
&& IsRegularFile
;
659 SupportsSeeking
= !EC
&& loc
!= (off_t
)-1;
661 if (!SupportsSeeking
)
664 pos
= static_cast<uint64_t>(loc
);
667 raw_fd_ostream::~raw_fd_ostream() {
671 if (auto EC
= sys::Process::SafelyCloseFileDescriptor(FD
))
677 // On mingw, global dtors should not call exit().
678 // report_fatal_error() invokes exit(). We know report_fatal_error()
679 // might not write messages to stderr when any errors were detected
684 // If there are any pending errors, report them now. Clients wishing
685 // to avoid report_fatal_error calls should check for errors with
686 // has_error() and clear the error flag with clear_error() before
687 // destructing raw_ostream objects which may have errors.
689 report_fatal_error(Twine("IO failure on output stream: ") +
691 /*gen_crash_diag=*/false);
695 // The most reliable way to print unicode in a Windows console is with
696 // WriteConsoleW. To use that, first transcode from UTF-8 to UTF-16. This
697 // assumes that LLVM programs always print valid UTF-8 to the console. The data
698 // might not be UTF-8 for two major reasons:
699 // 1. The program is printing binary (-filetype=obj -o -), in which case it
700 // would have been gibberish anyway.
701 // 2. The program is printing text in a semi-ascii compatible codepage like
702 // shift-jis or cp1252.
704 // Most LLVM programs don't produce non-ascii text unless they are quoting
705 // user source input. A well-behaved LLVM program should either validate that
706 // the input is UTF-8 or transcode from the local codepage to UTF-8 before
707 // quoting it. If they don't, this may mess up the encoding, but this is still
708 // probably the best compromise we can make.
709 static bool write_console_impl(int FD
, StringRef Data
) {
710 SmallVector
<wchar_t, 256> WideText
;
712 // Fall back to ::write if it wasn't valid UTF-8.
713 if (auto EC
= sys::windows::UTF8ToUTF16(Data
, WideText
))
716 // On Windows 7 and earlier, WriteConsoleW has a low maximum amount of data
717 // that can be written to the console at a time.
718 size_t MaxWriteSize
= WideText
.size();
719 if (!RunningWindows8OrGreater())
720 MaxWriteSize
= 32767;
722 size_t WCharsWritten
= 0;
724 size_t WCharsToWrite
=
725 std::min(MaxWriteSize
, WideText
.size() - WCharsWritten
);
726 DWORD ActuallyWritten
;
728 ::WriteConsoleW((HANDLE
)::_get_osfhandle(FD
), &WideText
[WCharsWritten
],
729 WCharsToWrite
, &ActuallyWritten
,
730 /*Reserved=*/nullptr);
732 // The most likely reason for WriteConsoleW to fail is that FD no longer
733 // points to a console. Fall back to ::write. If this isn't the first loop
734 // iteration, something is truly wrong.
738 WCharsWritten
+= ActuallyWritten
;
739 } while (WCharsWritten
!= WideText
.size());
744 void raw_fd_ostream::write_impl(const char *Ptr
, size_t Size
) {
745 assert(FD
>= 0 && "File already closed.");
749 // If this is a Windows console device, try re-encoding from UTF-8 to UTF-16
750 // and using WriteConsoleW. If that fails, fall back to plain write().
751 if (IsWindowsConsole
)
752 if (write_console_impl(FD
, StringRef(Ptr
, Size
)))
756 // The maximum write size is limited to INT32_MAX. A write
757 // greater than SSIZE_MAX is implementation-defined in POSIX,
758 // and Windows _write requires 32 bit input.
759 size_t MaxWriteSize
= INT32_MAX
;
761 #if defined(__linux__)
762 // It is observed that Linux returns EINVAL for a very large write (>2G).
763 // Make it a reasonably small value.
764 MaxWriteSize
= 1024 * 1024 * 1024;
768 size_t ChunkSize
= std::min(Size
, MaxWriteSize
);
769 ssize_t ret
= ::write(FD
, Ptr
, ChunkSize
);
772 // If it's a recoverable error, swallow it and retry the write.
774 // Ideally we wouldn't ever see EAGAIN or EWOULDBLOCK here, since
775 // raw_ostream isn't designed to do non-blocking I/O. However, some
776 // programs, such as old versions of bjam, have mistakenly used
777 // O_NONBLOCK. For compatibility, emulate blocking semantics by
778 // spinning until the write succeeds. If you don't want spinning,
779 // don't use O_NONBLOCK file descriptors with raw_ostream.
780 if (errno
== EINTR
|| errno
== EAGAIN
782 || errno
== EWOULDBLOCK
788 // Windows equivalents of SIGPIPE/EPIPE.
789 DWORD WinLastError
= GetLastError();
790 if (WinLastError
== ERROR_BROKEN_PIPE
||
791 (WinLastError
== ERROR_NO_DATA
&& errno
== EINVAL
)) {
792 llvm::sys::CallOneShotPipeSignalHandler();
796 // Otherwise it's a non-recoverable error. Note it and quit.
797 error_detected(std::error_code(errno
, std::generic_category()));
801 // The write may have written some or all of the data. Update the
802 // size and buffer pointer to reflect the remainder that needs
803 // to be written. If there are no bytes left, we're done.
809 void raw_fd_ostream::close() {
813 if (auto EC
= sys::Process::SafelyCloseFileDescriptor(FD
))
818 uint64_t raw_fd_ostream::seek(uint64_t off
) {
819 assert(SupportsSeeking
&& "Stream does not support seeking!");
822 pos
= ::_lseeki64(FD
, off
, SEEK_SET
);
824 pos
= ::lseek(FD
, off
, SEEK_SET
);
826 if (pos
== (uint64_t)-1)
827 error_detected(std::error_code(errno
, std::generic_category()));
831 void raw_fd_ostream::pwrite_impl(const char *Ptr
, size_t Size
,
833 uint64_t Pos
= tell();
839 size_t raw_fd_ostream::preferred_buffer_size() const {
841 // Disable buffering for console devices. Console output is re-encoded from
842 // UTF-8 to UTF-16 on Windows, and buffering it would require us to split the
843 // buffer on a valid UTF-8 codepoint boundary. Terminal buffering is disabled
844 // below on most other OSs, so do the same thing on Windows and avoid that
846 if (IsWindowsConsole
)
848 return raw_ostream::preferred_buffer_size();
850 assert(FD
>= 0 && "File not yet open!");
852 if (fstat(FD
, &statbuf
) != 0)
855 // If this is a terminal, don't use buffering. Line buffering
856 // would be a more traditional thing to do, but it's not worth
858 if (S_ISCHR(statbuf
.st_mode
) && is_displayed())
860 // Return the preferred block size.
861 return statbuf
.st_blksize
;
865 bool raw_fd_ostream::is_displayed() const {
866 return sys::Process::FileDescriptorIsDisplayed(FD
);
869 bool raw_fd_ostream::has_colors() const {
871 HasColors
= sys::Process::FileDescriptorHasColors(FD
);
875 Expected
<sys::fs::FileLocker
> raw_fd_ostream::lock() {
876 std::error_code EC
= sys::fs::lockFile(FD
);
878 return sys::fs::FileLocker(FD
);
879 return errorCodeToError(EC
);
882 Expected
<sys::fs::FileLocker
>
883 raw_fd_ostream::tryLockFor(Duration
const& Timeout
) {
884 std::error_code EC
= sys::fs::tryLockFile(FD
, Timeout
.getDuration());
886 return sys::fs::FileLocker(FD
);
887 return errorCodeToError(EC
);
890 void raw_fd_ostream::anchor() {}
892 //===----------------------------------------------------------------------===//
893 // outs(), errs(), nulls()
894 //===----------------------------------------------------------------------===//
896 raw_fd_ostream
&llvm::outs() {
897 // Set buffer settings to model stdout behavior.
900 EC
= enableAutoConversion(STDOUT_FILENO
);
903 static raw_fd_ostream
S("-", EC
, sys::fs::OF_None
);
908 raw_fd_ostream
&llvm::errs() {
909 // Set standard error to be unbuffered and tied to outs() by default.
911 std::error_code EC
= enableAutoConversion(STDERR_FILENO
);
914 static raw_fd_ostream
S(STDERR_FILENO
, false, true);
918 /// nulls() - This returns a reference to a raw_ostream which discards output.
919 raw_ostream
&llvm::nulls() {
920 static raw_null_ostream S
;
924 //===----------------------------------------------------------------------===//
926 //===----------------------------------------------------------------------===//
928 raw_fd_stream::raw_fd_stream(StringRef Filename
, std::error_code
&EC
)
929 : raw_fd_ostream(getFD(Filename
, EC
, sys::fs::CD_CreateAlways
,
930 sys::fs::FA_Write
| sys::fs::FA_Read
,
932 true, false, OStreamKind::OK_FDStream
) {
936 if (!isRegularFile())
937 EC
= std::make_error_code(std::errc::invalid_argument
);
940 raw_fd_stream::raw_fd_stream(int fd
, bool shouldClose
)
941 : raw_fd_ostream(fd
, shouldClose
, false, OStreamKind::OK_FDStream
) {}
943 ssize_t
raw_fd_stream::read(char *Ptr
, size_t Size
) {
944 assert(get_fd() >= 0 && "File already closed.");
945 ssize_t Ret
= ::read(get_fd(), (void *)Ptr
, Size
);
949 error_detected(std::error_code(errno
, std::generic_category()));
953 bool raw_fd_stream::classof(const raw_ostream
*OS
) {
954 return OS
->get_kind() == OStreamKind::OK_FDStream
;
957 //===----------------------------------------------------------------------===//
958 // raw_svector_ostream
959 //===----------------------------------------------------------------------===//
961 uint64_t raw_svector_ostream::current_pos() const { return OS
.size(); }
963 void raw_svector_ostream::write_impl(const char *Ptr
, size_t Size
) {
964 OS
.append(Ptr
, Ptr
+ Size
);
967 void raw_svector_ostream::pwrite_impl(const char *Ptr
, size_t Size
,
969 memcpy(OS
.data() + Offset
, Ptr
, Size
);
972 //===----------------------------------------------------------------------===//
974 //===----------------------------------------------------------------------===//
976 raw_null_ostream::~raw_null_ostream() {
978 // ~raw_ostream asserts that the buffer is empty. This isn't necessary
979 // with raw_null_ostream, but it's better to have raw_null_ostream follow
980 // the rules than to change the rules just for raw_null_ostream.
985 void raw_null_ostream::write_impl(const char *Ptr
, size_t Size
) {
988 uint64_t raw_null_ostream::current_pos() const {
992 void raw_null_ostream::pwrite_impl(const char *Ptr
, size_t Size
,
995 void raw_pwrite_stream::anchor() {}
997 void buffer_ostream::anchor() {}
999 void buffer_unique_ostream::anchor() {}
1001 Error
llvm::writeToOutput(StringRef OutputFileName
,
1002 std::function
<Error(raw_ostream
&)> Write
) {
1003 if (OutputFileName
== "-")
1004 return Write(outs());
1006 if (OutputFileName
== "/dev/null") {
1007 raw_null_ostream Out
;
1011 unsigned Mode
= sys::fs::all_read
| sys::fs::all_write
;
1012 Expected
<sys::fs::TempFile
> Temp
=
1013 sys::fs::TempFile::create(OutputFileName
+ ".temp-stream-%%%%%%", Mode
);
1015 return createFileError(OutputFileName
, Temp
.takeError());
1017 raw_fd_ostream
Out(Temp
->FD
, false);
1019 if (Error E
= Write(Out
)) {
1020 if (Error DiscardError
= Temp
->discard())
1021 return joinErrors(std::move(E
), std::move(DiscardError
));
1026 return Temp
->keep(OutputFileName
);