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" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Program.h" |
| 17 | #include "llvm/Support/Process.h" |
Daniel Dunbar | 10a049e | 2010-11-27 07:59:50 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 20 | #include "llvm/Config/config.h" |
Daniel Dunbar | b372c11 | 2009-03-17 21:15:18 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
Daniel Dunbar | 579fb87 | 2009-07-15 08:11:46 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Michael J. Spencer | b92cb30 | 2011-12-13 23:16:49 +0000 | [diff] [blame] | 23 | #include "llvm/Support/system_error.h" |
Dan Gohman | 61ee100 | 2009-08-24 04:13:01 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
Benjamin Kramer | 4f17eb8 | 2010-01-29 15:19:06 +0000 | [diff] [blame] | 25 | #include <cctype> |
Benjamin Kramer | 66760be | 2010-05-05 15:17:47 +0000 | [diff] [blame] | 26 | #include <cerrno> |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 969a46a | 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 | b0cfa6c | 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 | b68dc36 | 2008-08-17 09:25:21 +0000 | [diff] [blame] | 39 | |
NAKAMURA Takumi | 1b5de0e | 2010-10-19 01:21:55 +0000 | [diff] [blame] | 40 | #if defined(__CYGWIN__) |
| 41 | #include <io.h> |
| 42 | #endif |
| 43 | |
Argyrios Kyrtzidis | b68dc36 | 2008-08-17 09:25:21 +0000 | [diff] [blame] | 44 | #if defined(_MSC_VER) |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 45 | #include <io.h> |
Cedric Venet | a3f343f | 2008-08-24 11:56:40 +0000 | [diff] [blame] | 46 | #include <fcntl.h> |
Owen Anderson | cb37188 | 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 | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 56 | #endif |
| 57 | |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 58 | using namespace llvm; |
| 59 | |
Dan Gohman | e87b2ab | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 60 | raw_ostream::~raw_ostream() { |
Dan Gohman | 9a31254 | 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 | 906d5b4 | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 66 | if (BufferMode == InternalBuffer) |
| 67 | delete [] OutBufStart; |
Dan Gohman | e87b2ab | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 68 | } |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 60d3962 | 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 | cd0129f | 2009-12-19 01:38:42 +0000 | [diff] [blame] | 73 | size_t raw_ostream::preferred_buffer_size() const { |
Dan Gohman | 208ec0f | 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 | d7be663 | 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 | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 87 | void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size, |
Nick Lewycky | b1b051e | 2011-08-28 03:30:02 +0000 | [diff] [blame] | 88 | BufferKind Mode) { |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 89 | assert(((Mode == Unbuffered && BufferStart == 0 && Size == 0) || |
Daniel Dunbar | c8213b7 | 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 | 906d5b4 | 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 | 524dea4 | 2009-08-13 15:58:55 +0000 | [diff] [blame] | 95 | |
Daniel Dunbar | 906d5b4 | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 96 | if (BufferMode == InternalBuffer) |
| 97 | delete [] OutBufStart; |
| 98 | OutBufStart = BufferStart; |
Dan Gohman | 524dea4 | 2009-08-13 15:58:55 +0000 | [diff] [blame] | 99 | OutBufEnd = OutBufStart+Size; |
| 100 | OutBufCur = OutBufStart; |
Daniel Dunbar | 906d5b4 | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 101 | BufferMode = Mode; |
Daniel Dunbar | 425d08c | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 102 | |
| 103 | assert(OutBufStart <= OutBufEnd && "Invalid size!"); |
Dan Gohman | 524dea4 | 2009-08-13 15:58:55 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Owen Anderson | 66b17ba | 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 | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 110 | |
Owen Anderson | 66b17ba | 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 | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 114 | |
Owen Anderson | 66b17ba | 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 | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 124 | *this << '-'; |
Eli Friedman | 90406e1 | 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 | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 127 | } |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 128 | |
Owen Anderson | 66b17ba | 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 | ecbd0bc | 2009-08-19 16:25:25 +0000 | [diff] [blame] | 133 | // Output using 32-bit div/mod when possible. |
Daniel Dunbar | 78a3dd8 | 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 | ecbd0bc | 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 | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 140 | |
Daniel Dunbar | ecbd0bc | 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 | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | raw_ostream &raw_ostream::operator<<(long long N) { |
Chris Lattner | e211cd8 | 2010-08-03 16:41:24 +0000 | [diff] [blame] | 149 | if (N < 0) { |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 150 | *this << '-'; |
Chris Lattner | e211cd8 | 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 | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 153 | } |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 154 | |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 155 | return this->operator<<(static_cast<unsigned long long>(N)); |
| 156 | } |
| 157 | |
Daniel Dunbar | 48018e0 | 2009-07-30 18:21:23 +0000 | [diff] [blame] | 158 | raw_ostream &raw_ostream::write_hex(unsigned long long N) { |
Daniel Dunbar | b451a0c | 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 | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 168 | uintptr_t x = N % 16; |
Daniel Dunbar | b451a0c | 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 | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Daniel Dunbar | 10a049e | 2010-11-27 07:59:50 +0000 | [diff] [blame] | 176 | raw_ostream &raw_ostream::write_escaped(StringRef Str, |
| 177 | bool UseHexEscapes) { |
Daniel Dunbar | 522b113 | 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 | 10a049e | 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 | 522b113 | 2009-10-17 20:43:08 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | return *this; |
| 216 | } |
| 217 | |
Daniel Dunbar | 48018e0 | 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 | 23132b1 | 2009-08-24 03:52:50 +0000 | [diff] [blame] | 224 | raw_ostream &raw_ostream::operator<<(double N) { |
NAKAMURA Takumi | d4f4e6e | 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]; |
| 244 | if (isdigit(c1) && isdigit(c0)) { |
| 245 | // Trim leading '0': "...e+012" -> "...e+12\0" |
| 246 | buf[len - 3] = c1; |
| 247 | buf[len - 2] = c0; |
| 248 | buf[--len] = 0; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | return this->operator<<(buf); |
| 253 | } |
| 254 | #endif |
Benjamin Kramer | f304ff6 | 2010-01-29 14:40:33 +0000 | [diff] [blame] | 255 | return this->operator<<(format("%e", N)); |
Chris Lattner | 23132b1 | 2009-08-24 03:52:50 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | |
| 259 | |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame] | 260 | void raw_ostream::flush_nonempty() { |
| 261 | assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty."); |
Daniel Dunbar | 906d5b4 | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 262 | size_t Length = OutBufCur - OutBufStart; |
| 263 | OutBufCur = OutBufStart; |
| 264 | write_impl(OutBufStart, Length); |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 267 | raw_ostream &raw_ostream::write(unsigned char C) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 268 | // Group exceptional cases into a single branch. |
Daniel Dunbar | ecc67e2 | 2009-08-19 00:23:39 +0000 | [diff] [blame] | 269 | if (BUILTIN_EXPECT(OutBufCur >= OutBufEnd, false)) { |
| 270 | if (BUILTIN_EXPECT(!OutBufStart, false)) { |
Daniel Dunbar | 906d5b4 | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 271 | if (BufferMode == Unbuffered) { |
Dan Gohman | 3731604 | 2009-08-18 20:09:59 +0000 | [diff] [blame] | 272 | write_impl(reinterpret_cast<char*>(&C), 1); |
| 273 | return *this; |
| 274 | } |
Daniel Dunbar | ecc67e2 | 2009-08-19 00:23:39 +0000 | [diff] [blame] | 275 | // Set up a buffer and start over. |
| 276 | SetBuffered(); |
| 277 | return write(C); |
Dan Gohman | 3731604 | 2009-08-18 20:09:59 +0000 | [diff] [blame] | 278 | } |
Daniel Dunbar | ecc67e2 | 2009-08-19 00:23:39 +0000 | [diff] [blame] | 279 | |
| 280 | flush_nonempty(); |
Daniel Dunbar | d17d74b | 2009-03-17 01:13:35 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 283 | *OutBufCur++ = C; |
Daniel Dunbar | b451a0c | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 284 | return *this; |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 285 | } |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 286 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 287 | raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 288 | // Group exceptional cases into a single branch. |
Nick Lewycky | b1b051e | 2011-08-28 03:30:02 +0000 | [diff] [blame] | 289 | if (BUILTIN_EXPECT(size_t(OutBufEnd - OutBufCur) < Size, false)) { |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 290 | if (BUILTIN_EXPECT(!OutBufStart, false)) { |
Daniel Dunbar | 906d5b4 | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 291 | if (BufferMode == Unbuffered) { |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 292 | write_impl(Ptr, Size); |
| 293 | return *this; |
| 294 | } |
| 295 | // Set up a buffer and start over. |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 296 | SetBuffered(); |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 297 | return write(Ptr, Size); |
| 298 | } |
Daniel Dunbar | ecc67e2 | 2009-08-19 00:23:39 +0000 | [diff] [blame] | 299 | |
Benjamin Kramer | ca97144 | 2011-03-04 19:49:30 +0000 | [diff] [blame] | 300 | size_t NumBytes = OutBufEnd - OutBufCur; |
| 301 | |
Benjamin Kramer | e90756d | 2011-03-04 18:18:16 +0000 | [diff] [blame] | 302 | // If the buffer is empty at this point we have a string that is larger |
Benjamin Kramer | ca97144 | 2011-03-04 19:49:30 +0000 | [diff] [blame] | 303 | // than the buffer. Directly write the chunk that is a multiple of the |
| 304 | // preferred buffer size and put the remainder in the buffer. |
Benjamin Kramer | e90756d | 2011-03-04 18:18:16 +0000 | [diff] [blame] | 305 | if (BUILTIN_EXPECT(OutBufCur == OutBufStart, false)) { |
Benjamin Kramer | ca97144 | 2011-03-04 19:49:30 +0000 | [diff] [blame] | 306 | size_t BytesToWrite = Size - (Size % NumBytes); |
| 307 | write_impl(Ptr, BytesToWrite); |
| 308 | copy_to_buffer(Ptr + BytesToWrite, Size - BytesToWrite); |
Benjamin Kramer | e90756d | 2011-03-04 18:18:16 +0000 | [diff] [blame] | 309 | return *this; |
| 310 | } |
| 311 | |
| 312 | // We don't have enough space in the buffer to fit the string in. Insert as |
| 313 | // much as possible, flush and start over with the remainder. |
Benjamin Kramer | e90756d | 2011-03-04 18:18:16 +0000 | [diff] [blame] | 314 | copy_to_buffer(Ptr, NumBytes); |
| 315 | flush_nonempty(); |
| 316 | return write(Ptr + NumBytes, Size - NumBytes); |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 317 | } |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 318 | |
| 319 | copy_to_buffer(Ptr, Size); |
| 320 | |
| 321 | return *this; |
| 322 | } |
| 323 | |
| 324 | void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) { |
Dan Gohman | c6126e8 | 2009-08-13 20:32:03 +0000 | [diff] [blame] | 325 | assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!"); |
Dan Gohman | 4e8e642 | 2009-08-13 18:38:15 +0000 | [diff] [blame] | 326 | |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 327 | // Handle short strings specially, memcpy isn't very good at very short |
| 328 | // strings. |
| 329 | switch (Size) { |
| 330 | case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH |
| 331 | case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH |
| 332 | case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH |
| 333 | case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH |
| 334 | case 0: break; |
| 335 | default: |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 336 | memcpy(OutBufCur, Ptr, Size); |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 337 | break; |
| 338 | } |
Daniel Dunbar | e77e434 | 2009-03-10 16:21:55 +0000 | [diff] [blame] | 339 | |
Dan Gohman | 33e49ef | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 340 | OutBufCur += Size; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 343 | // Formatted output. |
| 344 | raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 345 | // If we have more than a few bytes left in our output buffer, try |
| 346 | // formatting directly onto its end. |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 347 | size_t NextBufferSize = 127; |
Daniel Dunbar | 9441cfe | 2009-08-23 20:31:39 +0000 | [diff] [blame] | 348 | size_t BufferBytesLeft = OutBufEnd - OutBufCur; |
| 349 | if (BufferBytesLeft > 3) { |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 350 | size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft); |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 351 | |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 352 | // Common case is that we have plenty of space. |
Daniel Dunbar | 9441cfe | 2009-08-23 20:31:39 +0000 | [diff] [blame] | 353 | if (BytesUsed <= BufferBytesLeft) { |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 354 | OutBufCur += BytesUsed; |
| 355 | return *this; |
| 356 | } |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 357 | |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 358 | // Otherwise, we overflowed and the return value tells us the size to try |
| 359 | // again with. |
| 360 | NextBufferSize = BytesUsed; |
| 361 | } |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 363 | // If we got here, we didn't have enough space in the output buffer for the |
| 364 | // string. Try printing into a SmallVector that is resized to have enough |
| 365 | // space. Iterate until we win. |
| 366 | SmallVector<char, 128> V; |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 367 | |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 368 | while (1) { |
| 369 | V.resize(NextBufferSize); |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 370 | |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 371 | // Try formatting into the SmallVector. |
Daniel Dunbar | 9441cfe | 2009-08-23 20:31:39 +0000 | [diff] [blame] | 372 | size_t BytesUsed = Fmt.print(V.data(), NextBufferSize); |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 373 | |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 374 | // If BytesUsed fit into the vector, we win. |
Chris Lattner | f61ca1e | 2008-10-26 19:20:47 +0000 | [diff] [blame] | 375 | if (BytesUsed <= NextBufferSize) |
Daniel Dunbar | 9441cfe | 2009-08-23 20:31:39 +0000 | [diff] [blame] | 376 | return write(V.data(), BytesUsed); |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 377 | |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 378 | // Otherwise, try again with a new size. |
| 379 | assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?"); |
| 380 | NextBufferSize = BytesUsed; |
| 381 | } |
| 382 | } |
| 383 | |
Chris Lattner | c5a227d | 2009-08-22 23:10:29 +0000 | [diff] [blame] | 384 | /// indent - Insert 'NumSpaces' spaces. |
| 385 | raw_ostream &raw_ostream::indent(unsigned NumSpaces) { |
Dan Gohman | 61ee100 | 2009-08-24 04:13:01 +0000 | [diff] [blame] | 386 | static const char Spaces[] = " " |
| 387 | " " |
| 388 | " "; |
Chris Lattner | c5a227d | 2009-08-22 23:10:29 +0000 | [diff] [blame] | 389 | |
| 390 | // Usually the indentation is small, handle it with a fastpath. |
Dan Gohman | cb1308b | 2009-08-24 04:43:38 +0000 | [diff] [blame] | 391 | if (NumSpaces < array_lengthof(Spaces)) |
Chris Lattner | c5a227d | 2009-08-22 23:10:29 +0000 | [diff] [blame] | 392 | return write(Spaces, NumSpaces); |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 393 | |
Chris Lattner | c5a227d | 2009-08-22 23:10:29 +0000 | [diff] [blame] | 394 | while (NumSpaces) { |
Dan Gohman | cb1308b | 2009-08-24 04:43:38 +0000 | [diff] [blame] | 395 | unsigned NumToWrite = std::min(NumSpaces, |
| 396 | (unsigned)array_lengthof(Spaces)-1); |
Chris Lattner | c5a227d | 2009-08-22 23:10:29 +0000 | [diff] [blame] | 397 | write(Spaces, NumToWrite); |
| 398 | NumSpaces -= NumToWrite; |
| 399 | } |
| 400 | return *this; |
| 401 | } |
| 402 | |
| 403 | |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 404 | //===----------------------------------------------------------------------===// |
| 405 | // Formatted Output |
| 406 | //===----------------------------------------------------------------------===// |
| 407 | |
| 408 | // Out of line virtual method. |
| 409 | void format_object_base::home() { |
| 410 | } |
| 411 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 412 | //===----------------------------------------------------------------------===// |
| 413 | // raw_fd_ostream |
| 414 | //===----------------------------------------------------------------------===// |
| 415 | |
Daniel Dunbar | 48534b3 | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 416 | /// raw_fd_ostream - Open the specified file for writing. If an error |
| 417 | /// occurs, information about the error is put into ErrorInfo, and the |
| 418 | /// stream should be immediately destroyed; the string will be empty |
| 419 | /// if no error occurred. |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 420 | raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo, |
Daniel Dunbar | b0cfa6c | 2011-02-03 03:32:32 +0000 | [diff] [blame] | 421 | unsigned Flags) |
| 422 | : Error(false), UseAtomicWrites(false), pos(0) |
| 423 | { |
Chris Lattner | f071d4e | 2010-03-05 00:49:08 +0000 | [diff] [blame] | 424 | assert(Filename != 0 && "Filename is null"); |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 425 | // Verify that we don't have both "append" and "excl". |
| 426 | assert((!(Flags & F_Excl) || !(Flags & F_Append)) && |
| 427 | "Cannot specify both 'excl' and 'append' file creation flags!"); |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 428 | |
Daniel Dunbar | 48534b3 | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 429 | ErrorInfo.clear(); |
| 430 | |
Dan Gohman | 2499990 | 2010-08-18 22:26:19 +0000 | [diff] [blame] | 431 | // Handle "-" as stdout. Note that when we do this, we consider ourself |
| 432 | // the owner of stdout. This means that we can do things like close the |
| 433 | // file descriptor when we're done and set the "binary" flag globally. |
Chris Lattner | c52b128 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 434 | if (Filename[0] == '-' && Filename[1] == 0) { |
| 435 | FD = STDOUT_FILENO; |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 436 | // If user requested binary then put stdout into binary mode if |
| 437 | // possible. |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 438 | if (Flags & F_Binary) |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 439 | sys::Program::ChangeStdoutToBinary(); |
Dan Gohman | 2499990 | 2010-08-18 22:26:19 +0000 | [diff] [blame] | 440 | // Close stdout when we're done, to detect any output errors. |
| 441 | ShouldClose = true; |
Chris Lattner | c52b128 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 442 | return; |
| 443 | } |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 444 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 445 | int OpenFlags = O_WRONLY|O_CREAT; |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 446 | #ifdef O_BINARY |
Benjamin Kramer | ae60324 | 2009-08-23 08:57:52 +0000 | [diff] [blame] | 447 | if (Flags & F_Binary) |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 448 | OpenFlags |= O_BINARY; |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 449 | #endif |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 450 | |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 451 | if (Flags & F_Append) |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 452 | OpenFlags |= O_APPEND; |
| 453 | else |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 454 | OpenFlags |= O_TRUNC; |
| 455 | if (Flags & F_Excl) |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 456 | OpenFlags |= O_EXCL; |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 457 | |
Dan Gohman | 0657279 | 2010-05-06 02:06:20 +0000 | [diff] [blame] | 458 | while ((FD = open(Filename, OpenFlags, 0664)) < 0) { |
| 459 | if (errno != EINTR) { |
| 460 | ErrorInfo = "Error opening output file '" + std::string(Filename) + "'"; |
| 461 | ShouldClose = false; |
| 462 | return; |
| 463 | } |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 464 | } |
Dan Gohman | 0657279 | 2010-05-06 02:06:20 +0000 | [diff] [blame] | 465 | |
| 466 | // Ok, we successfully opened the file, so it'll need to be closed. |
| 467 | ShouldClose = true; |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Francois Pichet | a872a0f | 2010-10-14 20:30:58 +0000 | [diff] [blame] | 470 | /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If |
| 471 | /// ShouldClose is true, this closes the file when the stream is destroyed. |
| 472 | raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered) |
| 473 | : raw_ostream(unbuffered), FD(fd), |
Daniel Dunbar | b0cfa6c | 2011-02-03 03:32:32 +0000 | [diff] [blame] | 474 | ShouldClose(shouldClose), Error(false), UseAtomicWrites(false) { |
Francois Pichet | a872a0f | 2010-10-14 20:30:58 +0000 | [diff] [blame] | 475 | #ifdef O_BINARY |
| 476 | // Setting STDOUT and STDERR to binary mode is necessary in Win32 |
| 477 | // to avoid undesirable linefeed conversion. |
| 478 | if (fd == STDOUT_FILENO || fd == STDERR_FILENO) |
| 479 | setmode(fd, O_BINARY); |
| 480 | #endif |
Michael J. Spencer | 2634a54 | 2011-01-17 15:53:12 +0000 | [diff] [blame] | 481 | |
| 482 | // Get the starting position. |
| 483 | off_t loc = ::lseek(FD, 0, SEEK_CUR); |
| 484 | if (loc == (off_t)-1) |
| 485 | pos = 0; |
| 486 | else |
| 487 | pos = static_cast<uint64_t>(loc); |
Francois Pichet | a872a0f | 2010-10-14 20:30:58 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 490 | raw_fd_ostream::~raw_fd_ostream() { |
Dan Gohman | 8df0e01 | 2010-08-20 16:34:20 +0000 | [diff] [blame] | 491 | if (FD >= 0) { |
| 492 | flush(); |
| 493 | if (ShouldClose) |
| 494 | while (::close(FD) != 0) |
| 495 | if (errno != EINTR) { |
| 496 | error_detected(); |
| 497 | break; |
| 498 | } |
| 499 | } |
| 500 | |
NAKAMURA Takumi | 7a7215b | 2011-03-16 02:53:39 +0000 | [diff] [blame] | 501 | #ifdef __MINGW32__ |
| 502 | // On mingw, global dtors should not call exit(). |
| 503 | // report_fatal_error() invokes exit(). We know report_fatal_error() |
| 504 | // might not write messages to stderr when any errors were detected |
| 505 | // on FD == 2. |
| 506 | if (FD == 2) return; |
| 507 | #endif |
| 508 | |
Dan Gohman | 8df0e01 | 2010-08-20 16:34:20 +0000 | [diff] [blame] | 509 | // If there are any pending errors, report them now. Clients wishing |
| 510 | // to avoid report_fatal_error calls should check for errors with |
| 511 | // has_error() and clear the error flag with clear_error() before |
| 512 | // destructing raw_ostream objects which may have errors. |
| 513 | if (has_error()) |
| 514 | report_fatal_error("IO failure on output stream."); |
Chris Lattner | 93d81b6 | 2010-08-17 23:11:56 +0000 | [diff] [blame] | 515 | } |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 516 | |
Dan Gohman | d2da327 | 2010-08-18 01:34:52 +0000 | [diff] [blame] | 517 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 518 | void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) { |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 519 | assert(FD >= 0 && "File already closed."); |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 520 | pos += Size; |
Dan Gohman | 5fe89d0 | 2010-05-06 01:27:36 +0000 | [diff] [blame] | 521 | |
Benjamin Kramer | 66760be | 2010-05-05 15:17:47 +0000 | [diff] [blame] | 522 | do { |
Daniel Dunbar | b0cfa6c | 2011-02-03 03:32:32 +0000 | [diff] [blame] | 523 | ssize_t ret; |
| 524 | |
| 525 | // Check whether we should attempt to use atomic writes. |
| 526 | if (BUILTIN_EXPECT(!UseAtomicWrites, true)) { |
| 527 | ret = ::write(FD, Ptr, Size); |
| 528 | } else { |
| 529 | // Use ::writev() where available. |
| 530 | #if defined(HAVE_WRITEV) |
| 531 | struct iovec IOV = { (void*) Ptr, Size }; |
| 532 | ret = ::writev(FD, &IOV, 1); |
| 533 | #else |
| 534 | ret = ::write(FD, Ptr, Size); |
| 535 | #endif |
| 536 | } |
Dan Gohman | 5fe89d0 | 2010-05-06 01:27:36 +0000 | [diff] [blame] | 537 | |
| 538 | if (ret < 0) { |
| 539 | // If it's a recoverable error, swallow it and retry the write. |
Dan Gohman | 68e947a | 2010-05-18 15:25:14 +0000 | [diff] [blame] | 540 | // |
| 541 | // Ideally we wouldn't ever see EAGAIN or EWOULDBLOCK here, since |
| 542 | // raw_ostream isn't designed to do non-blocking I/O. However, some |
| 543 | // programs, such as old versions of bjam, have mistakenly used |
| 544 | // O_NONBLOCK. For compatibility, emulate blocking semantics by |
| 545 | // spinning until the write succeeds. If you don't want spinning, |
| 546 | // don't use O_NONBLOCK file descriptors with raw_ostream. |
Dan Gohman | 0657279 | 2010-05-06 02:06:20 +0000 | [diff] [blame] | 547 | if (errno == EINTR || errno == EAGAIN |
| 548 | #ifdef EWOULDBLOCK |
| 549 | || errno == EWOULDBLOCK |
| 550 | #endif |
| 551 | ) |
Dan Gohman | 5fe89d0 | 2010-05-06 01:27:36 +0000 | [diff] [blame] | 552 | continue; |
| 553 | |
| 554 | // Otherwise it's a non-recoverable error. Note it and quit. |
| 555 | error_detected(); |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | // The write may have written some or all of the data. Update the |
| 560 | // size and buffer pointer to reflect the remainder that needs |
| 561 | // to be written. If there are no bytes left, we're done. |
| 562 | Ptr += ret; |
| 563 | Size -= ret; |
| 564 | } while (Size > 0); |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Dan Gohman | d2da327 | 2010-08-18 01:34:52 +0000 | [diff] [blame] | 567 | void raw_fd_ostream::close() { |
| 568 | assert(ShouldClose); |
| 569 | ShouldClose = false; |
| 570 | flush(); |
| 571 | while (::close(FD) != 0) |
| 572 | if (errno != EINTR) { |
| 573 | error_detected(); |
| 574 | break; |
| 575 | } |
| 576 | FD = -1; |
| 577 | } |
| 578 | |
Ted Kremenek | 9c27886 | 2009-01-26 21:42:04 +0000 | [diff] [blame] | 579 | uint64_t raw_fd_ostream::seek(uint64_t off) { |
| 580 | flush(); |
Daniel Dunbar | 579fb87 | 2009-07-15 08:11:46 +0000 | [diff] [blame] | 581 | pos = ::lseek(FD, off, SEEK_SET); |
Dan Gohman | d3a974f | 2009-07-15 16:43:01 +0000 | [diff] [blame] | 582 | if (pos != off) |
Dan Gohman | e87b2ab | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 583 | error_detected(); |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 584 | return pos; |
Ted Kremenek | 9c27886 | 2009-01-26 21:42:04 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Chris Lattner | cd0129f | 2009-12-19 01:38:42 +0000 | [diff] [blame] | 587 | size_t raw_fd_ostream::preferred_buffer_size() const { |
Chris Lattner | 29269d0 | 2010-07-07 15:52:27 +0000 | [diff] [blame] | 588 | #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__minix) |
Chris Lattner | 21aa347 | 2010-04-09 20:45:04 +0000 | [diff] [blame] | 589 | // Windows and Minix have no st_blksize. |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 590 | assert(FD >= 0 && "File not yet open!"); |
| 591 | struct stat statbuf; |
Chris Lattner | cd0129f | 2009-12-19 01:38:42 +0000 | [diff] [blame] | 592 | if (fstat(FD, &statbuf) != 0) |
| 593 | return 0; |
Dan Gohman | 16e0209 | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 594 | |
Chris Lattner | cd0129f | 2009-12-19 01:38:42 +0000 | [diff] [blame] | 595 | // If this is a terminal, don't use buffering. Line buffering |
| 596 | // would be a more traditional thing to do, but it's not worth |
| 597 | // the complexity. |
| 598 | if (S_ISCHR(statbuf.st_mode) && isatty(FD)) |
| 599 | return 0; |
| 600 | // Return the preferred block size. |
| 601 | return statbuf.st_blksize; |
Dan Gohman | 9ba7f65 | 2010-05-28 16:50:01 +0000 | [diff] [blame] | 602 | #else |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 603 | return raw_ostream::preferred_buffer_size(); |
Dan Gohman | 9ba7f65 | 2010-05-28 16:50:01 +0000 | [diff] [blame] | 604 | #endif |
Dan Gohman | 208ec0f | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 607 | raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold, |
| 608 | bool bg) { |
| 609 | if (sys::Process::ColorNeedsFlush()) |
| 610 | flush(); |
| 611 | const char *colorcode = |
| 612 | (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg) |
| 613 | : sys::Process::OutputColor(colors, bold, bg); |
| 614 | if (colorcode) { |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 615 | size_t len = strlen(colorcode); |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 616 | write(colorcode, len); |
| 617 | // don't account colors towards output characters |
| 618 | pos -= len; |
| 619 | } |
| 620 | return *this; |
| 621 | } |
| 622 | |
| 623 | raw_ostream &raw_fd_ostream::resetColor() { |
| 624 | if (sys::Process::ColorNeedsFlush()) |
| 625 | flush(); |
| 626 | const char *colorcode = sys::Process::ResetColor(); |
| 627 | if (colorcode) { |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 628 | size_t len = strlen(colorcode); |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 629 | write(colorcode, len); |
| 630 | // don't account colors towards output characters |
| 631 | pos -= len; |
| 632 | } |
| 633 | return *this; |
| 634 | } |
| 635 | |
Benjamin Kramer | 246de85 | 2012-04-16 08:56:50 +0000 | [diff] [blame] | 636 | raw_ostream &raw_fd_ostream::reverseColor() { |
| 637 | if (sys::Process::ColorNeedsFlush()) |
| 638 | flush(); |
| 639 | const char *colorcode = sys::Process::OutputReverse(); |
| 640 | if (colorcode) { |
| 641 | size_t len = strlen(colorcode); |
| 642 | write(colorcode, len); |
| 643 | // don't account colors towards output characters |
| 644 | pos -= len; |
| 645 | } |
| 646 | return *this; |
| 647 | } |
| 648 | |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 649 | bool raw_fd_ostream::is_displayed() const { |
| 650 | return sys::Process::FileDescriptorIsDisplayed(FD); |
| 651 | } |
| 652 | |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 653 | //===----------------------------------------------------------------------===// |
Dan Gohman | 5d56d9d | 2010-08-20 16:44:56 +0000 | [diff] [blame] | 654 | // outs(), errs(), nulls() |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 655 | //===----------------------------------------------------------------------===// |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 656 | |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 657 | /// outs() - This returns a reference to a raw_ostream for standard output. |
| 658 | /// Use it like: outs() << "foo" << "bar"; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 659 | raw_ostream &llvm::outs() { |
Dan Gohman | a1f89de | 2010-08-20 16:39:41 +0000 | [diff] [blame] | 660 | // Set buffer settings to model stdout behavior. |
Dan Gohman | 5d56d9d | 2010-08-20 16:44:56 +0000 | [diff] [blame] | 661 | // Delete the file descriptor when the program exists, forcing error |
| 662 | // detection. If you don't want this behavior, don't use outs(). |
| 663 | static raw_fd_ostream S(STDOUT_FILENO, true); |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 664 | return S; |
| 665 | } |
| 666 | |
| 667 | /// errs() - This returns a reference to a raw_ostream for standard error. |
| 668 | /// Use it like: errs() << "foo" << "bar"; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 669 | raw_ostream &llvm::errs() { |
Dan Gohman | a1f89de | 2010-08-20 16:39:41 +0000 | [diff] [blame] | 670 | // Set standard error to be unbuffered by default. |
| 671 | static raw_fd_ostream S(STDERR_FILENO, false, true); |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 672 | return S; |
| 673 | } |
| 674 | |
Daniel Dunbar | 34ccf03 | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 675 | /// nulls() - This returns a reference to a raw_ostream which discards output. |
| 676 | raw_ostream &llvm::nulls() { |
| 677 | static raw_null_ostream S; |
| 678 | return S; |
| 679 | } |
| 680 | |
Douglas Gregor | 8f7be47 | 2009-04-20 07:34:17 +0000 | [diff] [blame] | 681 | |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 682 | //===----------------------------------------------------------------------===// |
| 683 | // raw_string_ostream |
| 684 | //===----------------------------------------------------------------------===// |
| 685 | |
Daniel Dunbar | 35979c0 | 2009-08-18 20:07:36 +0000 | [diff] [blame] | 686 | raw_string_ostream::~raw_string_ostream() { |
| 687 | flush(); |
| 688 | } |
| 689 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 690 | void raw_string_ostream::write_impl(const char *Ptr, size_t Size) { |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 691 | OS.append(Ptr, Size); |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | //===----------------------------------------------------------------------===// |
| 695 | // raw_svector_ostream |
| 696 | //===----------------------------------------------------------------------===// |
| 697 | |
Daniel Dunbar | 425d08c | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 698 | // The raw_svector_ostream implementation uses the SmallVector itself as the |
| 699 | // buffer for the raw_ostream. We guarantee that the raw_ostream buffer is |
| 700 | // always pointing past the end of the vector, but within the vector |
| 701 | // capacity. This allows raw_ostream to write directly into the correct place, |
| 702 | // and we only need to set the vector size when the data is flushed. |
| 703 | |
Daniel Dunbar | 906d5b4 | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 704 | raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) { |
Daniel Dunbar | d14787e | 2009-08-19 18:40:58 +0000 | [diff] [blame] | 705 | // 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] | 706 | // least 128 bytes free; raw_ostream itself only requires 64, but we want to |
| 707 | // make sure that we don't grow the buffer unnecessarily on destruction (when |
| 708 | // the data is flushed). See the FIXME below. |
Daniel Dunbar | d14787e | 2009-08-19 18:40:58 +0000 | [diff] [blame] | 709 | OS.reserve(OS.size() + 128); |
Daniel Dunbar | 425d08c | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 710 | SetBuffer(OS.end(), OS.capacity() - OS.size()); |
Daniel Dunbar | 906d5b4 | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Daniel Dunbar | 35979c0 | 2009-08-18 20:07:36 +0000 | [diff] [blame] | 713 | raw_svector_ostream::~raw_svector_ostream() { |
Daniel Dunbar | 425d08c | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 714 | // FIXME: Prevent resizing during this flush(). |
Daniel Dunbar | 35979c0 | 2009-08-18 20:07:36 +0000 | [diff] [blame] | 715 | flush(); |
| 716 | } |
| 717 | |
Chris Lattner | 14ca177 | 2010-01-22 21:16:10 +0000 | [diff] [blame] | 718 | /// resync - This is called when the SmallVector we're appending to is changed |
| 719 | /// outside of the raw_svector_ostream's control. It is only safe to do this |
| 720 | /// if the raw_svector_ostream has previously been flushed. |
| 721 | void raw_svector_ostream::resync() { |
| 722 | assert(GetNumBytesInBuffer() == 0 && "Didn't flush before mutating vector"); |
| 723 | |
| 724 | if (OS.capacity() - OS.size() < 64) |
| 725 | OS.reserve(OS.capacity() * 2); |
Chris Lattner | d79d9dc | 2010-01-22 19:17:48 +0000 | [diff] [blame] | 726 | SetBuffer(OS.end(), OS.capacity() - OS.size()); |
| 727 | } |
| 728 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 729 | void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) { |
Chris Lattner | 17f26b4 | 2010-02-15 02:18:26 +0000 | [diff] [blame] | 730 | // If we're writing bytes from the end of the buffer into the smallvector, we |
| 731 | // don't need to copy the bytes, just commit the bytes because they are |
| 732 | // already in the right place. |
| 733 | if (Ptr == OS.end()) { |
| 734 | assert(OS.size() + Size <= OS.capacity() && "Invalid write_impl() call!"); |
| 735 | OS.set_size(OS.size() + Size); |
| 736 | } else { |
| 737 | assert(GetNumBytesInBuffer() == 0 && |
| 738 | "Should be writing from buffer if some bytes in it"); |
| 739 | // Otherwise, do copy the bytes. |
| 740 | OS.append(Ptr, Ptr+Size); |
| 741 | } |
Daniel Dunbar | 425d08c | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 742 | |
| 743 | // Grow the vector if necessary. |
| 744 | if (OS.capacity() - OS.size() < 64) |
| 745 | OS.reserve(OS.capacity() * 2); |
| 746 | |
| 747 | // Update the buffer position. |
| 748 | SetBuffer(OS.end(), OS.capacity() - OS.size()); |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Chris Lattner | cd0129f | 2009-12-19 01:38:42 +0000 | [diff] [blame] | 751 | uint64_t raw_svector_ostream::current_pos() const { |
| 752 | return OS.size(); |
| 753 | } |
Douglas Gregor | 8f7be47 | 2009-04-20 07:34:17 +0000 | [diff] [blame] | 754 | |
Daniel Dunbar | d14787e | 2009-08-19 18:40:58 +0000 | [diff] [blame] | 755 | StringRef raw_svector_ostream::str() { |
| 756 | flush(); |
| 757 | return StringRef(OS.begin(), OS.size()); |
| 758 | } |
| 759 | |
Daniel Dunbar | 34ccf03 | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 760 | //===----------------------------------------------------------------------===// |
| 761 | // raw_null_ostream |
| 762 | //===----------------------------------------------------------------------===// |
| 763 | |
Dan Gohman | f78c835 | 2009-07-27 21:46:02 +0000 | [diff] [blame] | 764 | raw_null_ostream::~raw_null_ostream() { |
| 765 | #ifndef NDEBUG |
| 766 | // ~raw_ostream asserts that the buffer is empty. This isn't necessary |
| 767 | // with raw_null_ostream, but it's better to have raw_null_ostream follow |
| 768 | // the rules than to change the rules just for raw_null_ostream. |
| 769 | flush(); |
| 770 | #endif |
| 771 | } |
| 772 | |
Daniel Dunbar | 34ccf03 | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 773 | void raw_null_ostream::write_impl(const char *Ptr, size_t Size) { |
| 774 | } |
| 775 | |
Chris Lattner | cd0129f | 2009-12-19 01:38:42 +0000 | [diff] [blame] | 776 | uint64_t raw_null_ostream::current_pos() const { |
Daniel Dunbar | 34ccf03 | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 777 | return 0; |
| 778 | } |