blob: 6ac37bc840c6a534a45d863aeccfa129ff45af0d [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"
Chris Lattner097af7f2008-08-23 19:23:10 +000017#include "llvm/ADT/SmallVector.h"
Chris Lattner969a46a2008-08-22 15:45:00 +000018#include "llvm/Config/config.h"
Daniel Dunbarb372c112009-03-17 21:15:18 +000019#include "llvm/Support/Compiler.h"
Chris Lattner07f51f72008-08-17 04:13:37 +000020#include <ostream>
Chris Lattner60d39622008-08-17 01:35:29 +000021
Chris Lattner969a46a2008-08-22 15:45:00 +000022#if defined(HAVE_UNISTD_H)
23# include <unistd.h>
24#endif
25#if defined(HAVE_FCNTL_H)
26# include <fcntl.h>
27#endif
Argyrios Kyrtzidisb68dc362008-08-17 09:25:21 +000028
29#if defined(_MSC_VER)
Chris Lattner60d39622008-08-17 01:35:29 +000030#include <io.h>
Cedric Veneta3f343f2008-08-24 11:56:40 +000031#include <fcntl.h>
Owen Andersoncb371882008-08-21 00:14:44 +000032#ifndef STDIN_FILENO
33# define STDIN_FILENO 0
34#endif
35#ifndef STDOUT_FILENO
36# define STDOUT_FILENO 1
37#endif
38#ifndef STDERR_FILENO
39# define STDERR_FILENO 2
40#endif
Chris Lattner60d39622008-08-17 01:35:29 +000041#endif
42
Chris Lattner969a46a2008-08-22 15:45:00 +000043using namespace llvm;
44
45
Chris Lattner60d39622008-08-17 01:35:29 +000046// An out of line virtual method to provide a home for the class vtable.
47void raw_ostream::handle() {}
48
Owen Anderson66b17ba2008-08-21 20:58:52 +000049raw_ostream &raw_ostream::operator<<(unsigned long N) {
50 // Zero is a special case.
51 if (N == 0)
52 return *this << '0';
53
54 char NumberBuffer[20];
55 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
56 char *CurPtr = EndPtr;
57
58 while (N) {
59 *--CurPtr = '0' + char(N % 10);
60 N /= 10;
61 }
62 return write(CurPtr, EndPtr-CurPtr);
63}
64
65raw_ostream &raw_ostream::operator<<(long N) {
66 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +000067 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +000068 N = -N;
69 }
70
71 return this->operator<<(static_cast<unsigned long>(N));
72}
73
74raw_ostream &raw_ostream::operator<<(unsigned long long N) {
75 // Zero is a special case.
76 if (N == 0)
77 return *this << '0';
78
79 char NumberBuffer[20];
80 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
81 char *CurPtr = EndPtr;
82
83 while (N) {
84 *--CurPtr = '0' + char(N % 10);
85 N /= 10;
86 }
87 return write(CurPtr, EndPtr-CurPtr);
88}
89
90raw_ostream &raw_ostream::operator<<(long long N) {
91 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +000092 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +000093 N = -N;
94 }
95
96 return this->operator<<(static_cast<unsigned long long>(N));
97}
98
Chris Lattner944fac72008-08-23 22:23:09 +000099raw_ostream &raw_ostream::operator<<(const void *P) {
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000100 uintptr_t N = (uintptr_t) P;
101 *this << '0' << 'x';
102
103 // Zero is a special case.
104 if (N == 0)
105 return *this << '0';
106
107 char NumberBuffer[20];
108 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
109 char *CurPtr = EndPtr;
110
111 while (N) {
112 unsigned x = N % 16;
113 *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10);
114 N /= 16;
115 }
116
117 return write(CurPtr, EndPtr-CurPtr);
Chris Lattner944fac72008-08-23 22:23:09 +0000118}
119
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000120void raw_ostream::flush_nonempty() {
121 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000122 write_impl(OutBufStart, OutBufCur - OutBufStart);
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000123 OutBufCur = OutBufStart;
124}
125
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000126raw_ostream &raw_ostream::write(unsigned char C) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000127 // Group exceptional cases into a single branch.
128 if (OutBufCur >= OutBufEnd) {
129 if (Unbuffered) {
130 write_impl(reinterpret_cast<char*>(&C), 1);
131 return *this;
132 }
133
134 if (!OutBufStart)
135 SetBufferSize();
136 else
137 flush_nonempty();
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000138 }
139
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000140 *OutBufCur++ = C;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000141 return *this;
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000142}
Chris Lattner944fac72008-08-23 22:23:09 +0000143
Owen Anderson66b17ba2008-08-21 20:58:52 +0000144raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000145 // Group exceptional cases into a single branch.
Daniel Dunbarb372c112009-03-17 21:15:18 +0000146 if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000147 if (Unbuffered) {
148 write_impl(Ptr, Size);
149 return *this;
150 }
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000151
Daniel Dunbar262541b2009-03-17 01:36:56 +0000152 if (!OutBufStart)
153 SetBufferSize();
154 else
155 flush_nonempty();
156 }
Owen Anderson66b17ba2008-08-21 20:58:52 +0000157
158 // Handle short strings specially, memcpy isn't very good at very short
159 // strings.
160 switch (Size) {
161 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
162 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
163 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
164 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
165 case 0: break;
166 default:
167 // Normally the string to emit is shorter than the buffer.
168 if (Size <= unsigned(OutBufEnd-OutBufStart)) {
169 memcpy(OutBufCur, Ptr, Size);
170 break;
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000171 }
Owen Anderson66b17ba2008-08-21 20:58:52 +0000172
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000173 // Otherwise we are emitting a string larger than our buffer. We
174 // know we already flushed, so just write it out directly.
175 write_impl(Ptr, Size);
176 Size = 0;
Owen Anderson66b17ba2008-08-21 20:58:52 +0000177 break;
178 }
179 OutBufCur += Size;
Daniel Dunbare77e4342009-03-10 16:21:55 +0000180
Owen Anderson66b17ba2008-08-21 20:58:52 +0000181 return *this;
182}
183
Chris Lattner097af7f2008-08-23 19:23:10 +0000184// Formatted output.
185raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000186 // If we have more than a few bytes left in our output buffer, try
187 // formatting directly onto its end.
188 //
189 // FIXME: This test is a bit silly, since if we don't have enough
190 // space in the buffer we will have to flush the formatted output
191 // anyway. We should just flush upfront in such cases, and use the
192 // whole buffer as our scratch pad. Note, however, that this case is
193 // also necessary for correctness on unbuffered streams.
Chris Lattner097af7f2008-08-23 19:23:10 +0000194 unsigned NextBufferSize = 127;
195 if (OutBufEnd-OutBufCur > 3) {
196 unsigned BufferBytesLeft = OutBufEnd-OutBufCur;
197 unsigned BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
198
199 // Common case is that we have plenty of space.
200 if (BytesUsed < BufferBytesLeft) {
201 OutBufCur += BytesUsed;
202 return *this;
203 }
204
205 // Otherwise, we overflowed and the return value tells us the size to try
206 // again with.
207 NextBufferSize = BytesUsed;
208 }
209
210 // If we got here, we didn't have enough space in the output buffer for the
211 // string. Try printing into a SmallVector that is resized to have enough
212 // space. Iterate until we win.
213 SmallVector<char, 128> V;
214
215 while (1) {
216 V.resize(NextBufferSize);
217
218 // Try formatting into the SmallVector.
219 unsigned BytesUsed = Fmt.print(&V[0], NextBufferSize);
220
221 // If BytesUsed fit into the vector, we win.
Chris Lattnerf61ca1e2008-10-26 19:20:47 +0000222 if (BytesUsed <= NextBufferSize)
Chris Lattner097af7f2008-08-23 19:23:10 +0000223 return write(&V[0], BytesUsed);
224
225 // Otherwise, try again with a new size.
226 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
227 NextBufferSize = BytesUsed;
228 }
229}
230
231//===----------------------------------------------------------------------===//
232// Formatted Output
233//===----------------------------------------------------------------------===//
234
235// Out of line virtual method.
236void format_object_base::home() {
237}
238
Chris Lattner60d39622008-08-17 01:35:29 +0000239//===----------------------------------------------------------------------===//
240// raw_fd_ostream
241//===----------------------------------------------------------------------===//
242
Daniel Dunbar48534b32008-10-21 19:53:10 +0000243/// raw_fd_ostream - Open the specified file for writing. If an error
244/// occurs, information about the error is put into ErrorInfo, and the
245/// stream should be immediately destroyed; the string will be empty
246/// if no error occurred.
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000247raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary,
Ted Kremenekd75ba1c2008-12-04 22:51:11 +0000248 std::string &ErrorInfo) : pos(0) {
Daniel Dunbar48534b32008-10-21 19:53:10 +0000249 ErrorInfo.clear();
250
Chris Lattnerc52b1282008-08-17 03:53:23 +0000251 // Handle "-" as stdout.
252 if (Filename[0] == '-' && Filename[1] == 0) {
253 FD = STDOUT_FILENO;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000254 // If user requested binary then put stdout into binary mode if
255 // possible.
256 if (Binary)
257 sys::Program::ChangeStdoutToBinary();
Chris Lattnerc52b1282008-08-17 03:53:23 +0000258 ShouldClose = false;
259 return;
260 }
261
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000262 int Flags = O_WRONLY|O_CREAT|O_TRUNC;
263#ifdef O_BINARY
264 if (Binary)
265 Flags |= O_BINARY;
266#endif
267 FD = open(Filename, Flags, 0644);
Chris Lattner60d39622008-08-17 01:35:29 +0000268 if (FD < 0) {
269 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
270 ShouldClose = false;
271 } else {
272 ShouldClose = true;
273 }
274}
275
276raw_fd_ostream::~raw_fd_ostream() {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000277 if (FD >= 0) {
278 flush();
279 if (ShouldClose)
280 ::close(FD);
281 }
Chris Lattner60d39622008-08-17 01:35:29 +0000282}
283
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000284void raw_fd_ostream::write_impl(const char *Ptr, unsigned Size) {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000285 assert (FD >= 0 && "File already closed.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000286 pos += Size;
287 ::write(FD, Ptr, Size);
Chris Lattner60d39622008-08-17 01:35:29 +0000288}
289
Ted Kremenek43d1f022008-10-23 23:49:09 +0000290void raw_fd_ostream::close() {
291 assert (ShouldClose);
292 ShouldClose = false;
293 flush();
294 ::close(FD);
295 FD = -1;
296}
297
Ted Kremenek9c278862009-01-26 21:42:04 +0000298uint64_t raw_fd_ostream::seek(uint64_t off) {
299 flush();
300 pos = lseek(FD, off, SEEK_SET);
301 return pos;
302}
303
Chris Lattner07f51f72008-08-17 04:13:37 +0000304//===----------------------------------------------------------------------===//
305// raw_stdout/err_ostream
306//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +0000307
308raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
Daniel Dunbare77e4342009-03-10 16:21:55 +0000309raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
310 true) {}
Chris Lattner60d39622008-08-17 01:35:29 +0000311
312// An out of line virtual method to provide a home for the class vtable.
313void raw_stdout_ostream::handle() {}
314void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +0000315
316/// outs() - This returns a reference to a raw_ostream for standard output.
317/// Use it like: outs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000318raw_ostream &llvm::outs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000319 static raw_stdout_ostream S;
320 return S;
321}
322
323/// errs() - This returns a reference to a raw_ostream for standard error.
324/// Use it like: errs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000325raw_ostream &llvm::errs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000326 static raw_stderr_ostream S;
327 return S;
328}
329
330//===----------------------------------------------------------------------===//
331// raw_os_ostream
332//===----------------------------------------------------------------------===//
333
Chris Lattner944fac72008-08-23 22:23:09 +0000334raw_os_ostream::~raw_os_ostream() {
335 flush();
336}
337
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000338void raw_os_ostream::write_impl(const char *Ptr, unsigned Size) {
339 OS.write(Ptr, Size);
Chris Lattner07f51f72008-08-17 04:13:37 +0000340}
Chris Lattner78a28122008-08-23 22:43:04 +0000341
Douglas Gregor8f7be472009-04-20 07:34:17 +0000342uint64_t raw_os_ostream::current_pos() { return OS.tellp(); }
343
344uint64_t raw_os_ostream::tell() {
345 return (uint64_t)OS.tellp() + GetNumBytesInBuffer();
346}
347
Chris Lattner78a28122008-08-23 22:43:04 +0000348//===----------------------------------------------------------------------===//
349// raw_string_ostream
350//===----------------------------------------------------------------------===//
351
352raw_string_ostream::~raw_string_ostream() {
353 flush();
354}
355
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000356void raw_string_ostream::write_impl(const char *Ptr, unsigned Size) {
357 OS.append(Ptr, Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000358}
359
360//===----------------------------------------------------------------------===//
361// raw_svector_ostream
362//===----------------------------------------------------------------------===//
363
364raw_svector_ostream::~raw_svector_ostream() {
365 flush();
366}
367
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000368void raw_svector_ostream::write_impl(const char *Ptr, unsigned Size) {
369 OS.append(Ptr, Ptr + Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000370}
371
Douglas Gregor8f7be472009-04-20 07:34:17 +0000372uint64_t raw_svector_ostream::current_pos() { return OS.size(); }
373
374uint64_t raw_svector_ostream::tell() {
375 return OS.size() + GetNumBytesInBuffer();
376}