LLVM: include/llvm/Support/raw_ostream.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13#ifndef LLVM_SUPPORT_RAW_OSTREAM_H
14#define LLVM_SUPPORT_RAW_OSTREAM_H
15
20#include
21#include
22#include
23#include
24#include
25#include
26#include <string_view>
27#include <system_error>
28#include <type_traits>
29
30namespace llvm {
31
38template class [[nodiscard]] Expected;
39
40namespace sys {
41namespace fs {
46}
47}
48
49
50
51
52
54public:
55
61
62private:
63 OStreamKind Kind;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 char *OutBufStart, *OutBufEnd, *OutBufCur;
84 bool ColorEnabled = false;
85
86 enum class BufferKind {
87 Unbuffered = 0,
88 InternalBuffer,
89 ExternalBuffer
90 } BufferMode;
91
92public:
93
114
133
136 : Kind(K), BufferMode(unbuffered ? BufferKind::Unbuffered
137 : BufferKind::InternalBuffer) {
138
139 OutBufStart = OutBufEnd = OutBufCur = nullptr;
140 }
141
144
146
147
149
151
152
153
154
155
156
157
158
159
160
162
163
164
165 void SetBuffered();
166
167
170 SetBufferAndMode(new char[Size], Size, BufferKind::InternalBuffer);
171 }
172
174
175
176 if (BufferMode != BufferKind::Unbuffered && OutBufStart == nullptr)
178
179
180 return OutBufEnd - OutBufStart;
181 }
182
183
184
185
188 SetBufferAndMode(nullptr, 0, BufferKind::Unbuffered);
189 }
190
192 return OutBufCur - OutBufStart;
193 }
194
195
196
197
198
200 if (OutBufCur != OutBufStart)
201 flush_nonempty();
202 }
203
205 if (OutBufCur >= OutBufEnd)
207 *OutBufCur++ = C;
208 return *this;
209 }
210
212 if (OutBufCur >= OutBufEnd)
214 *OutBufCur++ = C;
215 return *this;
216 }
217
219 if (OutBufCur >= OutBufEnd)
221 *OutBufCur++ = C;
222 return *this;
223 }
224
226
227 size_t Size = Str.size();
228
229
230 if (Size > (size_t)(OutBufEnd - OutBufCur))
232
234 memcpy(OutBufCur, Str.data(), Size);
235 OutBufCur += Size;
236 }
237 return *this;
238 }
239
240#if defined(__cpp_char8_t)
241
242
243
244
245
246
247
248
249
250
252#endif
253
260
262
263 return write(Str.data(), Str.length());
264 }
265
267 return write(Str.data(), Str.length());
268 }
269
271 return write(Str.data(), Str.size());
272 }
273
279
281 return this->operator<<(static_cast(N));
282 }
283
285 return this->operator<<(static_cast(N));
286 }
287
289
290
292
293
295
296
299
300
301
303
306
307
309
310
312
313
315
316
318
319
321
322
324
325
327
328
329
330
331
332
333
334
336 bool BG = false);
337
338
339
341
342
344
345
346
347
349
350
351
353
354
355
356 virtual void enable_colors(bool enable) { ColorEnabled = enable; }
357
359
360
361
362
363
364private:
365
366
367
368
369
370
371
372
373
374
375
376
377
378 virtual void write_impl(const char *Ptr, size_t Size) = 0;
379
380
381
382 virtual uint64_t current_pos() const = 0;
383
384protected:
385
386
387
389 SetBufferAndMode(BufferStart, Size, BufferKind::ExternalBuffer);
390 }
391
392
393 virtual size_t preferred_buffer_size() const;
394
395
396
398
399
400
401
402private:
403
404 void SetBufferAndMode(char *BufferStart, size_t Size, BufferKind Mode);
405
406
407
408 void flush_nonempty();
409
410
411
412 void copy_to_buffer(const char *Ptr, size_t Size);
413
414
415
416 bool prepare_colors();
417
418 virtual void anchor();
419};
420
421
422
423template <typename OStream, typename T>
424std::enable_if_t<!std::is_reference_v &&
425 std::is_base_of_v<raw_ostream, OStream>,
426 OStream &&>
427operator<<(OStream &&OS, const T &Value) {
428 OS << Value;
429 return std::move(OS);
430}
431
432
433
434
436 virtual void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) = 0;
437 void anchor() override;
438
439public:
444#ifndef NDEBUG
446
447
448 if (Pos)
449 assert(Size + Offset <= Pos && "We don't support extending the stream");
450#endif
452 }
453};
454
455
456
457
458
459
460
462 int FD;
463 bool ShouldClose;
464 bool SupportsSeeking = false;
465 bool IsRegularFile = false;
466 mutable std::optional HasColors;
467
468
469
471
472#ifdef _WIN32
473
474
475 bool IsWindowsConsole = false;
476#endif
477
478 std::error_code EC;
479
481
482
483 void write_impl(const char *Ptr, size_t Size) override;
484
485 void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
486
487
488
489 uint64_t current_pos() const override { return pos; }
490
491
492 size_t preferred_buffer_size() const override;
493
494 void anchor() override;
495
496protected:
497
499
500
501 int get_fd() const { return FD; }
502
503
505
506public:
507
508
509
510
511
512
513
514
525
526
527
528
529 raw_fd_ostream(int fd, bool shouldClose, bool unbuffered = false,
530 OStreamKind K = OStreamKind::OK_OStream);
531
533
534
535
536 void close();
537
539
541
542
543
545
546 bool is_displayed() const override;
547
548 bool has_colors() const override;
549
550
551
552
553
554
556
557 std::error_code error() const { return EC; }
558
559
560
561
562
564
565
566
567
568
569
570
571
572
573
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
597
598
599
600
601
602
603
604
607};
608
609
610
612
613
614
615
616
617
619
620
622
623
624
625
626
627
628
630public:
631
632
633
635
637
638
639
640
641
642
643
644
645
646
648
649
651};
652
653
654
655
656
657
658
659
660
661
663 std::string &OS;
664
665
666 void write_impl(const char *Ptr, size_t Size) override;
667
668
669
670 uint64_t current_pos() const override { return OS.size(); }
671
672public:
676
677
678
679
680 std::string &str() { return OS; }
681
683 OS.reserve(tell() + ExtraSize);
684 }
685};
686
687
688
689
690
691
694
695
696 void write_impl(const char *Ptr, size_t Size) override;
697
698 void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
699
700
701 uint64_t current_pos() const override;
702
703public:
704
705
706
707
710 OS(O) {
711
712
714 }
715
717
719
720
723
725 OS.reserve(tell() + ExtraSize);
726 }
727
728 static bool classof(const raw_ostream *OS);
729};
730
731
733
734 void write_impl(const char *Ptr, size_t size) override;
735 void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
736
737
738
739 uint64_t current_pos() const override;
740
741public:
744};
745
749
750 void anchor() override;
751
752public:
755};
756
758 std::unique_ptr<raw_ostream> OS;
760
761 void anchor() override;
762
763public:
766
767
768 this->OS->SetUnbuffered();
769 }
771};
772
773
774
775
776
777
778
779
780
781
826
830
831class Error;
832
833
834
835
836
837
838
840 std::function<Error(raw_ostream &)> Write);
841
843
844template <typename T, typename = decltype(std::declval<raw_ostream &>()
845 << std::declval<const T &>())>
847 if (O)
848 OS << *O;
849 else
850 OS << std::nullopt;
851 return OS;
852}
853
854}
855
856#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
raw_ostream & operator<<(raw_ostream &OS, const binary_le_impl< value_type > &BLE)
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
static void write(bool isBE, void *P, T V)
This file defines the SmallVector class.
std::pair< llvm::MachO::Target, std::string > UUID
Tagged union holding either a T or a Error.
This is a helper class used for format_hex() and format_decimal().
This is a helper class for left_justify, right_justify, and center_justify.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
buffer_ostream(raw_ostream &OS)
Definition raw_ostream.h:753
~buffer_ostream() override
Definition raw_ostream.h:754
buffer_unique_ostream(std::unique_ptr< raw_ostream > OS)
Definition raw_ostream.h:764
~buffer_unique_ostream() override
Definition raw_ostream.h:770
This is a helper class used for handling formatted output.
A raw_ostream that writes to a file descriptor.
Definition raw_ostream.h:461
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:563
std::error_code error() const
Definition raw_ostream.h:557
void inc_pos(uint64_t Delta)
Definition raw_ostream.h:504
void tie(raw_ostream *TieTo)
Tie this stream to the specified stream.
Definition raw_ostream.h:555
bool supportsSeeking() const
Definition raw_ostream.h:538
void clear_error()
Set the flag read by has_error() to false.
Definition raw_ostream.h:574
bool isRegularFile() const
Definition raw_ostream.h:540
int get_fd() const
Return the file descriptor.
Definition raw_ostream.h:501
raw_fd_ostream(StringRef Filename, std::error_code &EC)
Open the specified file for writing.
void error_detected(std::error_code EC)
Set the flag indicating that an output error has been encountered.
Definition raw_ostream.h:498
static LLVM_ABI bool classof(const raw_ostream *OS)
Check if OS is a pointer of type raw_fd_stream*.
LLVM_ABI raw_fd_stream(StringRef Filename, std::error_code &EC)
Open the specified file for reading/writing/seeking.
LLVM_ABI ssize_t read(char *Ptr, size_t Size)
This reads the Size bytes into a buffer pointed by Ptr.
A raw_ostream that discards all output.
Definition raw_ostream.h:732
raw_null_ostream()
Definition raw_ostream.h:742
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
raw_ostream & operator<<(unsigned char C)
Definition raw_ostream.h:211
raw_ostream(bool unbuffered=false, OStreamKind K=OStreamKind::OK_OStream)
Definition raw_ostream.h:134
uint64_t tell() const
tell - Return the current offset with the file.
Definition raw_ostream.h:148
static constexpr Colors WHITE
Definition raw_ostream.h:122
raw_ostream & operator<<(const std::string_view &Str)
Definition raw_ostream.h:266
static constexpr Colors BRIGHT_RED
Definition raw_ostream.h:124
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
Definition raw_ostream.h:168
Colors
Definition raw_ostream.h:94
@ BRIGHT_RED
Definition raw_ostream.h:104
@ BLACK
Definition raw_ostream.h:95
@ BLUE
Definition raw_ostream.h:99
@ BRIGHT_YELLOW
Definition raw_ostream.h:106
@ CYAN
Definition raw_ostream.h:101
@ BRIGHT_WHITE
Definition raw_ostream.h:110
@ BRIGHT_GREEN
Definition raw_ostream.h:105
@ YELLOW
Definition raw_ostream.h:98
@ GREEN
Definition raw_ostream.h:97
@ RED
Definition raw_ostream.h:96
@ BRIGHT_CYAN
Definition raw_ostream.h:109
@ BRIGHT_BLACK
Definition raw_ostream.h:103
@ RESET
Definition raw_ostream.h:112
@ WHITE
Definition raw_ostream.h:102
@ SAVEDCOLOR
Definition raw_ostream.h:111
@ MAGENTA
Definition raw_ostream.h:100
@ BRIGHT_MAGENTA
Definition raw_ostream.h:108
@ BRIGHT_BLUE
Definition raw_ostream.h:107
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.
size_t GetBufferSize() const
Definition raw_ostream.h:173
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
static constexpr Colors BLACK
Definition raw_ostream.h:115
raw_ostream & write_uuid(const uuid_t UUID)
raw_ostream & operator<<(char C)
Definition raw_ostream.h:204
void flush()
Definition raw_ostream.h:199
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
virtual raw_ostream & reverseColor()
Reverses the foreground and background colors.
void SetBuffer(char *BufferStart, size_t Size)
Use the provided buffer as the raw_ostream buffer.
Definition raw_ostream.h:388
virtual size_t preferred_buffer_size() const
Return an efficient buffer size for the underlying output mechanism.
static constexpr Colors RESET
Definition raw_ostream.h:132
void SetUnbuffered()
Set the stream to be unbuffered.
Definition raw_ostream.h:186
static constexpr Colors GREEN
Definition raw_ostream.h:117
virtual bool is_displayed() const
This function determines if this stream is connected to a "tty" or "console" window.
Definition raw_ostream.h:348
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
static constexpr Colors BLUE
Definition raw_ostream.h:119
static constexpr Colors RED
Definition raw_ostream.h:116
static constexpr Colors MAGENTA
Definition raw_ostream.h:120
const char * getBufferStart() const
Return the beginning of the current stream buffer, or 0 if the stream is unbuffered.
Definition raw_ostream.h:397
raw_ostream & operator<<(signed char C)
Definition raw_ostream.h:218
static constexpr Colors SAVEDCOLOR
Definition raw_ostream.h:131
static constexpr Colors BRIGHT_MAGENTA
Definition raw_ostream.h:128
uint8_t[16] uuid_t
Output a formatted UUID with dash separators.
Definition raw_ostream.h:297
static constexpr Colors YELLOW
Definition raw_ostream.h:118
virtual void enable_colors(bool enable)
Definition raw_ostream.h:356
raw_ostream & operator<<(int N)
Definition raw_ostream.h:284
raw_ostream & operator<<(const char *Str)
Definition raw_ostream.h:254
raw_ostream(const raw_ostream &)=delete
void operator=(const raw_ostream &)=delete
static constexpr Colors BRIGHT_YELLOW
Definition raw_ostream.h:126
raw_ostream & operator<<(const SmallVectorImpl< char > &Str)
Definition raw_ostream.h:270
raw_ostream & operator<<(StringRef Str)
Definition raw_ostream.h:225
static constexpr Colors BRIGHT_CYAN
Definition raw_ostream.h:129
static constexpr Colors CYAN
Definition raw_ostream.h:121
virtual void reserveExtraSpace(uint64_t ExtraSize)
If possible, pre-allocate ExtraSize bytes for stream data.
Definition raw_ostream.h:161
size_t GetNumBytesInBuffer() const
Definition raw_ostream.h:191
OStreamKind
Definition raw_ostream.h:56
@ OK_SVecStream
Definition raw_ostream.h:59
@ OK_OStream
Definition raw_ostream.h:57
@ OK_FDStream
Definition raw_ostream.h:58
raw_ostream & operator<<(const std::string &Str)
Definition raw_ostream.h:261
raw_ostream & operator<<(unsigned int N)
Definition raw_ostream.h:280
static constexpr Colors BRIGHT_BLACK
Definition raw_ostream.h:123
static constexpr Colors BRIGHT_WHITE
Definition raw_ostream.h:130
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
Definition raw_ostream.h:352
static constexpr Colors BRIGHT_GREEN
Definition raw_ostream.h:125
static constexpr Colors BRIGHT_BLUE
Definition raw_ostream.h:127
OStreamKind get_kind() const
Definition raw_ostream.h:150
bool colors_enabled() const
Definition raw_ostream.h:358
raw_pwrite_stream(bool Unbuffered=false, OStreamKind K=OStreamKind::OK_OStream)
Definition raw_ostream.h:440
void pwrite(const char *Ptr, size_t Size, uint64_t Offset)
Definition raw_ostream.h:443
std::string & str()
Returns the string's reference.
Definition raw_ostream.h:680
void reserveExtraSpace(uint64_t ExtraSize) override
If possible, pre-allocate ExtraSize bytes for stream data.
Definition raw_ostream.h:682
raw_string_ostream(std::string &O)
Definition raw_ostream.h:673
~raw_svector_ostream() override=default
SmallVectorImpl< char > & buffer()
Definition raw_ostream.h:722
void reserveExtraSpace(uint64_t ExtraSize) override
If possible, pre-allocate ExtraSize bytes for stream data.
Definition raw_ostream.h:724
StringRef str() const
Return a StringRef for the vector contents.
Definition raw_ostream.h:721
raw_svector_ostream(SmallVectorImpl< char > &O)
Construct a new raw_svector_ostream.
Definition raw_ostream.h:708
RAII class that facilitates file locking.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
LLVM_ABI Error writeToOutput(StringRef OutputFileName, std::function< Error(raw_ostream &)> Write)
This helper creates an output stream and then passes it to Write.
LLVM_ABI raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
@ Timeout
Reached timeout while waiting for the owner to release the lock.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI void write_hex(raw_ostream &S, uint64_t N, HexPrintStyle Style, std::optional< size_t > Width=std::nullopt)
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Implement std::hash so that hash_code can be used in STL containers.
Definition raw_ostream.h:782
unsigned Scale
Definition raw_ostream.h:785
void operator+=(unsigned N)
Definition raw_ostream.h:791
void operator-=(unsigned N)
Definition raw_ostream.h:792
indent(unsigned NumIndents, unsigned Scale=1)
Definition raw_ostream.h:787
indent operator+(unsigned N) const
Definition raw_ostream.h:796
indent operator--(int)
Definition raw_ostream.h:815
indent operator++(int)
Definition raw_ostream.h:805
indent operator-(unsigned N) const
Definition raw_ostream.h:797
indent & operator--()
Definition raw_ostream.h:810
unsigned NumIndents
Definition raw_ostream.h:784
indent & operator=(unsigned N)
Definition raw_ostream.h:821
indent & operator++()
Definition raw_ostream.h:801