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."); |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 121 | write_impl(OutBufStart, OutBufCur - OutBufStart); |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame] | 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 | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame^] | 126 | // Group exceptional cases into a single branch. |
| 127 | if (OutBufCur >= OutBufEnd) { |
| 128 | if (Unbuffered) { |
| 129 | write_impl(reinterpret_cast<char*>(&C), 1); |
| 130 | return *this; |
| 131 | } |
| 132 | |
| 133 | if (!OutBufStart) |
| 134 | SetBufferSize(); |
| 135 | else |
| 136 | flush_nonempty(); |
Daniel Dunbar | d17d74b | 2009-03-17 01:13:35 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 139 | *OutBufCur++ = C; |
Daniel Dunbar | b451a0c | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 140 | return *this; |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 141 | } |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 142 | |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 143 | raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame^] | 144 | // Group exceptional cases into a single branch. |
| 145 | if (OutBufCur+Size > OutBufEnd) { |
| 146 | if (Unbuffered) { |
| 147 | write_impl(Ptr, Size); |
| 148 | return *this; |
| 149 | } |
Daniel Dunbar | d17d74b | 2009-03-17 01:13:35 +0000 | [diff] [blame] | 150 | |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame^] | 151 | if (!OutBufStart) |
| 152 | SetBufferSize(); |
| 153 | else |
| 154 | flush_nonempty(); |
| 155 | } |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 156 | |
| 157 | // Handle short strings specially, memcpy isn't very good at very short |
| 158 | // strings. |
| 159 | switch (Size) { |
| 160 | case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH |
| 161 | case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH |
| 162 | case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH |
| 163 | case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH |
| 164 | case 0: break; |
| 165 | default: |
| 166 | // Normally the string to emit is shorter than the buffer. |
| 167 | if (Size <= unsigned(OutBufEnd-OutBufStart)) { |
| 168 | memcpy(OutBufCur, Ptr, Size); |
| 169 | break; |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 170 | } |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 171 | |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 172 | // Otherwise we are emitting a string larger than our buffer. We |
| 173 | // know we already flushed, so just write it out directly. |
| 174 | write_impl(Ptr, Size); |
| 175 | Size = 0; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 176 | break; |
| 177 | } |
| 178 | OutBufCur += Size; |
Daniel Dunbar | e77e434 | 2009-03-10 16:21:55 +0000 | [diff] [blame] | 179 | |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 180 | return *this; |
| 181 | } |
| 182 | |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 183 | // Formatted output. |
| 184 | raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame^] | 185 | // If we have more than a few bytes left in our output buffer, try |
| 186 | // formatting directly onto its end. |
| 187 | // |
| 188 | // FIXME: This test is a bit silly, since if we don't have enough |
| 189 | // space in the buffer we will have to flush the formatted output |
| 190 | // anyway. We should just flush upfront in such cases, and use the |
| 191 | // whole buffer as our scratch pad. Note, however, that this case is |
| 192 | // also necessary for correctness on unbuffered streams. |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 193 | unsigned NextBufferSize = 127; |
| 194 | if (OutBufEnd-OutBufCur > 3) { |
| 195 | unsigned BufferBytesLeft = OutBufEnd-OutBufCur; |
| 196 | unsigned BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft); |
| 197 | |
| 198 | // Common case is that we have plenty of space. |
| 199 | if (BytesUsed < BufferBytesLeft) { |
| 200 | OutBufCur += BytesUsed; |
| 201 | return *this; |
| 202 | } |
| 203 | |
| 204 | // Otherwise, we overflowed and the return value tells us the size to try |
| 205 | // again with. |
| 206 | NextBufferSize = BytesUsed; |
| 207 | } |
| 208 | |
| 209 | // If we got here, we didn't have enough space in the output buffer for the |
| 210 | // string. Try printing into a SmallVector that is resized to have enough |
| 211 | // space. Iterate until we win. |
| 212 | SmallVector<char, 128> V; |
| 213 | |
| 214 | while (1) { |
| 215 | V.resize(NextBufferSize); |
| 216 | |
| 217 | // Try formatting into the SmallVector. |
| 218 | unsigned BytesUsed = Fmt.print(&V[0], NextBufferSize); |
| 219 | |
| 220 | // If BytesUsed fit into the vector, we win. |
Chris Lattner | f61ca1e | 2008-10-26 19:20:47 +0000 | [diff] [blame] | 221 | if (BytesUsed <= NextBufferSize) |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 222 | return write(&V[0], BytesUsed); |
| 223 | |
| 224 | // Otherwise, try again with a new size. |
| 225 | assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?"); |
| 226 | NextBufferSize = BytesUsed; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | //===----------------------------------------------------------------------===// |
| 231 | // Formatted Output |
| 232 | //===----------------------------------------------------------------------===// |
| 233 | |
| 234 | // Out of line virtual method. |
| 235 | void format_object_base::home() { |
| 236 | } |
| 237 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 238 | //===----------------------------------------------------------------------===// |
| 239 | // raw_fd_ostream |
| 240 | //===----------------------------------------------------------------------===// |
| 241 | |
Daniel Dunbar | 48534b3 | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 242 | /// raw_fd_ostream - Open the specified file for writing. If an error |
| 243 | /// occurs, information about the error is put into ErrorInfo, and the |
| 244 | /// stream should be immediately destroyed; the string will be empty |
| 245 | /// if no error occurred. |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 246 | raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, |
Ted Kremenek | d75ba1c | 2008-12-04 22:51:11 +0000 | [diff] [blame] | 247 | std::string &ErrorInfo) : pos(0) { |
Daniel Dunbar | 48534b3 | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 248 | ErrorInfo.clear(); |
| 249 | |
Chris Lattner | c52b128 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 250 | // Handle "-" as stdout. |
| 251 | if (Filename[0] == '-' && Filename[1] == 0) { |
| 252 | FD = STDOUT_FILENO; |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 253 | // If user requested binary then put stdout into binary mode if |
| 254 | // possible. |
| 255 | if (Binary) |
| 256 | sys::Program::ChangeStdoutToBinary(); |
Chris Lattner | c52b128 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 257 | ShouldClose = false; |
| 258 | return; |
| 259 | } |
| 260 | |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 261 | int Flags = O_WRONLY|O_CREAT|O_TRUNC; |
| 262 | #ifdef O_BINARY |
| 263 | if (Binary) |
| 264 | Flags |= O_BINARY; |
| 265 | #endif |
| 266 | FD = open(Filename, Flags, 0644); |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 267 | if (FD < 0) { |
| 268 | ErrorInfo = "Error opening output file '" + std::string(Filename) + "'"; |
| 269 | ShouldClose = false; |
| 270 | } else { |
| 271 | ShouldClose = true; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | raw_fd_ostream::~raw_fd_ostream() { |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 276 | if (FD >= 0) { |
| 277 | flush(); |
| 278 | if (ShouldClose) |
| 279 | ::close(FD); |
| 280 | } |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 283 | void raw_fd_ostream::write_impl(const char *Ptr, unsigned Size) { |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 284 | assert (FD >= 0 && "File already closed."); |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 285 | pos += Size; |
| 286 | ::write(FD, Ptr, Size); |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 289 | void raw_fd_ostream::close() { |
| 290 | assert (ShouldClose); |
| 291 | ShouldClose = false; |
| 292 | flush(); |
| 293 | ::close(FD); |
| 294 | FD = -1; |
| 295 | } |
| 296 | |
Ted Kremenek | 9c27886 | 2009-01-26 21:42:04 +0000 | [diff] [blame] | 297 | uint64_t raw_fd_ostream::seek(uint64_t off) { |
| 298 | flush(); |
| 299 | pos = lseek(FD, off, SEEK_SET); |
| 300 | return pos; |
| 301 | } |
| 302 | |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 303 | //===----------------------------------------------------------------------===// |
| 304 | // raw_stdout/err_ostream |
| 305 | //===----------------------------------------------------------------------===// |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 306 | |
| 307 | raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {} |
Daniel Dunbar | e77e434 | 2009-03-10 16:21:55 +0000 | [diff] [blame] | 308 | raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false, |
| 309 | true) {} |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 310 | |
| 311 | // An out of line virtual method to provide a home for the class vtable. |
| 312 | void raw_stdout_ostream::handle() {} |
| 313 | void raw_stderr_ostream::handle() {} |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 314 | |
| 315 | /// outs() - This returns a reference to a raw_ostream for standard output. |
| 316 | /// Use it like: outs() << "foo" << "bar"; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 317 | raw_ostream &llvm::outs() { |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 318 | static raw_stdout_ostream S; |
| 319 | return S; |
| 320 | } |
| 321 | |
| 322 | /// errs() - This returns a reference to a raw_ostream for standard error. |
| 323 | /// Use it like: errs() << "foo" << "bar"; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 324 | raw_ostream &llvm::errs() { |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 325 | static raw_stderr_ostream S; |
| 326 | return S; |
| 327 | } |
| 328 | |
| 329 | //===----------------------------------------------------------------------===// |
| 330 | // raw_os_ostream |
| 331 | //===----------------------------------------------------------------------===// |
| 332 | |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 333 | raw_os_ostream::~raw_os_ostream() { |
| 334 | flush(); |
| 335 | } |
| 336 | |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 337 | void raw_os_ostream::write_impl(const char *Ptr, unsigned Size) { |
| 338 | OS.write(Ptr, Size); |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 339 | } |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 340 | |
| 341 | //===----------------------------------------------------------------------===// |
| 342 | // raw_string_ostream |
| 343 | //===----------------------------------------------------------------------===// |
| 344 | |
| 345 | raw_string_ostream::~raw_string_ostream() { |
| 346 | flush(); |
| 347 | } |
| 348 | |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 349 | void raw_string_ostream::write_impl(const char *Ptr, unsigned Size) { |
| 350 | OS.append(Ptr, Size); |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | //===----------------------------------------------------------------------===// |
| 354 | // raw_svector_ostream |
| 355 | //===----------------------------------------------------------------------===// |
| 356 | |
| 357 | raw_svector_ostream::~raw_svector_ostream() { |
| 358 | flush(); |
| 359 | } |
| 360 | |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 361 | void raw_svector_ostream::write_impl(const char *Ptr, unsigned Size) { |
| 362 | OS.append(Ptr, Ptr + Size); |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 363 | } |
| 364 | |