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