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