xref: /freebsd/contrib/llvm-project/llvm/lib/Support/raw_ostream.cpp (revision 2f9966ff63d65bd474478888c9088eeae3f9c669)
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/MathExtras.h"
24 #include "llvm/Support/NativeFormatting.h"
25 #include "llvm/Support/Process.h"
26 #include "llvm/Support/Program.h"
27 #include <algorithm>
28 #include <cerrno>
29 #include <cstdio>
30 #include <sys/stat.h>
31 
32 // <fcntl.h> may provide O_BINARY.
33 #if defined(HAVE_FCNTL_H)
34 # include <fcntl.h>
35 #endif
36 
37 #if defined(HAVE_UNISTD_H)
38 # include <unistd.h>
39 #endif
40 
41 #if defined(__CYGWIN__)
42 #include <io.h>
43 #endif
44 
45 #if defined(_MSC_VER)
46 #include <io.h>
47 #ifndef STDIN_FILENO
48 # define STDIN_FILENO 0
49 #endif
50 #ifndef STDOUT_FILENO
51 # define STDOUT_FILENO 1
52 #endif
53 #ifndef STDERR_FILENO
54 # define STDERR_FILENO 2
55 #endif
56 #endif
57 
58 #ifdef _WIN32
59 #include "llvm/Support/ConvertUTF.h"
60 #include "llvm/Support/Signals.h"
61 #include "llvm/Support/Windows/WindowsSupport.h"
62 #endif
63 
64 using namespace llvm;
65 
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;
76 
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!");
82 
83   if (BufferMode == BufferKind::InternalBuffer)
84     delete [] OutBufStart;
85 }
86 
87 size_t raw_ostream::preferred_buffer_size() const {
88 #ifdef _WIN32
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
91   // better default.
92   return (16 * 1024);
93 #else
94   // BUFSIZ is intended to be a reasonable default.
95   return BUFSIZ;
96 #endif
97 }
98 
99 void raw_ostream::SetBuffered() {
100   // Ask the subclass to determine an appropriate buffer size.
101   if (size_t Size = preferred_buffer_size())
102     SetBufferSize(Size);
103   else
104     // It may return 0, meaning this stream should be unbuffered.
105     SetUnbuffered();
106 }
107 
108 void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size,
109                                    BufferKind Mode) {
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!");
116 
117   if (BufferMode == BufferKind::InternalBuffer)
118     delete [] OutBufStart;
119   OutBufStart = BufferStart;
120   OutBufEnd = OutBufStart+Size;
121   OutBufCur = OutBufStart;
122   BufferMode = Mode;
123 
124   assert(OutBufStart <= OutBufEnd && "Invalid size!");
125 }
126 
127 raw_ostream &raw_ostream::operator<<(unsigned long N) {
128   write_integer(*this, static_cast<uint64_t>(N), 0, IntegerStyle::Integer);
129   return *this;
130 }
131 
132 raw_ostream &raw_ostream::operator<<(long N) {
133   write_integer(*this, static_cast<int64_t>(N), 0, IntegerStyle::Integer);
134   return *this;
135 }
136 
137 raw_ostream &raw_ostream::operator<<(unsigned long long N) {
138   write_integer(*this, static_cast<uint64_t>(N), 0, IntegerStyle::Integer);
139   return *this;
140 }
141 
142 raw_ostream &raw_ostream::operator<<(long long N) {
143   write_integer(*this, static_cast<int64_t>(N), 0, IntegerStyle::Integer);
144   return *this;
145 }
146 
147 raw_ostream &raw_ostream::write_hex(unsigned long long N) {
148   llvm::write_hex(*this, N, HexPrintStyle::Lower);
149   return *this;
150 }
151 
152 raw_ostream &raw_ostream::operator<<(Colors C) {
153   if (C == Colors::RESET)
154     resetColor();
155   else
156     changeColor(C);
157   return *this;
158 }
159 
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)
164       *this << "-";
165   }
166   return *this;
167 }
168 
169 
170 raw_ostream &raw_ostream::write_escaped(StringRef Str,
171                                         bool UseHexEscapes) {
172   for (unsigned char c : Str) {
173     switch (c) {
174     case '\\':
175       *this << '\\' << '\\';
176       break;
177     case '\t':
178       *this << '\\' << 't';
179       break;
180     case '\n':
181       *this << '\\' << 'n';
182       break;
183     case '"':
184       *this << '\\' << '"';
185       break;
186     default:
187       if (isPrint(c)) {
188         *this << c;
189         break;
190       }
191 
192       // Write out the escaped representation.
193       if (UseHexEscapes) {
194         *this << '\\' << 'x';
195         *this << hexdigit((c >> 4) & 0xF);
196         *this << hexdigit((c >> 0) & 0xF);
197       } else {
198         // Always use a full 3-character octal escape.
199         *this << '\\';
200         *this << char('0' + ((c >> 6) & 7));
201         *this << char('0' + ((c >> 3) & 7));
202         *this << char('0' + ((c >> 0) & 7));
203       }
204     }
205   }
206 
207   return *this;
208 }
209 
210 raw_ostream &raw_ostream::operator<<(const void *P) {
211   llvm::write_hex(*this, (uintptr_t)P, HexPrintStyle::PrefixLower);
212   return *this;
213 }
214 
215 raw_ostream &raw_ostream::operator<<(double N) {
216   llvm::write_double(*this, N, FloatStyle::Exponent);
217   return *this;
218 }
219 
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);
225 }
226 
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);
233         return *this;
234       }
235       // Set up a buffer and start over.
236       SetBuffered();
237       return write(C);
238     }
239 
240     flush_nonempty();
241   }
242 
243   *OutBufCur++ = C;
244   return *this;
245 }
246 
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);
253         return *this;
254       }
255       // Set up a buffer and start over.
256       SetBuffered();
257       return write(Ptr, Size);
258     }
259 
260     size_t NumBytes = OutBufEnd - OutBufCur;
261 
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);
273       }
274       copy_to_buffer(Ptr + BytesToWrite, BytesRemaining);
275       return *this;
276     }
277 
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);
281     flush_nonempty();
282     return write(Ptr + NumBytes, Size - NumBytes);
283   }
284 
285   copy_to_buffer(Ptr, Size);
286 
287   return *this;
288 }
289 
290 void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) {
291   assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!");
292 
293   // Handle short strings specially, memcpy isn't very good at very short
294   // strings.
295   switch (Size) {
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]];
300   case 0: break;
301   default:
302     memcpy(OutBufCur, Ptr, Size);
303     break;
304   }
305 
306   OutBufCur += Size;
307 }
308 
309 void raw_ostream::flush_tied_then_write(const char *Ptr, size_t Size) {
310   if (TiedStream)
311     TiedStream->flush();
312   write_impl(Ptr, Size);
313 }
314 
315 // Formatted output.
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);
323 
324     // Common case is that we have plenty of space.
325     if (BytesUsed <= BufferBytesLeft) {
326       OutBufCur += BytesUsed;
327       return *this;
328     }
329 
330     // Otherwise, we overflowed and the return value tells us the size to try
331     // again with.
332     NextBufferSize = BytesUsed;
333   }
334 
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;
339 
340   while (true) {
341     V.resize(NextBufferSize);
342 
343     // Try formatting into the SmallVector.
344     size_t BytesUsed = Fmt.print(V.data(), NextBufferSize);
345 
346     // If BytesUsed fit into the vector, we win.
347     if (BytesUsed <= NextBufferSize)
348       return write(V.data(), BytesUsed);
349 
350     // Otherwise, try again with a new size.
351     assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
352     NextBufferSize = BytesUsed;
353   }
354 }
355 
356 raw_ostream &raw_ostream::operator<<(const formatv_object_base &Obj) {
357   Obj.format(*this);
358   return *this;
359 }
360 
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:
368       break;
369     case FormattedString::JustifyLeft:
370       RightIndent = Difference;
371       break;
372     case FormattedString::JustifyRight:
373       LeftIndent = Difference;
374       break;
375     case FormattedString::JustifyCenter:
376       LeftIndent = Difference / 2;
377       RightIndent = Difference - LeftIndent;
378       break;
379     }
380   }
381   indent(LeftIndent);
382   (*this) << FS.Str;
383   indent(RightIndent);
384   return *this;
385 }
386 
387 raw_ostream &raw_ostream::operator<<(const FormattedNumber &FN) {
388   if (FN.Hex) {
389     HexPrintStyle Style;
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;
396     else
397       Style = HexPrintStyle::Lower;
398     llvm::write_hex(*this, FN.HexValue, Style, FN.Width);
399   } else {
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());
405     (*this) << Buffer;
406   }
407   return *this;
408 }
409 
410 raw_ostream &raw_ostream::operator<<(const FormattedBytes &FB) {
411   if (FB.Bytes.empty())
412     return *this;
413 
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;
425     unsigned Power = 0;
426     if (MaxOffset > 0)
427       Power = llvm::Log2_64_Ceil(MaxOffset);
428     OffsetWidth = std::max<uint64_t>(4, llvm::alignTo(Power, 4) / 4);
429   }
430 
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;
435 
436   while (!Bytes.empty()) {
437     indent(FB.IndentLevel);
438 
439     if (FB.FirstByteOffset) {
440       uint64_t Offset = *FB.FirstByteOffset;
441       llvm::write_hex(*this, Offset + LineIndex, HPS, OffsetWidth);
442       *this << ": ";
443     }
444 
445     auto Line = Bytes.take_front(FB.NumPerLine);
446 
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) {
451         ++CharsPrinted;
452         *this << " ";
453       }
454       llvm::write_hex(*this, Line[I], HPS, 2);
455     }
456 
457     if (FB.ASCII) {
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);
462       *this << "|";
463 
464       // Print the ASCII char values for each byte on this line
465       for (uint8_t Byte : Line) {
466         if (isPrint(Byte))
467           *this << static_cast<char>(Byte);
468         else
469           *this << '.';
470       }
471       *this << '|';
472     }
473 
474     Bytes = Bytes.drop_front(Line.size());
475     LineIndex += Line.size();
476     if (LineIndex < Size)
477       *this << '\n';
478   }
479   return *this;
480 }
481 
482 template <char C>
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};
489 
490   // Usually the indentation is small, handle it with a fastpath.
491   if (NumChars < std::size(Chars))
492     return OS.write(Chars, NumChars);
493 
494   while (NumChars) {
495     unsigned NumToWrite = std::min(NumChars, (unsigned)std::size(Chars) - 1);
496     OS.write(Chars, NumToWrite);
497     NumChars -= NumToWrite;
498   }
499   return OS;
500 }
501 
502 /// indent - Insert 'NumSpaces' spaces.
503 raw_ostream &raw_ostream::indent(unsigned NumSpaces) {
504   return write_padding<' '>(*this, NumSpaces);
505 }
506 
507 /// write_zeros - Insert 'NumZeros' nulls.
508 raw_ostream &raw_ostream::write_zeros(unsigned NumZeros) {
509   return write_padding<'\0'>(*this, NumZeros);
510 }
511 
512 bool raw_ostream::prepare_colors() {
513   // Colors were explicitly disabled.
514   if (!ColorEnabled)
515     return false;
516 
517   // Colors require changing the terminal but this stream is not going to a
518   // terminal.
519   if (sys::Process::ColorNeedsFlush() && !is_displayed())
520     return false;
521 
522   if (sys::Process::ColorNeedsFlush())
523     flush();
524 
525   return true;
526 }
527 
528 raw_ostream &raw_ostream::changeColor(enum Colors colors, bool bold, bool bg) {
529   if (!prepare_colors())
530     return *this;
531 
532   const char *colorcode =
533       (colors == SAVEDCOLOR)
534           ? sys::Process::OutputBold(bg)
535           : sys::Process::OutputColor(static_cast<char>(colors), bold, bg);
536   if (colorcode)
537     write(colorcode, strlen(colorcode));
538   return *this;
539 }
540 
541 raw_ostream &raw_ostream::resetColor() {
542   if (!prepare_colors())
543     return *this;
544 
545   if (const char *colorcode = sys::Process::ResetColor())
546     write(colorcode, strlen(colorcode));
547   return *this;
548 }
549 
550 raw_ostream &raw_ostream::reverseColor() {
551   if (!prepare_colors())
552     return *this;
553 
554   if (const char *colorcode = sys::Process::OutputReverse())
555     write(colorcode, strlen(colorcode));
556   return *this;
557 }
558 
559 void raw_ostream::anchor() {}
560 
561 //===----------------------------------------------------------------------===//
562 //  Formatted Output
563 //===----------------------------------------------------------------------===//
564 
565 // Out of line virtual method.
566 void format_object_base::home() {
567 }
568 
569 //===----------------------------------------------------------------------===//
570 //  raw_fd_ostream
571 //===----------------------------------------------------------------------===//
572 
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!");
578 
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;
586   }
587 
588   int FD;
589   if (Access & sys::fs::FA_Read)
590     EC = sys::fs::openFileForReadWrite(Filename, FD, Disp, Flags);
591   else
592     EC = sys::fs::openFileForWrite(Filename, FD, Disp, Flags);
593   if (EC)
594     return -1;
595 
596   return FD;
597 }
598 
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,
601                      sys::fs::OF_None) {}
602 
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) {}
606 
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,
610                      sys::fs::OF_None) {}
611 
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,
615                      Flags) {}
616 
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) {}
622 
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,
626                                OStreamKind K)
627     : raw_pwrite_stream(unbuffered, K), FD(fd), ShouldClose(shouldClose) {
628   if (FD < 0 ) {
629     ShouldClose = false;
630     return;
631   }
632 
633   enable_colors(true);
634 
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)
642     ShouldClose = false;
643 
644 #ifdef _WIN32
645   // Check if this is a console device. This is not equivalent to isatty.
646   IsWindowsConsole =
647       ::GetFileType((HANDLE)::_get_osfhandle(fd)) == FILE_TYPE_CHAR;
648 #endif
649 
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;
655 #ifdef _WIN32
656   // MSVCRT's _lseek(SEEK_CUR) doesn't return -1 for pipes.
657   SupportsSeeking = !EC && IsRegularFile;
658 #else
659   SupportsSeeking = !EC && loc != (off_t)-1;
660 #endif
661   if (!SupportsSeeking)
662     pos = 0;
663   else
664     pos = static_cast<uint64_t>(loc);
665 }
666 
667 raw_fd_ostream::~raw_fd_ostream() {
668   if (FD >= 0) {
669     flush();
670     if (ShouldClose) {
671       if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
672         error_detected(EC);
673     }
674   }
675 
676 #ifdef __MINGW32__
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
680   // on FD == 2.
681   if (FD == 2) return;
682 #endif
683 
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.
688   if (has_error())
689     report_fatal_error(Twine("IO failure on output stream: ") +
690                            error().message(),
691                        /*gen_crash_diag=*/false);
692 }
693 
694 #if defined(_WIN32)
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.
703 //
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;
711 
712   // Fall back to ::write if it wasn't valid UTF-8.
713   if (auto EC = sys::windows::UTF8ToUTF16(Data, WideText))
714     return false;
715 
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;
721 
722   size_t WCharsWritten = 0;
723   do {
724     size_t WCharsToWrite =
725         std::min(MaxWriteSize, WideText.size() - WCharsWritten);
726     DWORD ActuallyWritten;
727     bool Success =
728         ::WriteConsoleW((HANDLE)::_get_osfhandle(FD), &WideText[WCharsWritten],
729                         WCharsToWrite, &ActuallyWritten,
730                         /*Reserved=*/nullptr);
731 
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.
735     if (!Success)
736       return false;
737 
738     WCharsWritten += ActuallyWritten;
739   } while (WCharsWritten != WideText.size());
740   return true;
741 }
742 #endif
743 
744 void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
745   assert(FD >= 0 && "File already closed.");
746   pos += Size;
747 
748 #if defined(_WIN32)
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)))
753       return;
754 #endif
755 
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;
760 
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;
765 #endif
766 
767   do {
768     size_t ChunkSize = std::min(Size, MaxWriteSize);
769     ssize_t ret = ::write(FD, Ptr, ChunkSize);
770 
771     if (ret < 0) {
772       // If it's a recoverable error, swallow it and retry the write.
773       //
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
781 #ifdef EWOULDBLOCK
782           || errno == EWOULDBLOCK
783 #endif
784           )
785         continue;
786 
787 #ifdef _WIN32
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();
793         errno = EPIPE;
794       }
795 #endif
796       // Otherwise it's a non-recoverable error. Note it and quit.
797       error_detected(std::error_code(errno, std::generic_category()));
798       break;
799     }
800 
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.
804     Ptr += ret;
805     Size -= ret;
806   } while (Size > 0);
807 }
808 
809 void raw_fd_ostream::close() {
810   assert(ShouldClose);
811   ShouldClose = false;
812   flush();
813   if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
814     error_detected(EC);
815   FD = -1;
816 }
817 
818 uint64_t raw_fd_ostream::seek(uint64_t off) {
819   assert(SupportsSeeking && "Stream does not support seeking!");
820   flush();
821 #ifdef _WIN32
822   pos = ::_lseeki64(FD, off, SEEK_SET);
823 #else
824   pos = ::lseek(FD, off, SEEK_SET);
825 #endif
826   if (pos == (uint64_t)-1)
827     error_detected(std::error_code(errno, std::generic_category()));
828   return pos;
829 }
830 
831 void raw_fd_ostream::pwrite_impl(const char *Ptr, size_t Size,
832                                  uint64_t Offset) {
833   uint64_t Pos = tell();
834   seek(Offset);
835   write(Ptr, Size);
836   seek(Pos);
837 }
838 
839 size_t raw_fd_ostream::preferred_buffer_size() const {
840 #if defined(_WIN32)
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
845   // complexity.
846   if (IsWindowsConsole)
847     return 0;
848   return raw_ostream::preferred_buffer_size();
849 #else
850   assert(FD >= 0 && "File not yet open!");
851   struct stat statbuf;
852   if (fstat(FD, &statbuf) != 0)
853     return 0;
854 
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
857   // the complexity.
858   if (S_ISCHR(statbuf.st_mode) && is_displayed())
859     return 0;
860   // Return the preferred block size.
861   return statbuf.st_blksize;
862 #endif
863 }
864 
865 bool raw_fd_ostream::is_displayed() const {
866   return sys::Process::FileDescriptorIsDisplayed(FD);
867 }
868 
869 bool raw_fd_ostream::has_colors() const {
870   if (!HasColors)
871     HasColors = sys::Process::FileDescriptorHasColors(FD);
872   return *HasColors;
873 }
874 
875 Expected<sys::fs::FileLocker> raw_fd_ostream::lock() {
876   std::error_code EC = sys::fs::lockFile(FD);
877   if (!EC)
878     return sys::fs::FileLocker(FD);
879   return errorCodeToError(EC);
880 }
881 
882 Expected<sys::fs::FileLocker>
883 raw_fd_ostream::tryLockFor(Duration const& Timeout) {
884   std::error_code EC = sys::fs::tryLockFile(FD, Timeout.getDuration());
885   if (!EC)
886     return sys::fs::FileLocker(FD);
887   return errorCodeToError(EC);
888 }
889 
890 void raw_fd_ostream::anchor() {}
891 
892 //===----------------------------------------------------------------------===//
893 //  outs(), errs(), nulls()
894 //===----------------------------------------------------------------------===//
895 
896 raw_fd_ostream &llvm::outs() {
897   // Set buffer settings to model stdout behavior.
898   std::error_code EC;
899 #ifdef __MVS__
900   EC = enableAutoConversion(STDOUT_FILENO);
901   assert(!EC);
902 #endif
903   static raw_fd_ostream S("-", EC, sys::fs::OF_None);
904   assert(!EC);
905   return S;
906 }
907 
908 raw_fd_ostream &llvm::errs() {
909   // Set standard error to be unbuffered and tied to outs() by default.
910 #ifdef __MVS__
911   std::error_code EC = enableAutoConversion(STDERR_FILENO);
912   assert(!EC);
913 #endif
914   static raw_fd_ostream S(STDERR_FILENO, false, true);
915   return S;
916 }
917 
918 /// nulls() - This returns a reference to a raw_ostream which discards output.
919 raw_ostream &llvm::nulls() {
920   static raw_null_ostream S;
921   return S;
922 }
923 
924 //===----------------------------------------------------------------------===//
925 // File Streams
926 //===----------------------------------------------------------------------===//
927 
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,
931                            sys::fs::OF_None),
932                      true, false, OStreamKind::OK_FDStream) {
933   if (EC)
934     return;
935 
936   if (!isRegularFile())
937     EC = std::make_error_code(std::errc::invalid_argument);
938 }
939 
940 raw_fd_stream::raw_fd_stream(int fd, bool shouldClose)
941     : raw_fd_ostream(fd, shouldClose, false, OStreamKind::OK_FDStream) {}
942 
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);
946   if (Ret >= 0)
947     inc_pos(Ret);
948   else
949     error_detected(std::error_code(errno, std::generic_category()));
950   return Ret;
951 }
952 
953 bool raw_fd_stream::classof(const raw_ostream *OS) {
954   return OS->get_kind() == OStreamKind::OK_FDStream;
955 }
956 
957 //===----------------------------------------------------------------------===//
958 //  raw_svector_ostream
959 //===----------------------------------------------------------------------===//
960 
961 uint64_t raw_svector_ostream::current_pos() const { return OS.size(); }
962 
963 void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
964   OS.append(Ptr, Ptr + Size);
965 }
966 
967 void raw_svector_ostream::pwrite_impl(const char *Ptr, size_t Size,
968                                       uint64_t Offset) {
969   memcpy(OS.data() + Offset, Ptr, Size);
970 }
971 
972 //===----------------------------------------------------------------------===//
973 //  raw_null_ostream
974 //===----------------------------------------------------------------------===//
975 
976 raw_null_ostream::~raw_null_ostream() {
977 #ifndef NDEBUG
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.
981   flush();
982 #endif
983 }
984 
985 void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
986 }
987 
988 uint64_t raw_null_ostream::current_pos() const {
989   return 0;
990 }
991 
992 void raw_null_ostream::pwrite_impl(const char *Ptr, size_t Size,
993                                    uint64_t Offset) {}
994 
995 void raw_pwrite_stream::anchor() {}
996 
997 void buffer_ostream::anchor() {}
998 
999 void buffer_unique_ostream::anchor() {}
1000 
1001 Error llvm::writeToOutput(StringRef OutputFileName,
1002                           std::function<Error(raw_ostream &)> Write) {
1003   if (OutputFileName == "-")
1004     return Write(outs());
1005 
1006   if (OutputFileName == "/dev/null") {
1007     raw_null_ostream Out;
1008     return Write(Out);
1009   }
1010 
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);
1014   if (!Temp)
1015     return createFileError(OutputFileName, Temp.takeError());
1016 
1017   raw_fd_ostream Out(Temp->FD, false);
1018 
1019   if (Error E = Write(Out)) {
1020     if (Error DiscardError = Temp->discard())
1021       return joinErrors(std::move(E), std::move(DiscardError));
1022     return E;
1023   }
1024   Out.flush();
1025 
1026   return Temp->keep(OutputFileName);
1027 }
1028