WPILibC++  unspecified
raw_ostream.h
1 //===--- raw_ostream.h - Raw output stream ----------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the raw_ostream class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_SUPPORT_RAW_OSTREAM_H
15 #define LLVM_SUPPORT_RAW_OSTREAM_H
16 
17 #include "llvm/FileSystem.h"
18 #include "llvm/SmallVector.h"
19 #include "llvm/StringRef.h"
20 #include <cstdint>
21 #include <system_error>
22 
23 namespace llvm {
24 class format_object_base;
25 class FormattedString;
26 class FormattedNumber;
27 template <typename T> class SmallVectorImpl;
28 
33 class raw_ostream {
34 private:
35  void operator=(const raw_ostream &) = delete;
36  raw_ostream(const raw_ostream &) = delete;
37 
56  char *OutBufStart, *OutBufEnd, *OutBufCur;
57 
58  enum BufferKind {
59  Unbuffered = 0,
60  InternalBuffer,
61  ExternalBuffer
62  } BufferMode;
63 
64 public:
65  // color order matches ANSI escape sequence, don't change
66  enum Colors {
67  BLACK=0,
68  RED,
69  GREEN,
70  YELLOW,
71  BLUE,
72  MAGENTA,
73  CYAN,
74  WHITE,
75  SAVEDCOLOR
76  };
77 
78  explicit raw_ostream(bool unbuffered = false)
79  : BufferMode(unbuffered ? Unbuffered : InternalBuffer) {
80  // Start out ready to flush.
81  OutBufStart = OutBufEnd = OutBufCur = nullptr;
82  }
83 
84  virtual ~raw_ostream();
85 
87  uint64_t tell() const { return current_pos() + GetNumBytesInBuffer(); }
88 
89  //===--------------------------------------------------------------------===//
90  // Configuration Interface
91  //===--------------------------------------------------------------------===//
92 
95  void SetBuffered();
96 
98  void SetBufferSize(size_t Size) {
99  flush();
100  SetBufferAndMode(new char[Size], Size, InternalBuffer);
101  }
102 
103  size_t GetBufferSize() const {
104  // If we're supposed to be buffered but haven't actually gotten around
105  // to allocating the buffer yet, return the value that would be used.
106  if (BufferMode != Unbuffered && OutBufStart == nullptr)
107  return preferred_buffer_size();
108 
109  // Otherwise just return the size of the allocated buffer.
110  return OutBufEnd - OutBufStart;
111  }
112 
116  void SetUnbuffered() {
117  flush();
118  SetBufferAndMode(nullptr, 0, Unbuffered);
119  }
120 
121  size_t GetNumBytesInBuffer() const {
122  return OutBufCur - OutBufStart;
123  }
124 
125  //===--------------------------------------------------------------------===//
126  // Data Output Interface
127  //===--------------------------------------------------------------------===//
128 
129  void flush() {
130  if (OutBufCur != OutBufStart)
131  flush_nonempty();
132  }
133 
134  raw_ostream &operator<<(char C) {
135  if (OutBufCur >= OutBufEnd)
136  return write(C);
137  *OutBufCur++ = C;
138  return *this;
139  }
140 
141  raw_ostream &operator<<(unsigned char C) {
142  if (OutBufCur >= OutBufEnd)
143  return write(C);
144  *OutBufCur++ = C;
145  return *this;
146  }
147 
148  raw_ostream &operator<<(signed char C) {
149  if (OutBufCur >= OutBufEnd)
150  return write(C);
151  *OutBufCur++ = C;
152  return *this;
153  }
154 
155  raw_ostream &operator<<(StringRef Str) {
156  // Inline fast path, particularly for strings with a known length.
157  size_t Size = Str.size();
158 
159  // Make sure we can use the fast path.
160  if (Size > (size_t)(OutBufEnd - OutBufCur))
161  return write(Str.data(), Size);
162 
163  if (Size) {
164  memcpy(OutBufCur, Str.data(), Size);
165  OutBufCur += Size;
166  }
167  return *this;
168  }
169 
170  raw_ostream &operator<<(const char *Str) {
171  // Inline fast path, particularly for constant strings where a sufficiently
172  // smart compiler will simplify strlen.
173 
174  return this->operator<<(StringRef(Str));
175  }
176 
177  raw_ostream &operator<<(const std::string &Str) {
178  // Avoid the fast path, it would only increase code size for a marginal win.
179  return write(Str.data(), Str.length());
180  }
181 
182  raw_ostream &operator<<(const llvm::SmallVectorImpl<char> &Str) {
183  return write(Str.data(), Str.size());
184  }
185 
186  raw_ostream &operator<<(unsigned long N);
187  raw_ostream &operator<<(long N);
188  raw_ostream &operator<<(unsigned long long N);
189  raw_ostream &operator<<(long long N);
190  raw_ostream &operator<<(const void *P);
191  raw_ostream &operator<<(unsigned int N) {
192  return this->operator<<(static_cast<unsigned long>(N));
193  }
194 
195  raw_ostream &operator<<(int N) {
196  return this->operator<<(static_cast<long>(N));
197  }
198 
199  raw_ostream &operator<<(double N);
200 
202  raw_ostream &write_hex(unsigned long long N);
203 
206  raw_ostream &write_escaped(StringRef Str, bool UseHexEscapes = false);
207 
208  raw_ostream &write(unsigned char C);
209  raw_ostream &write(const char *Ptr, size_t Size);
210 
211  // Formatted output, see the format() function in Support/Format.h.
212  raw_ostream &operator<<(const format_object_base &Fmt);
213 
214  // Formatted output, see the leftJustify() function in Support/Format.h.
215  raw_ostream &operator<<(const FormattedString &);
216 
217  // Formatted output, see the formatHex() function in Support/Format.h.
218  raw_ostream &operator<<(const FormattedNumber &);
219 
221  raw_ostream &indent(unsigned NumSpaces);
222 
230  virtual raw_ostream &changeColor(enum Colors Color,
231  bool Bold = false,
232  bool BG = false) {
233  (void)Color;
234  (void)Bold;
235  (void)BG;
236  return *this;
237  }
238 
241  virtual raw_ostream &resetColor() { return *this; }
242 
244  virtual raw_ostream &reverseColor() { return *this; }
245 
249  virtual bool is_displayed() const { return false; }
250 
252  virtual bool has_colors() const { return is_displayed(); }
253 
254  //===--------------------------------------------------------------------===//
255  // Subclass Interface
256  //===--------------------------------------------------------------------===//
257 
258 private:
272  virtual void write_impl(const char *Ptr, size_t Size) = 0;
273 
274  // An out of line virtual method to provide a home for the class vtable.
275  virtual void handle();
276 
279  virtual uint64_t current_pos() const = 0;
280 
281 protected:
285  void SetBuffer(char *BufferStart, size_t Size) {
286  SetBufferAndMode(BufferStart, Size, ExternalBuffer);
287  }
288 
290  virtual size_t preferred_buffer_size() const;
291 
294  const char *getBufferStart() const { return OutBufStart; }
295 
296  //===--------------------------------------------------------------------===//
297  // Private Interface
298  //===--------------------------------------------------------------------===//
299 private:
301  void SetBufferAndMode(char *BufferStart, size_t Size, BufferKind Mode);
302 
305  void flush_nonempty();
306 
309  void copy_to_buffer(const char *Ptr, size_t Size);
310 };
311 
316  virtual void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) = 0;
317 
318 public:
319  explicit raw_pwrite_stream(bool Unbuffered = false)
320  : raw_ostream(Unbuffered) {}
321  void pwrite(const char *Ptr, size_t Size, uint64_t Offset) {
322 #ifndef NDBEBUG
323  uint64_t Pos = tell();
324  // /dev/null always reports a pos of 0, so we cannot perform this check
325  // in that case.
326  if (Pos)
327  assert(Size + Offset <= Pos && "We don't support extending the stream");
328 #endif
329  pwrite_impl(Ptr, Size, Offset);
330  }
331 };
332 
333 //===----------------------------------------------------------------------===//
334 // File Output Streams
335 //===----------------------------------------------------------------------===//
336 
340  int FD;
341  bool ShouldClose;
342 
345  bool Error;
346 
347  uint64_t pos;
348 
349  bool SupportsSeeking;
350 
352  void write_impl(const char *Ptr, size_t Size) override;
353 
354  void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
355 
358  uint64_t current_pos() const override { return pos; }
359 
361  size_t preferred_buffer_size() const override;
362 
364  void error_detected() { Error = true; }
365 
366 public:
377  raw_fd_ostream(StringRef Filename, std::error_code &EC,
378  sys::fs::OpenFlags Flags);
379 
382  raw_fd_ostream(int fd, bool shouldClose, bool unbuffered=false);
383 
384  ~raw_fd_ostream() override;
385 
388  void close();
389 
390  bool supportsSeeking() { return SupportsSeeking; }
391 
394  uint64_t seek(uint64_t off);
395 
400  bool has_error() const {
401  return Error;
402  }
403 
413  void clear_error() {
414  Error = false;
415  }
416 };
417 
420 raw_ostream &outs();
421 
424 raw_ostream &errs();
425 
427 raw_ostream &nulls();
428 
429 //===----------------------------------------------------------------------===//
430 // Output Stream Adaptors
431 //===----------------------------------------------------------------------===//
432 
436  std::string &OS;
437 
439  void write_impl(const char *Ptr, size_t Size) override;
440 
443  uint64_t current_pos() const override { return OS.size(); }
444 
445 public:
446  explicit raw_string_ostream(std::string &O) : OS(O) {}
447  ~raw_string_ostream() override;
448 
451  std::string& str() {
452  flush();
453  return OS;
454  }
455 };
456 
464 
466  void write_impl(const char *Ptr, size_t Size) override;
467 
468  void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
469 
471  uint64_t current_pos() const override;
472 
473 public:
479  SetUnbuffered();
480  }
481  ~raw_svector_ostream() override {}
482 
483  void flush() = delete;
484 
486  StringRef str() { return StringRef(OS.data(), OS.size()); }
487 };
488 
492  void write_impl(const char *Ptr, size_t size) override;
493  void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
494 
497  uint64_t current_pos() const override;
498 
499 public:
500  explicit raw_null_ostream() {}
501  ~raw_null_ostream() override;
502 };
503 
505  raw_ostream &OS;
506  SmallVector<char, 0> Buffer;
507 
508 public:
509  buffer_ostream(raw_ostream &OS) : raw_svector_ostream(Buffer), OS(OS) {}
510  ~buffer_ostream() override { OS << str(); }
511 };
512 
513 } // end llvm namespace
514 
515 #endif // LLVM_SUPPORT_RAW_OSTREAM_H
const char * getBufferStart() const
Return the beginning of the current stream buffer, or 0 if the stream is unbuffered.
Definition: raw_ostream.h:294
size_t size() const
size - Get the string size.
Definition: StringRef.h:149
Definition: Path.inc:31
A raw_ostream that discards all output.
Definition: raw_ostream.h:490
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
Definition: raw_ostream.h:252
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.h:230
raw_ostream & indent(unsigned NumSpaces)
indent - Insert &#39;NumSpaces&#39; spaces.
Definition: raw_ostream.cpp:452
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:462
This is a helper class used for left_justify() and right_justify().
Definition: Format.h:123
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:400
void SetBuffer(char *BufferStart, size_t Size)
Use the provided buffer as the raw_ostream buffer.
Definition: raw_ostream.h:285
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:87
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:139
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
Definition: raw_ostream.cpp:156
virtual bool is_displayed() const
This function determines if this stream is connected to a "tty" or "console" window.
Definition: raw_ostream.h:249
This is a helper class used for handling formatted output.
Definition: Format.h:37
void SetUnbuffered()
Set the stream to be unbuffered.
Definition: raw_ostream.h:116
virtual raw_ostream & reverseColor()
Reverses the foreground and background colors.
Definition: raw_ostream.h:244
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
Definition: raw_ostream.h:98
std::string & str()
Flushes the stream contents to the target string and returns the string&#39;s reference.
Definition: raw_ostream.h:451
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning &#39;\&#39;, &#39;&#39;, &#39; &#39;, &#39;"&#39;, and anything that doesn&#39;t satisfy std::isprint into an escape ...
Definition: raw_ostream.cpp:174
This is a helper class used for format_hex() and format_decimal().
Definition: Format.h:149
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:486
virtual size_t preferred_buffer_size() const
Return an efficient buffer size for the underlying output mechanism.
Definition: raw_ostream.cpp:71
void SetBuffered()
Set the stream to be buffered, with an automatically determined buffer size.
Definition: raw_ostream.cpp:76
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:339
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:134
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:315
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:435
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
Definition: raw_ostream.h:241
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:33
raw_svector_ostream(SmallVectorImpl< char > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:478
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42
Definition: raw_ostream.h:504
void clear_error()
Set the flag read by has_error() to false.
Definition: raw_ostream.h:413