blob: 30bc76b7fd1e7b2a25c6667c46e4f2a64eb5a30e [file] [log] [blame]
Chris Lattner18283402008-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 Lattner62ccbc72008-08-23 20:34:06 +000015#include "llvm/Support/Format.h"
Daniel Dunbar8c009a32008-11-13 05:01:07 +000016#include "llvm/System/Program.h"
Edwin Török8a06a5b2009-06-04 07:09:50 +000017#include "llvm/System/Process.h"
Chris Lattnerf9402fe2008-08-23 19:23:10 +000018#include "llvm/ADT/SmallVector.h"
Chris Lattner13456752008-08-22 15:45:00 +000019#include "llvm/Config/config.h"
Daniel Dunbard6976332009-03-17 21:15:18 +000020#include "llvm/Support/Compiler.h"
Daniel Dunbara3230b72009-07-15 08:11:46 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattner050c9612008-08-17 04:13:37 +000022#include <ostream>
Dan Gohman6bd7ece2009-08-13 17:27:29 +000023#include <sys/stat.h>
24#include <sys/types.h>
Chris Lattner18283402008-08-17 01:35:29 +000025
Chris Lattner13456752008-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
Argiris Kirtzidis8519c432008-08-17 09:25:21 +000032
33#if defined(_MSC_VER)
Chris Lattner18283402008-08-17 01:35:29 +000034#include <io.h>
Cédric Venet5e0a3582008-08-24 11:56:40 +000035#include <fcntl.h>
Owen Anderson847b99b2008-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 Lattner18283402008-08-17 01:35:29 +000045#endif
46
Chris Lattner13456752008-08-22 15:45:00 +000047using namespace llvm;
48
Dan Gohmanfea42602009-07-15 23:25:33 +000049raw_ostream::~raw_ostream() {
Dan Gohmanab7f0752009-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 Dunbar35993b32009-08-18 23:42:36 +000055 if (BufferMode == InternalBuffer)
56 delete [] OutBufStart;
Dan Gohmanfea42602009-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 Lattner13456752008-08-22 15:45:00 +000065
Chris Lattner18283402008-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 Gohman6bd7ece2009-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 Gohman314e3a62009-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 Gohman6bd7ece2009-08-13 17:27:29 +000081}
82
Daniel Dunbar35993b32009-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 Gohman48916522009-08-13 15:58:55 +000091
Daniel Dunbar35993b32009-08-18 23:42:36 +000092 if (BufferMode == InternalBuffer)
93 delete [] OutBufStart;
94 OutBufStart = BufferStart;
Dan Gohman48916522009-08-13 15:58:55 +000095 OutBufEnd = OutBufStart+Size;
96 OutBufCur = OutBufStart;
Daniel Dunbar35993b32009-08-18 23:42:36 +000097 BufferMode = Mode;
Daniel Dunbar7dfa7412009-08-19 17:54:29 +000098
99 assert(OutBufStart <= OutBufEnd && "Invalid size!");
Dan Gohman48916522009-08-13 15:58:55 +0000100}
101
Owen Anderson1046f622008-08-21 20:58:52 +0000102raw_ostream &raw_ostream::operator<<(unsigned long N) {
103 // Zero is a special case.
104 if (N == 0)
105 return *this << '0';
106
107 char NumberBuffer[20];
108 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
109 char *CurPtr = EndPtr;
110
111 while (N) {
112 *--CurPtr = '0' + char(N % 10);
113 N /= 10;
114 }
115 return write(CurPtr, EndPtr-CurPtr);
116}
117
118raw_ostream &raw_ostream::operator<<(long N) {
119 if (N < 0) {
Daniel Dunbar5cd6d452009-03-16 22:00:17 +0000120 *this << '-';
Owen Anderson1046f622008-08-21 20:58:52 +0000121 N = -N;
122 }
123
124 return this->operator<<(static_cast<unsigned long>(N));
125}
126
127raw_ostream &raw_ostream::operator<<(unsigned long long N) {
Daniel Dunbarbab91dd2009-08-19 16:25:25 +0000128 // Output using 32-bit div/mod when possible.
Daniel Dunbar2ece02f2009-07-29 06:45:14 +0000129 if (N == static_cast<unsigned long>(N))
130 return this->operator<<(static_cast<unsigned long>(N));
131
Daniel Dunbarbab91dd2009-08-19 16:25:25 +0000132 char NumberBuffer[20];
133 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
134 char *CurPtr = EndPtr;
135
136 while (N) {
137 *--CurPtr = '0' + char(N % 10);
138 N /= 10;
139 }
140 return write(CurPtr, EndPtr-CurPtr);
Owen Anderson1046f622008-08-21 20:58:52 +0000141}
142
143raw_ostream &raw_ostream::operator<<(long long N) {
144 if (N < 0) {
Daniel Dunbar5cd6d452009-03-16 22:00:17 +0000145 *this << '-';
Owen Anderson1046f622008-08-21 20:58:52 +0000146 N = -N;
147 }
148
149 return this->operator<<(static_cast<unsigned long long>(N));
150}
151
Daniel Dunbarea9b9d32009-07-30 18:21:23 +0000152raw_ostream &raw_ostream::write_hex(unsigned long long N) {
Daniel Dunbar3b909272009-03-16 22:08:44 +0000153 // Zero is a special case.
154 if (N == 0)
155 return *this << '0';
156
157 char NumberBuffer[20];
158 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
159 char *CurPtr = EndPtr;
160
161 while (N) {
Dan Gohman75ffec02009-07-16 15:24:40 +0000162 uintptr_t x = N % 16;
Daniel Dunbar3b909272009-03-16 22:08:44 +0000163 *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10);
164 N /= 16;
165 }
166
167 return write(CurPtr, EndPtr-CurPtr);
Chris Lattner1fefaac2008-08-23 22:23:09 +0000168}
169
Daniel Dunbarea9b9d32009-07-30 18:21:23 +0000170raw_ostream &raw_ostream::operator<<(const void *P) {
171 *this << '0' << 'x';
172
173 return write_hex((uintptr_t) P);
174}
175
Daniel Dunbar0a98b5f2009-03-16 22:55:06 +0000176void raw_ostream::flush_nonempty() {
177 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
Daniel Dunbar35993b32009-08-18 23:42:36 +0000178 size_t Length = OutBufCur - OutBufStart;
179 OutBufCur = OutBufStart;
180 write_impl(OutBufStart, Length);
Daniel Dunbar0a98b5f2009-03-16 22:55:06 +0000181}
182
Daniel Dunbar5cd6d452009-03-16 22:00:17 +0000183raw_ostream &raw_ostream::write(unsigned char C) {
Daniel Dunbar6c4570e2009-03-17 01:36:56 +0000184 // Group exceptional cases into a single branch.
Daniel Dunbar4855bfd2009-08-19 00:23:39 +0000185 if (BUILTIN_EXPECT(OutBufCur >= OutBufEnd, false)) {
186 if (BUILTIN_EXPECT(!OutBufStart, false)) {
Daniel Dunbar35993b32009-08-18 23:42:36 +0000187 if (BufferMode == Unbuffered) {
Dan Gohman7401d402009-08-18 20:09:59 +0000188 write_impl(reinterpret_cast<char*>(&C), 1);
189 return *this;
190 }
Daniel Dunbar4855bfd2009-08-19 00:23:39 +0000191 // Set up a buffer and start over.
192 SetBuffered();
193 return write(C);
Dan Gohman7401d402009-08-18 20:09:59 +0000194 }
Daniel Dunbar4855bfd2009-08-19 00:23:39 +0000195
196 flush_nonempty();
Daniel Dunbarc1730c22009-03-17 01:13:35 +0000197 }
198
Daniel Dunbar5cd6d452009-03-16 22:00:17 +0000199 *OutBufCur++ = C;
Daniel Dunbar3b909272009-03-16 22:08:44 +0000200 return *this;
Daniel Dunbar5cd6d452009-03-16 22:00:17 +0000201}
Chris Lattner1fefaac2008-08-23 22:23:09 +0000202
Dan Gohman75ffec02009-07-16 15:24:40 +0000203raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
Daniel Dunbar6c4570e2009-03-17 01:36:56 +0000204 // Group exceptional cases into a single branch.
Daniel Dunbard6976332009-03-17 21:15:18 +0000205 if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) {
Dan Gohman072828e2009-08-13 15:44:52 +0000206 if (BUILTIN_EXPECT(!OutBufStart, false)) {
Daniel Dunbar35993b32009-08-18 23:42:36 +0000207 if (BufferMode == Unbuffered) {
Dan Gohman072828e2009-08-13 15:44:52 +0000208 write_impl(Ptr, Size);
209 return *this;
210 }
211 // Set up a buffer and start over.
Dan Gohman6bd7ece2009-08-13 17:27:29 +0000212 SetBuffered();
Dan Gohman072828e2009-08-13 15:44:52 +0000213 return write(Ptr, Size);
214 }
Daniel Dunbar4855bfd2009-08-19 00:23:39 +0000215
Dan Gohman072828e2009-08-13 15:44:52 +0000216 // Write out the data in buffer-sized blocks until the remainder
217 // fits within the buffer.
218 do {
219 size_t NumBytes = OutBufEnd - OutBufCur;
220 copy_to_buffer(Ptr, NumBytes);
Daniel Dunbar6c4570e2009-03-17 01:36:56 +0000221 flush_nonempty();
Dan Gohman072828e2009-08-13 15:44:52 +0000222 Ptr += NumBytes;
223 Size -= NumBytes;
224 } while (OutBufCur+Size > OutBufEnd);
Daniel Dunbar6c4570e2009-03-17 01:36:56 +0000225 }
Dan Gohman072828e2009-08-13 15:44:52 +0000226
227 copy_to_buffer(Ptr, Size);
228
229 return *this;
230}
231
232void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) {
Dan Gohmane2633352009-08-13 20:32:03 +0000233 assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!");
Dan Gohman334a6822009-08-13 18:38:15 +0000234
Owen Anderson1046f622008-08-21 20:58:52 +0000235 // Handle short strings specially, memcpy isn't very good at very short
236 // strings.
237 switch (Size) {
238 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
239 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
240 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
241 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
242 case 0: break;
243 default:
Dan Gohman072828e2009-08-13 15:44:52 +0000244 memcpy(OutBufCur, Ptr, Size);
Owen Anderson1046f622008-08-21 20:58:52 +0000245 break;
246 }
Daniel Dunbarc9731c82009-03-10 16:21:55 +0000247
Dan Gohman072828e2009-08-13 15:44:52 +0000248 OutBufCur += Size;
Owen Anderson1046f622008-08-21 20:58:52 +0000249}
250
Chris Lattnerf9402fe2008-08-23 19:23:10 +0000251// Formatted output.
252raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
Daniel Dunbar6c4570e2009-03-17 01:36:56 +0000253 // If we have more than a few bytes left in our output buffer, try
254 // formatting directly onto its end.
Dan Gohman75ffec02009-07-16 15:24:40 +0000255 size_t NextBufferSize = 127;
Chris Lattnerf9402fe2008-08-23 19:23:10 +0000256 if (OutBufEnd-OutBufCur > 3) {
Dan Gohman75ffec02009-07-16 15:24:40 +0000257 size_t BufferBytesLeft = OutBufEnd-OutBufCur;
258 size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
Chris Lattnerf9402fe2008-08-23 19:23:10 +0000259
260 // Common case is that we have plenty of space.
261 if (BytesUsed < BufferBytesLeft) {
262 OutBufCur += BytesUsed;
263 return *this;
264 }
265
266 // Otherwise, we overflowed and the return value tells us the size to try
267 // again with.
268 NextBufferSize = BytesUsed;
269 }
270
271 // If we got here, we didn't have enough space in the output buffer for the
272 // string. Try printing into a SmallVector that is resized to have enough
273 // space. Iterate until we win.
274 SmallVector<char, 128> V;
275
276 while (1) {
277 V.resize(NextBufferSize);
278
279 // Try formatting into the SmallVector.
Dan Gohman75ffec02009-07-16 15:24:40 +0000280 size_t BytesUsed = Fmt.print(&V[0], NextBufferSize);
Chris Lattnerf9402fe2008-08-23 19:23:10 +0000281
282 // If BytesUsed fit into the vector, we win.
Chris Lattner0823c542008-10-26 19:20:47 +0000283 if (BytesUsed <= NextBufferSize)
Chris Lattnerf9402fe2008-08-23 19:23:10 +0000284 return write(&V[0], BytesUsed);
285
286 // Otherwise, try again with a new size.
287 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
288 NextBufferSize = BytesUsed;
289 }
290}
291
292//===----------------------------------------------------------------------===//
293// Formatted Output
294//===----------------------------------------------------------------------===//
295
296// Out of line virtual method.
297void format_object_base::home() {
298}
299
Chris Lattner18283402008-08-17 01:35:29 +0000300//===----------------------------------------------------------------------===//
301// raw_fd_ostream
302//===----------------------------------------------------------------------===//
303
Daniel Dunbaraa6fdf92008-10-21 19:53:10 +0000304/// raw_fd_ostream - Open the specified file for writing. If an error
305/// occurs, information about the error is put into ErrorInfo, and the
306/// stream should be immediately destroyed; the string will be empty
307/// if no error occurred.
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000308raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, bool Force,
Ted Kremeneke6d26892008-12-04 22:51:11 +0000309 std::string &ErrorInfo) : pos(0) {
Daniel Dunbaraa6fdf92008-10-21 19:53:10 +0000310 ErrorInfo.clear();
311
Chris Lattneref27e642008-08-17 03:53:23 +0000312 // Handle "-" as stdout.
313 if (Filename[0] == '-' && Filename[1] == 0) {
314 FD = STDOUT_FILENO;
Daniel Dunbar8c009a32008-11-13 05:01:07 +0000315 // If user requested binary then put stdout into binary mode if
316 // possible.
317 if (Binary)
318 sys::Program::ChangeStdoutToBinary();
Chris Lattneref27e642008-08-17 03:53:23 +0000319 ShouldClose = false;
320 return;
321 }
322
Daniel Dunbar8c009a32008-11-13 05:01:07 +0000323 int Flags = O_WRONLY|O_CREAT|O_TRUNC;
324#ifdef O_BINARY
325 if (Binary)
326 Flags |= O_BINARY;
327#endif
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000328 if (!Force)
329 Flags |= O_EXCL;
Dan Gohmancb87a6d2009-07-15 16:39:40 +0000330 FD = open(Filename, Flags, 0664);
Chris Lattner18283402008-08-17 01:35:29 +0000331 if (FD < 0) {
332 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
333 ShouldClose = false;
334 } else {
335 ShouldClose = true;
336 }
337}
338
339raw_fd_ostream::~raw_fd_ostream() {
Ted Kremeneke02443dc2008-10-23 23:49:09 +0000340 if (FD >= 0) {
341 flush();
342 if (ShouldClose)
Dan Gohman597da3c2009-07-15 16:43:01 +0000343 if (::close(FD) != 0)
Dan Gohmanfea42602009-07-15 23:25:33 +0000344 error_detected();
Ted Kremeneke02443dc2008-10-23 23:49:09 +0000345 }
Chris Lattner18283402008-08-17 01:35:29 +0000346}
347
Dan Gohman75ffec02009-07-16 15:24:40 +0000348void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
Ted Kremeneke02443dc2008-10-23 23:49:09 +0000349 assert (FD >= 0 && "File already closed.");
Daniel Dunbar308eff32009-03-16 23:29:31 +0000350 pos += Size;
Daniel Dunbara3230b72009-07-15 08:11:46 +0000351 if (::write(FD, Ptr, Size) != (ssize_t) Size)
Dan Gohmanfea42602009-07-15 23:25:33 +0000352 error_detected();
Chris Lattner18283402008-08-17 01:35:29 +0000353}
354
Ted Kremeneke02443dc2008-10-23 23:49:09 +0000355void raw_fd_ostream::close() {
356 assert (ShouldClose);
357 ShouldClose = false;
358 flush();
Dan Gohman597da3c2009-07-15 16:43:01 +0000359 if (::close(FD) != 0)
Dan Gohmanfea42602009-07-15 23:25:33 +0000360 error_detected();
Ted Kremeneke02443dc2008-10-23 23:49:09 +0000361 FD = -1;
362}
363
Ted Kremenekdbb64b02009-01-26 21:42:04 +0000364uint64_t raw_fd_ostream::seek(uint64_t off) {
365 flush();
Daniel Dunbara3230b72009-07-15 08:11:46 +0000366 pos = ::lseek(FD, off, SEEK_SET);
Dan Gohman597da3c2009-07-15 16:43:01 +0000367 if (pos != off)
Dan Gohmanfea42602009-07-15 23:25:33 +0000368 error_detected();
Ted Kremenekdbb64b02009-01-26 21:42:04 +0000369 return pos;
370}
371
Dan Gohman6bd7ece2009-08-13 17:27:29 +0000372size_t raw_fd_ostream::preferred_buffer_size() {
Dan Gohmane3bb7622009-08-15 21:41:03 +0000373#if !defined(_MSC_VER) && !defined(__MINGW32__) // Windows has no st_blksize.
Dan Gohman6bd7ece2009-08-13 17:27:29 +0000374 assert(FD >= 0 && "File not yet open!");
375 struct stat statbuf;
Dan Gohman314e3a62009-08-15 02:05:19 +0000376 if (fstat(FD, &statbuf) == 0) {
377 // If this is a terminal, don't use buffering. Line buffering
378 // would be a more traditional thing to do, but it's not worth
379 // the complexity.
380 if (S_ISCHR(statbuf.st_mode) && isatty(FD))
381 return 0;
382 // Return the preferred block size.
Dan Gohman6bd7ece2009-08-13 17:27:29 +0000383 return statbuf.st_blksize;
Dan Gohman314e3a62009-08-15 02:05:19 +0000384 }
Dan Gohman6bd7ece2009-08-13 17:27:29 +0000385 error_detected();
386#endif
387 return raw_ostream::preferred_buffer_size();
388}
389
Edwin Török8a06a5b2009-06-04 07:09:50 +0000390raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
391 bool bg) {
392 if (sys::Process::ColorNeedsFlush())
393 flush();
394 const char *colorcode =
395 (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
396 : sys::Process::OutputColor(colors, bold, bg);
397 if (colorcode) {
Dan Gohman75ffec02009-07-16 15:24:40 +0000398 size_t len = strlen(colorcode);
Edwin Török8a06a5b2009-06-04 07:09:50 +0000399 write(colorcode, len);
400 // don't account colors towards output characters
401 pos -= len;
402 }
403 return *this;
404}
405
406raw_ostream &raw_fd_ostream::resetColor() {
407 if (sys::Process::ColorNeedsFlush())
408 flush();
409 const char *colorcode = sys::Process::ResetColor();
410 if (colorcode) {
Dan Gohman75ffec02009-07-16 15:24:40 +0000411 size_t len = strlen(colorcode);
Edwin Török8a06a5b2009-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
Chris Lattner050c9612008-08-17 04:13:37 +0000419//===----------------------------------------------------------------------===//
420// raw_stdout/err_ostream
421//===----------------------------------------------------------------------===//
Chris Lattner18283402008-08-17 01:35:29 +0000422
Dan Gohmaneda5f0e2009-08-13 23:18:56 +0000423// Set buffer settings to model stdout and stderr behavior.
Dan Gohman314e3a62009-08-15 02:05:19 +0000424// Set standard error to be unbuffered by default.
425raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
426raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
Daniel Dunbarc9731c82009-03-10 16:21:55 +0000427 true) {}
Chris Lattner18283402008-08-17 01:35:29 +0000428
429// An out of line virtual method to provide a home for the class vtable.
430void raw_stdout_ostream::handle() {}
431void raw_stderr_ostream::handle() {}
Chris Lattner050c9612008-08-17 04:13:37 +0000432
433/// outs() - This returns a reference to a raw_ostream for standard output.
434/// Use it like: outs() << "foo" << "bar";
Owen Anderson847b99b2008-08-21 00:14:44 +0000435raw_ostream &llvm::outs() {
Chris Lattner050c9612008-08-17 04:13:37 +0000436 static raw_stdout_ostream S;
437 return S;
438}
439
440/// errs() - This returns a reference to a raw_ostream for standard error.
441/// Use it like: errs() << "foo" << "bar";
Owen Anderson847b99b2008-08-21 00:14:44 +0000442raw_ostream &llvm::errs() {
Chris Lattner050c9612008-08-17 04:13:37 +0000443 static raw_stderr_ostream S;
444 return S;
445}
446
Daniel Dunbaraa661bc2009-07-16 21:17:53 +0000447/// nulls() - This returns a reference to a raw_ostream which discards output.
448raw_ostream &llvm::nulls() {
449 static raw_null_ostream S;
450 return S;
451}
452
Chris Lattner050c9612008-08-17 04:13:37 +0000453//===----------------------------------------------------------------------===//
454// raw_os_ostream
455//===----------------------------------------------------------------------===//
456
Daniel Dunbar6c140bc2009-08-18 20:07:36 +0000457raw_os_ostream::~raw_os_ostream() {
458 flush();
459}
460
Dan Gohman75ffec02009-07-16 15:24:40 +0000461void raw_os_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar308eff32009-03-16 23:29:31 +0000462 OS.write(Ptr, Size);
Chris Lattner050c9612008-08-17 04:13:37 +0000463}
Chris Lattnera37b7612008-08-23 22:43:04 +0000464
Douglas Gregorfb5fc122009-04-20 07:34:17 +0000465uint64_t raw_os_ostream::current_pos() { return OS.tellp(); }
466
Chris Lattnera37b7612008-08-23 22:43:04 +0000467//===----------------------------------------------------------------------===//
468// raw_string_ostream
469//===----------------------------------------------------------------------===//
470
Daniel Dunbar6c140bc2009-08-18 20:07:36 +0000471raw_string_ostream::~raw_string_ostream() {
472 flush();
473}
474
Dan Gohman75ffec02009-07-16 15:24:40 +0000475void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar308eff32009-03-16 23:29:31 +0000476 OS.append(Ptr, Size);
Chris Lattnera37b7612008-08-23 22:43:04 +0000477}
478
479//===----------------------------------------------------------------------===//
480// raw_svector_ostream
481//===----------------------------------------------------------------------===//
482
Daniel Dunbar7dfa7412009-08-19 17:54:29 +0000483// The raw_svector_ostream implementation uses the SmallVector itself as the
484// buffer for the raw_ostream. We guarantee that the raw_ostream buffer is
485// always pointing past the end of the vector, but within the vector
486// capacity. This allows raw_ostream to write directly into the correct place,
487// and we only need to set the vector size when the data is flushed.
488
Daniel Dunbar35993b32009-08-18 23:42:36 +0000489raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {
Daniel Dunbar7dfa7412009-08-19 17:54:29 +0000490 // Set up the initial external buffer. We enforce that the buffer must have at
491 // least 128 bytes free; raw_ostream itself only requires 64, but we want to
492 // make sure that we don't grow the buffer unnecessarily on destruction (when
493 // the data is flushed). See the FIXME below.
494 if (OS.capacity() - OS.size() < 128)
495 llvm_report_error("Invalid argument, must have at least 128 bytes free!");
496 SetBuffer(OS.end(), OS.capacity() - OS.size());
Daniel Dunbar35993b32009-08-18 23:42:36 +0000497}
498
Daniel Dunbar6c140bc2009-08-18 20:07:36 +0000499raw_svector_ostream::~raw_svector_ostream() {
Daniel Dunbar7dfa7412009-08-19 17:54:29 +0000500 // FIXME: Prevent resizing during this flush().
Daniel Dunbar6c140bc2009-08-18 20:07:36 +0000501 flush();
502}
503
Dan Gohman75ffec02009-07-16 15:24:40 +0000504void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar7dfa7412009-08-19 17:54:29 +0000505 assert(Ptr == OS.end() && OS.size() + Size <= OS.capacity() &&
506 "Invalid write_impl() call!");
507
508 // We don't need to copy the bytes, just commit the bytes to the
509 // SmallVector.
510 OS.set_size(OS.size() + Size);
511
512 // Grow the vector if necessary.
513 if (OS.capacity() - OS.size() < 64)
514 OS.reserve(OS.capacity() * 2);
515
516 // Update the buffer position.
517 SetBuffer(OS.end(), OS.capacity() - OS.size());
Chris Lattnera37b7612008-08-23 22:43:04 +0000518}
519
Douglas Gregorfb5fc122009-04-20 07:34:17 +0000520uint64_t raw_svector_ostream::current_pos() { return OS.size(); }
521
Daniel Dunbaraa661bc2009-07-16 21:17:53 +0000522//===----------------------------------------------------------------------===//
523// raw_null_ostream
524//===----------------------------------------------------------------------===//
525
Dan Gohman7f4a9dd2009-07-27 21:46:02 +0000526raw_null_ostream::~raw_null_ostream() {
527#ifndef NDEBUG
528 // ~raw_ostream asserts that the buffer is empty. This isn't necessary
529 // with raw_null_ostream, but it's better to have raw_null_ostream follow
530 // the rules than to change the rules just for raw_null_ostream.
531 flush();
532#endif
533}
534
Daniel Dunbaraa661bc2009-07-16 21:17:53 +0000535void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
536}
537
538uint64_t raw_null_ostream::current_pos() {
539 return 0;
540}