blob: 3cec18b442ecf5b8ff38fa59a0edb604684ff5ef [file] [log] [blame]
Chris Lattner60d39622008-08-17 01:35:29 +00001//===--- raw_ostream.cpp - Implement the raw_ostream classes --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements support for bulk buffered stream output.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/raw_ostream.h"
Chris Lattner42f77ab2008-08-23 20:34:06 +000015#include "llvm/Support/Format.h"
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +000016#include "llvm/System/Program.h"
Torok Edwine8ebb0f2009-06-04 07:09:50 +000017#include "llvm/System/Process.h"
Chris Lattner097af7f2008-08-23 19:23:10 +000018#include "llvm/ADT/SmallVector.h"
Chris Lattner969a46a2008-08-22 15:45:00 +000019#include "llvm/Config/config.h"
Daniel Dunbarb372c112009-03-17 21:15:18 +000020#include "llvm/Support/Compiler.h"
Daniel Dunbar579fb872009-07-15 08:11:46 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattner07f51f72008-08-17 04:13:37 +000022#include <ostream>
Chris Lattner60d39622008-08-17 01:35:29 +000023
Chris Lattner969a46a2008-08-22 15:45:00 +000024#if defined(HAVE_UNISTD_H)
25# include <unistd.h>
26#endif
27#if defined(HAVE_FCNTL_H)
28# include <fcntl.h>
29#endif
Argyrios Kyrtzidisb68dc362008-08-17 09:25:21 +000030
31#if defined(_MSC_VER)
Chris Lattner60d39622008-08-17 01:35:29 +000032#include <io.h>
Cedric Veneta3f343f2008-08-24 11:56:40 +000033#include <fcntl.h>
Owen Andersoncb371882008-08-21 00:14:44 +000034#ifndef STDIN_FILENO
35# define STDIN_FILENO 0
36#endif
37#ifndef STDOUT_FILENO
38# define STDOUT_FILENO 1
39#endif
40#ifndef STDERR_FILENO
41# define STDERR_FILENO 2
42#endif
Chris Lattner60d39622008-08-17 01:35:29 +000043#endif
44
Chris Lattner969a46a2008-08-22 15:45:00 +000045using namespace llvm;
46
Dan Gohmane87b2ab2009-07-15 23:25:33 +000047raw_ostream::~raw_ostream() {
48 delete [] OutBufStart;
49
50 // If there are any pending errors, report them now. Clients wishing
51 // to avoid llvm_report_error calls should check for errors with
52 // has_error() and clear the error flag with clear_error() before
53 // destructing raw_ostream objects which may have errors.
54 if (Error)
55 llvm_report_error("IO failure on output stream.");
56}
Chris Lattner969a46a2008-08-22 15:45:00 +000057
Chris Lattner60d39622008-08-17 01:35:29 +000058// An out of line virtual method to provide a home for the class vtable.
59void raw_ostream::handle() {}
60
Owen Anderson66b17ba2008-08-21 20:58:52 +000061raw_ostream &raw_ostream::operator<<(unsigned long N) {
62 // Zero is a special case.
63 if (N == 0)
64 return *this << '0';
65
66 char NumberBuffer[20];
67 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
68 char *CurPtr = EndPtr;
69
70 while (N) {
71 *--CurPtr = '0' + char(N % 10);
72 N /= 10;
73 }
74 return write(CurPtr, EndPtr-CurPtr);
75}
76
77raw_ostream &raw_ostream::operator<<(long N) {
78 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +000079 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +000080 N = -N;
81 }
82
83 return this->operator<<(static_cast<unsigned long>(N));
84}
85
86raw_ostream &raw_ostream::operator<<(unsigned long long N) {
87 // Zero is a special case.
88 if (N == 0)
89 return *this << '0';
90
91 char NumberBuffer[20];
92 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
93 char *CurPtr = EndPtr;
94
95 while (N) {
96 *--CurPtr = '0' + char(N % 10);
97 N /= 10;
98 }
99 return write(CurPtr, EndPtr-CurPtr);
100}
101
102raw_ostream &raw_ostream::operator<<(long long N) {
103 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000104 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +0000105 N = -N;
106 }
107
108 return this->operator<<(static_cast<unsigned long long>(N));
109}
110
Chris Lattner944fac72008-08-23 22:23:09 +0000111raw_ostream &raw_ostream::operator<<(const void *P) {
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000112 uintptr_t N = (uintptr_t) P;
113 *this << '0' << 'x';
114
115 // Zero is a special case.
116 if (N == 0)
117 return *this << '0';
118
119 char NumberBuffer[20];
120 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
121 char *CurPtr = EndPtr;
122
123 while (N) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000124 uintptr_t x = N % 16;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000125 *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10);
126 N /= 16;
127 }
128
129 return write(CurPtr, EndPtr-CurPtr);
Chris Lattner944fac72008-08-23 22:23:09 +0000130}
131
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000132void raw_ostream::flush_nonempty() {
133 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000134 write_impl(OutBufStart, OutBufCur - OutBufStart);
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000135 OutBufCur = OutBufStart;
136}
137
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000138raw_ostream &raw_ostream::write(unsigned char C) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000139 // Group exceptional cases into a single branch.
140 if (OutBufCur >= OutBufEnd) {
141 if (Unbuffered) {
142 write_impl(reinterpret_cast<char*>(&C), 1);
143 return *this;
144 }
145
146 if (!OutBufStart)
147 SetBufferSize();
148 else
149 flush_nonempty();
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000150 }
151
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000152 *OutBufCur++ = C;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000153 return *this;
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000154}
Chris Lattner944fac72008-08-23 22:23:09 +0000155
Dan Gohmanad60f662009-07-16 15:24:40 +0000156raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000157 // Group exceptional cases into a single branch.
Daniel Dunbarb372c112009-03-17 21:15:18 +0000158 if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000159 if (Unbuffered) {
160 write_impl(Ptr, Size);
161 return *this;
162 }
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000163
Daniel Dunbar262541b2009-03-17 01:36:56 +0000164 if (!OutBufStart)
165 SetBufferSize();
166 else
167 flush_nonempty();
168 }
Owen Anderson66b17ba2008-08-21 20:58:52 +0000169
170 // Handle short strings specially, memcpy isn't very good at very short
171 // strings.
172 switch (Size) {
173 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
174 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
175 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
176 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
177 case 0: break;
178 default:
179 // Normally the string to emit is shorter than the buffer.
David Greene71847812009-07-14 20:18:05 +0000180 if (Size <= unsigned(OutBufEnd-OutBufCur)) {
Owen Anderson66b17ba2008-08-21 20:58:52 +0000181 memcpy(OutBufCur, Ptr, Size);
182 break;
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000183 }
Owen Anderson66b17ba2008-08-21 20:58:52 +0000184
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000185 // Otherwise we are emitting a string larger than our buffer. We
186 // know we already flushed, so just write it out directly.
187 write_impl(Ptr, Size);
188 Size = 0;
Owen Anderson66b17ba2008-08-21 20:58:52 +0000189 break;
190 }
191 OutBufCur += Size;
Daniel Dunbare77e4342009-03-10 16:21:55 +0000192
Owen Anderson66b17ba2008-08-21 20:58:52 +0000193 return *this;
194}
195
Chris Lattner097af7f2008-08-23 19:23:10 +0000196// Formatted output.
197raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000198 // If we have more than a few bytes left in our output buffer, try
199 // formatting directly onto its end.
200 //
201 // FIXME: This test is a bit silly, since if we don't have enough
202 // space in the buffer we will have to flush the formatted output
203 // anyway. We should just flush upfront in such cases, and use the
204 // whole buffer as our scratch pad. Note, however, that this case is
205 // also necessary for correctness on unbuffered streams.
Dan Gohmanad60f662009-07-16 15:24:40 +0000206 size_t NextBufferSize = 127;
Chris Lattner097af7f2008-08-23 19:23:10 +0000207 if (OutBufEnd-OutBufCur > 3) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000208 size_t BufferBytesLeft = OutBufEnd-OutBufCur;
209 size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
Chris Lattner097af7f2008-08-23 19:23:10 +0000210
211 // Common case is that we have plenty of space.
212 if (BytesUsed < BufferBytesLeft) {
213 OutBufCur += BytesUsed;
214 return *this;
215 }
216
217 // Otherwise, we overflowed and the return value tells us the size to try
218 // again with.
219 NextBufferSize = BytesUsed;
220 }
221
222 // If we got here, we didn't have enough space in the output buffer for the
223 // string. Try printing into a SmallVector that is resized to have enough
224 // space. Iterate until we win.
225 SmallVector<char, 128> V;
226
227 while (1) {
228 V.resize(NextBufferSize);
229
230 // Try formatting into the SmallVector.
Dan Gohmanad60f662009-07-16 15:24:40 +0000231 size_t BytesUsed = Fmt.print(&V[0], NextBufferSize);
Chris Lattner097af7f2008-08-23 19:23:10 +0000232
233 // If BytesUsed fit into the vector, we win.
Chris Lattnerf61ca1e2008-10-26 19:20:47 +0000234 if (BytesUsed <= NextBufferSize)
Chris Lattner097af7f2008-08-23 19:23:10 +0000235 return write(&V[0], BytesUsed);
236
237 // Otherwise, try again with a new size.
238 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
239 NextBufferSize = BytesUsed;
240 }
241}
242
243//===----------------------------------------------------------------------===//
244// Formatted Output
245//===----------------------------------------------------------------------===//
246
247// Out of line virtual method.
248void format_object_base::home() {
249}
250
Chris Lattner60d39622008-08-17 01:35:29 +0000251//===----------------------------------------------------------------------===//
252// raw_fd_ostream
253//===----------------------------------------------------------------------===//
254
Daniel Dunbar48534b32008-10-21 19:53:10 +0000255/// raw_fd_ostream - Open the specified file for writing. If an error
256/// occurs, information about the error is put into ErrorInfo, and the
257/// stream should be immediately destroyed; the string will be empty
258/// if no error occurred.
Dan Gohmana1bdced2009-07-15 17:29:42 +0000259raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, bool Force,
Ted Kremenekd75ba1c2008-12-04 22:51:11 +0000260 std::string &ErrorInfo) : pos(0) {
Daniel Dunbar48534b32008-10-21 19:53:10 +0000261 ErrorInfo.clear();
262
Chris Lattnerc52b1282008-08-17 03:53:23 +0000263 // Handle "-" as stdout.
264 if (Filename[0] == '-' && Filename[1] == 0) {
265 FD = STDOUT_FILENO;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000266 // If user requested binary then put stdout into binary mode if
267 // possible.
268 if (Binary)
269 sys::Program::ChangeStdoutToBinary();
Chris Lattnerc52b1282008-08-17 03:53:23 +0000270 ShouldClose = false;
271 return;
272 }
273
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000274 int Flags = O_WRONLY|O_CREAT|O_TRUNC;
275#ifdef O_BINARY
276 if (Binary)
277 Flags |= O_BINARY;
278#endif
Dan Gohmana1bdced2009-07-15 17:29:42 +0000279 if (!Force)
280 Flags |= O_EXCL;
Dan Gohman9f79d3e2009-07-15 16:39:40 +0000281 FD = open(Filename, Flags, 0664);
Chris Lattner60d39622008-08-17 01:35:29 +0000282 if (FD < 0) {
283 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
284 ShouldClose = false;
285 } else {
286 ShouldClose = true;
287 }
288}
289
290raw_fd_ostream::~raw_fd_ostream() {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000291 if (FD >= 0) {
292 flush();
293 if (ShouldClose)
Dan Gohmand3a974f2009-07-15 16:43:01 +0000294 if (::close(FD) != 0)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000295 error_detected();
Ted Kremenek43d1f022008-10-23 23:49:09 +0000296 }
Chris Lattner60d39622008-08-17 01:35:29 +0000297}
298
Dan Gohmanad60f662009-07-16 15:24:40 +0000299void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000300 assert (FD >= 0 && "File already closed.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000301 pos += Size;
Daniel Dunbar579fb872009-07-15 08:11:46 +0000302 if (::write(FD, Ptr, Size) != (ssize_t) Size)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000303 error_detected();
Chris Lattner60d39622008-08-17 01:35:29 +0000304}
305
Ted Kremenek43d1f022008-10-23 23:49:09 +0000306void raw_fd_ostream::close() {
307 assert (ShouldClose);
308 ShouldClose = false;
309 flush();
Dan Gohmand3a974f2009-07-15 16:43:01 +0000310 if (::close(FD) != 0)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000311 error_detected();
Ted Kremenek43d1f022008-10-23 23:49:09 +0000312 FD = -1;
313}
314
Ted Kremenek9c278862009-01-26 21:42:04 +0000315uint64_t raw_fd_ostream::seek(uint64_t off) {
316 flush();
Daniel Dunbar579fb872009-07-15 08:11:46 +0000317 pos = ::lseek(FD, off, SEEK_SET);
Dan Gohmand3a974f2009-07-15 16:43:01 +0000318 if (pos != off)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000319 error_detected();
Ted Kremenek9c278862009-01-26 21:42:04 +0000320 return pos;
321}
322
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000323raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
324 bool bg) {
325 if (sys::Process::ColorNeedsFlush())
326 flush();
327 const char *colorcode =
328 (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
329 : sys::Process::OutputColor(colors, bold, bg);
330 if (colorcode) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000331 size_t len = strlen(colorcode);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000332 write(colorcode, len);
333 // don't account colors towards output characters
334 pos -= len;
335 }
336 return *this;
337}
338
339raw_ostream &raw_fd_ostream::resetColor() {
340 if (sys::Process::ColorNeedsFlush())
341 flush();
342 const char *colorcode = sys::Process::ResetColor();
343 if (colorcode) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000344 size_t len = strlen(colorcode);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000345 write(colorcode, len);
346 // don't account colors towards output characters
347 pos -= len;
348 }
349 return *this;
350}
351
Chris Lattner07f51f72008-08-17 04:13:37 +0000352//===----------------------------------------------------------------------===//
353// raw_stdout/err_ostream
354//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +0000355
356raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
Daniel Dunbare77e4342009-03-10 16:21:55 +0000357raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
358 true) {}
Chris Lattner60d39622008-08-17 01:35:29 +0000359
360// An out of line virtual method to provide a home for the class vtable.
361void raw_stdout_ostream::handle() {}
362void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +0000363
364/// outs() - This returns a reference to a raw_ostream for standard output.
365/// Use it like: outs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000366raw_ostream &llvm::outs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000367 static raw_stdout_ostream S;
368 return S;
369}
370
371/// errs() - This returns a reference to a raw_ostream for standard error.
372/// Use it like: errs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000373raw_ostream &llvm::errs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000374 static raw_stderr_ostream S;
375 return S;
376}
377
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000378/// nulls() - This returns a reference to a raw_ostream which discards output.
379raw_ostream &llvm::nulls() {
380 static raw_null_ostream S;
381 return S;
382}
383
Chris Lattner07f51f72008-08-17 04:13:37 +0000384//===----------------------------------------------------------------------===//
385// raw_os_ostream
386//===----------------------------------------------------------------------===//
387
Chris Lattner944fac72008-08-23 22:23:09 +0000388raw_os_ostream::~raw_os_ostream() {
389 flush();
390}
391
Dan Gohmanad60f662009-07-16 15:24:40 +0000392void raw_os_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000393 OS.write(Ptr, Size);
Chris Lattner07f51f72008-08-17 04:13:37 +0000394}
Chris Lattner78a28122008-08-23 22:43:04 +0000395
Douglas Gregor8f7be472009-04-20 07:34:17 +0000396uint64_t raw_os_ostream::current_pos() { return OS.tellp(); }
397
398uint64_t raw_os_ostream::tell() {
399 return (uint64_t)OS.tellp() + GetNumBytesInBuffer();
400}
401
Chris Lattner78a28122008-08-23 22:43:04 +0000402//===----------------------------------------------------------------------===//
403// raw_string_ostream
404//===----------------------------------------------------------------------===//
405
406raw_string_ostream::~raw_string_ostream() {
407 flush();
408}
409
Dan Gohmanad60f662009-07-16 15:24:40 +0000410void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000411 OS.append(Ptr, Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000412}
413
414//===----------------------------------------------------------------------===//
415// raw_svector_ostream
416//===----------------------------------------------------------------------===//
417
418raw_svector_ostream::~raw_svector_ostream() {
419 flush();
420}
421
Dan Gohmanad60f662009-07-16 15:24:40 +0000422void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000423 OS.append(Ptr, Ptr + Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000424}
425
Douglas Gregor8f7be472009-04-20 07:34:17 +0000426uint64_t raw_svector_ostream::current_pos() { return OS.size(); }
427
428uint64_t raw_svector_ostream::tell() {
429 return OS.size() + GetNumBytesInBuffer();
430}
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000431
432//===----------------------------------------------------------------------===//
433// raw_null_ostream
434//===----------------------------------------------------------------------===//
435
436void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
437}
438
439uint64_t raw_null_ostream::current_pos() {
440 return 0;
441}