LLVM  6.0.0svn
raw_ostream.cpp
Go to the documentation of this file.
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 
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"
22 #include "llvm/Support/Format.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 #if defined(HAVE_SYS_UIO_H) && defined(HAVE_WRITEV)
45 # include <sys/uio.h>
46 #endif
47 
48 #if defined(__CYGWIN__)
49 #include <io.h>
50 #endif
51 
52 #if defined(_MSC_VER)
53 #include <io.h>
54 #ifndef STDIN_FILENO
55 # define STDIN_FILENO 0
56 #endif
57 #ifndef STDOUT_FILENO
58 # define STDOUT_FILENO 1
59 #endif
60 #ifndef STDERR_FILENO
61 # define STDERR_FILENO 2
62 #endif
63 #endif
64 
65 #ifdef LLVM_ON_WIN32
66 #include "Windows/WindowsSupport.h"
67 #endif
68 
69 using namespace llvm;
70 
72  // raw_ostream's subclasses should take care to flush the buffer
73  // in their destructors.
74  assert(OutBufCur == OutBufStart &&
75  "raw_ostream destructor called with non-empty buffer!");
76 
77  if (BufferMode == InternalBuffer)
78  delete [] OutBufStart;
79 }
80 
81 // An out of line virtual method to provide a home for the class vtable.
82 void raw_ostream::handle() {}
83 
85  // BUFSIZ is intended to be a reasonable default.
86  return BUFSIZ;
87 }
88 
90  // Ask the subclass to determine an appropriate buffer size.
91  if (size_t Size = preferred_buffer_size())
93  else
94  // It may return 0, meaning this stream should be unbuffered.
95  SetUnbuffered();
96 }
97 
98 void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size,
99  BufferKind Mode) {
100  assert(((Mode == Unbuffered && !BufferStart && Size == 0) ||
101  (Mode != Unbuffered && BufferStart && Size != 0)) &&
102  "stream must be unbuffered or have at least one byte");
103  // Make sure the current buffer is free of content (we can't flush here; the
104  // child buffer management logic will be in write_impl).
105  assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!");
106 
107  if (BufferMode == InternalBuffer)
108  delete [] OutBufStart;
109  OutBufStart = BufferStart;
110  OutBufEnd = OutBufStart+Size;
111  OutBufCur = OutBufStart;
112  BufferMode = Mode;
113 
114  assert(OutBufStart <= OutBufEnd && "Invalid size!");
115 }
116 
118  write_integer(*this, static_cast<uint64_t>(N), 0, IntegerStyle::Integer);
119  return *this;
120 }
121 
123  write_integer(*this, static_cast<int64_t>(N), 0, IntegerStyle::Integer);
124  return *this;
125 }
126 
127 raw_ostream &raw_ostream::operator<<(unsigned long long N) {
128  write_integer(*this, static_cast<uint64_t>(N), 0, IntegerStyle::Integer);
129  return *this;
130 }
131 
133  write_integer(*this, static_cast<int64_t>(N), 0, IntegerStyle::Integer);
134  return *this;
135 }
136 
137 raw_ostream &raw_ostream::write_hex(unsigned long long N) {
139  return *this;
140 }
141 
143  for (int Idx = 0; Idx < 16; ++Idx) {
144  *this << format("%02" PRIX32, UUID[Idx]);
145  if (Idx == 3 || Idx == 5 || Idx == 7 || Idx == 9)
146  *this << "-";
147  }
148  return *this;
149 }
150 
151 
153  bool UseHexEscapes) {
154  for (unsigned char c : Str) {
155  switch (c) {
156  case '\\':
157  *this << '\\' << '\\';
158  break;
159  case '\t':
160  *this << '\\' << 't';
161  break;
162  case '\n':
163  *this << '\\' << 'n';
164  break;
165  case '"':
166  *this << '\\' << '"';
167  break;
168  default:
169  if (std::isprint(c)) {
170  *this << c;
171  break;
172  }
173 
174  // Write out the escaped representation.
175  if (UseHexEscapes) {
176  *this << '\\' << 'x';
177  *this << hexdigit((c >> 4 & 0xF));
178  *this << hexdigit((c >> 0) & 0xF);
179  } else {
180  // Always use a full 3-character octal escape.
181  *this << '\\';
182  *this << char('0' + ((c >> 6) & 7));
183  *this << char('0' + ((c >> 3) & 7));
184  *this << char('0' + ((c >> 0) & 7));
185  }
186  }
187  }
188 
189  return *this;
190 }
191 
193  llvm::write_hex(*this, (uintptr_t)P, HexPrintStyle::PrefixLower);
194  return *this;
195 }
196 
199  return *this;
200 }
201 
202 void raw_ostream::flush_nonempty() {
203  assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
204  size_t Length = OutBufCur - OutBufStart;
205  OutBufCur = OutBufStart;
206  write_impl(OutBufStart, Length);
207 }
208 
210  // Group exceptional cases into a single branch.
211  if (LLVM_UNLIKELY(OutBufCur >= OutBufEnd)) {
212  if (LLVM_UNLIKELY(!OutBufStart)) {
213  if (BufferMode == Unbuffered) {
214  write_impl(reinterpret_cast<char*>(&C), 1);
215  return *this;
216  }
217  // Set up a buffer and start over.
218  SetBuffered();
219  return write(C);
220  }
221 
222  flush_nonempty();
223  }
224 
225  *OutBufCur++ = C;
226  return *this;
227 }
228 
229 raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
230  // Group exceptional cases into a single branch.
231  if (LLVM_UNLIKELY(size_t(OutBufEnd - OutBufCur) < Size)) {
232  if (LLVM_UNLIKELY(!OutBufStart)) {
233  if (BufferMode == Unbuffered) {
234  write_impl(Ptr, Size);
235  return *this;
236  }
237  // Set up a buffer and start over.
238  SetBuffered();
239  return write(Ptr, Size);
240  }
241 
242  size_t NumBytes = OutBufEnd - OutBufCur;
243 
244  // If the buffer is empty at this point we have a string that is larger
245  // than the buffer. Directly write the chunk that is a multiple of the
246  // preferred buffer size and put the remainder in the buffer.
247  if (LLVM_UNLIKELY(OutBufCur == OutBufStart)) {
248  assert(NumBytes != 0 && "undefined behavior");
249  size_t BytesToWrite = Size - (Size % NumBytes);
250  write_impl(Ptr, BytesToWrite);
251  size_t BytesRemaining = Size - BytesToWrite;
252  if (BytesRemaining > size_t(OutBufEnd - OutBufCur)) {
253  // Too much left over to copy into our buffer.
254  return write(Ptr + BytesToWrite, BytesRemaining);
255  }
256  copy_to_buffer(Ptr + BytesToWrite, BytesRemaining);
257  return *this;
258  }
259 
260  // We don't have enough space in the buffer to fit the string in. Insert as
261  // much as possible, flush and start over with the remainder.
262  copy_to_buffer(Ptr, NumBytes);
263  flush_nonempty();
264  return write(Ptr + NumBytes, Size - NumBytes);
265  }
266 
267  copy_to_buffer(Ptr, Size);
268 
269  return *this;
270 }
271 
272 void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) {
273  assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!");
274 
275  // Handle short strings specially, memcpy isn't very good at very short
276  // strings.
277  switch (Size) {
278  case 4: OutBufCur[3] = Ptr[3]; LLVM_FALLTHROUGH;
279  case 3: OutBufCur[2] = Ptr[2]; LLVM_FALLTHROUGH;
280  case 2: OutBufCur[1] = Ptr[1]; LLVM_FALLTHROUGH;
281  case 1: OutBufCur[0] = Ptr[0]; LLVM_FALLTHROUGH;
282  case 0: break;
283  default:
284  memcpy(OutBufCur, Ptr, Size);
285  break;
286  }
287 
288  OutBufCur += Size;
289 }
290 
291 // Formatted output.
293  // If we have more than a few bytes left in our output buffer, try
294  // formatting directly onto its end.
295  size_t NextBufferSize = 127;
296  size_t BufferBytesLeft = OutBufEnd - OutBufCur;
297  if (BufferBytesLeft > 3) {
298  size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
299 
300  // Common case is that we have plenty of space.
301  if (BytesUsed <= BufferBytesLeft) {
302  OutBufCur += BytesUsed;
303  return *this;
304  }
305 
306  // Otherwise, we overflowed and the return value tells us the size to try
307  // again with.
308  NextBufferSize = BytesUsed;
309  }
310 
311  // If we got here, we didn't have enough space in the output buffer for the
312  // string. Try printing into a SmallVector that is resized to have enough
313  // space. Iterate until we win.
315 
316  while (true) {
317  V.resize(NextBufferSize);
318 
319  // Try formatting into the SmallVector.
320  size_t BytesUsed = Fmt.print(V.data(), NextBufferSize);
321 
322  // If BytesUsed fit into the vector, we win.
323  if (BytesUsed <= NextBufferSize)
324  return write(V.data(), BytesUsed);
325 
326  // Otherwise, try again with a new size.
327  assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
328  NextBufferSize = BytesUsed;
329  }
330 }
331 
334  Obj.format(*this);
335  return *this;
336 }
337 
339  if (FS.Str.size() >= FS.Width || FS.Justify == FormattedString::JustifyNone) {
340  this->operator<<(FS.Str);
341  return *this;
342  }
343  const size_t Difference = FS.Width - FS.Str.size();
344  switch (FS.Justify) {
346  this->operator<<(FS.Str);
347  this->indent(Difference);
348  break;
350  this->indent(Difference);
351  this->operator<<(FS.Str);
352  break;
354  int PadAmount = Difference / 2;
355  this->indent(PadAmount);
356  this->operator<<(FS.Str);
357  this->indent(Difference - PadAmount);
358  break;
359  }
360  default:
361  llvm_unreachable("Bad Justification");
362  }
363  return *this;
364 }
365 
367  if (FN.Hex) {
369  if (FN.Upper && FN.HexPrefix)
371  else if (FN.Upper && !FN.HexPrefix)
372  Style = HexPrintStyle::Upper;
373  else if (!FN.Upper && FN.HexPrefix)
375  else
376  Style = HexPrintStyle::Lower;
377  llvm::write_hex(*this, FN.HexValue, Style, FN.Width);
378  } else {
379  llvm::SmallString<16> Buffer;
380  llvm::raw_svector_ostream Stream(Buffer);
381  llvm::write_integer(Stream, FN.DecValue, 0, IntegerStyle::Integer);
382  if (Buffer.size() < FN.Width)
383  indent(FN.Width - Buffer.size());
384  (*this) << Buffer;
385  }
386  return *this;
387 }
388 
390  if (FB.Bytes.empty())
391  return *this;
392 
393  size_t LineIndex = 0;
394  auto Bytes = FB.Bytes;
395  const size_t Size = Bytes.size();
397  uint64_t OffsetWidth = 0;
398  if (FB.FirstByteOffset.hasValue()) {
399  // Figure out how many nibbles are needed to print the largest offset
400  // represented by this data set, so that we can align the offset field
401  // to the right width.
402  size_t Lines = Size / FB.NumPerLine;
403  uint64_t MaxOffset = *FB.FirstByteOffset + Lines * FB.NumPerLine;
404  unsigned Power = 0;
405  if (MaxOffset > 0)
406  Power = llvm::Log2_64_Ceil(MaxOffset);
407  OffsetWidth = std::max<uint64_t>(4, llvm::alignTo(Power, 4) / 4);
408  }
409 
410  // The width of a block of data including all spaces for group separators.
411  unsigned NumByteGroups =
412  alignTo(FB.NumPerLine, FB.ByteGroupSize) / FB.ByteGroupSize;
413  unsigned BlockCharWidth = FB.NumPerLine * 2 + NumByteGroups - 1;
414 
415  while (!Bytes.empty()) {
416  indent(FB.IndentLevel);
417 
418  if (FB.FirstByteOffset.hasValue()) {
419  uint64_t Offset = FB.FirstByteOffset.getValue();
420  llvm::write_hex(*this, Offset + LineIndex, HPS, OffsetWidth);
421  *this << ": ";
422  }
423 
424  auto Line = Bytes.take_front(FB.NumPerLine);
425 
426  size_t CharsPrinted = 0;
427  // Print the hex bytes for this line in groups
428  for (size_t I = 0; I < Line.size(); ++I, CharsPrinted += 2) {
429  if (I && (I % FB.ByteGroupSize) == 0) {
430  ++CharsPrinted;
431  *this << " ";
432  }
433  llvm::write_hex(*this, Line[I], HPS, 2);
434  }
435 
436  if (FB.ASCII) {
437  // Print any spaces needed for any bytes that we didn't print on this
438  // line so that the ASCII bytes are correctly aligned.
439  assert(BlockCharWidth >= CharsPrinted);
440  indent(BlockCharWidth - CharsPrinted + 2);
441  *this << "|";
442 
443  // Print the ASCII char values for each byte on this line
444  for (uint8_t Byte : Line) {
445  if (isprint(Byte))
446  *this << static_cast<char>(Byte);
447  else
448  *this << '.';
449  }
450  *this << '|';
451  }
452 
453  Bytes = Bytes.drop_front(Line.size());
454  LineIndex += Line.size();
455  if (LineIndex < Size)
456  *this << '\n';
457  }
458  return *this;
459 }
460 
461 /// indent - Insert 'NumSpaces' spaces.
462 raw_ostream &raw_ostream::indent(unsigned NumSpaces) {
463  static const char Spaces[] = " "
464  " "
465  " ";
466 
467  // Usually the indentation is small, handle it with a fastpath.
468  if (NumSpaces < array_lengthof(Spaces))
469  return write(Spaces, NumSpaces);
470 
471  while (NumSpaces) {
472  unsigned NumToWrite = std::min(NumSpaces,
473  (unsigned)array_lengthof(Spaces)-1);
474  write(Spaces, NumToWrite);
475  NumSpaces -= NumToWrite;
476  }
477  return *this;
478 }
479 
480 //===----------------------------------------------------------------------===//
481 // Formatted Output
482 //===----------------------------------------------------------------------===//
483 
484 // Out of line virtual method.
486 }
487 
488 //===----------------------------------------------------------------------===//
489 // raw_fd_ostream
490 //===----------------------------------------------------------------------===//
491 
492 static int getFD(StringRef Filename, std::error_code &EC,
493  sys::fs::OpenFlags Flags) {
494  // Handle "-" as stdout. Note that when we do this, we consider ourself
495  // the owner of stdout and may set the "binary" flag globally based on Flags.
496  if (Filename == "-") {
497  EC = std::error_code();
498  // If user requested binary then put stdout into binary mode if
499  // possible.
500  if (!(Flags & sys::fs::F_Text))
502  return STDOUT_FILENO;
503  }
504 
505  int FD;
506  EC = sys::fs::openFileForWrite(Filename, FD, Flags);
507  if (EC)
508  return -1;
509 
510  return FD;
511 }
512 
513 raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
514  sys::fs::OpenFlags Flags)
515  : raw_fd_ostream(getFD(Filename, EC, Flags), true) {}
516 
517 /// FD is the file descriptor that this writes to. If ShouldClose is true, this
518 /// closes the file when the stream is destroyed.
519 raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered)
520  : raw_pwrite_stream(unbuffered), FD(fd), ShouldClose(shouldClose),
521  Error(false) {
522  if (FD < 0 ) {
523  ShouldClose = false;
524  return;
525  }
526 
527  // Do not attempt to close stdout or stderr. We used to try to maintain the
528  // property that tools that support writing file to stdout should not also
529  // write informational output to stdout, but in practice we were never able to
530  // maintain this invariant. Many features have been added to LLVM and clang
531  // (-fdump-record-layouts, optimization remarks, etc) that print to stdout, so
532  // users must simply be aware that mixed output and remarks is a possibility.
533  if (FD <= STDERR_FILENO)
534  ShouldClose = false;
535 
536  // Get the starting position.
537  off_t loc = ::lseek(FD, 0, SEEK_CUR);
538 #ifdef LLVM_ON_WIN32
539  // MSVCRT's _lseek(SEEK_CUR) doesn't return -1 for pipes.
540  sys::fs::file_status Status;
541  std::error_code EC = status(FD, Status);
542  SupportsSeeking = !EC && Status.type() == sys::fs::file_type::regular_file;
543 #else
544  SupportsSeeking = loc != (off_t)-1;
545 #endif
546  if (!SupportsSeeking)
547  pos = 0;
548  else
549  pos = static_cast<uint64_t>(loc);
550 }
551 
553  if (FD >= 0) {
554  flush();
555  if (ShouldClose && sys::Process::SafelyCloseFileDescriptor(FD))
556  error_detected();
557  }
558 
559 #ifdef __MINGW32__
560  // On mingw, global dtors should not call exit().
561  // report_fatal_error() invokes exit(). We know report_fatal_error()
562  // might not write messages to stderr when any errors were detected
563  // on FD == 2.
564  if (FD == 2) return;
565 #endif
566 
567  // If there are any pending errors, report them now. Clients wishing
568  // to avoid report_fatal_error calls should check for errors with
569  // has_error() and clear the error flag with clear_error() before
570  // destructing raw_ostream objects which may have errors.
571  if (has_error())
572  report_fatal_error("IO failure on output stream.", /*GenCrashDiag=*/false);
573 }
574 
575 void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
576  assert(FD >= 0 && "File already closed.");
577  pos += Size;
578 
579 #ifndef LLVM_ON_WIN32
580 #if defined(__linux__)
581  bool ShouldWriteInChunks = true;
582 #else
583  bool ShouldWriteInChunks = false;
584 #endif
585 #else
586  // Writing a large size of output to Windows console returns ENOMEM. It seems
587  // that, prior to Windows 8, WriteFile() is redirecting to WriteConsole(), and
588  // the latter has a size limit (66000 bytes or less, depending on heap usage).
589  bool ShouldWriteInChunks = !!::_isatty(FD) && !RunningWindows8OrGreater();
590 #endif
591 
592  do {
593  size_t ChunkSize = Size;
594  if (ChunkSize > 32767 && ShouldWriteInChunks)
595  ChunkSize = 32767;
596 
597  ssize_t ret = ::write(FD, Ptr, ChunkSize);
598 
599  if (ret < 0) {
600  // If it's a recoverable error, swallow it and retry the write.
601  //
602  // Ideally we wouldn't ever see EAGAIN or EWOULDBLOCK here, since
603  // raw_ostream isn't designed to do non-blocking I/O. However, some
604  // programs, such as old versions of bjam, have mistakenly used
605  // O_NONBLOCK. For compatibility, emulate blocking semantics by
606  // spinning until the write succeeds. If you don't want spinning,
607  // don't use O_NONBLOCK file descriptors with raw_ostream.
608  if (errno == EINTR || errno == EAGAIN
609 #ifdef EWOULDBLOCK
610  || errno == EWOULDBLOCK
611 #endif
612  )
613  continue;
614 
615  // Otherwise it's a non-recoverable error. Note it and quit.
616  error_detected();
617  break;
618  }
619 
620  // The write may have written some or all of the data. Update the
621  // size and buffer pointer to reflect the remainder that needs
622  // to be written. If there are no bytes left, we're done.
623  Ptr += ret;
624  Size -= ret;
625  } while (Size > 0);
626 }
627 
629  assert(ShouldClose);
630  ShouldClose = false;
631  flush();
633  error_detected();
634  FD = -1;
635 }
636 
637 uint64_t raw_fd_ostream::seek(uint64_t off) {
638  assert(SupportsSeeking && "Stream does not support seeking!");
639  flush();
640 #ifdef LLVM_ON_WIN32
641  pos = ::_lseeki64(FD, off, SEEK_SET);
642 #elif defined(HAVE_LSEEK64)
643  pos = ::lseek64(FD, off, SEEK_SET);
644 #else
645  pos = ::lseek(FD, off, SEEK_SET);
646 #endif
647  if (pos == (uint64_t)-1)
648  error_detected();
649  return pos;
650 }
651 
652 void raw_fd_ostream::pwrite_impl(const char *Ptr, size_t Size,
653  uint64_t Offset) {
654  uint64_t Pos = tell();
655  seek(Offset);
656  write(Ptr, Size);
657  seek(Pos);
658 }
659 
660 size_t raw_fd_ostream::preferred_buffer_size() const {
661 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__minix)
662  // Windows and Minix have no st_blksize.
663  assert(FD >= 0 && "File not yet open!");
664  struct stat statbuf;
665  if (fstat(FD, &statbuf) != 0)
666  return 0;
667 
668  // If this is a terminal, don't use buffering. Line buffering
669  // would be a more traditional thing to do, but it's not worth
670  // the complexity.
671  if (S_ISCHR(statbuf.st_mode) && isatty(FD))
672  return 0;
673  // Return the preferred block size.
674  return statbuf.st_blksize;
675 #else
677 #endif
678 }
679 
681  bool bg) {
683  flush();
684  const char *colorcode =
685  (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
686  : sys::Process::OutputColor(colors, bold, bg);
687  if (colorcode) {
688  size_t len = strlen(colorcode);
689  write(colorcode, len);
690  // don't account colors towards output characters
691  pos -= len;
692  }
693  return *this;
694 }
695 
698  flush();
699  const char *colorcode = sys::Process::ResetColor();
700  if (colorcode) {
701  size_t len = strlen(colorcode);
702  write(colorcode, len);
703  // don't account colors towards output characters
704  pos -= len;
705  }
706  return *this;
707 }
708 
711  flush();
712  const char *colorcode = sys::Process::OutputReverse();
713  if (colorcode) {
714  size_t len = strlen(colorcode);
715  write(colorcode, len);
716  // don't account colors towards output characters
717  pos -= len;
718  }
719  return *this;
720 }
721 
724 }
725 
728 }
729 
730 //===----------------------------------------------------------------------===//
731 // outs(), errs(), nulls()
732 //===----------------------------------------------------------------------===//
733 
734 /// outs() - This returns a reference to a raw_ostream for standard output.
735 /// Use it like: outs() << "foo" << "bar";
737  // Set buffer settings to model stdout behavior.
738  std::error_code EC;
739  static raw_fd_ostream S("-", EC, sys::fs::F_None);
740  assert(!EC);
741  return S;
742 }
743 
744 /// errs() - This returns a reference to a raw_ostream for standard error.
745 /// Use it like: errs() << "foo" << "bar";
747  // Set standard error to be unbuffered by default.
748  static raw_fd_ostream S(STDERR_FILENO, false, true);
749  return S;
750 }
751 
752 /// nulls() - This returns a reference to a raw_ostream which discards output.
754  static raw_null_ostream S;
755  return S;
756 }
757 
758 //===----------------------------------------------------------------------===//
759 // raw_string_ostream
760 //===----------------------------------------------------------------------===//
761 
763  flush();
764 }
765 
766 void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
767  OS.append(Ptr, Size);
768 }
769 
770 //===----------------------------------------------------------------------===//
771 // raw_svector_ostream
772 //===----------------------------------------------------------------------===//
773 
774 uint64_t raw_svector_ostream::current_pos() const { return OS.size(); }
775 
776 void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
777  OS.append(Ptr, Ptr + Size);
778 }
779 
780 void raw_svector_ostream::pwrite_impl(const char *Ptr, size_t Size,
781  uint64_t Offset) {
782  memcpy(OS.data() + Offset, Ptr, Size);
783 }
784 
785 //===----------------------------------------------------------------------===//
786 // raw_null_ostream
787 //===----------------------------------------------------------------------===//
788 
790 #ifndef NDEBUG
791  // ~raw_ostream asserts that the buffer is empty. This isn't necessary
792  // with raw_null_ostream, but it's better to have raw_null_ostream follow
793  // the rules than to change the rules just for raw_null_ostream.
794  flush();
795 #endif
796 }
797 
798 void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
799 }
800 
801 uint64_t raw_null_ostream::current_pos() const {
802  return 0;
803 }
804 
805 void raw_null_ostream::pwrite_impl(const char *Ptr, size_t Size,
806  uint64_t Offset) {}
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.
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:217
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:103
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
static const char * ResetColor()
Resets the terminals colors, or returns an escape sequence to do so.
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:176
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:136
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:522
virtual ~raw_ostream()
Definition: raw_ostream.cpp:71
uint64_t seek(uint64_t off)
Flushes the stream and repositions the underlying file descriptor position to the offset specified fr...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
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:493
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:677
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:218
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:129
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:129
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
~raw_fd_ostream() override
This is a helper class used for handling formatted output.
Definition: Format.h:38
raw_ostream & operator<<(char C)
Definition: raw_ostream.h:145
raw_ostream & outs()
This returns a reference to a raw_ostream for standard output.
#define P(N)
static int getFD(StringRef Filename, std::error_code &EC, sys::fs::OpenFlags Flags)
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:84
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:127
raw_fd_ostream(StringRef Filename, std::error_code &EC, sys::fs::OpenFlags Flags)
Open the specified file for writing.
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
Definition: raw_ostream.h:109
#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:431
bool RunningWindows8OrGreater()
Determines if the program is running on Windows 8 or newer.
raw_ostream & write(unsigned char C)
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 std::isprint into an escape ...
This is a helper class used for format_hex() and format_decimal().
Definition: Format.h:164
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:864
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
Definition: STLExtras.h:729
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:550
size_t GetNumBytesInBuffer() const
Definition: raw_ostream.h:132
The file should be opened in text mode on platforms that make this distinction.
Definition: FileSystem.h:730
void SetBuffered()
Set the stream to be buffered, with an automatically determined buffer size.
Definition: raw_ostream.cpp:89
Basic Alias true
bool hasValue() const
Definition: Optional.h:133
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:361
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:143
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
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:337
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:54
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:235
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
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:98
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
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:144
void resize(size_type N)
Definition: SmallVector.h:355
std::error_code openFileForWrite(const Twine &Name, int &ResultFD, OpenFlags Flags, unsigned Mode=0666)