blob: 3c242424beb900a6a5ec8e28e7c20c4f1bd4d9c9 [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"
Chris Lattner07f51f72008-08-17 04:13:37 +000019#include <ostream>
Chris Lattner60d39622008-08-17 01:35:29 +000020
Chris Lattner969a46a2008-08-22 15:45:00 +000021#if defined(HAVE_UNISTD_H)
22# include <unistd.h>
23#endif
24#if defined(HAVE_FCNTL_H)
25# include <fcntl.h>
26#endif
Argyrios Kyrtzidisb68dc362008-08-17 09:25:21 +000027
28#if defined(_MSC_VER)
Chris Lattner60d39622008-08-17 01:35:29 +000029#include <io.h>
Cedric Veneta3f343f2008-08-24 11:56:40 +000030#include <fcntl.h>
Owen Andersoncb371882008-08-21 00:14:44 +000031#ifndef STDIN_FILENO
32# define STDIN_FILENO 0
33#endif
34#ifndef STDOUT_FILENO
35# define STDOUT_FILENO 1
36#endif
37#ifndef STDERR_FILENO
38# define STDERR_FILENO 2
39#endif
Chris Lattner60d39622008-08-17 01:35:29 +000040#endif
41
Chris Lattner969a46a2008-08-22 15:45:00 +000042using namespace llvm;
43
44
Chris Lattner60d39622008-08-17 01:35:29 +000045// An out of line virtual method to provide a home for the class vtable.
46void raw_ostream::handle() {}
47
Owen Anderson66b17ba2008-08-21 20:58:52 +000048raw_ostream &raw_ostream::operator<<(unsigned long N) {
49 // Zero is a special case.
50 if (N == 0)
51 return *this << '0';
52
53 char NumberBuffer[20];
54 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
55 char *CurPtr = EndPtr;
56
57 while (N) {
58 *--CurPtr = '0' + char(N % 10);
59 N /= 10;
60 }
61 return write(CurPtr, EndPtr-CurPtr);
62}
63
64raw_ostream &raw_ostream::operator<<(long N) {
65 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +000066 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +000067 N = -N;
68 }
69
70 return this->operator<<(static_cast<unsigned long>(N));
71}
72
73raw_ostream &raw_ostream::operator<<(unsigned long long N) {
74 // Zero is a special case.
75 if (N == 0)
76 return *this << '0';
77
78 char NumberBuffer[20];
79 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
80 char *CurPtr = EndPtr;
81
82 while (N) {
83 *--CurPtr = '0' + char(N % 10);
84 N /= 10;
85 }
86 return write(CurPtr, EndPtr-CurPtr);
87}
88
89raw_ostream &raw_ostream::operator<<(long long N) {
90 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +000091 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +000092 N = -N;
93 }
94
95 return this->operator<<(static_cast<unsigned long long>(N));
96}
97
Chris Lattner944fac72008-08-23 22:23:09 +000098raw_ostream &raw_ostream::operator<<(const void *P) {
Daniel Dunbarb451a0c2009-03-16 22:08:44 +000099 uintptr_t N = (uintptr_t) P;
100 *this << '0' << 'x';
101
102 // Zero is a special case.
103 if (N == 0)
104 return *this << '0';
105
106 char NumberBuffer[20];
107 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
108 char *CurPtr = EndPtr;
109
110 while (N) {
111 unsigned x = N % 16;
112 *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10);
113 N /= 16;
114 }
115
116 return write(CurPtr, EndPtr-CurPtr);
Chris Lattner944fac72008-08-23 22:23:09 +0000117}
118
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000119raw_ostream &raw_ostream::write(unsigned char C) {
120 if (OutBufCur >= OutBufEnd)
121 flush_impl();
122
123 *OutBufCur++ = C;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000124 return *this;
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000125}
Chris Lattner944fac72008-08-23 22:23:09 +0000126
Owen Anderson66b17ba2008-08-21 20:58:52 +0000127raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) {
128 if (OutBufCur+Size > OutBufEnd)
129 flush_impl();
130
131 // Handle short strings specially, memcpy isn't very good at very short
132 // strings.
133 switch (Size) {
134 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
135 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
136 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
137 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
138 case 0: break;
139 default:
140 // Normally the string to emit is shorter than the buffer.
141 if (Size <= unsigned(OutBufEnd-OutBufStart)) {
142 memcpy(OutBufCur, Ptr, Size);
143 break;
144 }
145
146 // If emitting a string larger than our buffer, emit in chunks. In this
147 // case we know that we just flushed the buffer.
148 while (Size) {
149 unsigned NumToEmit = OutBufEnd-OutBufStart;
150 if (Size < NumToEmit) NumToEmit = Size;
151 assert(OutBufCur == OutBufStart);
152 memcpy(OutBufStart, Ptr, NumToEmit);
153 Ptr += NumToEmit;
Owen Andersonfd2a0532008-08-21 22:39:33 +0000154 Size -= NumToEmit;
Owen Anderson66b17ba2008-08-21 20:58:52 +0000155 OutBufCur = OutBufStart + NumToEmit;
156 flush_impl();
157 }
158 break;
159 }
160 OutBufCur += Size;
Daniel Dunbare77e4342009-03-10 16:21:55 +0000161
162 if (Unbuffered)
163 flush_impl();
Owen Anderson66b17ba2008-08-21 20:58:52 +0000164 return *this;
165}
166
Chris Lattner097af7f2008-08-23 19:23:10 +0000167// Formatted output.
168raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
169 // If we have more than a few bytes left in our output buffer, try formatting
170 // directly onto its end.
171 unsigned NextBufferSize = 127;
172 if (OutBufEnd-OutBufCur > 3) {
173 unsigned BufferBytesLeft = OutBufEnd-OutBufCur;
174 unsigned BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
175
176 // Common case is that we have plenty of space.
177 if (BytesUsed < BufferBytesLeft) {
178 OutBufCur += BytesUsed;
179 return *this;
180 }
181
182 // Otherwise, we overflowed and the return value tells us the size to try
183 // again with.
184 NextBufferSize = BytesUsed;
185 }
186
187 // If we got here, we didn't have enough space in the output buffer for the
188 // string. Try printing into a SmallVector that is resized to have enough
189 // space. Iterate until we win.
190 SmallVector<char, 128> V;
191
192 while (1) {
193 V.resize(NextBufferSize);
194
195 // Try formatting into the SmallVector.
196 unsigned BytesUsed = Fmt.print(&V[0], NextBufferSize);
197
198 // If BytesUsed fit into the vector, we win.
Chris Lattnerf61ca1e2008-10-26 19:20:47 +0000199 if (BytesUsed <= NextBufferSize)
Chris Lattner097af7f2008-08-23 19:23:10 +0000200 return write(&V[0], BytesUsed);
201
202 // Otherwise, try again with a new size.
203 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
204 NextBufferSize = BytesUsed;
205 }
206}
207
208//===----------------------------------------------------------------------===//
209// Formatted Output
210//===----------------------------------------------------------------------===//
211
212// Out of line virtual method.
213void format_object_base::home() {
214}
215
Chris Lattner60d39622008-08-17 01:35:29 +0000216//===----------------------------------------------------------------------===//
217// raw_fd_ostream
218//===----------------------------------------------------------------------===//
219
Daniel Dunbar48534b32008-10-21 19:53:10 +0000220/// raw_fd_ostream - Open the specified file for writing. If an error
221/// occurs, information about the error is put into ErrorInfo, and the
222/// stream should be immediately destroyed; the string will be empty
223/// if no error occurred.
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000224raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary,
Ted Kremenekd75ba1c2008-12-04 22:51:11 +0000225 std::string &ErrorInfo) : pos(0) {
Daniel Dunbar48534b32008-10-21 19:53:10 +0000226 ErrorInfo.clear();
227
Chris Lattnerc52b1282008-08-17 03:53:23 +0000228 // Handle "-" as stdout.
229 if (Filename[0] == '-' && Filename[1] == 0) {
230 FD = STDOUT_FILENO;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000231 // If user requested binary then put stdout into binary mode if
232 // possible.
233 if (Binary)
234 sys::Program::ChangeStdoutToBinary();
Chris Lattnerc52b1282008-08-17 03:53:23 +0000235 ShouldClose = false;
236 return;
237 }
238
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000239 int Flags = O_WRONLY|O_CREAT|O_TRUNC;
240#ifdef O_BINARY
241 if (Binary)
242 Flags |= O_BINARY;
243#endif
244 FD = open(Filename, Flags, 0644);
Chris Lattner60d39622008-08-17 01:35:29 +0000245 if (FD < 0) {
246 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
247 ShouldClose = false;
248 } else {
249 ShouldClose = true;
250 }
251}
252
253raw_fd_ostream::~raw_fd_ostream() {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000254 if (FD >= 0) {
255 flush();
256 if (ShouldClose)
257 ::close(FD);
258 }
Chris Lattner60d39622008-08-17 01:35:29 +0000259}
260
261void raw_fd_ostream::flush_impl() {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000262 assert (FD >= 0 && "File already closed.");
Ted Kremenekd75ba1c2008-12-04 22:51:11 +0000263 if (OutBufCur-OutBufStart) {
264 pos += (OutBufCur - OutBufStart);
Chris Lattnerd497df52008-08-17 01:46:05 +0000265 ::write(FD, OutBufStart, OutBufCur-OutBufStart);
Ted Kremenekd75ba1c2008-12-04 22:51:11 +0000266 }
Chris Lattner60d39622008-08-17 01:35:29 +0000267 HandleFlush();
268}
269
Ted Kremenek43d1f022008-10-23 23:49:09 +0000270void raw_fd_ostream::close() {
271 assert (ShouldClose);
272 ShouldClose = false;
273 flush();
274 ::close(FD);
275 FD = -1;
276}
277
Ted Kremenek9c278862009-01-26 21:42:04 +0000278uint64_t raw_fd_ostream::seek(uint64_t off) {
279 flush();
280 pos = lseek(FD, off, SEEK_SET);
281 return pos;
282}
283
Chris Lattner07f51f72008-08-17 04:13:37 +0000284//===----------------------------------------------------------------------===//
285// raw_stdout/err_ostream
286//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +0000287
288raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
Daniel Dunbare77e4342009-03-10 16:21:55 +0000289raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
290 true) {}
Chris Lattner60d39622008-08-17 01:35:29 +0000291
292// An out of line virtual method to provide a home for the class vtable.
293void raw_stdout_ostream::handle() {}
294void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +0000295
296/// outs() - This returns a reference to a raw_ostream for standard output.
297/// Use it like: outs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000298raw_ostream &llvm::outs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000299 static raw_stdout_ostream S;
300 return S;
301}
302
303/// errs() - This returns a reference to a raw_ostream for standard error.
304/// Use it like: errs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000305raw_ostream &llvm::errs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000306 static raw_stderr_ostream S;
307 return S;
308}
309
310//===----------------------------------------------------------------------===//
311// raw_os_ostream
312//===----------------------------------------------------------------------===//
313
Chris Lattner944fac72008-08-23 22:23:09 +0000314raw_os_ostream::~raw_os_ostream() {
315 flush();
316}
317
Chris Lattner07f51f72008-08-17 04:13:37 +0000318/// flush_impl - The is the piece of the class that is implemented by
319/// subclasses. This outputs the currently buffered data and resets the
320/// buffer to empty.
321void raw_os_ostream::flush_impl() {
322 if (OutBufCur-OutBufStart)
323 OS.write(OutBufStart, OutBufCur-OutBufStart);
324 HandleFlush();
325}
Chris Lattner78a28122008-08-23 22:43:04 +0000326
327//===----------------------------------------------------------------------===//
328// raw_string_ostream
329//===----------------------------------------------------------------------===//
330
331raw_string_ostream::~raw_string_ostream() {
332 flush();
333}
334
335/// flush_impl - The is the piece of the class that is implemented by
336/// subclasses. This outputs the currently buffered data and resets the
337/// buffer to empty.
338void raw_string_ostream::flush_impl() {
339 if (OutBufCur-OutBufStart)
340 OS.append(OutBufStart, OutBufCur-OutBufStart);
341 HandleFlush();
342}
343
344//===----------------------------------------------------------------------===//
345// raw_svector_ostream
346//===----------------------------------------------------------------------===//
347
348raw_svector_ostream::~raw_svector_ostream() {
349 flush();
350}
351
352/// flush_impl - The is the piece of the class that is implemented by
353/// subclasses. This outputs the currently buffered data and resets the
354/// buffer to empty.
355void raw_svector_ostream::flush_impl() {
356 if (OutBufCur-OutBufStart)
357 OS.append(OutBufStart, OutBufCur);
358 HandleFlush();
359}
360