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" |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 18 | #include "llvm/Config/config.h" |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 19 | #include <ostream> |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 20 | |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 21 | #if defined(HAVE_UNISTD_H) |
| 22 | # include <unistd.h> |
| 23 | #endif |
| 24 | #if defined(HAVE_FCNTL_H) |
| 25 | # include <fcntl.h> |
| 26 | #endif |
Argyrios Kyrtzidis | b68dc36 | 2008-08-17 09:25:21 +0000 | [diff] [blame] | 27 | |
| 28 | #if defined(_MSC_VER) |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 29 | #include <io.h> |
Cedric Venet | a3f343f | 2008-08-24 11:56:40 +0000 | [diff] [blame] | 30 | #include <fcntl.h> |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 31 | #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 Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 42 | using namespace llvm; |
| 43 | |
| 44 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 45 | // An out of line virtual method to provide a home for the class vtable. |
| 46 | void raw_ostream::handle() {} |
| 47 | |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 48 | raw_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 | |
| 64 | raw_ostream &raw_ostream::operator<<(long N) { |
| 65 | if (N < 0) { |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 66 | *this << '-'; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 67 | N = -N; |
| 68 | } |
| 69 | |
| 70 | return this->operator<<(static_cast<unsigned long>(N)); |
| 71 | } |
| 72 | |
| 73 | raw_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 | |
| 89 | raw_ostream &raw_ostream::operator<<(long long N) { |
| 90 | if (N < 0) { |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 91 | *this << '-'; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 92 | N = -N; |
| 93 | } |
| 94 | |
| 95 | return this->operator<<(static_cast<unsigned long long>(N)); |
| 96 | } |
| 97 | |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 98 | raw_ostream &raw_ostream::operator<<(const void *P) { |
Daniel Dunbar | b451a0c | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 99 | 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 Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame^] | 119 | void raw_ostream::flush_nonempty() { |
| 120 | assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty."); |
| 121 | flush_impl(); |
| 122 | OutBufCur = OutBufStart; |
| 123 | } |
| 124 | |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 125 | raw_ostream &raw_ostream::write(unsigned char C) { |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame^] | 126 | if (!OutBufStart) |
| 127 | SetBufferSize(4096); |
| 128 | else if (OutBufCur >= OutBufEnd) |
| 129 | flush_nonempty(); |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 130 | |
| 131 | *OutBufCur++ = C; |
Daniel Dunbar | b451a0c | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 132 | return *this; |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 133 | } |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 134 | |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 135 | raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) { |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame^] | 136 | if (!OutBufStart) |
| 137 | SetBufferSize(4096); |
| 138 | else if (OutBufCur+Size > OutBufEnd) |
| 139 | flush_nonempty(); |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 140 | |
| 141 | // Handle short strings specially, memcpy isn't very good at very short |
| 142 | // strings. |
| 143 | switch (Size) { |
| 144 | case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH |
| 145 | case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH |
| 146 | case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH |
| 147 | case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH |
| 148 | case 0: break; |
| 149 | default: |
| 150 | // Normally the string to emit is shorter than the buffer. |
| 151 | if (Size <= unsigned(OutBufEnd-OutBufStart)) { |
| 152 | memcpy(OutBufCur, Ptr, Size); |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | // If emitting a string larger than our buffer, emit in chunks. In this |
| 157 | // case we know that we just flushed the buffer. |
| 158 | while (Size) { |
| 159 | unsigned NumToEmit = OutBufEnd-OutBufStart; |
| 160 | if (Size < NumToEmit) NumToEmit = Size; |
| 161 | assert(OutBufCur == OutBufStart); |
| 162 | memcpy(OutBufStart, Ptr, NumToEmit); |
| 163 | Ptr += NumToEmit; |
Owen Anderson | fd2a053 | 2008-08-21 22:39:33 +0000 | [diff] [blame] | 164 | Size -= NumToEmit; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 165 | OutBufCur = OutBufStart + NumToEmit; |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame^] | 166 | flush_nonempty(); |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 167 | } |
| 168 | break; |
| 169 | } |
| 170 | OutBufCur += Size; |
Daniel Dunbar | e77e434 | 2009-03-10 16:21:55 +0000 | [diff] [blame] | 171 | |
| 172 | if (Unbuffered) |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame^] | 173 | flush(); |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 174 | return *this; |
| 175 | } |
| 176 | |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 177 | // Formatted output. |
| 178 | raw_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 Lattner | f61ca1e | 2008-10-26 19:20:47 +0000 | [diff] [blame] | 209 | if (BytesUsed <= NextBufferSize) |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 210 | 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. |
| 223 | void format_object_base::home() { |
| 224 | } |
| 225 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 226 | //===----------------------------------------------------------------------===// |
| 227 | // raw_fd_ostream |
| 228 | //===----------------------------------------------------------------------===// |
| 229 | |
Daniel Dunbar | 48534b3 | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 230 | /// 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 Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 234 | raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, |
Ted Kremenek | d75ba1c | 2008-12-04 22:51:11 +0000 | [diff] [blame] | 235 | std::string &ErrorInfo) : pos(0) { |
Daniel Dunbar | 48534b3 | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 236 | ErrorInfo.clear(); |
| 237 | |
Chris Lattner | c52b128 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 238 | // Handle "-" as stdout. |
| 239 | if (Filename[0] == '-' && Filename[1] == 0) { |
| 240 | FD = STDOUT_FILENO; |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 241 | // If user requested binary then put stdout into binary mode if |
| 242 | // possible. |
| 243 | if (Binary) |
| 244 | sys::Program::ChangeStdoutToBinary(); |
Chris Lattner | c52b128 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 245 | ShouldClose = false; |
| 246 | return; |
| 247 | } |
| 248 | |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 249 | 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 Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 255 | if (FD < 0) { |
| 256 | ErrorInfo = "Error opening output file '" + std::string(Filename) + "'"; |
| 257 | ShouldClose = false; |
| 258 | } else { |
| 259 | ShouldClose = true; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | raw_fd_ostream::~raw_fd_ostream() { |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 264 | if (FD >= 0) { |
| 265 | flush(); |
| 266 | if (ShouldClose) |
| 267 | ::close(FD); |
| 268 | } |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void raw_fd_ostream::flush_impl() { |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 272 | assert (FD >= 0 && "File already closed."); |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame^] | 273 | pos += (OutBufCur - OutBufStart); |
| 274 | ::write(FD, OutBufStart, OutBufCur-OutBufStart); |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 277 | void raw_fd_ostream::close() { |
| 278 | assert (ShouldClose); |
| 279 | ShouldClose = false; |
| 280 | flush(); |
| 281 | ::close(FD); |
| 282 | FD = -1; |
| 283 | } |
| 284 | |
Ted Kremenek | 9c27886 | 2009-01-26 21:42:04 +0000 | [diff] [blame] | 285 | uint64_t raw_fd_ostream::seek(uint64_t off) { |
| 286 | flush(); |
| 287 | pos = lseek(FD, off, SEEK_SET); |
| 288 | return pos; |
| 289 | } |
| 290 | |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 291 | //===----------------------------------------------------------------------===// |
| 292 | // raw_stdout/err_ostream |
| 293 | //===----------------------------------------------------------------------===// |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 294 | |
| 295 | raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {} |
Daniel Dunbar | e77e434 | 2009-03-10 16:21:55 +0000 | [diff] [blame] | 296 | raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false, |
| 297 | true) {} |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 298 | |
| 299 | // An out of line virtual method to provide a home for the class vtable. |
| 300 | void raw_stdout_ostream::handle() {} |
| 301 | void raw_stderr_ostream::handle() {} |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 302 | |
| 303 | /// outs() - This returns a reference to a raw_ostream for standard output. |
| 304 | /// Use it like: outs() << "foo" << "bar"; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 305 | raw_ostream &llvm::outs() { |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 306 | 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 Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 312 | raw_ostream &llvm::errs() { |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 313 | static raw_stderr_ostream S; |
| 314 | return S; |
| 315 | } |
| 316 | |
| 317 | //===----------------------------------------------------------------------===// |
| 318 | // raw_os_ostream |
| 319 | //===----------------------------------------------------------------------===// |
| 320 | |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 321 | raw_os_ostream::~raw_os_ostream() { |
| 322 | flush(); |
| 323 | } |
| 324 | |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 325 | /// flush_impl - The is the piece of the class that is implemented by |
| 326 | /// subclasses. This outputs the currently buffered data and resets the |
| 327 | /// buffer to empty. |
| 328 | void raw_os_ostream::flush_impl() { |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame^] | 329 | OS.write(OutBufStart, OutBufCur-OutBufStart); |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 330 | } |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 331 | |
| 332 | //===----------------------------------------------------------------------===// |
| 333 | // raw_string_ostream |
| 334 | //===----------------------------------------------------------------------===// |
| 335 | |
| 336 | raw_string_ostream::~raw_string_ostream() { |
| 337 | flush(); |
| 338 | } |
| 339 | |
| 340 | /// flush_impl - The is the piece of the class that is implemented by |
| 341 | /// subclasses. This outputs the currently buffered data and resets the |
| 342 | /// buffer to empty. |
| 343 | void raw_string_ostream::flush_impl() { |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame^] | 344 | OS.append(OutBufStart, OutBufCur-OutBufStart); |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | //===----------------------------------------------------------------------===// |
| 348 | // raw_svector_ostream |
| 349 | //===----------------------------------------------------------------------===// |
| 350 | |
| 351 | raw_svector_ostream::~raw_svector_ostream() { |
| 352 | flush(); |
| 353 | } |
| 354 | |
| 355 | /// flush_impl - The is the piece of the class that is implemented by |
| 356 | /// subclasses. This outputs the currently buffered data and resets the |
| 357 | /// buffer to empty. |
| 358 | void raw_svector_ostream::flush_impl() { |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame^] | 359 | OS.append(OutBufStart, OutBufCur); |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 360 | } |
| 361 | |