blob: 42e6fda97baf03807413792a7a48077fe4213058 [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"
Chris Lattner07f51f72008-08-17 04:13:37 +000021#include <ostream>
Chris Lattner60d39622008-08-17 01:35:29 +000022
Chris Lattner969a46a2008-08-22 15:45:00 +000023#if defined(HAVE_UNISTD_H)
24# include <unistd.h>
25#endif
26#if defined(HAVE_FCNTL_H)
27# include <fcntl.h>
28#endif
Argyrios Kyrtzidisb68dc362008-08-17 09:25:21 +000029
30#if defined(_MSC_VER)
Chris Lattner60d39622008-08-17 01:35:29 +000031#include <io.h>
Cedric Veneta3f343f2008-08-24 11:56:40 +000032#include <fcntl.h>
Owen Andersoncb371882008-08-21 00:14:44 +000033#ifndef STDIN_FILENO
34# define STDIN_FILENO 0
35#endif
36#ifndef STDOUT_FILENO
37# define STDOUT_FILENO 1
38#endif
39#ifndef STDERR_FILENO
40# define STDERR_FILENO 2
41#endif
Chris Lattner60d39622008-08-17 01:35:29 +000042#endif
43
Chris Lattner969a46a2008-08-22 15:45:00 +000044using namespace llvm;
45
46
Chris Lattner60d39622008-08-17 01:35:29 +000047// An out of line virtual method to provide a home for the class vtable.
48void raw_ostream::handle() {}
49
Owen Anderson66b17ba2008-08-21 20:58:52 +000050raw_ostream &raw_ostream::operator<<(unsigned long N) {
51 // Zero is a special case.
52 if (N == 0)
53 return *this << '0';
54
55 char NumberBuffer[20];
56 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
57 char *CurPtr = EndPtr;
58
59 while (N) {
60 *--CurPtr = '0' + char(N % 10);
61 N /= 10;
62 }
63 return write(CurPtr, EndPtr-CurPtr);
64}
65
66raw_ostream &raw_ostream::operator<<(long N) {
67 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +000068 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +000069 N = -N;
70 }
71
72 return this->operator<<(static_cast<unsigned long>(N));
73}
74
75raw_ostream &raw_ostream::operator<<(unsigned long long N) {
76 // Zero is a special case.
77 if (N == 0)
78 return *this << '0';
79
80 char NumberBuffer[20];
81 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
82 char *CurPtr = EndPtr;
83
84 while (N) {
85 *--CurPtr = '0' + char(N % 10);
86 N /= 10;
87 }
88 return write(CurPtr, EndPtr-CurPtr);
89}
90
91raw_ostream &raw_ostream::operator<<(long long N) {
92 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +000093 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +000094 N = -N;
95 }
96
97 return this->operator<<(static_cast<unsigned long long>(N));
98}
99
Chris Lattner944fac72008-08-23 22:23:09 +0000100raw_ostream &raw_ostream::operator<<(const void *P) {
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000101 uintptr_t N = (uintptr_t) P;
102 *this << '0' << 'x';
103
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 unsigned x = N % 16;
114 *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10);
115 N /= 16;
116 }
117
118 return write(CurPtr, EndPtr-CurPtr);
Chris Lattner944fac72008-08-23 22:23:09 +0000119}
120
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000121void raw_ostream::flush_nonempty() {
122 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000123 write_impl(OutBufStart, OutBufCur - OutBufStart);
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000124 OutBufCur = OutBufStart;
125}
126
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000127raw_ostream &raw_ostream::write(unsigned char C) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000128 // Group exceptional cases into a single branch.
129 if (OutBufCur >= OutBufEnd) {
130 if (Unbuffered) {
131 write_impl(reinterpret_cast<char*>(&C), 1);
132 return *this;
133 }
134
135 if (!OutBufStart)
136 SetBufferSize();
137 else
138 flush_nonempty();
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000139 }
140
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000141 *OutBufCur++ = C;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000142 return *this;
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000143}
Chris Lattner944fac72008-08-23 22:23:09 +0000144
Owen Anderson66b17ba2008-08-21 20:58:52 +0000145raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000146 // Group exceptional cases into a single branch.
Daniel Dunbarb372c112009-03-17 21:15:18 +0000147 if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000148 if (Unbuffered) {
149 write_impl(Ptr, Size);
150 return *this;
151 }
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000152
Daniel Dunbar262541b2009-03-17 01:36:56 +0000153 if (!OutBufStart)
154 SetBufferSize();
155 else
156 flush_nonempty();
157 }
Owen Anderson66b17ba2008-08-21 20:58:52 +0000158
159 // Handle short strings specially, memcpy isn't very good at very short
160 // strings.
161 switch (Size) {
162 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
163 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
164 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
165 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
166 case 0: break;
167 default:
168 // Normally the string to emit is shorter than the buffer.
169 if (Size <= unsigned(OutBufEnd-OutBufStart)) {
170 memcpy(OutBufCur, Ptr, Size);
171 break;
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000172 }
Owen Anderson66b17ba2008-08-21 20:58:52 +0000173
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000174 // Otherwise we are emitting a string larger than our buffer. We
175 // know we already flushed, so just write it out directly.
176 write_impl(Ptr, Size);
177 Size = 0;
Owen Anderson66b17ba2008-08-21 20:58:52 +0000178 break;
179 }
180 OutBufCur += Size;
Daniel Dunbare77e4342009-03-10 16:21:55 +0000181
Owen Anderson66b17ba2008-08-21 20:58:52 +0000182 return *this;
183}
184
Chris Lattner097af7f2008-08-23 19:23:10 +0000185// Formatted output.
186raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000187 // If we have more than a few bytes left in our output buffer, try
188 // formatting directly onto its end.
189 //
190 // FIXME: This test is a bit silly, since if we don't have enough
191 // space in the buffer we will have to flush the formatted output
192 // anyway. We should just flush upfront in such cases, and use the
193 // whole buffer as our scratch pad. Note, however, that this case is
194 // also necessary for correctness on unbuffered streams.
Chris Lattner097af7f2008-08-23 19:23:10 +0000195 unsigned NextBufferSize = 127;
196 if (OutBufEnd-OutBufCur > 3) {
197 unsigned BufferBytesLeft = OutBufEnd-OutBufCur;
198 unsigned BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
199
200 // Common case is that we have plenty of space.
201 if (BytesUsed < BufferBytesLeft) {
202 OutBufCur += BytesUsed;
203 return *this;
204 }
205
206 // Otherwise, we overflowed and the return value tells us the size to try
207 // again with.
208 NextBufferSize = BytesUsed;
209 }
210
211 // If we got here, we didn't have enough space in the output buffer for the
212 // string. Try printing into a SmallVector that is resized to have enough
213 // space. Iterate until we win.
214 SmallVector<char, 128> V;
215
216 while (1) {
217 V.resize(NextBufferSize);
218
219 // Try formatting into the SmallVector.
220 unsigned BytesUsed = Fmt.print(&V[0], NextBufferSize);
221
222 // If BytesUsed fit into the vector, we win.
Chris Lattnerf61ca1e2008-10-26 19:20:47 +0000223 if (BytesUsed <= NextBufferSize)
Chris Lattner097af7f2008-08-23 19:23:10 +0000224 return write(&V[0], BytesUsed);
225
226 // Otherwise, try again with a new size.
227 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
228 NextBufferSize = BytesUsed;
229 }
230}
231
232//===----------------------------------------------------------------------===//
233// Formatted Output
234//===----------------------------------------------------------------------===//
235
236// Out of line virtual method.
237void format_object_base::home() {
238}
239
Chris Lattner60d39622008-08-17 01:35:29 +0000240//===----------------------------------------------------------------------===//
241// raw_fd_ostream
242//===----------------------------------------------------------------------===//
243
Daniel Dunbar48534b32008-10-21 19:53:10 +0000244/// raw_fd_ostream - Open the specified file for writing. If an error
245/// occurs, information about the error is put into ErrorInfo, and the
246/// stream should be immediately destroyed; the string will be empty
247/// if no error occurred.
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000248raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary,
Ted Kremenekd75ba1c2008-12-04 22:51:11 +0000249 std::string &ErrorInfo) : pos(0) {
Daniel Dunbar48534b32008-10-21 19:53:10 +0000250 ErrorInfo.clear();
251
Chris Lattnerc52b1282008-08-17 03:53:23 +0000252 // Handle "-" as stdout.
253 if (Filename[0] == '-' && Filename[1] == 0) {
254 FD = STDOUT_FILENO;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000255 // If user requested binary then put stdout into binary mode if
256 // possible.
257 if (Binary)
258 sys::Program::ChangeStdoutToBinary();
Chris Lattnerc52b1282008-08-17 03:53:23 +0000259 ShouldClose = false;
260 return;
261 }
262
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000263 int Flags = O_WRONLY|O_CREAT|O_TRUNC;
264#ifdef O_BINARY
265 if (Binary)
266 Flags |= O_BINARY;
267#endif
268 FD = open(Filename, Flags, 0644);
Chris Lattner60d39622008-08-17 01:35:29 +0000269 if (FD < 0) {
270 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
271 ShouldClose = false;
272 } else {
273 ShouldClose = true;
274 }
275}
276
277raw_fd_ostream::~raw_fd_ostream() {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000278 if (FD >= 0) {
279 flush();
280 if (ShouldClose)
281 ::close(FD);
282 }
Chris Lattner60d39622008-08-17 01:35:29 +0000283}
284
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000285void raw_fd_ostream::write_impl(const char *Ptr, unsigned Size) {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000286 assert (FD >= 0 && "File already closed.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000287 pos += Size;
288 ::write(FD, Ptr, Size);
Chris Lattner60d39622008-08-17 01:35:29 +0000289}
290
Ted Kremenek43d1f022008-10-23 23:49:09 +0000291void raw_fd_ostream::close() {
292 assert (ShouldClose);
293 ShouldClose = false;
294 flush();
295 ::close(FD);
296 FD = -1;
297}
298
Ted Kremenek9c278862009-01-26 21:42:04 +0000299uint64_t raw_fd_ostream::seek(uint64_t off) {
300 flush();
301 pos = lseek(FD, off, SEEK_SET);
302 return pos;
303}
304
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000305raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
306 bool bg) {
307 if (sys::Process::ColorNeedsFlush())
308 flush();
309 const char *colorcode =
310 (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
311 : sys::Process::OutputColor(colors, bold, bg);
312 if (colorcode) {
313 unsigned len = strlen(colorcode);
314 write(colorcode, len);
315 // don't account colors towards output characters
316 pos -= len;
317 }
318 return *this;
319}
320
321raw_ostream &raw_fd_ostream::resetColor() {
322 if (sys::Process::ColorNeedsFlush())
323 flush();
324 const char *colorcode = sys::Process::ResetColor();
325 if (colorcode) {
326 unsigned len = strlen(colorcode);
327 write(colorcode, len);
328 // don't account colors towards output characters
329 pos -= len;
330 }
331 return *this;
332}
333
Chris Lattner07f51f72008-08-17 04:13:37 +0000334//===----------------------------------------------------------------------===//
335// raw_stdout/err_ostream
336//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +0000337
338raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
Daniel Dunbare77e4342009-03-10 16:21:55 +0000339raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
340 true) {}
Chris Lattner60d39622008-08-17 01:35:29 +0000341
342// An out of line virtual method to provide a home for the class vtable.
343void raw_stdout_ostream::handle() {}
344void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +0000345
346/// outs() - This returns a reference to a raw_ostream for standard output.
347/// Use it like: outs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000348raw_ostream &llvm::outs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000349 static raw_stdout_ostream S;
350 return S;
351}
352
353/// errs() - This returns a reference to a raw_ostream for standard error.
354/// Use it like: errs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000355raw_ostream &llvm::errs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000356 static raw_stderr_ostream S;
357 return S;
358}
359
360//===----------------------------------------------------------------------===//
361// raw_os_ostream
362//===----------------------------------------------------------------------===//
363
Chris Lattner944fac72008-08-23 22:23:09 +0000364raw_os_ostream::~raw_os_ostream() {
365 flush();
366}
367
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000368void raw_os_ostream::write_impl(const char *Ptr, unsigned Size) {
369 OS.write(Ptr, Size);
Chris Lattner07f51f72008-08-17 04:13:37 +0000370}
Chris Lattner78a28122008-08-23 22:43:04 +0000371
Douglas Gregor8f7be472009-04-20 07:34:17 +0000372uint64_t raw_os_ostream::current_pos() { return OS.tellp(); }
373
374uint64_t raw_os_ostream::tell() {
375 return (uint64_t)OS.tellp() + GetNumBytesInBuffer();
376}
377
Chris Lattner78a28122008-08-23 22:43:04 +0000378//===----------------------------------------------------------------------===//
379// raw_string_ostream
380//===----------------------------------------------------------------------===//
381
382raw_string_ostream::~raw_string_ostream() {
383 flush();
384}
385
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000386void raw_string_ostream::write_impl(const char *Ptr, unsigned Size) {
387 OS.append(Ptr, Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000388}
389
390//===----------------------------------------------------------------------===//
391// raw_svector_ostream
392//===----------------------------------------------------------------------===//
393
394raw_svector_ostream::~raw_svector_ostream() {
395 flush();
396}
397
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000398void raw_svector_ostream::write_impl(const char *Ptr, unsigned Size) {
399 OS.append(Ptr, Ptr + Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000400}
401
Douglas Gregor8f7be472009-04-20 07:34:17 +0000402uint64_t raw_svector_ostream::current_pos() { return OS.size(); }
403
404uint64_t raw_svector_ostream::tell() {
405 return OS.size() + GetNumBytesInBuffer();
406}