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; |
Daniel Dunbar | 425d08c | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 98 | |
| 99 | assert(OutBufStart <= OutBufEnd && "Invalid size!"); |
Dan Gohman | 524dea4 | 2009-08-13 15:58:55 +0000 | [diff] [blame] | 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 | ecbd0bc | 2009-08-19 16:25:25 +0000 | [diff] [blame] | 128 | // Output using 32-bit div/mod when possible. |
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 | ecbd0bc | 2009-08-19 16:25:25 +0000 | [diff] [blame] | 132 | char NumberBuffer[20]; |
| 133 | char *EndPtr = NumberBuffer+sizeof(NumberBuffer); |
| 134 | char *CurPtr = EndPtr; |
| 135 | |
| 136 | while (N) { |
| 137 | *--CurPtr = '0' + char(N % 10); |
| 138 | N /= 10; |
| 139 | } |
| 140 | return write(CurPtr, EndPtr-CurPtr); |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | raw_ostream &raw_ostream::operator<<(long long N) { |
| 144 | if (N < 0) { |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 145 | *this << '-'; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 146 | N = -N; |
| 147 | } |
| 148 | |
| 149 | return this->operator<<(static_cast<unsigned long long>(N)); |
| 150 | } |
| 151 | |
Daniel Dunbar | 48018e0 | 2009-07-30 18:21:23 +0000 | [diff] [blame] | 152 | raw_ostream &raw_ostream::write_hex(unsigned long long N) { |
Daniel Dunbar | b451a0c | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 153 | // Zero is a special case. |
| 154 | if (N == 0) |
| 155 | return *this << '0'; |
| 156 | |
| 157 | char NumberBuffer[20]; |
| 158 | char *EndPtr = NumberBuffer+sizeof(NumberBuffer); |
| 159 | char *CurPtr = EndPtr; |
| 160 | |
| 161 | while (N) { |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 162 | uintptr_t x = N % 16; |
Daniel Dunbar | b451a0c | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 163 | *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10); |
| 164 | N /= 16; |
| 165 | } |
| 166 | |
| 167 | return write(CurPtr, EndPtr-CurPtr); |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Daniel Dunbar | 48018e0 | 2009-07-30 18:21:23 +0000 | [diff] [blame] | 170 | raw_ostream &raw_ostream::operator<<(const void *P) { |
| 171 | *this << '0' << 'x'; |
| 172 | |
| 173 | return write_hex((uintptr_t) P); |
| 174 | } |
| 175 | |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame] | 176 | void raw_ostream::flush_nonempty() { |
| 177 | assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty."); |
Daniel Dunbar | 906d5b4 | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 178 | size_t Length = OutBufCur - OutBufStart; |
| 179 | OutBufCur = OutBufStart; |
| 180 | write_impl(OutBufStart, Length); |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 183 | raw_ostream &raw_ostream::write(unsigned char C) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 184 | // Group exceptional cases into a single branch. |
Daniel Dunbar | ecc67e2 | 2009-08-19 00:23:39 +0000 | [diff] [blame] | 185 | if (BUILTIN_EXPECT(OutBufCur >= OutBufEnd, false)) { |
| 186 | if (BUILTIN_EXPECT(!OutBufStart, false)) { |
Daniel Dunbar | 906d5b4 | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 187 | if (BufferMode == Unbuffered) { |
Dan Gohman | 3731604 | 2009-08-18 20:09:59 +0000 | [diff] [blame] | 188 | write_impl(reinterpret_cast<char*>(&C), 1); |
| 189 | return *this; |
| 190 | } |
Daniel Dunbar | ecc67e2 | 2009-08-19 00:23:39 +0000 | [diff] [blame] | 191 | // Set up a buffer and start over. |
| 192 | SetBuffered(); |
| 193 | return write(C); |
Dan Gohman | 3731604 | 2009-08-18 20:09:59 +0000 | [diff] [blame] | 194 | } |
Daniel Dunbar | ecc67e2 | 2009-08-19 00:23:39 +0000 | [diff] [blame] | 195 | |
| 196 | flush_nonempty(); |
Daniel Dunbar | d17d74b | 2009-03-17 01:13:35 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 199 | *OutBufCur++ = C; |
Daniel Dunbar | b451a0c | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 200 | return *this; |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 201 | } |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 202 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 203 | raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 204 | // Group exceptional cases into a single branch. |
Daniel Dunbar | b372c11 | 2009-03-17 21:15:18 +0000 | [diff] [blame] | 205 | if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) { |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 206 | if (BUILTIN_EXPECT(!OutBufStart, false)) { |
Daniel Dunbar | 906d5b4 | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 207 | if (BufferMode == Unbuffered) { |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 208 | write_impl(Ptr, Size); |
| 209 | return *this; |
| 210 | } |
| 211 | // Set up a buffer and start over. |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 212 | SetBuffered(); |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 213 | return write(Ptr, Size); |
| 214 | } |
Daniel Dunbar | ecc67e2 | 2009-08-19 00:23:39 +0000 | [diff] [blame] | 215 | |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 216 | // Write out the data in buffer-sized blocks until the remainder |
| 217 | // fits within the buffer. |
| 218 | do { |
| 219 | size_t NumBytes = OutBufEnd - OutBufCur; |
| 220 | copy_to_buffer(Ptr, NumBytes); |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 221 | flush_nonempty(); |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 222 | Ptr += NumBytes; |
| 223 | Size -= NumBytes; |
| 224 | } while (OutBufCur+Size > OutBufEnd); |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 225 | } |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 226 | |
| 227 | copy_to_buffer(Ptr, Size); |
| 228 | |
| 229 | return *this; |
| 230 | } |
| 231 | |
| 232 | void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) { |
Dan Gohman | c6126e8 | 2009-08-13 20:32:03 +0000 | [diff] [blame] | 233 | assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!"); |
Dan Gohman | 4e8e642 | 2009-08-13 18:38:15 +0000 | [diff] [blame] | 234 | |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 235 | // Handle short strings specially, memcpy isn't very good at very short |
| 236 | // strings. |
| 237 | switch (Size) { |
| 238 | case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH |
| 239 | case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH |
| 240 | case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH |
| 241 | case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH |
| 242 | case 0: break; |
| 243 | default: |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 244 | memcpy(OutBufCur, Ptr, Size); |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 245 | break; |
| 246 | } |
Daniel Dunbar | e77e434 | 2009-03-10 16:21:55 +0000 | [diff] [blame] | 247 | |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 248 | OutBufCur += Size; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 251 | // Formatted output. |
| 252 | raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 253 | // If we have more than a few bytes left in our output buffer, try |
| 254 | // formatting directly onto its end. |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 255 | size_t NextBufferSize = 127; |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 256 | if (OutBufEnd-OutBufCur > 3) { |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 257 | size_t BufferBytesLeft = OutBufEnd-OutBufCur; |
| 258 | size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft); |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 259 | |
| 260 | // Common case is that we have plenty of space. |
| 261 | if (BytesUsed < BufferBytesLeft) { |
| 262 | OutBufCur += BytesUsed; |
| 263 | return *this; |
| 264 | } |
| 265 | |
| 266 | // Otherwise, we overflowed and the return value tells us the size to try |
| 267 | // again with. |
| 268 | NextBufferSize = BytesUsed; |
| 269 | } |
| 270 | |
| 271 | // If we got here, we didn't have enough space in the output buffer for the |
| 272 | // string. Try printing into a SmallVector that is resized to have enough |
| 273 | // space. Iterate until we win. |
| 274 | SmallVector<char, 128> V; |
| 275 | |
| 276 | while (1) { |
| 277 | V.resize(NextBufferSize); |
| 278 | |
| 279 | // Try formatting into the SmallVector. |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 280 | size_t BytesUsed = Fmt.print(&V[0], NextBufferSize); |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 281 | |
| 282 | // If BytesUsed fit into the vector, we win. |
Chris Lattner | f61ca1e | 2008-10-26 19:20:47 +0000 | [diff] [blame] | 283 | if (BytesUsed <= NextBufferSize) |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 284 | return write(&V[0], BytesUsed); |
| 285 | |
| 286 | // Otherwise, try again with a new size. |
| 287 | assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?"); |
| 288 | NextBufferSize = BytesUsed; |
| 289 | } |
| 290 | } |
| 291 | |
Chris Lattner | c5a227d | 2009-08-22 23:10:29 +0000 | [diff] [blame] | 292 | /// indent - Insert 'NumSpaces' spaces. |
| 293 | raw_ostream &raw_ostream::indent(unsigned NumSpaces) { |
| 294 | const char *Spaces = " "; |
| 295 | |
| 296 | // Usually the indentation is small, handle it with a fastpath. |
| 297 | if (NumSpaces <= 16) |
| 298 | return write(Spaces, NumSpaces); |
| 299 | |
| 300 | while (NumSpaces) { |
| 301 | unsigned NumToWrite = std::min(NumSpaces, 16U); |
| 302 | write(Spaces, NumToWrite); |
| 303 | NumSpaces -= NumToWrite; |
| 304 | } |
| 305 | return *this; |
| 306 | } |
| 307 | |
| 308 | |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 309 | //===----------------------------------------------------------------------===// |
| 310 | // Formatted Output |
| 311 | //===----------------------------------------------------------------------===// |
| 312 | |
| 313 | // Out of line virtual method. |
| 314 | void format_object_base::home() { |
| 315 | } |
| 316 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 317 | //===----------------------------------------------------------------------===// |
| 318 | // raw_fd_ostream |
| 319 | //===----------------------------------------------------------------------===// |
| 320 | |
Daniel Dunbar | 48534b3 | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 321 | /// raw_fd_ostream - Open the specified file for writing. If an error |
| 322 | /// occurs, information about the error is put into ErrorInfo, and the |
| 323 | /// stream should be immediately destroyed; the string will be empty |
| 324 | /// if no error occurred. |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 325 | raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo, |
| 326 | unsigned Flags) : pos(0) { |
| 327 | // Verify that we don't have both "append" and "force". |
| 328 | assert((!(Flags & F_Force) || !(Flags & F_Append)) && |
| 329 | "Cannot specify both 'force' and 'append' file creation flags!"); |
| 330 | |
Daniel Dunbar | 48534b3 | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 331 | ErrorInfo.clear(); |
| 332 | |
Chris Lattner | c52b128 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 333 | // Handle "-" as stdout. |
| 334 | if (Filename[0] == '-' && Filename[1] == 0) { |
| 335 | FD = STDOUT_FILENO; |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 336 | // If user requested binary then put stdout into binary mode if |
| 337 | // possible. |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 338 | if (Flags & F_Binary) |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 339 | sys::Program::ChangeStdoutToBinary(); |
Chris Lattner | c52b128 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 340 | ShouldClose = false; |
| 341 | return; |
| 342 | } |
| 343 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 344 | int OpenFlags = O_WRONLY|O_CREAT; |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 345 | #ifdef O_BINARY |
Benjamin Kramer | ae60324 | 2009-08-23 08:57:52 +0000 | [diff] [blame^] | 346 | if (Flags & F_Binary) |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 347 | OpenFlags |= O_BINARY; |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 348 | #endif |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 349 | |
| 350 | if (Flags & F_Force) |
| 351 | OpenFlags |= O_TRUNC; |
| 352 | else if (Flags & F_Append) |
| 353 | OpenFlags |= O_APPEND; |
| 354 | else |
| 355 | OpenFlags |= O_EXCL; |
| 356 | |
| 357 | FD = open(Filename, OpenFlags, 0664); |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 358 | if (FD < 0) { |
| 359 | ErrorInfo = "Error opening output file '" + std::string(Filename) + "'"; |
| 360 | ShouldClose = false; |
| 361 | } else { |
| 362 | ShouldClose = true; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | raw_fd_ostream::~raw_fd_ostream() { |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 367 | if (FD < 0) return; |
| 368 | flush(); |
| 369 | if (ShouldClose) |
| 370 | if (::close(FD) != 0) |
| 371 | error_detected(); |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 374 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 375 | void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) { |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 376 | assert (FD >= 0 && "File already closed."); |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 377 | pos += Size; |
Daniel Dunbar | 579fb87 | 2009-07-15 08:11:46 +0000 | [diff] [blame] | 378 | if (::write(FD, Ptr, Size) != (ssize_t) Size) |
Dan Gohman | e87b2ab | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 379 | error_detected(); |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 382 | void raw_fd_ostream::close() { |
| 383 | assert (ShouldClose); |
| 384 | ShouldClose = false; |
| 385 | flush(); |
Dan Gohman | d3a974f | 2009-07-15 16:43:01 +0000 | [diff] [blame] | 386 | if (::close(FD) != 0) |
Dan Gohman | e87b2ab | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 387 | error_detected(); |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 388 | FD = -1; |
| 389 | } |
| 390 | |
Ted Kremenek | 9c27886 | 2009-01-26 21:42:04 +0000 | [diff] [blame] | 391 | uint64_t raw_fd_ostream::seek(uint64_t off) { |
| 392 | flush(); |
Daniel Dunbar | 579fb87 | 2009-07-15 08:11:46 +0000 | [diff] [blame] | 393 | pos = ::lseek(FD, off, SEEK_SET); |
Dan Gohman | d3a974f | 2009-07-15 16:43:01 +0000 | [diff] [blame] | 394 | if (pos != off) |
Dan Gohman | e87b2ab | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 395 | error_detected(); |
Ted Kremenek | 9c27886 | 2009-01-26 21:42:04 +0000 | [diff] [blame] | 396 | return pos; |
| 397 | } |
| 398 | |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 399 | size_t raw_fd_ostream::preferred_buffer_size() { |
Dan Gohman | b4741a7 | 2009-08-15 21:41:03 +0000 | [diff] [blame] | 400 | #if !defined(_MSC_VER) && !defined(__MINGW32__) // Windows has no st_blksize. |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 401 | assert(FD >= 0 && "File not yet open!"); |
| 402 | struct stat statbuf; |
Dan Gohman | d7be663 | 2009-08-15 02:05:19 +0000 | [diff] [blame] | 403 | if (fstat(FD, &statbuf) == 0) { |
| 404 | // If this is a terminal, don't use buffering. Line buffering |
| 405 | // would be a more traditional thing to do, but it's not worth |
| 406 | // the complexity. |
| 407 | if (S_ISCHR(statbuf.st_mode) && isatty(FD)) |
| 408 | return 0; |
| 409 | // Return the preferred block size. |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 410 | return statbuf.st_blksize; |
Dan Gohman | d7be663 | 2009-08-15 02:05:19 +0000 | [diff] [blame] | 411 | } |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 412 | error_detected(); |
| 413 | #endif |
| 414 | return raw_ostream::preferred_buffer_size(); |
| 415 | } |
| 416 | |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 417 | raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold, |
| 418 | bool bg) { |
| 419 | if (sys::Process::ColorNeedsFlush()) |
| 420 | flush(); |
| 421 | const char *colorcode = |
| 422 | (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg) |
| 423 | : sys::Process::OutputColor(colors, bold, bg); |
| 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 | |
| 433 | raw_ostream &raw_fd_ostream::resetColor() { |
| 434 | if (sys::Process::ColorNeedsFlush()) |
| 435 | flush(); |
| 436 | const char *colorcode = sys::Process::ResetColor(); |
| 437 | if (colorcode) { |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 438 | size_t len = strlen(colorcode); |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 439 | write(colorcode, len); |
| 440 | // don't account colors towards output characters |
| 441 | pos -= len; |
| 442 | } |
| 443 | return *this; |
| 444 | } |
| 445 | |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 446 | //===----------------------------------------------------------------------===// |
| 447 | // raw_stdout/err_ostream |
| 448 | //===----------------------------------------------------------------------===// |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 449 | |
Dan Gohman | ec9b261 | 2009-08-13 23:18:56 +0000 | [diff] [blame] | 450 | // Set buffer settings to model stdout and stderr behavior. |
Dan Gohman | d7be663 | 2009-08-15 02:05:19 +0000 | [diff] [blame] | 451 | // Set standard error to be unbuffered by default. |
| 452 | raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {} |
| 453 | raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false, |
Daniel Dunbar | e77e434 | 2009-03-10 16:21:55 +0000 | [diff] [blame] | 454 | true) {} |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 455 | |
| 456 | // An out of line virtual method to provide a home for the class vtable. |
| 457 | void raw_stdout_ostream::handle() {} |
| 458 | void raw_stderr_ostream::handle() {} |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 459 | |
| 460 | /// outs() - This returns a reference to a raw_ostream for standard output. |
| 461 | /// Use it like: outs() << "foo" << "bar"; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 462 | raw_ostream &llvm::outs() { |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 463 | static raw_stdout_ostream S; |
| 464 | return S; |
| 465 | } |
| 466 | |
| 467 | /// errs() - This returns a reference to a raw_ostream for standard error. |
| 468 | /// Use it like: errs() << "foo" << "bar"; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 469 | raw_ostream &llvm::errs() { |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 470 | static raw_stderr_ostream S; |
| 471 | return S; |
| 472 | } |
| 473 | |
Daniel Dunbar | 34ccf03 | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 474 | /// nulls() - This returns a reference to a raw_ostream which discards output. |
| 475 | raw_ostream &llvm::nulls() { |
| 476 | static raw_null_ostream S; |
| 477 | return S; |
| 478 | } |
| 479 | |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 480 | //===----------------------------------------------------------------------===// |
| 481 | // raw_os_ostream |
| 482 | //===----------------------------------------------------------------------===// |
| 483 | |
Daniel Dunbar | 35979c0 | 2009-08-18 20:07:36 +0000 | [diff] [blame] | 484 | raw_os_ostream::~raw_os_ostream() { |
| 485 | flush(); |
| 486 | } |
| 487 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 488 | void raw_os_ostream::write_impl(const char *Ptr, size_t Size) { |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 489 | OS.write(Ptr, Size); |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 490 | } |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 491 | |
Douglas Gregor | 8f7be47 | 2009-04-20 07:34:17 +0000 | [diff] [blame] | 492 | uint64_t raw_os_ostream::current_pos() { return OS.tellp(); } |
| 493 | |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 494 | //===----------------------------------------------------------------------===// |
| 495 | // raw_string_ostream |
| 496 | //===----------------------------------------------------------------------===// |
| 497 | |
Daniel Dunbar | 35979c0 | 2009-08-18 20:07:36 +0000 | [diff] [blame] | 498 | raw_string_ostream::~raw_string_ostream() { |
| 499 | flush(); |
| 500 | } |
| 501 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 502 | void raw_string_ostream::write_impl(const char *Ptr, size_t Size) { |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 503 | OS.append(Ptr, Size); |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | //===----------------------------------------------------------------------===// |
| 507 | // raw_svector_ostream |
| 508 | //===----------------------------------------------------------------------===// |
| 509 | |
Daniel Dunbar | 425d08c | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 510 | // The raw_svector_ostream implementation uses the SmallVector itself as the |
| 511 | // buffer for the raw_ostream. We guarantee that the raw_ostream buffer is |
| 512 | // always pointing past the end of the vector, but within the vector |
| 513 | // capacity. This allows raw_ostream to write directly into the correct place, |
| 514 | // and we only need to set the vector size when the data is flushed. |
| 515 | |
Daniel Dunbar | 906d5b4 | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 516 | raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) { |
Daniel Dunbar | d14787e | 2009-08-19 18:40:58 +0000 | [diff] [blame] | 517 | // Set up the initial external buffer. We make sure that the buffer has at |
Daniel Dunbar | 425d08c | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 518 | // least 128 bytes free; raw_ostream itself only requires 64, but we want to |
| 519 | // make sure that we don't grow the buffer unnecessarily on destruction (when |
| 520 | // the data is flushed). See the FIXME below. |
Daniel Dunbar | d14787e | 2009-08-19 18:40:58 +0000 | [diff] [blame] | 521 | OS.reserve(OS.size() + 128); |
Daniel Dunbar | 425d08c | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 522 | SetBuffer(OS.end(), OS.capacity() - OS.size()); |
Daniel Dunbar | 906d5b4 | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Daniel Dunbar | 35979c0 | 2009-08-18 20:07:36 +0000 | [diff] [blame] | 525 | raw_svector_ostream::~raw_svector_ostream() { |
Daniel Dunbar | 425d08c | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 526 | // FIXME: Prevent resizing during this flush(). |
Daniel Dunbar | 35979c0 | 2009-08-18 20:07:36 +0000 | [diff] [blame] | 527 | flush(); |
| 528 | } |
| 529 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 530 | void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) { |
Daniel Dunbar | 425d08c | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 531 | assert(Ptr == OS.end() && OS.size() + Size <= OS.capacity() && |
| 532 | "Invalid write_impl() call!"); |
| 533 | |
| 534 | // We don't need to copy the bytes, just commit the bytes to the |
| 535 | // SmallVector. |
| 536 | OS.set_size(OS.size() + Size); |
| 537 | |
| 538 | // Grow the vector if necessary. |
| 539 | if (OS.capacity() - OS.size() < 64) |
| 540 | OS.reserve(OS.capacity() * 2); |
| 541 | |
| 542 | // Update the buffer position. |
| 543 | SetBuffer(OS.end(), OS.capacity() - OS.size()); |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Douglas Gregor | 8f7be47 | 2009-04-20 07:34:17 +0000 | [diff] [blame] | 546 | uint64_t raw_svector_ostream::current_pos() { return OS.size(); } |
| 547 | |
Daniel Dunbar | d14787e | 2009-08-19 18:40:58 +0000 | [diff] [blame] | 548 | StringRef raw_svector_ostream::str() { |
| 549 | flush(); |
| 550 | return StringRef(OS.begin(), OS.size()); |
| 551 | } |
| 552 | |
Daniel Dunbar | 34ccf03 | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 553 | //===----------------------------------------------------------------------===// |
| 554 | // raw_null_ostream |
| 555 | //===----------------------------------------------------------------------===// |
| 556 | |
Dan Gohman | f78c835 | 2009-07-27 21:46:02 +0000 | [diff] [blame] | 557 | raw_null_ostream::~raw_null_ostream() { |
| 558 | #ifndef NDEBUG |
| 559 | // ~raw_ostream asserts that the buffer is empty. This isn't necessary |
| 560 | // with raw_null_ostream, but it's better to have raw_null_ostream follow |
| 561 | // the rules than to change the rules just for raw_null_ostream. |
| 562 | flush(); |
| 563 | #endif |
| 564 | } |
| 565 | |
Daniel Dunbar | 34ccf03 | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 566 | void raw_null_ostream::write_impl(const char *Ptr, size_t Size) { |
| 567 | } |
| 568 | |
| 569 | uint64_t raw_null_ostream::current_pos() { |
| 570 | return 0; |
| 571 | } |