blob: a91fe9b5364cb4d35b1a71f78ddcf8e8bf8b9395 [file] [log] [blame]
Chris Lattner18283402008-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 Lattner62ccbc72008-08-23 20:34:06 +000015#include "llvm/Support/Format.h"
Daniel Dunbar8c009a32008-11-13 05:01:07 +000016#include "llvm/System/Program.h"
Edwin Török8a06a5b2009-06-04 07:09:50 +000017#include "llvm/System/Process.h"
Chris Lattnerf9402fe2008-08-23 19:23:10 +000018#include "llvm/ADT/SmallVector.h"
Chris Lattner13456752008-08-22 15:45:00 +000019#include "llvm/Config/config.h"
Daniel Dunbard6976332009-03-17 21:15:18 +000020#include "llvm/Support/Compiler.h"
Daniel Dunbara3230b72009-07-15 08:11:46 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattner050c9612008-08-17 04:13:37 +000022#include <ostream>
Dan Gohman6bd7ece2009-08-13 17:27:29 +000023#include <sys/stat.h>
24#include <sys/types.h>
Chris Lattner18283402008-08-17 01:35:29 +000025
Chris Lattner13456752008-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
Argiris Kirtzidis8519c432008-08-17 09:25:21 +000032
33#if defined(_MSC_VER)
Chris Lattner18283402008-08-17 01:35:29 +000034#include <io.h>
Cédric Venet5e0a3582008-08-24 11:56:40 +000035#include <fcntl.h>
Owen Anderson847b99b2008-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 Lattner18283402008-08-17 01:35:29 +000045#endif
46
Chris Lattner13456752008-08-22 15:45:00 +000047using namespace llvm;
48
Dan Gohmanfea42602009-07-15 23:25:33 +000049raw_ostream::~raw_ostream() {
Dan Gohmanab7f0752009-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 Gohmanfea42602009-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 Lattner13456752008-08-22 15:45:00 +000064
Chris Lattner18283402008-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 Gohman6bd7ece2009-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 Gohman314e3a62009-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 Gohman6bd7ece2009-08-13 17:27:29 +000080}
81
Dan Gohman48916522009-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 Anderson1046f622008-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 Dunbar5cd6d452009-03-16 22:00:17 +0000120 *this << '-';
Owen Anderson1046f622008-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 Dunbar2ece02f2009-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 Anderson1046f622008-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 Dunbar5cd6d452009-03-16 22:00:17 +0000145 *this << '-';
Owen Anderson1046f622008-08-21 20:58:52 +0000146 N = -N;
147 }
148
149 return this->operator<<(static_cast<unsigned long long>(N));
150}
151
Daniel Dunbarea9b9d32009-07-30 18:21:23 +0000152raw_ostream &raw_ostream::write_hex(unsigned long long N) {
Daniel Dunbar3b909272009-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 Gohman75ffec02009-07-16 15:24:40 +0000162 uintptr_t x = N % 16;
Daniel Dunbar3b909272009-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 Lattner1fefaac2008-08-23 22:23:09 +0000168}
169
Daniel Dunbarea9b9d32009-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 Dunbar0a98b5f2009-03-16 22:55:06 +0000176void raw_ostream::flush_nonempty() {
177 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
Daniel Dunbar308eff32009-03-16 23:29:31 +0000178 write_impl(OutBufStart, OutBufCur - OutBufStart);
Daniel Dunbar0a98b5f2009-03-16 22:55:06 +0000179 OutBufCur = OutBufStart;
180}
181
Daniel Dunbar5cd6d452009-03-16 22:00:17 +0000182raw_ostream &raw_ostream::write(unsigned char C) {
Daniel Dunbar6c4570e2009-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
190 if (!OutBufStart)
Dan Gohman6bd7ece2009-08-13 17:27:29 +0000191 SetBuffered();
Daniel Dunbar6c4570e2009-03-17 01:36:56 +0000192 else
193 flush_nonempty();
Daniel Dunbarc1730c22009-03-17 01:13:35 +0000194 }
195
Daniel Dunbar5cd6d452009-03-16 22:00:17 +0000196 *OutBufCur++ = C;
Daniel Dunbar3b909272009-03-16 22:08:44 +0000197 return *this;
Daniel Dunbar5cd6d452009-03-16 22:00:17 +0000198}
Chris Lattner1fefaac2008-08-23 22:23:09 +0000199
Dan Gohman75ffec02009-07-16 15:24:40 +0000200raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
Daniel Dunbar6c4570e2009-03-17 01:36:56 +0000201 // Group exceptional cases into a single branch.
Daniel Dunbard6976332009-03-17 21:15:18 +0000202 if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) {
Dan Gohman072828e2009-08-13 15:44:52 +0000203 if (BUILTIN_EXPECT(!OutBufStart, false)) {
204 if (Unbuffered) {
205 write_impl(Ptr, Size);
206 return *this;
207 }
208 // Set up a buffer and start over.
Dan Gohman6bd7ece2009-08-13 17:27:29 +0000209 SetBuffered();
Dan Gohman072828e2009-08-13 15:44:52 +0000210 return write(Ptr, Size);
211 }
212 // Write out the data in buffer-sized blocks until the remainder
213 // fits within the buffer.
214 do {
215 size_t NumBytes = OutBufEnd - OutBufCur;
216 copy_to_buffer(Ptr, NumBytes);
Daniel Dunbar6c4570e2009-03-17 01:36:56 +0000217 flush_nonempty();
Dan Gohman072828e2009-08-13 15:44:52 +0000218 Ptr += NumBytes;
219 Size -= NumBytes;
220 } while (OutBufCur+Size > OutBufEnd);
Daniel Dunbar6c4570e2009-03-17 01:36:56 +0000221 }
Dan Gohman072828e2009-08-13 15:44:52 +0000222
223 copy_to_buffer(Ptr, Size);
224
225 return *this;
226}
227
228void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) {
Dan Gohmane2633352009-08-13 20:32:03 +0000229 assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!");
Dan Gohman334a6822009-08-13 18:38:15 +0000230
Owen Anderson1046f622008-08-21 20:58:52 +0000231 // Handle short strings specially, memcpy isn't very good at very short
232 // strings.
233 switch (Size) {
234 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
235 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
236 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
237 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
238 case 0: break;
239 default:
Dan Gohman072828e2009-08-13 15:44:52 +0000240 memcpy(OutBufCur, Ptr, Size);
Owen Anderson1046f622008-08-21 20:58:52 +0000241 break;
242 }
Daniel Dunbarc9731c82009-03-10 16:21:55 +0000243
Dan Gohman072828e2009-08-13 15:44:52 +0000244 OutBufCur += Size;
Owen Anderson1046f622008-08-21 20:58:52 +0000245}
246
Chris Lattnerf9402fe2008-08-23 19:23:10 +0000247// Formatted output.
248raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
Daniel Dunbar6c4570e2009-03-17 01:36:56 +0000249 // If we have more than a few bytes left in our output buffer, try
250 // formatting directly onto its end.
251 //
252 // FIXME: This test is a bit silly, since if we don't have enough
253 // space in the buffer we will have to flush the formatted output
254 // anyway. We should just flush upfront in such cases, and use the
255 // whole buffer as our scratch pad. Note, however, that this case is
256 // also necessary for correctness on unbuffered streams.
Dan Gohman75ffec02009-07-16 15:24:40 +0000257 size_t NextBufferSize = 127;
Chris Lattnerf9402fe2008-08-23 19:23:10 +0000258 if (OutBufEnd-OutBufCur > 3) {
Dan Gohman75ffec02009-07-16 15:24:40 +0000259 size_t BufferBytesLeft = OutBufEnd-OutBufCur;
260 size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
Chris Lattnerf9402fe2008-08-23 19:23:10 +0000261
262 // Common case is that we have plenty of space.
263 if (BytesUsed < BufferBytesLeft) {
264 OutBufCur += BytesUsed;
265 return *this;
266 }
267
268 // Otherwise, we overflowed and the return value tells us the size to try
269 // again with.
270 NextBufferSize = BytesUsed;
271 }
272
273 // If we got here, we didn't have enough space in the output buffer for the
274 // string. Try printing into a SmallVector that is resized to have enough
275 // space. Iterate until we win.
276 SmallVector<char, 128> V;
277
278 while (1) {
279 V.resize(NextBufferSize);
280
281 // Try formatting into the SmallVector.
Dan Gohman75ffec02009-07-16 15:24:40 +0000282 size_t BytesUsed = Fmt.print(&V[0], NextBufferSize);
Chris Lattnerf9402fe2008-08-23 19:23:10 +0000283
284 // If BytesUsed fit into the vector, we win.
Chris Lattner0823c542008-10-26 19:20:47 +0000285 if (BytesUsed <= NextBufferSize)
Chris Lattnerf9402fe2008-08-23 19:23:10 +0000286 return write(&V[0], BytesUsed);
287
288 // Otherwise, try again with a new size.
289 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
290 NextBufferSize = BytesUsed;
291 }
292}
293
294//===----------------------------------------------------------------------===//
295// Formatted Output
296//===----------------------------------------------------------------------===//
297
298// Out of line virtual method.
299void format_object_base::home() {
300}
301
Chris Lattner18283402008-08-17 01:35:29 +0000302//===----------------------------------------------------------------------===//
303// raw_fd_ostream
304//===----------------------------------------------------------------------===//
305
Daniel Dunbaraa6fdf92008-10-21 19:53:10 +0000306/// raw_fd_ostream - Open the specified file for writing. If an error
307/// occurs, information about the error is put into ErrorInfo, and the
308/// stream should be immediately destroyed; the string will be empty
309/// if no error occurred.
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000310raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, bool Force,
Ted Kremeneke6d26892008-12-04 22:51:11 +0000311 std::string &ErrorInfo) : pos(0) {
Daniel Dunbaraa6fdf92008-10-21 19:53:10 +0000312 ErrorInfo.clear();
313
Chris Lattneref27e642008-08-17 03:53:23 +0000314 // Handle "-" as stdout.
315 if (Filename[0] == '-' && Filename[1] == 0) {
316 FD = STDOUT_FILENO;
Daniel Dunbar8c009a32008-11-13 05:01:07 +0000317 // If user requested binary then put stdout into binary mode if
318 // possible.
319 if (Binary)
320 sys::Program::ChangeStdoutToBinary();
Chris Lattneref27e642008-08-17 03:53:23 +0000321 ShouldClose = false;
322 return;
323 }
324
Daniel Dunbar8c009a32008-11-13 05:01:07 +0000325 int Flags = O_WRONLY|O_CREAT|O_TRUNC;
326#ifdef O_BINARY
327 if (Binary)
328 Flags |= O_BINARY;
329#endif
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000330 if (!Force)
331 Flags |= O_EXCL;
Dan Gohmancb87a6d2009-07-15 16:39:40 +0000332 FD = open(Filename, Flags, 0664);
Chris Lattner18283402008-08-17 01:35:29 +0000333 if (FD < 0) {
334 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
335 ShouldClose = false;
336 } else {
337 ShouldClose = true;
338 }
339}
340
341raw_fd_ostream::~raw_fd_ostream() {
Ted Kremeneke02443dc2008-10-23 23:49:09 +0000342 if (FD >= 0) {
343 flush();
344 if (ShouldClose)
Dan Gohman597da3c2009-07-15 16:43:01 +0000345 if (::close(FD) != 0)
Dan Gohmanfea42602009-07-15 23:25:33 +0000346 error_detected();
Ted Kremeneke02443dc2008-10-23 23:49:09 +0000347 }
Chris Lattner18283402008-08-17 01:35:29 +0000348}
349
Dan Gohman75ffec02009-07-16 15:24:40 +0000350void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
Ted Kremeneke02443dc2008-10-23 23:49:09 +0000351 assert (FD >= 0 && "File already closed.");
Daniel Dunbar308eff32009-03-16 23:29:31 +0000352 pos += Size;
Daniel Dunbara3230b72009-07-15 08:11:46 +0000353 if (::write(FD, Ptr, Size) != (ssize_t) Size)
Dan Gohmanfea42602009-07-15 23:25:33 +0000354 error_detected();
Chris Lattner18283402008-08-17 01:35:29 +0000355}
356
Ted Kremeneke02443dc2008-10-23 23:49:09 +0000357void raw_fd_ostream::close() {
358 assert (ShouldClose);
359 ShouldClose = false;
360 flush();
Dan Gohman597da3c2009-07-15 16:43:01 +0000361 if (::close(FD) != 0)
Dan Gohmanfea42602009-07-15 23:25:33 +0000362 error_detected();
Ted Kremeneke02443dc2008-10-23 23:49:09 +0000363 FD = -1;
364}
365
Ted Kremenekdbb64b02009-01-26 21:42:04 +0000366uint64_t raw_fd_ostream::seek(uint64_t off) {
367 flush();
Daniel Dunbara3230b72009-07-15 08:11:46 +0000368 pos = ::lseek(FD, off, SEEK_SET);
Dan Gohman597da3c2009-07-15 16:43:01 +0000369 if (pos != off)
Dan Gohmanfea42602009-07-15 23:25:33 +0000370 error_detected();
Ted Kremenekdbb64b02009-01-26 21:42:04 +0000371 return pos;
372}
373
Dan Gohman6bd7ece2009-08-13 17:27:29 +0000374size_t raw_fd_ostream::preferred_buffer_size() {
Dan Gohmane3bb7622009-08-15 21:41:03 +0000375#if !defined(_MSC_VER) && !defined(__MINGW32__) // Windows has no st_blksize.
Dan Gohman6bd7ece2009-08-13 17:27:29 +0000376 assert(FD >= 0 && "File not yet open!");
377 struct stat statbuf;
Dan Gohman314e3a62009-08-15 02:05:19 +0000378 if (fstat(FD, &statbuf) == 0) {
379 // If this is a terminal, don't use buffering. Line buffering
380 // would be a more traditional thing to do, but it's not worth
381 // the complexity.
382 if (S_ISCHR(statbuf.st_mode) && isatty(FD))
383 return 0;
384 // Return the preferred block size.
Dan Gohman6bd7ece2009-08-13 17:27:29 +0000385 return statbuf.st_blksize;
Dan Gohman314e3a62009-08-15 02:05:19 +0000386 }
Dan Gohman6bd7ece2009-08-13 17:27:29 +0000387 error_detected();
388#endif
389 return raw_ostream::preferred_buffer_size();
390}
391
Edwin Török8a06a5b2009-06-04 07:09:50 +0000392raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
393 bool bg) {
394 if (sys::Process::ColorNeedsFlush())
395 flush();
396 const char *colorcode =
397 (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
398 : sys::Process::OutputColor(colors, bold, bg);
399 if (colorcode) {
Dan Gohman75ffec02009-07-16 15:24:40 +0000400 size_t len = strlen(colorcode);
Edwin Török8a06a5b2009-06-04 07:09:50 +0000401 write(colorcode, len);
402 // don't account colors towards output characters
403 pos -= len;
404 }
405 return *this;
406}
407
408raw_ostream &raw_fd_ostream::resetColor() {
409 if (sys::Process::ColorNeedsFlush())
410 flush();
411 const char *colorcode = sys::Process::ResetColor();
412 if (colorcode) {
Dan Gohman75ffec02009-07-16 15:24:40 +0000413 size_t len = strlen(colorcode);
Edwin Török8a06a5b2009-06-04 07:09:50 +0000414 write(colorcode, len);
415 // don't account colors towards output characters
416 pos -= len;
417 }
418 return *this;
419}
420
Chris Lattner050c9612008-08-17 04:13:37 +0000421//===----------------------------------------------------------------------===//
422// raw_stdout/err_ostream
423//===----------------------------------------------------------------------===//
Chris Lattner18283402008-08-17 01:35:29 +0000424
Dan Gohmaneda5f0e2009-08-13 23:18:56 +0000425// Set buffer settings to model stdout and stderr behavior.
Dan Gohman314e3a62009-08-15 02:05:19 +0000426// Set standard error to be unbuffered by default.
427raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
428raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
Daniel Dunbarc9731c82009-03-10 16:21:55 +0000429 true) {}
Chris Lattner18283402008-08-17 01:35:29 +0000430
431// An out of line virtual method to provide a home for the class vtable.
432void raw_stdout_ostream::handle() {}
433void raw_stderr_ostream::handle() {}
Chris Lattner050c9612008-08-17 04:13:37 +0000434
435/// outs() - This returns a reference to a raw_ostream for standard output.
436/// Use it like: outs() << "foo" << "bar";
Owen Anderson847b99b2008-08-21 00:14:44 +0000437raw_ostream &llvm::outs() {
Chris Lattner050c9612008-08-17 04:13:37 +0000438 static raw_stdout_ostream S;
439 return S;
440}
441
442/// errs() - This returns a reference to a raw_ostream for standard error.
443/// Use it like: errs() << "foo" << "bar";
Owen Anderson847b99b2008-08-21 00:14:44 +0000444raw_ostream &llvm::errs() {
Chris Lattner050c9612008-08-17 04:13:37 +0000445 static raw_stderr_ostream S;
446 return S;
447}
448
Daniel Dunbaraa661bc2009-07-16 21:17:53 +0000449/// nulls() - This returns a reference to a raw_ostream which discards output.
450raw_ostream &llvm::nulls() {
451 static raw_null_ostream S;
452 return S;
453}
454
Chris Lattner050c9612008-08-17 04:13:37 +0000455//===----------------------------------------------------------------------===//
456// raw_os_ostream
457//===----------------------------------------------------------------------===//
458
Daniel Dunbar6c140bc2009-08-18 20:07:36 +0000459raw_os_ostream::~raw_os_ostream() {
460 flush();
461}
462
Dan Gohman75ffec02009-07-16 15:24:40 +0000463void raw_os_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar308eff32009-03-16 23:29:31 +0000464 OS.write(Ptr, Size);
Chris Lattner050c9612008-08-17 04:13:37 +0000465}
Chris Lattnera37b7612008-08-23 22:43:04 +0000466
Douglas Gregorfb5fc122009-04-20 07:34:17 +0000467uint64_t raw_os_ostream::current_pos() { return OS.tellp(); }
468
469uint64_t raw_os_ostream::tell() {
470 return (uint64_t)OS.tellp() + GetNumBytesInBuffer();
471}
472
Chris Lattnera37b7612008-08-23 22:43:04 +0000473//===----------------------------------------------------------------------===//
474// raw_string_ostream
475//===----------------------------------------------------------------------===//
476
Daniel Dunbar6c140bc2009-08-18 20:07:36 +0000477raw_string_ostream::~raw_string_ostream() {
478 flush();
479}
480
Dan Gohman75ffec02009-07-16 15:24:40 +0000481void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar308eff32009-03-16 23:29:31 +0000482 OS.append(Ptr, Size);
Chris Lattnera37b7612008-08-23 22:43:04 +0000483}
484
485//===----------------------------------------------------------------------===//
486// raw_svector_ostream
487//===----------------------------------------------------------------------===//
488
Daniel Dunbar6c140bc2009-08-18 20:07:36 +0000489raw_svector_ostream::~raw_svector_ostream() {
490 flush();
491}
492
Dan Gohman75ffec02009-07-16 15:24:40 +0000493void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbar308eff32009-03-16 23:29:31 +0000494 OS.append(Ptr, Ptr + Size);
Chris Lattnera37b7612008-08-23 22:43:04 +0000495}
496
Douglas Gregorfb5fc122009-04-20 07:34:17 +0000497uint64_t raw_svector_ostream::current_pos() { return OS.size(); }
498
499uint64_t raw_svector_ostream::tell() {
500 return OS.size() + GetNumBytesInBuffer();
501}
Daniel Dunbaraa661bc2009-07-16 21:17:53 +0000502
503//===----------------------------------------------------------------------===//
504// raw_null_ostream
505//===----------------------------------------------------------------------===//
506
Dan Gohman7f4a9dd2009-07-27 21:46:02 +0000507raw_null_ostream::~raw_null_ostream() {
508#ifndef NDEBUG
509 // ~raw_ostream asserts that the buffer is empty. This isn't necessary
510 // with raw_null_ostream, but it's better to have raw_null_ostream follow
511 // the rules than to change the rules just for raw_null_ostream.
512 flush();
513#endif
514}
515
Daniel Dunbaraa661bc2009-07-16 21:17:53 +0000516void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
517}
518
519uint64_t raw_null_ostream::current_pos() {
520 return 0;
521}