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