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