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> |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 26 | #if defined(HAVE_UNISTD_H) |
| 27 | # include <unistd.h> |
| 28 | #endif |
| 29 | #if defined(HAVE_FCNTL_H) |
| 30 | # include <fcntl.h> |
| 31 | #endif |
Argyrios Kyrtzidis | b68dc36 | 2008-08-17 09:25:21 +0000 | [diff] [blame] | 32 | |
| 33 | #if defined(_MSC_VER) |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 34 | #include <io.h> |
Cedric Venet | a3f343f | 2008-08-24 11:56:40 +0000 | [diff] [blame] | 35 | #include <fcntl.h> |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 36 | #ifndef STDIN_FILENO |
| 37 | # define STDIN_FILENO 0 |
| 38 | #endif |
| 39 | #ifndef STDOUT_FILENO |
| 40 | # define STDOUT_FILENO 1 |
| 41 | #endif |
| 42 | #ifndef STDERR_FILENO |
| 43 | # define STDERR_FILENO 2 |
| 44 | #endif |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 45 | #endif |
| 46 | |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 47 | using namespace llvm; |
| 48 | |
Dan Gohman | e87b2ab | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 49 | raw_ostream::~raw_ostream() { |
Dan Gohman | 9a31254 | 2009-07-27 20:49:44 +0000 | [diff] [blame] | 50 | // raw_ostream's subclasses should take care to flush the buffer |
| 51 | // in their destructors. |
| 52 | assert(OutBufCur == OutBufStart && |
| 53 | "raw_ostream destructor called with non-empty buffer!"); |
| 54 | |
Dan Gohman | e87b2ab | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 55 | delete [] OutBufStart; |
| 56 | |
| 57 | // If there are any pending errors, report them now. Clients wishing |
| 58 | // to avoid llvm_report_error calls should check for errors with |
| 59 | // has_error() and clear the error flag with clear_error() before |
| 60 | // destructing raw_ostream objects which may have errors. |
| 61 | if (Error) |
| 62 | llvm_report_error("IO failure on output stream."); |
| 63 | } |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 65 | // An out of line virtual method to provide a home for the class vtable. |
| 66 | void raw_ostream::handle() {} |
| 67 | |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 68 | size_t raw_ostream::preferred_buffer_size() { |
| 69 | // BUFSIZ is intended to be a reasonable default. |
| 70 | return BUFSIZ; |
| 71 | } |
| 72 | |
| 73 | void raw_ostream::SetBuffered() { |
| 74 | // Ask the subclass to determine an appropriate buffer size. |
Dan Gohman | d7be663 | 2009-08-15 02:05:19 +0000 | [diff] [blame] | 75 | if (size_t Size = preferred_buffer_size()) |
| 76 | SetBufferSize(Size); |
| 77 | else |
| 78 | // It may return 0, meaning this stream should be unbuffered. |
| 79 | SetUnbuffered(); |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Dan Gohman | 524dea4 | 2009-08-13 15:58:55 +0000 | [diff] [blame] | 82 | void raw_ostream::SetBufferSize(size_t Size) { |
| 83 | assert(Size >= 64 && |
| 84 | "Buffer size must be somewhat large for invariants to hold"); |
| 85 | flush(); |
| 86 | |
| 87 | delete [] OutBufStart; |
| 88 | OutBufStart = new char[Size]; |
| 89 | OutBufEnd = OutBufStart+Size; |
| 90 | OutBufCur = OutBufStart; |
| 91 | Unbuffered = false; |
| 92 | } |
| 93 | |
| 94 | void raw_ostream::SetUnbuffered() { |
| 95 | flush(); |
| 96 | |
| 97 | delete [] OutBufStart; |
| 98 | OutBufStart = OutBufEnd = OutBufCur = 0; |
| 99 | Unbuffered = true; |
| 100 | } |
| 101 | |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 102 | raw_ostream &raw_ostream::operator<<(unsigned long N) { |
| 103 | // Zero is a special case. |
| 104 | if (N == 0) |
| 105 | return *this << '0'; |
| 106 | |
| 107 | char NumberBuffer[20]; |
| 108 | char *EndPtr = NumberBuffer+sizeof(NumberBuffer); |
| 109 | char *CurPtr = EndPtr; |
| 110 | |
| 111 | while (N) { |
| 112 | *--CurPtr = '0' + char(N % 10); |
| 113 | N /= 10; |
| 114 | } |
| 115 | return write(CurPtr, EndPtr-CurPtr); |
| 116 | } |
| 117 | |
| 118 | raw_ostream &raw_ostream::operator<<(long N) { |
| 119 | if (N < 0) { |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 120 | *this << '-'; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 121 | N = -N; |
| 122 | } |
| 123 | |
| 124 | return this->operator<<(static_cast<unsigned long>(N)); |
| 125 | } |
| 126 | |
| 127 | raw_ostream &raw_ostream::operator<<(unsigned long long N) { |
Daniel Dunbar | 3b3de92 | 2009-08-18 22:24:00 +0000 | [diff] [blame] | 128 | // Handle simple case when value fits in long already. |
Daniel Dunbar | 78a3dd8 | 2009-07-29 06:45:14 +0000 | [diff] [blame] | 129 | if (N == static_cast<unsigned long>(N)) |
| 130 | return this->operator<<(static_cast<unsigned long>(N)); |
| 131 | |
Daniel Dunbar | 3b3de92 | 2009-08-18 22:24:00 +0000 | [diff] [blame] | 132 | // Otherwise divide into at two or three 10**9 chunks and write out using |
| 133 | // long div/mod, this is substantially faster on a 32-bit system. |
| 134 | unsigned long Top = 0, Mid = 0, Bot = N % 1000000000; |
| 135 | N /= 1000000000; |
| 136 | if (N > 1000000000) { |
| 137 | Mid = N % 1000000000; |
| 138 | Top = N / 1000000000; |
| 139 | } else |
| 140 | Mid = N; |
| 141 | |
| 142 | if (Top) |
| 143 | this->operator<<(static_cast<unsigned long>(Top)); |
| 144 | this->operator<<(static_cast<unsigned long>(Mid)); |
| 145 | return this->operator<<(static_cast<unsigned long>(Bot)); |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | raw_ostream &raw_ostream::operator<<(long long N) { |
| 149 | if (N < 0) { |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 150 | *this << '-'; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 151 | N = -N; |
| 152 | } |
| 153 | |
| 154 | return this->operator<<(static_cast<unsigned long long>(N)); |
| 155 | } |
| 156 | |
Daniel Dunbar | 48018e0 | 2009-07-30 18:21:23 +0000 | [diff] [blame] | 157 | raw_ostream &raw_ostream::write_hex(unsigned long long N) { |
Daniel Dunbar | b451a0c | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 158 | // Zero is a special case. |
| 159 | if (N == 0) |
| 160 | return *this << '0'; |
| 161 | |
| 162 | char NumberBuffer[20]; |
| 163 | char *EndPtr = NumberBuffer+sizeof(NumberBuffer); |
| 164 | char *CurPtr = EndPtr; |
| 165 | |
| 166 | while (N) { |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 167 | uintptr_t x = N % 16; |
Daniel Dunbar | b451a0c | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 168 | *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10); |
| 169 | N /= 16; |
| 170 | } |
| 171 | |
| 172 | return write(CurPtr, EndPtr-CurPtr); |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Daniel Dunbar | 48018e0 | 2009-07-30 18:21:23 +0000 | [diff] [blame] | 175 | raw_ostream &raw_ostream::operator<<(const void *P) { |
| 176 | *this << '0' << 'x'; |
| 177 | |
| 178 | return write_hex((uintptr_t) P); |
| 179 | } |
| 180 | |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame] | 181 | void raw_ostream::flush_nonempty() { |
| 182 | assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty."); |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 183 | write_impl(OutBufStart, OutBufCur - OutBufStart); |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame] | 184 | OutBufCur = OutBufStart; |
| 185 | } |
| 186 | |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 187 | raw_ostream &raw_ostream::write(unsigned char C) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 188 | // Group exceptional cases into a single branch. |
| 189 | if (OutBufCur >= OutBufEnd) { |
| 190 | if (Unbuffered) { |
| 191 | write_impl(reinterpret_cast<char*>(&C), 1); |
| 192 | return *this; |
| 193 | } |
| 194 | |
Dan Gohman | 3731604 | 2009-08-18 20:09:59 +0000 | [diff] [blame] | 195 | if (OutBufStart) |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 196 | flush_nonempty(); |
Dan Gohman | 3731604 | 2009-08-18 20:09:59 +0000 | [diff] [blame] | 197 | else { |
| 198 | SetBuffered(); |
| 199 | // It's possible for the underlying stream to decline |
| 200 | // buffering, so check this condition again. |
| 201 | if (Unbuffered) { |
| 202 | write_impl(reinterpret_cast<char*>(&C), 1); |
| 203 | return *this; |
| 204 | } |
| 205 | } |
Daniel Dunbar | d17d74b | 2009-03-17 01:13:35 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 208 | *OutBufCur++ = C; |
Daniel Dunbar | b451a0c | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 209 | return *this; |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 210 | } |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 211 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 212 | raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 213 | // Group exceptional cases into a single branch. |
Daniel Dunbar | b372c11 | 2009-03-17 21:15:18 +0000 | [diff] [blame] | 214 | if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) { |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 215 | if (BUILTIN_EXPECT(!OutBufStart, false)) { |
| 216 | if (Unbuffered) { |
| 217 | write_impl(Ptr, Size); |
| 218 | return *this; |
| 219 | } |
| 220 | // Set up a buffer and start over. |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 221 | SetBuffered(); |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 222 | return write(Ptr, Size); |
| 223 | } |
| 224 | // Write out the data in buffer-sized blocks until the remainder |
| 225 | // fits within the buffer. |
| 226 | do { |
| 227 | size_t NumBytes = OutBufEnd - OutBufCur; |
| 228 | copy_to_buffer(Ptr, NumBytes); |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 229 | flush_nonempty(); |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 230 | Ptr += NumBytes; |
| 231 | Size -= NumBytes; |
| 232 | } while (OutBufCur+Size > OutBufEnd); |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 233 | } |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 234 | |
| 235 | copy_to_buffer(Ptr, Size); |
| 236 | |
| 237 | return *this; |
| 238 | } |
| 239 | |
| 240 | void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) { |
Dan Gohman | c6126e8 | 2009-08-13 20:32:03 +0000 | [diff] [blame] | 241 | assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!"); |
Dan Gohman | 4e8e642 | 2009-08-13 18:38:15 +0000 | [diff] [blame] | 242 | |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 243 | // Handle short strings specially, memcpy isn't very good at very short |
| 244 | // strings. |
| 245 | switch (Size) { |
| 246 | case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH |
| 247 | case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH |
| 248 | case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH |
| 249 | case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH |
| 250 | case 0: break; |
| 251 | default: |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 252 | memcpy(OutBufCur, Ptr, Size); |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 253 | break; |
| 254 | } |
Daniel Dunbar | e77e434 | 2009-03-10 16:21:55 +0000 | [diff] [blame] | 255 | |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 256 | OutBufCur += Size; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 259 | // Formatted output. |
| 260 | raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 261 | // If we have more than a few bytes left in our output buffer, try |
| 262 | // formatting directly onto its end. |
| 263 | // |
| 264 | // FIXME: This test is a bit silly, since if we don't have enough |
| 265 | // space in the buffer we will have to flush the formatted output |
| 266 | // anyway. We should just flush upfront in such cases, and use the |
| 267 | // whole buffer as our scratch pad. Note, however, that this case is |
| 268 | // also necessary for correctness on unbuffered streams. |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 269 | size_t NextBufferSize = 127; |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 270 | if (OutBufEnd-OutBufCur > 3) { |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 271 | size_t BufferBytesLeft = OutBufEnd-OutBufCur; |
| 272 | size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft); |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 273 | |
| 274 | // Common case is that we have plenty of space. |
| 275 | if (BytesUsed < BufferBytesLeft) { |
| 276 | OutBufCur += BytesUsed; |
| 277 | return *this; |
| 278 | } |
| 279 | |
| 280 | // Otherwise, we overflowed and the return value tells us the size to try |
| 281 | // again with. |
| 282 | NextBufferSize = BytesUsed; |
| 283 | } |
| 284 | |
| 285 | // If we got here, we didn't have enough space in the output buffer for the |
| 286 | // string. Try printing into a SmallVector that is resized to have enough |
| 287 | // space. Iterate until we win. |
| 288 | SmallVector<char, 128> V; |
| 289 | |
| 290 | while (1) { |
| 291 | V.resize(NextBufferSize); |
| 292 | |
| 293 | // Try formatting into the SmallVector. |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 294 | size_t BytesUsed = Fmt.print(&V[0], NextBufferSize); |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 295 | |
| 296 | // If BytesUsed fit into the vector, we win. |
Chris Lattner | f61ca1e | 2008-10-26 19:20:47 +0000 | [diff] [blame] | 297 | if (BytesUsed <= NextBufferSize) |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 298 | return write(&V[0], BytesUsed); |
| 299 | |
| 300 | // Otherwise, try again with a new size. |
| 301 | assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?"); |
| 302 | NextBufferSize = BytesUsed; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | //===----------------------------------------------------------------------===// |
| 307 | // Formatted Output |
| 308 | //===----------------------------------------------------------------------===// |
| 309 | |
| 310 | // Out of line virtual method. |
| 311 | void format_object_base::home() { |
| 312 | } |
| 313 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 314 | //===----------------------------------------------------------------------===// |
| 315 | // raw_fd_ostream |
| 316 | //===----------------------------------------------------------------------===// |
| 317 | |
Daniel Dunbar | 48534b3 | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 318 | /// raw_fd_ostream - Open the specified file for writing. If an error |
| 319 | /// occurs, information about the error is put into ErrorInfo, and the |
| 320 | /// stream should be immediately destroyed; the string will be empty |
| 321 | /// if no error occurred. |
Dan Gohman | a1bdced | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 322 | raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, bool Force, |
Ted Kremenek | d75ba1c | 2008-12-04 22:51:11 +0000 | [diff] [blame] | 323 | std::string &ErrorInfo) : pos(0) { |
Daniel Dunbar | 48534b3 | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 324 | ErrorInfo.clear(); |
| 325 | |
Chris Lattner | c52b128 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 326 | // Handle "-" as stdout. |
| 327 | if (Filename[0] == '-' && Filename[1] == 0) { |
| 328 | FD = STDOUT_FILENO; |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 329 | // If user requested binary then put stdout into binary mode if |
| 330 | // possible. |
| 331 | if (Binary) |
| 332 | sys::Program::ChangeStdoutToBinary(); |
Chris Lattner | c52b128 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 333 | ShouldClose = false; |
| 334 | return; |
| 335 | } |
| 336 | |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 337 | int Flags = O_WRONLY|O_CREAT|O_TRUNC; |
| 338 | #ifdef O_BINARY |
| 339 | if (Binary) |
| 340 | Flags |= O_BINARY; |
| 341 | #endif |
Dan Gohman | a1bdced | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 342 | if (!Force) |
| 343 | Flags |= O_EXCL; |
Dan Gohman | 9f79d3e | 2009-07-15 16:39:40 +0000 | [diff] [blame] | 344 | FD = open(Filename, Flags, 0664); |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 345 | if (FD < 0) { |
| 346 | ErrorInfo = "Error opening output file '" + std::string(Filename) + "'"; |
| 347 | ShouldClose = false; |
| 348 | } else { |
| 349 | ShouldClose = true; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | raw_fd_ostream::~raw_fd_ostream() { |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 354 | if (FD >= 0) { |
| 355 | flush(); |
| 356 | if (ShouldClose) |
Dan Gohman | d3a974f | 2009-07-15 16:43:01 +0000 | [diff] [blame] | 357 | if (::close(FD) != 0) |
Dan Gohman | e87b2ab | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 358 | error_detected(); |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 359 | } |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 362 | void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) { |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 363 | assert (FD >= 0 && "File already closed."); |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 364 | pos += Size; |
Daniel Dunbar | 579fb87 | 2009-07-15 08:11:46 +0000 | [diff] [blame] | 365 | if (::write(FD, Ptr, Size) != (ssize_t) Size) |
Dan Gohman | e87b2ab | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 366 | error_detected(); |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 369 | void raw_fd_ostream::close() { |
| 370 | assert (ShouldClose); |
| 371 | ShouldClose = false; |
| 372 | flush(); |
Dan Gohman | d3a974f | 2009-07-15 16:43:01 +0000 | [diff] [blame] | 373 | if (::close(FD) != 0) |
Dan Gohman | e87b2ab | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 374 | error_detected(); |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 375 | FD = -1; |
| 376 | } |
| 377 | |
Ted Kremenek | 9c27886 | 2009-01-26 21:42:04 +0000 | [diff] [blame] | 378 | uint64_t raw_fd_ostream::seek(uint64_t off) { |
| 379 | flush(); |
Daniel Dunbar | 579fb87 | 2009-07-15 08:11:46 +0000 | [diff] [blame] | 380 | pos = ::lseek(FD, off, SEEK_SET); |
Dan Gohman | d3a974f | 2009-07-15 16:43:01 +0000 | [diff] [blame] | 381 | if (pos != off) |
Dan Gohman | e87b2ab | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 382 | error_detected(); |
Ted Kremenek | 9c27886 | 2009-01-26 21:42:04 +0000 | [diff] [blame] | 383 | return pos; |
| 384 | } |
| 385 | |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 386 | size_t raw_fd_ostream::preferred_buffer_size() { |
Dan Gohman | b4741a7 | 2009-08-15 21:41:03 +0000 | [diff] [blame] | 387 | #if !defined(_MSC_VER) && !defined(__MINGW32__) // Windows has no st_blksize. |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 388 | assert(FD >= 0 && "File not yet open!"); |
| 389 | struct stat statbuf; |
Dan Gohman | d7be663 | 2009-08-15 02:05:19 +0000 | [diff] [blame] | 390 | if (fstat(FD, &statbuf) == 0) { |
| 391 | // If this is a terminal, don't use buffering. Line buffering |
| 392 | // would be a more traditional thing to do, but it's not worth |
| 393 | // the complexity. |
| 394 | if (S_ISCHR(statbuf.st_mode) && isatty(FD)) |
| 395 | return 0; |
| 396 | // Return the preferred block size. |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 397 | return statbuf.st_blksize; |
Dan Gohman | d7be663 | 2009-08-15 02:05:19 +0000 | [diff] [blame] | 398 | } |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 399 | error_detected(); |
| 400 | #endif |
| 401 | return raw_ostream::preferred_buffer_size(); |
| 402 | } |
| 403 | |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 404 | raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold, |
| 405 | bool bg) { |
| 406 | if (sys::Process::ColorNeedsFlush()) |
| 407 | flush(); |
| 408 | const char *colorcode = |
| 409 | (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg) |
| 410 | : sys::Process::OutputColor(colors, bold, bg); |
| 411 | if (colorcode) { |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 412 | size_t len = strlen(colorcode); |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 413 | write(colorcode, len); |
| 414 | // don't account colors towards output characters |
| 415 | pos -= len; |
| 416 | } |
| 417 | return *this; |
| 418 | } |
| 419 | |
| 420 | raw_ostream &raw_fd_ostream::resetColor() { |
| 421 | if (sys::Process::ColorNeedsFlush()) |
| 422 | flush(); |
| 423 | const char *colorcode = sys::Process::ResetColor(); |
| 424 | if (colorcode) { |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 425 | size_t len = strlen(colorcode); |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 426 | write(colorcode, len); |
| 427 | // don't account colors towards output characters |
| 428 | pos -= len; |
| 429 | } |
| 430 | return *this; |
| 431 | } |
| 432 | |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 433 | //===----------------------------------------------------------------------===// |
| 434 | // raw_stdout/err_ostream |
| 435 | //===----------------------------------------------------------------------===// |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 436 | |
Dan Gohman | ec9b261 | 2009-08-13 23:18:56 +0000 | [diff] [blame] | 437 | // Set buffer settings to model stdout and stderr behavior. |
Dan Gohman | d7be663 | 2009-08-15 02:05:19 +0000 | [diff] [blame] | 438 | // Set standard error to be unbuffered by default. |
| 439 | raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {} |
| 440 | raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false, |
Daniel Dunbar | e77e434 | 2009-03-10 16:21:55 +0000 | [diff] [blame] | 441 | true) {} |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 442 | |
| 443 | // An out of line virtual method to provide a home for the class vtable. |
| 444 | void raw_stdout_ostream::handle() {} |
| 445 | void raw_stderr_ostream::handle() {} |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 446 | |
| 447 | /// outs() - This returns a reference to a raw_ostream for standard output. |
| 448 | /// Use it like: outs() << "foo" << "bar"; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 449 | raw_ostream &llvm::outs() { |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 450 | static raw_stdout_ostream S; |
| 451 | return S; |
| 452 | } |
| 453 | |
| 454 | /// errs() - This returns a reference to a raw_ostream for standard error. |
| 455 | /// Use it like: errs() << "foo" << "bar"; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 456 | raw_ostream &llvm::errs() { |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 457 | static raw_stderr_ostream S; |
| 458 | return S; |
| 459 | } |
| 460 | |
Daniel Dunbar | 34ccf03 | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 461 | /// nulls() - This returns a reference to a raw_ostream which discards output. |
| 462 | raw_ostream &llvm::nulls() { |
| 463 | static raw_null_ostream S; |
| 464 | return S; |
| 465 | } |
| 466 | |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 467 | //===----------------------------------------------------------------------===// |
| 468 | // raw_os_ostream |
| 469 | //===----------------------------------------------------------------------===// |
| 470 | |
Daniel Dunbar | 35979c0 | 2009-08-18 20:07:36 +0000 | [diff] [blame] | 471 | raw_os_ostream::~raw_os_ostream() { |
| 472 | flush(); |
| 473 | } |
| 474 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 475 | void raw_os_ostream::write_impl(const char *Ptr, size_t Size) { |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 476 | OS.write(Ptr, Size); |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 477 | } |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 478 | |
Douglas Gregor | 8f7be47 | 2009-04-20 07:34:17 +0000 | [diff] [blame] | 479 | uint64_t raw_os_ostream::current_pos() { return OS.tellp(); } |
| 480 | |
| 481 | uint64_t raw_os_ostream::tell() { |
| 482 | return (uint64_t)OS.tellp() + GetNumBytesInBuffer(); |
| 483 | } |
| 484 | |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 485 | //===----------------------------------------------------------------------===// |
| 486 | // raw_string_ostream |
| 487 | //===----------------------------------------------------------------------===// |
| 488 | |
Daniel Dunbar | 35979c0 | 2009-08-18 20:07:36 +0000 | [diff] [blame] | 489 | raw_string_ostream::~raw_string_ostream() { |
| 490 | flush(); |
| 491 | } |
| 492 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 493 | void raw_string_ostream::write_impl(const char *Ptr, size_t Size) { |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 494 | OS.append(Ptr, Size); |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | //===----------------------------------------------------------------------===// |
| 498 | // raw_svector_ostream |
| 499 | //===----------------------------------------------------------------------===// |
| 500 | |
Daniel Dunbar | 35979c0 | 2009-08-18 20:07:36 +0000 | [diff] [blame] | 501 | raw_svector_ostream::~raw_svector_ostream() { |
| 502 | flush(); |
| 503 | } |
| 504 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 505 | void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) { |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 506 | OS.append(Ptr, Ptr + Size); |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Douglas Gregor | 8f7be47 | 2009-04-20 07:34:17 +0000 | [diff] [blame] | 509 | uint64_t raw_svector_ostream::current_pos() { return OS.size(); } |
| 510 | |
| 511 | uint64_t raw_svector_ostream::tell() { |
| 512 | return OS.size() + GetNumBytesInBuffer(); |
| 513 | } |
Daniel Dunbar | 34ccf03 | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 514 | |
| 515 | //===----------------------------------------------------------------------===// |
| 516 | // raw_null_ostream |
| 517 | //===----------------------------------------------------------------------===// |
| 518 | |
Dan Gohman | f78c835 | 2009-07-27 21:46:02 +0000 | [diff] [blame] | 519 | raw_null_ostream::~raw_null_ostream() { |
| 520 | #ifndef NDEBUG |
| 521 | // ~raw_ostream asserts that the buffer is empty. This isn't necessary |
| 522 | // with raw_null_ostream, but it's better to have raw_null_ostream follow |
| 523 | // the rules than to change the rules just for raw_null_ostream. |
| 524 | flush(); |
| 525 | #endif |
| 526 | } |
| 527 | |
Daniel Dunbar | 34ccf03 | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 528 | void raw_null_ostream::write_impl(const char *Ptr, size_t Size) { |
| 529 | } |
| 530 | |
| 531 | uint64_t raw_null_ostream::current_pos() { |
| 532 | return 0; |
| 533 | } |