blob: 20e50efd2cc69b729fbefe02f1fde1f414e4008e [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() {
Dan Gohman9a312542009-07-27 20:49:44 +000048 // raw_ostream's subclasses should take care to flush the buffer
49 // in their destructors.
50 assert(OutBufCur == OutBufStart &&
51 "raw_ostream destructor called with non-empty buffer!");
52
Dan Gohmane87b2ab2009-07-15 23:25:33 +000053 delete [] OutBufStart;
54
55 // If there are any pending errors, report them now. Clients wishing
56 // to avoid llvm_report_error calls should check for errors with
57 // has_error() and clear the error flag with clear_error() before
58 // destructing raw_ostream objects which may have errors.
59 if (Error)
60 llvm_report_error("IO failure on output stream.");
61}
Chris Lattner969a46a2008-08-22 15:45:00 +000062
Chris Lattner60d39622008-08-17 01:35:29 +000063// An out of line virtual method to provide a home for the class vtable.
64void raw_ostream::handle() {}
65
Owen Anderson66b17ba2008-08-21 20:58:52 +000066raw_ostream &raw_ostream::operator<<(unsigned long N) {
67 // Zero is a special case.
68 if (N == 0)
69 return *this << '0';
70
71 char NumberBuffer[20];
72 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
73 char *CurPtr = EndPtr;
74
75 while (N) {
76 *--CurPtr = '0' + char(N % 10);
77 N /= 10;
78 }
79 return write(CurPtr, EndPtr-CurPtr);
80}
81
82raw_ostream &raw_ostream::operator<<(long N) {
83 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +000084 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +000085 N = -N;
86 }
87
88 return this->operator<<(static_cast<unsigned long>(N));
89}
90
91raw_ostream &raw_ostream::operator<<(unsigned long long N) {
Daniel Dunbar78a3dd82009-07-29 06:45:14 +000092 // Output using 32-bit div/mod when possible.
93 if (N == static_cast<unsigned long>(N))
94 return this->operator<<(static_cast<unsigned long>(N));
95
Owen Anderson66b17ba2008-08-21 20:58:52 +000096 char NumberBuffer[20];
97 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
98 char *CurPtr = EndPtr;
99
100 while (N) {
101 *--CurPtr = '0' + char(N % 10);
102 N /= 10;
103 }
104 return write(CurPtr, EndPtr-CurPtr);
105}
106
107raw_ostream &raw_ostream::operator<<(long long N) {
108 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000109 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +0000110 N = -N;
111 }
112
113 return this->operator<<(static_cast<unsigned long long>(N));
114}
115
Chris Lattner944fac72008-08-23 22:23:09 +0000116raw_ostream &raw_ostream::operator<<(const void *P) {
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000117 uintptr_t N = (uintptr_t) P;
118 *this << '0' << 'x';
119
120 // Zero is a special case.
121 if (N == 0)
122 return *this << '0';
123
124 char NumberBuffer[20];
125 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
126 char *CurPtr = EndPtr;
127
128 while (N) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000129 uintptr_t x = N % 16;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000130 *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10);
131 N /= 16;
132 }
133
134 return write(CurPtr, EndPtr-CurPtr);
Chris Lattner944fac72008-08-23 22:23:09 +0000135}
136
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000137void raw_ostream::flush_nonempty() {
138 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000139 write_impl(OutBufStart, OutBufCur - OutBufStart);
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000140 OutBufCur = OutBufStart;
141}
142
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000143raw_ostream &raw_ostream::write(unsigned char C) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000144 // Group exceptional cases into a single branch.
145 if (OutBufCur >= OutBufEnd) {
146 if (Unbuffered) {
147 write_impl(reinterpret_cast<char*>(&C), 1);
148 return *this;
149 }
150
151 if (!OutBufStart)
152 SetBufferSize();
153 else
154 flush_nonempty();
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000155 }
156
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000157 *OutBufCur++ = C;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000158 return *this;
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000159}
Chris Lattner944fac72008-08-23 22:23:09 +0000160
Dan Gohmanad60f662009-07-16 15:24:40 +0000161raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000162 // Group exceptional cases into a single branch.
Daniel Dunbarb372c112009-03-17 21:15:18 +0000163 if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000164 if (Unbuffered) {
165 write_impl(Ptr, Size);
166 return *this;
167 }
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000168
Daniel Dunbar262541b2009-03-17 01:36:56 +0000169 if (!OutBufStart)
170 SetBufferSize();
171 else
172 flush_nonempty();
173 }
Owen Anderson66b17ba2008-08-21 20:58:52 +0000174
175 // Handle short strings specially, memcpy isn't very good at very short
176 // strings.
177 switch (Size) {
178 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
179 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
180 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
181 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
182 case 0: break;
183 default:
184 // Normally the string to emit is shorter than the buffer.
David Greene71847812009-07-14 20:18:05 +0000185 if (Size <= unsigned(OutBufEnd-OutBufCur)) {
Owen Anderson66b17ba2008-08-21 20:58:52 +0000186 memcpy(OutBufCur, Ptr, Size);
187 break;
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000188 }
Owen Anderson66b17ba2008-08-21 20:58:52 +0000189
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000190 // Otherwise we are emitting a string larger than our buffer. We
191 // know we already flushed, so just write it out directly.
192 write_impl(Ptr, Size);
193 Size = 0;
Owen Anderson66b17ba2008-08-21 20:58:52 +0000194 break;
195 }
196 OutBufCur += Size;
Daniel Dunbare77e4342009-03-10 16:21:55 +0000197
Owen Anderson66b17ba2008-08-21 20:58:52 +0000198 return *this;
199}
200
Chris Lattner097af7f2008-08-23 19:23:10 +0000201// Formatted output.
202raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000203 // If we have more than a few bytes left in our output buffer, try
204 // formatting directly onto its end.
205 //
206 // FIXME: This test is a bit silly, since if we don't have enough
207 // space in the buffer we will have to flush the formatted output
208 // anyway. We should just flush upfront in such cases, and use the
209 // whole buffer as our scratch pad. Note, however, that this case is
210 // also necessary for correctness on unbuffered streams.
Dan Gohmanad60f662009-07-16 15:24:40 +0000211 size_t NextBufferSize = 127;
Chris Lattner097af7f2008-08-23 19:23:10 +0000212 if (OutBufEnd-OutBufCur > 3) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000213 size_t BufferBytesLeft = OutBufEnd-OutBufCur;
214 size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
Chris Lattner097af7f2008-08-23 19:23:10 +0000215
216 // Common case is that we have plenty of space.
217 if (BytesUsed < BufferBytesLeft) {
218 OutBufCur += BytesUsed;
219 return *this;
220 }
221
222 // Otherwise, we overflowed and the return value tells us the size to try
223 // again with.
224 NextBufferSize = BytesUsed;
225 }
226
227 // If we got here, we didn't have enough space in the output buffer for the
228 // string. Try printing into a SmallVector that is resized to have enough
229 // space. Iterate until we win.
230 SmallVector<char, 128> V;
231
232 while (1) {
233 V.resize(NextBufferSize);
234
235 // Try formatting into the SmallVector.
Dan Gohmanad60f662009-07-16 15:24:40 +0000236 size_t BytesUsed = Fmt.print(&V[0], NextBufferSize);
Chris Lattner097af7f2008-08-23 19:23:10 +0000237
238 // If BytesUsed fit into the vector, we win.
Chris Lattnerf61ca1e2008-10-26 19:20:47 +0000239 if (BytesUsed <= NextBufferSize)
Chris Lattner097af7f2008-08-23 19:23:10 +0000240 return write(&V[0], BytesUsed);
241
242 // Otherwise, try again with a new size.
243 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
244 NextBufferSize = BytesUsed;
245 }
246}
247
248//===----------------------------------------------------------------------===//
249// Formatted Output
250//===----------------------------------------------------------------------===//
251
252// Out of line virtual method.
253void format_object_base::home() {
254}
255
Chris Lattner60d39622008-08-17 01:35:29 +0000256//===----------------------------------------------------------------------===//
257// raw_fd_ostream
258//===----------------------------------------------------------------------===//
259
Daniel Dunbar48534b32008-10-21 19:53:10 +0000260/// raw_fd_ostream - Open the specified file for writing. If an error
261/// occurs, information about the error is put into ErrorInfo, and the
262/// stream should be immediately destroyed; the string will be empty
263/// if no error occurred.
Dan Gohmana1bdced2009-07-15 17:29:42 +0000264raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, bool Force,
Ted Kremenekd75ba1c2008-12-04 22:51:11 +0000265 std::string &ErrorInfo) : pos(0) {
Daniel Dunbar48534b32008-10-21 19:53:10 +0000266 ErrorInfo.clear();
267
Chris Lattnerc52b1282008-08-17 03:53:23 +0000268 // Handle "-" as stdout.
269 if (Filename[0] == '-' && Filename[1] == 0) {
270 FD = STDOUT_FILENO;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000271 // If user requested binary then put stdout into binary mode if
272 // possible.
273 if (Binary)
274 sys::Program::ChangeStdoutToBinary();
Chris Lattnerc52b1282008-08-17 03:53:23 +0000275 ShouldClose = false;
276 return;
277 }
278
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000279 int Flags = O_WRONLY|O_CREAT|O_TRUNC;
280#ifdef O_BINARY
281 if (Binary)
282 Flags |= O_BINARY;
283#endif
Dan Gohmana1bdced2009-07-15 17:29:42 +0000284 if (!Force)
285 Flags |= O_EXCL;
Dan Gohman9f79d3e2009-07-15 16:39:40 +0000286 FD = open(Filename, Flags, 0664);
Chris Lattner60d39622008-08-17 01:35:29 +0000287 if (FD < 0) {
288 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
289 ShouldClose = false;
290 } else {
291 ShouldClose = true;
292 }
293}
294
295raw_fd_ostream::~raw_fd_ostream() {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000296 if (FD >= 0) {
297 flush();
298 if (ShouldClose)
Dan Gohmand3a974f2009-07-15 16:43:01 +0000299 if (::close(FD) != 0)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000300 error_detected();
Ted Kremenek43d1f022008-10-23 23:49:09 +0000301 }
Chris Lattner60d39622008-08-17 01:35:29 +0000302}
303
Dan Gohmanad60f662009-07-16 15:24:40 +0000304void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000305 assert (FD >= 0 && "File already closed.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000306 pos += Size;
Daniel Dunbar579fb872009-07-15 08:11:46 +0000307 if (::write(FD, Ptr, Size) != (ssize_t) Size)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000308 error_detected();
Chris Lattner60d39622008-08-17 01:35:29 +0000309}
310
Ted Kremenek43d1f022008-10-23 23:49:09 +0000311void raw_fd_ostream::close() {
312 assert (ShouldClose);
313 ShouldClose = false;
314 flush();
Dan Gohmand3a974f2009-07-15 16:43:01 +0000315 if (::close(FD) != 0)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000316 error_detected();
Ted Kremenek43d1f022008-10-23 23:49:09 +0000317 FD = -1;
318}
319
Ted Kremenek9c278862009-01-26 21:42:04 +0000320uint64_t raw_fd_ostream::seek(uint64_t off) {
321 flush();
Daniel Dunbar579fb872009-07-15 08:11:46 +0000322 pos = ::lseek(FD, off, SEEK_SET);
Dan Gohmand3a974f2009-07-15 16:43:01 +0000323 if (pos != off)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000324 error_detected();
Ted Kremenek9c278862009-01-26 21:42:04 +0000325 return pos;
326}
327
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000328raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
329 bool bg) {
330 if (sys::Process::ColorNeedsFlush())
331 flush();
332 const char *colorcode =
333 (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
334 : sys::Process::OutputColor(colors, bold, bg);
335 if (colorcode) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000336 size_t len = strlen(colorcode);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000337 write(colorcode, len);
338 // don't account colors towards output characters
339 pos -= len;
340 }
341 return *this;
342}
343
344raw_ostream &raw_fd_ostream::resetColor() {
345 if (sys::Process::ColorNeedsFlush())
346 flush();
347 const char *colorcode = sys::Process::ResetColor();
348 if (colorcode) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000349 size_t len = strlen(colorcode);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000350 write(colorcode, len);
351 // don't account colors towards output characters
352 pos -= len;
353 }
354 return *this;
355}
356
Chris Lattner07f51f72008-08-17 04:13:37 +0000357//===----------------------------------------------------------------------===//
358// raw_stdout/err_ostream
359//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +0000360
361raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
Daniel Dunbare77e4342009-03-10 16:21:55 +0000362raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
363 true) {}
Chris Lattner60d39622008-08-17 01:35:29 +0000364
365// An out of line virtual method to provide a home for the class vtable.
366void raw_stdout_ostream::handle() {}
367void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +0000368
369/// outs() - This returns a reference to a raw_ostream for standard output.
370/// Use it like: outs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000371raw_ostream &llvm::outs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000372 static raw_stdout_ostream S;
373 return S;
374}
375
376/// errs() - This returns a reference to a raw_ostream for standard error.
377/// Use it like: errs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000378raw_ostream &llvm::errs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000379 static raw_stderr_ostream S;
380 return S;
381}
382
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000383/// nulls() - This returns a reference to a raw_ostream which discards output.
384raw_ostream &llvm::nulls() {
385 static raw_null_ostream S;
386 return S;
387}
388
Chris Lattner07f51f72008-08-17 04:13:37 +0000389//===----------------------------------------------------------------------===//
390// raw_os_ostream
391//===----------------------------------------------------------------------===//
392
Chris Lattner944fac72008-08-23 22:23:09 +0000393raw_os_ostream::~raw_os_ostream() {
394 flush();
395}
396
Dan Gohmanad60f662009-07-16 15:24:40 +0000397void raw_os_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000398 OS.write(Ptr, Size);
Chris Lattner07f51f72008-08-17 04:13:37 +0000399}
Chris Lattner78a28122008-08-23 22:43:04 +0000400
Douglas Gregor8f7be472009-04-20 07:34:17 +0000401uint64_t raw_os_ostream::current_pos() { return OS.tellp(); }
402
403uint64_t raw_os_ostream::tell() {
404 return (uint64_t)OS.tellp() + GetNumBytesInBuffer();
405}
406
Chris Lattner78a28122008-08-23 22:43:04 +0000407//===----------------------------------------------------------------------===//
408// raw_string_ostream
409//===----------------------------------------------------------------------===//
410
411raw_string_ostream::~raw_string_ostream() {
412 flush();
413}
414
Dan Gohmanad60f662009-07-16 15:24:40 +0000415void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000416 OS.append(Ptr, Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000417}
418
419//===----------------------------------------------------------------------===//
420// raw_svector_ostream
421//===----------------------------------------------------------------------===//
422
423raw_svector_ostream::~raw_svector_ostream() {
424 flush();
425}
426
Dan Gohmanad60f662009-07-16 15:24:40 +0000427void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000428 OS.append(Ptr, Ptr + Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000429}
430
Douglas Gregor8f7be472009-04-20 07:34:17 +0000431uint64_t raw_svector_ostream::current_pos() { return OS.size(); }
432
433uint64_t raw_svector_ostream::tell() {
434 return OS.size() + GetNumBytesInBuffer();
435}
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000436
437//===----------------------------------------------------------------------===//
438// raw_null_ostream
439//===----------------------------------------------------------------------===//
440
Dan Gohmanf78c8352009-07-27 21:46:02 +0000441raw_null_ostream::~raw_null_ostream() {
442#ifndef NDEBUG
443 // ~raw_ostream asserts that the buffer is empty. This isn't necessary
444 // with raw_null_ostream, but it's better to have raw_null_ostream follow
445 // the rules than to change the rules just for raw_null_ostream.
446 flush();
447#endif
448}
449
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000450void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
451}
452
453uint64_t raw_null_ostream::current_pos() {
454 return 0;
455}