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" |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 17 | #include "llvm/Config/config.h" |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 18 | #include <ostream> |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 20 | #if defined(HAVE_UNISTD_H) |
| 21 | # include <unistd.h> |
| 22 | #endif |
| 23 | #if defined(HAVE_FCNTL_H) |
| 24 | # include <fcntl.h> |
| 25 | #endif |
Argyrios Kyrtzidis | b68dc36 | 2008-08-17 09:25:21 +0000 | [diff] [blame] | 26 | |
| 27 | #if defined(_MSC_VER) |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 28 | #include <io.h> |
Cedric Venet | a3f343f | 2008-08-24 11:56:40 +0000 | [diff] [blame] | 29 | #include <fcntl.h> |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 30 | #ifndef STDIN_FILENO |
| 31 | # define STDIN_FILENO 0 |
| 32 | #endif |
| 33 | #ifndef STDOUT_FILENO |
| 34 | # define STDOUT_FILENO 1 |
| 35 | #endif |
| 36 | #ifndef STDERR_FILENO |
| 37 | # define STDERR_FILENO 2 |
| 38 | #endif |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 41 | using namespace llvm; |
| 42 | |
| 43 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 44 | // An out of line virtual method to provide a home for the class vtable. |
| 45 | void raw_ostream::handle() {} |
| 46 | |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 47 | raw_ostream &raw_ostream::operator<<(unsigned long N) { |
| 48 | // Zero is a special case. |
| 49 | if (N == 0) |
| 50 | return *this << '0'; |
| 51 | |
| 52 | char NumberBuffer[20]; |
| 53 | char *EndPtr = NumberBuffer+sizeof(NumberBuffer); |
| 54 | char *CurPtr = EndPtr; |
| 55 | |
| 56 | while (N) { |
| 57 | *--CurPtr = '0' + char(N % 10); |
| 58 | N /= 10; |
| 59 | } |
| 60 | return write(CurPtr, EndPtr-CurPtr); |
| 61 | } |
| 62 | |
| 63 | raw_ostream &raw_ostream::operator<<(long N) { |
| 64 | if (N < 0) { |
| 65 | if (OutBufCur >= OutBufEnd) |
| 66 | flush_impl(); |
| 67 | *OutBufCur++ = '-'; |
| 68 | |
| 69 | N = -N; |
| 70 | } |
| 71 | |
| 72 | return this->operator<<(static_cast<unsigned long>(N)); |
| 73 | } |
| 74 | |
| 75 | raw_ostream &raw_ostream::operator<<(unsigned long long N) { |
| 76 | // Zero is a special case. |
| 77 | if (N == 0) |
| 78 | return *this << '0'; |
| 79 | |
| 80 | char NumberBuffer[20]; |
| 81 | char *EndPtr = NumberBuffer+sizeof(NumberBuffer); |
| 82 | char *CurPtr = EndPtr; |
| 83 | |
| 84 | while (N) { |
| 85 | *--CurPtr = '0' + char(N % 10); |
| 86 | N /= 10; |
| 87 | } |
| 88 | return write(CurPtr, EndPtr-CurPtr); |
| 89 | } |
| 90 | |
| 91 | raw_ostream &raw_ostream::operator<<(long long N) { |
| 92 | if (N < 0) { |
| 93 | if (OutBufCur >= OutBufEnd) |
| 94 | flush_impl(); |
| 95 | *OutBufCur++ = '-'; |
| 96 | |
| 97 | N = -N; |
| 98 | } |
| 99 | |
| 100 | return this->operator<<(static_cast<unsigned long long>(N)); |
| 101 | } |
| 102 | |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 103 | raw_ostream &raw_ostream::operator<<(const void *P) { |
| 104 | // FIXME: This could be much faster if it matters. |
| 105 | return *this << format("%p", P); |
| 106 | } |
| 107 | |
| 108 | |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 109 | raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) { |
| 110 | if (OutBufCur+Size > OutBufEnd) |
| 111 | flush_impl(); |
| 112 | |
| 113 | // Handle short strings specially, memcpy isn't very good at very short |
| 114 | // strings. |
| 115 | switch (Size) { |
| 116 | case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH |
| 117 | case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH |
| 118 | case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH |
| 119 | case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH |
| 120 | case 0: break; |
| 121 | default: |
| 122 | // Normally the string to emit is shorter than the buffer. |
| 123 | if (Size <= unsigned(OutBufEnd-OutBufStart)) { |
| 124 | memcpy(OutBufCur, Ptr, Size); |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | // If emitting a string larger than our buffer, emit in chunks. In this |
| 129 | // case we know that we just flushed the buffer. |
| 130 | while (Size) { |
| 131 | unsigned NumToEmit = OutBufEnd-OutBufStart; |
| 132 | if (Size < NumToEmit) NumToEmit = Size; |
| 133 | assert(OutBufCur == OutBufStart); |
| 134 | memcpy(OutBufStart, Ptr, NumToEmit); |
| 135 | Ptr += NumToEmit; |
Owen Anderson | fd2a053 | 2008-08-21 22:39:33 +0000 | [diff] [blame] | 136 | Size -= NumToEmit; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 137 | OutBufCur = OutBufStart + NumToEmit; |
| 138 | flush_impl(); |
| 139 | } |
| 140 | break; |
| 141 | } |
| 142 | OutBufCur += Size; |
| 143 | return *this; |
| 144 | } |
| 145 | |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 146 | // Formatted output. |
| 147 | raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) { |
| 148 | // If we have more than a few bytes left in our output buffer, try formatting |
| 149 | // directly onto its end. |
| 150 | unsigned NextBufferSize = 127; |
| 151 | if (OutBufEnd-OutBufCur > 3) { |
| 152 | unsigned BufferBytesLeft = OutBufEnd-OutBufCur; |
| 153 | unsigned BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft); |
| 154 | |
| 155 | // Common case is that we have plenty of space. |
| 156 | if (BytesUsed < BufferBytesLeft) { |
| 157 | OutBufCur += BytesUsed; |
| 158 | return *this; |
| 159 | } |
| 160 | |
| 161 | // Otherwise, we overflowed and the return value tells us the size to try |
| 162 | // again with. |
| 163 | NextBufferSize = BytesUsed; |
| 164 | } |
| 165 | |
| 166 | // If we got here, we didn't have enough space in the output buffer for the |
| 167 | // string. Try printing into a SmallVector that is resized to have enough |
| 168 | // space. Iterate until we win. |
| 169 | SmallVector<char, 128> V; |
| 170 | |
| 171 | while (1) { |
| 172 | V.resize(NextBufferSize); |
| 173 | |
| 174 | // Try formatting into the SmallVector. |
| 175 | unsigned BytesUsed = Fmt.print(&V[0], NextBufferSize); |
| 176 | |
| 177 | // If BytesUsed fit into the vector, we win. |
| 178 | if (BytesUsed < NextBufferSize) |
| 179 | return write(&V[0], BytesUsed); |
| 180 | |
| 181 | // Otherwise, try again with a new size. |
| 182 | assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?"); |
| 183 | NextBufferSize = BytesUsed; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | //===----------------------------------------------------------------------===// |
| 188 | // Formatted Output |
| 189 | //===----------------------------------------------------------------------===// |
| 190 | |
| 191 | // Out of line virtual method. |
| 192 | void format_object_base::home() { |
| 193 | } |
| 194 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 195 | //===----------------------------------------------------------------------===// |
| 196 | // raw_fd_ostream |
| 197 | //===----------------------------------------------------------------------===// |
| 198 | |
Daniel Dunbar | 48534b3 | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 199 | /// raw_fd_ostream - Open the specified file for writing. If an error |
| 200 | /// occurs, information about the error is put into ErrorInfo, and the |
| 201 | /// stream should be immediately destroyed; the string will be empty |
| 202 | /// if no error occurred. |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 203 | raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo) { |
Daniel Dunbar | 48534b3 | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 204 | ErrorInfo.clear(); |
| 205 | |
Chris Lattner | c52b128 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 206 | // Handle "-" as stdout. |
| 207 | if (Filename[0] == '-' && Filename[1] == 0) { |
| 208 | FD = STDOUT_FILENO; |
| 209 | ShouldClose = false; |
| 210 | return; |
| 211 | } |
| 212 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 213 | FD = open(Filename, O_WRONLY|O_CREAT|O_TRUNC, 0644); |
| 214 | if (FD < 0) { |
| 215 | ErrorInfo = "Error opening output file '" + std::string(Filename) + "'"; |
| 216 | ShouldClose = false; |
| 217 | } else { |
| 218 | ShouldClose = true; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | raw_fd_ostream::~raw_fd_ostream() { |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame^] | 223 | if (FD >= 0) { |
| 224 | flush(); |
| 225 | if (ShouldClose) |
| 226 | ::close(FD); |
| 227 | } |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void raw_fd_ostream::flush_impl() { |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame^] | 231 | assert (FD >= 0 && "File already closed."); |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 232 | if (OutBufCur-OutBufStart) |
Chris Lattner | d497df5 | 2008-08-17 01:46:05 +0000 | [diff] [blame] | 233 | ::write(FD, OutBufStart, OutBufCur-OutBufStart); |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 234 | HandleFlush(); |
| 235 | } |
| 236 | |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame^] | 237 | void raw_fd_ostream::close() { |
| 238 | assert (ShouldClose); |
| 239 | ShouldClose = false; |
| 240 | flush(); |
| 241 | ::close(FD); |
| 242 | FD = -1; |
| 243 | } |
| 244 | |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 245 | //===----------------------------------------------------------------------===// |
| 246 | // raw_stdout/err_ostream |
| 247 | //===----------------------------------------------------------------------===// |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 248 | |
| 249 | raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {} |
| 250 | raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false) {} |
| 251 | |
| 252 | // An out of line virtual method to provide a home for the class vtable. |
| 253 | void raw_stdout_ostream::handle() {} |
| 254 | void raw_stderr_ostream::handle() {} |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 255 | |
| 256 | /// outs() - This returns a reference to a raw_ostream for standard output. |
| 257 | /// Use it like: outs() << "foo" << "bar"; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 258 | raw_ostream &llvm::outs() { |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 259 | static raw_stdout_ostream S; |
| 260 | return S; |
| 261 | } |
| 262 | |
| 263 | /// errs() - This returns a reference to a raw_ostream for standard error. |
| 264 | /// Use it like: errs() << "foo" << "bar"; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 265 | raw_ostream &llvm::errs() { |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 266 | static raw_stderr_ostream S; |
| 267 | return S; |
| 268 | } |
| 269 | |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | // raw_os_ostream |
| 272 | //===----------------------------------------------------------------------===// |
| 273 | |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 274 | raw_os_ostream::~raw_os_ostream() { |
| 275 | flush(); |
| 276 | } |
| 277 | |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 278 | /// flush_impl - The is the piece of the class that is implemented by |
| 279 | /// subclasses. This outputs the currently buffered data and resets the |
| 280 | /// buffer to empty. |
| 281 | void raw_os_ostream::flush_impl() { |
| 282 | if (OutBufCur-OutBufStart) |
| 283 | OS.write(OutBufStart, OutBufCur-OutBufStart); |
| 284 | HandleFlush(); |
| 285 | } |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 286 | |
| 287 | //===----------------------------------------------------------------------===// |
| 288 | // raw_string_ostream |
| 289 | //===----------------------------------------------------------------------===// |
| 290 | |
| 291 | raw_string_ostream::~raw_string_ostream() { |
| 292 | flush(); |
| 293 | } |
| 294 | |
| 295 | /// flush_impl - The is the piece of the class that is implemented by |
| 296 | /// subclasses. This outputs the currently buffered data and resets the |
| 297 | /// buffer to empty. |
| 298 | void raw_string_ostream::flush_impl() { |
| 299 | if (OutBufCur-OutBufStart) |
| 300 | OS.append(OutBufStart, OutBufCur-OutBufStart); |
| 301 | HandleFlush(); |
| 302 | } |
| 303 | |
| 304 | //===----------------------------------------------------------------------===// |
| 305 | // raw_svector_ostream |
| 306 | //===----------------------------------------------------------------------===// |
| 307 | |
| 308 | raw_svector_ostream::~raw_svector_ostream() { |
| 309 | flush(); |
| 310 | } |
| 311 | |
| 312 | /// flush_impl - The is the piece of the class that is implemented by |
| 313 | /// subclasses. This outputs the currently buffered data and resets the |
| 314 | /// buffer to empty. |
| 315 | void raw_svector_ostream::flush_impl() { |
| 316 | if (OutBufCur-OutBufStart) |
| 317 | OS.append(OutBufStart, OutBufCur); |
| 318 | HandleFlush(); |
| 319 | } |
| 320 | |