Chris Lattner | 84b94f7 | 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" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | d564f29 | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 18 | #include "llvm/Config/config.h" |
Daniel Dunbar | 437b8a5 | 2009-03-17 21:15:18 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Compiler.h" |
Daniel Dunbar | dcb50b9 | 2009-07-15 08:11:46 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Format.h" |
| 22 | #include "llvm/Support/Process.h" |
| 23 | #include "llvm/Support/Program.h" |
Michael J. Spencer | a2755f8 | 2011-12-13 23:16:49 +0000 | [diff] [blame] | 24 | #include "llvm/Support/system_error.h" |
Benjamin Kramer | ef14f80 | 2010-01-29 15:19:06 +0000 | [diff] [blame] | 25 | #include <cctype> |
Benjamin Kramer | ce84a25 | 2010-05-05 15:17:47 +0000 | [diff] [blame] | 26 | #include <cerrno> |
Dan Gohman | 84487b9 | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 29 | |
Chris Lattner | d564f29 | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 30 | #if defined(HAVE_UNISTD_H) |
| 31 | # include <unistd.h> |
| 32 | #endif |
| 33 | #if defined(HAVE_FCNTL_H) |
| 34 | # include <fcntl.h> |
| 35 | #endif |
Daniel Dunbar | 4fed887 | 2011-02-03 03:32:32 +0000 | [diff] [blame] | 36 | #if defined(HAVE_SYS_UIO_H) && defined(HAVE_WRITEV) |
| 37 | # include <sys/uio.h> |
| 38 | #endif |
Argyrios Kyrtzidis | b97ff82 | 2008-08-17 09:25:21 +0000 | [diff] [blame] | 39 | |
NAKAMURA Takumi | fe84f39 | 2010-10-19 01:21:55 +0000 | [diff] [blame] | 40 | #if defined(__CYGWIN__) |
| 41 | #include <io.h> |
| 42 | #endif |
| 43 | |
Argyrios Kyrtzidis | b97ff82 | 2008-08-17 09:25:21 +0000 | [diff] [blame] | 44 | #if defined(_MSC_VER) |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 45 | #include <io.h> |
Cedric Venet | 0f7b566 | 2008-08-24 11:56:40 +0000 | [diff] [blame] | 46 | #include <fcntl.h> |
Owen Anderson | 9371964 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 47 | #ifndef STDIN_FILENO |
| 48 | # define STDIN_FILENO 0 |
| 49 | #endif |
| 50 | #ifndef STDOUT_FILENO |
| 51 | # define STDOUT_FILENO 1 |
| 52 | #endif |
| 53 | #ifndef STDERR_FILENO |
| 54 | # define STDERR_FILENO 2 |
| 55 | #endif |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 56 | #endif |
| 57 | |
Chris Lattner | d564f29 | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 58 | using namespace llvm; |
| 59 | |
Dan Gohman | 58fcef9 | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 60 | raw_ostream::~raw_ostream() { |
Dan Gohman | 1b76329 | 2009-07-27 20:49:44 +0000 | [diff] [blame] | 61 | // raw_ostream's subclasses should take care to flush the buffer |
| 62 | // in their destructors. |
| 63 | assert(OutBufCur == OutBufStart && |
| 64 | "raw_ostream destructor called with non-empty buffer!"); |
| 65 | |
Daniel Dunbar | 317a6cd | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 66 | if (BufferMode == InternalBuffer) |
| 67 | delete [] OutBufStart; |
Dan Gohman | 58fcef9 | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 68 | } |
Chris Lattner | d564f29 | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 70 | // An out of line virtual method to provide a home for the class vtable. |
| 71 | void raw_ostream::handle() {} |
| 72 | |
Chris Lattner | dd3e9aa | 2009-12-19 01:38:42 +0000 | [diff] [blame] | 73 | size_t raw_ostream::preferred_buffer_size() const { |
Dan Gohman | 84487b9 | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 74 | // BUFSIZ is intended to be a reasonable default. |
| 75 | return BUFSIZ; |
| 76 | } |
| 77 | |
| 78 | void raw_ostream::SetBuffered() { |
| 79 | // Ask the subclass to determine an appropriate buffer size. |
Dan Gohman | 1771022 | 2009-08-15 02:05:19 +0000 | [diff] [blame] | 80 | if (size_t Size = preferred_buffer_size()) |
| 81 | SetBufferSize(Size); |
| 82 | else |
| 83 | // It may return 0, meaning this stream should be unbuffered. |
| 84 | SetUnbuffered(); |
Dan Gohman | 84487b9 | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 87 | void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size, |
Nick Lewycky | 7bfd86d | 2011-08-28 03:30:02 +0000 | [diff] [blame] | 88 | BufferKind Mode) { |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 89 | assert(((Mode == Unbuffered && BufferStart == 0 && Size == 0) || |
Daniel Dunbar | 316b4a0 | 2009-09-15 20:31:46 +0000 | [diff] [blame] | 90 | (Mode != Unbuffered && BufferStart && Size)) && |
| 91 | "stream must be unbuffered or have at least one byte"); |
Daniel Dunbar | 317a6cd | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 92 | // Make sure the current buffer is free of content (we can't flush here; the |
| 93 | // child buffer management logic will be in write_impl). |
| 94 | assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!"); |
Dan Gohman | 54401d4 | 2009-08-13 15:58:55 +0000 | [diff] [blame] | 95 | |
Daniel Dunbar | 317a6cd | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 96 | if (BufferMode == InternalBuffer) |
| 97 | delete [] OutBufStart; |
| 98 | OutBufStart = BufferStart; |
Dan Gohman | 54401d4 | 2009-08-13 15:58:55 +0000 | [diff] [blame] | 99 | OutBufEnd = OutBufStart+Size; |
| 100 | OutBufCur = OutBufStart; |
Daniel Dunbar | 317a6cd | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 101 | BufferMode = Mode; |
Daniel Dunbar | b090bf4 | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 102 | |
| 103 | assert(OutBufStart <= OutBufEnd && "Invalid size!"); |
Dan Gohman | 54401d4 | 2009-08-13 15:58:55 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 106 | raw_ostream &raw_ostream::operator<<(unsigned long N) { |
| 107 | // Zero is a special case. |
| 108 | if (N == 0) |
| 109 | return *this << '0'; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 110 | |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 111 | char NumberBuffer[20]; |
| 112 | char *EndPtr = NumberBuffer+sizeof(NumberBuffer); |
| 113 | char *CurPtr = EndPtr; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 114 | |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 115 | while (N) { |
| 116 | *--CurPtr = '0' + char(N % 10); |
| 117 | N /= 10; |
| 118 | } |
| 119 | return write(CurPtr, EndPtr-CurPtr); |
| 120 | } |
| 121 | |
| 122 | raw_ostream &raw_ostream::operator<<(long N) { |
| 123 | if (N < 0) { |
Daniel Dunbar | 7a9bb9e | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 124 | *this << '-'; |
Eli Friedman | 53ba208 | 2011-10-13 22:49:56 +0000 | [diff] [blame] | 125 | // Avoid undefined behavior on LONG_MIN with a cast. |
| 126 | N = -(unsigned long)N; |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 127 | } |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 128 | |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 129 | return this->operator<<(static_cast<unsigned long>(N)); |
| 130 | } |
| 131 | |
| 132 | raw_ostream &raw_ostream::operator<<(unsigned long long N) { |
Daniel Dunbar | 465de3e | 2009-08-19 16:25:25 +0000 | [diff] [blame] | 133 | // Output using 32-bit div/mod when possible. |
Daniel Dunbar | a94f58a | 2009-07-29 06:45:14 +0000 | [diff] [blame] | 134 | if (N == static_cast<unsigned long>(N)) |
| 135 | return this->operator<<(static_cast<unsigned long>(N)); |
| 136 | |
Daniel Dunbar | 465de3e | 2009-08-19 16:25:25 +0000 | [diff] [blame] | 137 | char NumberBuffer[20]; |
| 138 | char *EndPtr = NumberBuffer+sizeof(NumberBuffer); |
| 139 | char *CurPtr = EndPtr; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 140 | |
Daniel Dunbar | 465de3e | 2009-08-19 16:25:25 +0000 | [diff] [blame] | 141 | while (N) { |
| 142 | *--CurPtr = '0' + char(N % 10); |
| 143 | N /= 10; |
| 144 | } |
| 145 | return write(CurPtr, EndPtr-CurPtr); |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | raw_ostream &raw_ostream::operator<<(long long N) { |
Chris Lattner | 3db3bb0 | 2010-08-03 16:41:24 +0000 | [diff] [blame] | 149 | if (N < 0) { |
Daniel Dunbar | 7a9bb9e | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 150 | *this << '-'; |
Chris Lattner | 3db3bb0 | 2010-08-03 16:41:24 +0000 | [diff] [blame] | 151 | // Avoid undefined behavior on INT64_MIN with a cast. |
| 152 | N = -(unsigned long long)N; |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 153 | } |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 154 | |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 155 | return this->operator<<(static_cast<unsigned long long>(N)); |
| 156 | } |
| 157 | |
Daniel Dunbar | 6c9629b | 2009-07-30 18:21:23 +0000 | [diff] [blame] | 158 | raw_ostream &raw_ostream::write_hex(unsigned long long N) { |
Daniel Dunbar | 8786218 | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 159 | // Zero is a special case. |
| 160 | if (N == 0) |
| 161 | return *this << '0'; |
| 162 | |
| 163 | char NumberBuffer[20]; |
| 164 | char *EndPtr = NumberBuffer+sizeof(NumberBuffer); |
| 165 | char *CurPtr = EndPtr; |
| 166 | |
| 167 | while (N) { |
Dan Gohman | f199ad6 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 168 | uintptr_t x = N % 16; |
Daniel Dunbar | 8786218 | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 169 | *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10); |
| 170 | N /= 16; |
| 171 | } |
| 172 | |
| 173 | return write(CurPtr, EndPtr-CurPtr); |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Daniel Dunbar | 65dc891 | 2010-11-27 07:59:50 +0000 | [diff] [blame] | 176 | raw_ostream &raw_ostream::write_escaped(StringRef Str, |
| 177 | bool UseHexEscapes) { |
Daniel Dunbar | 4108c43 | 2009-10-17 20:43:08 +0000 | [diff] [blame] | 178 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 179 | unsigned char c = Str[i]; |
| 180 | |
| 181 | switch (c) { |
| 182 | case '\\': |
| 183 | *this << '\\' << '\\'; |
| 184 | break; |
| 185 | case '\t': |
| 186 | *this << '\\' << 't'; |
| 187 | break; |
| 188 | case '\n': |
| 189 | *this << '\\' << 'n'; |
| 190 | break; |
| 191 | case '"': |
| 192 | *this << '\\' << '"'; |
| 193 | break; |
| 194 | default: |
| 195 | if (std::isprint(c)) { |
| 196 | *this << c; |
| 197 | break; |
| 198 | } |
| 199 | |
Daniel Dunbar | 65dc891 | 2010-11-27 07:59:50 +0000 | [diff] [blame] | 200 | // Write out the escaped representation. |
| 201 | if (UseHexEscapes) { |
| 202 | *this << '\\' << 'x'; |
| 203 | *this << hexdigit((c >> 4 & 0xF)); |
| 204 | *this << hexdigit((c >> 0) & 0xF); |
| 205 | } else { |
| 206 | // Always use a full 3-character octal escape. |
| 207 | *this << '\\'; |
| 208 | *this << char('0' + ((c >> 6) & 7)); |
| 209 | *this << char('0' + ((c >> 3) & 7)); |
| 210 | *this << char('0' + ((c >> 0) & 7)); |
| 211 | } |
Daniel Dunbar | 4108c43 | 2009-10-17 20:43:08 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | return *this; |
| 216 | } |
| 217 | |
Daniel Dunbar | 6c9629b | 2009-07-30 18:21:23 +0000 | [diff] [blame] | 218 | raw_ostream &raw_ostream::operator<<(const void *P) { |
| 219 | *this << '0' << 'x'; |
| 220 | |
| 221 | return write_hex((uintptr_t) P); |
| 222 | } |
| 223 | |
Chris Lattner | 06fa176 | 2009-08-24 03:52:50 +0000 | [diff] [blame] | 224 | raw_ostream &raw_ostream::operator<<(double N) { |
NAKAMURA Takumi | bac0d76 | 2011-03-18 09:30:10 +0000 | [diff] [blame] | 225 | #ifdef _WIN32 |
| 226 | // On MSVCRT and compatible, output of %e is incompatible to Posix |
| 227 | // by default. Number of exponent digits should be at least 2. "%+03d" |
| 228 | // FIXME: Implement our formatter to here or Support/Format.h! |
| 229 | int fpcl = _fpclass(N); |
| 230 | |
| 231 | // negative zero |
| 232 | if (fpcl == _FPCLASS_NZ) |
| 233 | return *this << "-0.000000e+00"; |
| 234 | |
| 235 | char buf[16]; |
| 236 | unsigned len; |
| 237 | len = snprintf(buf, sizeof(buf), "%e", N); |
| 238 | if (len <= sizeof(buf) - 2) { |
| 239 | if (len >= 5 && buf[len - 5] == 'e' && buf[len - 3] == '0') { |
| 240 | int cs = buf[len - 4]; |
| 241 | if (cs == '+' || cs == '-') { |
| 242 | int c1 = buf[len - 2]; |
| 243 | int c0 = buf[len - 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 244 | if (isdigit(static_cast<unsigned char>(c1)) && |
| 245 | isdigit(static_cast<unsigned char>(c0))) { |
NAKAMURA Takumi | bac0d76 | 2011-03-18 09:30:10 +0000 | [diff] [blame] | 246 | // Trim leading '0': "...e+012" -> "...e+12\0" |
| 247 | buf[len - 3] = c1; |
| 248 | buf[len - 2] = c0; |
| 249 | buf[--len] = 0; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | return this->operator<<(buf); |
| 254 | } |
| 255 | #endif |
Benjamin Kramer | 6bee24a | 2010-01-29 14:40:33 +0000 | [diff] [blame] | 256 | return this->operator<<(format("%e", N)); |
Chris Lattner | 06fa176 | 2009-08-24 03:52:50 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | |
| 260 | |
Daniel Dunbar | d24535f | 2009-03-16 22:55:06 +0000 | [diff] [blame] | 261 | void raw_ostream::flush_nonempty() { |
| 262 | assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty."); |
Daniel Dunbar | 317a6cd | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 263 | size_t Length = OutBufCur - OutBufStart; |
| 264 | OutBufCur = OutBufStart; |
| 265 | write_impl(OutBufStart, Length); |
Daniel Dunbar | d24535f | 2009-03-16 22:55:06 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Daniel Dunbar | 7a9bb9e | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 268 | raw_ostream &raw_ostream::write(unsigned char C) { |
Daniel Dunbar | 64fa386 | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 269 | // Group exceptional cases into a single branch. |
Benjamin Kramer | bd7f8d0 | 2012-08-29 22:57:00 +0000 | [diff] [blame] | 270 | if (LLVM_UNLIKELY(OutBufCur >= OutBufEnd)) { |
| 271 | if (LLVM_UNLIKELY(!OutBufStart)) { |
Daniel Dunbar | 317a6cd | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 272 | if (BufferMode == Unbuffered) { |
Dan Gohman | 23f90c1 | 2009-08-18 20:09:59 +0000 | [diff] [blame] | 273 | write_impl(reinterpret_cast<char*>(&C), 1); |
| 274 | return *this; |
| 275 | } |
Daniel Dunbar | 0cf1686 | 2009-08-19 00:23:39 +0000 | [diff] [blame] | 276 | // Set up a buffer and start over. |
| 277 | SetBuffered(); |
| 278 | return write(C); |
Dan Gohman | 23f90c1 | 2009-08-18 20:09:59 +0000 | [diff] [blame] | 279 | } |
Daniel Dunbar | 0cf1686 | 2009-08-19 00:23:39 +0000 | [diff] [blame] | 280 | |
| 281 | flush_nonempty(); |
Daniel Dunbar | 2d603da | 2009-03-17 01:13:35 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Daniel Dunbar | 7a9bb9e | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 284 | *OutBufCur++ = C; |
Daniel Dunbar | 8786218 | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 285 | return *this; |
Daniel Dunbar | 7a9bb9e | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 286 | } |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 287 | |
Dan Gohman | f199ad6 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 288 | raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) { |
Daniel Dunbar | 64fa386 | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 289 | // Group exceptional cases into a single branch. |
Benjamin Kramer | bd7f8d0 | 2012-08-29 22:57:00 +0000 | [diff] [blame] | 290 | if (LLVM_UNLIKELY(size_t(OutBufEnd - OutBufCur) < Size)) { |
| 291 | if (LLVM_UNLIKELY(!OutBufStart)) { |
Daniel Dunbar | 317a6cd | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 292 | if (BufferMode == Unbuffered) { |
Dan Gohman | 52022c2 | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 293 | write_impl(Ptr, Size); |
| 294 | return *this; |
| 295 | } |
| 296 | // Set up a buffer and start over. |
Dan Gohman | 84487b9 | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 297 | SetBuffered(); |
Dan Gohman | 52022c2 | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 298 | return write(Ptr, Size); |
| 299 | } |
Daniel Dunbar | 0cf1686 | 2009-08-19 00:23:39 +0000 | [diff] [blame] | 300 | |
Benjamin Kramer | dfb0ad3 | 2011-03-04 19:49:30 +0000 | [diff] [blame] | 301 | size_t NumBytes = OutBufEnd - OutBufCur; |
| 302 | |
Benjamin Kramer | acf0842 | 2011-03-04 18:18:16 +0000 | [diff] [blame] | 303 | // If the buffer is empty at this point we have a string that is larger |
Benjamin Kramer | dfb0ad3 | 2011-03-04 19:49:30 +0000 | [diff] [blame] | 304 | // than the buffer. Directly write the chunk that is a multiple of the |
| 305 | // preferred buffer size and put the remainder in the buffer. |
Benjamin Kramer | bd7f8d0 | 2012-08-29 22:57:00 +0000 | [diff] [blame] | 306 | if (LLVM_UNLIKELY(OutBufCur == OutBufStart)) { |
Benjamin Kramer | dfb0ad3 | 2011-03-04 19:49:30 +0000 | [diff] [blame] | 307 | size_t BytesToWrite = Size - (Size % NumBytes); |
| 308 | write_impl(Ptr, BytesToWrite); |
Matt Beaumont-Gay | a182be8 | 2013-03-12 23:55:24 +0000 | [diff] [blame] | 309 | size_t BytesRemaining = Size - BytesToWrite; |
| 310 | if (BytesRemaining > size_t(OutBufEnd - OutBufCur)) { |
| 311 | // Too much left over to copy into our buffer. |
| 312 | return write(Ptr + BytesToWrite, BytesRemaining); |
| 313 | } |
| 314 | copy_to_buffer(Ptr + BytesToWrite, BytesRemaining); |
Benjamin Kramer | acf0842 | 2011-03-04 18:18:16 +0000 | [diff] [blame] | 315 | return *this; |
| 316 | } |
| 317 | |
| 318 | // We don't have enough space in the buffer to fit the string in. Insert as |
| 319 | // much as possible, flush and start over with the remainder. |
Benjamin Kramer | acf0842 | 2011-03-04 18:18:16 +0000 | [diff] [blame] | 320 | copy_to_buffer(Ptr, NumBytes); |
| 321 | flush_nonempty(); |
| 322 | return write(Ptr + NumBytes, Size - NumBytes); |
Daniel Dunbar | 64fa386 | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 323 | } |
Dan Gohman | 52022c2 | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 324 | |
| 325 | copy_to_buffer(Ptr, Size); |
| 326 | |
| 327 | return *this; |
| 328 | } |
| 329 | |
| 330 | void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) { |
Dan Gohman | c04a00a | 2009-08-13 20:32:03 +0000 | [diff] [blame] | 331 | assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!"); |
Dan Gohman | 9820555 | 2009-08-13 18:38:15 +0000 | [diff] [blame] | 332 | |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 333 | // Handle short strings specially, memcpy isn't very good at very short |
| 334 | // strings. |
| 335 | switch (Size) { |
| 336 | case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH |
| 337 | case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH |
| 338 | case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH |
| 339 | case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH |
| 340 | case 0: break; |
| 341 | default: |
Dan Gohman | 52022c2 | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 342 | memcpy(OutBufCur, Ptr, Size); |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 343 | break; |
| 344 | } |
Daniel Dunbar | 3da6a70 | 2009-03-10 16:21:55 +0000 | [diff] [blame] | 345 | |
Dan Gohman | 52022c2 | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 346 | OutBufCur += Size; |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 349 | // Formatted output. |
| 350 | raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) { |
Daniel Dunbar | 64fa386 | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 351 | // If we have more than a few bytes left in our output buffer, try |
| 352 | // formatting directly onto its end. |
Dan Gohman | f199ad6 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 353 | size_t NextBufferSize = 127; |
Daniel Dunbar | 47a309c | 2009-08-23 20:31:39 +0000 | [diff] [blame] | 354 | size_t BufferBytesLeft = OutBufEnd - OutBufCur; |
| 355 | if (BufferBytesLeft > 3) { |
Dan Gohman | f199ad6 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 356 | size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 357 | |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 358 | // Common case is that we have plenty of space. |
Daniel Dunbar | 47a309c | 2009-08-23 20:31:39 +0000 | [diff] [blame] | 359 | if (BytesUsed <= BufferBytesLeft) { |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 360 | OutBufCur += BytesUsed; |
| 361 | return *this; |
| 362 | } |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 363 | |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 364 | // Otherwise, we overflowed and the return value tells us the size to try |
| 365 | // again with. |
| 366 | NextBufferSize = BytesUsed; |
| 367 | } |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 368 | |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 369 | // If we got here, we didn't have enough space in the output buffer for the |
| 370 | // string. Try printing into a SmallVector that is resized to have enough |
| 371 | // space. Iterate until we win. |
| 372 | SmallVector<char, 128> V; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 373 | |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 374 | while (1) { |
| 375 | V.resize(NextBufferSize); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 376 | |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 377 | // Try formatting into the SmallVector. |
Daniel Dunbar | 47a309c | 2009-08-23 20:31:39 +0000 | [diff] [blame] | 378 | size_t BytesUsed = Fmt.print(V.data(), NextBufferSize); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 379 | |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 380 | // If BytesUsed fit into the vector, we win. |
Chris Lattner | 2bfc72e | 2008-10-26 19:20:47 +0000 | [diff] [blame] | 381 | if (BytesUsed <= NextBufferSize) |
Daniel Dunbar | 47a309c | 2009-08-23 20:31:39 +0000 | [diff] [blame] | 382 | return write(V.data(), BytesUsed); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 383 | |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 384 | // Otherwise, try again with a new size. |
| 385 | assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?"); |
| 386 | NextBufferSize = BytesUsed; |
| 387 | } |
| 388 | } |
| 389 | |
Chris Lattner | e6db2c3 | 2009-08-22 23:10:29 +0000 | [diff] [blame] | 390 | /// indent - Insert 'NumSpaces' spaces. |
| 391 | raw_ostream &raw_ostream::indent(unsigned NumSpaces) { |
Dan Gohman | f88f52e | 2009-08-24 04:13:01 +0000 | [diff] [blame] | 392 | static const char Spaces[] = " " |
| 393 | " " |
| 394 | " "; |
Chris Lattner | e6db2c3 | 2009-08-22 23:10:29 +0000 | [diff] [blame] | 395 | |
| 396 | // Usually the indentation is small, handle it with a fastpath. |
Dan Gohman | c9fa051 | 2009-08-24 04:43:38 +0000 | [diff] [blame] | 397 | if (NumSpaces < array_lengthof(Spaces)) |
Chris Lattner | e6db2c3 | 2009-08-22 23:10:29 +0000 | [diff] [blame] | 398 | return write(Spaces, NumSpaces); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 399 | |
Chris Lattner | e6db2c3 | 2009-08-22 23:10:29 +0000 | [diff] [blame] | 400 | while (NumSpaces) { |
Dan Gohman | c9fa051 | 2009-08-24 04:43:38 +0000 | [diff] [blame] | 401 | unsigned NumToWrite = std::min(NumSpaces, |
| 402 | (unsigned)array_lengthof(Spaces)-1); |
Chris Lattner | e6db2c3 | 2009-08-22 23:10:29 +0000 | [diff] [blame] | 403 | write(Spaces, NumToWrite); |
| 404 | NumSpaces -= NumToWrite; |
| 405 | } |
| 406 | return *this; |
| 407 | } |
| 408 | |
| 409 | |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 410 | //===----------------------------------------------------------------------===// |
| 411 | // Formatted Output |
| 412 | //===----------------------------------------------------------------------===// |
| 413 | |
| 414 | // Out of line virtual method. |
| 415 | void format_object_base::home() { |
| 416 | } |
| 417 | |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 418 | //===----------------------------------------------------------------------===// |
| 419 | // raw_fd_ostream |
| 420 | //===----------------------------------------------------------------------===// |
| 421 | |
Daniel Dunbar | 1ca20df | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 422 | /// raw_fd_ostream - Open the specified file for writing. If an error |
| 423 | /// occurs, information about the error is put into ErrorInfo, and the |
| 424 | /// stream should be immediately destroyed; the string will be empty |
| 425 | /// if no error occurred. |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 426 | raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo, |
Daniel Dunbar | 4fed887 | 2011-02-03 03:32:32 +0000 | [diff] [blame] | 427 | unsigned Flags) |
| 428 | : Error(false), UseAtomicWrites(false), pos(0) |
| 429 | { |
Chris Lattner | 6783832 | 2010-03-05 00:49:08 +0000 | [diff] [blame] | 430 | assert(Filename != 0 && "Filename is null"); |
Dan Gohman | 61a8796 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 431 | // Verify that we don't have both "append" and "excl". |
| 432 | assert((!(Flags & F_Excl) || !(Flags & F_Append)) && |
| 433 | "Cannot specify both 'excl' and 'append' file creation flags!"); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 434 | |
Daniel Dunbar | 1ca20df | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 435 | ErrorInfo.clear(); |
| 436 | |
Dan Gohman | c825cee | 2010-08-18 22:26:19 +0000 | [diff] [blame] | 437 | // Handle "-" as stdout. Note that when we do this, we consider ourself |
| 438 | // the owner of stdout. This means that we can do things like close the |
| 439 | // file descriptor when we're done and set the "binary" flag globally. |
Chris Lattner | d5bc068 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 440 | if (Filename[0] == '-' && Filename[1] == 0) { |
| 441 | FD = STDOUT_FILENO; |
Daniel Dunbar | ed90e70 | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 442 | // If user requested binary then put stdout into binary mode if |
| 443 | // possible. |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 444 | if (Flags & F_Binary) |
Daniel Dunbar | ed90e70 | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 445 | sys::Program::ChangeStdoutToBinary(); |
Dan Gohman | c825cee | 2010-08-18 22:26:19 +0000 | [diff] [blame] | 446 | // Close stdout when we're done, to detect any output errors. |
| 447 | ShouldClose = true; |
Chris Lattner | d5bc068 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 448 | return; |
| 449 | } |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 450 | |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 451 | int OpenFlags = O_WRONLY|O_CREAT; |
Daniel Dunbar | ed90e70 | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 452 | #ifdef O_BINARY |
Benjamin Kramer | b451afb | 2009-08-23 08:57:52 +0000 | [diff] [blame] | 453 | if (Flags & F_Binary) |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 454 | OpenFlags |= O_BINARY; |
Daniel Dunbar | ed90e70 | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 455 | #endif |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 456 | |
Dan Gohman | 61a8796 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 457 | if (Flags & F_Append) |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 458 | OpenFlags |= O_APPEND; |
| 459 | else |
Dan Gohman | 61a8796 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 460 | OpenFlags |= O_TRUNC; |
| 461 | if (Flags & F_Excl) |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 462 | OpenFlags |= O_EXCL; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 463 | |
Dan Gohman | d351116 | 2010-05-06 02:06:20 +0000 | [diff] [blame] | 464 | while ((FD = open(Filename, OpenFlags, 0664)) < 0) { |
| 465 | if (errno != EINTR) { |
| 466 | ErrorInfo = "Error opening output file '" + std::string(Filename) + "'"; |
| 467 | ShouldClose = false; |
| 468 | return; |
| 469 | } |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 470 | } |
Dan Gohman | d351116 | 2010-05-06 02:06:20 +0000 | [diff] [blame] | 471 | |
| 472 | // Ok, we successfully opened the file, so it'll need to be closed. |
| 473 | ShouldClose = true; |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Francois Pichet | a3037c3 | 2010-10-14 20:30:58 +0000 | [diff] [blame] | 476 | /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If |
| 477 | /// ShouldClose is true, this closes the file when the stream is destroyed. |
| 478 | raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered) |
| 479 | : raw_ostream(unbuffered), FD(fd), |
Daniel Dunbar | 4fed887 | 2011-02-03 03:32:32 +0000 | [diff] [blame] | 480 | ShouldClose(shouldClose), Error(false), UseAtomicWrites(false) { |
Francois Pichet | a3037c3 | 2010-10-14 20:30:58 +0000 | [diff] [blame] | 481 | #ifdef O_BINARY |
| 482 | // Setting STDOUT and STDERR to binary mode is necessary in Win32 |
| 483 | // to avoid undesirable linefeed conversion. |
| 484 | if (fd == STDOUT_FILENO || fd == STDERR_FILENO) |
| 485 | setmode(fd, O_BINARY); |
| 486 | #endif |
Michael J. Spencer | b747990 | 2011-01-17 15:53:12 +0000 | [diff] [blame] | 487 | |
| 488 | // Get the starting position. |
| 489 | off_t loc = ::lseek(FD, 0, SEEK_CUR); |
| 490 | if (loc == (off_t)-1) |
| 491 | pos = 0; |
| 492 | else |
| 493 | pos = static_cast<uint64_t>(loc); |
Francois Pichet | a3037c3 | 2010-10-14 20:30:58 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 496 | raw_fd_ostream::~raw_fd_ostream() { |
Dan Gohman | 38adfdd | 2010-08-20 16:34:20 +0000 | [diff] [blame] | 497 | if (FD >= 0) { |
| 498 | flush(); |
| 499 | if (ShouldClose) |
| 500 | while (::close(FD) != 0) |
| 501 | if (errno != EINTR) { |
| 502 | error_detected(); |
| 503 | break; |
| 504 | } |
| 505 | } |
| 506 | |
NAKAMURA Takumi | 76e68ea | 2011-03-16 02:53:39 +0000 | [diff] [blame] | 507 | #ifdef __MINGW32__ |
| 508 | // On mingw, global dtors should not call exit(). |
| 509 | // report_fatal_error() invokes exit(). We know report_fatal_error() |
| 510 | // might not write messages to stderr when any errors were detected |
| 511 | // on FD == 2. |
| 512 | if (FD == 2) return; |
| 513 | #endif |
| 514 | |
Dan Gohman | 38adfdd | 2010-08-20 16:34:20 +0000 | [diff] [blame] | 515 | // If there are any pending errors, report them now. Clients wishing |
| 516 | // to avoid report_fatal_error calls should check for errors with |
| 517 | // has_error() and clear the error flag with clear_error() before |
| 518 | // destructing raw_ostream objects which may have errors. |
| 519 | if (has_error()) |
Chad Rosier | 7ce810c | 2013-03-27 18:30:00 +0000 | [diff] [blame^] | 520 | report_fatal_error("IO failure on output stream.", /*GenCrashDiag=*/false); |
Chris Lattner | ce3b2c3 | 2010-08-17 23:11:56 +0000 | [diff] [blame] | 521 | } |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 522 | |
Dan Gohman | 44790e7 | 2010-08-18 01:34:52 +0000 | [diff] [blame] | 523 | |
Dan Gohman | f199ad6 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 524 | void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) { |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 525 | assert(FD >= 0 && "File already closed."); |
Daniel Dunbar | db7a36c | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 526 | pos += Size; |
Dan Gohman | ef969f3 | 2010-05-06 01:27:36 +0000 | [diff] [blame] | 527 | |
Benjamin Kramer | ce84a25 | 2010-05-05 15:17:47 +0000 | [diff] [blame] | 528 | do { |
Daniel Dunbar | 4fed887 | 2011-02-03 03:32:32 +0000 | [diff] [blame] | 529 | ssize_t ret; |
| 530 | |
| 531 | // Check whether we should attempt to use atomic writes. |
Benjamin Kramer | bd7f8d0 | 2012-08-29 22:57:00 +0000 | [diff] [blame] | 532 | if (LLVM_LIKELY(!UseAtomicWrites)) { |
Daniel Dunbar | 4fed887 | 2011-02-03 03:32:32 +0000 | [diff] [blame] | 533 | ret = ::write(FD, Ptr, Size); |
| 534 | } else { |
| 535 | // Use ::writev() where available. |
| 536 | #if defined(HAVE_WRITEV) |
Galina Kistanova | 7da6578 | 2012-07-12 20:45:36 +0000 | [diff] [blame] | 537 | const void *Addr = static_cast<const void *>(Ptr); |
| 538 | struct iovec IOV = {const_cast<void *>(Addr), Size }; |
Daniel Dunbar | 4fed887 | 2011-02-03 03:32:32 +0000 | [diff] [blame] | 539 | ret = ::writev(FD, &IOV, 1); |
| 540 | #else |
| 541 | ret = ::write(FD, Ptr, Size); |
| 542 | #endif |
| 543 | } |
Dan Gohman | ef969f3 | 2010-05-06 01:27:36 +0000 | [diff] [blame] | 544 | |
| 545 | if (ret < 0) { |
| 546 | // If it's a recoverable error, swallow it and retry the write. |
Dan Gohman | dea5310 | 2010-05-18 15:25:14 +0000 | [diff] [blame] | 547 | // |
| 548 | // Ideally we wouldn't ever see EAGAIN or EWOULDBLOCK here, since |
| 549 | // raw_ostream isn't designed to do non-blocking I/O. However, some |
| 550 | // programs, such as old versions of bjam, have mistakenly used |
| 551 | // O_NONBLOCK. For compatibility, emulate blocking semantics by |
| 552 | // spinning until the write succeeds. If you don't want spinning, |
| 553 | // don't use O_NONBLOCK file descriptors with raw_ostream. |
Dan Gohman | d351116 | 2010-05-06 02:06:20 +0000 | [diff] [blame] | 554 | if (errno == EINTR || errno == EAGAIN |
| 555 | #ifdef EWOULDBLOCK |
| 556 | || errno == EWOULDBLOCK |
| 557 | #endif |
| 558 | ) |
Dan Gohman | ef969f3 | 2010-05-06 01:27:36 +0000 | [diff] [blame] | 559 | continue; |
| 560 | |
| 561 | // Otherwise it's a non-recoverable error. Note it and quit. |
| 562 | error_detected(); |
| 563 | break; |
| 564 | } |
| 565 | |
| 566 | // The write may have written some or all of the data. Update the |
| 567 | // size and buffer pointer to reflect the remainder that needs |
| 568 | // to be written. If there are no bytes left, we're done. |
| 569 | Ptr += ret; |
| 570 | Size -= ret; |
| 571 | } while (Size > 0); |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Dan Gohman | 44790e7 | 2010-08-18 01:34:52 +0000 | [diff] [blame] | 574 | void raw_fd_ostream::close() { |
| 575 | assert(ShouldClose); |
| 576 | ShouldClose = false; |
| 577 | flush(); |
| 578 | while (::close(FD) != 0) |
| 579 | if (errno != EINTR) { |
| 580 | error_detected(); |
| 581 | break; |
| 582 | } |
| 583 | FD = -1; |
| 584 | } |
| 585 | |
Ted Kremenek | a266992 | 2009-01-26 21:42:04 +0000 | [diff] [blame] | 586 | uint64_t raw_fd_ostream::seek(uint64_t off) { |
| 587 | flush(); |
Daniel Dunbar | dcb50b9 | 2009-07-15 08:11:46 +0000 | [diff] [blame] | 588 | pos = ::lseek(FD, off, SEEK_SET); |
Dan Gohman | 213e87b | 2009-07-15 16:43:01 +0000 | [diff] [blame] | 589 | if (pos != off) |
Dan Gohman | 58fcef9 | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 590 | error_detected(); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 591 | return pos; |
Ted Kremenek | a266992 | 2009-01-26 21:42:04 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Chris Lattner | dd3e9aa | 2009-12-19 01:38:42 +0000 | [diff] [blame] | 594 | size_t raw_fd_ostream::preferred_buffer_size() const { |
Chris Lattner | ca97c92 | 2010-07-07 15:52:27 +0000 | [diff] [blame] | 595 | #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__minix) |
Chris Lattner | c86cdc7 | 2010-04-09 20:45:04 +0000 | [diff] [blame] | 596 | // Windows and Minix have no st_blksize. |
Dan Gohman | 84487b9 | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 597 | assert(FD >= 0 && "File not yet open!"); |
| 598 | struct stat statbuf; |
Chris Lattner | dd3e9aa | 2009-12-19 01:38:42 +0000 | [diff] [blame] | 599 | if (fstat(FD, &statbuf) != 0) |
| 600 | return 0; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 601 | |
Chris Lattner | dd3e9aa | 2009-12-19 01:38:42 +0000 | [diff] [blame] | 602 | // If this is a terminal, don't use buffering. Line buffering |
| 603 | // would be a more traditional thing to do, but it's not worth |
| 604 | // the complexity. |
| 605 | if (S_ISCHR(statbuf.st_mode) && isatty(FD)) |
| 606 | return 0; |
| 607 | // Return the preferred block size. |
| 608 | return statbuf.st_blksize; |
Dan Gohman | feaeb36 | 2010-05-28 16:50:01 +0000 | [diff] [blame] | 609 | #else |
Dan Gohman | 84487b9 | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 610 | return raw_ostream::preferred_buffer_size(); |
Dan Gohman | feaeb36 | 2010-05-28 16:50:01 +0000 | [diff] [blame] | 611 | #endif |
Dan Gohman | 84487b9 | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 614 | raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold, |
| 615 | bool bg) { |
| 616 | if (sys::Process::ColorNeedsFlush()) |
| 617 | flush(); |
| 618 | const char *colorcode = |
| 619 | (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg) |
| 620 | : sys::Process::OutputColor(colors, bold, bg); |
| 621 | if (colorcode) { |
Dan Gohman | f199ad6 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 622 | size_t len = strlen(colorcode); |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 623 | write(colorcode, len); |
| 624 | // don't account colors towards output characters |
| 625 | pos -= len; |
| 626 | } |
| 627 | return *this; |
| 628 | } |
| 629 | |
| 630 | raw_ostream &raw_fd_ostream::resetColor() { |
| 631 | if (sys::Process::ColorNeedsFlush()) |
| 632 | flush(); |
| 633 | const char *colorcode = sys::Process::ResetColor(); |
| 634 | if (colorcode) { |
Dan Gohman | f199ad6 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 635 | size_t len = strlen(colorcode); |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 636 | write(colorcode, len); |
| 637 | // don't account colors towards output characters |
| 638 | pos -= len; |
| 639 | } |
| 640 | return *this; |
| 641 | } |
| 642 | |
Benjamin Kramer | 13d16f3 | 2012-04-16 08:56:50 +0000 | [diff] [blame] | 643 | raw_ostream &raw_fd_ostream::reverseColor() { |
| 644 | if (sys::Process::ColorNeedsFlush()) |
| 645 | flush(); |
| 646 | const char *colorcode = sys::Process::OutputReverse(); |
| 647 | if (colorcode) { |
| 648 | size_t len = strlen(colorcode); |
| 649 | write(colorcode, len); |
| 650 | // don't account colors towards output characters |
| 651 | pos -= len; |
| 652 | } |
| 653 | return *this; |
| 654 | } |
| 655 | |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 656 | bool raw_fd_ostream::is_displayed() const { |
| 657 | return sys::Process::FileDescriptorIsDisplayed(FD); |
| 658 | } |
| 659 | |
Daniel Dunbar | 04b4583 | 2012-07-20 18:29:41 +0000 | [diff] [blame] | 660 | bool raw_fd_ostream::has_colors() const { |
| 661 | return sys::Process::FileDescriptorHasColors(FD); |
| 662 | } |
| 663 | |
Chris Lattner | d3723fc | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 664 | //===----------------------------------------------------------------------===// |
Dan Gohman | e9a4691 | 2010-08-20 16:44:56 +0000 | [diff] [blame] | 665 | // outs(), errs(), nulls() |
Chris Lattner | d3723fc | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 666 | //===----------------------------------------------------------------------===// |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 667 | |
Chris Lattner | d3723fc | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 668 | /// outs() - This returns a reference to a raw_ostream for standard output. |
| 669 | /// Use it like: outs() << "foo" << "bar"; |
Owen Anderson | 9371964 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 670 | raw_ostream &llvm::outs() { |
Dan Gohman | 443f2d6 | 2010-08-20 16:39:41 +0000 | [diff] [blame] | 671 | // Set buffer settings to model stdout behavior. |
Dan Gohman | e9a4691 | 2010-08-20 16:44:56 +0000 | [diff] [blame] | 672 | // Delete the file descriptor when the program exists, forcing error |
| 673 | // detection. If you don't want this behavior, don't use outs(). |
| 674 | static raw_fd_ostream S(STDOUT_FILENO, true); |
Chris Lattner | d3723fc | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 675 | return S; |
| 676 | } |
| 677 | |
| 678 | /// errs() - This returns a reference to a raw_ostream for standard error. |
| 679 | /// Use it like: errs() << "foo" << "bar"; |
Owen Anderson | 9371964 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 680 | raw_ostream &llvm::errs() { |
Dan Gohman | 443f2d6 | 2010-08-20 16:39:41 +0000 | [diff] [blame] | 681 | // Set standard error to be unbuffered by default. |
| 682 | static raw_fd_ostream S(STDERR_FILENO, false, true); |
Chris Lattner | d3723fc | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 683 | return S; |
| 684 | } |
| 685 | |
Daniel Dunbar | 95a551a | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 686 | /// nulls() - This returns a reference to a raw_ostream which discards output. |
| 687 | raw_ostream &llvm::nulls() { |
| 688 | static raw_null_ostream S; |
| 689 | return S; |
| 690 | } |
| 691 | |
Douglas Gregor | b231e57 | 2009-04-20 07:34:17 +0000 | [diff] [blame] | 692 | |
Chris Lattner | 205af96 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 693 | //===----------------------------------------------------------------------===// |
| 694 | // raw_string_ostream |
| 695 | //===----------------------------------------------------------------------===// |
| 696 | |
Daniel Dunbar | d882f4b | 2009-08-18 20:07:36 +0000 | [diff] [blame] | 697 | raw_string_ostream::~raw_string_ostream() { |
| 698 | flush(); |
| 699 | } |
| 700 | |
Dan Gohman | f199ad6 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 701 | void raw_string_ostream::write_impl(const char *Ptr, size_t Size) { |
Daniel Dunbar | db7a36c | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 702 | OS.append(Ptr, Size); |
Chris Lattner | 205af96 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | //===----------------------------------------------------------------------===// |
| 706 | // raw_svector_ostream |
| 707 | //===----------------------------------------------------------------------===// |
| 708 | |
Daniel Dunbar | b090bf4 | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 709 | // The raw_svector_ostream implementation uses the SmallVector itself as the |
| 710 | // buffer for the raw_ostream. We guarantee that the raw_ostream buffer is |
| 711 | // always pointing past the end of the vector, but within the vector |
| 712 | // capacity. This allows raw_ostream to write directly into the correct place, |
| 713 | // and we only need to set the vector size when the data is flushed. |
| 714 | |
Daniel Dunbar | 317a6cd | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 715 | raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) { |
Daniel Dunbar | e813cba | 2009-08-19 18:40:58 +0000 | [diff] [blame] | 716 | // Set up the initial external buffer. We make sure that the buffer has at |
Daniel Dunbar | b090bf4 | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 717 | // least 128 bytes free; raw_ostream itself only requires 64, but we want to |
| 718 | // make sure that we don't grow the buffer unnecessarily on destruction (when |
| 719 | // the data is flushed). See the FIXME below. |
Daniel Dunbar | e813cba | 2009-08-19 18:40:58 +0000 | [diff] [blame] | 720 | OS.reserve(OS.size() + 128); |
Daniel Dunbar | b090bf4 | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 721 | SetBuffer(OS.end(), OS.capacity() - OS.size()); |
Daniel Dunbar | 317a6cd | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Daniel Dunbar | d882f4b | 2009-08-18 20:07:36 +0000 | [diff] [blame] | 724 | raw_svector_ostream::~raw_svector_ostream() { |
Daniel Dunbar | b090bf4 | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 725 | // FIXME: Prevent resizing during this flush(). |
Daniel Dunbar | d882f4b | 2009-08-18 20:07:36 +0000 | [diff] [blame] | 726 | flush(); |
| 727 | } |
| 728 | |
Chris Lattner | 1386a88 | 2010-01-22 21:16:10 +0000 | [diff] [blame] | 729 | /// resync - This is called when the SmallVector we're appending to is changed |
| 730 | /// outside of the raw_svector_ostream's control. It is only safe to do this |
| 731 | /// if the raw_svector_ostream has previously been flushed. |
| 732 | void raw_svector_ostream::resync() { |
| 733 | assert(GetNumBytesInBuffer() == 0 && "Didn't flush before mutating vector"); |
| 734 | |
| 735 | if (OS.capacity() - OS.size() < 64) |
| 736 | OS.reserve(OS.capacity() * 2); |
Chris Lattner | 8fa0e35 | 2010-01-22 19:17:48 +0000 | [diff] [blame] | 737 | SetBuffer(OS.end(), OS.capacity() - OS.size()); |
| 738 | } |
| 739 | |
Dan Gohman | f199ad6 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 740 | void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) { |
Chris Lattner | 115158f | 2010-02-15 02:18:26 +0000 | [diff] [blame] | 741 | // If we're writing bytes from the end of the buffer into the smallvector, we |
| 742 | // don't need to copy the bytes, just commit the bytes because they are |
| 743 | // already in the right place. |
| 744 | if (Ptr == OS.end()) { |
| 745 | assert(OS.size() + Size <= OS.capacity() && "Invalid write_impl() call!"); |
| 746 | OS.set_size(OS.size() + Size); |
| 747 | } else { |
| 748 | assert(GetNumBytesInBuffer() == 0 && |
| 749 | "Should be writing from buffer if some bytes in it"); |
| 750 | // Otherwise, do copy the bytes. |
| 751 | OS.append(Ptr, Ptr+Size); |
| 752 | } |
Daniel Dunbar | b090bf4 | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 753 | |
| 754 | // Grow the vector if necessary. |
| 755 | if (OS.capacity() - OS.size() < 64) |
| 756 | OS.reserve(OS.capacity() * 2); |
| 757 | |
| 758 | // Update the buffer position. |
| 759 | SetBuffer(OS.end(), OS.capacity() - OS.size()); |
Chris Lattner | 205af96 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Chris Lattner | dd3e9aa | 2009-12-19 01:38:42 +0000 | [diff] [blame] | 762 | uint64_t raw_svector_ostream::current_pos() const { |
| 763 | return OS.size(); |
| 764 | } |
Douglas Gregor | b231e57 | 2009-04-20 07:34:17 +0000 | [diff] [blame] | 765 | |
Daniel Dunbar | e813cba | 2009-08-19 18:40:58 +0000 | [diff] [blame] | 766 | StringRef raw_svector_ostream::str() { |
| 767 | flush(); |
| 768 | return StringRef(OS.begin(), OS.size()); |
| 769 | } |
| 770 | |
Daniel Dunbar | 95a551a | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 771 | //===----------------------------------------------------------------------===// |
| 772 | // raw_null_ostream |
| 773 | //===----------------------------------------------------------------------===// |
| 774 | |
Dan Gohman | 4b66b47 | 2009-07-27 21:46:02 +0000 | [diff] [blame] | 775 | raw_null_ostream::~raw_null_ostream() { |
| 776 | #ifndef NDEBUG |
| 777 | // ~raw_ostream asserts that the buffer is empty. This isn't necessary |
| 778 | // with raw_null_ostream, but it's better to have raw_null_ostream follow |
| 779 | // the rules than to change the rules just for raw_null_ostream. |
| 780 | flush(); |
| 781 | #endif |
| 782 | } |
| 783 | |
Daniel Dunbar | 95a551a | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 784 | void raw_null_ostream::write_impl(const char *Ptr, size_t Size) { |
| 785 | } |
| 786 | |
Chris Lattner | dd3e9aa | 2009-12-19 01:38:42 +0000 | [diff] [blame] | 787 | uint64_t raw_null_ostream::current_pos() const { |
Daniel Dunbar | 95a551a | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 788 | return 0; |
| 789 | } |