1//===--- raw_ostream.cpp - Implement the raw_ostream classes --------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This implements support for bulk buffered stream output.
10//
11//===----------------------------------------------------------------------===//
12
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/IOSandbox.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"
28#include <algorithm>
29#include <cerrno>
30#include <cstdio>
31#include <sys/stat.h>
32
33// <fcntl.h> may provide O_BINARY.
34# include <fcntl.h>
35
36#if defined(HAVE_UNISTD_H)
37# include <unistd.h>
38#endif
39
40#if defined(__CYGWIN__)
41#include <io.h>
42#endif
43
44#if defined(_MSC_VER)
45#include <io.h>
46#ifndef STDIN_FILENO
47# define STDIN_FILENO 0
48#endif
49#ifndef STDOUT_FILENO
50# define STDOUT_FILENO 1
51#endif
52#ifndef STDERR_FILENO
53# define STDERR_FILENO 2
54#endif
55#endif
56
57#ifdef _WIN32
58#include "llvm/Support/ConvertUTF.h"
59#include "llvm/Support/Signals.h"
60#include "llvm/Support/Windows/WindowsSupport.h"
61#endif
62
63using namespace llvm;
64
65raw_ostream::~raw_ostream() {
66 // raw_ostream's subclasses should take care to flush the buffer
67 // in their destructors.
68 assert(OutBufCur == OutBufStart &&
69 "raw_ostream destructor called with non-empty buffer!");
70
71 if (BufferMode == BufferKind::InternalBuffer)
72 delete [] OutBufStart;
73}
74
75size_t raw_ostream::preferred_buffer_size() const {
76#ifdef _WIN32
77 // On Windows BUFSIZ is only 512 which results in more calls to write. This
78 // overhead can cause significant performance degradation. Therefore use a
79 // better default.
80 return (16 * 1024);
81#else
82 // BUFSIZ is intended to be a reasonable default.
83 return BUFSIZ;
84#endif
85}
86
87void raw_ostream::SetBuffered() {
88 // Ask the subclass to determine an appropriate buffer size.
89 if (size_t Size = preferred_buffer_size())
90 SetBufferSize(Size);
91 else
92 // It may return 0, meaning this stream should be unbuffered.
93 SetUnbuffered();
94}
95
96void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size,
97 BufferKind Mode) {
98 assert(((Mode == BufferKind::Unbuffered && !BufferStart && Size == 0) ||
99 (Mode != BufferKind::Unbuffered && BufferStart && Size != 0)) &&
100 "stream must be unbuffered or have at least one byte");
101 // Make sure the current buffer is free of content (we can't flush here; the
102 // child buffer management logic will be in write_impl).
103 assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!");
104
105 if (BufferMode == BufferKind::InternalBuffer)
106 delete [] OutBufStart;
107 OutBufStart = BufferStart;
108 OutBufEnd = OutBufStart+Size;
109 OutBufCur = OutBufStart;
110 BufferMode = Mode;
111
112 assert(OutBufStart <= OutBufEnd && "Invalid size!");
113}
114
115raw_ostream &raw_ostream::operator<<(unsigned long N) {
116 write_integer(S&: *this, N: static_cast<uint64_t>(N), MinDigits: 0, Style: IntegerStyle::Integer);
117 return *this;
118}
119
120raw_ostream &raw_ostream::operator<<(long N) {
121 write_integer(S&: *this, N: static_cast<int64_t>(N), MinDigits: 0, Style: IntegerStyle::Integer);
122 return *this;
123}
124
125raw_ostream &raw_ostream::operator<<(unsigned long long N) {
126 write_integer(S&: *this, N: static_cast<uint64_t>(N), MinDigits: 0, Style: IntegerStyle::Integer);
127 return *this;
128}
129
130raw_ostream &raw_ostream::operator<<(long long N) {
131 write_integer(S&: *this, N: static_cast<int64_t>(N), MinDigits: 0, Style: IntegerStyle::Integer);
132 return *this;
133}
134
135raw_ostream &raw_ostream::write_hex(unsigned long long N) {
136 llvm::write_hex(S&: *this, N, Style: HexPrintStyle::Lower);
137 return *this;
138}
139
140raw_ostream &raw_ostream::operator<<(Colors C) {
141 if (C == Colors::RESET)
142 resetColor();
143 else
144 changeColor(Color: C);
145 return *this;
146}
147
148raw_ostream &raw_ostream::write_uuid(const uuid_t UUID) {
149 for (int Idx = 0; Idx < 16; ++Idx) {
150 *this << format(Fmt: "%02" PRIX32, Vals: UUID[Idx]);
151 if (Idx == 3 || Idx == 5 || Idx == 7 || Idx == 9)
152 *this << "-";
153 }
154 return *this;
155}
156
157
158raw_ostream &raw_ostream::write_escaped(StringRef Str,
159 bool UseHexEscapes) {
160 for (unsigned char c : Str) {
161 switch (c) {
162 case '\\':
163 *this << '\\' << '\\';
164 break;
165 case '\t':
166 *this << '\\' << 't';
167 break;
168 case '\n':
169 *this << '\\' << 'n';
170 break;
171 case '"':
172 *this << '\\' << '"';
173 break;
174 default:
175 if (isPrint(C: c)) {
176 *this << c;
177 break;
178 }
179
180 // Write out the escaped representation.
181 if (UseHexEscapes) {
182 *this << '\\' << 'x';
183 *this << hexdigit(X: (c >> 4) & 0xF);
184 *this << hexdigit(X: (c >> 0) & 0xF);
185 } else {
186 // Always use a full 3-character octal escape.
187 *this << '\\';
188 *this << char('0' + ((c >> 6) & 7));
189 *this << char('0' + ((c >> 3) & 7));
190 *this << char('0' + ((c >> 0) & 7));
191 }
192 }
193 }
194
195 return *this;
196}
197
198raw_ostream &raw_ostream::operator<<(const void *P) {
199 llvm::write_hex(S&: *this, N: (uintptr_t)P, Style: HexPrintStyle::PrefixLower);
200 return *this;
201}
202
203raw_ostream &raw_ostream::operator<<(double N) {
204 llvm::write_double(S&: *this, D: N, Style: FloatStyle::Exponent);
205 return *this;
206}
207
208void raw_ostream::flush_nonempty() {
209 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
210 size_t Length = OutBufCur - OutBufStart;
211 OutBufCur = OutBufStart;
212 write_impl(Ptr: OutBufStart, Size: Length);
213}
214
215raw_ostream &raw_ostream::write(unsigned char C) {
216 // Group exceptional cases into a single branch.
217 if (LLVM_UNLIKELY(OutBufCur >= OutBufEnd)) {
218 if (LLVM_UNLIKELY(!OutBufStart)) {
219 if (BufferMode == BufferKind::Unbuffered) {
220 write_impl(Ptr: reinterpret_cast<char *>(&C), Size: 1);
221 return *this;
222 }
223 // Set up a buffer and start over.
224 SetBuffered();
225 return write(C);
226 }
227
228 flush_nonempty();
229 }
230
231 *OutBufCur++ = C;
232 return *this;
233}
234
235raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
236 // Group exceptional cases into a single branch.
237 if (LLVM_UNLIKELY(size_t(OutBufEnd - OutBufCur) < Size)) {
238 if (LLVM_UNLIKELY(!OutBufStart)) {
239 if (BufferMode == BufferKind::Unbuffered) {
240 write_impl(Ptr, Size);
241 return *this;
242 }
243 // Set up a buffer and start over.
244 SetBuffered();
245 return write(Ptr, Size);
246 }
247
248 size_t NumBytes = OutBufEnd - OutBufCur;
249
250 // If the buffer is empty at this point we have a string that is larger
251 // than the buffer. Directly write the chunk that is a multiple of the
252 // preferred buffer size and put the remainder in the buffer.
253 if (LLVM_UNLIKELY(OutBufCur == OutBufStart)) {
254 assert(NumBytes != 0 && "undefined behavior");
255 size_t BytesToWrite = Size - (Size % NumBytes);
256 write_impl(Ptr, Size: BytesToWrite);
257 size_t BytesRemaining = Size - BytesToWrite;
258 if (BytesRemaining > size_t(OutBufEnd - OutBufCur)) {
259 // Too much left over to copy into our buffer.
260 return write(Ptr: Ptr + BytesToWrite, Size: BytesRemaining);
261 }
262 copy_to_buffer(Ptr: Ptr + BytesToWrite, Size: BytesRemaining);
263 return *this;
264 }
265
266 // We don't have enough space in the buffer to fit the string in. Insert as
267 // much as possible, flush and start over with the remainder.
268 copy_to_buffer(Ptr, Size: NumBytes);
269 flush_nonempty();
270 return write(Ptr: Ptr + NumBytes, Size: Size - NumBytes);
271 }
272
273 copy_to_buffer(Ptr, Size);
274
275 return *this;
276}
277
278void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) {
279 assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!");
280
281 // Handle short strings specially, memcpy isn't very good at very short
282 // strings.
283 switch (Size) {
284 case 4: OutBufCur[3] = Ptr[3]; [[fallthrough]];
285 case 3: OutBufCur[2] = Ptr[2]; [[fallthrough]];
286 case 2: OutBufCur[1] = Ptr[1]; [[fallthrough]];
287 case 1: OutBufCur[0] = Ptr[0]; [[fallthrough]];
288 case 0: break;
289 default:
290 memcpy(dest: OutBufCur, src: Ptr, n: Size);
291 break;
292 }
293
294 OutBufCur += Size;
295}
296
297// Formatted output.
298raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
299 // If we have more than a few bytes left in our output buffer, try
300 // formatting directly onto its end.
301 size_t NextBufferSize = 127;
302 size_t BufferBytesLeft = OutBufEnd - OutBufCur;
303 if (BufferBytesLeft > 3) {
304 size_t BytesUsed = Fmt.print(Buffer: OutBufCur, BufferSize: BufferBytesLeft);
305
306 // Common case is that we have plenty of space.
307 if (BytesUsed <= BufferBytesLeft) {
308 OutBufCur += BytesUsed;
309 return *this;
310 }
311
312 // Otherwise, we overflowed and the return value tells us the size to try
313 // again with.
314 NextBufferSize = BytesUsed;
315 }
316
317 // If we got here, we didn't have enough space in the output buffer for the
318 // string. Try printing into a SmallVector that is resized to have enough
319 // space. Iterate until we win.
320 SmallVector<char, 128> V;
321
322 while (true) {
323 V.resize(N: NextBufferSize);
324
325 // Try formatting into the SmallVector.
326 size_t BytesUsed = Fmt.print(Buffer: V.data(), BufferSize: NextBufferSize);
327
328 // If BytesUsed fit into the vector, we win.
329 if (BytesUsed <= NextBufferSize)
330 return write(Ptr: V.data(), Size: BytesUsed);
331
332 // Otherwise, try again with a new size.
333 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
334 NextBufferSize = BytesUsed;
335 }
336}
337
338raw_ostream &raw_ostream::operator<<(const formatv_object_base &Obj) {
339 Obj.format(S&: *this);
340 return *this;
341}
342
343raw_ostream &raw_ostream::operator<<(const FormattedString &FS) {
344 unsigned LeftIndent = 0;
345 unsigned RightIndent = 0;
346 const ssize_t Difference = FS.Width - FS.Str.size();
347 if (Difference > 0) {
348 switch (FS.Justify) {
349 case FormattedString::JustifyNone:
350 break;
351 case FormattedString::JustifyLeft:
352 RightIndent = Difference;
353 break;
354 case FormattedString::JustifyRight:
355 LeftIndent = Difference;
356 break;
357 case FormattedString::JustifyCenter:
358 LeftIndent = Difference / 2;
359 RightIndent = Difference - LeftIndent;
360 break;
361 }
362 }
363 indent(NumSpaces: LeftIndent);
364 (*this) << FS.Str;
365 indent(NumSpaces: RightIndent);
366 return *this;
367}
368
369raw_ostream &raw_ostream::operator<<(const FormattedNumber &FN) {
370 if (FN.Hex) {
371 HexPrintStyle Style;
372 if (FN.Upper && FN.HexPrefix)
373 Style = HexPrintStyle::PrefixUpper;
374 else if (FN.Upper && !FN.HexPrefix)
375 Style = HexPrintStyle::Upper;
376 else if (!FN.Upper && FN.HexPrefix)
377 Style = HexPrintStyle::PrefixLower;
378 else
379 Style = HexPrintStyle::Lower;
380 llvm::write_hex(S&: *this, N: FN.HexValue, Style, Width: FN.Width);
381 } else {
382 llvm::SmallString<16> Buffer;
383 llvm::raw_svector_ostream Stream(Buffer);
384 llvm::write_integer(S&: Stream, N: FN.DecValue, MinDigits: 0, Style: IntegerStyle::Integer);
385 if (Buffer.size() < FN.Width)
386 indent(NumSpaces: FN.Width - Buffer.size());
387 (*this) << Buffer;
388 }
389 return *this;
390}
391
392raw_ostream &raw_ostream::operator<<(const FormattedBytes &FB) {
393 if (FB.Bytes.empty())
394 return *this;
395
396 size_t LineIndex = 0;
397 auto Bytes = FB.Bytes;
398 const size_t Size = Bytes.size();
399 HexPrintStyle HPS = FB.Upper ? HexPrintStyle::Upper : HexPrintStyle::Lower;
400 uint64_t OffsetWidth = 0;
401 if (FB.FirstByteOffset) {
402 // Figure out how many nibbles are needed to print the largest offset
403 // represented by this data set, so that we can align the offset field
404 // to the right width.
405 size_t Lines = Size / FB.NumPerLine;
406 uint64_t MaxOffset = *FB.FirstByteOffset + Lines * FB.NumPerLine;
407 unsigned Power = 0;
408 if (MaxOffset > 0)
409 Power = llvm::Log2_64_Ceil(Value: MaxOffset);
410 OffsetWidth = std::max<uint64_t>(a: 4, b: llvm::alignTo(Value: Power, Align: 4) / 4);
411 }
412
413 // The width of a block of data including all spaces for group separators.
414 unsigned NumByteGroups =
415 alignTo(Value: FB.NumPerLine, Align: FB.ByteGroupSize) / FB.ByteGroupSize;
416 unsigned BlockCharWidth = FB.NumPerLine * 2 + NumByteGroups - 1;
417
418 while (!Bytes.empty()) {
419 indent(NumSpaces: FB.IndentLevel);
420
421 if (FB.FirstByteOffset) {
422 uint64_t Offset = *FB.FirstByteOffset;
423 llvm::write_hex(S&: *this, N: Offset + LineIndex, Style: HPS, Width: OffsetWidth);
424 *this << ": ";
425 }
426
427 auto Line = Bytes.take_front(N: FB.NumPerLine);
428
429 size_t CharsPrinted = 0;
430 // Print the hex bytes for this line in groups
431 for (size_t I = 0; I < Line.size(); ++I, CharsPrinted += 2) {
432 if (I && (I % FB.ByteGroupSize) == 0) {
433 ++CharsPrinted;
434 *this << " ";
435 }
436 llvm::write_hex(S&: *this, N: Line[I], Style: HPS, Width: 2);
437 }
438
439 if (FB.ASCII) {
440 // Print any spaces needed for any bytes that we didn't print on this
441 // line so that the ASCII bytes are correctly aligned.
442 assert(BlockCharWidth >= CharsPrinted);
443 indent(NumSpaces: BlockCharWidth - CharsPrinted + 2);
444 *this << "|";
445
446 // Print the ASCII char values for each byte on this line
447 for (uint8_t Byte : Line) {
448 if (isPrint(C: Byte))
449 *this << static_cast<char>(Byte);
450 else
451 *this << '.';
452 }
453 *this << '|';
454 }
455
456 Bytes = Bytes.drop_front(N: Line.size());
457 LineIndex += Line.size();
458 if (LineIndex < Size)
459 *this << '\n';
460 }
461 return *this;
462}
463
464template <char C>
465static raw_ostream &write_padding(raw_ostream &OS, unsigned NumChars) {
466 static const char Chars[] = {C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C,
467 C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C,
468 C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C,
469 C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C,
470 C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C};
471
472 // Usually the indentation is small, handle it with a fastpath.
473 if (NumChars < std::size(Chars))
474 return OS.write(Ptr: Chars, Size: NumChars);
475
476 while (NumChars) {
477 unsigned NumToWrite = std::min(a: NumChars, b: (unsigned)std::size(Chars) - 1);
478 OS.write(Ptr: Chars, Size: NumToWrite);
479 NumChars -= NumToWrite;
480 }
481 return OS;
482}
483
484/// indent - Insert 'NumSpaces' spaces.
485raw_ostream &raw_ostream::indent(unsigned NumSpaces) {
486 return write_padding<' '>(OS&: *this, NumChars: NumSpaces);
487}
488
489/// write_zeros - Insert 'NumZeros' nulls.
490raw_ostream &raw_ostream::write_zeros(unsigned NumZeros) {
491 return write_padding<'\0'>(OS&: *this, NumChars: NumZeros);
492}
493
494bool raw_ostream::prepare_colors() {
495 // Colors were explicitly disabled.
496 if (!ColorEnabled)
497 return false;
498
499 // Colors require changing the terminal but this stream is not going to a
500 // terminal.
501 if (sys::Process::ColorNeedsFlush() && !is_displayed())
502 return false;
503
504 if (sys::Process::ColorNeedsFlush())
505 flush();
506
507 return true;
508}
509
510raw_ostream &raw_ostream::changeColor(enum Colors colors, bool bold, bool bg) {
511 if (!prepare_colors())
512 return *this;
513
514 const char *colorcode =
515 (colors == SAVEDCOLOR)
516 ? sys::Process::OutputBold(bg)
517 : sys::Process::OutputColor(c: static_cast<char>(colors), bold, bg);
518 if (colorcode)
519 write(Ptr: colorcode, Size: strlen(s: colorcode));
520 return *this;
521}
522
523raw_ostream &raw_ostream::resetColor() {
524 if (!prepare_colors())
525 return *this;
526
527 if (const char *colorcode = sys::Process::ResetColor())
528 write(Ptr: colorcode, Size: strlen(s: colorcode));
529 return *this;
530}
531
532raw_ostream &raw_ostream::reverseColor() {
533 if (!prepare_colors())
534 return *this;
535
536 if (const char *colorcode = sys::Process::OutputReverse())
537 write(Ptr: colorcode, Size: strlen(s: colorcode));
538 return *this;
539}
540
541void raw_ostream::anchor() {}
542
543//===----------------------------------------------------------------------===//
544// Formatted Output
545//===----------------------------------------------------------------------===//
546
547// Out of line virtual method.
548void format_object_base::home() {
549}
550
551//===----------------------------------------------------------------------===//
552// raw_fd_ostream
553//===----------------------------------------------------------------------===//
554
555static int getFD(StringRef Filename, std::error_code &EC,
556 sys::fs::CreationDisposition Disp, sys::fs::FileAccess Access,
557 sys::fs::OpenFlags Flags) {
558 // FIXME(sandboxing): Remove this by adopting `llvm::vfs::OutputBackend`.
559 auto BypassSandbox = sys::sandbox::scopedDisable();
560
561 assert((Access & sys::fs::FA_Write) &&
562 "Cannot make a raw_ostream from a read-only descriptor!");
563
564 // Handle "-" as stdout. Note that when we do this, we consider ourself
565 // the owner of stdout and may set the "binary" flag globally based on Flags.
566 if (Filename == "-") {
567 EC = std::error_code();
568 // Change stdout's text/binary mode based on the Flags.
569 sys::ChangeStdoutMode(Flags);
570 return STDOUT_FILENO;
571 }
572
573 int FD;
574 if (Access & sys::fs::FA_Read)
575 EC = sys::fs::openFileForReadWrite(Name: Filename, ResultFD&: FD, Disp, Flags);
576 else
577 EC = sys::fs::openFileForWrite(Name: Filename, ResultFD&: FD, Disp, Flags);
578 if (EC)
579 return -1;
580
581 return FD;
582}
583
584raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC)
585 : raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, sys::fs::FA_Write,
586 sys::fs::OF_None) {}
587
588raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
589 sys::fs::CreationDisposition Disp)
590 : raw_fd_ostream(Filename, EC, Disp, sys::fs::FA_Write, sys::fs::OF_None) {}
591
592raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
593 sys::fs::FileAccess Access)
594 : raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, Access,
595 sys::fs::OF_None) {}
596
597raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
598 sys::fs::OpenFlags Flags)
599 : raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, sys::fs::FA_Write,
600 Flags) {}
601
602raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
603 sys::fs::CreationDisposition Disp,
604 sys::fs::FileAccess Access,
605 sys::fs::OpenFlags Flags)
606 : raw_fd_ostream(getFD(Filename, EC, Disp, Access, Flags), true) {}
607
608/// FD is the file descriptor that this writes to. If ShouldClose is true, this
609/// closes the file when the stream is destroyed.
610raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered,
611 OStreamKind K)
612 : raw_pwrite_stream(unbuffered, K), FD(fd), ShouldClose(shouldClose) {
613 // FIXME(sandboxing): Remove this by adopting `llvm::vfs::OutputBackend`.
614 auto BypassSandbox = sys::sandbox::scopedDisable();
615
616 if (FD < 0 ) {
617 ShouldClose = false;
618 return;
619 }
620
621 enable_colors(enable: true);
622
623 // Do not attempt to close stdout or stderr. We used to try to maintain the
624 // property that tools that support writing file to stdout should not also
625 // write informational output to stdout, but in practice we were never able to
626 // maintain this invariant. Many features have been added to LLVM and clang
627 // (-fdump-record-layouts, optimization remarks, etc) that print to stdout, so
628 // users must simply be aware that mixed output and remarks is a possibility.
629 if (FD <= STDERR_FILENO)
630 ShouldClose = false;
631
632#ifdef _WIN32
633 // Check if this is a console device. This is not equivalent to isatty.
634 IsWindowsConsole =
635 ::GetFileType((HANDLE)::_get_osfhandle(fd)) == FILE_TYPE_CHAR;
636#endif
637
638 // Get the starting position.
639 off_t loc = ::lseek(fd: FD, offset: 0, SEEK_CUR);
640 sys::fs::file_status Status;
641 std::error_code EC = status(FD, Result&: Status);
642 IsRegularFile = Status.type() == sys::fs::file_type::regular_file;
643#ifdef _WIN32
644 // MSVCRT's _lseek(SEEK_CUR) doesn't return -1 for pipes.
645 SupportsSeeking = !EC && IsRegularFile;
646#else
647 SupportsSeeking = !EC && loc != (off_t)-1;
648#endif
649 if (!SupportsSeeking)
650 pos = 0;
651 else
652 pos = static_cast<uint64_t>(loc);
653}
654
655raw_fd_ostream::~raw_fd_ostream() {
656 if (FD >= 0) {
657 flush();
658 if (ShouldClose) {
659 if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
660 error_detected(EC);
661 }
662 }
663
664#ifdef __MINGW32__
665 // On mingw, global dtors should not call exit().
666 // report_fatal_error() invokes exit(). We know report_fatal_error()
667 // might not write messages to stderr when any errors were detected
668 // on FD == 2.
669 if (FD == 2) return;
670#endif
671
672 // If there are any pending errors, report them now. Clients wishing
673 // to avoid report_fatal_error calls should check for errors with
674 // has_error() and clear the error flag with clear_error() before
675 // destructing raw_ostream objects which may have errors.
676 if (has_error())
677 reportFatalUsageError(reason: Twine("IO failure on output stream: ") +
678 error().message());
679}
680
681#if defined(_WIN32)
682// The most reliable way to print unicode in a Windows console is with
683// WriteConsoleW. To use that, first transcode from UTF-8 to UTF-16. This
684// assumes that LLVM programs always print valid UTF-8 to the console. The data
685// might not be UTF-8 for two major reasons:
686// 1. The program is printing binary (-filetype=obj -o -), in which case it
687// would have been gibberish anyway.
688// 2. The program is printing text in a semi-ascii compatible codepage like
689// shift-jis or cp1252.
690//
691// Most LLVM programs don't produce non-ascii text unless they are quoting
692// user source input. A well-behaved LLVM program should either validate that
693// the input is UTF-8 or transcode from the local codepage to UTF-8 before
694// quoting it. If they don't, this may mess up the encoding, but this is still
695// probably the best compromise we can make.
696static bool write_console_impl(int FD, StringRef Data) {
697 SmallVector<wchar_t, 256> WideText;
698
699 // Fall back to ::write if it wasn't valid UTF-8.
700 if (auto EC = sys::windows::UTF8ToUTF16(Data, WideText))
701 return false;
702
703 // On Windows 7 and earlier, WriteConsoleW has a low maximum amount of data
704 // that can be written to the console at a time.
705 size_t MaxWriteSize = WideText.size();
706 if (!RunningWindows8OrGreater())
707 MaxWriteSize = 32767;
708
709 size_t WCharsWritten = 0;
710 do {
711 size_t WCharsToWrite =
712 std::min(MaxWriteSize, WideText.size() - WCharsWritten);
713 DWORD ActuallyWritten;
714 bool Success =
715 ::WriteConsoleW((HANDLE)::_get_osfhandle(FD), &WideText[WCharsWritten],
716 WCharsToWrite, &ActuallyWritten,
717 /*Reserved=*/nullptr);
718
719 // The most likely reason for WriteConsoleW to fail is that FD no longer
720 // points to a console. Fall back to ::write. If this isn't the first loop
721 // iteration, something is truly wrong.
722 if (!Success)
723 return false;
724
725 WCharsWritten += ActuallyWritten;
726 } while (WCharsWritten != WideText.size());
727 return true;
728}
729#endif
730
731void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
732 if (TiedStream)
733 TiedStream->flush();
734
735 assert(FD >= 0 && "File already closed.");
736 pos += Size;
737
738#if defined(_WIN32)
739 // If this is a Windows console device, try re-encoding from UTF-8 to UTF-16
740 // and using WriteConsoleW. If that fails, fall back to plain write().
741 if (IsWindowsConsole)
742 if (write_console_impl(FD, StringRef(Ptr, Size)))
743 return;
744#endif
745
746 // The maximum write size is limited to INT32_MAX. A write
747 // greater than SSIZE_MAX is implementation-defined in POSIX,
748 // and Windows _write requires 32 bit input.
749 size_t MaxWriteSize = INT32_MAX;
750
751#if defined(__linux__)
752 // It is observed that Linux returns EINVAL for a very large write (>2G).
753 // Make it a reasonably small value.
754 MaxWriteSize = 1024 * 1024 * 1024;
755#endif
756
757 do {
758 size_t ChunkSize = std::min(a: Size, b: MaxWriteSize);
759 ssize_t ret = ::write(fd: FD, buf: Ptr, n: ChunkSize);
760
761 if (ret < 0) {
762 // If it's a recoverable error, swallow it and retry the write.
763 //
764 // Ideally we wouldn't ever see EAGAIN or EWOULDBLOCK here, since
765 // raw_ostream isn't designed to do non-blocking I/O. However, some
766 // programs, such as old versions of bjam, have mistakenly used
767 // O_NONBLOCK. For compatibility, emulate blocking semantics by
768 // spinning until the write succeeds. If you don't want spinning,
769 // don't use O_NONBLOCK file descriptors with raw_ostream.
770 if (errno == EINTR || errno == EAGAIN
771#ifdef EWOULDBLOCK
772 || errno == EWOULDBLOCK
773#endif
774 )
775 continue;
776
777#ifdef _WIN32
778 // Windows equivalents of SIGPIPE/EPIPE.
779 DWORD WinLastError = GetLastError();
780 if (WinLastError == ERROR_BROKEN_PIPE ||
781 (WinLastError == ERROR_NO_DATA && errno == EINVAL)) {
782 llvm::sys::CallOneShotPipeSignalHandler();
783 errno = EPIPE;
784 }
785#endif
786 // Otherwise it's a non-recoverable error. Note it and quit.
787 error_detected(EC: errnoAsErrorCode());
788 break;
789 }
790
791 // The write may have written some or all of the data. Update the
792 // size and buffer pointer to reflect the remainder that needs
793 // to be written. If there are no bytes left, we're done.
794 Ptr += ret;
795 Size -= ret;
796 } while (Size > 0);
797}
798
799void raw_fd_ostream::close() {
800 assert(ShouldClose);
801 ShouldClose = false;
802 flush();
803 if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
804 error_detected(EC);
805 FD = -1;
806}
807
808uint64_t raw_fd_ostream::seek(uint64_t off) {
809 assert(SupportsSeeking && "Stream does not support seeking!");
810 flush();
811#ifdef _WIN32
812 pos = ::_lseeki64(FD, off, SEEK_SET);
813#else
814 pos = ::lseek(fd: FD, offset: off, SEEK_SET);
815#endif
816 if (pos == (uint64_t)-1)
817 error_detected(EC: errnoAsErrorCode());
818 return pos;
819}
820
821void raw_fd_ostream::pwrite_impl(const char *Ptr, size_t Size,
822 uint64_t Offset) {
823 uint64_t Pos = tell();
824 seek(off: Offset);
825 write(Ptr, Size);
826 seek(off: Pos);
827}
828
829size_t raw_fd_ostream::preferred_buffer_size() const {
830#if defined(_WIN32)
831 // Disable buffering for console devices. Console output is re-encoded from
832 // UTF-8 to UTF-16 on Windows, and buffering it would require us to split the
833 // buffer on a valid UTF-8 codepoint boundary. Terminal buffering is disabled
834 // below on most other OSs, so do the same thing on Windows and avoid that
835 // complexity.
836 if (IsWindowsConsole)
837 return 0;
838 return raw_ostream::preferred_buffer_size();
839#elif defined(__MVS__)
840 // The buffer size on z/OS is defined with macro BUFSIZ, which can be
841 // retrieved by invoking function raw_ostream::preferred_buffer_size().
842 return raw_ostream::preferred_buffer_size();
843#else
844 assert(FD >= 0 && "File not yet open!");
845 struct stat statbuf;
846 if (fstat(fd: FD, buf: &statbuf) != 0)
847 return 0;
848
849 // If this is a terminal, don't use buffering. Line buffering
850 // would be a more traditional thing to do, but it's not worth
851 // the complexity.
852 if (S_ISCHR(statbuf.st_mode) && is_displayed())
853 return 0;
854 // Return the preferred block size.
855 return statbuf.st_blksize;
856#endif
857}
858
859bool raw_fd_ostream::is_displayed() const {
860 return sys::Process::FileDescriptorIsDisplayed(fd: FD);
861}
862
863bool raw_fd_ostream::has_colors() const {
864 if (!HasColors)
865 HasColors = sys::Process::FileDescriptorHasColors(fd: FD);
866 return *HasColors;
867}
868
869Expected<sys::fs::FileLocker> raw_fd_ostream::lock() {
870 std::error_code EC = sys::fs::lockFile(FD);
871 if (!EC)
872 return sys::fs::FileLocker(FD);
873 return errorCodeToError(EC);
874}
875
876Expected<sys::fs::FileLocker>
877raw_fd_ostream::tryLockFor(Duration const& Timeout) {
878 std::error_code EC = sys::fs::tryLockFile(FD, Timeout: Timeout.getDuration());
879 if (!EC)
880 return sys::fs::FileLocker(FD);
881 return errorCodeToError(EC);
882}
883
884void raw_fd_ostream::anchor() {}
885
886//===----------------------------------------------------------------------===//
887// outs(), errs(), nulls()
888//===----------------------------------------------------------------------===//
889
890raw_fd_ostream &llvm::outs() {
891 // Set buffer settings to model stdout behavior.
892 std::error_code EC;
893
894 // On z/OS we need to enable auto conversion
895 static std::error_code EC1 = enableAutoConversion(STDOUT_FILENO);
896 assert(!EC1);
897 (void)EC1;
898
899 static raw_fd_ostream S("-", EC, sys::fs::OF_None);
900 assert(!EC);
901 return S;
902}
903
904raw_fd_ostream &llvm::errs() {
905 // On z/OS we need to enable auto conversion
906 static std::error_code EC = enableAutoConversion(STDERR_FILENO);
907 assert(!EC);
908 (void)EC;
909
910 // Set standard error to be unbuffered.
911 static raw_fd_ostream S(STDERR_FILENO, false, true);
912 return S;
913}
914
915/// nulls() - This returns a reference to a raw_ostream which discards output.
916raw_ostream &llvm::nulls() {
917 static raw_null_ostream S;
918 return S;
919}
920
921//===----------------------------------------------------------------------===//
922// File Streams
923//===----------------------------------------------------------------------===//
924
925raw_fd_stream::raw_fd_stream(StringRef Filename, std::error_code &EC)
926 : raw_fd_ostream(getFD(Filename, EC, Disp: sys::fs::CD_CreateAlways,
927 Access: sys::fs::FA_Write | sys::fs::FA_Read,
928 Flags: sys::fs::OF_None),
929 true, false, OStreamKind::OK_FDStream) {
930 if (EC)
931 return;
932
933 if (!isRegularFile())
934 EC = std::make_error_code(e: std::errc::invalid_argument);
935}
936
937raw_fd_stream::raw_fd_stream(int fd, bool shouldClose)
938 : raw_fd_ostream(fd, shouldClose, false, OStreamKind::OK_FDStream) {}
939
940ssize_t raw_fd_stream::read(char *Ptr, size_t Size) {
941 assert(get_fd() >= 0 && "File already closed.");
942 ssize_t Ret = ::read(fd: get_fd(), buf: (void *)Ptr, nbytes: Size);
943 if (Ret >= 0)
944 inc_pos(Delta: Ret);
945 else
946 error_detected(EC: errnoAsErrorCode());
947 return Ret;
948}
949
950bool raw_fd_stream::classof(const raw_ostream *OS) {
951 return OS->get_kind() == OStreamKind::OK_FDStream;
952}
953
954//===----------------------------------------------------------------------===//
955// raw_string_ostream
956//===----------------------------------------------------------------------===//
957
958void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
959 OS.append(s: Ptr, n: Size);
960}
961
962//===----------------------------------------------------------------------===//
963// raw_svector_ostream
964//===----------------------------------------------------------------------===//
965
966uint64_t raw_svector_ostream::current_pos() const { return OS.size(); }
967
968void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
969 OS.append(in_start: Ptr, in_end: Ptr + Size);
970}
971
972void raw_svector_ostream::pwrite_impl(const char *Ptr, size_t Size,
973 uint64_t Offset) {
974 memcpy(dest: OS.data() + Offset, src: Ptr, n: Size);
975}
976
977bool raw_svector_ostream::classof(const raw_ostream *OS) {
978 return OS->get_kind() == OStreamKind::OK_SVecStream;
979}
980
981//===----------------------------------------------------------------------===//
982// raw_null_ostream
983//===----------------------------------------------------------------------===//
984
985raw_null_ostream::~raw_null_ostream() {
986#ifndef NDEBUG
987 // ~raw_ostream asserts that the buffer is empty. This isn't necessary
988 // with raw_null_ostream, but it's better to have raw_null_ostream follow
989 // the rules than to change the rules just for raw_null_ostream.
990 flush();
991#endif
992}
993
994void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
995}
996
997uint64_t raw_null_ostream::current_pos() const {
998 return 0;
999}
1000
1001void raw_null_ostream::pwrite_impl(const char *Ptr, size_t Size,
1002 uint64_t Offset) {}
1003
1004void raw_pwrite_stream::anchor() {}
1005
1006void buffer_ostream::anchor() {}
1007
1008void buffer_unique_ostream::anchor() {}
1009
1010Error llvm::writeToOutput(StringRef OutputFileName,
1011 std::function<Error(raw_ostream &)> Write) {
1012 if (OutputFileName == "-")
1013 return Write(outs());
1014
1015 if (OutputFileName == "/dev/null") {
1016 raw_null_ostream Out;
1017 return Write(Out);
1018 }
1019
1020 unsigned Mode = sys::fs::all_read | sys::fs::all_write;
1021 Expected<sys::fs::TempFile> Temp =
1022 sys::fs::TempFile::create(Model: OutputFileName + ".temp-stream-%%%%%%", Mode);
1023 if (!Temp)
1024 return createFileError(F: OutputFileName, E: Temp.takeError());
1025
1026 raw_fd_ostream Out(Temp->FD, false);
1027
1028 if (Error E = Write(Out)) {
1029 if (Error DiscardError = Temp->discard())
1030 return joinErrors(E1: std::move(E), E2: std::move(DiscardError));
1031 return E;
1032 }
1033 Out.flush();
1034
1035 return Temp->keep(Name: OutputFileName);
1036}
1037