blob: e7e4ad3f9069a5ee414320cf0d75525e761c656a [file] [log] [blame]
Chris Lattner60d39622008-08-17 01:35:29 +00001//===--- raw_ostream.cpp - Implement the raw_ostream classes --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements support for bulk buffered stream output.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/raw_ostream.h"
Chris Lattner42f77ab2008-08-23 20:34:06 +000015#include "llvm/Support/Format.h"
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +000016#include "llvm/System/Program.h"
Torok Edwine8ebb0f2009-06-04 07:09:50 +000017#include "llvm/System/Process.h"
Chris Lattner097af7f2008-08-23 19:23:10 +000018#include "llvm/ADT/SmallVector.h"
Chris Lattner969a46a2008-08-22 15:45:00 +000019#include "llvm/Config/config.h"
Daniel Dunbarb372c112009-03-17 21:15:18 +000020#include "llvm/Support/Compiler.h"
Daniel Dunbar579fb872009-07-15 08:11:46 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattner07f51f72008-08-17 04:13:37 +000022#include <ostream>
Dan Gohman208ec0f2009-08-13 17:27:29 +000023#include <sys/stat.h>
24#include <sys/types.h>
Chris Lattner60d39622008-08-17 01:35:29 +000025
Chris Lattner969a46a2008-08-22 15:45:00 +000026#if defined(HAVE_UNISTD_H)
27# include <unistd.h>
28#endif
29#if defined(HAVE_FCNTL_H)
30# include <fcntl.h>
31#endif
Argyrios Kyrtzidisb68dc362008-08-17 09:25:21 +000032
33#if defined(_MSC_VER)
Chris Lattner60d39622008-08-17 01:35:29 +000034#include <io.h>
Cedric Veneta3f343f2008-08-24 11:56:40 +000035#include <fcntl.h>
Owen Andersoncb371882008-08-21 00:14:44 +000036#ifndef STDIN_FILENO
37# define STDIN_FILENO 0
38#endif
39#ifndef STDOUT_FILENO
40# define STDOUT_FILENO 1
41#endif
42#ifndef STDERR_FILENO
43# define STDERR_FILENO 2
44#endif
Chris Lattner60d39622008-08-17 01:35:29 +000045#endif
46
Chris Lattner969a46a2008-08-22 15:45:00 +000047using namespace llvm;
48
Dan Gohmane87b2ab2009-07-15 23:25:33 +000049raw_ostream::~raw_ostream() {
Dan Gohman9a312542009-07-27 20:49:44 +000050 // raw_ostream's subclasses should take care to flush the buffer
51 // in their destructors.
52 assert(OutBufCur == OutBufStart &&
53 "raw_ostream destructor called with non-empty buffer!");
54
Daniel Dunbar906d5b42009-08-18 23:42:36 +000055 if (BufferMode == InternalBuffer)
56 delete [] OutBufStart;
Dan Gohmane87b2ab2009-07-15 23:25:33 +000057
58 // If there are any pending errors, report them now. Clients wishing
59 // to avoid llvm_report_error calls should check for errors with
60 // has_error() and clear the error flag with clear_error() before
61 // destructing raw_ostream objects which may have errors.
62 if (Error)
63 llvm_report_error("IO failure on output stream.");
64}
Chris Lattner969a46a2008-08-22 15:45:00 +000065
Chris Lattner60d39622008-08-17 01:35:29 +000066// An out of line virtual method to provide a home for the class vtable.
67void raw_ostream::handle() {}
68
Dan Gohman208ec0f2009-08-13 17:27:29 +000069size_t raw_ostream::preferred_buffer_size() {
70 // BUFSIZ is intended to be a reasonable default.
71 return BUFSIZ;
72}
73
74void raw_ostream::SetBuffered() {
75 // Ask the subclass to determine an appropriate buffer size.
Dan Gohmand7be6632009-08-15 02:05:19 +000076 if (size_t Size = preferred_buffer_size())
77 SetBufferSize(Size);
78 else
79 // It may return 0, meaning this stream should be unbuffered.
80 SetUnbuffered();
Dan Gohman208ec0f2009-08-13 17:27:29 +000081}
82
Daniel Dunbar906d5b42009-08-18 23:42:36 +000083void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size,
84 BufferKind Mode) {
85 assert(((Mode == Unbuffered && BufferStart == 0 && Size == 0) ||
86 (Mode != Unbuffered && BufferStart && Size >= 64)) &&
87 "stream must be unbuffered, or have >= 64 bytes of buffer");
88 // Make sure the current buffer is free of content (we can't flush here; the
89 // child buffer management logic will be in write_impl).
90 assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!");
Dan Gohman524dea42009-08-13 15:58:55 +000091
Daniel Dunbar906d5b42009-08-18 23:42:36 +000092 if (BufferMode == InternalBuffer)
93 delete [] OutBufStart;
94 OutBufStart = BufferStart;
Dan Gohman524dea42009-08-13 15:58:55 +000095 OutBufEnd = OutBufStart+Size;
96 OutBufCur = OutBufStart;
Daniel Dunbar906d5b42009-08-18 23:42:36 +000097 BufferMode = Mode;
Dan Gohman524dea42009-08-13 15:58:55 +000098}
99
Owen Anderson66b17ba2008-08-21 20:58:52 +0000100raw_ostream &raw_ostream::operator<<(unsigned long N) {
101 // Zero is a special case.
102 if (N == 0)
103 return *this << '0';
104
105 char NumberBuffer[20];
106 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
107 char *CurPtr = EndPtr;
108
109 while (N) {
110 *--CurPtr = '0' + char(N % 10);
111 N /= 10;
112 }
113 return write(CurPtr, EndPtr-CurPtr);
114}
115
116raw_ostream &raw_ostream::operator<<(long N) {
117 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000118 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +0000119 N = -N;
120 }
121
122 return this->operator<<(static_cast<unsigned long>(N));
123}
124
125raw_ostream &raw_ostream::operator<<(unsigned long long N) {
Daniel Dunbarecbd0bc2009-08-19 16:25:25 +0000126 // Output using 32-bit div/mod when possible.
Daniel Dunbar78a3dd82009-07-29 06:45:14 +0000127 if (N == static_cast<unsigned long>(N))
128 return this->operator<<(static_cast<unsigned long>(N));
129
Daniel Dunbarecbd0bc2009-08-19 16:25:25 +0000130 char NumberBuffer[20];
131 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
132 char *CurPtr = EndPtr;
133
134 while (N) {
135 *--CurPtr = '0' + char(N % 10);
136 N /= 10;
137 }
138 return write(CurPtr, EndPtr-CurPtr);
Owen Anderson66b17ba2008-08-21 20:58:52 +0000139}
140
141raw_ostream &raw_ostream::operator<<(long long N) {
142 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000143 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +0000144 N = -N;
145 }
146
147 return this->operator<<(static_cast<unsigned long long>(N));
148}
149
Daniel Dunbar48018e02009-07-30 18:21:23 +0000150raw_ostream &raw_ostream::write_hex(unsigned long long N) {
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000151 // Zero is a special case.
152 if (N == 0)
153 return *this << '0';
154
155 char NumberBuffer[20];
156 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
157 char *CurPtr = EndPtr;
158
159 while (N) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000160 uintptr_t x = N % 16;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000161 *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10);
162 N /= 16;
163 }
164
165 return write(CurPtr, EndPtr-CurPtr);
Chris Lattner944fac72008-08-23 22:23:09 +0000166}
167
Daniel Dunbar48018e02009-07-30 18:21:23 +0000168raw_ostream &raw_ostream::operator<<(const void *P) {
169 *this << '0' << 'x';
170
171 return write_hex((uintptr_t) P);
172}
173
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000174void raw_ostream::flush_nonempty() {
175 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000176 size_t Length = OutBufCur - OutBufStart;
177 OutBufCur = OutBufStart;
178 write_impl(OutBufStart, Length);
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000179}
180
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000181raw_ostream &raw_ostream::write(unsigned char C) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000182 // Group exceptional cases into a single branch.
Daniel Dunbarecc67e22009-08-19 00:23:39 +0000183 if (BUILTIN_EXPECT(OutBufCur >= OutBufEnd, false)) {
184 if (BUILTIN_EXPECT(!OutBufStart, false)) {
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000185 if (BufferMode == Unbuffered) {
Dan Gohman37316042009-08-18 20:09:59 +0000186 write_impl(reinterpret_cast<char*>(&C), 1);
187 return *this;
188 }
Daniel Dunbarecc67e22009-08-19 00:23:39 +0000189 // Set up a buffer and start over.
190 SetBuffered();
191 return write(C);
Dan Gohman37316042009-08-18 20:09:59 +0000192 }
Daniel Dunbarecc67e22009-08-19 00:23:39 +0000193
194 flush_nonempty();
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000195 }
196
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000197 *OutBufCur++ = C;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000198 return *this;
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000199}
Chris Lattner944fac72008-08-23 22:23:09 +0000200
Dan Gohmanad60f662009-07-16 15:24:40 +0000201raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000202 // Group exceptional cases into a single branch.
Daniel Dunbarb372c112009-03-17 21:15:18 +0000203 if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) {
Dan Gohman33e49ef2009-08-13 15:44:52 +0000204 if (BUILTIN_EXPECT(!OutBufStart, false)) {
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000205 if (BufferMode == Unbuffered) {
Dan Gohman33e49ef2009-08-13 15:44:52 +0000206 write_impl(Ptr, Size);
207 return *this;
208 }
209 // Set up a buffer and start over.
Dan Gohman208ec0f2009-08-13 17:27:29 +0000210 SetBuffered();
Dan Gohman33e49ef2009-08-13 15:44:52 +0000211 return write(Ptr, Size);
212 }
Daniel Dunbarecc67e22009-08-19 00:23:39 +0000213
Dan Gohman33e49ef2009-08-13 15:44:52 +0000214 // Write out the data in buffer-sized blocks until the remainder
215 // fits within the buffer.
216 do {
217 size_t NumBytes = OutBufEnd - OutBufCur;
218 copy_to_buffer(Ptr, NumBytes);
Daniel Dunbar262541b2009-03-17 01:36:56 +0000219 flush_nonempty();
Dan Gohman33e49ef2009-08-13 15:44:52 +0000220 Ptr += NumBytes;
221 Size -= NumBytes;
222 } while (OutBufCur+Size > OutBufEnd);
Daniel Dunbar262541b2009-03-17 01:36:56 +0000223 }
Dan Gohman33e49ef2009-08-13 15:44:52 +0000224
225 copy_to_buffer(Ptr, Size);
226
227 return *this;
228}
229
230void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) {
Dan Gohmanc6126e82009-08-13 20:32:03 +0000231 assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!");
Dan Gohman4e8e6422009-08-13 18:38:15 +0000232
Owen Anderson66b17ba2008-08-21 20:58:52 +0000233 // Handle short strings specially, memcpy isn't very good at very short
234 // strings.
235 switch (Size) {
236 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
237 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
238 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
239 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
240 case 0: break;
241 default:
Dan Gohman33e49ef2009-08-13 15:44:52 +0000242 memcpy(OutBufCur, Ptr, Size);
Owen Anderson66b17ba2008-08-21 20:58:52 +0000243 break;
244 }
Daniel Dunbare77e4342009-03-10 16:21:55 +0000245
Dan Gohman33e49ef2009-08-13 15:44:52 +0000246 OutBufCur += Size;
Owen Anderson66b17ba2008-08-21 20:58:52 +0000247}
248
Chris Lattner097af7f2008-08-23 19:23:10 +0000249// Formatted output.
250raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000251 // If we have more than a few bytes left in our output buffer, try
252 // formatting directly onto its end.
Dan Gohmanad60f662009-07-16 15:24:40 +0000253 size_t NextBufferSize = 127;
Chris Lattner097af7f2008-08-23 19:23:10 +0000254 if (OutBufEnd-OutBufCur > 3) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000255 size_t BufferBytesLeft = OutBufEnd-OutBufCur;
256 size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
Chris Lattner097af7f2008-08-23 19:23:10 +0000257
258 // Common case is that we have plenty of space.
259 if (BytesUsed < BufferBytesLeft) {
260 OutBufCur += BytesUsed;
261 return *this;
262 }
263
264 // Otherwise, we overflowed and the return value tells us the size to try
265 // again with.
266 NextBufferSize = BytesUsed;
267 }
268
269 // If we got here, we didn't have enough space in the output buffer for the
270 // string. Try printing into a SmallVector that is resized to have enough
271 // space. Iterate until we win.
272 SmallVector<char, 128> V;
273
274 while (1) {
275 V.resize(NextBufferSize);
276
277 // Try formatting into the SmallVector.
Dan Gohmanad60f662009-07-16 15:24:40 +0000278 size_t BytesUsed = Fmt.print(&V[0], NextBufferSize);
Chris Lattner097af7f2008-08-23 19:23:10 +0000279
280 // If BytesUsed fit into the vector, we win.
Chris Lattnerf61ca1e2008-10-26 19:20:47 +0000281 if (BytesUsed <= NextBufferSize)
Chris Lattner097af7f2008-08-23 19:23:10 +0000282 return write(&V[0], BytesUsed);
283
284 // Otherwise, try again with a new size.
285 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
286 NextBufferSize = BytesUsed;
287 }
288}
289
290//===----------------------------------------------------------------------===//
291// Formatted Output
292//===----------------------------------------------------------------------===//
293
294// Out of line virtual method.
295void format_object_base::home() {
296}
297
Chris Lattner60d39622008-08-17 01:35:29 +0000298//===----------------------------------------------------------------------===//
299// raw_fd_ostream
300//===----------------------------------------------------------------------===//
301
Daniel Dunbar48534b32008-10-21 19:53:10 +0000302/// raw_fd_ostream - Open the specified file for writing. If an error
303/// occurs, information about the error is put into ErrorInfo, and the
304/// stream should be immediately destroyed; the string will be empty
305/// if no error occurred.
Dan Gohmana1bdced2009-07-15 17:29:42 +0000306raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, bool Force,
Ted Kremenekd75ba1c2008-12-04 22:51:11 +0000307 std::string &ErrorInfo) : pos(0) {
Daniel Dunbar48534b32008-10-21 19:53:10 +0000308 ErrorInfo.clear();
309
Chris Lattnerc52b1282008-08-17 03:53:23 +0000310 // Handle "-" as stdout.
311 if (Filename[0] == '-' && Filename[1] == 0) {
312 FD = STDOUT_FILENO;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000313 // If user requested binary then put stdout into binary mode if
314 // possible.
315 if (Binary)
316 sys::Program::ChangeStdoutToBinary();
Chris Lattnerc52b1282008-08-17 03:53:23 +0000317 ShouldClose = false;
318 return;
319 }
320
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000321 int Flags = O_WRONLY|O_CREAT|O_TRUNC;
322#ifdef O_BINARY
323 if (Binary)
324 Flags |= O_BINARY;
325#endif
Dan Gohmana1bdced2009-07-15 17:29:42 +0000326 if (!Force)
327 Flags |= O_EXCL;
Dan Gohman9f79d3e2009-07-15 16:39:40 +0000328 FD = open(Filename, Flags, 0664);
Chris Lattner60d39622008-08-17 01:35:29 +0000329 if (FD < 0) {
330 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
331 ShouldClose = false;
332 } else {
333 ShouldClose = true;
334 }
335}
336
337raw_fd_ostream::~raw_fd_ostream() {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000338 if (FD >= 0) {
339 flush();
340 if (ShouldClose)
Dan Gohmand3a974f2009-07-15 16:43:01 +0000341 if (::close(FD) != 0)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000342 error_detected();
Ted Kremenek43d1f022008-10-23 23:49:09 +0000343 }
Chris Lattner60d39622008-08-17 01:35:29 +0000344}
345
Dan Gohmanad60f662009-07-16 15:24:40 +0000346void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000347 assert (FD >= 0 && "File already closed.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000348 pos += Size;
Daniel Dunbar579fb872009-07-15 08:11:46 +0000349 if (::write(FD, Ptr, Size) != (ssize_t) Size)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000350 error_detected();
Chris Lattner60d39622008-08-17 01:35:29 +0000351}
352
Ted Kremenek43d1f022008-10-23 23:49:09 +0000353void raw_fd_ostream::close() {
354 assert (ShouldClose);
355 ShouldClose = false;
356 flush();
Dan Gohmand3a974f2009-07-15 16:43:01 +0000357 if (::close(FD) != 0)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000358 error_detected();
Ted Kremenek43d1f022008-10-23 23:49:09 +0000359 FD = -1;
360}
361
Ted Kremenek9c278862009-01-26 21:42:04 +0000362uint64_t raw_fd_ostream::seek(uint64_t off) {
363 flush();
Daniel Dunbar579fb872009-07-15 08:11:46 +0000364 pos = ::lseek(FD, off, SEEK_SET);
Dan Gohmand3a974f2009-07-15 16:43:01 +0000365 if (pos != off)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000366 error_detected();
Ted Kremenek9c278862009-01-26 21:42:04 +0000367 return pos;
368}
369
Dan Gohman208ec0f2009-08-13 17:27:29 +0000370size_t raw_fd_ostream::preferred_buffer_size() {
Dan Gohmanb4741a72009-08-15 21:41:03 +0000371#if !defined(_MSC_VER) && !defined(__MINGW32__) // Windows has no st_blksize.
Dan Gohman208ec0f2009-08-13 17:27:29 +0000372 assert(FD >= 0 && "File not yet open!");
373 struct stat statbuf;
Dan Gohmand7be6632009-08-15 02:05:19 +0000374 if (fstat(FD, &statbuf) == 0) {
375 // If this is a terminal, don't use buffering. Line buffering
376 // would be a more traditional thing to do, but it's not worth
377 // the complexity.
378 if (S_ISCHR(statbuf.st_mode) && isatty(FD))
379 return 0;
380 // Return the preferred block size.
Dan Gohman208ec0f2009-08-13 17:27:29 +0000381 return statbuf.st_blksize;
Dan Gohmand7be6632009-08-15 02:05:19 +0000382 }
Dan Gohman208ec0f2009-08-13 17:27:29 +0000383 error_detected();
384#endif
385 return raw_ostream::preferred_buffer_size();
386}
387
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000388raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
389 bool bg) {
390 if (sys::Process::ColorNeedsFlush())
391 flush();
392 const char *colorcode =
393 (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
394 : sys::Process::OutputColor(colors, bold, bg);
395 if (colorcode) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000396 size_t len = strlen(colorcode);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000397 write(colorcode, len);
398 // don't account colors towards output characters
399 pos -= len;
400 }
401 return *this;
402}
403
404raw_ostream &raw_fd_ostream::resetColor() {
405 if (sys::Process::ColorNeedsFlush())
406 flush();
407 const char *colorcode = sys::Process::ResetColor();
408 if (colorcode) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000409 size_t len = strlen(colorcode);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000410 write(colorcode, len);
411 // don't account colors towards output characters
412 pos -= len;
413 }
414 return *this;
415}
416
Chris Lattner07f51f72008-08-17 04:13:37 +0000417//===----------------------------------------------------------------------===//
418// raw_stdout/err_ostream
419//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +0000420
Dan Gohmanec9b2612009-08-13 23:18:56 +0000421// Set buffer settings to model stdout and stderr behavior.
Dan Gohmand7be6632009-08-15 02:05:19 +0000422// Set standard error to be unbuffered by default.
423raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
424raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
Daniel Dunbare77e4342009-03-10 16:21:55 +0000425 true) {}
Chris Lattner60d39622008-08-17 01:35:29 +0000426
427// An out of line virtual method to provide a home for the class vtable.
428void raw_stdout_ostream::handle() {}
429void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +0000430
431/// outs() - This returns a reference to a raw_ostream for standard output.
432/// Use it like: outs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000433raw_ostream &llvm::outs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000434 static raw_stdout_ostream S;
435 return S;
436}
437
438/// errs() - This returns a reference to a raw_ostream for standard error.
439/// Use it like: errs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000440raw_ostream &llvm::errs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000441 static raw_stderr_ostream S;
442 return S;
443}
444
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000445/// nulls() - This returns a reference to a raw_ostream which discards output.
446raw_ostream &llvm::nulls() {
447 static raw_null_ostream S;
448 return S;
449}
450
Chris Lattner07f51f72008-08-17 04:13:37 +0000451//===----------------------------------------------------------------------===//
452// raw_os_ostream
453//===----------------------------------------------------------------------===//
454
Daniel Dunbar35979c02009-08-18 20:07:36 +0000455raw_os_ostream::~raw_os_ostream() {
456 flush();
457}
458
Dan Gohmanad60f662009-07-16 15:24:40 +0000459void raw_os_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000460 OS.write(Ptr, Size);
Chris Lattner07f51f72008-08-17 04:13:37 +0000461}
Chris Lattner78a28122008-08-23 22:43:04 +0000462
Douglas Gregor8f7be472009-04-20 07:34:17 +0000463uint64_t raw_os_ostream::current_pos() { return OS.tellp(); }
464
Chris Lattner78a28122008-08-23 22:43:04 +0000465//===----------------------------------------------------------------------===//
466// raw_string_ostream
467//===----------------------------------------------------------------------===//
468
Daniel Dunbar35979c02009-08-18 20:07:36 +0000469raw_string_ostream::~raw_string_ostream() {
470 flush();
471}
472
Dan Gohmanad60f662009-07-16 15:24:40 +0000473void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000474 OS.append(Ptr, Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000475}
476
477//===----------------------------------------------------------------------===//
478// raw_svector_ostream
479//===----------------------------------------------------------------------===//
480
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000481raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {
482}
483
Daniel Dunbar35979c02009-08-18 20:07:36 +0000484raw_svector_ostream::~raw_svector_ostream() {
485 flush();
486}
487
Dan Gohmanad60f662009-07-16 15:24:40 +0000488void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000489 OS.append(Ptr, Ptr + Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000490}
491
Douglas Gregor8f7be472009-04-20 07:34:17 +0000492uint64_t raw_svector_ostream::current_pos() { return OS.size(); }
493
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000494//===----------------------------------------------------------------------===//
495// raw_null_ostream
496//===----------------------------------------------------------------------===//
497
Dan Gohmanf78c8352009-07-27 21:46:02 +0000498raw_null_ostream::~raw_null_ostream() {
499#ifndef NDEBUG
500 // ~raw_ostream asserts that the buffer is empty. This isn't necessary
501 // with raw_null_ostream, but it's better to have raw_null_ostream follow
502 // the rules than to change the rules just for raw_null_ostream.
503 flush();
504#endif
505}
506
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000507void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
508}
509
510uint64_t raw_null_ostream::current_pos() {
511 return 0;
512}