Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 1 | //===--- 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 Lattner | 42f77ab | 2008-08-23 20:34:06 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Format.h" |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 16 | #include "llvm/System/Program.h" |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 17 | #include "llvm/System/Process.h" |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 19 | #include "llvm/Config/config.h" |
Daniel Dunbar | b372c11 | 2009-03-17 21:15:18 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
Daniel Dunbar | 579fb87 | 2009-07-15 08:11:46 +0000 | [diff] [blame^] | 21 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 22 | #include <ostream> |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 24 | #if defined(HAVE_UNISTD_H) |
| 25 | # include <unistd.h> |
| 26 | #endif |
| 27 | #if defined(HAVE_FCNTL_H) |
| 28 | # include <fcntl.h> |
| 29 | #endif |
Argyrios Kyrtzidis | b68dc36 | 2008-08-17 09:25:21 +0000 | [diff] [blame] | 30 | |
| 31 | #if defined(_MSC_VER) |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 32 | #include <io.h> |
Cedric Venet | a3f343f | 2008-08-24 11:56:40 +0000 | [diff] [blame] | 33 | #include <fcntl.h> |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 34 | #ifndef STDIN_FILENO |
| 35 | # define STDIN_FILENO 0 |
| 36 | #endif |
| 37 | #ifndef STDOUT_FILENO |
| 38 | # define STDOUT_FILENO 1 |
| 39 | #endif |
| 40 | #ifndef STDERR_FILENO |
| 41 | # define STDERR_FILENO 2 |
| 42 | #endif |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | |
| 47 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 48 | // An out of line virtual method to provide a home for the class vtable. |
| 49 | void raw_ostream::handle() {} |
| 50 | |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 51 | raw_ostream &raw_ostream::operator<<(unsigned long N) { |
| 52 | // Zero is a special case. |
| 53 | if (N == 0) |
| 54 | return *this << '0'; |
| 55 | |
| 56 | char NumberBuffer[20]; |
| 57 | char *EndPtr = NumberBuffer+sizeof(NumberBuffer); |
| 58 | char *CurPtr = EndPtr; |
| 59 | |
| 60 | while (N) { |
| 61 | *--CurPtr = '0' + char(N % 10); |
| 62 | N /= 10; |
| 63 | } |
| 64 | return write(CurPtr, EndPtr-CurPtr); |
| 65 | } |
| 66 | |
| 67 | raw_ostream &raw_ostream::operator<<(long N) { |
| 68 | if (N < 0) { |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 69 | *this << '-'; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 70 | N = -N; |
| 71 | } |
| 72 | |
| 73 | return this->operator<<(static_cast<unsigned long>(N)); |
| 74 | } |
| 75 | |
| 76 | raw_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 | |
| 92 | raw_ostream &raw_ostream::operator<<(long long N) { |
| 93 | if (N < 0) { |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 94 | *this << '-'; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 95 | N = -N; |
| 96 | } |
| 97 | |
| 98 | return this->operator<<(static_cast<unsigned long long>(N)); |
| 99 | } |
| 100 | |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 101 | raw_ostream &raw_ostream::operator<<(const void *P) { |
Daniel Dunbar | b451a0c | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 102 | uintptr_t N = (uintptr_t) P; |
| 103 | *this << '0' << 'x'; |
| 104 | |
| 105 | // Zero is a special case. |
| 106 | if (N == 0) |
| 107 | return *this << '0'; |
| 108 | |
| 109 | char NumberBuffer[20]; |
| 110 | char *EndPtr = NumberBuffer+sizeof(NumberBuffer); |
| 111 | char *CurPtr = EndPtr; |
| 112 | |
| 113 | while (N) { |
| 114 | unsigned x = N % 16; |
| 115 | *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10); |
| 116 | N /= 16; |
| 117 | } |
| 118 | |
| 119 | return write(CurPtr, EndPtr-CurPtr); |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame] | 122 | void raw_ostream::flush_nonempty() { |
| 123 | assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty."); |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 124 | write_impl(OutBufStart, OutBufCur - OutBufStart); |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame] | 125 | OutBufCur = OutBufStart; |
| 126 | } |
| 127 | |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 128 | raw_ostream &raw_ostream::write(unsigned char C) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 129 | // Group exceptional cases into a single branch. |
| 130 | if (OutBufCur >= OutBufEnd) { |
| 131 | if (Unbuffered) { |
| 132 | write_impl(reinterpret_cast<char*>(&C), 1); |
| 133 | return *this; |
| 134 | } |
| 135 | |
| 136 | if (!OutBufStart) |
| 137 | SetBufferSize(); |
| 138 | else |
| 139 | flush_nonempty(); |
Daniel Dunbar | d17d74b | 2009-03-17 01:13:35 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 142 | *OutBufCur++ = C; |
Daniel Dunbar | b451a0c | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 143 | return *this; |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 144 | } |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 145 | |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 146 | raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 147 | // Group exceptional cases into a single branch. |
Daniel Dunbar | b372c11 | 2009-03-17 21:15:18 +0000 | [diff] [blame] | 148 | if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 149 | if (Unbuffered) { |
| 150 | write_impl(Ptr, Size); |
| 151 | return *this; |
| 152 | } |
Daniel Dunbar | d17d74b | 2009-03-17 01:13:35 +0000 | [diff] [blame] | 153 | |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 154 | if (!OutBufStart) |
| 155 | SetBufferSize(); |
| 156 | else |
| 157 | flush_nonempty(); |
| 158 | } |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 159 | |
| 160 | // Handle short strings specially, memcpy isn't very good at very short |
| 161 | // strings. |
| 162 | switch (Size) { |
| 163 | case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH |
| 164 | case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH |
| 165 | case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH |
| 166 | case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH |
| 167 | case 0: break; |
| 168 | default: |
| 169 | // Normally the string to emit is shorter than the buffer. |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 170 | if (Size <= unsigned(OutBufEnd-OutBufCur)) { |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 171 | memcpy(OutBufCur, Ptr, Size); |
| 172 | break; |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 173 | } |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 174 | |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 175 | // Otherwise we are emitting a string larger than our buffer. We |
| 176 | // know we already flushed, so just write it out directly. |
| 177 | write_impl(Ptr, Size); |
| 178 | Size = 0; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 179 | break; |
| 180 | } |
| 181 | OutBufCur += Size; |
Daniel Dunbar | e77e434 | 2009-03-10 16:21:55 +0000 | [diff] [blame] | 182 | |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 183 | return *this; |
| 184 | } |
| 185 | |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 186 | // Formatted output. |
| 187 | raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 188 | // If we have more than a few bytes left in our output buffer, try |
| 189 | // formatting directly onto its end. |
| 190 | // |
| 191 | // FIXME: This test is a bit silly, since if we don't have enough |
| 192 | // space in the buffer we will have to flush the formatted output |
| 193 | // anyway. We should just flush upfront in such cases, and use the |
| 194 | // whole buffer as our scratch pad. Note, however, that this case is |
| 195 | // also necessary for correctness on unbuffered streams. |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 196 | unsigned NextBufferSize = 127; |
| 197 | if (OutBufEnd-OutBufCur > 3) { |
| 198 | unsigned BufferBytesLeft = OutBufEnd-OutBufCur; |
| 199 | unsigned BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft); |
| 200 | |
| 201 | // Common case is that we have plenty of space. |
| 202 | if (BytesUsed < BufferBytesLeft) { |
| 203 | OutBufCur += BytesUsed; |
| 204 | return *this; |
| 205 | } |
| 206 | |
| 207 | // Otherwise, we overflowed and the return value tells us the size to try |
| 208 | // again with. |
| 209 | NextBufferSize = BytesUsed; |
| 210 | } |
| 211 | |
| 212 | // If we got here, we didn't have enough space in the output buffer for the |
| 213 | // string. Try printing into a SmallVector that is resized to have enough |
| 214 | // space. Iterate until we win. |
| 215 | SmallVector<char, 128> V; |
| 216 | |
| 217 | while (1) { |
| 218 | V.resize(NextBufferSize); |
| 219 | |
| 220 | // Try formatting into the SmallVector. |
| 221 | unsigned BytesUsed = Fmt.print(&V[0], NextBufferSize); |
| 222 | |
| 223 | // If BytesUsed fit into the vector, we win. |
Chris Lattner | f61ca1e | 2008-10-26 19:20:47 +0000 | [diff] [blame] | 224 | if (BytesUsed <= NextBufferSize) |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 225 | return write(&V[0], BytesUsed); |
| 226 | |
| 227 | // Otherwise, try again with a new size. |
| 228 | assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?"); |
| 229 | NextBufferSize = BytesUsed; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | //===----------------------------------------------------------------------===// |
| 234 | // Formatted Output |
| 235 | //===----------------------------------------------------------------------===// |
| 236 | |
| 237 | // Out of line virtual method. |
| 238 | void format_object_base::home() { |
| 239 | } |
| 240 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 241 | //===----------------------------------------------------------------------===// |
| 242 | // raw_fd_ostream |
| 243 | //===----------------------------------------------------------------------===// |
| 244 | |
Daniel Dunbar | 48534b3 | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 245 | /// raw_fd_ostream - Open the specified file for writing. If an error |
| 246 | /// occurs, information about the error is put into ErrorInfo, and the |
| 247 | /// stream should be immediately destroyed; the string will be empty |
| 248 | /// if no error occurred. |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 249 | raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, |
Ted Kremenek | d75ba1c | 2008-12-04 22:51:11 +0000 | [diff] [blame] | 250 | std::string &ErrorInfo) : pos(0) { |
Daniel Dunbar | 48534b3 | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 251 | ErrorInfo.clear(); |
| 252 | |
Chris Lattner | c52b128 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 253 | // Handle "-" as stdout. |
| 254 | if (Filename[0] == '-' && Filename[1] == 0) { |
| 255 | FD = STDOUT_FILENO; |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 256 | // If user requested binary then put stdout into binary mode if |
| 257 | // possible. |
| 258 | if (Binary) |
| 259 | sys::Program::ChangeStdoutToBinary(); |
Chris Lattner | c52b128 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 260 | ShouldClose = false; |
| 261 | return; |
| 262 | } |
| 263 | |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 264 | int Flags = O_WRONLY|O_CREAT|O_TRUNC; |
| 265 | #ifdef O_BINARY |
| 266 | if (Binary) |
| 267 | Flags |= O_BINARY; |
| 268 | #endif |
| 269 | FD = open(Filename, Flags, 0644); |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 270 | if (FD < 0) { |
| 271 | ErrorInfo = "Error opening output file '" + std::string(Filename) + "'"; |
| 272 | ShouldClose = false; |
| 273 | } else { |
| 274 | ShouldClose = true; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | raw_fd_ostream::~raw_fd_ostream() { |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 279 | if (FD >= 0) { |
| 280 | flush(); |
| 281 | if (ShouldClose) |
| 282 | ::close(FD); |
| 283 | } |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 286 | void raw_fd_ostream::write_impl(const char *Ptr, unsigned Size) { |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 287 | assert (FD >= 0 && "File already closed."); |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 288 | pos += Size; |
Daniel Dunbar | 579fb87 | 2009-07-15 08:11:46 +0000 | [diff] [blame^] | 289 | if (::write(FD, Ptr, Size) != (ssize_t) Size) |
| 290 | llvm_report_error("IO failure writing to output stream."); |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 293 | void raw_fd_ostream::close() { |
| 294 | assert (ShouldClose); |
| 295 | ShouldClose = false; |
| 296 | flush(); |
| 297 | ::close(FD); |
| 298 | FD = -1; |
| 299 | } |
| 300 | |
Ted Kremenek | 9c27886 | 2009-01-26 21:42:04 +0000 | [diff] [blame] | 301 | uint64_t raw_fd_ostream::seek(uint64_t off) { |
| 302 | flush(); |
Daniel Dunbar | 579fb87 | 2009-07-15 08:11:46 +0000 | [diff] [blame^] | 303 | pos = ::lseek(FD, off, SEEK_SET); |
Ted Kremenek | 9c27886 | 2009-01-26 21:42:04 +0000 | [diff] [blame] | 304 | return pos; |
| 305 | } |
| 306 | |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 307 | raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold, |
| 308 | bool bg) { |
| 309 | if (sys::Process::ColorNeedsFlush()) |
| 310 | flush(); |
| 311 | const char *colorcode = |
| 312 | (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg) |
| 313 | : sys::Process::OutputColor(colors, bold, bg); |
| 314 | if (colorcode) { |
| 315 | unsigned len = strlen(colorcode); |
| 316 | write(colorcode, len); |
| 317 | // don't account colors towards output characters |
| 318 | pos -= len; |
| 319 | } |
| 320 | return *this; |
| 321 | } |
| 322 | |
| 323 | raw_ostream &raw_fd_ostream::resetColor() { |
| 324 | if (sys::Process::ColorNeedsFlush()) |
| 325 | flush(); |
| 326 | const char *colorcode = sys::Process::ResetColor(); |
| 327 | if (colorcode) { |
| 328 | unsigned len = strlen(colorcode); |
| 329 | write(colorcode, len); |
| 330 | // don't account colors towards output characters |
| 331 | pos -= len; |
| 332 | } |
| 333 | return *this; |
| 334 | } |
| 335 | |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 336 | //===----------------------------------------------------------------------===// |
| 337 | // raw_stdout/err_ostream |
| 338 | //===----------------------------------------------------------------------===// |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 339 | |
| 340 | raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {} |
Daniel Dunbar | e77e434 | 2009-03-10 16:21:55 +0000 | [diff] [blame] | 341 | raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false, |
| 342 | true) {} |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 343 | |
| 344 | // An out of line virtual method to provide a home for the class vtable. |
| 345 | void raw_stdout_ostream::handle() {} |
| 346 | void raw_stderr_ostream::handle() {} |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 347 | |
| 348 | /// outs() - This returns a reference to a raw_ostream for standard output. |
| 349 | /// Use it like: outs() << "foo" << "bar"; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 350 | raw_ostream &llvm::outs() { |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 351 | static raw_stdout_ostream S; |
| 352 | return S; |
| 353 | } |
| 354 | |
| 355 | /// errs() - This returns a reference to a raw_ostream for standard error. |
| 356 | /// Use it like: errs() << "foo" << "bar"; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 357 | raw_ostream &llvm::errs() { |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 358 | static raw_stderr_ostream S; |
| 359 | return S; |
| 360 | } |
| 361 | |
| 362 | //===----------------------------------------------------------------------===// |
| 363 | // raw_os_ostream |
| 364 | //===----------------------------------------------------------------------===// |
| 365 | |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 366 | raw_os_ostream::~raw_os_ostream() { |
| 367 | flush(); |
| 368 | } |
| 369 | |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 370 | void raw_os_ostream::write_impl(const char *Ptr, unsigned Size) { |
| 371 | OS.write(Ptr, Size); |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 372 | } |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 373 | |
Douglas Gregor | 8f7be47 | 2009-04-20 07:34:17 +0000 | [diff] [blame] | 374 | uint64_t raw_os_ostream::current_pos() { return OS.tellp(); } |
| 375 | |
| 376 | uint64_t raw_os_ostream::tell() { |
| 377 | return (uint64_t)OS.tellp() + GetNumBytesInBuffer(); |
| 378 | } |
| 379 | |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 380 | //===----------------------------------------------------------------------===// |
| 381 | // raw_string_ostream |
| 382 | //===----------------------------------------------------------------------===// |
| 383 | |
| 384 | raw_string_ostream::~raw_string_ostream() { |
| 385 | flush(); |
| 386 | } |
| 387 | |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 388 | void raw_string_ostream::write_impl(const char *Ptr, unsigned Size) { |
| 389 | OS.append(Ptr, Size); |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | //===----------------------------------------------------------------------===// |
| 393 | // raw_svector_ostream |
| 394 | //===----------------------------------------------------------------------===// |
| 395 | |
| 396 | raw_svector_ostream::~raw_svector_ostream() { |
| 397 | flush(); |
| 398 | } |
| 399 | |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 400 | void raw_svector_ostream::write_impl(const char *Ptr, unsigned Size) { |
| 401 | OS.append(Ptr, Ptr + Size); |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Douglas Gregor | 8f7be47 | 2009-04-20 07:34:17 +0000 | [diff] [blame] | 404 | uint64_t raw_svector_ostream::current_pos() { return OS.size(); } |
| 405 | |
| 406 | uint64_t raw_svector_ostream::tell() { |
| 407 | return OS.size() + GetNumBytesInBuffer(); |
| 408 | } |