llvm.org GIT mirror llvm / dc45a1e
Don't repeat names in comments. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@234412 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
1 changed file(s) with 84 addition(s) and 93 deletion(s). Raw diff Collapse all Expand all
1919 #include
2020
2121 namespace llvm {
22 class format_object_base;
23 class FormattedString;
24 class FormattedNumber;
25 template
26 class SmallVectorImpl;
27
28 namespace sys {
29 namespace fs {
30 enum OpenFlags : unsigned;
31 }
32 }
33
34 /// raw_ostream - This class implements an extremely fast bulk output stream
35 /// that can *only* output to a stream. It does not support seeking, reopening,
36 /// rewinding, line buffered disciplines etc. It is a simple buffer that outputs
22 class format_object_base;
23 class FormattedString;
24 class FormattedNumber;
25 template class SmallVectorImpl;
26
27 namespace sys {
28 namespace fs {
29 enum OpenFlags : unsigned;
30 }
31 }
32
33 /// This class implements an extremely fast bulk output stream that can *only*
34 /// output to a stream. It does not support seeking, reopening, rewinding, line
35 /// buffered disciplines etc. It is a simple buffer that outputs
3736 /// a chunk at a time.
3837 class raw_ostream {
3938 private:
9594 // Configuration Interface
9695 //===--------------------------------------------------------------------===//
9796
98 /// SetBuffered - Set the stream to be buffered, with an automatically
99 /// determined buffer size.
97 /// Set the stream to be buffered, with an automatically determined buffer
98 /// size.
10099 void SetBuffered();
101100
102 /// SetBufferSize - Set the stream to be buffered, using the
103 /// specified buffer size.
101 /// Set the stream to be buffered, using the specified buffer size.
104102 void SetBufferSize(size_t Size) {
105103 flush();
106104 SetBufferAndMode(new char[Size], Size, InternalBuffer);
116114 return OutBufEnd - OutBufStart;
117115 }
118116
119 /// SetUnbuffered - Set the stream to be unbuffered. When
120 /// unbuffered, the stream will flush after every write. This routine
121 /// will also flush the buffer immediately when the stream is being
122 /// set to unbuffered.
117 /// Set the stream to be unbuffered. When unbuffered, the stream will flush
118 /// after every write. This routine will also flush the buffer immediately
119 /// when the stream is being set to unbuffered.
123120 void SetUnbuffered() {
124121 flush();
125122 SetBufferAndMode(nullptr, 0, Unbuffered);
203200
204201 raw_ostream &operator<<(double N);
205202
206 /// write_hex - Output \p N in hexadecimal, without any prefix or padding.
203 /// Output \p N in hexadecimal, without any prefix or padding.
207204 raw_ostream &write_hex(unsigned long long N);
208205
209 /// write_escaped - Output \p Str, turning '\\', '\t', '\n', '"', and
210 /// anything that doesn't satisfy std::isprint into an escape sequence.
206 /// Output \p Str, turning '\\', '\t', '\n', '"', and anything that doesn't
207 /// satisfy std::isprint into an escape sequence.
211208 raw_ostream &write_escaped(StringRef Str, bool UseHexEscapes = false);
212209
213210 raw_ostream &write(unsigned char C);
262259 //===--------------------------------------------------------------------===//
263260
264261 private:
265 /// write_impl - The is the piece of the class that is implemented
266 /// by subclasses. This writes the \p Size bytes starting at
262 /// The is the piece of the class that is implemented by subclasses. This
263 /// writes the \p Size bytes starting at
267264 /// \p Ptr to the underlying stream.
268265 ///
269266 /// This function is guaranteed to only be called at a point at which it is
280277 // An out of line virtual method to provide a home for the class vtable.
281278 virtual void handle();
282279
283 /// current_pos - Return the current position within the stream, not
284 /// counting the bytes currently in the buffer.
280 /// Return the current position within the stream, not counting the bytes
281 /// currently in the buffer.
285282 virtual uint64_t current_pos() const = 0;
286283
287284 protected:
288 /// SetBuffer - Use the provided buffer as the raw_ostream buffer. This is
289 /// intended for use only by subclasses which can arrange for the output to go
290 /// directly into the desired output buffer, instead of being copied on each
291 /// flush.
285 /// Use the provided buffer as the raw_ostream buffer. This is intended for
286 /// use only by subclasses which can arrange for the output to go directly
287 /// into the desired output buffer, instead of being copied on each flush.
292288 void SetBuffer(char *BufferStart, size_t Size) {
293289 SetBufferAndMode(BufferStart, Size, ExternalBuffer);
294290 }
295291
296 /// preferred_buffer_size - Return an efficient buffer size for the
297 /// underlying output mechanism.
292 /// Return an efficient buffer size for the underlying output mechanism.
298293 virtual size_t preferred_buffer_size() const;
299294
300 /// getBufferStart - Return the beginning of the current stream buffer, or 0
301 /// if the stream is unbuffered.
295 /// Return the beginning of the current stream buffer, or 0 if the stream is
296 /// unbuffered.
302297 const char *getBufferStart() const { return OutBufStart; }
303298
304299 //===--------------------------------------------------------------------===//
305300 // Private Interface
306301 //===--------------------------------------------------------------------===//
307302 private:
308 /// SetBufferAndMode - Install the given buffer and mode.
303 /// Install the given buffer and mode.
309304 void SetBufferAndMode(char *BufferStart, size_t Size, BufferKind Mode);
310305
311 /// flush_nonempty - Flush the current buffer, which is known to be
312 /// non-empty. This outputs the currently buffered data and resets
313 /// the buffer to empty.
306 /// Flush the current buffer, which is known to be non-empty. This outputs the
307 /// currently buffered data and resets the buffer to empty.
314308 void flush_nonempty();
315309
316 /// copy_to_buffer - Copy data into the buffer. Size must not be
317 /// greater than the number of unused bytes in the buffer.
310 /// Copy data into the buffer. Size must not be greater than the number of
311 /// unused bytes in the buffer.
318312 void copy_to_buffer(const char *Ptr, size_t Size);
319313 };
320314
322316 // File Output Streams
323317 //===----------------------------------------------------------------------===//
324318
325 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
319 /// A raw_ostream that writes to a file descriptor.
326320 ///
327321 class raw_fd_ostream : public raw_ostream {
328322 int FD;
338332
339333 uint64_t pos;
340334
341 /// write_impl - See raw_ostream::write_impl.
335 /// See raw_ostream::write_impl.
342336 void write_impl(const char *Ptr, size_t Size) override;
343337
344 /// current_pos - Return the current position within the stream, not
345 /// counting the bytes currently in the buffer.
338 /// Return the current position within the stream, not counting the bytes
339 /// currently in the buffer.
346340 uint64_t current_pos() const override { return pos; }
347341
348 /// preferred_buffer_size - Determine an efficient buffer size.
342 /// Determine an efficient buffer size.
349343 size_t preferred_buffer_size() const override;
350344
351 /// error_detected - Set the flag indicating that an output error has
352 /// been encountered.
345 /// Set the flag indicating that an output error has been encountered.
353346 void error_detected() { Error = true; }
354347
355348 public:
366359 raw_fd_ostream(StringRef Filename, std::error_code &EC,
367360 sys::fs::OpenFlags Flags);
368361
369 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
370 /// ShouldClose is true, this closes the file when the stream is destroyed.
362 /// FD is the file descriptor that this writes to. If ShouldClose is true,
363 /// this closes the file when the stream is destroyed.
371364 raw_fd_ostream(int fd, bool shouldClose, bool unbuffered=false);
372365
373366 ~raw_fd_ostream();
374367
375 /// close - Manually flush the stream and close the file.
376 /// Note that this does not call fsync.
368 /// Manually flush the stream and close the file. Note that this does not call
369 /// fsync.
377370 void close();
378371
379 /// seek - Flushes the stream and repositions the underlying file descriptor
380 /// position to the offset specified from the beginning of the file.
372 /// Flushes the stream and repositions the underlying file descriptor position
373 /// to the offset specified from the beginning of the file.
381374 uint64_t seek(uint64_t off);
382375
383 /// SetUseAtomicWrite - Set the stream to attempt to use atomic writes for
384 /// individual output routines where possible.
376 /// Set the stream to attempt to use atomic writes for individual output
377 /// routines where possible.
385378 ///
386379 /// Note that because raw_ostream's are typically buffered, this flag is only
387380 /// sensible when used on unbuffered streams which will flush their output
400393
401394 bool has_colors() const override;
402395
403 /// has_error - Return the value of the flag in this raw_fd_ostream indicating
404 /// whether an output error has been encountered.
396 /// Return the value of the flag in this raw_fd_ostream indicating whether an
397 /// output error has been encountered.
405398 /// This doesn't implicitly flush any pending output. Also, it doesn't
406399 /// guarantee to detect all errors unless the stream has been closed.
407400 bool has_error() const {
408401 return Error;
409402 }
410403
411 /// clear_error - Set the flag read by has_error() to false. If the error
412 /// flag is set at the time when this raw_ostream's destructor is called,
413 /// report_fatal_error is called to report the error. Use clear_error()
414 /// after handling the error to avoid this behavior.
404 /// Set the flag read by has_error() to false. If the error flag is set at the
405 /// time when this raw_ostream's destructor is called, report_fatal_error is
406 /// called to report the error. Use clear_error() after handling the error to
407 /// avoid this behavior.
415408 ///
416409 /// "Errors should never pass silently.
417410 /// Unless explicitly silenced."
422415 }
423416 };
424417
425 /// outs() - This returns a reference to a raw_ostream for standard output.
426 /// Use it like: outs() << "foo" << "bar";
418 /// This returns a reference to a raw_ostream for standard output. Use it like:
419 /// outs() << "foo" << "bar";
427420 raw_ostream &outs();
428421
429 /// errs() - This returns a reference to a raw_ostream for standard error.
430 /// Use it like: errs() << "foo" << "bar";
422 /// This returns a reference to a raw_ostream for standard error. Use it like:
423 /// errs() << "foo" << "bar";
431424 raw_ostream &errs();
432425
433 /// nulls() - This returns a reference to a raw_ostream which simply discards
434 /// output.
426 /// This returns a reference to a raw_ostream which simply discards output.
435427 raw_ostream &nulls();
436428
437429 //===----------------------------------------------------------------------===//
438430 // Output Stream Adaptors
439431 //===----------------------------------------------------------------------===//
440432
441 /// raw_string_ostream - A raw_ostream that writes to an std::string. This is a
442 /// simple adaptor class. This class does not encounter output errors.
433 /// A raw_ostream that writes to an std::string. This is a simple adaptor
434 /// class. This class does not encounter output errors.
443435 class raw_string_ostream : public raw_ostream {
444436 std::string &OS;
445437
446 /// write_impl - See raw_ostream::write_impl.
438 /// See raw_ostream::write_impl.
447439 void write_impl(const char *Ptr, size_t Size) override;
448440
449 /// current_pos - Return the current position within the stream, not
450 /// counting the bytes currently in the buffer.
441 /// Return the current position within the stream, not counting the bytes
442 /// currently in the buffer.
451443 uint64_t current_pos() const override { return OS.size(); }
452444 public:
453445 explicit raw_string_ostream(std::string &O) : OS(O) {}
454446 ~raw_string_ostream();
455447
456 /// str - Flushes the stream contents to the target string and returns
457 /// the string's reference.
448 /// Flushes the stream contents to the target string and returns the string's
449 /// reference.
458450 std::string& str() {
459451 flush();
460452 return OS;
461453 }
462454 };
463455
464 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
465 /// SmallString. This is a simple adaptor class. This class does not
466 /// encounter output errors.
456 /// A raw_ostream that writes to an SmallVector or SmallString. This is a
457 /// simple adaptor class. This class does not encounter output errors.
467458 class raw_svector_ostream : public raw_ostream {
468459 SmallVectorImpl &OS;
469460
470 /// write_impl - See raw_ostream::write_impl.
461 /// See raw_ostream::write_impl.
471462 void write_impl(const char *Ptr, size_t Size) override;
472463
473 /// current_pos - Return the current position within the stream, not
474 /// counting the bytes currently in the buffer.
464 /// Return the current position within the stream, not counting the bytes
465 /// currently in the buffer.
475466 uint64_t current_pos() const override;
476467 public:
477468 /// Construct a new raw_svector_ostream.
481472 explicit raw_svector_ostream(SmallVectorImpl &O);
482473 ~raw_svector_ostream();
483474
484 /// resync - This is called when the SmallVector we're appending to is changed
485 /// outside of the raw_svector_ostream's control. It is only safe to do this
486 /// if the raw_svector_ostream has previously been flushed.
475 /// This is called when the SmallVector we're appending to is changed outside
476 /// of the raw_svector_ostream's control. It is only safe to do this if the
477 /// raw_svector_ostream has previously been flushed.
487478 void resync();
488479
489 /// str - Flushes the stream contents to the target vector and return a
490 /// StringRef for the vector contents.
480 /// Flushes the stream contents to the target vector and return a StringRef
481 /// for the vector contents.
491482 StringRef str();
492483 };
493484
494 /// raw_null_ostream - A raw_ostream that discards all output.
485 /// A raw_ostream that discards all output.
495486 class raw_null_ostream : public raw_ostream {
496 /// write_impl - See raw_ostream::write_impl.
487 /// See raw_ostream::write_impl.
497488 void write_impl(const char *Ptr, size_t size) override;
498489
499 /// current_pos - Return the current position within the stream, not
500 /// counting the bytes currently in the buffer.
490 /// Return the current position within the stream, not counting the bytes
491 /// currently in the buffer.
501492 uint64_t current_pos() const override;
502493
503494 public: