xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Support/raw_ostream.h (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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 <cassert>
19 #include <cstddef>
20 #include <cstdint>
21 #include <cstring>
22 #include <string>
23 #include <system_error>
24 #include <type_traits>
25 
26 namespace llvm {
27 
28 class formatv_object_base;
29 class format_object_base;
30 class FormattedString;
31 class FormattedNumber;
32 class FormattedBytes;
33 
34 namespace sys {
35 namespace fs {
36 enum FileAccess : unsigned;
37 enum OpenFlags : unsigned;
38 enum CreationDisposition : unsigned;
39 } // end namespace fs
40 } // end namespace sys
41 
42 /// This class implements an extremely fast bulk output stream that can *only*
43 /// output to a stream.  It does not support seeking, reopening, rewinding, line
44 /// buffered disciplines etc. It is a simple buffer that outputs
45 /// a chunk at a time.
46 class raw_ostream {
47 private:
48   /// The buffer is handled in such a way that the buffer is
49   /// uninitialized, unbuffered, or out of space when OutBufCur >=
50   /// OutBufEnd. Thus a single comparison suffices to determine if we
51   /// need to take the slow path to write a single character.
52   ///
53   /// The buffer is in one of three states:
54   ///  1. Unbuffered (BufferMode == Unbuffered)
55   ///  1. Uninitialized (BufferMode != Unbuffered && OutBufStart == 0).
56   ///  2. Buffered (BufferMode != Unbuffered && OutBufStart != 0 &&
57   ///               OutBufEnd - OutBufStart >= 1).
58   ///
59   /// If buffered, then the raw_ostream owns the buffer if (BufferMode ==
60   /// InternalBuffer); otherwise the buffer has been set via SetBuffer and is
61   /// managed by the subclass.
62   ///
63   /// If a subclass installs an external buffer using SetBuffer then it can wait
64   /// for a \see write_impl() call to handle the data which has been put into
65   /// this buffer.
66   char *OutBufStart, *OutBufEnd, *OutBufCur;
67   bool ColorEnabled = false;
68 
69   /// Optional stream this stream is tied to. If this stream is written to, the
70   /// tied-to stream will be flushed first.
71   raw_ostream *TiedStream = nullptr;
72 
73   enum class BufferKind {
74     Unbuffered = 0,
75     InternalBuffer,
76     ExternalBuffer
77   } BufferMode;
78 
79 public:
80   // color order matches ANSI escape sequence, don't change
81   enum class Colors {
82     BLACK = 0,
83     RED,
84     GREEN,
85     YELLOW,
86     BLUE,
87     MAGENTA,
88     CYAN,
89     WHITE,
90     SAVEDCOLOR,
91     RESET,
92   };
93 
94   static constexpr Colors BLACK = Colors::BLACK;
95   static constexpr Colors RED = Colors::RED;
96   static constexpr Colors GREEN = Colors::GREEN;
97   static constexpr Colors YELLOW = Colors::YELLOW;
98   static constexpr Colors BLUE = Colors::BLUE;
99   static constexpr Colors MAGENTA = Colors::MAGENTA;
100   static constexpr Colors CYAN = Colors::CYAN;
101   static constexpr Colors WHITE = Colors::WHITE;
102   static constexpr Colors SAVEDCOLOR = Colors::SAVEDCOLOR;
103   static constexpr Colors RESET = Colors::RESET;
104 
105   explicit raw_ostream(bool unbuffered = false)
106       : BufferMode(unbuffered ? BufferKind::Unbuffered
107                               : BufferKind::InternalBuffer) {
108     // Start out ready to flush.
109     OutBufStart = OutBufEnd = OutBufCur = nullptr;
110   }
111 
112   raw_ostream(const raw_ostream &) = delete;
113   void operator=(const raw_ostream &) = delete;
114 
115   virtual ~raw_ostream();
116 
117   /// tell - Return the current offset with the file.
118   uint64_t tell() const { return current_pos() + GetNumBytesInBuffer(); }
119 
120   //===--------------------------------------------------------------------===//
121   // Configuration Interface
122   //===--------------------------------------------------------------------===//
123 
124   /// Set the stream to be buffered, with an automatically determined buffer
125   /// size.
126   void SetBuffered();
127 
128   /// Set the stream to be buffered, using the specified buffer size.
129   void SetBufferSize(size_t Size) {
130     flush();
131     SetBufferAndMode(new char[Size], Size, BufferKind::InternalBuffer);
132   }
133 
134   size_t GetBufferSize() const {
135     // If we're supposed to be buffered but haven't actually gotten around
136     // to allocating the buffer yet, return the value that would be used.
137     if (BufferMode != BufferKind::Unbuffered && OutBufStart == nullptr)
138       return preferred_buffer_size();
139 
140     // Otherwise just return the size of the allocated buffer.
141     return OutBufEnd - OutBufStart;
142   }
143 
144   /// Set the stream to be unbuffered. When unbuffered, the stream will flush
145   /// after every write. This routine will also flush the buffer immediately
146   /// when the stream is being set to unbuffered.
147   void SetUnbuffered() {
148     flush();
149     SetBufferAndMode(nullptr, 0, BufferKind::Unbuffered);
150   }
151 
152   size_t GetNumBytesInBuffer() const {
153     return OutBufCur - OutBufStart;
154   }
155 
156   //===--------------------------------------------------------------------===//
157   // Data Output Interface
158   //===--------------------------------------------------------------------===//
159 
160   void flush() {
161     if (OutBufCur != OutBufStart)
162       flush_nonempty();
163   }
164 
165   raw_ostream &operator<<(char C) {
166     if (OutBufCur >= OutBufEnd)
167       return write(C);
168     *OutBufCur++ = C;
169     return *this;
170   }
171 
172   raw_ostream &operator<<(unsigned char C) {
173     if (OutBufCur >= OutBufEnd)
174       return write(C);
175     *OutBufCur++ = C;
176     return *this;
177   }
178 
179   raw_ostream &operator<<(signed char C) {
180     if (OutBufCur >= OutBufEnd)
181       return write(C);
182     *OutBufCur++ = C;
183     return *this;
184   }
185 
186   raw_ostream &operator<<(StringRef Str) {
187     // Inline fast path, particularly for strings with a known length.
188     size_t Size = Str.size();
189 
190     // Make sure we can use the fast path.
191     if (Size > (size_t)(OutBufEnd - OutBufCur))
192       return write(Str.data(), Size);
193 
194     if (Size) {
195       memcpy(OutBufCur, Str.data(), Size);
196       OutBufCur += Size;
197     }
198     return *this;
199   }
200 
201   raw_ostream &operator<<(const char *Str) {
202     // Inline fast path, particularly for constant strings where a sufficiently
203     // smart compiler will simplify strlen.
204 
205     return this->operator<<(StringRef(Str));
206   }
207 
208   raw_ostream &operator<<(const std::string &Str) {
209     // Avoid the fast path, it would only increase code size for a marginal win.
210     return write(Str.data(), Str.length());
211   }
212 
213   raw_ostream &operator<<(const SmallVectorImpl<char> &Str) {
214     return write(Str.data(), Str.size());
215   }
216 
217   raw_ostream &operator<<(unsigned long N);
218   raw_ostream &operator<<(long N);
219   raw_ostream &operator<<(unsigned long long N);
220   raw_ostream &operator<<(long long N);
221   raw_ostream &operator<<(const void *P);
222 
223   raw_ostream &operator<<(unsigned int N) {
224     return this->operator<<(static_cast<unsigned long>(N));
225   }
226 
227   raw_ostream &operator<<(int N) {
228     return this->operator<<(static_cast<long>(N));
229   }
230 
231   raw_ostream &operator<<(double N);
232 
233   /// Output \p N in hexadecimal, without any prefix or padding.
234   raw_ostream &write_hex(unsigned long long N);
235 
236   // Change the foreground color of text.
237   raw_ostream &operator<<(Colors C);
238 
239   /// Output a formatted UUID with dash separators.
240   using uuid_t = uint8_t[16];
241   raw_ostream &write_uuid(const uuid_t UUID);
242 
243   /// Output \p Str, turning '\\', '\t', '\n', '"', and anything that doesn't
244   /// satisfy llvm::isPrint into an escape sequence.
245   raw_ostream &write_escaped(StringRef Str, bool UseHexEscapes = false);
246 
247   raw_ostream &write(unsigned char C);
248   raw_ostream &write(const char *Ptr, size_t Size);
249 
250   // Formatted output, see the format() function in Support/Format.h.
251   raw_ostream &operator<<(const format_object_base &Fmt);
252 
253   // Formatted output, see the leftJustify() function in Support/Format.h.
254   raw_ostream &operator<<(const FormattedString &);
255 
256   // Formatted output, see the formatHex() function in Support/Format.h.
257   raw_ostream &operator<<(const FormattedNumber &);
258 
259   // Formatted output, see the formatv() function in Support/FormatVariadic.h.
260   raw_ostream &operator<<(const formatv_object_base &);
261 
262   // Formatted output, see the format_bytes() function in Support/Format.h.
263   raw_ostream &operator<<(const FormattedBytes &);
264 
265   /// indent - Insert 'NumSpaces' spaces.
266   raw_ostream &indent(unsigned NumSpaces);
267 
268   /// write_zeros - Insert 'NumZeros' nulls.
269   raw_ostream &write_zeros(unsigned NumZeros);
270 
271   /// Changes the foreground color of text that will be output from this point
272   /// forward.
273   /// @param Color ANSI color to use, the special SAVEDCOLOR can be used to
274   /// change only the bold attribute, and keep colors untouched
275   /// @param Bold bold/brighter text, default false
276   /// @param BG if true change the background, default: change foreground
277   /// @returns itself so it can be used within << invocations
278   virtual raw_ostream &changeColor(enum Colors Color, bool Bold = false,
279                                    bool BG = false);
280 
281   /// Resets the colors to terminal defaults. Call this when you are done
282   /// outputting colored text, or before program exit.
283   virtual raw_ostream &resetColor();
284 
285   /// Reverses the foreground and background colors.
286   virtual raw_ostream &reverseColor();
287 
288   /// This function determines if this stream is connected to a "tty" or
289   /// "console" window. That is, the output would be displayed to the user
290   /// rather than being put on a pipe or stored in a file.
291   virtual bool is_displayed() const { return false; }
292 
293   /// This function determines if this stream is displayed and supports colors.
294   /// The result is unaffected by calls to enable_color().
295   virtual bool has_colors() const { return is_displayed(); }
296 
297   // Enable or disable colors. Once enable_colors(false) is called,
298   // changeColor() has no effect until enable_colors(true) is called.
299   virtual void enable_colors(bool enable) { ColorEnabled = enable; }
300 
301   /// Tie this stream to the specified stream. Replaces any existing tied-to
302   /// stream. Specifying a nullptr unties the stream.
303   void tie(raw_ostream *TieTo) { TiedStream = TieTo; }
304 
305   //===--------------------------------------------------------------------===//
306   // Subclass Interface
307   //===--------------------------------------------------------------------===//
308 
309 private:
310   /// The is the piece of the class that is implemented by subclasses.  This
311   /// writes the \p Size bytes starting at
312   /// \p Ptr to the underlying stream.
313   ///
314   /// This function is guaranteed to only be called at a point at which it is
315   /// safe for the subclass to install a new buffer via SetBuffer.
316   ///
317   /// \param Ptr The start of the data to be written. For buffered streams this
318   /// is guaranteed to be the start of the buffer.
319   ///
320   /// \param Size The number of bytes to be written.
321   ///
322   /// \invariant { Size > 0 }
323   virtual void write_impl(const char *Ptr, size_t Size) = 0;
324 
325   /// Return the current position within the stream, not counting the bytes
326   /// currently in the buffer.
327   virtual uint64_t current_pos() const = 0;
328 
329 protected:
330   /// Use the provided buffer as the raw_ostream buffer. This is intended for
331   /// use only by subclasses which can arrange for the output to go directly
332   /// into the desired output buffer, instead of being copied on each flush.
333   void SetBuffer(char *BufferStart, size_t Size) {
334     SetBufferAndMode(BufferStart, Size, BufferKind::ExternalBuffer);
335   }
336 
337   /// Return an efficient buffer size for the underlying output mechanism.
338   virtual size_t preferred_buffer_size() const;
339 
340   /// Return the beginning of the current stream buffer, or 0 if the stream is
341   /// unbuffered.
342   const char *getBufferStart() const { return OutBufStart; }
343 
344   //===--------------------------------------------------------------------===//
345   // Private Interface
346   //===--------------------------------------------------------------------===//
347 private:
348   /// Install the given buffer and mode.
349   void SetBufferAndMode(char *BufferStart, size_t Size, BufferKind Mode);
350 
351   /// Flush the current buffer, which is known to be non-empty. This outputs the
352   /// currently buffered data and resets the buffer to empty.
353   void flush_nonempty();
354 
355   /// Copy data into the buffer. Size must not be greater than the number of
356   /// unused bytes in the buffer.
357   void copy_to_buffer(const char *Ptr, size_t Size);
358 
359   /// Compute whether colors should be used and do the necessary work such as
360   /// flushing. The result is affected by calls to enable_color().
361   bool prepare_colors();
362 
363   /// Flush the tied-to stream (if present) and then write the required data.
364   void flush_tied_then_write(const char *Ptr, size_t Size);
365 
366   virtual void anchor();
367 };
368 
369 /// Call the appropriate insertion operator, given an rvalue reference to a
370 /// raw_ostream object and return a stream of the same type as the argument.
371 template <typename OStream, typename T>
372 std::enable_if_t<!std::is_reference<OStream>::value &&
373                      std::is_base_of<raw_ostream, OStream>::value,
374                  OStream &&>
375 operator<<(OStream &&OS, const T &Value) {
376   OS << Value;
377   return std::move(OS);
378 }
379 
380 /// An abstract base class for streams implementations that also support a
381 /// pwrite operation. This is useful for code that can mostly stream out data,
382 /// but needs to patch in a header that needs to know the output size.
383 class raw_pwrite_stream : public raw_ostream {
384   virtual void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) = 0;
385   void anchor() override;
386 
387 public:
388   explicit raw_pwrite_stream(bool Unbuffered = false)
389       : raw_ostream(Unbuffered) {}
390   void pwrite(const char *Ptr, size_t Size, uint64_t Offset) {
391 #ifndef NDEBUG
392     uint64_t Pos = tell();
393     // /dev/null always reports a pos of 0, so we cannot perform this check
394     // in that case.
395     if (Pos)
396       assert(Size + Offset <= Pos && "We don't support extending the stream");
397 #endif
398     pwrite_impl(Ptr, Size, Offset);
399   }
400 };
401 
402 //===----------------------------------------------------------------------===//
403 // File Output Streams
404 //===----------------------------------------------------------------------===//
405 
406 /// A raw_ostream that writes to a file descriptor.
407 ///
408 class raw_fd_ostream : public raw_pwrite_stream {
409   int FD;
410   bool ShouldClose;
411   bool SupportsSeeking = false;
412 
413 #ifdef _WIN32
414   /// True if this fd refers to a Windows console device. Mintty and other
415   /// terminal emulators are TTYs, but they are not consoles.
416   bool IsWindowsConsole = false;
417 #endif
418 
419   std::error_code EC;
420 
421   uint64_t pos = 0;
422 
423   /// See raw_ostream::write_impl.
424   void write_impl(const char *Ptr, size_t Size) override;
425 
426   void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
427 
428   /// Return the current position within the stream, not counting the bytes
429   /// currently in the buffer.
430   uint64_t current_pos() const override { return pos; }
431 
432   /// Determine an efficient buffer size.
433   size_t preferred_buffer_size() const override;
434 
435   /// Set the flag indicating that an output error has been encountered.
436   void error_detected(std::error_code EC) { this->EC = EC; }
437 
438   void anchor() override;
439 
440 public:
441   /// Open the specified file for writing. If an error occurs, information
442   /// about the error is put into EC, and the stream should be immediately
443   /// destroyed;
444   /// \p Flags allows optional flags to control how the file will be opened.
445   ///
446   /// As a special case, if Filename is "-", then the stream will use
447   /// STDOUT_FILENO instead of opening a file. This will not close the stdout
448   /// descriptor.
449   raw_fd_ostream(StringRef Filename, std::error_code &EC);
450   raw_fd_ostream(StringRef Filename, std::error_code &EC,
451                  sys::fs::CreationDisposition Disp);
452   raw_fd_ostream(StringRef Filename, std::error_code &EC,
453                  sys::fs::FileAccess Access);
454   raw_fd_ostream(StringRef Filename, std::error_code &EC,
455                  sys::fs::OpenFlags Flags);
456   raw_fd_ostream(StringRef Filename, std::error_code &EC,
457                  sys::fs::CreationDisposition Disp, sys::fs::FileAccess Access,
458                  sys::fs::OpenFlags Flags);
459 
460   /// FD is the file descriptor that this writes to.  If ShouldClose is true,
461   /// this closes the file when the stream is destroyed. If FD is for stdout or
462   /// stderr, it will not be closed.
463   raw_fd_ostream(int fd, bool shouldClose, bool unbuffered=false);
464 
465   ~raw_fd_ostream() override;
466 
467   /// Manually flush the stream and close the file. Note that this does not call
468   /// fsync.
469   void close();
470 
471   bool supportsSeeking() { return SupportsSeeking; }
472 
473   /// Flushes the stream and repositions the underlying file descriptor position
474   /// to the offset specified from the beginning of the file.
475   uint64_t seek(uint64_t off);
476 
477   bool is_displayed() const override;
478 
479   bool has_colors() const override;
480 
481   std::error_code error() const { return EC; }
482 
483   /// Return the value of the flag in this raw_fd_ostream indicating whether an
484   /// output error has been encountered.
485   /// This doesn't implicitly flush any pending output.  Also, it doesn't
486   /// guarantee to detect all errors unless the stream has been closed.
487   bool has_error() const { return bool(EC); }
488 
489   /// Set the flag read by has_error() to false. If the error flag is set at the
490   /// time when this raw_ostream's destructor is called, report_fatal_error is
491   /// called to report the error. Use clear_error() after handling the error to
492   /// avoid this behavior.
493   ///
494   ///   "Errors should never pass silently.
495   ///    Unless explicitly silenced."
496   ///      - from The Zen of Python, by Tim Peters
497   ///
498   void clear_error() { EC = std::error_code(); }
499 };
500 
501 /// This returns a reference to a raw_fd_ostream for standard output. Use it
502 /// like: outs() << "foo" << "bar";
503 raw_fd_ostream &outs();
504 
505 /// This returns a reference to a raw_ostream for standard error.
506 /// Use it like: errs() << "foo" << "bar";
507 /// By default, the stream is tied to stdout to ensure stdout is flushed before
508 /// stderr is written, to ensure the error messages are written in their
509 /// expected place.
510 raw_fd_ostream &errs();
511 
512 /// This returns a reference to a raw_ostream which simply discards output.
513 raw_ostream &nulls();
514 
515 //===----------------------------------------------------------------------===//
516 // Output Stream Adaptors
517 //===----------------------------------------------------------------------===//
518 
519 /// A raw_ostream that writes to an std::string.  This is a simple adaptor
520 /// class. This class does not encounter output errors.
521 class raw_string_ostream : public raw_ostream {
522   std::string &OS;
523 
524   /// See raw_ostream::write_impl.
525   void write_impl(const char *Ptr, size_t Size) override;
526 
527   /// Return the current position within the stream, not counting the bytes
528   /// currently in the buffer.
529   uint64_t current_pos() const override { return OS.size(); }
530 
531 public:
532   explicit raw_string_ostream(std::string &O) : OS(O) {
533     SetUnbuffered();
534   }
535   ~raw_string_ostream() override;
536 
537   /// Flushes the stream contents to the target string and returns  the string's
538   /// reference.
539   std::string& str() {
540     flush();
541     return OS;
542   }
543 };
544 
545 /// A raw_ostream that writes to an SmallVector or SmallString.  This is a
546 /// simple adaptor class. This class does not encounter output errors.
547 /// raw_svector_ostream operates without a buffer, delegating all memory
548 /// management to the SmallString. Thus the SmallString is always up-to-date,
549 /// may be used directly and there is no need to call flush().
550 class raw_svector_ostream : public raw_pwrite_stream {
551   SmallVectorImpl<char> &OS;
552 
553   /// See raw_ostream::write_impl.
554   void write_impl(const char *Ptr, size_t Size) override;
555 
556   void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
557 
558   /// Return the current position within the stream.
559   uint64_t current_pos() const override;
560 
561 public:
562   /// Construct a new raw_svector_ostream.
563   ///
564   /// \param O The vector to write to; this should generally have at least 128
565   /// bytes free to avoid any extraneous memory overhead.
566   explicit raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {
567     SetUnbuffered();
568   }
569 
570   ~raw_svector_ostream() override = default;
571 
572   void flush() = delete;
573 
574   /// Return a StringRef for the vector contents.
575   StringRef str() const { return StringRef(OS.data(), OS.size()); }
576 };
577 
578 /// A raw_ostream that discards all output.
579 class raw_null_ostream : public raw_pwrite_stream {
580   /// See raw_ostream::write_impl.
581   void write_impl(const char *Ptr, size_t size) override;
582   void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
583 
584   /// Return the current position within the stream, not counting the bytes
585   /// currently in the buffer.
586   uint64_t current_pos() const override;
587 
588 public:
589   explicit raw_null_ostream() = default;
590   ~raw_null_ostream() override;
591 };
592 
593 class buffer_ostream : public raw_svector_ostream {
594   raw_ostream &OS;
595   SmallVector<char, 0> Buffer;
596 
597   virtual void anchor() override;
598 
599 public:
600   buffer_ostream(raw_ostream &OS) : raw_svector_ostream(Buffer), OS(OS) {}
601   ~buffer_ostream() override { OS << str(); }
602 };
603 
604 } // end namespace llvm
605 
606 #endif // LLVM_SUPPORT_RAW_OSTREAM_H
607