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