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