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 const Colors BLACK = Colors::BLACK; 90 static const Colors RED = Colors::RED; 91 static const Colors GREEN = Colors::GREEN; 92 static const Colors YELLOW = Colors::YELLOW; 93 static const Colors BLUE = Colors::BLUE; 94 static const Colors MAGENTA = Colors::MAGENTA; 95 static const Colors CYAN = Colors::CYAN; 96 static const Colors WHITE = Colors::WHITE; 97 static const Colors SAVEDCOLOR = Colors::SAVEDCOLOR; 98 static const Colors RESET = Colors::RESET; 99 100 explicit raw_ostream(bool unbuffered = false) 101 : BufferMode(unbuffered ? BufferKind::Unbuffered 102 : BufferKind::InternalBuffer) { 103 // Start out ready to flush. 104 OutBufStart = OutBufEnd = OutBufCur = nullptr; 105 } 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 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 void SetBufferSize(size_t Size) { 125 flush(); 126 SetBufferAndMode(new char[Size], Size, BufferKind::InternalBuffer); 127 } 128 129 size_t GetBufferSize() const { 130 // If we're supposed to be buffered but haven't actually gotten around 131 // to allocating the buffer yet, return the value that would be used. 132 if (BufferMode != BufferKind::Unbuffered && OutBufStart == nullptr) 133 return preferred_buffer_size(); 134 135 // Otherwise just return the size of the allocated buffer. 136 return OutBufEnd - OutBufStart; 137 } 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 void SetUnbuffered() { 143 flush(); 144 SetBufferAndMode(nullptr, 0, BufferKind::Unbuffered); 145 } 146 147 size_t GetNumBytesInBuffer() const { 148 return OutBufCur - OutBufStart; 149 } 150 151 //===--------------------------------------------------------------------===// 152 // Data Output Interface 153 //===--------------------------------------------------------------------===// 154 155 void flush() { 156 if (OutBufCur != OutBufStart) 157 flush_nonempty(); 158 } 159 160 raw_ostream &operator<<(char C) { 161 if (OutBufCur >= OutBufEnd) 162 return write(C); 163 *OutBufCur++ = C; 164 return *this; 165 } 166 167 raw_ostream &operator<<(unsigned char C) { 168 if (OutBufCur >= OutBufEnd) 169 return write(C); 170 *OutBufCur++ = C; 171 return *this; 172 } 173 174 raw_ostream &operator<<(signed char C) { 175 if (OutBufCur >= OutBufEnd) 176 return write(C); 177 *OutBufCur++ = C; 178 return *this; 179 } 180 181 raw_ostream &operator<<(StringRef Str) { 182 // Inline fast path, particularly for strings with a known length. 183 size_t Size = Str.size(); 184 185 // Make sure we can use the fast path. 186 if (Size > (size_t)(OutBufEnd - OutBufCur)) 187 return write(Str.data(), Size); 188 189 if (Size) { 190 memcpy(OutBufCur, Str.data(), Size); 191 OutBufCur += Size; 192 } 193 return *this; 194 } 195 196 raw_ostream &operator<<(const char *Str) { 197 // Inline fast path, particularly for constant strings where a sufficiently 198 // smart compiler will simplify strlen. 199 200 return this->operator<<(StringRef(Str)); 201 } 202 203 raw_ostream &operator<<(const std::string &Str) { 204 // Avoid the fast path, it would only increase code size for a marginal win. 205 return write(Str.data(), Str.length()); 206 } 207 208 raw_ostream &operator<<(const SmallVectorImpl<char> &Str) { 209 return write(Str.data(), Str.size()); 210 } 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 raw_ostream &operator<<(unsigned int N) { 219 return this->operator<<(static_cast<unsigned long>(N)); 220 } 221 222 raw_ostream &operator<<(int N) { 223 return this->operator<<(static_cast<long>(N)); 224 } 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 bool BG = false) { 276 (void)Color; 277 (void)Bold; 278 (void)BG; 279 return *this; 280 } 281 282 /// Resets the colors to terminal defaults. Call this when you are done 283 /// outputting colored text, or before program exit. 284 virtual raw_ostream &resetColor() { return *this; } 285 286 /// Reverses the foreground and background colors. 287 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 virtual bool is_displayed() const { return false; } 293 294 /// This function determines if this stream is displayed and supports colors. 295 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 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 void SetBuffer(char *BufferStart, size_t Size) { 330 SetBufferAndMode(BufferStart, Size, BufferKind::ExternalBuffer); 331 } 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 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 typename std::enable_if<!std::is_reference<OStream>::value && 362 std::is_base_of<raw_ostream, OStream>::value, 363 OStream &&>::type 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 : raw_ostream(Unbuffered) {} 379 void pwrite(const char *Ptr, size_t Size, uint64_t Offset) { 380 #ifndef NDEBUG 381 uint64_t Pos = tell(); 382 // /dev/null always reports a pos of 0, so we cannot perform this check 383 // in that case. 384 if (Pos) 385 assert(Size + Offset <= Pos && "We don't support extending the stream"); 386 #endif 387 pwrite_impl(Ptr, Size, Offset); 388 } 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 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 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 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 void enable_colors(bool enable) override { ColorEnabled = enable; } 478 479 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 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 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 uint64_t current_pos() const override { return OS.size(); } 525 526 public: 527 explicit raw_string_ostream(std::string &O) : OS(O) {} 528 ~raw_string_ostream() override; 529 530 /// Flushes the stream contents to the target string and returns the string's 531 /// reference. 532 std::string& str() { 533 flush(); 534 return OS; 535 } 536 }; 537 538 /// A raw_ostream that writes to an SmallVector or SmallString. This is a 539 /// simple adaptor class. This class does not encounter output errors. 540 /// raw_svector_ostream operates without a buffer, delegating all memory 541 /// management to the SmallString. Thus the SmallString is always up-to-date, 542 /// may be used directly and there is no need to call flush(). 543 class raw_svector_ostream : public raw_pwrite_stream { 544 SmallVectorImpl<char> &OS; 545 546 /// See raw_ostream::write_impl. 547 void write_impl(const char *Ptr, size_t Size) override; 548 549 void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override; 550 551 /// Return the current position within the stream. 552 uint64_t current_pos() const override; 553 554 public: 555 /// Construct a new raw_svector_ostream. 556 /// 557 /// \param O The vector to write to; this should generally have at least 128 558 /// bytes free to avoid any extraneous memory overhead. 559 explicit raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) { 560 SetUnbuffered(); 561 } 562 563 ~raw_svector_ostream() override = default; 564 565 void flush() = delete; 566 567 /// Return a StringRef for the vector contents. 568 StringRef str() { return StringRef(OS.data(), OS.size()); } 569 }; 570 571 /// A raw_ostream that discards all output. 572 class raw_null_ostream : public raw_pwrite_stream { 573 /// See raw_ostream::write_impl. 574 void write_impl(const char *Ptr, size_t size) override; 575 void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override; 576 577 /// Return the current position within the stream, not counting the bytes 578 /// currently in the buffer. 579 uint64_t current_pos() const override; 580 581 public: 582 explicit raw_null_ostream() = default; 583 ~raw_null_ostream() override; 584 }; 585 586 class buffer_ostream : public raw_svector_ostream { 587 raw_ostream &OS; 588 SmallVector<char, 0> Buffer; 589 590 virtual void anchor() override; 591 592 public: 593 buffer_ostream(raw_ostream &OS) : raw_svector_ostream(Buffer), OS(OS) {} 594 ~buffer_ostream() override { OS << str(); } 595 }; 596 597 } // end namespace llvm 598 599 #endif // LLVM_SUPPORT_RAW_OSTREAM_H 600