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