blob: f020681e6fa5e1e961b6f3279d2370140ea90a22 [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"
Dan Gohman61ee1002009-08-24 04:13:01 +000022#include "llvm/ADT/STLExtras.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
Chris Lattnercd0129f2009-12-19 01:38:42 +000069size_t raw_ostream::preferred_buffer_size() const {
Dan Gohman208ec0f2009-08-13 17:27:29 +000070 // 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) ||
Daniel Dunbarc8213b72009-09-15 20:31:46 +000086 (Mode != Unbuffered && BufferStart && Size)) &&
87 "stream must be unbuffered or have at least one byte");
Daniel Dunbar906d5b42009-08-18 23:42:36 +000088 // 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 Dunbar522b1132009-10-17 20:43:08 +0000170raw_ostream &raw_ostream::write_escaped(StringRef Str) {
171 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
172 unsigned char c = Str[i];
173
174 switch (c) {
175 case '\\':
176 *this << '\\' << '\\';
177 break;
178 case '\t':
179 *this << '\\' << 't';
180 break;
181 case '\n':
182 *this << '\\' << 'n';
183 break;
184 case '"':
185 *this << '\\' << '"';
186 break;
187 default:
188 if (std::isprint(c)) {
189 *this << c;
190 break;
191 }
192
193 // Always expand to a 3-character octal escape.
194 *this << '\\';
195 *this << char('0' + ((c >> 6) & 7));
196 *this << char('0' + ((c >> 3) & 7));
197 *this << char('0' + ((c >> 0) & 7));
198 }
199 }
200
201 return *this;
202}
203
Daniel Dunbar48018e02009-07-30 18:21:23 +0000204raw_ostream &raw_ostream::operator<<(const void *P) {
205 *this << '0' << 'x';
206
207 return write_hex((uintptr_t) P);
208}
209
Chris Lattner23132b12009-08-24 03:52:50 +0000210raw_ostream &raw_ostream::operator<<(double N) {
Benjamin Kramerf304ff62010-01-29 14:40:33 +0000211 return this->operator<<(format("%e", N));
Chris Lattner23132b12009-08-24 03:52:50 +0000212}
213
214
215
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000216void raw_ostream::flush_nonempty() {
217 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000218 size_t Length = OutBufCur - OutBufStart;
219 OutBufCur = OutBufStart;
220 write_impl(OutBufStart, Length);
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000221}
222
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000223raw_ostream &raw_ostream::write(unsigned char C) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000224 // Group exceptional cases into a single branch.
Daniel Dunbarecc67e22009-08-19 00:23:39 +0000225 if (BUILTIN_EXPECT(OutBufCur >= OutBufEnd, false)) {
226 if (BUILTIN_EXPECT(!OutBufStart, false)) {
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000227 if (BufferMode == Unbuffered) {
Dan Gohman37316042009-08-18 20:09:59 +0000228 write_impl(reinterpret_cast<char*>(&C), 1);
229 return *this;
230 }
Daniel Dunbarecc67e22009-08-19 00:23:39 +0000231 // Set up a buffer and start over.
232 SetBuffered();
233 return write(C);
Dan Gohman37316042009-08-18 20:09:59 +0000234 }
Daniel Dunbarecc67e22009-08-19 00:23:39 +0000235
236 flush_nonempty();
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000237 }
238
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000239 *OutBufCur++ = C;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000240 return *this;
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000241}
Chris Lattner944fac72008-08-23 22:23:09 +0000242
Dan Gohmanad60f662009-07-16 15:24:40 +0000243raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000244 // Group exceptional cases into a single branch.
Daniel Dunbarb372c112009-03-17 21:15:18 +0000245 if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) {
Dan Gohman33e49ef2009-08-13 15:44:52 +0000246 if (BUILTIN_EXPECT(!OutBufStart, false)) {
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000247 if (BufferMode == Unbuffered) {
Dan Gohman33e49ef2009-08-13 15:44:52 +0000248 write_impl(Ptr, Size);
249 return *this;
250 }
251 // Set up a buffer and start over.
Dan Gohman208ec0f2009-08-13 17:27:29 +0000252 SetBuffered();
Dan Gohman33e49ef2009-08-13 15:44:52 +0000253 return write(Ptr, Size);
254 }
Daniel Dunbarecc67e22009-08-19 00:23:39 +0000255
Dan Gohman33e49ef2009-08-13 15:44:52 +0000256 // Write out the data in buffer-sized blocks until the remainder
257 // fits within the buffer.
258 do {
259 size_t NumBytes = OutBufEnd - OutBufCur;
260 copy_to_buffer(Ptr, NumBytes);
Daniel Dunbar262541b2009-03-17 01:36:56 +0000261 flush_nonempty();
Dan Gohman33e49ef2009-08-13 15:44:52 +0000262 Ptr += NumBytes;
263 Size -= NumBytes;
264 } while (OutBufCur+Size > OutBufEnd);
Daniel Dunbar262541b2009-03-17 01:36:56 +0000265 }
Dan Gohman33e49ef2009-08-13 15:44:52 +0000266
267 copy_to_buffer(Ptr, Size);
268
269 return *this;
270}
271
272void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) {
Dan Gohmanc6126e82009-08-13 20:32:03 +0000273 assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!");
Dan Gohman4e8e6422009-08-13 18:38:15 +0000274
Owen Anderson66b17ba2008-08-21 20:58:52 +0000275 // Handle short strings specially, memcpy isn't very good at very short
276 // strings.
277 switch (Size) {
278 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
279 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
280 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
281 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
282 case 0: break;
283 default:
Dan Gohman33e49ef2009-08-13 15:44:52 +0000284 memcpy(OutBufCur, Ptr, Size);
Owen Anderson66b17ba2008-08-21 20:58:52 +0000285 break;
286 }
Daniel Dunbare77e4342009-03-10 16:21:55 +0000287
Dan Gohman33e49ef2009-08-13 15:44:52 +0000288 OutBufCur += Size;
Owen Anderson66b17ba2008-08-21 20:58:52 +0000289}
290
Chris Lattner097af7f2008-08-23 19:23:10 +0000291// Formatted output.
292raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000293 // If we have more than a few bytes left in our output buffer, try
294 // formatting directly onto its end.
Dan Gohmanad60f662009-07-16 15:24:40 +0000295 size_t NextBufferSize = 127;
Daniel Dunbar9441cfe2009-08-23 20:31:39 +0000296 size_t BufferBytesLeft = OutBufEnd - OutBufCur;
297 if (BufferBytesLeft > 3) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000298 size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
Chris Lattner097af7f2008-08-23 19:23:10 +0000299
300 // Common case is that we have plenty of space.
Daniel Dunbar9441cfe2009-08-23 20:31:39 +0000301 if (BytesUsed <= BufferBytesLeft) {
Chris Lattner097af7f2008-08-23 19:23:10 +0000302 OutBufCur += BytesUsed;
303 return *this;
304 }
305
306 // Otherwise, we overflowed and the return value tells us the size to try
307 // again with.
308 NextBufferSize = BytesUsed;
309 }
310
311 // If we got here, we didn't have enough space in the output buffer for the
312 // string. Try printing into a SmallVector that is resized to have enough
313 // space. Iterate until we win.
314 SmallVector<char, 128> V;
315
316 while (1) {
317 V.resize(NextBufferSize);
318
319 // Try formatting into the SmallVector.
Daniel Dunbar9441cfe2009-08-23 20:31:39 +0000320 size_t BytesUsed = Fmt.print(V.data(), NextBufferSize);
Chris Lattner097af7f2008-08-23 19:23:10 +0000321
322 // If BytesUsed fit into the vector, we win.
Chris Lattnerf61ca1e2008-10-26 19:20:47 +0000323 if (BytesUsed <= NextBufferSize)
Daniel Dunbar9441cfe2009-08-23 20:31:39 +0000324 return write(V.data(), BytesUsed);
Chris Lattner097af7f2008-08-23 19:23:10 +0000325
326 // Otherwise, try again with a new size.
327 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
328 NextBufferSize = BytesUsed;
329 }
330}
331
Chris Lattnerc5a227d2009-08-22 23:10:29 +0000332/// indent - Insert 'NumSpaces' spaces.
333raw_ostream &raw_ostream::indent(unsigned NumSpaces) {
Dan Gohman61ee1002009-08-24 04:13:01 +0000334 static const char Spaces[] = " "
335 " "
336 " ";
Chris Lattnerc5a227d2009-08-22 23:10:29 +0000337
338 // Usually the indentation is small, handle it with a fastpath.
Dan Gohmancb1308b2009-08-24 04:43:38 +0000339 if (NumSpaces < array_lengthof(Spaces))
Chris Lattnerc5a227d2009-08-22 23:10:29 +0000340 return write(Spaces, NumSpaces);
341
342 while (NumSpaces) {
Dan Gohmancb1308b2009-08-24 04:43:38 +0000343 unsigned NumToWrite = std::min(NumSpaces,
344 (unsigned)array_lengthof(Spaces)-1);
Chris Lattnerc5a227d2009-08-22 23:10:29 +0000345 write(Spaces, NumToWrite);
346 NumSpaces -= NumToWrite;
347 }
348 return *this;
349}
350
351
Chris Lattner097af7f2008-08-23 19:23:10 +0000352//===----------------------------------------------------------------------===//
353// Formatted Output
354//===----------------------------------------------------------------------===//
355
356// Out of line virtual method.
357void format_object_base::home() {
358}
359
Chris Lattner60d39622008-08-17 01:35:29 +0000360//===----------------------------------------------------------------------===//
361// raw_fd_ostream
362//===----------------------------------------------------------------------===//
363
Daniel Dunbar48534b32008-10-21 19:53:10 +0000364/// raw_fd_ostream - Open the specified file for writing. If an error
365/// occurs, information about the error is put into ErrorInfo, and the
366/// stream should be immediately destroyed; the string will be empty
367/// if no error occurred.
Chris Lattner17e9edc2009-08-23 02:51:22 +0000368raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
369 unsigned Flags) : pos(0) {
Dan Gohmanbaa26392009-08-25 15:34:52 +0000370 // Verify that we don't have both "append" and "excl".
371 assert((!(Flags & F_Excl) || !(Flags & F_Append)) &&
372 "Cannot specify both 'excl' and 'append' file creation flags!");
Chris Lattner17e9edc2009-08-23 02:51:22 +0000373
Daniel Dunbar48534b32008-10-21 19:53:10 +0000374 ErrorInfo.clear();
375
Chris Lattnerc52b1282008-08-17 03:53:23 +0000376 // Handle "-" as stdout.
377 if (Filename[0] == '-' && Filename[1] == 0) {
378 FD = STDOUT_FILENO;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000379 // If user requested binary then put stdout into binary mode if
380 // possible.
Chris Lattner17e9edc2009-08-23 02:51:22 +0000381 if (Flags & F_Binary)
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000382 sys::Program::ChangeStdoutToBinary();
Chris Lattnerc52b1282008-08-17 03:53:23 +0000383 ShouldClose = false;
384 return;
385 }
386
Chris Lattner17e9edc2009-08-23 02:51:22 +0000387 int OpenFlags = O_WRONLY|O_CREAT;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000388#ifdef O_BINARY
Benjamin Kramerae603242009-08-23 08:57:52 +0000389 if (Flags & F_Binary)
Chris Lattner17e9edc2009-08-23 02:51:22 +0000390 OpenFlags |= O_BINARY;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000391#endif
Chris Lattner17e9edc2009-08-23 02:51:22 +0000392
Dan Gohmanbaa26392009-08-25 15:34:52 +0000393 if (Flags & F_Append)
Chris Lattner17e9edc2009-08-23 02:51:22 +0000394 OpenFlags |= O_APPEND;
395 else
Dan Gohmanbaa26392009-08-25 15:34:52 +0000396 OpenFlags |= O_TRUNC;
397 if (Flags & F_Excl)
Chris Lattner17e9edc2009-08-23 02:51:22 +0000398 OpenFlags |= O_EXCL;
399
400 FD = open(Filename, OpenFlags, 0664);
Chris Lattner60d39622008-08-17 01:35:29 +0000401 if (FD < 0) {
402 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
403 ShouldClose = false;
404 } else {
405 ShouldClose = true;
406 }
407}
408
409raw_fd_ostream::~raw_fd_ostream() {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000410 if (FD < 0) return;
411 flush();
412 if (ShouldClose)
413 if (::close(FD) != 0)
414 error_detected();
Chris Lattner60d39622008-08-17 01:35:29 +0000415}
416
Chris Lattner17e9edc2009-08-23 02:51:22 +0000417
Dan Gohmanad60f662009-07-16 15:24:40 +0000418void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000419 assert (FD >= 0 && "File already closed.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000420 pos += Size;
Daniel Dunbar579fb872009-07-15 08:11:46 +0000421 if (::write(FD, Ptr, Size) != (ssize_t) Size)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000422 error_detected();
Chris Lattner60d39622008-08-17 01:35:29 +0000423}
424
Ted Kremenek43d1f022008-10-23 23:49:09 +0000425void raw_fd_ostream::close() {
426 assert (ShouldClose);
427 ShouldClose = false;
428 flush();
Dan Gohmand3a974f2009-07-15 16:43:01 +0000429 if (::close(FD) != 0)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000430 error_detected();
Ted Kremenek43d1f022008-10-23 23:49:09 +0000431 FD = -1;
432}
433
Ted Kremenek9c278862009-01-26 21:42:04 +0000434uint64_t raw_fd_ostream::seek(uint64_t off) {
435 flush();
Daniel Dunbar579fb872009-07-15 08:11:46 +0000436 pos = ::lseek(FD, off, SEEK_SET);
Dan Gohmand3a974f2009-07-15 16:43:01 +0000437 if (pos != off)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000438 error_detected();
Ted Kremenek9c278862009-01-26 21:42:04 +0000439 return pos;
440}
441
Chris Lattnercd0129f2009-12-19 01:38:42 +0000442size_t raw_fd_ostream::preferred_buffer_size() const {
Dan Gohmanb4741a72009-08-15 21:41:03 +0000443#if !defined(_MSC_VER) && !defined(__MINGW32__) // Windows has no st_blksize.
Dan Gohman208ec0f2009-08-13 17:27:29 +0000444 assert(FD >= 0 && "File not yet open!");
445 struct stat statbuf;
Chris Lattnercd0129f2009-12-19 01:38:42 +0000446 if (fstat(FD, &statbuf) != 0)
447 return 0;
448
449 // If this is a terminal, don't use buffering. Line buffering
450 // would be a more traditional thing to do, but it's not worth
451 // the complexity.
452 if (S_ISCHR(statbuf.st_mode) && isatty(FD))
453 return 0;
454 // Return the preferred block size.
455 return statbuf.st_blksize;
Dan Gohman208ec0f2009-08-13 17:27:29 +0000456#endif
457 return raw_ostream::preferred_buffer_size();
458}
459
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000460raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
461 bool bg) {
462 if (sys::Process::ColorNeedsFlush())
463 flush();
464 const char *colorcode =
465 (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
466 : sys::Process::OutputColor(colors, bold, bg);
467 if (colorcode) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000468 size_t len = strlen(colorcode);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000469 write(colorcode, len);
470 // don't account colors towards output characters
471 pos -= len;
472 }
473 return *this;
474}
475
476raw_ostream &raw_fd_ostream::resetColor() {
477 if (sys::Process::ColorNeedsFlush())
478 flush();
479 const char *colorcode = sys::Process::ResetColor();
480 if (colorcode) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000481 size_t len = strlen(colorcode);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000482 write(colorcode, len);
483 // don't account colors towards output characters
484 pos -= len;
485 }
486 return *this;
487}
488
Dan Gohmanec080462009-09-11 20:46:33 +0000489bool raw_fd_ostream::is_displayed() const {
490 return sys::Process::FileDescriptorIsDisplayed(FD);
491}
492
Chris Lattner07f51f72008-08-17 04:13:37 +0000493//===----------------------------------------------------------------------===//
494// raw_stdout/err_ostream
495//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +0000496
Dan Gohmanec9b2612009-08-13 23:18:56 +0000497// Set buffer settings to model stdout and stderr behavior.
Dan Gohmand7be6632009-08-15 02:05:19 +0000498// Set standard error to be unbuffered by default.
499raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
500raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
Daniel Dunbare77e4342009-03-10 16:21:55 +0000501 true) {}
Chris Lattner60d39622008-08-17 01:35:29 +0000502
503// An out of line virtual method to provide a home for the class vtable.
504void raw_stdout_ostream::handle() {}
505void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +0000506
507/// outs() - This returns a reference to a raw_ostream for standard output.
508/// Use it like: outs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000509raw_ostream &llvm::outs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000510 static raw_stdout_ostream S;
511 return S;
512}
513
514/// errs() - This returns a reference to a raw_ostream for standard error.
515/// Use it like: errs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000516raw_ostream &llvm::errs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000517 static raw_stderr_ostream S;
518 return S;
519}
520
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000521/// nulls() - This returns a reference to a raw_ostream which discards output.
522raw_ostream &llvm::nulls() {
523 static raw_null_ostream S;
524 return S;
525}
526
Douglas Gregor8f7be472009-04-20 07:34:17 +0000527
Chris Lattner78a28122008-08-23 22:43:04 +0000528//===----------------------------------------------------------------------===//
529// raw_string_ostream
530//===----------------------------------------------------------------------===//
531
Daniel Dunbar35979c02009-08-18 20:07:36 +0000532raw_string_ostream::~raw_string_ostream() {
533 flush();
534}
535
Dan Gohmanad60f662009-07-16 15:24:40 +0000536void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000537 OS.append(Ptr, Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000538}
539
540//===----------------------------------------------------------------------===//
541// raw_svector_ostream
542//===----------------------------------------------------------------------===//
543
Daniel Dunbar425d08c2009-08-19 17:54:29 +0000544// The raw_svector_ostream implementation uses the SmallVector itself as the
545// buffer for the raw_ostream. We guarantee that the raw_ostream buffer is
546// always pointing past the end of the vector, but within the vector
547// capacity. This allows raw_ostream to write directly into the correct place,
548// and we only need to set the vector size when the data is flushed.
549
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000550raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {
Daniel Dunbard14787e2009-08-19 18:40:58 +0000551 // Set up the initial external buffer. We make sure that the buffer has at
Daniel Dunbar425d08c2009-08-19 17:54:29 +0000552 // least 128 bytes free; raw_ostream itself only requires 64, but we want to
553 // make sure that we don't grow the buffer unnecessarily on destruction (when
554 // the data is flushed). See the FIXME below.
Daniel Dunbard14787e2009-08-19 18:40:58 +0000555 OS.reserve(OS.size() + 128);
Daniel Dunbar425d08c2009-08-19 17:54:29 +0000556 SetBuffer(OS.end(), OS.capacity() - OS.size());
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000557}
558
Daniel Dunbar35979c02009-08-18 20:07:36 +0000559raw_svector_ostream::~raw_svector_ostream() {
Daniel Dunbar425d08c2009-08-19 17:54:29 +0000560 // FIXME: Prevent resizing during this flush().
Daniel Dunbar35979c02009-08-18 20:07:36 +0000561 flush();
562}
563
Chris Lattner14ca1772010-01-22 21:16:10 +0000564/// resync - This is called when the SmallVector we're appending to is changed
565/// outside of the raw_svector_ostream's control. It is only safe to do this
566/// if the raw_svector_ostream has previously been flushed.
567void raw_svector_ostream::resync() {
568 assert(GetNumBytesInBuffer() == 0 && "Didn't flush before mutating vector");
569
570 if (OS.capacity() - OS.size() < 64)
571 OS.reserve(OS.capacity() * 2);
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000572 SetBuffer(OS.end(), OS.capacity() - OS.size());
573}
574
Dan Gohmanad60f662009-07-16 15:24:40 +0000575void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar425d08c2009-08-19 17:54:29 +0000576 assert(Ptr == OS.end() && OS.size() + Size <= OS.capacity() &&
577 "Invalid write_impl() call!");
578
579 // We don't need to copy the bytes, just commit the bytes to the
580 // SmallVector.
581 OS.set_size(OS.size() + Size);
582
583 // Grow the vector if necessary.
584 if (OS.capacity() - OS.size() < 64)
585 OS.reserve(OS.capacity() * 2);
586
587 // Update the buffer position.
588 SetBuffer(OS.end(), OS.capacity() - OS.size());
Chris Lattner78a28122008-08-23 22:43:04 +0000589}
590
Chris Lattnercd0129f2009-12-19 01:38:42 +0000591uint64_t raw_svector_ostream::current_pos() const {
592 return OS.size();
593}
Douglas Gregor8f7be472009-04-20 07:34:17 +0000594
Daniel Dunbard14787e2009-08-19 18:40:58 +0000595StringRef raw_svector_ostream::str() {
596 flush();
597 return StringRef(OS.begin(), OS.size());
598}
599
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000600//===----------------------------------------------------------------------===//
601// raw_null_ostream
602//===----------------------------------------------------------------------===//
603
Dan Gohmanf78c8352009-07-27 21:46:02 +0000604raw_null_ostream::~raw_null_ostream() {
605#ifndef NDEBUG
606 // ~raw_ostream asserts that the buffer is empty. This isn't necessary
607 // with raw_null_ostream, but it's better to have raw_null_ostream follow
608 // the rules than to change the rules just for raw_null_ostream.
609 flush();
610#endif
611}
612
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000613void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
614}
615
Chris Lattnercd0129f2009-12-19 01:38:42 +0000616uint64_t raw_null_ostream::current_pos() const {
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000617 return 0;
618}