blob: 044b963ee1238afd5181c17131ac83dee31bb7d9 [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 Dunbar3b3de922009-08-18 22:24:00 +0000126 // Handle simple case when value fits in long already.
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 Dunbar3b3de922009-08-18 22:24:00 +0000130 // Otherwise divide into at two or three 10**9 chunks and write out using
131 // long div/mod, this is substantially faster on a 32-bit system.
132 unsigned long Top = 0, Mid = 0, Bot = N % 1000000000;
133 N /= 1000000000;
134 if (N > 1000000000) {
135 Mid = N % 1000000000;
136 Top = N / 1000000000;
137 } else
138 Mid = N;
139
140 if (Top)
141 this->operator<<(static_cast<unsigned long>(Top));
142 this->operator<<(static_cast<unsigned long>(Mid));
143 return this->operator<<(static_cast<unsigned long>(Bot));
Owen Anderson66b17ba2008-08-21 20:58:52 +0000144}
145
146raw_ostream &raw_ostream::operator<<(long long N) {
147 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000148 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +0000149 N = -N;
150 }
151
152 return this->operator<<(static_cast<unsigned long long>(N));
153}
154
Daniel Dunbar48018e02009-07-30 18:21:23 +0000155raw_ostream &raw_ostream::write_hex(unsigned long long N) {
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000156 // Zero is a special case.
157 if (N == 0)
158 return *this << '0';
159
160 char NumberBuffer[20];
161 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
162 char *CurPtr = EndPtr;
163
164 while (N) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000165 uintptr_t x = N % 16;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000166 *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10);
167 N /= 16;
168 }
169
170 return write(CurPtr, EndPtr-CurPtr);
Chris Lattner944fac72008-08-23 22:23:09 +0000171}
172
Daniel Dunbar48018e02009-07-30 18:21:23 +0000173raw_ostream &raw_ostream::operator<<(const void *P) {
174 *this << '0' << 'x';
175
176 return write_hex((uintptr_t) P);
177}
178
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000179void raw_ostream::flush_nonempty() {
180 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000181 size_t Length = OutBufCur - OutBufStart;
182 OutBufCur = OutBufStart;
183 write_impl(OutBufStart, Length);
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000184}
185
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000186raw_ostream &raw_ostream::write(unsigned char C) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000187 // Group exceptional cases into a single branch.
188 if (OutBufCur >= OutBufEnd) {
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000189 if (BufferMode == Unbuffered) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000190 write_impl(reinterpret_cast<char*>(&C), 1);
191 return *this;
192 }
193
Dan Gohman37316042009-08-18 20:09:59 +0000194 if (OutBufStart)
Daniel Dunbar262541b2009-03-17 01:36:56 +0000195 flush_nonempty();
Dan Gohman37316042009-08-18 20:09:59 +0000196 else {
197 SetBuffered();
198 // It's possible for the underlying stream to decline
199 // buffering, so check this condition again.
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000200 if (BufferMode == Unbuffered) {
Dan Gohman37316042009-08-18 20:09:59 +0000201 write_impl(reinterpret_cast<char*>(&C), 1);
202 return *this;
203 }
204 }
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000205 }
206
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000207 *OutBufCur++ = C;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000208 return *this;
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000209}
Chris Lattner944fac72008-08-23 22:23:09 +0000210
Dan Gohmanad60f662009-07-16 15:24:40 +0000211raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000212 // Group exceptional cases into a single branch.
Daniel Dunbarb372c112009-03-17 21:15:18 +0000213 if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) {
Dan Gohman33e49ef2009-08-13 15:44:52 +0000214 if (BUILTIN_EXPECT(!OutBufStart, false)) {
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000215 if (BufferMode == Unbuffered) {
Dan Gohman33e49ef2009-08-13 15:44:52 +0000216 write_impl(Ptr, Size);
217 return *this;
218 }
219 // Set up a buffer and start over.
Dan Gohman208ec0f2009-08-13 17:27:29 +0000220 SetBuffered();
Dan Gohman33e49ef2009-08-13 15:44:52 +0000221 return write(Ptr, Size);
222 }
223 // Write out the data in buffer-sized blocks until the remainder
224 // fits within the buffer.
225 do {
226 size_t NumBytes = OutBufEnd - OutBufCur;
227 copy_to_buffer(Ptr, NumBytes);
Daniel Dunbar262541b2009-03-17 01:36:56 +0000228 flush_nonempty();
Dan Gohman33e49ef2009-08-13 15:44:52 +0000229 Ptr += NumBytes;
230 Size -= NumBytes;
231 } while (OutBufCur+Size > OutBufEnd);
Daniel Dunbar262541b2009-03-17 01:36:56 +0000232 }
Dan Gohman33e49ef2009-08-13 15:44:52 +0000233
234 copy_to_buffer(Ptr, Size);
235
236 return *this;
237}
238
239void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) {
Dan Gohmanc6126e82009-08-13 20:32:03 +0000240 assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!");
Dan Gohman4e8e6422009-08-13 18:38:15 +0000241
Owen Anderson66b17ba2008-08-21 20:58:52 +0000242 // Handle short strings specially, memcpy isn't very good at very short
243 // strings.
244 switch (Size) {
245 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
246 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
247 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
248 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
249 case 0: break;
250 default:
Dan Gohman33e49ef2009-08-13 15:44:52 +0000251 memcpy(OutBufCur, Ptr, Size);
Owen Anderson66b17ba2008-08-21 20:58:52 +0000252 break;
253 }
Daniel Dunbare77e4342009-03-10 16:21:55 +0000254
Dan Gohman33e49ef2009-08-13 15:44:52 +0000255 OutBufCur += Size;
Owen Anderson66b17ba2008-08-21 20:58:52 +0000256}
257
Chris Lattner097af7f2008-08-23 19:23:10 +0000258// Formatted output.
259raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000260 // If we have more than a few bytes left in our output buffer, try
261 // formatting directly onto its end.
262 //
263 // FIXME: This test is a bit silly, since if we don't have enough
264 // space in the buffer we will have to flush the formatted output
265 // anyway. We should just flush upfront in such cases, and use the
266 // whole buffer as our scratch pad. Note, however, that this case is
267 // also necessary for correctness on unbuffered streams.
Dan Gohmanad60f662009-07-16 15:24:40 +0000268 size_t NextBufferSize = 127;
Chris Lattner097af7f2008-08-23 19:23:10 +0000269 if (OutBufEnd-OutBufCur > 3) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000270 size_t BufferBytesLeft = OutBufEnd-OutBufCur;
271 size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
Chris Lattner097af7f2008-08-23 19:23:10 +0000272
273 // Common case is that we have plenty of space.
274 if (BytesUsed < BufferBytesLeft) {
275 OutBufCur += BytesUsed;
276 return *this;
277 }
278
279 // Otherwise, we overflowed and the return value tells us the size to try
280 // again with.
281 NextBufferSize = BytesUsed;
282 }
283
284 // If we got here, we didn't have enough space in the output buffer for the
285 // string. Try printing into a SmallVector that is resized to have enough
286 // space. Iterate until we win.
287 SmallVector<char, 128> V;
288
289 while (1) {
290 V.resize(NextBufferSize);
291
292 // Try formatting into the SmallVector.
Dan Gohmanad60f662009-07-16 15:24:40 +0000293 size_t BytesUsed = Fmt.print(&V[0], NextBufferSize);
Chris Lattner097af7f2008-08-23 19:23:10 +0000294
295 // If BytesUsed fit into the vector, we win.
Chris Lattnerf61ca1e2008-10-26 19:20:47 +0000296 if (BytesUsed <= NextBufferSize)
Chris Lattner097af7f2008-08-23 19:23:10 +0000297 return write(&V[0], BytesUsed);
298
299 // Otherwise, try again with a new size.
300 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
301 NextBufferSize = BytesUsed;
302 }
303}
304
305//===----------------------------------------------------------------------===//
306// Formatted Output
307//===----------------------------------------------------------------------===//
308
309// Out of line virtual method.
310void format_object_base::home() {
311}
312
Chris Lattner60d39622008-08-17 01:35:29 +0000313//===----------------------------------------------------------------------===//
314// raw_fd_ostream
315//===----------------------------------------------------------------------===//
316
Daniel Dunbar48534b32008-10-21 19:53:10 +0000317/// raw_fd_ostream - Open the specified file for writing. If an error
318/// occurs, information about the error is put into ErrorInfo, and the
319/// stream should be immediately destroyed; the string will be empty
320/// if no error occurred.
Dan Gohmana1bdced2009-07-15 17:29:42 +0000321raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, bool Force,
Ted Kremenekd75ba1c2008-12-04 22:51:11 +0000322 std::string &ErrorInfo) : pos(0) {
Daniel Dunbar48534b32008-10-21 19:53:10 +0000323 ErrorInfo.clear();
324
Chris Lattnerc52b1282008-08-17 03:53:23 +0000325 // Handle "-" as stdout.
326 if (Filename[0] == '-' && Filename[1] == 0) {
327 FD = STDOUT_FILENO;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000328 // If user requested binary then put stdout into binary mode if
329 // possible.
330 if (Binary)
331 sys::Program::ChangeStdoutToBinary();
Chris Lattnerc52b1282008-08-17 03:53:23 +0000332 ShouldClose = false;
333 return;
334 }
335
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000336 int Flags = O_WRONLY|O_CREAT|O_TRUNC;
337#ifdef O_BINARY
338 if (Binary)
339 Flags |= O_BINARY;
340#endif
Dan Gohmana1bdced2009-07-15 17:29:42 +0000341 if (!Force)
342 Flags |= O_EXCL;
Dan Gohman9f79d3e2009-07-15 16:39:40 +0000343 FD = open(Filename, Flags, 0664);
Chris Lattner60d39622008-08-17 01:35:29 +0000344 if (FD < 0) {
345 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
346 ShouldClose = false;
347 } else {
348 ShouldClose = true;
349 }
350}
351
352raw_fd_ostream::~raw_fd_ostream() {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000353 if (FD >= 0) {
354 flush();
355 if (ShouldClose)
Dan Gohmand3a974f2009-07-15 16:43:01 +0000356 if (::close(FD) != 0)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000357 error_detected();
Ted Kremenek43d1f022008-10-23 23:49:09 +0000358 }
Chris Lattner60d39622008-08-17 01:35:29 +0000359}
360
Dan Gohmanad60f662009-07-16 15:24:40 +0000361void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000362 assert (FD >= 0 && "File already closed.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000363 pos += Size;
Daniel Dunbar579fb872009-07-15 08:11:46 +0000364 if (::write(FD, Ptr, Size) != (ssize_t) Size)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000365 error_detected();
Chris Lattner60d39622008-08-17 01:35:29 +0000366}
367
Ted Kremenek43d1f022008-10-23 23:49:09 +0000368void raw_fd_ostream::close() {
369 assert (ShouldClose);
370 ShouldClose = false;
371 flush();
Dan Gohmand3a974f2009-07-15 16:43:01 +0000372 if (::close(FD) != 0)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000373 error_detected();
Ted Kremenek43d1f022008-10-23 23:49:09 +0000374 FD = -1;
375}
376
Ted Kremenek9c278862009-01-26 21:42:04 +0000377uint64_t raw_fd_ostream::seek(uint64_t off) {
378 flush();
Daniel Dunbar579fb872009-07-15 08:11:46 +0000379 pos = ::lseek(FD, off, SEEK_SET);
Dan Gohmand3a974f2009-07-15 16:43:01 +0000380 if (pos != off)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000381 error_detected();
Ted Kremenek9c278862009-01-26 21:42:04 +0000382 return pos;
383}
384
Dan Gohman208ec0f2009-08-13 17:27:29 +0000385size_t raw_fd_ostream::preferred_buffer_size() {
Dan Gohmanb4741a72009-08-15 21:41:03 +0000386#if !defined(_MSC_VER) && !defined(__MINGW32__) // Windows has no st_blksize.
Dan Gohman208ec0f2009-08-13 17:27:29 +0000387 assert(FD >= 0 && "File not yet open!");
388 struct stat statbuf;
Dan Gohmand7be6632009-08-15 02:05:19 +0000389 if (fstat(FD, &statbuf) == 0) {
390 // If this is a terminal, don't use buffering. Line buffering
391 // would be a more traditional thing to do, but it's not worth
392 // the complexity.
393 if (S_ISCHR(statbuf.st_mode) && isatty(FD))
394 return 0;
395 // Return the preferred block size.
Dan Gohman208ec0f2009-08-13 17:27:29 +0000396 return statbuf.st_blksize;
Dan Gohmand7be6632009-08-15 02:05:19 +0000397 }
Dan Gohman208ec0f2009-08-13 17:27:29 +0000398 error_detected();
399#endif
400 return raw_ostream::preferred_buffer_size();
401}
402
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000403raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
404 bool bg) {
405 if (sys::Process::ColorNeedsFlush())
406 flush();
407 const char *colorcode =
408 (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
409 : sys::Process::OutputColor(colors, bold, bg);
410 if (colorcode) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000411 size_t len = strlen(colorcode);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000412 write(colorcode, len);
413 // don't account colors towards output characters
414 pos -= len;
415 }
416 return *this;
417}
418
419raw_ostream &raw_fd_ostream::resetColor() {
420 if (sys::Process::ColorNeedsFlush())
421 flush();
422 const char *colorcode = sys::Process::ResetColor();
423 if (colorcode) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000424 size_t len = strlen(colorcode);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000425 write(colorcode, len);
426 // don't account colors towards output characters
427 pos -= len;
428 }
429 return *this;
430}
431
Chris Lattner07f51f72008-08-17 04:13:37 +0000432//===----------------------------------------------------------------------===//
433// raw_stdout/err_ostream
434//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +0000435
Dan Gohmanec9b2612009-08-13 23:18:56 +0000436// Set buffer settings to model stdout and stderr behavior.
Dan Gohmand7be6632009-08-15 02:05:19 +0000437// Set standard error to be unbuffered by default.
438raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
439raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
Daniel Dunbare77e4342009-03-10 16:21:55 +0000440 true) {}
Chris Lattner60d39622008-08-17 01:35:29 +0000441
442// An out of line virtual method to provide a home for the class vtable.
443void raw_stdout_ostream::handle() {}
444void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +0000445
446/// outs() - This returns a reference to a raw_ostream for standard output.
447/// Use it like: outs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000448raw_ostream &llvm::outs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000449 static raw_stdout_ostream S;
450 return S;
451}
452
453/// errs() - This returns a reference to a raw_ostream for standard error.
454/// Use it like: errs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000455raw_ostream &llvm::errs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000456 static raw_stderr_ostream S;
457 return S;
458}
459
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000460/// nulls() - This returns a reference to a raw_ostream which discards output.
461raw_ostream &llvm::nulls() {
462 static raw_null_ostream S;
463 return S;
464}
465
Chris Lattner07f51f72008-08-17 04:13:37 +0000466//===----------------------------------------------------------------------===//
467// raw_os_ostream
468//===----------------------------------------------------------------------===//
469
Daniel Dunbar35979c02009-08-18 20:07:36 +0000470raw_os_ostream::~raw_os_ostream() {
471 flush();
472}
473
Dan Gohmanad60f662009-07-16 15:24:40 +0000474void raw_os_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000475 OS.write(Ptr, Size);
Chris Lattner07f51f72008-08-17 04:13:37 +0000476}
Chris Lattner78a28122008-08-23 22:43:04 +0000477
Douglas Gregor8f7be472009-04-20 07:34:17 +0000478uint64_t raw_os_ostream::current_pos() { return OS.tellp(); }
479
480uint64_t raw_os_ostream::tell() {
481 return (uint64_t)OS.tellp() + GetNumBytesInBuffer();
482}
483
Chris Lattner78a28122008-08-23 22:43:04 +0000484//===----------------------------------------------------------------------===//
485// raw_string_ostream
486//===----------------------------------------------------------------------===//
487
Daniel Dunbar35979c02009-08-18 20:07:36 +0000488raw_string_ostream::~raw_string_ostream() {
489 flush();
490}
491
Dan Gohmanad60f662009-07-16 15:24:40 +0000492void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000493 OS.append(Ptr, Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000494}
495
496//===----------------------------------------------------------------------===//
497// raw_svector_ostream
498//===----------------------------------------------------------------------===//
499
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000500raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {
501}
502
Daniel Dunbar35979c02009-08-18 20:07:36 +0000503raw_svector_ostream::~raw_svector_ostream() {
504 flush();
505}
506
Dan Gohmanad60f662009-07-16 15:24:40 +0000507void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000508 OS.append(Ptr, Ptr + Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000509}
510
Douglas Gregor8f7be472009-04-20 07:34:17 +0000511uint64_t raw_svector_ostream::current_pos() { return OS.size(); }
512
513uint64_t raw_svector_ostream::tell() {
514 return OS.size() + GetNumBytesInBuffer();
515}
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000516
517//===----------------------------------------------------------------------===//
518// raw_null_ostream
519//===----------------------------------------------------------------------===//
520
Dan Gohmanf78c8352009-07-27 21:46:02 +0000521raw_null_ostream::~raw_null_ostream() {
522#ifndef NDEBUG
523 // ~raw_ostream asserts that the buffer is empty. This isn't necessary
524 // with raw_null_ostream, but it's better to have raw_null_ostream follow
525 // the rules than to change the rules just for raw_null_ostream.
526 flush();
527#endif
528}
529
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000530void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
531}
532
533uint64_t raw_null_ostream::current_pos() {
534 return 0;
535}