1 //===--- raw_ostream.h - Raw output stream ----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the raw_ostream class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_RAW_OSTREAM_H
15 #define LLVM_SUPPORT_RAW_OSTREAM_H
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/StringRef.h"
25 class format_object_base;
27 class SmallVectorImpl;
29 /// raw_ostream - This class implements an extremely fast bulk output stream
30 /// that can *only* output to a stream. It does not support seeking, reopening,
31 /// rewinding, line buffered disciplines etc. It is a simple buffer that outputs
32 /// a chunk at a time.
35 /// The buffer is handled in such a way that the buffer is
36 /// uninitialized, unbuffered, or out of space when OutBufCur >=
37 /// OutBufEnd. Thus a single comparison suffices to determine if we
38 /// need to take the slow path to write a single character.
40 /// The buffer is in one of three states:
41 /// 1. Unbuffered (Unbuffered == true)
42 /// 1. Uninitialized (Unbuffered == false && OutBufStart == 0).
43 /// 2. Buffered (Unbuffered == false && OutBufStart != 0 &&
44 /// OutBufEnd - OutBufStart >= 64).
45 char *OutBufStart, *OutBufEnd, *OutBufCur;
48 /// Error This flag is true if an error of any kind has been detected.
53 // color order matches ANSI escape sequence, don't change
66 explicit raw_ostream(bool unbuffered=false)
67 : Unbuffered(unbuffered), Error(false) {
68 // Start out ready to flush.
69 OutBufStart = OutBufEnd = OutBufCur = 0;
72 virtual ~raw_ostream();
74 /// tell - Return the current offset with the file.
75 uint64_t tell() { return current_pos() + GetNumBytesInBuffer(); }
77 /// has_error - Return the value of the flag in this raw_ostream indicating
78 /// whether an output error has been encountered.
79 bool has_error() const {
83 /// clear_error - Set the flag read by has_error() to false. If the error
84 /// flag is set at the time when this raw_ostream's destructor is called,
85 /// llvm_report_error is called to report the error. Use clear_error()
86 /// after handling the error to avoid this behavior.
91 //===--------------------------------------------------------------------===//
92 // Configuration Interface
93 //===--------------------------------------------------------------------===//
95 /// SetBufferSize - Set the internal buffer size to the specified amount
96 /// instead of the default.
97 void SetBufferSize(size_t Size=4096) {
99 "Buffer size must be somewhat large for invariants to hold");
102 delete [] OutBufStart;
103 OutBufStart = new char[Size];
104 OutBufEnd = OutBufStart+Size;
105 OutBufCur = OutBufStart;
109 /// SetUnbuffered - Set the streams buffering status. When
110 /// unbuffered the stream will flush after every write. This routine
111 /// will also flush the buffer immediately when the stream is being
112 /// set to unbuffered.
113 void SetUnbuffered() {
116 delete [] OutBufStart;
117 OutBufStart = OutBufEnd = OutBufCur = 0;
121 size_t GetNumBytesInBuffer() const {
122 return OutBufCur - OutBufStart;
125 //===--------------------------------------------------------------------===//
126 // Data Output Interface
127 //===--------------------------------------------------------------------===//
130 if (OutBufCur != OutBufStart)
134 raw_ostream &operator<<(char C) {
135 if (OutBufCur >= OutBufEnd)
141 raw_ostream &operator<<(unsigned char C) {
142 if (OutBufCur >= OutBufEnd)
148 raw_ostream &operator<<(signed char C) {
149 if (OutBufCur >= OutBufEnd)
155 raw_ostream &operator<<(const StringRef &Str) {
156 // Inline fast path, particularly for strings with a known length.
157 size_t Size = Str.size();
159 // Make sure we can use the fast path.
160 if (OutBufCur+Size > OutBufEnd)
161 return write(Str.data(), Size);
163 memcpy(OutBufCur, Str.data(), Size);
168 raw_ostream &operator<<(const char *Str) {
169 // Inline fast path, particulary for constant strings where a sufficiently
170 // smart compiler will simplify strlen.
172 this->operator<<(StringRef(Str));
176 raw_ostream &operator<<(const std::string& Str) {
177 write(Str.data(), Str.length());
181 raw_ostream &operator<<(unsigned long N);
182 raw_ostream &operator<<(long N);
183 raw_ostream &operator<<(unsigned long long N);
184 raw_ostream &operator<<(long long N);
185 raw_ostream &operator<<(const void *P);
186 raw_ostream &operator<<(unsigned int N) {
187 this->operator<<(static_cast<unsigned long>(N));
191 raw_ostream &operator<<(int N) {
192 this->operator<<(static_cast<long>(N));
196 raw_ostream &operator<<(double N) {
197 this->operator<<(ftostr(N));
201 raw_ostream &write(unsigned char C);
202 raw_ostream &write(const char *Ptr, size_t Size);
204 // Formatted output, see the format() function in Support/Format.h.
205 raw_ostream &operator<<(const format_object_base &Fmt);
207 /// Changes the foreground color of text that will be output from this point
209 /// @param colors ANSI color to use, the special SAVEDCOLOR can be used to
210 /// change only the bold attribute, and keep colors untouched
211 /// @param bold bold/brighter text, default false
212 /// @param bg if true change the background, default: change foreground
213 /// @returns itself so it can be used within << invocations
214 virtual raw_ostream &changeColor(enum Colors colors, bool bold=false,
215 bool bg=false) { return *this; }
217 /// Resets the colors to terminal defaults. Call this when you are done
218 /// outputting colored text, or before program exit.
219 virtual raw_ostream &resetColor() { return *this; }
221 //===--------------------------------------------------------------------===//
222 // Subclass Interface
223 //===--------------------------------------------------------------------===//
226 /// write_impl - The is the piece of the class that is implemented
227 /// by subclasses. This writes the \args Size bytes starting at
228 /// \arg Ptr to the underlying stream.
230 /// \invariant { Size > 0 }
231 virtual void write_impl(const char *Ptr, size_t Size) = 0;
233 // An out of line virtual method to provide a home for the class vtable.
234 virtual void handle();
236 /// current_pos - Return the current position within the stream, not
237 /// counting the bytes currently in the buffer.
238 virtual uint64_t current_pos() = 0;
241 /// error_detected - Set the flag indicating that an output error has
242 /// been encountered.
243 void error_detected() { Error = true; }
245 typedef char * iterator;
246 iterator begin(void) { return OutBufStart; }
247 iterator end(void) { return OutBufCur; }
249 //===--------------------------------------------------------------------===//
251 //===--------------------------------------------------------------------===//
253 /// flush_nonempty - Flush the current buffer, which is known to be
254 /// non-empty. This outputs the currently buffered data and resets
255 /// the buffer to empty.
256 void flush_nonempty();
259 //===----------------------------------------------------------------------===//
260 // File Output Streams
261 //===----------------------------------------------------------------------===//
263 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
265 class raw_fd_ostream : public raw_ostream {
270 /// write_impl - See raw_ostream::write_impl.
271 virtual void write_impl(const char *Ptr, size_t Size);
273 /// current_pos - Return the current position within the stream, not
274 /// counting the bytes currently in the buffer.
275 virtual uint64_t current_pos() { return pos; }
278 /// raw_fd_ostream - Open the specified file for writing. If an
279 /// error occurs, information about the error is put into ErrorInfo,
280 /// and the stream should be immediately destroyed; the string will
281 /// be empty if no error occurred.
283 /// \param Filename - The file to open. If this is "-" then the
284 /// stream will use stdout instead.
285 /// \param Binary - The file should be opened in binary mode on
286 /// platforms that support this distinction.
287 /// \param Force - Don't consider the case where the file already
288 /// exists to be an error.
289 raw_fd_ostream(const char *Filename, bool Binary, bool Force,
290 std::string &ErrorInfo);
292 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
293 /// ShouldClose is true, this closes the file when the stream is destroyed.
294 raw_fd_ostream(int fd, bool shouldClose,
295 bool unbuffered=false) : raw_ostream(unbuffered), FD(fd),
296 ShouldClose(shouldClose) {}
300 /// close - Manually flush the stream and close the file.
303 /// tell - Return the current offset with the file.
304 uint64_t tell() { return pos + GetNumBytesInBuffer(); }
306 /// seek - Flushes the stream and repositions the underlying file descriptor
307 /// positition to the offset specified from the beginning of the file.
308 uint64_t seek(uint64_t off);
310 virtual raw_ostream &changeColor(enum Colors colors, bool bold=false,
312 virtual raw_ostream &resetColor();
315 /// raw_stdout_ostream - This is a stream that always prints to stdout.
317 class raw_stdout_ostream : public raw_fd_ostream {
318 // An out of line virtual method to provide a home for the class vtable.
319 virtual void handle();
321 raw_stdout_ostream();
324 /// raw_stderr_ostream - This is a stream that always prints to stderr.
326 class raw_stderr_ostream : public raw_fd_ostream {
327 // An out of line virtual method to provide a home for the class vtable.
328 virtual void handle();
330 raw_stderr_ostream();
333 /// outs() - This returns a reference to a raw_ostream for standard output.
334 /// Use it like: outs() << "foo" << "bar";
337 /// errs() - This returns a reference to a raw_ostream for standard error.
338 /// Use it like: errs() << "foo" << "bar";
341 /// nulls() - This returns a reference to a raw_ostream which simply discards
343 raw_ostream &nulls();
345 //===----------------------------------------------------------------------===//
346 // Output Stream Adaptors
347 //===----------------------------------------------------------------------===//
349 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
350 /// simple adaptor class. It does not check for output errors; clients should
351 /// use the underlying stream to detect errors.
352 class raw_os_ostream : public raw_ostream {
355 /// write_impl - See raw_ostream::write_impl.
356 virtual void write_impl(const char *Ptr, size_t Size);
358 /// current_pos - Return the current position within the stream, not
359 /// counting the bytes currently in the buffer.
360 virtual uint64_t current_pos();
363 raw_os_ostream(std::ostream &O) : OS(O) {}
366 /// tell - Return the current offset with the stream.
370 /// raw_string_ostream - A raw_ostream that writes to an std::string. This is a
371 /// simple adaptor class. This class does not encounter output errors.
372 class raw_string_ostream : public raw_ostream {
375 /// write_impl - See raw_ostream::write_impl.
376 virtual void write_impl(const char *Ptr, size_t Size);
378 /// current_pos - Return the current position within the stream, not
379 /// counting the bytes currently in the buffer.
380 virtual uint64_t current_pos() { return OS.size(); }
382 explicit raw_string_ostream(std::string &O) : OS(O) {}
383 ~raw_string_ostream();
385 /// tell - Return the current offset with the stream.
386 uint64_t tell() { return OS.size() + GetNumBytesInBuffer(); }
388 /// str - Flushes the stream contents to the target string and returns
389 /// the string's reference.
396 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
397 /// SmallString. This is a simple adaptor class. This class does not
398 /// encounter output errors.
399 class raw_svector_ostream : public raw_ostream {
400 SmallVectorImpl<char> &OS;
402 /// write_impl - See raw_ostream::write_impl.
403 virtual void write_impl(const char *Ptr, size_t Size);
405 /// current_pos - Return the current position within the stream, not
406 /// counting the bytes currently in the buffer.
407 virtual uint64_t current_pos();
409 explicit raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
410 ~raw_svector_ostream();
412 /// tell - Return the current offset with the stream.
416 /// raw_null_ostream - A raw_ostream that discards all output.
417 class raw_null_ostream : public raw_ostream {
418 /// write_impl - See raw_ostream::write_impl.
419 virtual void write_impl(const char *Ptr, size_t size);
421 /// current_pos - Return the current position within the stream, not
422 /// counting the bytes currently in the buffer.
423 virtual uint64_t current_pos();
426 explicit raw_null_ostream() {}
430 } // end llvm namespace