blob: 6aec9478fa202584ea851ff5ad58bafaf0bcc29a [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 Dunbarcf2a2c62009-03-16 22:55:06 +0000119void raw_ostream::flush_nonempty() {
120 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000121 write_impl(OutBufStart, OutBufCur - OutBufStart);
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000122 OutBufCur = OutBufStart;
123}
124
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000125raw_ostream &raw_ostream::write(unsigned char C) {
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000126 if (Unbuffered) {
127 write_impl(reinterpret_cast<char*>(&C), 1);
128 return *this;
129 }
130
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000131 if (!OutBufStart)
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000132 SetBufferSize();
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000133 else if (OutBufCur >= OutBufEnd)
134 flush_nonempty();
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000135
136 *OutBufCur++ = C;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000137 return *this;
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000138}
Chris Lattner944fac72008-08-23 22:23:09 +0000139
Owen Anderson66b17ba2008-08-21 20:58:52 +0000140raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) {
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000141 if (Unbuffered) {
142 write_impl(Ptr, Size);
143 return *this;
144 }
145
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000146 if (!OutBufStart)
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000147 SetBufferSize();
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000148 else if (OutBufCur+Size > OutBufEnd)
149 flush_nonempty();
Owen Anderson66b17ba2008-08-21 20:58:52 +0000150
151 // Handle short strings specially, memcpy isn't very good at very short
152 // strings.
153 switch (Size) {
154 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
155 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
156 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
157 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
158 case 0: break;
159 default:
160 // Normally the string to emit is shorter than the buffer.
161 if (Size <= unsigned(OutBufEnd-OutBufStart)) {
162 memcpy(OutBufCur, Ptr, Size);
163 break;
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000164 }
Owen Anderson66b17ba2008-08-21 20:58:52 +0000165
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000166 // Otherwise we are emitting a string larger than our buffer. We
167 // know we already flushed, so just write it out directly.
168 write_impl(Ptr, Size);
169 Size = 0;
Owen Anderson66b17ba2008-08-21 20:58:52 +0000170 break;
171 }
172 OutBufCur += Size;
Daniel Dunbare77e4342009-03-10 16:21:55 +0000173
Owen Anderson66b17ba2008-08-21 20:58:52 +0000174 return *this;
175}
176
Chris Lattner097af7f2008-08-23 19:23:10 +0000177// Formatted output.
178raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
179 // If we have more than a few bytes left in our output buffer, try formatting
180 // directly onto its end.
181 unsigned NextBufferSize = 127;
182 if (OutBufEnd-OutBufCur > 3) {
183 unsigned BufferBytesLeft = OutBufEnd-OutBufCur;
184 unsigned BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
185
186 // Common case is that we have plenty of space.
187 if (BytesUsed < BufferBytesLeft) {
188 OutBufCur += BytesUsed;
189 return *this;
190 }
191
192 // Otherwise, we overflowed and the return value tells us the size to try
193 // again with.
194 NextBufferSize = BytesUsed;
195 }
196
197 // If we got here, we didn't have enough space in the output buffer for the
198 // string. Try printing into a SmallVector that is resized to have enough
199 // space. Iterate until we win.
200 SmallVector<char, 128> V;
201
202 while (1) {
203 V.resize(NextBufferSize);
204
205 // Try formatting into the SmallVector.
206 unsigned BytesUsed = Fmt.print(&V[0], NextBufferSize);
207
208 // If BytesUsed fit into the vector, we win.
Chris Lattnerf61ca1e2008-10-26 19:20:47 +0000209 if (BytesUsed <= NextBufferSize)
Chris Lattner097af7f2008-08-23 19:23:10 +0000210 return write(&V[0], BytesUsed);
211
212 // Otherwise, try again with a new size.
213 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
214 NextBufferSize = BytesUsed;
215 }
216}
217
218//===----------------------------------------------------------------------===//
219// Formatted Output
220//===----------------------------------------------------------------------===//
221
222// Out of line virtual method.
223void format_object_base::home() {
224}
225
Chris Lattner60d39622008-08-17 01:35:29 +0000226//===----------------------------------------------------------------------===//
227// raw_fd_ostream
228//===----------------------------------------------------------------------===//
229
Daniel Dunbar48534b32008-10-21 19:53:10 +0000230/// raw_fd_ostream - Open the specified file for writing. If an error
231/// occurs, information about the error is put into ErrorInfo, and the
232/// stream should be immediately destroyed; the string will be empty
233/// if no error occurred.
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000234raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary,
Ted Kremenekd75ba1c2008-12-04 22:51:11 +0000235 std::string &ErrorInfo) : pos(0) {
Daniel Dunbar48534b32008-10-21 19:53:10 +0000236 ErrorInfo.clear();
237
Chris Lattnerc52b1282008-08-17 03:53:23 +0000238 // Handle "-" as stdout.
239 if (Filename[0] == '-' && Filename[1] == 0) {
240 FD = STDOUT_FILENO;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000241 // If user requested binary then put stdout into binary mode if
242 // possible.
243 if (Binary)
244 sys::Program::ChangeStdoutToBinary();
Chris Lattnerc52b1282008-08-17 03:53:23 +0000245 ShouldClose = false;
246 return;
247 }
248
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000249 int Flags = O_WRONLY|O_CREAT|O_TRUNC;
250#ifdef O_BINARY
251 if (Binary)
252 Flags |= O_BINARY;
253#endif
254 FD = open(Filename, Flags, 0644);
Chris Lattner60d39622008-08-17 01:35:29 +0000255 if (FD < 0) {
256 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
257 ShouldClose = false;
258 } else {
259 ShouldClose = true;
260 }
261}
262
263raw_fd_ostream::~raw_fd_ostream() {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000264 if (FD >= 0) {
265 flush();
266 if (ShouldClose)
267 ::close(FD);
268 }
Chris Lattner60d39622008-08-17 01:35:29 +0000269}
270
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000271void raw_fd_ostream::write_impl(const char *Ptr, unsigned Size) {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000272 assert (FD >= 0 && "File already closed.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000273 pos += Size;
274 ::write(FD, Ptr, Size);
Chris Lattner60d39622008-08-17 01:35:29 +0000275}
276
Ted Kremenek43d1f022008-10-23 23:49:09 +0000277void raw_fd_ostream::close() {
278 assert (ShouldClose);
279 ShouldClose = false;
280 flush();
281 ::close(FD);
282 FD = -1;
283}
284
Ted Kremenek9c278862009-01-26 21:42:04 +0000285uint64_t raw_fd_ostream::seek(uint64_t off) {
286 flush();
287 pos = lseek(FD, off, SEEK_SET);
288 return pos;
289}
290
Chris Lattner07f51f72008-08-17 04:13:37 +0000291//===----------------------------------------------------------------------===//
292// raw_stdout/err_ostream
293//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +0000294
295raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
Daniel Dunbare77e4342009-03-10 16:21:55 +0000296raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
297 true) {}
Chris Lattner60d39622008-08-17 01:35:29 +0000298
299// An out of line virtual method to provide a home for the class vtable.
300void raw_stdout_ostream::handle() {}
301void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +0000302
303/// outs() - This returns a reference to a raw_ostream for standard output.
304/// Use it like: outs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000305raw_ostream &llvm::outs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000306 static raw_stdout_ostream S;
307 return S;
308}
309
310/// errs() - This returns a reference to a raw_ostream for standard error.
311/// Use it like: errs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000312raw_ostream &llvm::errs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000313 static raw_stderr_ostream S;
314 return S;
315}
316
317//===----------------------------------------------------------------------===//
318// raw_os_ostream
319//===----------------------------------------------------------------------===//
320
Chris Lattner944fac72008-08-23 22:23:09 +0000321raw_os_ostream::~raw_os_ostream() {
322 flush();
323}
324
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000325void raw_os_ostream::write_impl(const char *Ptr, unsigned Size) {
326 OS.write(Ptr, Size);
Chris Lattner07f51f72008-08-17 04:13:37 +0000327}
Chris Lattner78a28122008-08-23 22:43:04 +0000328
329//===----------------------------------------------------------------------===//
330// raw_string_ostream
331//===----------------------------------------------------------------------===//
332
333raw_string_ostream::~raw_string_ostream() {
334 flush();
335}
336
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000337void raw_string_ostream::write_impl(const char *Ptr, unsigned Size) {
338 OS.append(Ptr, Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000339}
340
341//===----------------------------------------------------------------------===//
342// raw_svector_ostream
343//===----------------------------------------------------------------------===//
344
345raw_svector_ostream::~raw_svector_ostream() {
346 flush();
347}
348
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000349void raw_svector_ostream::write_impl(const char *Ptr, unsigned Size) {
350 OS.append(Ptr, Ptr + Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000351}
352