blob: b13d922b217724006f598bda8f9cfa6c2eeb5946 [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
47
Chris Lattner60d39622008-08-17 01:35:29 +000048// An out of line virtual method to provide a home for the class vtable.
49void raw_ostream::handle() {}
50
Owen Anderson66b17ba2008-08-21 20:58:52 +000051raw_ostream &raw_ostream::operator<<(unsigned long N) {
52 // Zero is a special case.
53 if (N == 0)
54 return *this << '0';
55
56 char NumberBuffer[20];
57 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
58 char *CurPtr = EndPtr;
59
60 while (N) {
61 *--CurPtr = '0' + char(N % 10);
62 N /= 10;
63 }
64 return write(CurPtr, EndPtr-CurPtr);
65}
66
67raw_ostream &raw_ostream::operator<<(long N) {
68 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +000069 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +000070 N = -N;
71 }
72
73 return this->operator<<(static_cast<unsigned long>(N));
74}
75
76raw_ostream &raw_ostream::operator<<(unsigned long long N) {
77 // Zero is a special case.
78 if (N == 0)
79 return *this << '0';
80
81 char NumberBuffer[20];
82 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
83 char *CurPtr = EndPtr;
84
85 while (N) {
86 *--CurPtr = '0' + char(N % 10);
87 N /= 10;
88 }
89 return write(CurPtr, EndPtr-CurPtr);
90}
91
92raw_ostream &raw_ostream::operator<<(long long N) {
93 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +000094 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +000095 N = -N;
96 }
97
98 return this->operator<<(static_cast<unsigned long long>(N));
99}
100
Chris Lattner944fac72008-08-23 22:23:09 +0000101raw_ostream &raw_ostream::operator<<(const void *P) {
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000102 uintptr_t N = (uintptr_t) P;
103 *this << '0' << 'x';
104
105 // Zero is a special case.
106 if (N == 0)
107 return *this << '0';
108
109 char NumberBuffer[20];
110 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
111 char *CurPtr = EndPtr;
112
113 while (N) {
114 unsigned x = N % 16;
115 *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10);
116 N /= 16;
117 }
118
119 return write(CurPtr, EndPtr-CurPtr);
Chris Lattner944fac72008-08-23 22:23:09 +0000120}
121
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000122void raw_ostream::flush_nonempty() {
123 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000124 write_impl(OutBufStart, OutBufCur - OutBufStart);
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000125 OutBufCur = OutBufStart;
126}
127
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000128raw_ostream &raw_ostream::write(unsigned char C) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000129 // Group exceptional cases into a single branch.
130 if (OutBufCur >= OutBufEnd) {
131 if (Unbuffered) {
132 write_impl(reinterpret_cast<char*>(&C), 1);
133 return *this;
134 }
135
136 if (!OutBufStart)
137 SetBufferSize();
138 else
139 flush_nonempty();
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000140 }
141
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000142 *OutBufCur++ = C;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000143 return *this;
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000144}
Chris Lattner944fac72008-08-23 22:23:09 +0000145
Owen Anderson66b17ba2008-08-21 20:58:52 +0000146raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000147 // Group exceptional cases into a single branch.
Daniel Dunbarb372c112009-03-17 21:15:18 +0000148 if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000149 if (Unbuffered) {
150 write_impl(Ptr, Size);
151 return *this;
152 }
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000153
Daniel Dunbar262541b2009-03-17 01:36:56 +0000154 if (!OutBufStart)
155 SetBufferSize();
156 else
157 flush_nonempty();
158 }
Owen Anderson66b17ba2008-08-21 20:58:52 +0000159
160 // Handle short strings specially, memcpy isn't very good at very short
161 // strings.
162 switch (Size) {
163 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
164 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
165 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
166 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
167 case 0: break;
168 default:
169 // Normally the string to emit is shorter than the buffer.
David Greene71847812009-07-14 20:18:05 +0000170 if (Size <= unsigned(OutBufEnd-OutBufCur)) {
Owen Anderson66b17ba2008-08-21 20:58:52 +0000171 memcpy(OutBufCur, Ptr, Size);
172 break;
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000173 }
Owen Anderson66b17ba2008-08-21 20:58:52 +0000174
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000175 // Otherwise we are emitting a string larger than our buffer. We
176 // know we already flushed, so just write it out directly.
177 write_impl(Ptr, Size);
178 Size = 0;
Owen Anderson66b17ba2008-08-21 20:58:52 +0000179 break;
180 }
181 OutBufCur += Size;
Daniel Dunbare77e4342009-03-10 16:21:55 +0000182
Owen Anderson66b17ba2008-08-21 20:58:52 +0000183 return *this;
184}
185
Chris Lattner097af7f2008-08-23 19:23:10 +0000186// Formatted output.
187raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000188 // If we have more than a few bytes left in our output buffer, try
189 // formatting directly onto its end.
190 //
191 // FIXME: This test is a bit silly, since if we don't have enough
192 // space in the buffer we will have to flush the formatted output
193 // anyway. We should just flush upfront in such cases, and use the
194 // whole buffer as our scratch pad. Note, however, that this case is
195 // also necessary for correctness on unbuffered streams.
Chris Lattner097af7f2008-08-23 19:23:10 +0000196 unsigned NextBufferSize = 127;
197 if (OutBufEnd-OutBufCur > 3) {
198 unsigned BufferBytesLeft = OutBufEnd-OutBufCur;
199 unsigned BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
200
201 // Common case is that we have plenty of space.
202 if (BytesUsed < BufferBytesLeft) {
203 OutBufCur += BytesUsed;
204 return *this;
205 }
206
207 // Otherwise, we overflowed and the return value tells us the size to try
208 // again with.
209 NextBufferSize = BytesUsed;
210 }
211
212 // If we got here, we didn't have enough space in the output buffer for the
213 // string. Try printing into a SmallVector that is resized to have enough
214 // space. Iterate until we win.
215 SmallVector<char, 128> V;
216
217 while (1) {
218 V.resize(NextBufferSize);
219
220 // Try formatting into the SmallVector.
221 unsigned BytesUsed = Fmt.print(&V[0], NextBufferSize);
222
223 // If BytesUsed fit into the vector, we win.
Chris Lattnerf61ca1e2008-10-26 19:20:47 +0000224 if (BytesUsed <= NextBufferSize)
Chris Lattner097af7f2008-08-23 19:23:10 +0000225 return write(&V[0], BytesUsed);
226
227 // Otherwise, try again with a new size.
228 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
229 NextBufferSize = BytesUsed;
230 }
231}
232
233//===----------------------------------------------------------------------===//
234// Formatted Output
235//===----------------------------------------------------------------------===//
236
237// Out of line virtual method.
238void format_object_base::home() {
239}
240
Chris Lattner60d39622008-08-17 01:35:29 +0000241//===----------------------------------------------------------------------===//
242// raw_fd_ostream
243//===----------------------------------------------------------------------===//
244
Daniel Dunbar48534b32008-10-21 19:53:10 +0000245/// raw_fd_ostream - Open the specified file for writing. If an error
246/// occurs, information about the error is put into ErrorInfo, and the
247/// stream should be immediately destroyed; the string will be empty
248/// if no error occurred.
Dan Gohmana1bdced2009-07-15 17:29:42 +0000249raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, bool Force,
Ted Kremenekd75ba1c2008-12-04 22:51:11 +0000250 std::string &ErrorInfo) : pos(0) {
Daniel Dunbar48534b32008-10-21 19:53:10 +0000251 ErrorInfo.clear();
252
Chris Lattnerc52b1282008-08-17 03:53:23 +0000253 // Handle "-" as stdout.
254 if (Filename[0] == '-' && Filename[1] == 0) {
255 FD = STDOUT_FILENO;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000256 // If user requested binary then put stdout into binary mode if
257 // possible.
258 if (Binary)
259 sys::Program::ChangeStdoutToBinary();
Chris Lattnerc52b1282008-08-17 03:53:23 +0000260 ShouldClose = false;
261 return;
262 }
263
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000264 int Flags = O_WRONLY|O_CREAT|O_TRUNC;
265#ifdef O_BINARY
266 if (Binary)
267 Flags |= O_BINARY;
268#endif
Dan Gohmana1bdced2009-07-15 17:29:42 +0000269 if (!Force)
270 Flags |= O_EXCL;
Dan Gohman9f79d3e2009-07-15 16:39:40 +0000271 FD = open(Filename, Flags, 0664);
Chris Lattner60d39622008-08-17 01:35:29 +0000272 if (FD < 0) {
273 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
274 ShouldClose = false;
275 } else {
276 ShouldClose = true;
277 }
278}
279
280raw_fd_ostream::~raw_fd_ostream() {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000281 if (FD >= 0) {
282 flush();
283 if (ShouldClose)
Dan Gohmand3a974f2009-07-15 16:43:01 +0000284 if (::close(FD) != 0)
285 llvm_report_error("IO failure closing output stream.");
Ted Kremenek43d1f022008-10-23 23:49:09 +0000286 }
Chris Lattner60d39622008-08-17 01:35:29 +0000287}
288
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000289void raw_fd_ostream::write_impl(const char *Ptr, unsigned Size) {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000290 assert (FD >= 0 && "File already closed.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000291 pos += Size;
Daniel Dunbar579fb872009-07-15 08:11:46 +0000292 if (::write(FD, Ptr, Size) != (ssize_t) Size)
293 llvm_report_error("IO failure writing to output stream.");
Chris Lattner60d39622008-08-17 01:35:29 +0000294}
295
Ted Kremenek43d1f022008-10-23 23:49:09 +0000296void raw_fd_ostream::close() {
297 assert (ShouldClose);
298 ShouldClose = false;
299 flush();
Dan Gohmand3a974f2009-07-15 16:43:01 +0000300 if (::close(FD) != 0)
301 llvm_report_error("IO failure closing output stream.");
Ted Kremenek43d1f022008-10-23 23:49:09 +0000302 FD = -1;
303}
304
Ted Kremenek9c278862009-01-26 21:42:04 +0000305uint64_t raw_fd_ostream::seek(uint64_t off) {
306 flush();
Daniel Dunbar579fb872009-07-15 08:11:46 +0000307 pos = ::lseek(FD, off, SEEK_SET);
Dan Gohmand3a974f2009-07-15 16:43:01 +0000308 if (pos != off)
309 llvm_report_error("IO failure seeking on output stream.");
Ted Kremenek9c278862009-01-26 21:42:04 +0000310 return pos;
311}
312
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000313raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
314 bool bg) {
315 if (sys::Process::ColorNeedsFlush())
316 flush();
317 const char *colorcode =
318 (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
319 : sys::Process::OutputColor(colors, bold, bg);
320 if (colorcode) {
321 unsigned len = strlen(colorcode);
322 write(colorcode, len);
323 // don't account colors towards output characters
324 pos -= len;
325 }
326 return *this;
327}
328
329raw_ostream &raw_fd_ostream::resetColor() {
330 if (sys::Process::ColorNeedsFlush())
331 flush();
332 const char *colorcode = sys::Process::ResetColor();
333 if (colorcode) {
334 unsigned len = strlen(colorcode);
335 write(colorcode, len);
336 // don't account colors towards output characters
337 pos -= len;
338 }
339 return *this;
340}
341
Chris Lattner07f51f72008-08-17 04:13:37 +0000342//===----------------------------------------------------------------------===//
343// raw_stdout/err_ostream
344//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +0000345
346raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
Daniel Dunbare77e4342009-03-10 16:21:55 +0000347raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
348 true) {}
Chris Lattner60d39622008-08-17 01:35:29 +0000349
350// An out of line virtual method to provide a home for the class vtable.
351void raw_stdout_ostream::handle() {}
352void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +0000353
354/// outs() - This returns a reference to a raw_ostream for standard output.
355/// Use it like: outs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000356raw_ostream &llvm::outs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000357 static raw_stdout_ostream S;
358 return S;
359}
360
361/// errs() - This returns a reference to a raw_ostream for standard error.
362/// Use it like: errs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000363raw_ostream &llvm::errs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000364 static raw_stderr_ostream S;
365 return S;
366}
367
368//===----------------------------------------------------------------------===//
369// raw_os_ostream
370//===----------------------------------------------------------------------===//
371
Chris Lattner944fac72008-08-23 22:23:09 +0000372raw_os_ostream::~raw_os_ostream() {
373 flush();
374}
375
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000376void raw_os_ostream::write_impl(const char *Ptr, unsigned Size) {
377 OS.write(Ptr, Size);
Chris Lattner07f51f72008-08-17 04:13:37 +0000378}
Chris Lattner78a28122008-08-23 22:43:04 +0000379
Douglas Gregor8f7be472009-04-20 07:34:17 +0000380uint64_t raw_os_ostream::current_pos() { return OS.tellp(); }
381
382uint64_t raw_os_ostream::tell() {
383 return (uint64_t)OS.tellp() + GetNumBytesInBuffer();
384}
385
Chris Lattner78a28122008-08-23 22:43:04 +0000386//===----------------------------------------------------------------------===//
387// raw_string_ostream
388//===----------------------------------------------------------------------===//
389
390raw_string_ostream::~raw_string_ostream() {
391 flush();
392}
393
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000394void raw_string_ostream::write_impl(const char *Ptr, unsigned Size) {
395 OS.append(Ptr, Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000396}
397
398//===----------------------------------------------------------------------===//
399// raw_svector_ostream
400//===----------------------------------------------------------------------===//
401
402raw_svector_ostream::~raw_svector_ostream() {
403 flush();
404}
405
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000406void raw_svector_ostream::write_impl(const char *Ptr, unsigned Size) {
407 OS.append(Ptr, Ptr + Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000408}
409
Douglas Gregor8f7be472009-04-20 07:34:17 +0000410uint64_t raw_svector_ostream::current_pos() { return OS.size(); }
411
412uint64_t raw_svector_ostream::tell() {
413 return OS.size() + GetNumBytesInBuffer();
414}