Coverage Report

Created: 2020-06-26 05:44

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