LLVM  13.0.0git
raw_ostream.h
Go to the documentation of this file.
1 //===--- raw_ostream.h - Raw output stream ----------------------*- C++ -*-===//
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 file defines the raw_ostream class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_SUPPORT_RAW_OSTREAM_H
14 #define LLVM_SUPPORT_RAW_OSTREAM_H
15 
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/DataTypes.h"
19 #include <cassert>
20 #include <chrono>
21 #include <cstddef>
22 #include <cstdint>
23 #include <cstring>
24 #include <string>
25 #include <system_error>
26 #include <type_traits>
27 
28 namespace llvm {
29 
30 class formatv_object_base;
31 class format_object_base;
32 class FormattedString;
33 class FormattedNumber;
34 class FormattedBytes;
35 template <class T> class LLVM_NODISCARD Expected;
36 
37 namespace sys {
38 namespace fs {
39 enum FileAccess : unsigned;
40 enum OpenFlags : unsigned;
41 enum CreationDisposition : unsigned;
42 class FileLocker;
43 } // end namespace fs
44 } // end namespace sys
45 
46 /// This class implements an extremely fast bulk output stream that can *only*
47 /// output to a stream. It does not support seeking, reopening, rewinding, line
48 /// buffered disciplines etc. It is a simple buffer that outputs
49 /// a chunk at a time.
50 class raw_ostream {
51 public:
52  // Class kinds to support LLVM-style RTTI.
53  enum class OStreamKind {
54  OK_OStream,
56  };
57 
58 private:
59  OStreamKind Kind;
60 
61  /// The buffer is handled in such a way that the buffer is
62  /// uninitialized, unbuffered, or out of space when OutBufCur >=
63  /// OutBufEnd. Thus a single comparison suffices to determine if we
64  /// need to take the slow path to write a single character.
65  ///
66  /// The buffer is in one of three states:
67  /// 1. Unbuffered (BufferMode == Unbuffered)
68  /// 1. Uninitialized (BufferMode != Unbuffered && OutBufStart == 0).
69  /// 2. Buffered (BufferMode != Unbuffered && OutBufStart != 0 &&
70  /// OutBufEnd - OutBufStart >= 1).
71  ///
72  /// If buffered, then the raw_ostream owns the buffer if (BufferMode ==
73  /// InternalBuffer); otherwise the buffer has been set via SetBuffer and is
74  /// managed by the subclass.
75  ///
76  /// If a subclass installs an external buffer using SetBuffer then it can wait
77  /// for a \see write_impl() call to handle the data which has been put into
78  /// this buffer.
79  char *OutBufStart, *OutBufEnd, *OutBufCur;
80  bool ColorEnabled = false;
81 
82  /// Optional stream this stream is tied to. If this stream is written to, the
83  /// tied-to stream will be flushed first.
84  raw_ostream *TiedStream = nullptr;
85 
86  enum class BufferKind {
87  Unbuffered = 0,
88  InternalBuffer,
89  ExternalBuffer
90  } BufferMode;
91 
92 public:
93  // color order matches ANSI escape sequence, don't change
94  enum class Colors {
95  BLACK = 0,
96  RED,
97  GREEN,
98  YELLOW,
99  BLUE,
100  MAGENTA,
101  CYAN,
102  WHITE,
103  SAVEDCOLOR,
104  RESET,
105  };
106 
107  static constexpr Colors BLACK = Colors::BLACK;
108  static constexpr Colors RED = Colors::RED;
109  static constexpr Colors GREEN = Colors::GREEN;
110  static constexpr Colors YELLOW = Colors::YELLOW;
111  static constexpr Colors BLUE = Colors::BLUE;
112  static constexpr Colors MAGENTA = Colors::MAGENTA;
113  static constexpr Colors CYAN = Colors::CYAN;
114  static constexpr Colors WHITE = Colors::WHITE;
116  static constexpr Colors RESET = Colors::RESET;
117 
118  explicit raw_ostream(bool unbuffered = false,
120  : Kind(K), BufferMode(unbuffered ? BufferKind::Unbuffered
121  : BufferKind::InternalBuffer) {
122  // Start out ready to flush.
123  OutBufStart = OutBufEnd = OutBufCur = nullptr;
124  }
125 
126  raw_ostream(const raw_ostream &) = delete;
127  void operator=(const raw_ostream &) = delete;
128 
129  virtual ~raw_ostream();
130 
131  /// tell - Return the current offset with the file.
132  uint64_t tell() const { return current_pos() + GetNumBytesInBuffer(); }
133 
134  OStreamKind get_kind() const { return Kind; }
135 
136  //===--------------------------------------------------------------------===//
137  // Configuration Interface
138  //===--------------------------------------------------------------------===//
139 
140  /// If possible, pre-allocate \p ExtraSize bytes for stream data.
141  /// i.e. it extends internal buffers to keep additional ExtraSize bytes.
142  /// So that the stream could keep at least tell() + ExtraSize bytes
143  /// without re-allocations. reserveExtraSpace() does not change
144  /// the size/data of the stream.
145  virtual void reserveExtraSpace(uint64_t ExtraSize) {}
146 
147  /// Set the stream to be buffered, with an automatically determined buffer
148  /// size.
149  void SetBuffered();
150 
151  /// Set the stream to be buffered, using the specified buffer size.
152  void SetBufferSize(size_t Size) {
153  flush();
154  SetBufferAndMode(new char[Size], Size, BufferKind::InternalBuffer);
155  }
156 
157  size_t GetBufferSize() const {
158  // If we're supposed to be buffered but haven't actually gotten around
159  // to allocating the buffer yet, return the value that would be used.
160  if (BufferMode != BufferKind::Unbuffered && OutBufStart == nullptr)
161  return preferred_buffer_size();
162 
163  // Otherwise just return the size of the allocated buffer.
164  return OutBufEnd - OutBufStart;
165  }
166 
167  /// Set the stream to be unbuffered. When unbuffered, the stream will flush
168  /// after every write. This routine will also flush the buffer immediately
169  /// when the stream is being set to unbuffered.
170  void SetUnbuffered() {
171  flush();
172  SetBufferAndMode(nullptr, 0, BufferKind::Unbuffered);
173  }
174 
175  size_t GetNumBytesInBuffer() const {
176  return OutBufCur - OutBufStart;
177  }
178 
179  //===--------------------------------------------------------------------===//
180  // Data Output Interface
181  //===--------------------------------------------------------------------===//
182 
183  void flush() {
184  if (OutBufCur != OutBufStart)
185  flush_nonempty();
186  }
187 
189  if (OutBufCur >= OutBufEnd)
190  return write(C);
191  *OutBufCur++ = C;
192  return *this;
193  }
194 
195  raw_ostream &operator<<(unsigned char C) {
196  if (OutBufCur >= OutBufEnd)
197  return write(C);
198  *OutBufCur++ = C;
199  return *this;
200  }
201 
202  raw_ostream &operator<<(signed char C) {
203  if (OutBufCur >= OutBufEnd)
204  return write(C);
205  *OutBufCur++ = C;
206  return *this;
207  }
208 
210  // Inline fast path, particularly for strings with a known length.
211  size_t Size = Str.size();
212 
213  // Make sure we can use the fast path.
214  if (Size > (size_t)(OutBufEnd - OutBufCur))
215  return write(Str.data(), Size);
216 
217  if (Size) {
218  memcpy(OutBufCur, Str.data(), Size);
219  OutBufCur += Size;
220  }
221  return *this;
222  }
223 
224  raw_ostream &operator<<(const char *Str) {
225  // Inline fast path, particularly for constant strings where a sufficiently
226  // smart compiler will simplify strlen.
227 
228  return this->operator<<(StringRef(Str));
229  }
230 
231  raw_ostream &operator<<(const std::string &Str) {
232  // Avoid the fast path, it would only increase code size for a marginal win.
233  return write(Str.data(), Str.length());
234  }
235 
237  return write(Str.data(), Str.size());
238  }
239 
240  raw_ostream &operator<<(unsigned long N);
241  raw_ostream &operator<<(long N);
242  raw_ostream &operator<<(unsigned long long N);
243  raw_ostream &operator<<(long long N);
244  raw_ostream &operator<<(const void *P);
245 
246  raw_ostream &operator<<(unsigned int N) {
247  return this->operator<<(static_cast<unsigned long>(N));
248  }
249 
251  return this->operator<<(static_cast<long>(N));
252  }
253 
254  raw_ostream &operator<<(double N);
255 
256  /// Output \p N in hexadecimal, without any prefix or padding.
257  raw_ostream &write_hex(unsigned long long N);
258 
259  // Change the foreground color of text.
261 
262  /// Output a formatted UUID with dash separators.
263  using uuid_t = uint8_t[16];
265 
266  /// Output \p Str, turning '\\', '\t', '\n', '"', and anything that doesn't
267  /// satisfy llvm::isPrint into an escape sequence.
268  raw_ostream &write_escaped(StringRef Str, bool UseHexEscapes = false);
269 
270  raw_ostream &write(unsigned char C);
271  raw_ostream &write(const char *Ptr, size_t Size);
272 
273  // Formatted output, see the format() function in Support/Format.h.
275 
276  // Formatted output, see the leftJustify() function in Support/Format.h.
278 
279  // Formatted output, see the formatHex() function in Support/Format.h.
281 
282  // Formatted output, see the formatv() function in Support/FormatVariadic.h.
284 
285  // Formatted output, see the format_bytes() function in Support/Format.h.
287 
288  /// indent - Insert 'NumSpaces' spaces.
289  raw_ostream &indent(unsigned NumSpaces);
290 
291  /// write_zeros - Insert 'NumZeros' nulls.
292  raw_ostream &write_zeros(unsigned NumZeros);
293 
294  /// Changes the foreground color of text that will be output from this point
295  /// forward.
296  /// @param Color ANSI color to use, the special SAVEDCOLOR can be used to
297  /// change only the bold attribute, and keep colors untouched
298  /// @param Bold bold/brighter text, default false
299  /// @param BG if true change the background, default: change foreground
300  /// @returns itself so it can be used within << invocations
301  virtual raw_ostream &changeColor(enum Colors Color, bool Bold = false,
302  bool BG = false);
303 
304  /// Resets the colors to terminal defaults. Call this when you are done
305  /// outputting colored text, or before program exit.
306  virtual raw_ostream &resetColor();
307 
308  /// Reverses the foreground and background colors.
309  virtual raw_ostream &reverseColor();
310 
311  /// This function determines if this stream is connected to a "tty" or
312  /// "console" window. That is, the output would be displayed to the user
313  /// rather than being put on a pipe or stored in a file.
314  virtual bool is_displayed() const { return false; }
315 
316  /// This function determines if this stream is displayed and supports colors.
317  /// The result is unaffected by calls to enable_color().
318  virtual bool has_colors() const { return is_displayed(); }
319 
320  // Enable or disable colors. Once enable_colors(false) is called,
321  // changeColor() has no effect until enable_colors(true) is called.
322  virtual void enable_colors(bool enable) { ColorEnabled = enable; }
323 
324  /// Tie this stream to the specified stream. Replaces any existing tied-to
325  /// stream. Specifying a nullptr unties the stream.
326  void tie(raw_ostream *TieTo) { TiedStream = TieTo; }
327 
328  //===--------------------------------------------------------------------===//
329  // Subclass Interface
330  //===--------------------------------------------------------------------===//
331 
332 private:
333  /// The is the piece of the class that is implemented by subclasses. This
334  /// writes the \p Size bytes starting at
335  /// \p Ptr to the underlying stream.
336  ///
337  /// This function is guaranteed to only be called at a point at which it is
338  /// safe for the subclass to install a new buffer via SetBuffer.
339  ///
340  /// \param Ptr The start of the data to be written. For buffered streams this
341  /// is guaranteed to be the start of the buffer.
342  ///
343  /// \param Size The number of bytes to be written.
344  ///
345  /// \invariant { Size > 0 }
346  virtual void write_impl(const char *Ptr, size_t Size) = 0;
347 
348  /// Return the current position within the stream, not counting the bytes
349  /// currently in the buffer.
350  virtual uint64_t current_pos() const = 0;
351 
352 protected:
353  /// Use the provided buffer as the raw_ostream buffer. This is intended for
354  /// use only by subclasses which can arrange for the output to go directly
355  /// into the desired output buffer, instead of being copied on each flush.
356  void SetBuffer(char *BufferStart, size_t Size) {
357  SetBufferAndMode(BufferStart, Size, BufferKind::ExternalBuffer);
358  }
359 
360  /// Return an efficient buffer size for the underlying output mechanism.
361  virtual size_t preferred_buffer_size() const;
362 
363  /// Return the beginning of the current stream buffer, or 0 if the stream is
364  /// unbuffered.
365  const char *getBufferStart() const { return OutBufStart; }
366 
367  //===--------------------------------------------------------------------===//
368  // Private Interface
369  //===--------------------------------------------------------------------===//
370 private:
371  /// Install the given buffer and mode.
372  void SetBufferAndMode(char *BufferStart, size_t Size, BufferKind Mode);
373 
374  /// Flush the current buffer, which is known to be non-empty. This outputs the
375  /// currently buffered data and resets the buffer to empty.
376  void flush_nonempty();
377 
378  /// Copy data into the buffer. Size must not be greater than the number of
379  /// unused bytes in the buffer.
380  void copy_to_buffer(const char *Ptr, size_t Size);
381 
382  /// Compute whether colors should be used and do the necessary work such as
383  /// flushing. The result is affected by calls to enable_color().
384  bool prepare_colors();
385 
386  /// Flush the tied-to stream (if present) and then write the required data.
387  void flush_tied_then_write(const char *Ptr, size_t Size);
388 
389  virtual void anchor();
390 };
391 
392 /// Call the appropriate insertion operator, given an rvalue reference to a
393 /// raw_ostream object and return a stream of the same type as the argument.
394 template <typename OStream, typename T>
395 std::enable_if_t<!std::is_reference<OStream>::value &&
396  std::is_base_of<raw_ostream, OStream>::value,
397  OStream &&>
398 operator<<(OStream &&OS, const T &Value) {
399  OS << Value;
400  return std::move(OS);
401 }
402 
403 /// An abstract base class for streams implementations that also support a
404 /// pwrite operation. This is useful for code that can mostly stream out data,
405 /// but needs to patch in a header that needs to know the output size.
407  virtual void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) = 0;
408  void anchor() override;
409 
410 public:
411  explicit raw_pwrite_stream(bool Unbuffered = false,
413  : raw_ostream(Unbuffered, K) {}
414  void pwrite(const char *Ptr, size_t Size, uint64_t Offset) {
415 #ifndef NDEBUG
416  uint64_t Pos = tell();
417  // /dev/null always reports a pos of 0, so we cannot perform this check
418  // in that case.
419  if (Pos)
420  assert(Size + Offset <= Pos && "We don't support extending the stream");
421 #endif
422  pwrite_impl(Ptr, Size, Offset);
423  }
424 };
425 
426 //===----------------------------------------------------------------------===//
427 // File Output Streams
428 //===----------------------------------------------------------------------===//
429 
430 /// A raw_ostream that writes to a file descriptor.
431 ///
433  int FD;
434  bool ShouldClose;
435  bool SupportsSeeking = false;
436  mutable Optional<bool> HasColors;
437 
438 #ifdef _WIN32
439  /// True if this fd refers to a Windows console device. Mintty and other
440  /// terminal emulators are TTYs, but they are not consoles.
441  bool IsWindowsConsole = false;
442 #endif
443 
444  std::error_code EC;
445 
446  uint64_t pos = 0;
447 
448  /// See raw_ostream::write_impl.
449  void write_impl(const char *Ptr, size_t Size) override;
450 
451  void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
452 
453  /// Return the current position within the stream, not counting the bytes
454  /// currently in the buffer.
455  uint64_t current_pos() const override { return pos; }
456 
457  /// Determine an efficient buffer size.
458  size_t preferred_buffer_size() const override;
459 
460  void anchor() override;
461 
462 protected:
463  /// Set the flag indicating that an output error has been encountered.
464  void error_detected(std::error_code EC) { this->EC = EC; }
465 
466  /// Return the file descriptor.
467  int get_fd() const { return FD; }
468 
469  // Update the file position by increasing \p Delta.
470  void inc_pos(uint64_t Delta) { pos += Delta; }
471 
472 public:
473  /// Open the specified file for writing. If an error occurs, information
474  /// about the error is put into EC, and the stream should be immediately
475  /// destroyed;
476  /// \p Flags allows optional flags to control how the file will be opened.
477  ///
478  /// As a special case, if Filename is "-", then the stream will use
479  /// STDOUT_FILENO instead of opening a file. This will not close the stdout
480  /// descriptor.
481  raw_fd_ostream(StringRef Filename, std::error_code &EC);
482  raw_fd_ostream(StringRef Filename, std::error_code &EC,
484  raw_fd_ostream(StringRef Filename, std::error_code &EC,
485  sys::fs::FileAccess Access);
486  raw_fd_ostream(StringRef Filename, std::error_code &EC,
487  sys::fs::OpenFlags Flags);
488  raw_fd_ostream(StringRef Filename, std::error_code &EC,
490  sys::fs::OpenFlags Flags);
491 
492  /// FD is the file descriptor that this writes to. If ShouldClose is true,
493  /// this closes the file when the stream is destroyed. If FD is for stdout or
494  /// stderr, it will not be closed.
495  raw_fd_ostream(int fd, bool shouldClose, bool unbuffered = false,
497 
498  ~raw_fd_ostream() override;
499 
500  /// Manually flush the stream and close the file. Note that this does not call
501  /// fsync.
502  void close();
503 
504  bool supportsSeeking() const { return SupportsSeeking; }
505 
506  /// Flushes the stream and repositions the underlying file descriptor position
507  /// to the offset specified from the beginning of the file.
508  uint64_t seek(uint64_t off);
509 
510  bool is_displayed() const override;
511 
512  bool has_colors() const override;
513 
514  std::error_code error() const { return EC; }
515 
516  /// Return the value of the flag in this raw_fd_ostream indicating whether an
517  /// output error has been encountered.
518  /// This doesn't implicitly flush any pending output. Also, it doesn't
519  /// guarantee to detect all errors unless the stream has been closed.
520  bool has_error() const { return bool(EC); }
521 
522  /// Set the flag read by has_error() to false. If the error flag is set at the
523  /// time when this raw_ostream's destructor is called, report_fatal_error is
524  /// called to report the error. Use clear_error() after handling the error to
525  /// avoid this behavior.
526  ///
527  /// "Errors should never pass silently.
528  /// Unless explicitly silenced."
529  /// - from The Zen of Python, by Tim Peters
530  ///
531  void clear_error() { EC = std::error_code(); }
532 
533  /// Locks the underlying file.
534  ///
535  /// @returns RAII object that releases the lock upon leaving the scope, if the
536  /// locking was successful. Otherwise returns corresponding
537  /// error code.
538  ///
539  /// The function blocks the current thread until the lock become available or
540  /// error occurs.
541  ///
542  /// Possible use of this function may be as follows:
543  ///
544  /// @code{.cpp}
545  /// if (auto L = stream.lock()) {
546  /// // ... do action that require file to be locked.
547  /// } else {
548  /// handleAllErrors(std::move(L.takeError()), [&](ErrorInfoBase &EIB) {
549  /// // ... handle lock error.
550  /// });
551  /// }
552  /// @endcode
554 
555  /// Tries to lock the underlying file within the specified period.
556  ///
557  /// @returns RAII object that releases the lock upon leaving the scope, if the
558  /// locking was successful. Otherwise returns corresponding
559  /// error code.
560  ///
561  /// It is used as @ref lock.
563  Expected<sys::fs::FileLocker> tryLockFor(std::chrono::milliseconds Timeout);
564 };
565 
566 /// This returns a reference to a raw_fd_ostream for standard output. Use it
567 /// like: outs() << "foo" << "bar";
568 raw_fd_ostream &outs();
569 
570 /// This returns a reference to a raw_ostream for standard error.
571 /// Use it like: errs() << "foo" << "bar";
572 /// By default, the stream is tied to stdout to ensure stdout is flushed before
573 /// stderr is written, to ensure the error messages are written in their
574 /// expected place.
575 raw_fd_ostream &errs();
576 
577 /// This returns a reference to a raw_ostream which simply discards output.
578 raw_ostream &nulls();
579 
580 //===----------------------------------------------------------------------===//
581 // File Streams
582 //===----------------------------------------------------------------------===//
583 
584 /// A raw_ostream of a file for reading/writing/seeking.
585 ///
587 public:
588  /// Open the specified file for reading/writing/seeking. If an error occurs,
589  /// information about the error is put into EC, and the stream should be
590  /// immediately destroyed.
591  raw_fd_stream(StringRef Filename, std::error_code &EC);
592 
593  /// This reads the \p Size bytes into a buffer pointed by \p Ptr.
594  ///
595  /// \param Ptr The start of the buffer to hold data to be read.
596  ///
597  /// \param Size The number of bytes to be read.
598  ///
599  /// On success, the number of bytes read is returned, and the file position is
600  /// advanced by this number. On error, -1 is returned, use error() to get the
601  /// error code.
602  ssize_t read(char *Ptr, size_t Size);
603 
604  /// Check if \p OS is a pointer of type raw_fd_stream*.
605  static bool classof(const raw_ostream *OS);
606 };
607 
608 //===----------------------------------------------------------------------===//
609 // Output Stream Adaptors
610 //===----------------------------------------------------------------------===//
611 
612 /// A raw_ostream that writes to an std::string. This is a simple adaptor
613 /// class. This class does not encounter output errors.
615  std::string &OS;
616 
617  /// See raw_ostream::write_impl.
618  void write_impl(const char *Ptr, size_t Size) override;
619 
620  /// Return the current position within the stream, not counting the bytes
621  /// currently in the buffer.
622  uint64_t current_pos() const override { return OS.size(); }
623 
624 public:
625  explicit raw_string_ostream(std::string &O) : OS(O) {
626  SetUnbuffered();
627  }
628  ~raw_string_ostream() override;
629 
630  /// Flushes the stream contents to the target string and returns the string's
631  /// reference.
632  std::string& str() {
633  flush();
634  return OS;
635  }
636 
637  void reserveExtraSpace(uint64_t ExtraSize) override {
638  OS.reserve(tell() + ExtraSize);
639  }
640 };
641 
642 /// A raw_ostream that writes to an SmallVector or SmallString. This is a
643 /// simple adaptor class. This class does not encounter output errors.
644 /// raw_svector_ostream operates without a buffer, delegating all memory
645 /// management to the SmallString. Thus the SmallString is always up-to-date,
646 /// may be used directly and there is no need to call flush().
649 
650  /// See raw_ostream::write_impl.
651  void write_impl(const char *Ptr, size_t Size) override;
652 
653  void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
654 
655  /// Return the current position within the stream.
656  uint64_t current_pos() const override;
657 
658 public:
659  /// Construct a new raw_svector_ostream.
660  ///
661  /// \param O The vector to write to; this should generally have at least 128
662  /// bytes free to avoid any extraneous memory overhead.
664  SetUnbuffered();
665  }
666 
667  ~raw_svector_ostream() override = default;
668 
669  void flush() = delete;
670 
671  /// Return a StringRef for the vector contents.
672  StringRef str() const { return StringRef(OS.data(), OS.size()); }
673 
674  void reserveExtraSpace(uint64_t ExtraSize) override {
675  OS.reserve(tell() + ExtraSize);
676  }
677 };
678 
679 /// A raw_ostream that discards all output.
681  /// See raw_ostream::write_impl.
682  void write_impl(const char *Ptr, size_t size) override;
683  void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
684 
685  /// Return the current position within the stream, not counting the bytes
686  /// currently in the buffer.
687  uint64_t current_pos() const override;
688 
689 public:
690  explicit raw_null_ostream() = default;
691  ~raw_null_ostream() override;
692 };
693 
695  raw_ostream &OS;
696  SmallVector<char, 0> Buffer;
697 
698  virtual void anchor() override;
699 
700 public:
702  ~buffer_ostream() override { OS << str(); }
703 };
704 
706  std::unique_ptr<raw_ostream> OS;
707  SmallVector<char, 0> Buffer;
708 
709  virtual void anchor() override;
710 
711 public:
712  buffer_unique_ostream(std::unique_ptr<raw_ostream> OS)
713  : raw_svector_ostream(Buffer), OS(std::move(OS)) {}
714  ~buffer_unique_ostream() override { *OS << str(); }
715 };
716 
717 class Error;
718 
719 /// This helper creates an output stream and then passes it to \p Write.
720 /// The stream created is based on the specified \p OutputFileName:
721 /// llvm::outs for "-", raw_null_ostream for "/dev/null", and raw_fd_ostream
722 /// for other names. For raw_fd_ostream instances, the stream writes to
723 /// a temporary file. The final output file is atomically replaced with the
724 /// temporary file after the \p Write function is finished.
725 Error writeToOutput(StringRef OutputFileName,
726  std::function<Error(raw_ostream &)> Write);
727 
728 } // end namespace llvm
729 
730 #endif // LLVM_SUPPORT_RAW_OSTREAM_H
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::raw_ostream::tell
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:132
llvm::buffer_unique_ostream::~buffer_unique_ostream
~buffer_unique_ostream() override
Definition: raw_ostream.h:714
llvm::raw_fd_ostream::supportsSeeking
bool supportsSeeking() const
Definition: raw_ostream.h:504
llvm::buffer_ostream
Definition: raw_ostream.h:694
llvm::sys::fs::CreationDisposition
CreationDisposition
Definition: FileSystem.h:729
llvm
Definition: AllocatorList.h:23
llvm::raw_ostream::reserveExtraSpace
virtual void reserveExtraSpace(uint64_t ExtraSize)
If possible, pre-allocate ExtraSize bytes for stream data.
Definition: raw_ostream.h:145
llvm::raw_fd_ostream::close
void close()
Manually flush the stream and close the file.
Definition: raw_ostream.cpp:792
llvm::raw_string_ostream::reserveExtraSpace
void reserveExtraSpace(uint64_t ExtraSize) override
If possible, pre-allocate ExtraSize bytes for stream data.
Definition: raw_ostream.h:637
llvm::buffer_ostream::buffer_ostream
buffer_ostream(raw_ostream &OS)
Definition: raw_ostream.h:701
llvm::lltok::Error
@ Error
Definition: LLToken.h:21
llvm::raw_ostream::SetBuffered
void SetBuffered()
Set the stream to be buffered, with an automatically determined buffer size.
Definition: raw_ostream.cpp:92
llvm::raw_ostream::operator=
void operator=(const raw_ostream &)=delete
llvm::raw_ostream::SetUnbuffered
void SetUnbuffered()
Set the stream to be unbuffered.
Definition: raw_ostream.h:170
llvm::raw_ostream::get_kind
OStreamKind get_kind() const
Definition: raw_ostream.h:134
llvm::raw_ostream::raw_ostream
raw_ostream(bool unbuffered=false, OStreamKind K=OStreamKind::OK_OStream)
Definition: raw_ostream.h:118
llvm::raw_ostream::is_displayed
virtual bool is_displayed() const
This function determines if this stream is connected to a "tty" or "console" window.
Definition: raw_ostream.h:314
llvm::raw_ostream::write_zeros
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
Definition: raw_ostream.cpp:502
StringRef.h
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::raw_ostream::~raw_ostream
virtual ~raw_ostream()
Definition: raw_ostream.cpp:77
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:614
llvm::raw_ostream::OStreamKind::OK_FDStream
@ OK_FDStream
llvm::SmallVector< char, 0 >
llvm::buffer_unique_ostream::buffer_unique_ostream
buffer_unique_ostream(std::unique_ptr< raw_ostream > OS)
Definition: raw_ostream.h:712
llvm::FormattedNumber
This is a helper class used for format_hex() and format_decimal().
Definition: Format.h:164
llvm::raw_ostream::write_escaped
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
Definition: raw_ostream.cpp:163
llvm::raw_ostream::SetBuffer
void SetBuffer(char *BufferStart, size_t Size)
Use the provided buffer as the raw_ostream buffer.
Definition: raw_ostream.h:356
llvm::raw_fd_ostream::raw_fd_ostream
raw_fd_ostream(StringRef Filename, std::error_code &EC)
Open the specified file for writing.
Definition: raw_ostream.cpp:593
llvm::raw_ostream::GREEN
static constexpr Colors GREEN
Definition: raw_ostream.h:109
llvm::Optional< bool >
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::raw_ostream::OStreamKind
OStreamKind
Definition: raw_ostream.h:53
llvm::sys::fs::FileAccess
FileAccess
Definition: FileSystem.h:751
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:892
llvm::raw_ostream::RED
static constexpr Colors RED
Definition: raw_ostream.h:108
llvm::raw_ostream::Colors::RESET
@ RESET
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::nulls
raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
Definition: raw_ostream.cpp:899
llvm::raw_fd_ostream::has_colors
bool has_colors() const override
This function determines if this stream is displayed and supports colors.
Definition: raw_ostream.cpp:857
llvm::raw_ostream::Colors::SAVEDCOLOR
@ SAVEDCOLOR
llvm::raw_ostream::MAGENTA
static constexpr Colors MAGENTA
Definition: raw_ostream.h:112
llvm::raw_fd_ostream::tryLockFor
LLVM_NODISCARD Expected< sys::fs::FileLocker > tryLockFor(std::chrono::milliseconds Timeout)
Tries to lock the underlying file within the specified period.
Definition: raw_ostream.cpp:871
llvm::raw_fd_stream::classof
static bool classof(const raw_ostream *OS)
Check if OS is a pointer of type raw_fd_stream*.
Definition: raw_ostream.cpp:931
llvm::raw_ostream::operator<<
raw_ostream & operator<<(StringRef Str)
Definition: raw_ostream.h:209
llvm::raw_ostream::changeColor
virtual raw_ostream & changeColor(enum Colors Color, bool Bold=false, bool BG=false)
Changes the foreground color of text that will be output from this point forward.
Definition: raw_ostream.cpp:522
llvm::raw_ostream::Colors::CYAN
@ CYAN
llvm::raw_ostream::preferred_buffer_size
virtual size_t preferred_buffer_size() const
Return an efficient buffer size for the underlying output mechanism.
Definition: raw_ostream.cpp:87
llvm::outs
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
Definition: raw_ostream.cpp:884
llvm::format_object_base
This is a helper class used for handling formatted output.
Definition: Format.h:38
llvm::raw_fd_ostream::has_error
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:520
llvm::raw_fd_ostream::inc_pos
void inc_pos(uint64_t Delta)
Definition: raw_ostream.h:470
llvm::raw_ostream::Colors::MAGENTA
@ MAGENTA
llvm::raw_fd_ostream::get_fd
int get_fd() const
Return the file descriptor.
Definition: raw_ostream.h:467
llvm::writeToOutput
Error writeToOutput(StringRef OutputFileName, std::function< Error(raw_ostream &)> Write)
This helper creates an output stream and then passes it to Write.
Definition: raw_ostream.cpp:991
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::raw_fd_ostream::clear_error
void clear_error()
Set the flag read by has_error() to false.
Definition: raw_ostream.h:531
llvm::raw_ostream::operator<<
raw_ostream & operator<<(unsigned int N)
Definition: raw_ostream.h:246
llvm::raw_ostream::Colors::RED
@ RED
llvm::raw_null_ostream::~raw_null_ostream
~raw_null_ostream() override
Definition: raw_ostream.cpp:966
llvm::raw_ostream::write
raw_ostream & write(unsigned char C)
Definition: raw_ostream.cpp:220
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
llvm::raw_ostream::flush
void flush()
Definition: raw_ostream.h:183
llvm::operator<<
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:230
llvm::raw_ostream::OStreamKind::OK_OStream
@ OK_OStream
llvm::raw_ostream::GetNumBytesInBuffer
size_t GetNumBytesInBuffer() const
Definition: raw_ostream.h:175
llvm::raw_fd_ostream::error_detected
void error_detected(std::error_code EC)
Set the flag indicating that an output error has been encountered.
Definition: raw_ostream.h:464
llvm::raw_ostream::WHITE
static constexpr Colors WHITE
Definition: raw_ostream.h:114
llvm::raw_fd_ostream::seek
uint64_t seek(uint64_t off)
Flushes the stream and repositions the underlying file descriptor position to the offset specified fr...
Definition: raw_ostream.cpp:801
llvm::raw_string_ostream::raw_string_ostream
raw_string_ostream(std::string &O)
Definition: raw_ostream.h:625
llvm::raw_ostream::enable_colors
virtual void enable_colors(bool enable)
Definition: raw_ostream.h:322
llvm::raw_ostream::Colors::BLUE
@ BLUE
llvm::raw_ostream::Colors::WHITE
@ WHITE
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:128
llvm::raw_svector_ostream::flush
void flush()=delete
llvm::raw_pwrite_stream
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:406
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::raw_ostream::operator<<
raw_ostream & operator<<(const SmallVectorImpl< char > &Str)
Definition: raw_ostream.h:236
llvm::raw_ostream::tie
void tie(raw_ostream *TieTo)
Tie this stream to the specified stream.
Definition: raw_ostream.h:326
llvm::raw_ostream::operator<<
raw_ostream & operator<<(int N)
Definition: raw_ostream.h:250
llvm::raw_fd_stream::read
ssize_t read(char *Ptr, size_t Size)
This reads the Size bytes into a buffer pointed by Ptr.
Definition: raw_ostream.cpp:921
llvm::raw_null_ostream::raw_null_ostream
raw_null_ostream()=default
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1563
memcpy
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
Mode
SI Whole Quad Mode
Definition: SIWholeQuadMode.cpp:262
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:83
UUID
std::pair< llvm::MachO::Target, std::string > UUID
Definition: TextStubCommon.h:23
llvm::raw_ostream::operator<<
raw_ostream & operator<<(const char *Str)
Definition: raw_ostream.h:224
llvm::raw_ostream::CYAN
static constexpr Colors CYAN
Definition: raw_ostream.h:113
llvm::raw_null_ostream
A raw_ostream that discards all output.
Definition: raw_ostream.h:680
llvm::size
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1486
llvm::raw_ostream::reverseColor
virtual raw_ostream & reverseColor()
Reverses the foreground and background colors.
Definition: raw_ostream.cpp:544
llvm::raw_ostream::GetBufferSize
size_t GetBufferSize() const
Definition: raw_ostream.h:157
llvm::raw_fd_ostream::error
std::error_code error() const
Definition: raw_ostream.h:514
llvm::raw_ostream::Colors::YELLOW
@ YELLOW
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::raw_ostream::operator<<
raw_ostream & operator<<(signed char C)
Definition: raw_ostream.h:202
llvm::raw_svector_ostream::str
StringRef str() const
Return a StringRef for the vector contents.
Definition: raw_ostream.h:672
llvm::raw_fd_ostream::is_displayed
bool is_displayed() const override
This function determines if this stream is connected to a "tty" or "console" window.
Definition: raw_ostream.cpp:853
llvm::buffer_unique_ostream
Definition: raw_ostream.h:705
llvm::raw_fd_ostream
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:432
llvm::FormattedBytes
Definition: Format.h:215
llvm::raw_ostream::operator<<
raw_ostream & operator<<(char C)
Definition: raw_ostream.h:188
llvm::raw_ostream::Colors::BLACK
@ BLACK
llvm::raw_ostream::has_colors
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
Definition: raw_ostream.h:318
std
Definition: BitVector.h:838
llvm::raw_ostream::RESET
static constexpr Colors RESET
Definition: raw_ostream.h:116
llvm::raw_pwrite_stream::pwrite
void pwrite(const char *Ptr, size_t Size, uint64_t Offset)
Definition: raw_ostream.h:414
llvm::sys::fs::OpenFlags
OpenFlags
Definition: FileSystem.h:756
LLVM_NODISCARD
#define LLVM_NODISCARD
LLVM_NODISCARD - Warn if a type or return value is discarded.
Definition: Compiler.h:161
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::raw_svector_ostream::~raw_svector_ostream
~raw_svector_ostream() override=default
llvm::raw_ostream::SAVEDCOLOR
static constexpr Colors SAVEDCOLOR
Definition: raw_ostream.h:115
llvm::raw_fd_ostream::~raw_fd_ostream
~raw_fd_ostream() override
Definition: raw_ostream.cpp:660
llvm::raw_ostream::operator<<
raw_ostream & operator<<(const std::string &Str)
Definition: raw_ostream.h:231
llvm::raw_pwrite_stream::raw_pwrite_stream
raw_pwrite_stream(bool Unbuffered=false, OStreamKind K=OStreamKind::OK_OStream)
Definition: raw_ostream.h:411
llvm::raw_fd_ostream::lock
LLVM_NODISCARD Expected< sys::fs::FileLocker > lock()
Locks the underlying file.
Definition: raw_ostream.cpp:863
llvm::raw_fd_stream::raw_fd_stream
raw_fd_stream(StringRef Filename, std::error_code &EC)
Open the specified file for reading/writing/seeking.
Definition: raw_ostream.cpp:908
llvm::raw_ostream::SetBufferSize
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
Definition: raw_ostream.h:152
llvm::FormattedString
This is a helper class for left_justify, right_justify, and center_justify.
Definition: Format.h:129
llvm::raw_svector_ostream::reserveExtraSpace
void reserveExtraSpace(uint64_t ExtraSize) override
If possible, pre-allocate ExtraSize bytes for stream data.
Definition: raw_ostream.h:674
llvm::raw_string_ostream::~raw_string_ostream
~raw_string_ostream() override
Definition: raw_ostream.cpp:939
SmallVector.h
llvm::raw_fd_stream
A raw_ostream of a file for reading/writing/seeking.
Definition: raw_ostream.h:586
llvm::buffer_ostream::~buffer_ostream
~buffer_ostream() override
Definition: raw_ostream.h:702
llvm::raw_ostream::indent
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
Definition: raw_ostream.cpp:497
llvm::raw_ostream::BLACK
static constexpr Colors BLACK
Definition: raw_ostream.h:107
N
#define N
llvm::raw_ostream::Colors
Colors
Definition: raw_ostream.h:94
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:647
llvm::StringRef::data
const LLVM_NODISCARD char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:152
llvm::raw_ostream::resetColor
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
Definition: raw_ostream.cpp:535
llvm::SmallVectorImpl< char >
DataTypes.h
llvm::formatv_object_base
Definition: FormatVariadic.h:65
llvm::raw_ostream::BLUE
static constexpr Colors BLUE
Definition: raw_ostream.h:111
llvm::raw_ostream::uuid_t
uint8_t[16] uuid_t
Output a formatted UUID with dash separators.
Definition: raw_ostream.h:263
llvm::raw_ostream::Colors::GREEN
@ GREEN
llvm::raw_ostream::getBufferStart
const char * getBufferStart() const
Return the beginning of the current stream buffer, or 0 if the stream is unbuffered.
Definition: raw_ostream.h:365
llvm::raw_ostream::YELLOW
static constexpr Colors YELLOW
Definition: raw_ostream.h:110
llvm::StringRef::size
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:160
llvm::SmallVectorImpl::reserve
void reserve(size_type N)
Definition: SmallVector.h:624
llvm::raw_string_ostream::str
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
Definition: raw_ostream.h:632
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::raw_ostream::write_uuid
raw_ostream & write_uuid(const uuid_t UUID)
Definition: raw_ostream.cpp:153
llvm::raw_ostream::operator<<
raw_ostream & operator<<(unsigned char C)
Definition: raw_ostream.h:195
llvm::raw_ostream::write_hex
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
Definition: raw_ostream.cpp:140
llvm::raw_svector_ostream::raw_svector_ostream
raw_svector_ostream(SmallVectorImpl< char > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:663