blob: 0736a85eec0c0da6110688b7ae35282bb47f5837 [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 Lattner23132b12009-08-24 03:52:50 +000022#include "llvm/ADT/StringExtras.h"
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;
Daniel Dunbar425d08c2009-08-19 17:54:29 +000098
99 assert(OutBufStart <= OutBufEnd && "Invalid size!");
Dan Gohman524dea42009-08-13 15:58:55 +0000100}
101
Owen Anderson66b17ba2008-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 Dunbarde75d7f2009-03-16 22:00:17 +0000120 *this << '-';
Owen Anderson66b17ba2008-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 Dunbarecbd0bc2009-08-19 16:25:25 +0000128 // Output using 32-bit div/mod when possible.
Daniel Dunbar78a3dd82009-07-29 06:45:14 +0000129 if (N == static_cast<unsigned long>(N))
130 return this->operator<<(static_cast<unsigned long>(N));
131
Daniel Dunbarecbd0bc2009-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 Anderson66b17ba2008-08-21 20:58:52 +0000141}
142
143raw_ostream &raw_ostream::operator<<(long long N) {
144 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000145 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +0000146 N = -N;
147 }
148
149 return this->operator<<(static_cast<unsigned long long>(N));
150}
151
Daniel Dunbar48018e02009-07-30 18:21:23 +0000152raw_ostream &raw_ostream::write_hex(unsigned long long N) {
Daniel Dunbarb451a0c2009-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 Gohmanad60f662009-07-16 15:24:40 +0000162 uintptr_t x = N % 16;
Daniel Dunbarb451a0c2009-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 Lattner944fac72008-08-23 22:23:09 +0000168}
169
Daniel Dunbar48018e02009-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
Chris Lattner23132b12009-08-24 03:52:50 +0000176raw_ostream &raw_ostream::operator<<(double N) {
177 this->operator<<(ftostr(N));
178 return *this;
179}
180
181
182
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000183void raw_ostream::flush_nonempty() {
184 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000185 size_t Length = OutBufCur - OutBufStart;
186 OutBufCur = OutBufStart;
187 write_impl(OutBufStart, Length);
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000188}
189
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000190raw_ostream &raw_ostream::write(unsigned char C) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000191 // Group exceptional cases into a single branch.
Daniel Dunbarecc67e22009-08-19 00:23:39 +0000192 if (BUILTIN_EXPECT(OutBufCur >= OutBufEnd, false)) {
193 if (BUILTIN_EXPECT(!OutBufStart, false)) {
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000194 if (BufferMode == Unbuffered) {
Dan Gohman37316042009-08-18 20:09:59 +0000195 write_impl(reinterpret_cast<char*>(&C), 1);
196 return *this;
197 }
Daniel Dunbarecc67e22009-08-19 00:23:39 +0000198 // Set up a buffer and start over.
199 SetBuffered();
200 return write(C);
Dan Gohman37316042009-08-18 20:09:59 +0000201 }
Daniel Dunbarecc67e22009-08-19 00:23:39 +0000202
203 flush_nonempty();
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000204 }
205
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000206 *OutBufCur++ = C;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000207 return *this;
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000208}
Chris Lattner944fac72008-08-23 22:23:09 +0000209
Dan Gohmanad60f662009-07-16 15:24:40 +0000210raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000211 // Group exceptional cases into a single branch.
Daniel Dunbarb372c112009-03-17 21:15:18 +0000212 if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) {
Dan Gohman33e49ef2009-08-13 15:44:52 +0000213 if (BUILTIN_EXPECT(!OutBufStart, false)) {
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000214 if (BufferMode == Unbuffered) {
Dan Gohman33e49ef2009-08-13 15:44:52 +0000215 write_impl(Ptr, Size);
216 return *this;
217 }
218 // Set up a buffer and start over.
Dan Gohman208ec0f2009-08-13 17:27:29 +0000219 SetBuffered();
Dan Gohman33e49ef2009-08-13 15:44:52 +0000220 return write(Ptr, Size);
221 }
Daniel Dunbarecc67e22009-08-19 00:23:39 +0000222
Dan Gohman33e49ef2009-08-13 15:44:52 +0000223 // 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.
Dan Gohmanad60f662009-07-16 15:24:40 +0000262 size_t NextBufferSize = 127;
Daniel Dunbar9441cfe2009-08-23 20:31:39 +0000263 size_t BufferBytesLeft = OutBufEnd - OutBufCur;
264 if (BufferBytesLeft > 3) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000265 size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
Chris Lattner097af7f2008-08-23 19:23:10 +0000266
267 // Common case is that we have plenty of space.
Daniel Dunbar9441cfe2009-08-23 20:31:39 +0000268 if (BytesUsed <= BufferBytesLeft) {
Chris Lattner097af7f2008-08-23 19:23:10 +0000269 OutBufCur += BytesUsed;
270 return *this;
271 }
272
273 // Otherwise, we overflowed and the return value tells us the size to try
274 // again with.
275 NextBufferSize = BytesUsed;
276 }
277
278 // If we got here, we didn't have enough space in the output buffer for the
279 // string. Try printing into a SmallVector that is resized to have enough
280 // space. Iterate until we win.
281 SmallVector<char, 128> V;
282
283 while (1) {
284 V.resize(NextBufferSize);
285
286 // Try formatting into the SmallVector.
Daniel Dunbar9441cfe2009-08-23 20:31:39 +0000287 size_t BytesUsed = Fmt.print(V.data(), NextBufferSize);
Chris Lattner097af7f2008-08-23 19:23:10 +0000288
289 // If BytesUsed fit into the vector, we win.
Chris Lattnerf61ca1e2008-10-26 19:20:47 +0000290 if (BytesUsed <= NextBufferSize)
Daniel Dunbar9441cfe2009-08-23 20:31:39 +0000291 return write(V.data(), BytesUsed);
Chris Lattner097af7f2008-08-23 19:23:10 +0000292
293 // Otherwise, try again with a new size.
294 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
295 NextBufferSize = BytesUsed;
296 }
297}
298
Chris Lattnerc5a227d2009-08-22 23:10:29 +0000299/// indent - Insert 'NumSpaces' spaces.
300raw_ostream &raw_ostream::indent(unsigned NumSpaces) {
301 const char *Spaces = " ";
302
303 // Usually the indentation is small, handle it with a fastpath.
304 if (NumSpaces <= 16)
305 return write(Spaces, NumSpaces);
306
307 while (NumSpaces) {
308 unsigned NumToWrite = std::min(NumSpaces, 16U);
309 write(Spaces, NumToWrite);
310 NumSpaces -= NumToWrite;
311 }
312 return *this;
313}
314
315
Chris Lattner097af7f2008-08-23 19:23:10 +0000316//===----------------------------------------------------------------------===//
317// Formatted Output
318//===----------------------------------------------------------------------===//
319
320// Out of line virtual method.
321void format_object_base::home() {
322}
323
Chris Lattner60d39622008-08-17 01:35:29 +0000324//===----------------------------------------------------------------------===//
325// raw_fd_ostream
326//===----------------------------------------------------------------------===//
327
Daniel Dunbar48534b32008-10-21 19:53:10 +0000328/// raw_fd_ostream - Open the specified file for writing. If an error
329/// occurs, information about the error is put into ErrorInfo, and the
330/// stream should be immediately destroyed; the string will be empty
331/// if no error occurred.
Chris Lattner17e9edc2009-08-23 02:51:22 +0000332raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
333 unsigned Flags) : pos(0) {
334 // Verify that we don't have both "append" and "force".
335 assert((!(Flags & F_Force) || !(Flags & F_Append)) &&
336 "Cannot specify both 'force' and 'append' file creation flags!");
337
Daniel Dunbar48534b32008-10-21 19:53:10 +0000338 ErrorInfo.clear();
339
Chris Lattnerc52b1282008-08-17 03:53:23 +0000340 // Handle "-" as stdout.
341 if (Filename[0] == '-' && Filename[1] == 0) {
342 FD = STDOUT_FILENO;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000343 // If user requested binary then put stdout into binary mode if
344 // possible.
Chris Lattner17e9edc2009-08-23 02:51:22 +0000345 if (Flags & F_Binary)
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000346 sys::Program::ChangeStdoutToBinary();
Chris Lattnerc52b1282008-08-17 03:53:23 +0000347 ShouldClose = false;
348 return;
349 }
350
Chris Lattner17e9edc2009-08-23 02:51:22 +0000351 int OpenFlags = O_WRONLY|O_CREAT;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000352#ifdef O_BINARY
Benjamin Kramerae603242009-08-23 08:57:52 +0000353 if (Flags & F_Binary)
Chris Lattner17e9edc2009-08-23 02:51:22 +0000354 OpenFlags |= O_BINARY;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000355#endif
Chris Lattner17e9edc2009-08-23 02:51:22 +0000356
357 if (Flags & F_Force)
358 OpenFlags |= O_TRUNC;
359 else if (Flags & F_Append)
360 OpenFlags |= O_APPEND;
361 else
362 OpenFlags |= O_EXCL;
363
364 FD = open(Filename, OpenFlags, 0664);
Chris Lattner60d39622008-08-17 01:35:29 +0000365 if (FD < 0) {
366 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
367 ShouldClose = false;
368 } else {
369 ShouldClose = true;
370 }
371}
372
373raw_fd_ostream::~raw_fd_ostream() {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000374 if (FD < 0) return;
375 flush();
376 if (ShouldClose)
377 if (::close(FD) != 0)
378 error_detected();
Chris Lattner60d39622008-08-17 01:35:29 +0000379}
380
Chris Lattner17e9edc2009-08-23 02:51:22 +0000381
Dan Gohmanad60f662009-07-16 15:24:40 +0000382void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000383 assert (FD >= 0 && "File already closed.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000384 pos += Size;
Daniel Dunbar579fb872009-07-15 08:11:46 +0000385 if (::write(FD, Ptr, Size) != (ssize_t) Size)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000386 error_detected();
Chris Lattner60d39622008-08-17 01:35:29 +0000387}
388
Ted Kremenek43d1f022008-10-23 23:49:09 +0000389void raw_fd_ostream::close() {
390 assert (ShouldClose);
391 ShouldClose = false;
392 flush();
Dan Gohmand3a974f2009-07-15 16:43:01 +0000393 if (::close(FD) != 0)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000394 error_detected();
Ted Kremenek43d1f022008-10-23 23:49:09 +0000395 FD = -1;
396}
397
Ted Kremenek9c278862009-01-26 21:42:04 +0000398uint64_t raw_fd_ostream::seek(uint64_t off) {
399 flush();
Daniel Dunbar579fb872009-07-15 08:11:46 +0000400 pos = ::lseek(FD, off, SEEK_SET);
Dan Gohmand3a974f2009-07-15 16:43:01 +0000401 if (pos != off)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000402 error_detected();
Ted Kremenek9c278862009-01-26 21:42:04 +0000403 return pos;
404}
405
Dan Gohman208ec0f2009-08-13 17:27:29 +0000406size_t raw_fd_ostream::preferred_buffer_size() {
Dan Gohmanb4741a72009-08-15 21:41:03 +0000407#if !defined(_MSC_VER) && !defined(__MINGW32__) // Windows has no st_blksize.
Dan Gohman208ec0f2009-08-13 17:27:29 +0000408 assert(FD >= 0 && "File not yet open!");
409 struct stat statbuf;
Dan Gohmand7be6632009-08-15 02:05:19 +0000410 if (fstat(FD, &statbuf) == 0) {
411 // If this is a terminal, don't use buffering. Line buffering
412 // would be a more traditional thing to do, but it's not worth
413 // the complexity.
414 if (S_ISCHR(statbuf.st_mode) && isatty(FD))
415 return 0;
416 // Return the preferred block size.
Dan Gohman208ec0f2009-08-13 17:27:29 +0000417 return statbuf.st_blksize;
Dan Gohmand7be6632009-08-15 02:05:19 +0000418 }
Dan Gohman208ec0f2009-08-13 17:27:29 +0000419 error_detected();
420#endif
421 return raw_ostream::preferred_buffer_size();
422}
423
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000424raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
425 bool bg) {
426 if (sys::Process::ColorNeedsFlush())
427 flush();
428 const char *colorcode =
429 (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
430 : sys::Process::OutputColor(colors, bold, bg);
431 if (colorcode) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000432 size_t len = strlen(colorcode);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000433 write(colorcode, len);
434 // don't account colors towards output characters
435 pos -= len;
436 }
437 return *this;
438}
439
440raw_ostream &raw_fd_ostream::resetColor() {
441 if (sys::Process::ColorNeedsFlush())
442 flush();
443 const char *colorcode = sys::Process::ResetColor();
444 if (colorcode) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000445 size_t len = strlen(colorcode);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000446 write(colorcode, len);
447 // don't account colors towards output characters
448 pos -= len;
449 }
450 return *this;
451}
452
Chris Lattner07f51f72008-08-17 04:13:37 +0000453//===----------------------------------------------------------------------===//
454// raw_stdout/err_ostream
455//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +0000456
Dan Gohmanec9b2612009-08-13 23:18:56 +0000457// Set buffer settings to model stdout and stderr behavior.
Dan Gohmand7be6632009-08-15 02:05:19 +0000458// Set standard error to be unbuffered by default.
459raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
460raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
Daniel Dunbare77e4342009-03-10 16:21:55 +0000461 true) {}
Chris Lattner60d39622008-08-17 01:35:29 +0000462
463// An out of line virtual method to provide a home for the class vtable.
464void raw_stdout_ostream::handle() {}
465void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +0000466
467/// outs() - This returns a reference to a raw_ostream for standard output.
468/// Use it like: outs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000469raw_ostream &llvm::outs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000470 static raw_stdout_ostream S;
471 return S;
472}
473
474/// errs() - This returns a reference to a raw_ostream for standard error.
475/// Use it like: errs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000476raw_ostream &llvm::errs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000477 static raw_stderr_ostream S;
478 return S;
479}
480
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000481/// nulls() - This returns a reference to a raw_ostream which discards output.
482raw_ostream &llvm::nulls() {
483 static raw_null_ostream S;
484 return S;
485}
486
Douglas Gregor8f7be472009-04-20 07:34:17 +0000487
Chris Lattner78a28122008-08-23 22:43:04 +0000488//===----------------------------------------------------------------------===//
489// raw_string_ostream
490//===----------------------------------------------------------------------===//
491
Daniel Dunbar35979c02009-08-18 20:07:36 +0000492raw_string_ostream::~raw_string_ostream() {
493 flush();
494}
495
Dan Gohmanad60f662009-07-16 15:24:40 +0000496void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000497 OS.append(Ptr, Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000498}
499
500//===----------------------------------------------------------------------===//
501// raw_svector_ostream
502//===----------------------------------------------------------------------===//
503
Daniel Dunbar425d08c2009-08-19 17:54:29 +0000504// The raw_svector_ostream implementation uses the SmallVector itself as the
505// buffer for the raw_ostream. We guarantee that the raw_ostream buffer is
506// always pointing past the end of the vector, but within the vector
507// capacity. This allows raw_ostream to write directly into the correct place,
508// and we only need to set the vector size when the data is flushed.
509
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000510raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {
Daniel Dunbard14787e2009-08-19 18:40:58 +0000511 // Set up the initial external buffer. We make sure that the buffer has at
Daniel Dunbar425d08c2009-08-19 17:54:29 +0000512 // least 128 bytes free; raw_ostream itself only requires 64, but we want to
513 // make sure that we don't grow the buffer unnecessarily on destruction (when
514 // the data is flushed). See the FIXME below.
Daniel Dunbard14787e2009-08-19 18:40:58 +0000515 OS.reserve(OS.size() + 128);
Daniel Dunbar425d08c2009-08-19 17:54:29 +0000516 SetBuffer(OS.end(), OS.capacity() - OS.size());
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000517}
518
Daniel Dunbar35979c02009-08-18 20:07:36 +0000519raw_svector_ostream::~raw_svector_ostream() {
Daniel Dunbar425d08c2009-08-19 17:54:29 +0000520 // FIXME: Prevent resizing during this flush().
Daniel Dunbar35979c02009-08-18 20:07:36 +0000521 flush();
522}
523
Dan Gohmanad60f662009-07-16 15:24:40 +0000524void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar425d08c2009-08-19 17:54:29 +0000525 assert(Ptr == OS.end() && OS.size() + Size <= OS.capacity() &&
526 "Invalid write_impl() call!");
527
528 // We don't need to copy the bytes, just commit the bytes to the
529 // SmallVector.
530 OS.set_size(OS.size() + Size);
531
532 // Grow the vector if necessary.
533 if (OS.capacity() - OS.size() < 64)
534 OS.reserve(OS.capacity() * 2);
535
536 // Update the buffer position.
537 SetBuffer(OS.end(), OS.capacity() - OS.size());
Chris Lattner78a28122008-08-23 22:43:04 +0000538}
539
Douglas Gregor8f7be472009-04-20 07:34:17 +0000540uint64_t raw_svector_ostream::current_pos() { return OS.size(); }
541
Daniel Dunbard14787e2009-08-19 18:40:58 +0000542StringRef raw_svector_ostream::str() {
543 flush();
544 return StringRef(OS.begin(), OS.size());
545}
546
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000547//===----------------------------------------------------------------------===//
548// raw_null_ostream
549//===----------------------------------------------------------------------===//
550
Dan Gohmanf78c8352009-07-27 21:46:02 +0000551raw_null_ostream::~raw_null_ostream() {
552#ifndef NDEBUG
553 // ~raw_ostream asserts that the buffer is empty. This isn't necessary
554 // with raw_null_ostream, but it's better to have raw_null_ostream follow
555 // the rules than to change the rules just for raw_null_ostream.
556 flush();
557#endif
558}
559
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000560void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
561}
562
563uint64_t raw_null_ostream::current_pos() {
564 return 0;
565}