LCOV - code coverage report
Current view: top level - lib/Support - raw_ostream.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 344 379 90.8 %
Date: 2018-09-23 13:06:45 Functions: 55 64 85.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13