blob: 31451ccfdb148c389ff08703ac95df6e239abdfa [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"
Chris Lattner23132b12009-08-24 03:52:50 +000023#include "llvm/ADT/StringExtras.h"
Dan Gohman208ec0f2009-08-13 17:27:29 +000024#include <sys/stat.h>
25#include <sys/types.h>
Chris Lattner60d39622008-08-17 01:35:29 +000026
Chris Lattner969a46a2008-08-22 15:45:00 +000027#if defined(HAVE_UNISTD_H)
28# include <unistd.h>
29#endif
30#if defined(HAVE_FCNTL_H)
31# include <fcntl.h>
32#endif
Argyrios Kyrtzidisb68dc362008-08-17 09:25:21 +000033
34#if defined(_MSC_VER)
Chris Lattner60d39622008-08-17 01:35:29 +000035#include <io.h>
Cedric Veneta3f343f2008-08-24 11:56:40 +000036#include <fcntl.h>
Owen Andersoncb371882008-08-21 00:14:44 +000037#ifndef STDIN_FILENO
38# define STDIN_FILENO 0
39#endif
40#ifndef STDOUT_FILENO
41# define STDOUT_FILENO 1
42#endif
43#ifndef STDERR_FILENO
44# define STDERR_FILENO 2
45#endif
Chris Lattner60d39622008-08-17 01:35:29 +000046#endif
47
Chris Lattner969a46a2008-08-22 15:45:00 +000048using namespace llvm;
49
Dan Gohmane87b2ab2009-07-15 23:25:33 +000050raw_ostream::~raw_ostream() {
Dan Gohman9a312542009-07-27 20:49:44 +000051 // raw_ostream's subclasses should take care to flush the buffer
52 // in their destructors.
53 assert(OutBufCur == OutBufStart &&
54 "raw_ostream destructor called with non-empty buffer!");
55
Daniel Dunbar906d5b42009-08-18 23:42:36 +000056 if (BufferMode == InternalBuffer)
57 delete [] OutBufStart;
Dan Gohmane87b2ab2009-07-15 23:25:33 +000058
59 // If there are any pending errors, report them now. Clients wishing
60 // to avoid llvm_report_error calls should check for errors with
61 // has_error() and clear the error flag with clear_error() before
62 // destructing raw_ostream objects which may have errors.
63 if (Error)
64 llvm_report_error("IO failure on output stream.");
65}
Chris Lattner969a46a2008-08-22 15:45:00 +000066
Chris Lattner60d39622008-08-17 01:35:29 +000067// An out of line virtual method to provide a home for the class vtable.
68void raw_ostream::handle() {}
69
Dan Gohman208ec0f2009-08-13 17:27:29 +000070size_t raw_ostream::preferred_buffer_size() {
71 // BUFSIZ is intended to be a reasonable default.
72 return BUFSIZ;
73}
74
75void raw_ostream::SetBuffered() {
76 // Ask the subclass to determine an appropriate buffer size.
Dan Gohmand7be6632009-08-15 02:05:19 +000077 if (size_t Size = preferred_buffer_size())
78 SetBufferSize(Size);
79 else
80 // It may return 0, meaning this stream should be unbuffered.
81 SetUnbuffered();
Dan Gohman208ec0f2009-08-13 17:27:29 +000082}
83
Daniel Dunbar906d5b42009-08-18 23:42:36 +000084void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size,
85 BufferKind Mode) {
86 assert(((Mode == Unbuffered && BufferStart == 0 && Size == 0) ||
Daniel Dunbarc8213b72009-09-15 20:31:46 +000087 (Mode != Unbuffered && BufferStart && Size)) &&
88 "stream must be unbuffered or have at least one byte");
Daniel Dunbar906d5b42009-08-18 23:42:36 +000089 // Make sure the current buffer is free of content (we can't flush here; the
90 // child buffer management logic will be in write_impl).
91 assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!");
Dan Gohman524dea42009-08-13 15:58:55 +000092
Daniel Dunbar906d5b42009-08-18 23:42:36 +000093 if (BufferMode == InternalBuffer)
94 delete [] OutBufStart;
95 OutBufStart = BufferStart;
Dan Gohman524dea42009-08-13 15:58:55 +000096 OutBufEnd = OutBufStart+Size;
97 OutBufCur = OutBufStart;
Daniel Dunbar906d5b42009-08-18 23:42:36 +000098 BufferMode = Mode;
Daniel Dunbar425d08c2009-08-19 17:54:29 +000099
100 assert(OutBufStart <= OutBufEnd && "Invalid size!");
Dan Gohman524dea42009-08-13 15:58:55 +0000101}
102
Owen Anderson66b17ba2008-08-21 20:58:52 +0000103raw_ostream &raw_ostream::operator<<(unsigned long N) {
104 // Zero is a special case.
105 if (N == 0)
106 return *this << '0';
107
108 char NumberBuffer[20];
109 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
110 char *CurPtr = EndPtr;
111
112 while (N) {
113 *--CurPtr = '0' + char(N % 10);
114 N /= 10;
115 }
116 return write(CurPtr, EndPtr-CurPtr);
117}
118
119raw_ostream &raw_ostream::operator<<(long N) {
120 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000121 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +0000122 N = -N;
123 }
124
125 return this->operator<<(static_cast<unsigned long>(N));
126}
127
128raw_ostream &raw_ostream::operator<<(unsigned long long N) {
Daniel Dunbarecbd0bc2009-08-19 16:25:25 +0000129 // Output using 32-bit div/mod when possible.
Daniel Dunbar78a3dd82009-07-29 06:45:14 +0000130 if (N == static_cast<unsigned long>(N))
131 return this->operator<<(static_cast<unsigned long>(N));
132
Daniel Dunbarecbd0bc2009-08-19 16:25:25 +0000133 char NumberBuffer[20];
134 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
135 char *CurPtr = EndPtr;
136
137 while (N) {
138 *--CurPtr = '0' + char(N % 10);
139 N /= 10;
140 }
141 return write(CurPtr, EndPtr-CurPtr);
Owen Anderson66b17ba2008-08-21 20:58:52 +0000142}
143
144raw_ostream &raw_ostream::operator<<(long long N) {
145 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000146 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +0000147 N = -N;
148 }
149
150 return this->operator<<(static_cast<unsigned long long>(N));
151}
152
Daniel Dunbar48018e02009-07-30 18:21:23 +0000153raw_ostream &raw_ostream::write_hex(unsigned long long N) {
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000154 // Zero is a special case.
155 if (N == 0)
156 return *this << '0';
157
158 char NumberBuffer[20];
159 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
160 char *CurPtr = EndPtr;
161
162 while (N) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000163 uintptr_t x = N % 16;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000164 *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10);
165 N /= 16;
166 }
167
168 return write(CurPtr, EndPtr-CurPtr);
Chris Lattner944fac72008-08-23 22:23:09 +0000169}
170
Daniel Dunbar522b1132009-10-17 20:43:08 +0000171raw_ostream &raw_ostream::write_escaped(StringRef Str) {
172 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
173 unsigned char c = Str[i];
174
175 switch (c) {
176 case '\\':
177 *this << '\\' << '\\';
178 break;
179 case '\t':
180 *this << '\\' << 't';
181 break;
182 case '\n':
183 *this << '\\' << 'n';
184 break;
185 case '"':
186 *this << '\\' << '"';
187 break;
188 default:
189 if (std::isprint(c)) {
190 *this << c;
191 break;
192 }
193
194 // Always expand to a 3-character octal escape.
195 *this << '\\';
196 *this << char('0' + ((c >> 6) & 7));
197 *this << char('0' + ((c >> 3) & 7));
198 *this << char('0' + ((c >> 0) & 7));
199 }
200 }
201
202 return *this;
203}
204
Daniel Dunbar48018e02009-07-30 18:21:23 +0000205raw_ostream &raw_ostream::operator<<(const void *P) {
206 *this << '0' << 'x';
207
208 return write_hex((uintptr_t) P);
209}
210
Chris Lattner23132b12009-08-24 03:52:50 +0000211raw_ostream &raw_ostream::operator<<(double N) {
212 this->operator<<(ftostr(N));
213 return *this;
214}
215
216
217
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000218void raw_ostream::flush_nonempty() {
219 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000220 size_t Length = OutBufCur - OutBufStart;
221 OutBufCur = OutBufStart;
222 write_impl(OutBufStart, Length);
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000223}
224
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000225raw_ostream &raw_ostream::write(unsigned char C) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000226 // Group exceptional cases into a single branch.
Daniel Dunbarecc67e22009-08-19 00:23:39 +0000227 if (BUILTIN_EXPECT(OutBufCur >= OutBufEnd, false)) {
228 if (BUILTIN_EXPECT(!OutBufStart, false)) {
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000229 if (BufferMode == Unbuffered) {
Dan Gohman37316042009-08-18 20:09:59 +0000230 write_impl(reinterpret_cast<char*>(&C), 1);
231 return *this;
232 }
Daniel Dunbarecc67e22009-08-19 00:23:39 +0000233 // Set up a buffer and start over.
234 SetBuffered();
235 return write(C);
Dan Gohman37316042009-08-18 20:09:59 +0000236 }
Daniel Dunbarecc67e22009-08-19 00:23:39 +0000237
238 flush_nonempty();
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000239 }
240
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000241 *OutBufCur++ = C;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000242 return *this;
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000243}
Chris Lattner944fac72008-08-23 22:23:09 +0000244
Dan Gohmanad60f662009-07-16 15:24:40 +0000245raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000246 // Group exceptional cases into a single branch.
Daniel Dunbarb372c112009-03-17 21:15:18 +0000247 if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) {
Dan Gohman33e49ef2009-08-13 15:44:52 +0000248 if (BUILTIN_EXPECT(!OutBufStart, false)) {
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000249 if (BufferMode == Unbuffered) {
Dan Gohman33e49ef2009-08-13 15:44:52 +0000250 write_impl(Ptr, Size);
251 return *this;
252 }
253 // Set up a buffer and start over.
Dan Gohman208ec0f2009-08-13 17:27:29 +0000254 SetBuffered();
Dan Gohman33e49ef2009-08-13 15:44:52 +0000255 return write(Ptr, Size);
256 }
Daniel Dunbarecc67e22009-08-19 00:23:39 +0000257
Dan Gohman33e49ef2009-08-13 15:44:52 +0000258 // Write out the data in buffer-sized blocks until the remainder
259 // fits within the buffer.
260 do {
261 size_t NumBytes = OutBufEnd - OutBufCur;
262 copy_to_buffer(Ptr, NumBytes);
Daniel Dunbar262541b2009-03-17 01:36:56 +0000263 flush_nonempty();
Dan Gohman33e49ef2009-08-13 15:44:52 +0000264 Ptr += NumBytes;
265 Size -= NumBytes;
266 } while (OutBufCur+Size > OutBufEnd);
Daniel Dunbar262541b2009-03-17 01:36:56 +0000267 }
Dan Gohman33e49ef2009-08-13 15:44:52 +0000268
269 copy_to_buffer(Ptr, Size);
270
271 return *this;
272}
273
274void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) {
Dan Gohmanc6126e82009-08-13 20:32:03 +0000275 assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!");
Dan Gohman4e8e6422009-08-13 18:38:15 +0000276
Owen Anderson66b17ba2008-08-21 20:58:52 +0000277 // Handle short strings specially, memcpy isn't very good at very short
278 // strings.
279 switch (Size) {
280 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
281 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
282 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
283 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
284 case 0: break;
285 default:
Dan Gohman33e49ef2009-08-13 15:44:52 +0000286 memcpy(OutBufCur, Ptr, Size);
Owen Anderson66b17ba2008-08-21 20:58:52 +0000287 break;
288 }
Daniel Dunbare77e4342009-03-10 16:21:55 +0000289
Dan Gohman33e49ef2009-08-13 15:44:52 +0000290 OutBufCur += Size;
Owen Anderson66b17ba2008-08-21 20:58:52 +0000291}
292
Chris Lattner097af7f2008-08-23 19:23:10 +0000293// Formatted output.
294raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000295 // If we have more than a few bytes left in our output buffer, try
296 // formatting directly onto its end.
Dan Gohmanad60f662009-07-16 15:24:40 +0000297 size_t NextBufferSize = 127;
Daniel Dunbar9441cfe2009-08-23 20:31:39 +0000298 size_t BufferBytesLeft = OutBufEnd - OutBufCur;
299 if (BufferBytesLeft > 3) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000300 size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
Chris Lattner097af7f2008-08-23 19:23:10 +0000301
302 // Common case is that we have plenty of space.
Daniel Dunbar9441cfe2009-08-23 20:31:39 +0000303 if (BytesUsed <= BufferBytesLeft) {
Chris Lattner097af7f2008-08-23 19:23:10 +0000304 OutBufCur += BytesUsed;
305 return *this;
306 }
307
308 // Otherwise, we overflowed and the return value tells us the size to try
309 // again with.
310 NextBufferSize = BytesUsed;
311 }
312
313 // If we got here, we didn't have enough space in the output buffer for the
314 // string. Try printing into a SmallVector that is resized to have enough
315 // space. Iterate until we win.
316 SmallVector<char, 128> V;
317
318 while (1) {
319 V.resize(NextBufferSize);
320
321 // Try formatting into the SmallVector.
Daniel Dunbar9441cfe2009-08-23 20:31:39 +0000322 size_t BytesUsed = Fmt.print(V.data(), NextBufferSize);
Chris Lattner097af7f2008-08-23 19:23:10 +0000323
324 // If BytesUsed fit into the vector, we win.
Chris Lattnerf61ca1e2008-10-26 19:20:47 +0000325 if (BytesUsed <= NextBufferSize)
Daniel Dunbar9441cfe2009-08-23 20:31:39 +0000326 return write(V.data(), BytesUsed);
Chris Lattner097af7f2008-08-23 19:23:10 +0000327
328 // Otherwise, try again with a new size.
329 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
330 NextBufferSize = BytesUsed;
331 }
332}
333
Chris Lattnerc5a227d2009-08-22 23:10:29 +0000334/// indent - Insert 'NumSpaces' spaces.
335raw_ostream &raw_ostream::indent(unsigned NumSpaces) {
Dan Gohman61ee1002009-08-24 04:13:01 +0000336 static const char Spaces[] = " "
337 " "
338 " ";
Chris Lattnerc5a227d2009-08-22 23:10:29 +0000339
340 // Usually the indentation is small, handle it with a fastpath.
Dan Gohmancb1308b2009-08-24 04:43:38 +0000341 if (NumSpaces < array_lengthof(Spaces))
Chris Lattnerc5a227d2009-08-22 23:10:29 +0000342 return write(Spaces, NumSpaces);
343
344 while (NumSpaces) {
Dan Gohmancb1308b2009-08-24 04:43:38 +0000345 unsigned NumToWrite = std::min(NumSpaces,
346 (unsigned)array_lengthof(Spaces)-1);
Chris Lattnerc5a227d2009-08-22 23:10:29 +0000347 write(Spaces, NumToWrite);
348 NumSpaces -= NumToWrite;
349 }
350 return *this;
351}
352
353
Chris Lattner097af7f2008-08-23 19:23:10 +0000354//===----------------------------------------------------------------------===//
355// Formatted Output
356//===----------------------------------------------------------------------===//
357
358// Out of line virtual method.
359void format_object_base::home() {
360}
361
Chris Lattner60d39622008-08-17 01:35:29 +0000362//===----------------------------------------------------------------------===//
363// raw_fd_ostream
364//===----------------------------------------------------------------------===//
365
Daniel Dunbar48534b32008-10-21 19:53:10 +0000366/// raw_fd_ostream - Open the specified file for writing. If an error
367/// occurs, information about the error is put into ErrorInfo, and the
368/// stream should be immediately destroyed; the string will be empty
369/// if no error occurred.
Chris Lattner17e9edc2009-08-23 02:51:22 +0000370raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
371 unsigned Flags) : pos(0) {
Dan Gohmanbaa26392009-08-25 15:34:52 +0000372 // Verify that we don't have both "append" and "excl".
373 assert((!(Flags & F_Excl) || !(Flags & F_Append)) &&
374 "Cannot specify both 'excl' and 'append' file creation flags!");
Chris Lattner17e9edc2009-08-23 02:51:22 +0000375
Daniel Dunbar48534b32008-10-21 19:53:10 +0000376 ErrorInfo.clear();
377
Chris Lattnerc52b1282008-08-17 03:53:23 +0000378 // Handle "-" as stdout.
379 if (Filename[0] == '-' && Filename[1] == 0) {
380 FD = STDOUT_FILENO;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000381 // If user requested binary then put stdout into binary mode if
382 // possible.
Chris Lattner17e9edc2009-08-23 02:51:22 +0000383 if (Flags & F_Binary)
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000384 sys::Program::ChangeStdoutToBinary();
Chris Lattnerc52b1282008-08-17 03:53:23 +0000385 ShouldClose = false;
386 return;
387 }
388
Chris Lattner17e9edc2009-08-23 02:51:22 +0000389 int OpenFlags = O_WRONLY|O_CREAT;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000390#ifdef O_BINARY
Benjamin Kramerae603242009-08-23 08:57:52 +0000391 if (Flags & F_Binary)
Chris Lattner17e9edc2009-08-23 02:51:22 +0000392 OpenFlags |= O_BINARY;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000393#endif
Chris Lattner17e9edc2009-08-23 02:51:22 +0000394
Dan Gohmanbaa26392009-08-25 15:34:52 +0000395 if (Flags & F_Append)
Chris Lattner17e9edc2009-08-23 02:51:22 +0000396 OpenFlags |= O_APPEND;
397 else
Dan Gohmanbaa26392009-08-25 15:34:52 +0000398 OpenFlags |= O_TRUNC;
399 if (Flags & F_Excl)
Chris Lattner17e9edc2009-08-23 02:51:22 +0000400 OpenFlags |= O_EXCL;
401
402 FD = open(Filename, OpenFlags, 0664);
Chris Lattner60d39622008-08-17 01:35:29 +0000403 if (FD < 0) {
404 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
405 ShouldClose = false;
406 } else {
407 ShouldClose = true;
408 }
409}
410
411raw_fd_ostream::~raw_fd_ostream() {
Chris Lattner17e9edc2009-08-23 02:51:22 +0000412 if (FD < 0) return;
413 flush();
414 if (ShouldClose)
415 if (::close(FD) != 0)
416 error_detected();
Chris Lattner60d39622008-08-17 01:35:29 +0000417}
418
Chris Lattner17e9edc2009-08-23 02:51:22 +0000419
Dan Gohmanad60f662009-07-16 15:24:40 +0000420void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000421 assert (FD >= 0 && "File already closed.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000422 pos += Size;
Daniel Dunbar579fb872009-07-15 08:11:46 +0000423 if (::write(FD, Ptr, Size) != (ssize_t) Size)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000424 error_detected();
Chris Lattner60d39622008-08-17 01:35:29 +0000425}
426
Ted Kremenek43d1f022008-10-23 23:49:09 +0000427void raw_fd_ostream::close() {
428 assert (ShouldClose);
429 ShouldClose = false;
430 flush();
Dan Gohmand3a974f2009-07-15 16:43:01 +0000431 if (::close(FD) != 0)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000432 error_detected();
Ted Kremenek43d1f022008-10-23 23:49:09 +0000433 FD = -1;
434}
435
Ted Kremenek9c278862009-01-26 21:42:04 +0000436uint64_t raw_fd_ostream::seek(uint64_t off) {
437 flush();
Daniel Dunbar579fb872009-07-15 08:11:46 +0000438 pos = ::lseek(FD, off, SEEK_SET);
Dan Gohmand3a974f2009-07-15 16:43:01 +0000439 if (pos != off)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000440 error_detected();
Ted Kremenek9c278862009-01-26 21:42:04 +0000441 return pos;
442}
443
Dan Gohman208ec0f2009-08-13 17:27:29 +0000444size_t raw_fd_ostream::preferred_buffer_size() {
Dan Gohmanb4741a72009-08-15 21:41:03 +0000445#if !defined(_MSC_VER) && !defined(__MINGW32__) // Windows has no st_blksize.
Dan Gohman208ec0f2009-08-13 17:27:29 +0000446 assert(FD >= 0 && "File not yet open!");
447 struct stat statbuf;
Dan Gohmand7be6632009-08-15 02:05:19 +0000448 if (fstat(FD, &statbuf) == 0) {
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.
Dan Gohman208ec0f2009-08-13 17:27:29 +0000455 return statbuf.st_blksize;
Dan Gohmand7be6632009-08-15 02:05:19 +0000456 }
Dan Gohman208ec0f2009-08-13 17:27:29 +0000457 error_detected();
458#endif
459 return raw_ostream::preferred_buffer_size();
460}
461
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000462raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
463 bool bg) {
464 if (sys::Process::ColorNeedsFlush())
465 flush();
466 const char *colorcode =
467 (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
468 : sys::Process::OutputColor(colors, bold, bg);
469 if (colorcode) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000470 size_t len = strlen(colorcode);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000471 write(colorcode, len);
472 // don't account colors towards output characters
473 pos -= len;
474 }
475 return *this;
476}
477
478raw_ostream &raw_fd_ostream::resetColor() {
479 if (sys::Process::ColorNeedsFlush())
480 flush();
481 const char *colorcode = sys::Process::ResetColor();
482 if (colorcode) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000483 size_t len = strlen(colorcode);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000484 write(colorcode, len);
485 // don't account colors towards output characters
486 pos -= len;
487 }
488 return *this;
489}
490
Dan Gohmanec080462009-09-11 20:46:33 +0000491bool raw_fd_ostream::is_displayed() const {
492 return sys::Process::FileDescriptorIsDisplayed(FD);
493}
494
Chris Lattner07f51f72008-08-17 04:13:37 +0000495//===----------------------------------------------------------------------===//
496// raw_stdout/err_ostream
497//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +0000498
Dan Gohmanec9b2612009-08-13 23:18:56 +0000499// Set buffer settings to model stdout and stderr behavior.
Dan Gohmand7be6632009-08-15 02:05:19 +0000500// Set standard error to be unbuffered by default.
501raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
502raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
Daniel Dunbare77e4342009-03-10 16:21:55 +0000503 true) {}
Chris Lattner60d39622008-08-17 01:35:29 +0000504
505// An out of line virtual method to provide a home for the class vtable.
506void raw_stdout_ostream::handle() {}
507void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +0000508
509/// outs() - This returns a reference to a raw_ostream for standard output.
510/// Use it like: outs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000511raw_ostream &llvm::outs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000512 static raw_stdout_ostream S;
513 return S;
514}
515
516/// errs() - This returns a reference to a raw_ostream for standard error.
517/// Use it like: errs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000518raw_ostream &llvm::errs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000519 static raw_stderr_ostream S;
520 return S;
521}
522
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000523/// nulls() - This returns a reference to a raw_ostream which discards output.
524raw_ostream &llvm::nulls() {
525 static raw_null_ostream S;
526 return S;
527}
528
Douglas Gregor8f7be472009-04-20 07:34:17 +0000529
Chris Lattner78a28122008-08-23 22:43:04 +0000530//===----------------------------------------------------------------------===//
531// raw_string_ostream
532//===----------------------------------------------------------------------===//
533
Daniel Dunbar35979c02009-08-18 20:07:36 +0000534raw_string_ostream::~raw_string_ostream() {
535 flush();
536}
537
Dan Gohmanad60f662009-07-16 15:24:40 +0000538void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000539 OS.append(Ptr, Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000540}
541
542//===----------------------------------------------------------------------===//
543// raw_svector_ostream
544//===----------------------------------------------------------------------===//
545
Daniel Dunbar425d08c2009-08-19 17:54:29 +0000546// The raw_svector_ostream implementation uses the SmallVector itself as the
547// buffer for the raw_ostream. We guarantee that the raw_ostream buffer is
548// always pointing past the end of the vector, but within the vector
549// capacity. This allows raw_ostream to write directly into the correct place,
550// and we only need to set the vector size when the data is flushed.
551
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000552raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {
Daniel Dunbard14787e2009-08-19 18:40:58 +0000553 // Set up the initial external buffer. We make sure that the buffer has at
Daniel Dunbar425d08c2009-08-19 17:54:29 +0000554 // least 128 bytes free; raw_ostream itself only requires 64, but we want to
555 // make sure that we don't grow the buffer unnecessarily on destruction (when
556 // the data is flushed). See the FIXME below.
Daniel Dunbard14787e2009-08-19 18:40:58 +0000557 OS.reserve(OS.size() + 128);
Daniel Dunbar425d08c2009-08-19 17:54:29 +0000558 SetBuffer(OS.end(), OS.capacity() - OS.size());
Daniel Dunbar906d5b42009-08-18 23:42:36 +0000559}
560
Daniel Dunbar35979c02009-08-18 20:07:36 +0000561raw_svector_ostream::~raw_svector_ostream() {
Daniel Dunbar425d08c2009-08-19 17:54:29 +0000562 // FIXME: Prevent resizing during this flush().
Daniel Dunbar35979c02009-08-18 20:07:36 +0000563 flush();
564}
565
Dan Gohmanad60f662009-07-16 15:24:40 +0000566void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar425d08c2009-08-19 17:54:29 +0000567 assert(Ptr == OS.end() && OS.size() + Size <= OS.capacity() &&
568 "Invalid write_impl() call!");
569
570 // We don't need to copy the bytes, just commit the bytes to the
571 // SmallVector.
572 OS.set_size(OS.size() + Size);
573
574 // Grow the vector if necessary.
575 if (OS.capacity() - OS.size() < 64)
576 OS.reserve(OS.capacity() * 2);
577
578 // Update the buffer position.
579 SetBuffer(OS.end(), OS.capacity() - OS.size());
Chris Lattner78a28122008-08-23 22:43:04 +0000580}
581
Douglas Gregor8f7be472009-04-20 07:34:17 +0000582uint64_t raw_svector_ostream::current_pos() { return OS.size(); }
583
Daniel Dunbard14787e2009-08-19 18:40:58 +0000584StringRef raw_svector_ostream::str() {
585 flush();
586 return StringRef(OS.begin(), OS.size());
587}
588
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000589//===----------------------------------------------------------------------===//
590// raw_null_ostream
591//===----------------------------------------------------------------------===//
592
Dan Gohmanf78c8352009-07-27 21:46:02 +0000593raw_null_ostream::~raw_null_ostream() {
594#ifndef NDEBUG
595 // ~raw_ostream asserts that the buffer is empty. This isn't necessary
596 // with raw_null_ostream, but it's better to have raw_null_ostream follow
597 // the rules than to change the rules just for raw_null_ostream.
598 flush();
599#endif
600}
601
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000602void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
603}
604
605uint64_t raw_null_ostream::current_pos() {
606 return 0;
607}