blob: ef725018ee23a73ce56e5447c0ada191e2cbeb47 [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.
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000249raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary,
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
269 FD = open(Filename, Flags, 0644);
Chris Lattner60d39622008-08-17 01:35:29 +0000270 if (FD < 0) {
271 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
272 ShouldClose = false;
273 } else {
274 ShouldClose = true;
275 }
276}
277
278raw_fd_ostream::~raw_fd_ostream() {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000279 if (FD >= 0) {
280 flush();
281 if (ShouldClose)
282 ::close(FD);
283 }
Chris Lattner60d39622008-08-17 01:35:29 +0000284}
285
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000286void raw_fd_ostream::write_impl(const char *Ptr, unsigned Size) {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000287 assert (FD >= 0 && "File already closed.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000288 pos += Size;
Daniel Dunbar579fb872009-07-15 08:11:46 +0000289 if (::write(FD, Ptr, Size) != (ssize_t) Size)
290 llvm_report_error("IO failure writing to output stream.");
Chris Lattner60d39622008-08-17 01:35:29 +0000291}
292
Ted Kremenek43d1f022008-10-23 23:49:09 +0000293void raw_fd_ostream::close() {
294 assert (ShouldClose);
295 ShouldClose = false;
296 flush();
297 ::close(FD);
298 FD = -1;
299}
300
Ted Kremenek9c278862009-01-26 21:42:04 +0000301uint64_t raw_fd_ostream::seek(uint64_t off) {
302 flush();
Daniel Dunbar579fb872009-07-15 08:11:46 +0000303 pos = ::lseek(FD, off, SEEK_SET);
Ted Kremenek9c278862009-01-26 21:42:04 +0000304 return pos;
305}
306
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000307raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
308 bool bg) {
309 if (sys::Process::ColorNeedsFlush())
310 flush();
311 const char *colorcode =
312 (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
313 : sys::Process::OutputColor(colors, bold, bg);
314 if (colorcode) {
315 unsigned len = strlen(colorcode);
316 write(colorcode, len);
317 // don't account colors towards output characters
318 pos -= len;
319 }
320 return *this;
321}
322
323raw_ostream &raw_fd_ostream::resetColor() {
324 if (sys::Process::ColorNeedsFlush())
325 flush();
326 const char *colorcode = sys::Process::ResetColor();
327 if (colorcode) {
328 unsigned len = strlen(colorcode);
329 write(colorcode, len);
330 // don't account colors towards output characters
331 pos -= len;
332 }
333 return *this;
334}
335
Chris Lattner07f51f72008-08-17 04:13:37 +0000336//===----------------------------------------------------------------------===//
337// raw_stdout/err_ostream
338//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +0000339
340raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
Daniel Dunbare77e4342009-03-10 16:21:55 +0000341raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
342 true) {}
Chris Lattner60d39622008-08-17 01:35:29 +0000343
344// An out of line virtual method to provide a home for the class vtable.
345void raw_stdout_ostream::handle() {}
346void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +0000347
348/// outs() - This returns a reference to a raw_ostream for standard output.
349/// Use it like: outs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000350raw_ostream &llvm::outs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000351 static raw_stdout_ostream S;
352 return S;
353}
354
355/// errs() - This returns a reference to a raw_ostream for standard error.
356/// Use it like: errs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000357raw_ostream &llvm::errs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000358 static raw_stderr_ostream S;
359 return S;
360}
361
362//===----------------------------------------------------------------------===//
363// raw_os_ostream
364//===----------------------------------------------------------------------===//
365
Chris Lattner944fac72008-08-23 22:23:09 +0000366raw_os_ostream::~raw_os_ostream() {
367 flush();
368}
369
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000370void raw_os_ostream::write_impl(const char *Ptr, unsigned Size) {
371 OS.write(Ptr, Size);
Chris Lattner07f51f72008-08-17 04:13:37 +0000372}
Chris Lattner78a28122008-08-23 22:43:04 +0000373
Douglas Gregor8f7be472009-04-20 07:34:17 +0000374uint64_t raw_os_ostream::current_pos() { return OS.tellp(); }
375
376uint64_t raw_os_ostream::tell() {
377 return (uint64_t)OS.tellp() + GetNumBytesInBuffer();
378}
379
Chris Lattner78a28122008-08-23 22:43:04 +0000380//===----------------------------------------------------------------------===//
381// raw_string_ostream
382//===----------------------------------------------------------------------===//
383
384raw_string_ostream::~raw_string_ostream() {
385 flush();
386}
387
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000388void raw_string_ostream::write_impl(const char *Ptr, unsigned Size) {
389 OS.append(Ptr, Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000390}
391
392//===----------------------------------------------------------------------===//
393// raw_svector_ostream
394//===----------------------------------------------------------------------===//
395
396raw_svector_ostream::~raw_svector_ostream() {
397 flush();
398}
399
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000400void raw_svector_ostream::write_impl(const char *Ptr, unsigned Size) {
401 OS.append(Ptr, Ptr + Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000402}
403
Douglas Gregor8f7be472009-04-20 07:34:17 +0000404uint64_t raw_svector_ostream::current_pos() { return OS.size(); }
405
406uint64_t raw_svector_ostream::tell() {
407 return OS.size() + GetNumBytesInBuffer();
408}