blob: a8e6c782bc97e276a137b53b641a0a2feb2a77be [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) {
66 if (OutBufCur >= OutBufEnd)
67 flush_impl();
68 *OutBufCur++ = '-';
69
70 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) {
94 if (OutBufCur >= OutBufEnd)
95 flush_impl();
96 *OutBufCur++ = '-';
97
98 N = -N;
99 }
100
101 return this->operator<<(static_cast<unsigned long long>(N));
102}
103
Chris Lattner944fac72008-08-23 22:23:09 +0000104raw_ostream &raw_ostream::operator<<(const void *P) {
105 // FIXME: This could be much faster if it matters.
106 return *this << format("%p", P);
107}
108
109
Owen Anderson66b17ba2008-08-21 20:58:52 +0000110raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) {
111 if (OutBufCur+Size > OutBufEnd)
112 flush_impl();
113
114 // Handle short strings specially, memcpy isn't very good at very short
115 // strings.
116 switch (Size) {
117 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
118 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
119 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
120 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
121 case 0: break;
122 default:
123 // Normally the string to emit is shorter than the buffer.
124 if (Size <= unsigned(OutBufEnd-OutBufStart)) {
125 memcpy(OutBufCur, Ptr, Size);
126 break;
127 }
128
129 // If emitting a string larger than our buffer, emit in chunks. In this
130 // case we know that we just flushed the buffer.
131 while (Size) {
132 unsigned NumToEmit = OutBufEnd-OutBufStart;
133 if (Size < NumToEmit) NumToEmit = Size;
134 assert(OutBufCur == OutBufStart);
135 memcpy(OutBufStart, Ptr, NumToEmit);
136 Ptr += NumToEmit;
Owen Andersonfd2a0532008-08-21 22:39:33 +0000137 Size -= NumToEmit;
Owen Anderson66b17ba2008-08-21 20:58:52 +0000138 OutBufCur = OutBufStart + NumToEmit;
139 flush_impl();
140 }
141 break;
142 }
143 OutBufCur += Size;
144 return *this;
145}
146
Chris Lattner097af7f2008-08-23 19:23:10 +0000147// Formatted output.
148raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
149 // If we have more than a few bytes left in our output buffer, try formatting
150 // directly onto its end.
151 unsigned NextBufferSize = 127;
152 if (OutBufEnd-OutBufCur > 3) {
153 unsigned BufferBytesLeft = OutBufEnd-OutBufCur;
154 unsigned BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
155
156 // Common case is that we have plenty of space.
157 if (BytesUsed < BufferBytesLeft) {
158 OutBufCur += BytesUsed;
159 return *this;
160 }
161
162 // Otherwise, we overflowed and the return value tells us the size to try
163 // again with.
164 NextBufferSize = BytesUsed;
165 }
166
167 // If we got here, we didn't have enough space in the output buffer for the
168 // string. Try printing into a SmallVector that is resized to have enough
169 // space. Iterate until we win.
170 SmallVector<char, 128> V;
171
172 while (1) {
173 V.resize(NextBufferSize);
174
175 // Try formatting into the SmallVector.
176 unsigned BytesUsed = Fmt.print(&V[0], NextBufferSize);
177
178 // If BytesUsed fit into the vector, we win.
Chris Lattnerf61ca1e2008-10-26 19:20:47 +0000179 if (BytesUsed <= NextBufferSize)
Chris Lattner097af7f2008-08-23 19:23:10 +0000180 return write(&V[0], BytesUsed);
181
182 // Otherwise, try again with a new size.
183 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
184 NextBufferSize = BytesUsed;
185 }
186}
187
188//===----------------------------------------------------------------------===//
189// Formatted Output
190//===----------------------------------------------------------------------===//
191
192// Out of line virtual method.
193void format_object_base::home() {
194}
195
Chris Lattner60d39622008-08-17 01:35:29 +0000196//===----------------------------------------------------------------------===//
197// raw_fd_ostream
198//===----------------------------------------------------------------------===//
199
Daniel Dunbar48534b32008-10-21 19:53:10 +0000200/// raw_fd_ostream - Open the specified file for writing. If an error
201/// occurs, information about the error is put into ErrorInfo, and the
202/// stream should be immediately destroyed; the string will be empty
203/// if no error occurred.
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000204raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary,
205 std::string &ErrorInfo) {
Daniel Dunbar48534b32008-10-21 19:53:10 +0000206 ErrorInfo.clear();
207
Chris Lattnerc52b1282008-08-17 03:53:23 +0000208 // Handle "-" as stdout.
209 if (Filename[0] == '-' && Filename[1] == 0) {
210 FD = STDOUT_FILENO;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000211 // If user requested binary then put stdout into binary mode if
212 // possible.
213 if (Binary)
214 sys::Program::ChangeStdoutToBinary();
Chris Lattnerc52b1282008-08-17 03:53:23 +0000215 ShouldClose = false;
216 return;
217 }
218
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000219 int Flags = O_WRONLY|O_CREAT|O_TRUNC;
220#ifdef O_BINARY
221 if (Binary)
222 Flags |= O_BINARY;
223#endif
224 FD = open(Filename, Flags, 0644);
Chris Lattner60d39622008-08-17 01:35:29 +0000225 if (FD < 0) {
226 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
227 ShouldClose = false;
228 } else {
229 ShouldClose = true;
230 }
231}
232
233raw_fd_ostream::~raw_fd_ostream() {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000234 if (FD >= 0) {
235 flush();
236 if (ShouldClose)
237 ::close(FD);
238 }
Chris Lattner60d39622008-08-17 01:35:29 +0000239}
240
241void raw_fd_ostream::flush_impl() {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000242 assert (FD >= 0 && "File already closed.");
Chris Lattner60d39622008-08-17 01:35:29 +0000243 if (OutBufCur-OutBufStart)
Chris Lattnerd497df52008-08-17 01:46:05 +0000244 ::write(FD, OutBufStart, OutBufCur-OutBufStart);
Chris Lattner60d39622008-08-17 01:35:29 +0000245 HandleFlush();
246}
247
Ted Kremenek43d1f022008-10-23 23:49:09 +0000248void raw_fd_ostream::close() {
249 assert (ShouldClose);
250 ShouldClose = false;
251 flush();
252 ::close(FD);
253 FD = -1;
254}
255
Chris Lattner07f51f72008-08-17 04:13:37 +0000256//===----------------------------------------------------------------------===//
257// raw_stdout/err_ostream
258//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +0000259
260raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
261raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false) {}
262
263// An out of line virtual method to provide a home for the class vtable.
264void raw_stdout_ostream::handle() {}
265void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +0000266
267/// outs() - This returns a reference to a raw_ostream for standard output.
268/// Use it like: outs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000269raw_ostream &llvm::outs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000270 static raw_stdout_ostream S;
271 return S;
272}
273
274/// errs() - This returns a reference to a raw_ostream for standard error.
275/// Use it like: errs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000276raw_ostream &llvm::errs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000277 static raw_stderr_ostream S;
278 return S;
279}
280
281//===----------------------------------------------------------------------===//
282// raw_os_ostream
283//===----------------------------------------------------------------------===//
284
Chris Lattner944fac72008-08-23 22:23:09 +0000285raw_os_ostream::~raw_os_ostream() {
286 flush();
287}
288
Chris Lattner07f51f72008-08-17 04:13:37 +0000289/// flush_impl - The is the piece of the class that is implemented by
290/// subclasses. This outputs the currently buffered data and resets the
291/// buffer to empty.
292void raw_os_ostream::flush_impl() {
293 if (OutBufCur-OutBufStart)
294 OS.write(OutBufStart, OutBufCur-OutBufStart);
295 HandleFlush();
296}
Chris Lattner78a28122008-08-23 22:43:04 +0000297
298//===----------------------------------------------------------------------===//
299// raw_string_ostream
300//===----------------------------------------------------------------------===//
301
302raw_string_ostream::~raw_string_ostream() {
303 flush();
304}
305
306/// flush_impl - The is the piece of the class that is implemented by
307/// subclasses. This outputs the currently buffered data and resets the
308/// buffer to empty.
309void raw_string_ostream::flush_impl() {
310 if (OutBufCur-OutBufStart)
311 OS.append(OutBufStart, OutBufCur-OutBufStart);
312 HandleFlush();
313}
314
315//===----------------------------------------------------------------------===//
316// raw_svector_ostream
317//===----------------------------------------------------------------------===//
318
319raw_svector_ostream::~raw_svector_ostream() {
320 flush();
321}
322
323/// flush_impl - The is the piece of the class that is implemented by
324/// subclasses. This outputs the currently buffered data and resets the
325/// buffer to empty.
326void raw_svector_ostream::flush_impl() {
327 if (OutBufCur-OutBufStart)
328 OS.append(OutBufStart, OutBufCur);
329 HandleFlush();
330}
331