blob: 0c1843c457c0f4d47e3189b6b0e3f187517cc2fe [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>
Dan Gohman208ec0f2009-08-13 17:27:29 +000023#include <sys/stat.h>
24#include <sys/types.h>
Chris Lattner60d39622008-08-17 01:35:29 +000025
Chris Lattner969a46a2008-08-22 15:45:00 +000026#if defined(HAVE_UNISTD_H)
27# include <unistd.h>
28#endif
29#if defined(HAVE_FCNTL_H)
30# include <fcntl.h>
31#endif
Argyrios Kyrtzidisb68dc362008-08-17 09:25:21 +000032
33#if defined(_MSC_VER)
Chris Lattner60d39622008-08-17 01:35:29 +000034#include <io.h>
Cedric Veneta3f343f2008-08-24 11:56:40 +000035#include <fcntl.h>
Owen Andersoncb371882008-08-21 00:14:44 +000036#ifndef STDIN_FILENO
37# define STDIN_FILENO 0
38#endif
39#ifndef STDOUT_FILENO
40# define STDOUT_FILENO 1
41#endif
42#ifndef STDERR_FILENO
43# define STDERR_FILENO 2
44#endif
Chris Lattner60d39622008-08-17 01:35:29 +000045#endif
46
Chris Lattner969a46a2008-08-22 15:45:00 +000047using namespace llvm;
48
Dan Gohmane87b2ab2009-07-15 23:25:33 +000049raw_ostream::~raw_ostream() {
Dan Gohman9a312542009-07-27 20:49:44 +000050 // raw_ostream's subclasses should take care to flush the buffer
51 // in their destructors.
52 assert(OutBufCur == OutBufStart &&
53 "raw_ostream destructor called with non-empty buffer!");
54
Dan Gohmane87b2ab2009-07-15 23:25:33 +000055 delete [] OutBufStart;
56
57 // If there are any pending errors, report them now. Clients wishing
58 // to avoid llvm_report_error calls should check for errors with
59 // has_error() and clear the error flag with clear_error() before
60 // destructing raw_ostream objects which may have errors.
61 if (Error)
62 llvm_report_error("IO failure on output stream.");
63}
Chris Lattner969a46a2008-08-22 15:45:00 +000064
Chris Lattner60d39622008-08-17 01:35:29 +000065// An out of line virtual method to provide a home for the class vtable.
66void raw_ostream::handle() {}
67
Dan Gohman208ec0f2009-08-13 17:27:29 +000068size_t raw_ostream::preferred_buffer_size() {
69 // BUFSIZ is intended to be a reasonable default.
70 return BUFSIZ;
71}
72
73void raw_ostream::SetBuffered() {
74 // Ask the subclass to determine an appropriate buffer size.
Dan Gohmand7be6632009-08-15 02:05:19 +000075 if (size_t Size = preferred_buffer_size())
76 SetBufferSize(Size);
77 else
78 // It may return 0, meaning this stream should be unbuffered.
79 SetUnbuffered();
Dan Gohman208ec0f2009-08-13 17:27:29 +000080}
81
Dan Gohman524dea42009-08-13 15:58:55 +000082void raw_ostream::SetBufferSize(size_t Size) {
83 assert(Size >= 64 &&
84 "Buffer size must be somewhat large for invariants to hold");
85 flush();
86
87 delete [] OutBufStart;
88 OutBufStart = new char[Size];
89 OutBufEnd = OutBufStart+Size;
90 OutBufCur = OutBufStart;
91 Unbuffered = false;
92}
93
94void raw_ostream::SetUnbuffered() {
95 flush();
96
97 delete [] OutBufStart;
98 OutBufStart = OutBufEnd = OutBufCur = 0;
99 Unbuffered = true;
100}
101
Owen Anderson66b17ba2008-08-21 20:58:52 +0000102raw_ostream &raw_ostream::operator<<(unsigned long N) {
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 *--CurPtr = '0' + char(N % 10);
113 N /= 10;
114 }
115 return write(CurPtr, EndPtr-CurPtr);
116}
117
118raw_ostream &raw_ostream::operator<<(long N) {
119 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000120 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +0000121 N = -N;
122 }
123
124 return this->operator<<(static_cast<unsigned long>(N));
125}
126
127raw_ostream &raw_ostream::operator<<(unsigned long long N) {
Daniel Dunbar78a3dd82009-07-29 06:45:14 +0000128 // Output using 32-bit div/mod when possible.
129 if (N == static_cast<unsigned long>(N))
130 return this->operator<<(static_cast<unsigned long>(N));
131
Owen Anderson66b17ba2008-08-21 20:58:52 +0000132 char NumberBuffer[20];
133 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
134 char *CurPtr = EndPtr;
135
136 while (N) {
137 *--CurPtr = '0' + char(N % 10);
138 N /= 10;
139 }
140 return write(CurPtr, EndPtr-CurPtr);
141}
142
143raw_ostream &raw_ostream::operator<<(long long N) {
144 if (N < 0) {
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000145 *this << '-';
Owen Anderson66b17ba2008-08-21 20:58:52 +0000146 N = -N;
147 }
148
149 return this->operator<<(static_cast<unsigned long long>(N));
150}
151
Daniel Dunbar48018e02009-07-30 18:21:23 +0000152raw_ostream &raw_ostream::write_hex(unsigned long long N) {
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000153 // Zero is a special case.
154 if (N == 0)
155 return *this << '0';
156
157 char NumberBuffer[20];
158 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
159 char *CurPtr = EndPtr;
160
161 while (N) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000162 uintptr_t x = N % 16;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000163 *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10);
164 N /= 16;
165 }
166
167 return write(CurPtr, EndPtr-CurPtr);
Chris Lattner944fac72008-08-23 22:23:09 +0000168}
169
Daniel Dunbar48018e02009-07-30 18:21:23 +0000170raw_ostream &raw_ostream::operator<<(const void *P) {
171 *this << '0' << 'x';
172
173 return write_hex((uintptr_t) P);
174}
175
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000176void raw_ostream::flush_nonempty() {
177 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000178 write_impl(OutBufStart, OutBufCur - OutBufStart);
Daniel Dunbarcf2a2c62009-03-16 22:55:06 +0000179 OutBufCur = OutBufStart;
180}
181
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000182raw_ostream &raw_ostream::write(unsigned char C) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000183 // Group exceptional cases into a single branch.
184 if (OutBufCur >= OutBufEnd) {
185 if (Unbuffered) {
186 write_impl(reinterpret_cast<char*>(&C), 1);
187 return *this;
188 }
189
Dan Gohman37316042009-08-18 20:09:59 +0000190 if (OutBufStart)
Daniel Dunbar262541b2009-03-17 01:36:56 +0000191 flush_nonempty();
Dan Gohman37316042009-08-18 20:09:59 +0000192 else {
193 SetBuffered();
194 // It's possible for the underlying stream to decline
195 // buffering, so check this condition again.
196 if (Unbuffered) {
197 write_impl(reinterpret_cast<char*>(&C), 1);
198 return *this;
199 }
200 }
Daniel Dunbard17d74b2009-03-17 01:13:35 +0000201 }
202
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000203 *OutBufCur++ = C;
Daniel Dunbarb451a0c2009-03-16 22:08:44 +0000204 return *this;
Daniel Dunbarde75d7f2009-03-16 22:00:17 +0000205}
Chris Lattner944fac72008-08-23 22:23:09 +0000206
Dan Gohmanad60f662009-07-16 15:24:40 +0000207raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000208 // Group exceptional cases into a single branch.
Daniel Dunbarb372c112009-03-17 21:15:18 +0000209 if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) {
Dan Gohman33e49ef2009-08-13 15:44:52 +0000210 if (BUILTIN_EXPECT(!OutBufStart, false)) {
211 if (Unbuffered) {
212 write_impl(Ptr, Size);
213 return *this;
214 }
215 // Set up a buffer and start over.
Dan Gohman208ec0f2009-08-13 17:27:29 +0000216 SetBuffered();
Dan Gohman33e49ef2009-08-13 15:44:52 +0000217 return write(Ptr, Size);
218 }
219 // Write out the data in buffer-sized blocks until the remainder
220 // fits within the buffer.
221 do {
222 size_t NumBytes = OutBufEnd - OutBufCur;
223 copy_to_buffer(Ptr, NumBytes);
Daniel Dunbar262541b2009-03-17 01:36:56 +0000224 flush_nonempty();
Dan Gohman33e49ef2009-08-13 15:44:52 +0000225 Ptr += NumBytes;
226 Size -= NumBytes;
227 } while (OutBufCur+Size > OutBufEnd);
Daniel Dunbar262541b2009-03-17 01:36:56 +0000228 }
Dan Gohman33e49ef2009-08-13 15:44:52 +0000229
230 copy_to_buffer(Ptr, Size);
231
232 return *this;
233}
234
235void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) {
Dan Gohmanc6126e82009-08-13 20:32:03 +0000236 assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!");
Dan Gohman4e8e6422009-08-13 18:38:15 +0000237
Owen Anderson66b17ba2008-08-21 20:58:52 +0000238 // Handle short strings specially, memcpy isn't very good at very short
239 // strings.
240 switch (Size) {
241 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
242 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
243 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
244 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
245 case 0: break;
246 default:
Dan Gohman33e49ef2009-08-13 15:44:52 +0000247 memcpy(OutBufCur, Ptr, Size);
Owen Anderson66b17ba2008-08-21 20:58:52 +0000248 break;
249 }
Daniel Dunbare77e4342009-03-10 16:21:55 +0000250
Dan Gohman33e49ef2009-08-13 15:44:52 +0000251 OutBufCur += Size;
Owen Anderson66b17ba2008-08-21 20:58:52 +0000252}
253
Chris Lattner097af7f2008-08-23 19:23:10 +0000254// Formatted output.
255raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
Daniel Dunbar262541b2009-03-17 01:36:56 +0000256 // If we have more than a few bytes left in our output buffer, try
257 // formatting directly onto its end.
258 //
259 // FIXME: This test is a bit silly, since if we don't have enough
260 // space in the buffer we will have to flush the formatted output
261 // anyway. We should just flush upfront in such cases, and use the
262 // whole buffer as our scratch pad. Note, however, that this case is
263 // also necessary for correctness on unbuffered streams.
Dan Gohmanad60f662009-07-16 15:24:40 +0000264 size_t NextBufferSize = 127;
Chris Lattner097af7f2008-08-23 19:23:10 +0000265 if (OutBufEnd-OutBufCur > 3) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000266 size_t BufferBytesLeft = OutBufEnd-OutBufCur;
267 size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
Chris Lattner097af7f2008-08-23 19:23:10 +0000268
269 // Common case is that we have plenty of space.
270 if (BytesUsed < BufferBytesLeft) {
271 OutBufCur += BytesUsed;
272 return *this;
273 }
274
275 // Otherwise, we overflowed and the return value tells us the size to try
276 // again with.
277 NextBufferSize = BytesUsed;
278 }
279
280 // If we got here, we didn't have enough space in the output buffer for the
281 // string. Try printing into a SmallVector that is resized to have enough
282 // space. Iterate until we win.
283 SmallVector<char, 128> V;
284
285 while (1) {
286 V.resize(NextBufferSize);
287
288 // Try formatting into the SmallVector.
Dan Gohmanad60f662009-07-16 15:24:40 +0000289 size_t BytesUsed = Fmt.print(&V[0], NextBufferSize);
Chris Lattner097af7f2008-08-23 19:23:10 +0000290
291 // If BytesUsed fit into the vector, we win.
Chris Lattnerf61ca1e2008-10-26 19:20:47 +0000292 if (BytesUsed <= NextBufferSize)
Chris Lattner097af7f2008-08-23 19:23:10 +0000293 return write(&V[0], BytesUsed);
294
295 // Otherwise, try again with a new size.
296 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
297 NextBufferSize = BytesUsed;
298 }
299}
300
301//===----------------------------------------------------------------------===//
302// Formatted Output
303//===----------------------------------------------------------------------===//
304
305// Out of line virtual method.
306void format_object_base::home() {
307}
308
Chris Lattner60d39622008-08-17 01:35:29 +0000309//===----------------------------------------------------------------------===//
310// raw_fd_ostream
311//===----------------------------------------------------------------------===//
312
Daniel Dunbar48534b32008-10-21 19:53:10 +0000313/// raw_fd_ostream - Open the specified file for writing. If an error
314/// occurs, information about the error is put into ErrorInfo, and the
315/// stream should be immediately destroyed; the string will be empty
316/// if no error occurred.
Dan Gohmana1bdced2009-07-15 17:29:42 +0000317raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, bool Force,
Ted Kremenekd75ba1c2008-12-04 22:51:11 +0000318 std::string &ErrorInfo) : pos(0) {
Daniel Dunbar48534b32008-10-21 19:53:10 +0000319 ErrorInfo.clear();
320
Chris Lattnerc52b1282008-08-17 03:53:23 +0000321 // Handle "-" as stdout.
322 if (Filename[0] == '-' && Filename[1] == 0) {
323 FD = STDOUT_FILENO;
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000324 // If user requested binary then put stdout into binary mode if
325 // possible.
326 if (Binary)
327 sys::Program::ChangeStdoutToBinary();
Chris Lattnerc52b1282008-08-17 03:53:23 +0000328 ShouldClose = false;
329 return;
330 }
331
Daniel Dunbar0d9eb9b2008-11-13 05:01:07 +0000332 int Flags = O_WRONLY|O_CREAT|O_TRUNC;
333#ifdef O_BINARY
334 if (Binary)
335 Flags |= O_BINARY;
336#endif
Dan Gohmana1bdced2009-07-15 17:29:42 +0000337 if (!Force)
338 Flags |= O_EXCL;
Dan Gohman9f79d3e2009-07-15 16:39:40 +0000339 FD = open(Filename, Flags, 0664);
Chris Lattner60d39622008-08-17 01:35:29 +0000340 if (FD < 0) {
341 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
342 ShouldClose = false;
343 } else {
344 ShouldClose = true;
345 }
346}
347
348raw_fd_ostream::~raw_fd_ostream() {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000349 if (FD >= 0) {
350 flush();
351 if (ShouldClose)
Dan Gohmand3a974f2009-07-15 16:43:01 +0000352 if (::close(FD) != 0)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000353 error_detected();
Ted Kremenek43d1f022008-10-23 23:49:09 +0000354 }
Chris Lattner60d39622008-08-17 01:35:29 +0000355}
356
Dan Gohmanad60f662009-07-16 15:24:40 +0000357void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
Ted Kremenek43d1f022008-10-23 23:49:09 +0000358 assert (FD >= 0 && "File already closed.");
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000359 pos += Size;
Daniel Dunbar579fb872009-07-15 08:11:46 +0000360 if (::write(FD, Ptr, Size) != (ssize_t) Size)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000361 error_detected();
Chris Lattner60d39622008-08-17 01:35:29 +0000362}
363
Ted Kremenek43d1f022008-10-23 23:49:09 +0000364void raw_fd_ostream::close() {
365 assert (ShouldClose);
366 ShouldClose = false;
367 flush();
Dan Gohmand3a974f2009-07-15 16:43:01 +0000368 if (::close(FD) != 0)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000369 error_detected();
Ted Kremenek43d1f022008-10-23 23:49:09 +0000370 FD = -1;
371}
372
Ted Kremenek9c278862009-01-26 21:42:04 +0000373uint64_t raw_fd_ostream::seek(uint64_t off) {
374 flush();
Daniel Dunbar579fb872009-07-15 08:11:46 +0000375 pos = ::lseek(FD, off, SEEK_SET);
Dan Gohmand3a974f2009-07-15 16:43:01 +0000376 if (pos != off)
Dan Gohmane87b2ab2009-07-15 23:25:33 +0000377 error_detected();
Ted Kremenek9c278862009-01-26 21:42:04 +0000378 return pos;
379}
380
Dan Gohman208ec0f2009-08-13 17:27:29 +0000381size_t raw_fd_ostream::preferred_buffer_size() {
Dan Gohmanb4741a72009-08-15 21:41:03 +0000382#if !defined(_MSC_VER) && !defined(__MINGW32__) // Windows has no st_blksize.
Dan Gohman208ec0f2009-08-13 17:27:29 +0000383 assert(FD >= 0 && "File not yet open!");
384 struct stat statbuf;
Dan Gohmand7be6632009-08-15 02:05:19 +0000385 if (fstat(FD, &statbuf) == 0) {
386 // If this is a terminal, don't use buffering. Line buffering
387 // would be a more traditional thing to do, but it's not worth
388 // the complexity.
389 if (S_ISCHR(statbuf.st_mode) && isatty(FD))
390 return 0;
391 // Return the preferred block size.
Dan Gohman208ec0f2009-08-13 17:27:29 +0000392 return statbuf.st_blksize;
Dan Gohmand7be6632009-08-15 02:05:19 +0000393 }
Dan Gohman208ec0f2009-08-13 17:27:29 +0000394 error_detected();
395#endif
396 return raw_ostream::preferred_buffer_size();
397}
398
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000399raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
400 bool bg) {
401 if (sys::Process::ColorNeedsFlush())
402 flush();
403 const char *colorcode =
404 (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
405 : sys::Process::OutputColor(colors, bold, bg);
406 if (colorcode) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000407 size_t len = strlen(colorcode);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000408 write(colorcode, len);
409 // don't account colors towards output characters
410 pos -= len;
411 }
412 return *this;
413}
414
415raw_ostream &raw_fd_ostream::resetColor() {
416 if (sys::Process::ColorNeedsFlush())
417 flush();
418 const char *colorcode = sys::Process::ResetColor();
419 if (colorcode) {
Dan Gohmanad60f662009-07-16 15:24:40 +0000420 size_t len = strlen(colorcode);
Torok Edwine8ebb0f2009-06-04 07:09:50 +0000421 write(colorcode, len);
422 // don't account colors towards output characters
423 pos -= len;
424 }
425 return *this;
426}
427
Chris Lattner07f51f72008-08-17 04:13:37 +0000428//===----------------------------------------------------------------------===//
429// raw_stdout/err_ostream
430//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +0000431
Dan Gohmanec9b2612009-08-13 23:18:56 +0000432// Set buffer settings to model stdout and stderr behavior.
Dan Gohmand7be6632009-08-15 02:05:19 +0000433// Set standard error to be unbuffered by default.
434raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
435raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
Daniel Dunbare77e4342009-03-10 16:21:55 +0000436 true) {}
Chris Lattner60d39622008-08-17 01:35:29 +0000437
438// An out of line virtual method to provide a home for the class vtable.
439void raw_stdout_ostream::handle() {}
440void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +0000441
442/// outs() - This returns a reference to a raw_ostream for standard output.
443/// Use it like: outs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000444raw_ostream &llvm::outs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000445 static raw_stdout_ostream S;
446 return S;
447}
448
449/// errs() - This returns a reference to a raw_ostream for standard error.
450/// Use it like: errs() << "foo" << "bar";
Owen Andersoncb371882008-08-21 00:14:44 +0000451raw_ostream &llvm::errs() {
Chris Lattner07f51f72008-08-17 04:13:37 +0000452 static raw_stderr_ostream S;
453 return S;
454}
455
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000456/// nulls() - This returns a reference to a raw_ostream which discards output.
457raw_ostream &llvm::nulls() {
458 static raw_null_ostream S;
459 return S;
460}
461
Chris Lattner07f51f72008-08-17 04:13:37 +0000462//===----------------------------------------------------------------------===//
463// raw_os_ostream
464//===----------------------------------------------------------------------===//
465
Daniel Dunbar35979c02009-08-18 20:07:36 +0000466raw_os_ostream::~raw_os_ostream() {
467 flush();
468}
469
Dan Gohmanad60f662009-07-16 15:24:40 +0000470void raw_os_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000471 OS.write(Ptr, Size);
Chris Lattner07f51f72008-08-17 04:13:37 +0000472}
Chris Lattner78a28122008-08-23 22:43:04 +0000473
Douglas Gregor8f7be472009-04-20 07:34:17 +0000474uint64_t raw_os_ostream::current_pos() { return OS.tellp(); }
475
476uint64_t raw_os_ostream::tell() {
477 return (uint64_t)OS.tellp() + GetNumBytesInBuffer();
478}
479
Chris Lattner78a28122008-08-23 22:43:04 +0000480//===----------------------------------------------------------------------===//
481// raw_string_ostream
482//===----------------------------------------------------------------------===//
483
Daniel Dunbar35979c02009-08-18 20:07:36 +0000484raw_string_ostream::~raw_string_ostream() {
485 flush();
486}
487
Dan Gohmanad60f662009-07-16 15:24:40 +0000488void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000489 OS.append(Ptr, Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000490}
491
492//===----------------------------------------------------------------------===//
493// raw_svector_ostream
494//===----------------------------------------------------------------------===//
495
Daniel Dunbar35979c02009-08-18 20:07:36 +0000496raw_svector_ostream::~raw_svector_ostream() {
497 flush();
498}
499
Dan Gohmanad60f662009-07-16 15:24:40 +0000500void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar89a66a92009-03-16 23:29:31 +0000501 OS.append(Ptr, Ptr + Size);
Chris Lattner78a28122008-08-23 22:43:04 +0000502}
503
Douglas Gregor8f7be472009-04-20 07:34:17 +0000504uint64_t raw_svector_ostream::current_pos() { return OS.size(); }
505
506uint64_t raw_svector_ostream::tell() {
507 return OS.size() + GetNumBytesInBuffer();
508}
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000509
510//===----------------------------------------------------------------------===//
511// raw_null_ostream
512//===----------------------------------------------------------------------===//
513
Dan Gohmanf78c8352009-07-27 21:46:02 +0000514raw_null_ostream::~raw_null_ostream() {
515#ifndef NDEBUG
516 // ~raw_ostream asserts that the buffer is empty. This isn't necessary
517 // with raw_null_ostream, but it's better to have raw_null_ostream follow
518 // the rules than to change the rules just for raw_null_ostream.
519 flush();
520#endif
521}
522
Daniel Dunbar34ccf032009-07-16 21:17:53 +0000523void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
524}
525
526uint64_t raw_null_ostream::current_pos() {
527 return 0;
528}