LLVM  9.0.0svn
raw_ostream.cpp
Go to the documentation of this file.
1 //===--- raw_ostream.cpp - Implement the raw_ostream classes --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This implements support for bulk buffered stream output.
10 //
11 //===----------------------------------------------------------------------===//
12 
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"
21 #include "llvm/Support/Format.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
63 #include "Windows/WindowsSupport.h"
64 #endif
65 
66 using namespace llvm;
67 
69  // raw_ostream's subclasses should take care to flush the buffer
70  // in their destructors.
71  assert(OutBufCur == OutBufStart &&
72  "raw_ostream destructor called with non-empty buffer!");
73 
74  if (BufferMode == InternalBuffer)
75  delete [] OutBufStart;
76 }
77 
79  // BUFSIZ is intended to be a reasonable default.
80  return BUFSIZ;
81 }
82 
84  // Ask the subclass to determine an appropriate buffer size.
85  if (size_t Size = preferred_buffer_size())
87  else
88  // It may return 0, meaning this stream should be unbuffered.
89  SetUnbuffered();
90 }
91 
92 void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size,
93  BufferKind Mode) {
94  assert(((Mode == Unbuffered && !BufferStart && Size == 0) ||
95  (Mode != Unbuffered && BufferStart && Size != 0)) &&
96  "stream must be unbuffered or have at least one byte");
97  // Make sure the current buffer is free of content (we can't flush here; the
98  // child buffer management logic will be in write_impl).
99  assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!");
100 
101  if (BufferMode == InternalBuffer)
102  delete [] OutBufStart;
103  OutBufStart = BufferStart;
104  OutBufEnd = OutBufStart+Size;
105  OutBufCur = OutBufStart;
106  BufferMode = Mode;
107 
108  assert(OutBufStart <= OutBufEnd && "Invalid size!");
109 }
110 
112  write_integer(*this, static_cast<uint64_t>(N), 0, IntegerStyle::Integer);
113  return *this;
114 }
115 
117  write_integer(*this, static_cast<int64_t>(N), 0, IntegerStyle::Integer);
118  return *this;
119 }
120 
121 raw_ostream &raw_ostream::operator<<(unsigned long long N) {
122  write_integer(*this, static_cast<uint64_t>(N), 0, IntegerStyle::Integer);
123  return *this;
124 }
125 
127  write_integer(*this, static_cast<int64_t>(N), 0, IntegerStyle::Integer);
128  return *this;
129 }
130 
131 raw_ostream &raw_ostream::write_hex(unsigned long long N) {
133  return *this;
134 }
135 
137  for (int Idx = 0; Idx < 16; ++Idx) {
138  *this << format("%02" PRIX32, UUID[Idx]);
139  if (Idx == 3 || Idx == 5 || Idx == 7 || Idx == 9)
140  *this << "-";
141  }
142  return *this;
143 }
144 
145 
147  bool UseHexEscapes) {
148  for (unsigned char c : Str) {
149  switch (c) {
150  case '\\':
151  *this << '\\' << '\\';
152  break;
153  case '\t':
154  *this << '\\' << 't';
155  break;
156  case '\n':
157  *this << '\\' << 'n';
158  break;
159  case '"':
160  *this << '\\' << '"';
161  break;
162  default:
163  if (isPrint(c)) {
164  *this << c;
165  break;
166  }
167 
168  // Write out the escaped representation.
169  if (UseHexEscapes) {
170  *this << '\\' << 'x';
171  *this << hexdigit((c >> 4 & 0xF));
172  *this << hexdigit((c >> 0) & 0xF);
173  } else {
174  // Always use a full 3-character octal escape.
175  *this << '\\';
176  *this << char('0' + ((c >> 6) & 7));
177  *this << char('0' + ((c >> 3) & 7));
178  *this << char('0' + ((c >> 0) & 7));
179  }
180  }
181  }
182 
183  return *this;
184 }
185 
187  llvm::write_hex(*this, (uintptr_t)P, HexPrintStyle::PrefixLower);
188  return *this;
189 }
190 
193  return *this;
194 }
195 
196 void raw_ostream::flush_nonempty() {
197  assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
198  size_t Length = OutBufCur - OutBufStart;
199  OutBufCur = OutBufStart;
200  write_impl(OutBufStart, Length);
201 }
202 
204  // Group exceptional cases into a single branch.
205  if (LLVM_UNLIKELY(OutBufCur >= OutBufEnd)) {
206  if (LLVM_UNLIKELY(!OutBufStart)) {
207  if (BufferMode == Unbuffered) {
208  write_impl(reinterpret_cast<char*>(&C), 1);
209  return *this;
210  }
211  // Set up a buffer and start over.
212  SetBuffered();
213  return write(C);
214  }
215 
216  flush_nonempty();
217  }
218 
219  *OutBufCur++ = C;
220  return *this;
221 }
222 
223 raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
224  // Group exceptional cases into a single branch.
225  if (LLVM_UNLIKELY(size_t(OutBufEnd - OutBufCur) < Size)) {
226  if (LLVM_UNLIKELY(!OutBufStart)) {
227  if (BufferMode == Unbuffered) {
228  write_impl(Ptr, Size);
229  return *this;
230  }
231  // Set up a buffer and start over.
232  SetBuffered();
233  return write(Ptr, Size);
234  }
235 
236  size_t NumBytes = OutBufEnd - OutBufCur;
237 
238  // If the buffer is empty at this point we have a string that is larger
239  // than the buffer. Directly write the chunk that is a multiple of the
240  // preferred buffer size and put the remainder in the buffer.
241  if (LLVM_UNLIKELY(OutBufCur == OutBufStart)) {
242  assert(NumBytes != 0 && "undefined behavior");
243  size_t BytesToWrite = Size - (Size % NumBytes);
244  write_impl(Ptr, BytesToWrite);
245  size_t BytesRemaining = Size - BytesToWrite;
246  if (BytesRemaining > size_t(OutBufEnd - OutBufCur)) {
247  // Too much left over to copy into our buffer.
248  return write(Ptr + BytesToWrite, BytesRemaining);
249  }
250  copy_to_buffer(Ptr + BytesToWrite, BytesRemaining);
251  return *this;
252  }
253 
254  // We don't have enough space in the buffer to fit the string in. Insert as
255  // much as possible, flush and start over with the remainder.
256  copy_to_buffer(Ptr, NumBytes);
257  flush_nonempty();
258  return write(Ptr + NumBytes, Size - NumBytes);
259  }
260 
261  copy_to_buffer(Ptr, Size);
262 
263  return *this;
264 }
265 
266 void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) {
267  assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!");
268 
269  // Handle short strings specially, memcpy isn't very good at very short
270  // strings.
271  switch (Size) {
272  case 4: OutBufCur[3] = Ptr[3]; LLVM_FALLTHROUGH;
273  case 3: OutBufCur[2] = Ptr[2]; LLVM_FALLTHROUGH;
274  case 2: OutBufCur[1] = Ptr[1]; LLVM_FALLTHROUGH;
275  case 1: OutBufCur[0] = Ptr[0]; LLVM_FALLTHROUGH;
276  case 0: break;
277  default:
278  memcpy(OutBufCur, Ptr, Size);
279  break;
280  }
281 
282  OutBufCur += Size;
283 }
284 
285 // Formatted output.
287  // If we have more than a few bytes left in our output buffer, try
288  // formatting directly onto its end.
289  size_t NextBufferSize = 127;
290  size_t BufferBytesLeft = OutBufEnd - OutBufCur;
291  if (BufferBytesLeft > 3) {
292  size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
293 
294  // Common case is that we have plenty of space.
295  if (BytesUsed <= BufferBytesLeft) {
296  OutBufCur += BytesUsed;
297  return *this;
298  }
299 
300  // Otherwise, we overflowed and the return value tells us the size to try
301  // again with.
302  NextBufferSize = BytesUsed;
303  }
304 
305  // If we got here, we didn't have enough space in the output buffer for the
306  // string. Try printing into a SmallVector that is resized to have enough
307  // space. Iterate until we win.
309 
310  while (true) {
311  V.resize(NextBufferSize);
312 
313  // Try formatting into the SmallVector.
314  size_t BytesUsed = Fmt.print(V.data(), NextBufferSize);
315 
316  // If BytesUsed fit into the vector, we win.
317  if (BytesUsed <= NextBufferSize)
318  return write(V.data(), BytesUsed);
319 
320  // Otherwise, try again with a new size.
321  assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
322  NextBufferSize = BytesUsed;
323  }
324 }
325 
328  Obj.format(*this);
329  return *this;
330 }
331 
333  if (FS.Str.size() >= FS.Width || FS.Justify == FormattedString::JustifyNone) {
334  this->operator<<(FS.Str);
335  return *this;
336  }
337  const size_t Difference = FS.Width - FS.Str.size();
338  switch (FS.Justify) {
340  this->operator<<(FS.Str);
341  this->indent(Difference);
342  break;
344  this->indent(Difference);
345  this->operator<<(FS.Str);
346  break;
348  int PadAmount = Difference / 2;
349  this->indent(PadAmount);
350  this->operator<<(FS.Str);
351  this->indent(Difference - PadAmount);
352  break;
353  }
354  default:
355  llvm_unreachable("Bad Justification");
356  }
357  return *this;
358 }
359 
361  if (FN.Hex) {
363  if (FN.Upper && FN.HexPrefix)
365  else if (FN.Upper && !FN.HexPrefix)
366  Style = HexPrintStyle::Upper;
367  else if (!FN.Upper && FN.HexPrefix)
369  else
370  Style = HexPrintStyle::Lower;
371  llvm::write_hex(*this, FN.HexValue, Style, FN.Width);
372  } else {
373  llvm::SmallString<16> Buffer;
374  llvm::raw_svector_ostream Stream(Buffer);
375  llvm::write_integer(Stream, FN.DecValue, 0, IntegerStyle::Integer);
376  if (Buffer.size() < FN.Width)
377  indent(FN.Width - Buffer.size());
378  (*this) << Buffer;
379  }
380  return *this;
381 }
382 
384  if (FB.Bytes.empty())
385  return *this;
386 
387  size_t LineIndex = 0;
388  auto Bytes = FB.Bytes;
389  const size_t Size = Bytes.size();
391  uint64_t OffsetWidth = 0;
392  if (FB.FirstByteOffset.hasValue()) {
393  // Figure out how many nibbles are needed to print the largest offset
394  // represented by this data set, so that we can align the offset field
395  // to the right width.
396  size_t Lines = Size / FB.NumPerLine;
397  uint64_t MaxOffset = *FB.FirstByteOffset + Lines * FB.NumPerLine;
398  unsigned Power = 0;
399  if (MaxOffset > 0)
400  Power = llvm::Log2_64_Ceil(MaxOffset);
401  OffsetWidth = std::max<uint64_t>(4, llvm::alignTo(Power, 4) / 4);
402  }
403 
404  // The width of a block of data including all spaces for group separators.
405  unsigned NumByteGroups =
406  alignTo(FB.NumPerLine, FB.ByteGroupSize) / FB.ByteGroupSize;
407  unsigned BlockCharWidth = FB.NumPerLine * 2 + NumByteGroups - 1;
408 
409  while (!Bytes.empty()) {
410  indent(FB.IndentLevel);
411 
412  if (FB.FirstByteOffset.hasValue()) {
413  uint64_t Offset = FB.FirstByteOffset.getValue();
414  llvm::write_hex(*this, Offset + LineIndex, HPS, OffsetWidth);
415  *this << ": ";
416  }
417 
418  auto Line = Bytes.take_front(FB.NumPerLine);
419 
420  size_t CharsPrinted = 0;
421  // Print the hex bytes for this line in groups
422  for (size_t I = 0; I < Line.size(); ++I, CharsPrinted += 2) {
423  if (I && (I % FB.ByteGroupSize) == 0) {
424  ++CharsPrinted;
425  *this << " ";
426  }
427  llvm::write_hex(*this, Line[I], HPS, 2);
428  }
429 
430  if (FB.ASCII) {
431  // Print any spaces needed for any bytes that we didn't print on this
432  // line so that the ASCII bytes are correctly aligned.
433  assert(BlockCharWidth >= CharsPrinted);
434  indent(BlockCharWidth - CharsPrinted + 2);
435  *this << "|";
436 
437  // Print the ASCII char values for each byte on this line
438  for (uint8_t Byte : Line) {
439  if (isPrint(Byte))
440  *this << static_cast<char>(Byte);
441  else
442  *this << '.';
443  }
444  *this << '|';
445  }
446 
447  Bytes = Bytes.drop_front(Line.size());
448  LineIndex += Line.size();
449  if (LineIndex < Size)
450  *this << '\n';
451  }
452  return *this;
453 }
454 
455 template <char C>
456 static raw_ostream &write_padding(raw_ostream &OS, unsigned NumChars) {
457  static const char Chars[] = {C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C,
458  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 
463  // Usually the indentation is small, handle it with a fastpath.
464  if (NumChars < array_lengthof(Chars))
465  return OS.write(Chars, NumChars);
466 
467  while (NumChars) {
468  unsigned NumToWrite = std::min(NumChars,
469  (unsigned)array_lengthof(Chars)-1);
470  OS.write(Chars, NumToWrite);
471  NumChars -= NumToWrite;
472  }
473  return OS;
474 }
475 
476 /// indent - Insert 'NumSpaces' spaces.
477 raw_ostream &raw_ostream::indent(unsigned NumSpaces) {
478  return write_padding<' '>(*this, NumSpaces);
479 }
480 
481 /// write_zeros - Insert 'NumZeros' nulls.
483  return write_padding<'\0'>(*this, NumZeros);
484 }
485 
486 void raw_ostream::anchor() {}
487 
488 //===----------------------------------------------------------------------===//
489 // Formatted Output
490 //===----------------------------------------------------------------------===//
491 
492 // Out of line virtual method.
494 }
495 
496 //===----------------------------------------------------------------------===//
497 // raw_fd_ostream
498 //===----------------------------------------------------------------------===//
499 
500 static int getFD(StringRef Filename, std::error_code &EC,
502  sys::fs::OpenFlags Flags) {
503  assert((Access & sys::fs::FA_Write) &&
504  "Cannot make a raw_ostream from a read-only descriptor!");
505 
506  // Handle "-" as stdout. Note that when we do this, we consider ourself
507  // the owner of stdout and may set the "binary" flag globally based on Flags.
508  if (Filename == "-") {
509  EC = std::error_code();
510  // If user requested binary then put stdout into binary mode if
511  // possible.
512  if (!(Flags & sys::fs::OF_Text))
514  return STDOUT_FILENO;
515  }
516 
517  int FD;
518  if (Access & sys::fs::FA_Read)
519  EC = sys::fs::openFileForReadWrite(Filename, FD, Disp, Flags);
520  else
521  EC = sys::fs::openFileForWrite(Filename, FD, Disp, Flags);
522  if (EC)
523  return -1;
524 
525  return FD;
526 }
527 
528 raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC)
529  : raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, sys::fs::FA_Write,
530  sys::fs::OF_None) {}
531 
532 raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
534  : raw_fd_ostream(Filename, EC, Disp, sys::fs::FA_Write, sys::fs::OF_None) {}
535 
536 raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
537  sys::fs::FileAccess Access)
538  : raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, Access,
539  sys::fs::OF_None) {}
540 
541 raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
542  sys::fs::OpenFlags Flags)
543  : raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, sys::fs::FA_Write,
544  Flags) {}
545 
546 raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
548  sys::fs::FileAccess Access,
549  sys::fs::OpenFlags Flags)
550  : raw_fd_ostream(getFD(Filename, EC, Disp, Access, Flags), true) {}
551 
552 /// FD is the file descriptor that this writes to. If ShouldClose is true, this
553 /// closes the file when the stream is destroyed.
554 raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered)
555  : raw_pwrite_stream(unbuffered), FD(fd), ShouldClose(shouldClose) {
556  if (FD < 0 ) {
557  ShouldClose = false;
558  return;
559  }
560 
561  // Do not attempt to close stdout or stderr. We used to try to maintain the
562  // property that tools that support writing file to stdout should not also
563  // write informational output to stdout, but in practice we were never able to
564  // maintain this invariant. Many features have been added to LLVM and clang
565  // (-fdump-record-layouts, optimization remarks, etc) that print to stdout, so
566  // users must simply be aware that mixed output and remarks is a possibility.
567  if (FD <= STDERR_FILENO)
568  ShouldClose = false;
569 
570 #ifdef _WIN32
571  // Check if this is a console device. This is not equivalent to isatty.
572  IsWindowsConsole =
573  ::GetFileType((HANDLE)::_get_osfhandle(fd)) == FILE_TYPE_CHAR;
574 #endif
575 
576  // Get the starting position.
577  off_t loc = ::lseek(FD, 0, SEEK_CUR);
578 #ifdef _WIN32
579  // MSVCRT's _lseek(SEEK_CUR) doesn't return -1 for pipes.
581  std::error_code EC = status(FD, Status);
582  SupportsSeeking = !EC && Status.type() == sys::fs::file_type::regular_file;
583 #else
584  SupportsSeeking = loc != (off_t)-1;
585 #endif
586  if (!SupportsSeeking)
587  pos = 0;
588  else
589  pos = static_cast<uint64_t>(loc);
590 }
591 
593  if (FD >= 0) {
594  flush();
595  if (ShouldClose) {
596  if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
597  error_detected(EC);
598  }
599  }
600 
601 #ifdef __MINGW32__
602  // On mingw, global dtors should not call exit().
603  // report_fatal_error() invokes exit(). We know report_fatal_error()
604  // might not write messages to stderr when any errors were detected
605  // on FD == 2.
606  if (FD == 2) return;
607 #endif
608 
609  // If there are any pending errors, report them now. Clients wishing
610  // to avoid report_fatal_error calls should check for errors with
611  // has_error() and clear the error flag with clear_error() before
612  // destructing raw_ostream objects which may have errors.
613  if (has_error())
614  report_fatal_error("IO failure on output stream: " + error().message(),
615  /*GenCrashDiag=*/false);
616 }
617 
618 #if defined(_WIN32)
619 // The most reliable way to print unicode in a Windows console is with
620 // WriteConsoleW. To use that, first transcode from UTF-8 to UTF-16. This
621 // assumes that LLVM programs always print valid UTF-8 to the console. The data
622 // might not be UTF-8 for two major reasons:
623 // 1. The program is printing binary (-filetype=obj -o -), in which case it
624 // would have been gibberish anyway.
625 // 2. The program is printing text in a semi-ascii compatible codepage like
626 // shift-jis or cp1252.
627 //
628 // Most LLVM programs don't produce non-ascii text unless they are quoting
629 // user source input. A well-behaved LLVM program should either validate that
630 // the input is UTF-8 or transcode from the local codepage to UTF-8 before
631 // quoting it. If they don't, this may mess up the encoding, but this is still
632 // probably the best compromise we can make.
633 static bool write_console_impl(int FD, StringRef Data) {
634  SmallVector<wchar_t, 256> WideText;
635 
636  // Fall back to ::write if it wasn't valid UTF-8.
637  if (auto EC = sys::windows::UTF8ToUTF16(Data, WideText))
638  return false;
639 
640  // On Windows 7 and earlier, WriteConsoleW has a low maximum amount of data
641  // that can be written to the console at a time.
642  size_t MaxWriteSize = WideText.size();
644  MaxWriteSize = 32767;
645 
646  size_t WCharsWritten = 0;
647  do {
648  size_t WCharsToWrite =
649  std::min(MaxWriteSize, WideText.size() - WCharsWritten);
650  DWORD ActuallyWritten;
651  bool Success =
652  ::WriteConsoleW((HANDLE)::_get_osfhandle(FD), &WideText[WCharsWritten],
653  WCharsToWrite, &ActuallyWritten,
654  /*Reserved=*/nullptr);
655 
656  // The most likely reason for WriteConsoleW to fail is that FD no longer
657  // points to a console. Fall back to ::write. If this isn't the first loop
658  // iteration, something is truly wrong.
659  if (!Success)
660  return false;
661 
662  WCharsWritten += ActuallyWritten;
663  } while (WCharsWritten != WideText.size());
664  return true;
665 }
666 #endif
667 
668 void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
669  assert(FD >= 0 && "File already closed.");
670  pos += Size;
671 
672 #if defined(_WIN32)
673  // If this is a Windows console device, try re-encoding from UTF-8 to UTF-16
674  // and using WriteConsoleW. If that fails, fall back to plain write().
675  if (IsWindowsConsole)
676  if (write_console_impl(FD, StringRef(Ptr, Size)))
677  return;
678 #endif
679 
680  // The maximum write size is limited to INT32_MAX. A write
681  // greater than SSIZE_MAX is implementation-defined in POSIX,
682  // and Windows _write requires 32 bit input.
683  size_t MaxWriteSize = INT32_MAX;
684 
685 #if defined(__linux__)
686  // It is observed that Linux returns EINVAL for a very large write (>2G).
687  // Make it a reasonably small value.
688  MaxWriteSize = 1024 * 1024 * 1024;
689 #endif
690 
691  do {
692  size_t ChunkSize = std::min(Size, MaxWriteSize);
693  ssize_t ret = ::write(FD, Ptr, ChunkSize);
694 
695  if (ret < 0) {
696  // If it's a recoverable error, swallow it and retry the write.
697  //
698  // Ideally we wouldn't ever see EAGAIN or EWOULDBLOCK here, since
699  // raw_ostream isn't designed to do non-blocking I/O. However, some
700  // programs, such as old versions of bjam, have mistakenly used
701  // O_NONBLOCK. For compatibility, emulate blocking semantics by
702  // spinning until the write succeeds. If you don't want spinning,
703  // don't use O_NONBLOCK file descriptors with raw_ostream.
704  if (errno == EINTR || errno == EAGAIN
705 #ifdef EWOULDBLOCK
706  || errno == EWOULDBLOCK
707 #endif
708  )
709  continue;
710 
711  // Otherwise it's a non-recoverable error. Note it and quit.
712  error_detected(std::error_code(errno, std::generic_category()));
713  break;
714  }
715 
716  // The write may have written some or all of the data. Update the
717  // size and buffer pointer to reflect the remainder that needs
718  // to be written. If there are no bytes left, we're done.
719  Ptr += ret;
720  Size -= ret;
721  } while (Size > 0);
722 }
723 
725  assert(ShouldClose);
726  ShouldClose = false;
727  flush();
728  if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
729  error_detected(EC);
730  FD = -1;
731 }
732 
733 uint64_t raw_fd_ostream::seek(uint64_t off) {
734  assert(SupportsSeeking && "Stream does not support seeking!");
735  flush();
736 #ifdef _WIN32
737  pos = ::_lseeki64(FD, off, SEEK_SET);
738 #elif defined(HAVE_LSEEK64)
739  pos = ::lseek64(FD, off, SEEK_SET);
740 #else
741  pos = ::lseek(FD, off, SEEK_SET);
742 #endif
743  if (pos == (uint64_t)-1)
744  error_detected(std::error_code(errno, std::generic_category()));
745  return pos;
746 }
747 
748 void raw_fd_ostream::pwrite_impl(const char *Ptr, size_t Size,
749  uint64_t Offset) {
750  uint64_t Pos = tell();
751  seek(Offset);
752  write(Ptr, Size);
753  seek(Pos);
754 }
755 
756 size_t raw_fd_ostream::preferred_buffer_size() const {
757 #if defined(_WIN32)
758  // Disable buffering for console devices. Console output is re-encoded from
759  // UTF-8 to UTF-16 on Windows, and buffering it would require us to split the
760  // buffer on a valid UTF-8 codepoint boundary. Terminal buffering is disabled
761  // below on most other OSs, so do the same thing on Windows and avoid that
762  // complexity.
763  if (IsWindowsConsole)
764  return 0;
766 #elif !defined(__minix)
767  // Minix has no st_blksize.
768  assert(FD >= 0 && "File not yet open!");
769  struct stat statbuf;
770  if (fstat(FD, &statbuf) != 0)
771  return 0;
772 
773  // If this is a terminal, don't use buffering. Line buffering
774  // would be a more traditional thing to do, but it's not worth
775  // the complexity.
776  if (S_ISCHR(statbuf.st_mode) && isatty(FD))
777  return 0;
778  // Return the preferred block size.
779  return statbuf.st_blksize;
780 #else
782 #endif
783 }
784 
786  bool bg) {
788  flush();
789  const char *colorcode =
790  (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
791  : sys::Process::OutputColor(colors, bold, bg);
792  if (colorcode) {
793  size_t len = strlen(colorcode);
794  write(colorcode, len);
795  // don't account colors towards output characters
796  pos -= len;
797  }
798  return *this;
799 }
800 
803  flush();
804  const char *colorcode = sys::Process::ResetColor();
805  if (colorcode) {
806  size_t len = strlen(colorcode);
807  write(colorcode, len);
808  // don't account colors towards output characters
809  pos -= len;
810  }
811  return *this;
812 }
813 
816  flush();
817  const char *colorcode = sys::Process::OutputReverse();
818  if (colorcode) {
819  size_t len = strlen(colorcode);
820  write(colorcode, len);
821  // don't account colors towards output characters
822  pos -= len;
823  }
824  return *this;
825 }
826 
829 }
830 
833 }
834 
835 void raw_fd_ostream::anchor() {}
836 
837 //===----------------------------------------------------------------------===//
838 // outs(), errs(), nulls()
839 //===----------------------------------------------------------------------===//
840 
841 /// outs() - This returns a reference to a raw_ostream for standard output.
842 /// Use it like: outs() << "foo" << "bar";
844  // Set buffer settings to model stdout behavior.
845  std::error_code EC;
846  static raw_fd_ostream S("-", EC, sys::fs::F_None);
847  assert(!EC);
848  return S;
849 }
850 
851 /// errs() - This returns a reference to a raw_ostream for standard error.
852 /// Use it like: errs() << "foo" << "bar";
854  // Set standard error to be unbuffered by default.
855  static raw_fd_ostream S(STDERR_FILENO, false, true);
856  return S;
857 }
858 
859 /// nulls() - This returns a reference to a raw_ostream which discards output.
861  static raw_null_ostream S;
862  return S;
863 }
864 
865 //===----------------------------------------------------------------------===//
866 // raw_string_ostream
867 //===----------------------------------------------------------------------===//
868 
870  flush();
871 }
872 
873 void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
874  OS.append(Ptr, Size);
875 }
876 
877 //===----------------------------------------------------------------------===//
878 // raw_svector_ostream
879 //===----------------------------------------------------------------------===//
880 
881 uint64_t raw_svector_ostream::current_pos() const { return OS.size(); }
882 
883 void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
884  OS.append(Ptr, Ptr + Size);
885 }
886 
887 void raw_svector_ostream::pwrite_impl(const char *Ptr, size_t Size,
888  uint64_t Offset) {
889  memcpy(OS.data() + Offset, Ptr, Size);
890 }
891 
892 //===----------------------------------------------------------------------===//
893 // raw_null_ostream
894 //===----------------------------------------------------------------------===//
895 
897 #ifndef NDEBUG
898  // ~raw_ostream asserts that the buffer is empty. This isn't necessary
899  // with raw_null_ostream, but it's better to have raw_null_ostream follow
900  // the rules than to change the rules just for raw_null_ostream.
901  flush();
902 #endif
903 }
904 
905 void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
906 }
907 
908 uint64_t raw_null_ostream::current_pos() const {
909  return 0;
910 }
911 
912 void raw_null_ostream::pwrite_impl(const char *Ptr, size_t Size,
913  uint64_t Offset) {}
914 
915 void raw_pwrite_stream::anchor() {}
916 
917 void buffer_ostream::anchor() {}
uint64_t CallInst * C
bool has_colors() const override
This function determines if this stream is displayed and supports colors.
bool is_displayed() const override
This function determines if this stream is connected to a "tty" or "console" window.
std::error_code openFileForReadWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp, OpenFlags Flags, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
Definition: FileSystem.h:991
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
uint8_t[16] uuid_t
Output a formatted UUID with dash separators.
Definition: raw_ostream.h:218
bool isPrint(char C)
Checks whether character C is printable.
Definition: StringExtras.h:105
SI Whole Quad Mode
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static const char * ResetColor()
Resets the terminals colors, or returns an escape sequence to do so.
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:191
static const char * OutputColor(char c, bool bold, bool bg)
This function returns the colorcode escape sequences.
std::error_code ChangeStdoutToBinary()
A raw_ostream that discards all output.
Definition: raw_ostream.h:538
virtual ~raw_ostream()
Definition: raw_ostream.cpp:68
uint64_t seek(uint64_t off)
Flushes the stream and repositions the underlying file descriptor position to the offset specified fr...
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert &#39;NumZeros&#39; nulls.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:123
raw_ostream & indent(unsigned NumSpaces)
indent - Insert &#39;NumSpaces&#39; spaces.
static bool FileDescriptorHasColors(int fd)
This function determines if the given file descriptor is displayd and supports colors.
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:509
raw_fd_ostream(StringRef Filename, std::error_code &EC)
Open the specified file for writing.
static bool FileDescriptorIsDisplayed(int fd)
This function determines if the given file descriptor is connected to a "tty" or "console" window...
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:684
block Block Frequency true
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:246
std::error_code error() const
Definition: raw_ostream.h:445
raw_ostream & write_uuid(const uuid_t UUID)
void write_double(raw_ostream &S, double D, FloatStyle Style, Optional< size_t > Precision=None)
This is a helper class for left_justify, right_justify, and center_justify.
Definition: Format.h:128
static bool ColorNeedsFlush()
Whether changing colors requires the output to be flushed.
static const char * OutputReverse()
This function returns the escape sequence to reverse forground and background colors.
const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:162
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
~raw_fd_ostream() override
This is a helper class used for handling formatted output.
Definition: Format.h:37
The file should be opened in text mode on platforms that make this distinction.
Definition: FileSystem.h:749
raw_ostream & operator<<(char C)
Definition: raw_ostream.h:146
static raw_ostream & write_padding(raw_ostream &OS, unsigned NumChars)
raw_ostream & outs()
This returns a reference to a raw_ostream for standard output.
#define P(N)
raw_ostream & resetColor() override
Resets the colors to terminal defaults.
virtual size_t preferred_buffer_size() const
Return an efficient buffer size for the underlying output mechanism.
Definition: raw_ostream.cpp:78
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
void format(raw_ostream &S) const
void SetUnbuffered()
Set the stream to be unbuffered.
Definition: raw_ostream.h:128
std::error_code openFileForWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp=CD_CreateAlways, OpenFlags Flags=OF_None, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
Definition: FileSystem.h:950
bool RunningWindows8OrGreater()
Determines if the program is running on Windows 8 or newer.
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
Definition: raw_ostream.h:110
size_t size() const
Definition: SmallVector.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
~raw_null_ostream() override
bool has_error() const
Return the value of the flag in this raw_fd_ostream indicating whether an output error has been encou...
Definition: raw_ostream.h:451
raw_ostream & write(unsigned char C)
static int getFD(StringRef Filename, std::error_code &EC, sys::fs::CreationDisposition Disp, sys::fs::FileAccess Access, sys::fs::OpenFlags Flags)
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning &#39;\&#39;, &#39;&#39;, &#39; &#39;, &#39;"&#39;, and anything that doesn&#39;t satisfy llvm::isPrint into an escape...
This is a helper class used for format_hex() and format_decimal().
Definition: Format.h:163
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:839
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
Definition: STLExtras.h:1043
unsigned Log2_64_Ceil(uint64_t Value)
Return the ceil log base 2 of the specified value, 64 if the value is zero.
Definition: MathExtras.h:557
size_t GetNumBytesInBuffer() const
Definition: raw_ostream.h:133
void SetBuffered()
Set the stream to be buffered, with an automatically determined buffer size.
Definition: raw_ostream.cpp:83
#define Success
bool hasValue() const
Definition: Optional.h:166
Provides a library for accessing information about this process and other processes on the operating ...
void write_hex(raw_ostream &S, uint64_t N, HexPrintStyle Style, Optional< size_t > Width=None)
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:365
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:144
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
uint32_t Size
Definition: Profile.cpp:46
void close()
Manually flush the stream and close the file.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:340
static const char * OutputBold(bool bg)
Same as OutputColor, but only enables the bold attribute.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned print(char *Buffer, unsigned BufferSize) const
Format the object into the specified buffer.
Definition: Format.h:53
CD_CreateAlways - When opening a file:
Definition: FileSystem.h:720
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:250
raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
raw_ostream & changeColor(enum Colors colors, bool bold=false, bool bg=false) override
Changes the foreground color of text that will be output from this point forward. ...
static std::error_code SafelyCloseFileDescriptor(int FD)
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:99
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
void write_integer(raw_ostream &S, unsigned int N, size_t MinDigits, IntegerStyle Style)
raw_ostream & reverseColor() override
Reverses the foreground and background colors.
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:143
void resize(size_type N)
Definition: SmallVector.h:343