Mercurial > hg > CbC > CbC_llvm
comparison include/llvm/Support/raw_ostream.h @ 121:803732b1fca8
LLVM 5.0
author | kono |
---|---|
date | Fri, 27 Oct 2017 17:07:41 +0900 |
parents | 1172e4bd9c6f |
children | c2174574ed3a |
comparison
equal
deleted
inserted
replaced
120:1172e4bd9c6f | 121:803732b1fca8 |
---|---|
14 #ifndef LLVM_SUPPORT_RAW_OSTREAM_H | 14 #ifndef LLVM_SUPPORT_RAW_OSTREAM_H |
15 #define LLVM_SUPPORT_RAW_OSTREAM_H | 15 #define LLVM_SUPPORT_RAW_OSTREAM_H |
16 | 16 |
17 #include "llvm/ADT/SmallVector.h" | 17 #include "llvm/ADT/SmallVector.h" |
18 #include "llvm/ADT/StringRef.h" | 18 #include "llvm/ADT/StringRef.h" |
19 #include "llvm/Support/DataTypes.h" | 19 #include <cassert> |
20 #include <cstddef> | |
21 #include <cstdint> | |
22 #include <cstring> | |
23 #include <string> | |
20 #include <system_error> | 24 #include <system_error> |
21 | 25 |
22 namespace llvm { | 26 namespace llvm { |
27 | |
23 class formatv_object_base; | 28 class formatv_object_base; |
24 class format_object_base; | 29 class format_object_base; |
25 class FormattedString; | 30 class FormattedString; |
26 class FormattedNumber; | 31 class FormattedNumber; |
27 class FormattedBytes; | 32 class FormattedBytes; |
28 template <typename T> class SmallVectorImpl; | |
29 | 33 |
30 namespace sys { | 34 namespace sys { |
31 namespace fs { | 35 namespace fs { |
32 enum OpenFlags : unsigned; | 36 enum OpenFlags : unsigned; |
33 } | 37 } // end namespace fs |
34 } | 38 } // end namespace sys |
35 | 39 |
36 /// This class implements an extremely fast bulk output stream that can *only* | 40 /// This class implements an extremely fast bulk output stream that can *only* |
37 /// output to a stream. It does not support seeking, reopening, rewinding, line | 41 /// output to a stream. It does not support seeking, reopening, rewinding, line |
38 /// buffered disciplines etc. It is a simple buffer that outputs | 42 /// buffered disciplines etc. It is a simple buffer that outputs |
39 /// a chunk at a time. | 43 /// a chunk at a time. |
40 class raw_ostream { | 44 class raw_ostream { |
41 private: | 45 private: |
42 void operator=(const raw_ostream &) = delete; | |
43 raw_ostream(const raw_ostream &) = delete; | |
44 | |
45 /// The buffer is handled in such a way that the buffer is | 46 /// The buffer is handled in such a way that the buffer is |
46 /// uninitialized, unbuffered, or out of space when OutBufCur >= | 47 /// uninitialized, unbuffered, or out of space when OutBufCur >= |
47 /// OutBufEnd. Thus a single comparison suffices to determine if we | 48 /// OutBufEnd. Thus a single comparison suffices to determine if we |
48 /// need to take the slow path to write a single character. | 49 /// need to take the slow path to write a single character. |
49 /// | 50 /// |
69 } BufferMode; | 70 } BufferMode; |
70 | 71 |
71 public: | 72 public: |
72 // color order matches ANSI escape sequence, don't change | 73 // color order matches ANSI escape sequence, don't change |
73 enum Colors { | 74 enum Colors { |
74 BLACK=0, | 75 BLACK = 0, |
75 RED, | 76 RED, |
76 GREEN, | 77 GREEN, |
77 YELLOW, | 78 YELLOW, |
78 BLUE, | 79 BLUE, |
79 MAGENTA, | 80 MAGENTA, |
86 : BufferMode(unbuffered ? Unbuffered : InternalBuffer) { | 87 : BufferMode(unbuffered ? Unbuffered : InternalBuffer) { |
87 // Start out ready to flush. | 88 // Start out ready to flush. |
88 OutBufStart = OutBufEnd = OutBufCur = nullptr; | 89 OutBufStart = OutBufEnd = OutBufCur = nullptr; |
89 } | 90 } |
90 | 91 |
92 raw_ostream(const raw_ostream &) = delete; | |
93 void operator=(const raw_ostream &) = delete; | |
94 | |
91 virtual ~raw_ostream(); | 95 virtual ~raw_ostream(); |
92 | 96 |
93 /// tell - Return the current offset with the file. | 97 /// tell - Return the current offset with the file. |
94 uint64_t tell() const { return current_pos() + GetNumBytesInBuffer(); } | 98 uint64_t tell() const { return current_pos() + GetNumBytesInBuffer(); } |
95 | 99 |
184 raw_ostream &operator<<(const std::string &Str) { | 188 raw_ostream &operator<<(const std::string &Str) { |
185 // Avoid the fast path, it would only increase code size for a marginal win. | 189 // Avoid the fast path, it would only increase code size for a marginal win. |
186 return write(Str.data(), Str.length()); | 190 return write(Str.data(), Str.length()); |
187 } | 191 } |
188 | 192 |
189 raw_ostream &operator<<(const llvm::SmallVectorImpl<char> &Str) { | 193 raw_ostream &operator<<(const SmallVectorImpl<char> &Str) { |
190 return write(Str.data(), Str.size()); | 194 return write(Str.data(), Str.size()); |
191 } | 195 } |
192 | 196 |
193 raw_ostream &operator<<(unsigned long N); | 197 raw_ostream &operator<<(unsigned long N); |
194 raw_ostream &operator<<(long N); | 198 raw_ostream &operator<<(long N); |
195 raw_ostream &operator<<(unsigned long long N); | 199 raw_ostream &operator<<(unsigned long long N); |
196 raw_ostream &operator<<(long long N); | 200 raw_ostream &operator<<(long long N); |
197 raw_ostream &operator<<(const void *P); | 201 raw_ostream &operator<<(const void *P); |
202 | |
198 raw_ostream &operator<<(unsigned int N) { | 203 raw_ostream &operator<<(unsigned int N) { |
199 return this->operator<<(static_cast<unsigned long>(N)); | 204 return this->operator<<(static_cast<unsigned long>(N)); |
200 } | 205 } |
201 | 206 |
202 raw_ostream &operator<<(int N) { | 207 raw_ostream &operator<<(int N) { |
205 | 210 |
206 raw_ostream &operator<<(double N); | 211 raw_ostream &operator<<(double N); |
207 | 212 |
208 /// Output \p N in hexadecimal, without any prefix or padding. | 213 /// Output \p N in hexadecimal, without any prefix or padding. |
209 raw_ostream &write_hex(unsigned long long N); | 214 raw_ostream &write_hex(unsigned long long N); |
215 | |
216 /// Output a formatted UUID with dash separators. | |
217 using uuid_t = uint8_t[16]; | |
218 raw_ostream &write_uuid(const uuid_t UUID); | |
210 | 219 |
211 /// Output \p Str, turning '\\', '\t', '\n', '"', and anything that doesn't | 220 /// Output \p Str, turning '\\', '\t', '\n', '"', and anything that doesn't |
212 /// satisfy std::isprint into an escape sequence. | 221 /// satisfy std::isprint into an escape sequence. |
213 raw_ostream &write_escaped(StringRef Str, bool UseHexEscapes = false); | 222 raw_ostream &write_escaped(StringRef Str, bool UseHexEscapes = false); |
214 | 223 |
351 /// | 360 /// |
352 class raw_fd_ostream : public raw_pwrite_stream { | 361 class raw_fd_ostream : public raw_pwrite_stream { |
353 int FD; | 362 int FD; |
354 bool ShouldClose; | 363 bool ShouldClose; |
355 | 364 |
356 /// Error This flag is true if an error of any kind has been detected. | 365 std::error_code EC; |
357 /// | |
358 bool Error; | |
359 | 366 |
360 uint64_t pos; | 367 uint64_t pos; |
361 | 368 |
362 bool SupportsSeeking; | 369 bool SupportsSeeking; |
363 | 370 |
372 | 379 |
373 /// Determine an efficient buffer size. | 380 /// Determine an efficient buffer size. |
374 size_t preferred_buffer_size() const override; | 381 size_t preferred_buffer_size() const override; |
375 | 382 |
376 /// Set the flag indicating that an output error has been encountered. | 383 /// Set the flag indicating that an output error has been encountered. |
377 void error_detected() { Error = true; } | 384 void error_detected(std::error_code EC) { this->EC = EC; } |
378 | 385 |
379 public: | 386 public: |
380 /// Open the specified file for writing. If an error occurs, information | 387 /// Open the specified file for writing. If an error occurs, information |
381 /// about the error is put into EC, and the stream should be immediately | 388 /// about the error is put into EC, and the stream should be immediately |
382 /// destroyed; | 389 /// destroyed; |
383 /// \p Flags allows optional flags to control how the file will be opened. | 390 /// \p Flags allows optional flags to control how the file will be opened. |
384 /// | 391 /// |
385 /// As a special case, if Filename is "-", then the stream will use | 392 /// As a special case, if Filename is "-", then the stream will use |
386 /// STDOUT_FILENO instead of opening a file. Note that it will still consider | 393 /// STDOUT_FILENO instead of opening a file. This will not close the stdout |
387 /// itself to own the file descriptor. In particular, it will close the | 394 /// descriptor. |
388 /// file descriptor when it is done (this is necessary to detect | |
389 /// output errors). | |
390 raw_fd_ostream(StringRef Filename, std::error_code &EC, | 395 raw_fd_ostream(StringRef Filename, std::error_code &EC, |
391 sys::fs::OpenFlags Flags); | 396 sys::fs::OpenFlags Flags); |
392 | 397 |
393 /// FD is the file descriptor that this writes to. If ShouldClose is true, | 398 /// FD is the file descriptor that this writes to. If ShouldClose is true, |
394 /// this closes the file when the stream is destroyed. | 399 /// this closes the file when the stream is destroyed. If FD is for stdout or |
400 /// stderr, it will not be closed. | |
395 raw_fd_ostream(int fd, bool shouldClose, bool unbuffered=false); | 401 raw_fd_ostream(int fd, bool shouldClose, bool unbuffered=false); |
396 | 402 |
397 ~raw_fd_ostream() override; | 403 ~raw_fd_ostream() override; |
398 | 404 |
399 /// Manually flush the stream and close the file. Note that this does not call | 405 /// Manually flush the stream and close the file. Note that this does not call |
413 raw_ostream &reverseColor() override; | 419 raw_ostream &reverseColor() override; |
414 | 420 |
415 bool is_displayed() const override; | 421 bool is_displayed() const override; |
416 | 422 |
417 bool has_colors() const override; | 423 bool has_colors() const override; |
424 | |
425 std::error_code error() const { return EC; } | |
418 | 426 |
419 /// Return the value of the flag in this raw_fd_ostream indicating whether an | 427 /// Return the value of the flag in this raw_fd_ostream indicating whether an |
420 /// output error has been encountered. | 428 /// output error has been encountered. |
421 /// This doesn't implicitly flush any pending output. Also, it doesn't | 429 /// This doesn't implicitly flush any pending output. Also, it doesn't |
422 /// guarantee to detect all errors unless the stream has been closed. | 430 /// guarantee to detect all errors unless the stream has been closed. |
423 bool has_error() const { | 431 bool has_error() const { return bool(EC); } |
424 return Error; | |
425 } | |
426 | 432 |
427 /// Set the flag read by has_error() to false. If the error flag is set at the | 433 /// Set the flag read by has_error() to false. If the error flag is set at the |
428 /// time when this raw_ostream's destructor is called, report_fatal_error is | 434 /// time when this raw_ostream's destructor is called, report_fatal_error is |
429 /// called to report the error. Use clear_error() after handling the error to | 435 /// called to report the error. Use clear_error() after handling the error to |
430 /// avoid this behavior. | 436 /// avoid this behavior. |
431 /// | 437 /// |
432 /// "Errors should never pass silently. | 438 /// "Errors should never pass silently. |
433 /// Unless explicitly silenced." | 439 /// Unless explicitly silenced." |
434 /// - from The Zen of Python, by Tim Peters | 440 /// - from The Zen of Python, by Tim Peters |
435 /// | 441 /// |
436 void clear_error() { | 442 void clear_error() { EC = std::error_code(); } |
437 Error = false; | |
438 } | |
439 }; | 443 }; |
440 | 444 |
441 /// This returns a reference to a raw_ostream for standard output. Use it like: | 445 /// This returns a reference to a raw_ostream for standard output. Use it like: |
442 /// outs() << "foo" << "bar"; | 446 /// outs() << "foo" << "bar"; |
443 raw_ostream &outs(); | 447 raw_ostream &outs(); |
499 /// \param O The vector to write to; this should generally have at least 128 | 503 /// \param O The vector to write to; this should generally have at least 128 |
500 /// bytes free to avoid any extraneous memory overhead. | 504 /// bytes free to avoid any extraneous memory overhead. |
501 explicit raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) { | 505 explicit raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) { |
502 SetUnbuffered(); | 506 SetUnbuffered(); |
503 } | 507 } |
504 ~raw_svector_ostream() override {} | 508 |
509 ~raw_svector_ostream() override = default; | |
505 | 510 |
506 void flush() = delete; | 511 void flush() = delete; |
507 | 512 |
508 /// Return a StringRef for the vector contents. | 513 /// Return a StringRef for the vector contents. |
509 StringRef str() { return StringRef(OS.data(), OS.size()); } | 514 StringRef str() { return StringRef(OS.data(), OS.size()); } |
518 /// Return the current position within the stream, not counting the bytes | 523 /// Return the current position within the stream, not counting the bytes |
519 /// currently in the buffer. | 524 /// currently in the buffer. |
520 uint64_t current_pos() const override; | 525 uint64_t current_pos() const override; |
521 | 526 |
522 public: | 527 public: |
523 explicit raw_null_ostream() {} | 528 explicit raw_null_ostream() = default; |
524 ~raw_null_ostream() override; | 529 ~raw_null_ostream() override; |
525 }; | 530 }; |
526 | 531 |
527 class buffer_ostream : public raw_svector_ostream { | 532 class buffer_ostream : public raw_svector_ostream { |
528 raw_ostream &OS; | 533 raw_ostream &OS; |
531 public: | 536 public: |
532 buffer_ostream(raw_ostream &OS) : raw_svector_ostream(Buffer), OS(OS) {} | 537 buffer_ostream(raw_ostream &OS) : raw_svector_ostream(Buffer), OS(OS) {} |
533 ~buffer_ostream() override { OS << str(); } | 538 ~buffer_ostream() override { OS << str(); } |
534 }; | 539 }; |
535 | 540 |
536 } // end llvm namespace | 541 } // end namespace llvm |
537 | 542 |
538 #endif // LLVM_SUPPORT_RAW_OSTREAM_H | 543 #endif // LLVM_SUPPORT_RAW_OSTREAM_H |