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" |
Rafael Espindola | 6d35481 | 2013-07-16 19:44:17 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FileSystem.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Format.h" |
Nick Kledzik | e648037 | 2014-09-25 20:30:58 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MathExtras.h" |
Zachary Turner | 733be51 | 2016-10-11 19:24:45 +0000 | [diff] [blame] | 24 | #include "llvm/Support/NativeFormatting.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Process.h" |
| 26 | #include "llvm/Support/Program.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Benjamin Kramer | ef14f80 | 2010-01-29 15:19:06 +0000 | [diff] [blame] | 28 | #include <cctype> |
Benjamin Kramer | ce84a25 | 2010-05-05 15:17:47 +0000 | [diff] [blame] | 29 | #include <cerrno> |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 30 | #include <cstdio> |
| 31 | #include <iterator> |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 32 | #include <sys/stat.h> |
Zachary Turner | 733be51 | 2016-10-11 19:24:45 +0000 | [diff] [blame] | 33 | #include <system_error> |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 34 | |
NAKAMURA Takumi | 212c80a | 2013-07-17 02:21:10 +0000 | [diff] [blame] | 35 | // <fcntl.h> may provide O_BINARY. |
| 36 | #if defined(HAVE_FCNTL_H) |
| 37 | # include <fcntl.h> |
| 38 | #endif |
| 39 | |
Chris Lattner | d564f29 | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 40 | #if defined(HAVE_UNISTD_H) |
| 41 | # include <unistd.h> |
| 42 | #endif |
Daniel Dunbar | 4fed887 | 2011-02-03 03:32:32 +0000 | [diff] [blame] | 43 | #if defined(HAVE_SYS_UIO_H) && defined(HAVE_WRITEV) |
| 44 | # include <sys/uio.h> |
| 45 | #endif |
Argyrios Kyrtzidis | b97ff82 | 2008-08-17 09:25:21 +0000 | [diff] [blame] | 46 | |
NAKAMURA Takumi | fe84f39 | 2010-10-19 01:21:55 +0000 | [diff] [blame] | 47 | #if defined(__CYGWIN__) |
| 48 | #include <io.h> |
| 49 | #endif |
| 50 | |
Argyrios Kyrtzidis | b97ff82 | 2008-08-17 09:25:21 +0000 | [diff] [blame] | 51 | #if defined(_MSC_VER) |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 52 | #include <io.h> |
Owen Anderson | 9371964 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 53 | #ifndef STDIN_FILENO |
| 54 | # define STDIN_FILENO 0 |
| 55 | #endif |
| 56 | #ifndef STDOUT_FILENO |
| 57 | # define STDOUT_FILENO 1 |
| 58 | #endif |
| 59 | #ifndef STDERR_FILENO |
| 60 | # define STDERR_FILENO 2 |
| 61 | #endif |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 62 | #endif |
| 63 | |
Yunzhong Gao | fb2a9c4 | 2016-01-06 00:50:06 +0000 | [diff] [blame] | 64 | #ifdef LLVM_ON_WIN32 |
| 65 | #include "Windows/WindowsSupport.h" |
| 66 | #endif |
| 67 | |
Chris Lattner | d564f29 | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 68 | using namespace llvm; |
| 69 | |
Dan Gohman | 58fcef9 | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 70 | raw_ostream::~raw_ostream() { |
Dan Gohman | 1b76329 | 2009-07-27 20:49:44 +0000 | [diff] [blame] | 71 | // raw_ostream's subclasses should take care to flush the buffer |
| 72 | // in their destructors. |
| 73 | assert(OutBufCur == OutBufStart && |
| 74 | "raw_ostream destructor called with non-empty buffer!"); |
| 75 | |
Daniel Dunbar | 317a6cd | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 76 | if (BufferMode == InternalBuffer) |
| 77 | delete [] OutBufStart; |
Dan Gohman | 58fcef9 | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 78 | } |
Chris Lattner | d564f29 | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 80 | // An out of line virtual method to provide a home for the class vtable. |
| 81 | void raw_ostream::handle() {} |
| 82 | |
Chris Lattner | dd3e9aa | 2009-12-19 01:38:42 +0000 | [diff] [blame] | 83 | size_t raw_ostream::preferred_buffer_size() const { |
Dan Gohman | 84487b9 | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 84 | // BUFSIZ is intended to be a reasonable default. |
| 85 | return BUFSIZ; |
| 86 | } |
| 87 | |
| 88 | void raw_ostream::SetBuffered() { |
| 89 | // Ask the subclass to determine an appropriate buffer size. |
Dan Gohman | 1771022 | 2009-08-15 02:05:19 +0000 | [diff] [blame] | 90 | if (size_t Size = preferred_buffer_size()) |
| 91 | SetBufferSize(Size); |
| 92 | else |
| 93 | // It may return 0, meaning this stream should be unbuffered. |
| 94 | SetUnbuffered(); |
Dan Gohman | 84487b9 | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 97 | void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size, |
Nick Lewycky | 7bfd86d | 2011-08-28 03:30:02 +0000 | [diff] [blame] | 98 | BufferKind Mode) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 99 | assert(((Mode == Unbuffered && !BufferStart && Size == 0) || |
| 100 | (Mode != Unbuffered && BufferStart && Size != 0)) && |
Daniel Dunbar | 316b4a0 | 2009-09-15 20:31:46 +0000 | [diff] [blame] | 101 | "stream must be unbuffered or have at least one byte"); |
Daniel Dunbar | 317a6cd | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 102 | // Make sure the current buffer is free of content (we can't flush here; the |
| 103 | // child buffer management logic will be in write_impl). |
| 104 | assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!"); |
Dan Gohman | 54401d4 | 2009-08-13 15:58:55 +0000 | [diff] [blame] | 105 | |
Daniel Dunbar | 317a6cd | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 106 | if (BufferMode == InternalBuffer) |
| 107 | delete [] OutBufStart; |
| 108 | OutBufStart = BufferStart; |
Dan Gohman | 54401d4 | 2009-08-13 15:58:55 +0000 | [diff] [blame] | 109 | OutBufEnd = OutBufStart+Size; |
| 110 | OutBufCur = OutBufStart; |
Daniel Dunbar | 317a6cd | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 111 | BufferMode = Mode; |
Daniel Dunbar | b090bf4 | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 112 | |
| 113 | assert(OutBufStart <= OutBufEnd && "Invalid size!"); |
Dan Gohman | 54401d4 | 2009-08-13 15:58:55 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 116 | raw_ostream &raw_ostream::operator<<(unsigned long N) { |
Zachary Turner | 5b2243e | 2016-10-29 00:27:22 +0000 | [diff] [blame] | 117 | write_integer(*this, static_cast<uint64_t>(N), IntegerStyle::Integer); |
Zachary Turner | 733be51 | 2016-10-11 19:24:45 +0000 | [diff] [blame] | 118 | return *this; |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | raw_ostream &raw_ostream::operator<<(long N) { |
Zachary Turner | 5b2243e | 2016-10-29 00:27:22 +0000 | [diff] [blame] | 122 | write_integer(*this, static_cast<int64_t>(N), IntegerStyle::Integer); |
Zachary Turner | 733be51 | 2016-10-11 19:24:45 +0000 | [diff] [blame] | 123 | return *this; |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | raw_ostream &raw_ostream::operator<<(unsigned long long N) { |
Zachary Turner | 5b2243e | 2016-10-29 00:27:22 +0000 | [diff] [blame] | 127 | write_integer(*this, static_cast<uint64_t>(N), IntegerStyle::Integer); |
Zachary Turner | 733be51 | 2016-10-11 19:24:45 +0000 | [diff] [blame] | 128 | return *this; |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | raw_ostream &raw_ostream::operator<<(long long N) { |
Zachary Turner | 5b2243e | 2016-10-29 00:27:22 +0000 | [diff] [blame] | 132 | write_integer(*this, static_cast<int64_t>(N), IntegerStyle::Integer); |
Zachary Turner | 733be51 | 2016-10-11 19:24:45 +0000 | [diff] [blame] | 133 | return *this; |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Daniel Dunbar | 6c9629b | 2009-07-30 18:21:23 +0000 | [diff] [blame] | 136 | raw_ostream &raw_ostream::write_hex(unsigned long long N) { |
Zachary Turner | 5b2243e | 2016-10-29 00:27:22 +0000 | [diff] [blame] | 137 | llvm::write_hex(*this, N, HexPrintStyle::Lower); |
Zachary Turner | 733be51 | 2016-10-11 19:24:45 +0000 | [diff] [blame] | 138 | return *this; |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Daniel Dunbar | 65dc891 | 2010-11-27 07:59:50 +0000 | [diff] [blame] | 141 | raw_ostream &raw_ostream::write_escaped(StringRef Str, |
| 142 | bool UseHexEscapes) { |
Craig Topper | 775fb73 | 2016-02-04 06:51:41 +0000 | [diff] [blame] | 143 | for (unsigned char c : Str) { |
Daniel Dunbar | 4108c43 | 2009-10-17 20:43:08 +0000 | [diff] [blame] | 144 | switch (c) { |
| 145 | case '\\': |
| 146 | *this << '\\' << '\\'; |
| 147 | break; |
| 148 | case '\t': |
| 149 | *this << '\\' << 't'; |
| 150 | break; |
| 151 | case '\n': |
| 152 | *this << '\\' << 'n'; |
| 153 | break; |
| 154 | case '"': |
| 155 | *this << '\\' << '"'; |
| 156 | break; |
| 157 | default: |
| 158 | if (std::isprint(c)) { |
| 159 | *this << c; |
| 160 | break; |
| 161 | } |
| 162 | |
Daniel Dunbar | 65dc891 | 2010-11-27 07:59:50 +0000 | [diff] [blame] | 163 | // Write out the escaped representation. |
| 164 | if (UseHexEscapes) { |
| 165 | *this << '\\' << 'x'; |
| 166 | *this << hexdigit((c >> 4 & 0xF)); |
| 167 | *this << hexdigit((c >> 0) & 0xF); |
| 168 | } else { |
| 169 | // Always use a full 3-character octal escape. |
| 170 | *this << '\\'; |
| 171 | *this << char('0' + ((c >> 6) & 7)); |
| 172 | *this << char('0' + ((c >> 3) & 7)); |
| 173 | *this << char('0' + ((c >> 0) & 7)); |
| 174 | } |
Daniel Dunbar | 4108c43 | 2009-10-17 20:43:08 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | return *this; |
| 179 | } |
| 180 | |
Daniel Dunbar | 6c9629b | 2009-07-30 18:21:23 +0000 | [diff] [blame] | 181 | raw_ostream &raw_ostream::operator<<(const void *P) { |
Zachary Turner | 5b2243e | 2016-10-29 00:27:22 +0000 | [diff] [blame] | 182 | llvm::write_hex(*this, (uintptr_t)P, HexPrintStyle::PrefixLower); |
Zachary Turner | 733be51 | 2016-10-11 19:24:45 +0000 | [diff] [blame] | 183 | return *this; |
Daniel Dunbar | 6c9629b | 2009-07-30 18:21:23 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Chris Lattner | 06fa176 | 2009-08-24 03:52:50 +0000 | [diff] [blame] | 186 | raw_ostream &raw_ostream::operator<<(double N) { |
Zachary Turner | 5b2243e | 2016-10-29 00:27:22 +0000 | [diff] [blame] | 187 | llvm::write_double(*this, N, FloatStyle::Exponent); |
Zachary Turner | 733be51 | 2016-10-11 19:24:45 +0000 | [diff] [blame] | 188 | return *this; |
Chris Lattner | 06fa176 | 2009-08-24 03:52:50 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Daniel Dunbar | d24535f | 2009-03-16 22:55:06 +0000 | [diff] [blame] | 191 | void raw_ostream::flush_nonempty() { |
| 192 | assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty."); |
Daniel Dunbar | 317a6cd | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 193 | size_t Length = OutBufCur - OutBufStart; |
| 194 | OutBufCur = OutBufStart; |
| 195 | write_impl(OutBufStart, Length); |
Daniel Dunbar | d24535f | 2009-03-16 22:55:06 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Daniel Dunbar | 7a9bb9e | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 198 | raw_ostream &raw_ostream::write(unsigned char C) { |
Daniel Dunbar | 64fa386 | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 199 | // Group exceptional cases into a single branch. |
Benjamin Kramer | bd7f8d0 | 2012-08-29 22:57:00 +0000 | [diff] [blame] | 200 | if (LLVM_UNLIKELY(OutBufCur >= OutBufEnd)) { |
| 201 | if (LLVM_UNLIKELY(!OutBufStart)) { |
Daniel Dunbar | 317a6cd | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 202 | if (BufferMode == Unbuffered) { |
Dan Gohman | 23f90c1 | 2009-08-18 20:09:59 +0000 | [diff] [blame] | 203 | write_impl(reinterpret_cast<char*>(&C), 1); |
| 204 | return *this; |
| 205 | } |
Daniel Dunbar | 0cf1686 | 2009-08-19 00:23:39 +0000 | [diff] [blame] | 206 | // Set up a buffer and start over. |
| 207 | SetBuffered(); |
| 208 | return write(C); |
Dan Gohman | 23f90c1 | 2009-08-18 20:09:59 +0000 | [diff] [blame] | 209 | } |
Daniel Dunbar | 0cf1686 | 2009-08-19 00:23:39 +0000 | [diff] [blame] | 210 | |
| 211 | flush_nonempty(); |
Daniel Dunbar | 2d603da | 2009-03-17 01:13:35 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Daniel Dunbar | 7a9bb9e | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 214 | *OutBufCur++ = C; |
Daniel Dunbar | 8786218 | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 215 | return *this; |
Daniel Dunbar | 7a9bb9e | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 216 | } |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 217 | |
Dan Gohman | f199ad6 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 218 | raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) { |
Daniel Dunbar | 64fa386 | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 219 | // Group exceptional cases into a single branch. |
Benjamin Kramer | bd7f8d0 | 2012-08-29 22:57:00 +0000 | [diff] [blame] | 220 | if (LLVM_UNLIKELY(size_t(OutBufEnd - OutBufCur) < Size)) { |
| 221 | if (LLVM_UNLIKELY(!OutBufStart)) { |
Daniel Dunbar | 317a6cd | 2009-08-18 23:42:36 +0000 | [diff] [blame] | 222 | if (BufferMode == Unbuffered) { |
Dan Gohman | 52022c2 | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 223 | write_impl(Ptr, Size); |
| 224 | return *this; |
| 225 | } |
| 226 | // Set up a buffer and start over. |
Dan Gohman | 84487b9 | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 227 | SetBuffered(); |
Dan Gohman | 52022c2 | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 228 | return write(Ptr, Size); |
| 229 | } |
Daniel Dunbar | 0cf1686 | 2009-08-19 00:23:39 +0000 | [diff] [blame] | 230 | |
Benjamin Kramer | dfb0ad3 | 2011-03-04 19:49:30 +0000 | [diff] [blame] | 231 | size_t NumBytes = OutBufEnd - OutBufCur; |
| 232 | |
Benjamin Kramer | acf0842 | 2011-03-04 18:18:16 +0000 | [diff] [blame] | 233 | // 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] | 234 | // than the buffer. Directly write the chunk that is a multiple of the |
| 235 | // preferred buffer size and put the remainder in the buffer. |
Benjamin Kramer | bd7f8d0 | 2012-08-29 22:57:00 +0000 | [diff] [blame] | 236 | if (LLVM_UNLIKELY(OutBufCur == OutBufStart)) { |
Michael Ilseman | 5be22a1 | 2014-12-12 21:48:03 +0000 | [diff] [blame] | 237 | assert(NumBytes != 0 && "undefined behavior"); |
Benjamin Kramer | dfb0ad3 | 2011-03-04 19:49:30 +0000 | [diff] [blame] | 238 | size_t BytesToWrite = Size - (Size % NumBytes); |
| 239 | write_impl(Ptr, BytesToWrite); |
Matt Beaumont-Gay | a182be8 | 2013-03-12 23:55:24 +0000 | [diff] [blame] | 240 | size_t BytesRemaining = Size - BytesToWrite; |
| 241 | if (BytesRemaining > size_t(OutBufEnd - OutBufCur)) { |
| 242 | // Too much left over to copy into our buffer. |
| 243 | return write(Ptr + BytesToWrite, BytesRemaining); |
| 244 | } |
| 245 | copy_to_buffer(Ptr + BytesToWrite, BytesRemaining); |
Benjamin Kramer | acf0842 | 2011-03-04 18:18:16 +0000 | [diff] [blame] | 246 | return *this; |
| 247 | } |
| 248 | |
| 249 | // We don't have enough space in the buffer to fit the string in. Insert as |
| 250 | // much as possible, flush and start over with the remainder. |
Benjamin Kramer | acf0842 | 2011-03-04 18:18:16 +0000 | [diff] [blame] | 251 | copy_to_buffer(Ptr, NumBytes); |
| 252 | flush_nonempty(); |
| 253 | return write(Ptr + NumBytes, Size - NumBytes); |
Daniel Dunbar | 64fa386 | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 254 | } |
Dan Gohman | 52022c2 | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 255 | |
| 256 | copy_to_buffer(Ptr, Size); |
| 257 | |
| 258 | return *this; |
| 259 | } |
| 260 | |
| 261 | void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) { |
Dan Gohman | c04a00a | 2009-08-13 20:32:03 +0000 | [diff] [blame] | 262 | assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!"); |
Dan Gohman | 9820555 | 2009-08-13 18:38:15 +0000 | [diff] [blame] | 263 | |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 264 | // Handle short strings specially, memcpy isn't very good at very short |
| 265 | // strings. |
| 266 | switch (Size) { |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 267 | case 4: OutBufCur[3] = Ptr[3]; LLVM_FALLTHROUGH; |
| 268 | case 3: OutBufCur[2] = Ptr[2]; LLVM_FALLTHROUGH; |
| 269 | case 2: OutBufCur[1] = Ptr[1]; LLVM_FALLTHROUGH; |
| 270 | case 1: OutBufCur[0] = Ptr[0]; LLVM_FALLTHROUGH; |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 271 | case 0: break; |
| 272 | default: |
Dan Gohman | 52022c2 | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 273 | memcpy(OutBufCur, Ptr, Size); |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 274 | break; |
| 275 | } |
Daniel Dunbar | 3da6a70 | 2009-03-10 16:21:55 +0000 | [diff] [blame] | 276 | |
Dan Gohman | 52022c2 | 2009-08-13 15:44:52 +0000 | [diff] [blame] | 277 | OutBufCur += Size; |
Owen Anderson | d285053 | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 280 | // Formatted output. |
| 281 | raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) { |
Daniel Dunbar | 64fa386 | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 282 | // If we have more than a few bytes left in our output buffer, try |
| 283 | // formatting directly onto its end. |
Dan Gohman | f199ad6 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 284 | size_t NextBufferSize = 127; |
Daniel Dunbar | 47a309c | 2009-08-23 20:31:39 +0000 | [diff] [blame] | 285 | size_t BufferBytesLeft = OutBufEnd - OutBufCur; |
| 286 | if (BufferBytesLeft > 3) { |
Dan Gohman | f199ad6 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 287 | size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 288 | |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 289 | // Common case is that we have plenty of space. |
Daniel Dunbar | 47a309c | 2009-08-23 20:31:39 +0000 | [diff] [blame] | 290 | if (BytesUsed <= BufferBytesLeft) { |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 291 | OutBufCur += BytesUsed; |
| 292 | return *this; |
| 293 | } |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 294 | |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 295 | // Otherwise, we overflowed and the return value tells us the size to try |
| 296 | // again with. |
| 297 | NextBufferSize = BytesUsed; |
| 298 | } |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 299 | |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 300 | // If we got here, we didn't have enough space in the output buffer for the |
| 301 | // string. Try printing into a SmallVector that is resized to have enough |
| 302 | // space. Iterate until we win. |
| 303 | SmallVector<char, 128> V; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 304 | |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 305 | while (true) { |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 306 | V.resize(NextBufferSize); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 308 | // Try formatting into the SmallVector. |
Daniel Dunbar | 47a309c | 2009-08-23 20:31:39 +0000 | [diff] [blame] | 309 | size_t BytesUsed = Fmt.print(V.data(), NextBufferSize); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 310 | |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 311 | // If BytesUsed fit into the vector, we win. |
Chris Lattner | 2bfc72e | 2008-10-26 19:20:47 +0000 | [diff] [blame] | 312 | if (BytesUsed <= NextBufferSize) |
Daniel Dunbar | 47a309c | 2009-08-23 20:31:39 +0000 | [diff] [blame] | 313 | return write(V.data(), BytesUsed); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 314 | |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 315 | // Otherwise, try again with a new size. |
| 316 | assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?"); |
| 317 | NextBufferSize = BytesUsed; |
| 318 | } |
| 319 | } |
| 320 | |
Nick Kledzik | e648037 | 2014-09-25 20:30:58 +0000 | [diff] [blame] | 321 | raw_ostream &raw_ostream::operator<<(const FormattedString &FS) { |
| 322 | unsigned Len = FS.Str.size(); |
| 323 | int PadAmount = FS.Width - Len; |
| 324 | if (FS.RightJustify && (PadAmount > 0)) |
| 325 | this->indent(PadAmount); |
| 326 | this->operator<<(FS.Str); |
| 327 | if (!FS.RightJustify && (PadAmount > 0)) |
| 328 | this->indent(PadAmount); |
| 329 | return *this; |
| 330 | } |
| 331 | |
| 332 | raw_ostream &raw_ostream::operator<<(const FormattedNumber &FN) { |
| 333 | if (FN.Hex) { |
Zachary Turner | 5b2243e | 2016-10-29 00:27:22 +0000 | [diff] [blame] | 334 | HexPrintStyle Style; |
| 335 | if (FN.Upper && FN.HexPrefix) |
| 336 | Style = HexPrintStyle::PrefixUpper; |
| 337 | else if (FN.Upper && !FN.HexPrefix) |
| 338 | Style = HexPrintStyle::Upper; |
| 339 | else if (!FN.Upper && FN.HexPrefix) |
| 340 | Style = HexPrintStyle::PrefixLower; |
| 341 | else |
| 342 | Style = HexPrintStyle::Lower; |
| 343 | llvm::write_hex(*this, FN.HexValue, Style, FN.Width); |
Nick Kledzik | e648037 | 2014-09-25 20:30:58 +0000 | [diff] [blame] | 344 | } else { |
Zachary Turner | 5b2243e | 2016-10-29 00:27:22 +0000 | [diff] [blame] | 345 | llvm::SmallString<16> Buffer; |
| 346 | llvm::raw_svector_ostream Stream(Buffer); |
| 347 | llvm::write_integer(Stream, FN.DecValue, IntegerStyle::Integer); |
| 348 | if (Buffer.size() < FN.Width) |
| 349 | indent(FN.Width - Buffer.size()); |
| 350 | (*this) << Buffer; |
Nick Kledzik | e648037 | 2014-09-25 20:30:58 +0000 | [diff] [blame] | 351 | } |
Zachary Turner | 733be51 | 2016-10-11 19:24:45 +0000 | [diff] [blame] | 352 | return *this; |
Nick Kledzik | e648037 | 2014-09-25 20:30:58 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Chris Lattner | e6db2c3 | 2009-08-22 23:10:29 +0000 | [diff] [blame] | 355 | /// indent - Insert 'NumSpaces' spaces. |
| 356 | raw_ostream &raw_ostream::indent(unsigned NumSpaces) { |
Dan Gohman | f88f52e | 2009-08-24 04:13:01 +0000 | [diff] [blame] | 357 | static const char Spaces[] = " " |
| 358 | " " |
| 359 | " "; |
Chris Lattner | e6db2c3 | 2009-08-22 23:10:29 +0000 | [diff] [blame] | 360 | |
| 361 | // Usually the indentation is small, handle it with a fastpath. |
Dan Gohman | c9fa051 | 2009-08-24 04:43:38 +0000 | [diff] [blame] | 362 | if (NumSpaces < array_lengthof(Spaces)) |
Chris Lattner | e6db2c3 | 2009-08-22 23:10:29 +0000 | [diff] [blame] | 363 | return write(Spaces, NumSpaces); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 364 | |
Chris Lattner | e6db2c3 | 2009-08-22 23:10:29 +0000 | [diff] [blame] | 365 | while (NumSpaces) { |
Dan Gohman | c9fa051 | 2009-08-24 04:43:38 +0000 | [diff] [blame] | 366 | unsigned NumToWrite = std::min(NumSpaces, |
| 367 | (unsigned)array_lengthof(Spaces)-1); |
Chris Lattner | e6db2c3 | 2009-08-22 23:10:29 +0000 | [diff] [blame] | 368 | write(Spaces, NumToWrite); |
| 369 | NumSpaces -= NumToWrite; |
| 370 | } |
| 371 | return *this; |
| 372 | } |
| 373 | |
Chris Lattner | 22b52c9 | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 374 | //===----------------------------------------------------------------------===// |
| 375 | // Formatted Output |
| 376 | //===----------------------------------------------------------------------===// |
| 377 | |
| 378 | // Out of line virtual method. |
| 379 | void format_object_base::home() { |
| 380 | } |
| 381 | |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 382 | //===----------------------------------------------------------------------===// |
| 383 | // raw_fd_ostream |
| 384 | //===----------------------------------------------------------------------===// |
| 385 | |
Rafael Espindola | 79be4cc | 2015-04-13 10:28:56 +0000 | [diff] [blame] | 386 | static int getFD(StringRef Filename, std::error_code &EC, |
| 387 | sys::fs::OpenFlags Flags) { |
Dan Gohman | c825cee | 2010-08-18 22:26:19 +0000 | [diff] [blame] | 388 | // Handle "-" as stdout. Note that when we do this, we consider ourself |
| 389 | // the owner of stdout. This means that we can do things like close the |
| 390 | // file descriptor when we're done and set the "binary" flag globally. |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 391 | if (Filename == "-") { |
Rafael Espindola | 79be4cc | 2015-04-13 10:28:56 +0000 | [diff] [blame] | 392 | EC = std::error_code(); |
Daniel Dunbar | ed90e70 | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 393 | // If user requested binary then put stdout into binary mode if |
| 394 | // possible. |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 395 | if (!(Flags & sys::fs::F_Text)) |
Rafael Espindola | cb2eca0 | 2013-06-12 20:58:35 +0000 | [diff] [blame] | 396 | sys::ChangeStdoutToBinary(); |
Rafael Espindola | 79be4cc | 2015-04-13 10:28:56 +0000 | [diff] [blame] | 397 | return STDOUT_FILENO; |
Chris Lattner | d5bc068 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 398 | } |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 399 | |
Rafael Espindola | 79be4cc | 2015-04-13 10:28:56 +0000 | [diff] [blame] | 400 | int FD; |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 401 | EC = sys::fs::openFileForWrite(Filename, FD, Flags); |
Rafael Espindola | 79be4cc | 2015-04-13 10:28:56 +0000 | [diff] [blame] | 402 | if (EC) |
| 403 | return -1; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 404 | |
Rafael Espindola | 79be4cc | 2015-04-13 10:28:56 +0000 | [diff] [blame] | 405 | return FD; |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Rafael Espindola | 79be4cc | 2015-04-13 10:28:56 +0000 | [diff] [blame] | 408 | raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC, |
| 409 | sys::fs::OpenFlags Flags) |
| 410 | : raw_fd_ostream(getFD(Filename, EC, Flags), true) {} |
| 411 | |
Rafael Espindola | 62e6ec0 | 2015-04-09 16:59:07 +0000 | [diff] [blame] | 412 | /// FD is the file descriptor that this writes to. If ShouldClose is true, this |
| 413 | /// closes the file when the stream is destroyed. |
Francois Pichet | a3037c3 | 2010-10-14 20:30:58 +0000 | [diff] [blame] | 414 | raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered) |
Rafael Espindola | 37b7015 | 2015-04-14 15:00:34 +0000 | [diff] [blame] | 415 | : raw_pwrite_stream(unbuffered), FD(fd), ShouldClose(shouldClose), |
Rafael Espindola | 3f210fc | 2015-12-16 22:59:06 +0000 | [diff] [blame] | 416 | Error(false) { |
Rafael Espindola | 79be4cc | 2015-04-13 10:28:56 +0000 | [diff] [blame] | 417 | if (FD < 0 ) { |
| 418 | ShouldClose = false; |
| 419 | return; |
| 420 | } |
Michael J. Spencer | b747990 | 2011-01-17 15:53:12 +0000 | [diff] [blame] | 421 | |
| 422 | // Get the starting position. |
| 423 | off_t loc = ::lseek(FD, 0, SEEK_CUR); |
Rafael Espindola | a9b84ab | 2015-04-13 11:09:48 +0000 | [diff] [blame] | 424 | #ifdef LLVM_ON_WIN32 |
| 425 | // MSVCRT's _lseek(SEEK_CUR) doesn't return -1 for pipes. |
| 426 | sys::fs::file_status Status; |
| 427 | std::error_code EC = status(FD, Status); |
| 428 | SupportsSeeking = !EC && Status.type() == sys::fs::file_type::regular_file; |
| 429 | #else |
Rafael Espindola | 74d2f61 | 2015-04-10 18:15:51 +0000 | [diff] [blame] | 430 | SupportsSeeking = loc != (off_t)-1; |
Rafael Espindola | a9b84ab | 2015-04-13 11:09:48 +0000 | [diff] [blame] | 431 | #endif |
Rafael Espindola | 74d2f61 | 2015-04-10 18:15:51 +0000 | [diff] [blame] | 432 | if (!SupportsSeeking) |
Michael J. Spencer | b747990 | 2011-01-17 15:53:12 +0000 | [diff] [blame] | 433 | pos = 0; |
| 434 | else |
| 435 | pos = static_cast<uint64_t>(loc); |
Francois Pichet | a3037c3 | 2010-10-14 20:30:58 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 438 | raw_fd_ostream::~raw_fd_ostream() { |
Dan Gohman | 38adfdd | 2010-08-20 16:34:20 +0000 | [diff] [blame] | 439 | if (FD >= 0) { |
| 440 | flush(); |
David Majnemer | 51c2afc | 2014-10-07 05:48:40 +0000 | [diff] [blame] | 441 | if (ShouldClose && sys::Process::SafelyCloseFileDescriptor(FD)) |
| 442 | error_detected(); |
Dan Gohman | 38adfdd | 2010-08-20 16:34:20 +0000 | [diff] [blame] | 443 | } |
| 444 | |
NAKAMURA Takumi | 76e68ea | 2011-03-16 02:53:39 +0000 | [diff] [blame] | 445 | #ifdef __MINGW32__ |
| 446 | // On mingw, global dtors should not call exit(). |
| 447 | // report_fatal_error() invokes exit(). We know report_fatal_error() |
| 448 | // might not write messages to stderr when any errors were detected |
| 449 | // on FD == 2. |
| 450 | if (FD == 2) return; |
| 451 | #endif |
| 452 | |
Dan Gohman | 38adfdd | 2010-08-20 16:34:20 +0000 | [diff] [blame] | 453 | // If there are any pending errors, report them now. Clients wishing |
| 454 | // to avoid report_fatal_error calls should check for errors with |
| 455 | // has_error() and clear the error flag with clear_error() before |
| 456 | // destructing raw_ostream objects which may have errors. |
| 457 | if (has_error()) |
Chad Rosier | 7ce810c | 2013-03-27 18:30:00 +0000 | [diff] [blame] | 458 | report_fatal_error("IO failure on output stream.", /*GenCrashDiag=*/false); |
Chris Lattner | ce3b2c3 | 2010-08-17 23:11:56 +0000 | [diff] [blame] | 459 | } |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 460 | |
Dan Gohman | f199ad6 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 461 | void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) { |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 462 | assert(FD >= 0 && "File already closed."); |
Daniel Dunbar | db7a36c | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 463 | pos += Size; |
Dan Gohman | ef969f3 | 2010-05-06 01:27:36 +0000 | [diff] [blame] | 464 | |
Yunzhong Gao | fb2a9c4 | 2016-01-06 00:50:06 +0000 | [diff] [blame] | 465 | #ifndef LLVM_ON_WIN32 |
| 466 | bool ShouldWriteInChunks = false; |
| 467 | #else |
| 468 | // Writing a large size of output to Windows console returns ENOMEM. It seems |
| 469 | // that, prior to Windows 8, WriteFile() is redirecting to WriteConsole(), and |
| 470 | // the latter has a size limit (66000 bytes or less, depending on heap usage). |
Reid Kleckner | 5fb7a58 | 2016-01-11 23:33:03 +0000 | [diff] [blame] | 471 | bool ShouldWriteInChunks = !!::_isatty(FD) && !RunningWindows8OrGreater(); |
Yunzhong Gao | fb2a9c4 | 2016-01-06 00:50:06 +0000 | [diff] [blame] | 472 | #endif |
| 473 | |
Benjamin Kramer | ce84a25 | 2010-05-05 15:17:47 +0000 | [diff] [blame] | 474 | do { |
Yunzhong Gao | fb2a9c4 | 2016-01-06 00:50:06 +0000 | [diff] [blame] | 475 | size_t ChunkSize = Size; |
| 476 | if (ChunkSize > 32767 && ShouldWriteInChunks) |
| 477 | ChunkSize = 32767; |
| 478 | |
| 479 | ssize_t ret = ::write(FD, Ptr, ChunkSize); |
Dan Gohman | ef969f3 | 2010-05-06 01:27:36 +0000 | [diff] [blame] | 480 | |
| 481 | if (ret < 0) { |
| 482 | // If it's a recoverable error, swallow it and retry the write. |
Dan Gohman | dea5310 | 2010-05-18 15:25:14 +0000 | [diff] [blame] | 483 | // |
| 484 | // Ideally we wouldn't ever see EAGAIN or EWOULDBLOCK here, since |
| 485 | // raw_ostream isn't designed to do non-blocking I/O. However, some |
| 486 | // programs, such as old versions of bjam, have mistakenly used |
| 487 | // O_NONBLOCK. For compatibility, emulate blocking semantics by |
| 488 | // spinning until the write succeeds. If you don't want spinning, |
| 489 | // don't use O_NONBLOCK file descriptors with raw_ostream. |
Dan Gohman | d351116 | 2010-05-06 02:06:20 +0000 | [diff] [blame] | 490 | if (errno == EINTR || errno == EAGAIN |
| 491 | #ifdef EWOULDBLOCK |
| 492 | || errno == EWOULDBLOCK |
| 493 | #endif |
| 494 | ) |
Dan Gohman | ef969f3 | 2010-05-06 01:27:36 +0000 | [diff] [blame] | 495 | continue; |
| 496 | |
| 497 | // Otherwise it's a non-recoverable error. Note it and quit. |
| 498 | error_detected(); |
| 499 | break; |
| 500 | } |
| 501 | |
| 502 | // The write may have written some or all of the data. Update the |
| 503 | // size and buffer pointer to reflect the remainder that needs |
| 504 | // to be written. If there are no bytes left, we're done. |
| 505 | Ptr += ret; |
| 506 | Size -= ret; |
| 507 | } while (Size > 0); |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Dan Gohman | 44790e7 | 2010-08-18 01:34:52 +0000 | [diff] [blame] | 510 | void raw_fd_ostream::close() { |
| 511 | assert(ShouldClose); |
| 512 | ShouldClose = false; |
| 513 | flush(); |
David Majnemer | 51c2afc | 2014-10-07 05:48:40 +0000 | [diff] [blame] | 514 | if (sys::Process::SafelyCloseFileDescriptor(FD)) |
| 515 | error_detected(); |
Dan Gohman | 44790e7 | 2010-08-18 01:34:52 +0000 | [diff] [blame] | 516 | FD = -1; |
| 517 | } |
| 518 | |
Ted Kremenek | a266992 | 2009-01-26 21:42:04 +0000 | [diff] [blame] | 519 | uint64_t raw_fd_ostream::seek(uint64_t off) { |
Yaron Keren | 7cf7f80 | 2016-02-23 07:17:58 +0000 | [diff] [blame] | 520 | assert(SupportsSeeking && "Stream does not support seeking!"); |
Ted Kremenek | a266992 | 2009-01-26 21:42:04 +0000 | [diff] [blame] | 521 | flush(); |
Daniel Dunbar | dcb50b9 | 2009-07-15 08:11:46 +0000 | [diff] [blame] | 522 | pos = ::lseek(FD, off, SEEK_SET); |
Rafael Espindola | 642a221 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 523 | if (pos == (uint64_t)-1) |
Dan Gohman | 58fcef9 | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 524 | error_detected(); |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 525 | return pos; |
Ted Kremenek | a266992 | 2009-01-26 21:42:04 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Rafael Espindola | 4ba9af1 | 2015-04-20 13:04:30 +0000 | [diff] [blame] | 528 | void raw_fd_ostream::pwrite_impl(const char *Ptr, size_t Size, |
| 529 | uint64_t Offset) { |
Rafael Espindola | 37b7015 | 2015-04-14 15:00:34 +0000 | [diff] [blame] | 530 | uint64_t Pos = tell(); |
| 531 | seek(Offset); |
| 532 | write(Ptr, Size); |
| 533 | seek(Pos); |
| 534 | } |
| 535 | |
Chris Lattner | dd3e9aa | 2009-12-19 01:38:42 +0000 | [diff] [blame] | 536 | size_t raw_fd_ostream::preferred_buffer_size() const { |
Chris Lattner | ca97c92 | 2010-07-07 15:52:27 +0000 | [diff] [blame] | 537 | #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__minix) |
Chris Lattner | c86cdc7 | 2010-04-09 20:45:04 +0000 | [diff] [blame] | 538 | // Windows and Minix have no st_blksize. |
Dan Gohman | 84487b9 | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 539 | assert(FD >= 0 && "File not yet open!"); |
| 540 | struct stat statbuf; |
Chris Lattner | dd3e9aa | 2009-12-19 01:38:42 +0000 | [diff] [blame] | 541 | if (fstat(FD, &statbuf) != 0) |
| 542 | return 0; |
Dan Gohman | b452d4e | 2010-03-24 19:38:02 +0000 | [diff] [blame] | 543 | |
Chris Lattner | dd3e9aa | 2009-12-19 01:38:42 +0000 | [diff] [blame] | 544 | // If this is a terminal, don't use buffering. Line buffering |
| 545 | // would be a more traditional thing to do, but it's not worth |
| 546 | // the complexity. |
| 547 | if (S_ISCHR(statbuf.st_mode) && isatty(FD)) |
| 548 | return 0; |
| 549 | // Return the preferred block size. |
| 550 | return statbuf.st_blksize; |
Dan Gohman | feaeb36 | 2010-05-28 16:50:01 +0000 | [diff] [blame] | 551 | #else |
Dan Gohman | 84487b9 | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 552 | return raw_ostream::preferred_buffer_size(); |
Dan Gohman | feaeb36 | 2010-05-28 16:50:01 +0000 | [diff] [blame] | 553 | #endif |
Dan Gohman | 84487b9 | 2009-08-13 17:27:29 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 556 | raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold, |
| 557 | bool bg) { |
| 558 | if (sys::Process::ColorNeedsFlush()) |
| 559 | flush(); |
| 560 | const char *colorcode = |
| 561 | (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg) |
| 562 | : sys::Process::OutputColor(colors, bold, bg); |
| 563 | if (colorcode) { |
Dan Gohman | f199ad6 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 564 | size_t len = strlen(colorcode); |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 565 | write(colorcode, len); |
| 566 | // don't account colors towards output characters |
| 567 | pos -= len; |
| 568 | } |
| 569 | return *this; |
| 570 | } |
| 571 | |
| 572 | raw_ostream &raw_fd_ostream::resetColor() { |
| 573 | if (sys::Process::ColorNeedsFlush()) |
| 574 | flush(); |
| 575 | const char *colorcode = sys::Process::ResetColor(); |
| 576 | if (colorcode) { |
Dan Gohman | f199ad6 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 577 | size_t len = strlen(colorcode); |
Torok Edwin | 9b5a47f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 578 | write(colorcode, len); |
| 579 | // don't account colors towards output characters |
| 580 | pos -= len; |
| 581 | } |
| 582 | return *this; |
| 583 | } |
| 584 | |
Benjamin Kramer | 13d16f3 | 2012-04-16 08:56:50 +0000 | [diff] [blame] | 585 | raw_ostream &raw_fd_ostream::reverseColor() { |
| 586 | if (sys::Process::ColorNeedsFlush()) |
| 587 | flush(); |
| 588 | const char *colorcode = sys::Process::OutputReverse(); |
| 589 | if (colorcode) { |
| 590 | size_t len = strlen(colorcode); |
| 591 | write(colorcode, len); |
| 592 | // don't account colors towards output characters |
| 593 | pos -= len; |
| 594 | } |
| 595 | return *this; |
| 596 | } |
| 597 | |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 598 | bool raw_fd_ostream::is_displayed() const { |
| 599 | return sys::Process::FileDescriptorIsDisplayed(FD); |
| 600 | } |
| 601 | |
Daniel Dunbar | 04b4583 | 2012-07-20 18:29:41 +0000 | [diff] [blame] | 602 | bool raw_fd_ostream::has_colors() const { |
| 603 | return sys::Process::FileDescriptorHasColors(FD); |
| 604 | } |
| 605 | |
Chris Lattner | d3723fc | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 606 | //===----------------------------------------------------------------------===// |
Dan Gohman | e9a4691 | 2010-08-20 16:44:56 +0000 | [diff] [blame] | 607 | // outs(), errs(), nulls() |
Chris Lattner | d3723fc | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 608 | //===----------------------------------------------------------------------===// |
Chris Lattner | 84b94f7 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 609 | |
Chris Lattner | d3723fc | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 610 | /// outs() - This returns a reference to a raw_ostream for standard output. |
| 611 | /// Use it like: outs() << "foo" << "bar"; |
Owen Anderson | 9371964 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 612 | raw_ostream &llvm::outs() { |
Justin Lebar | c75d566 | 2016-02-19 00:18:46 +0000 | [diff] [blame] | 613 | // Set buffer settings to model stdout behavior. Delete the file descriptor |
| 614 | // when the program exits, forcing error detection. This means that if you |
| 615 | // ever call outs(), you can't open another raw_fd_ostream on stdout, as we'll |
| 616 | // close stdout twice and print an error the second time. |
Rafael Espindola | 79be4cc | 2015-04-13 10:28:56 +0000 | [diff] [blame] | 617 | std::error_code EC; |
| 618 | static raw_fd_ostream S("-", EC, sys::fs::F_None); |
| 619 | assert(!EC); |
Chris Lattner | d3723fc | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 620 | return S; |
| 621 | } |
| 622 | |
| 623 | /// errs() - This returns a reference to a raw_ostream for standard error. |
| 624 | /// Use it like: errs() << "foo" << "bar"; |
Owen Anderson | 9371964 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 625 | raw_ostream &llvm::errs() { |
Dan Gohman | 443f2d6 | 2010-08-20 16:39:41 +0000 | [diff] [blame] | 626 | // Set standard error to be unbuffered by default. |
| 627 | static raw_fd_ostream S(STDERR_FILENO, false, true); |
Chris Lattner | d3723fc | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 628 | return S; |
| 629 | } |
| 630 | |
Daniel Dunbar | 95a551a | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 631 | /// nulls() - This returns a reference to a raw_ostream which discards output. |
| 632 | raw_ostream &llvm::nulls() { |
| 633 | static raw_null_ostream S; |
| 634 | return S; |
| 635 | } |
| 636 | |
Chris Lattner | 205af96 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 637 | //===----------------------------------------------------------------------===// |
| 638 | // raw_string_ostream |
| 639 | //===----------------------------------------------------------------------===// |
| 640 | |
Daniel Dunbar | d882f4b | 2009-08-18 20:07:36 +0000 | [diff] [blame] | 641 | raw_string_ostream::~raw_string_ostream() { |
| 642 | flush(); |
| 643 | } |
| 644 | |
Dan Gohman | f199ad6 | 2009-07-16 15:24:40 +0000 | [diff] [blame] | 645 | void raw_string_ostream::write_impl(const char *Ptr, size_t Size) { |
Daniel Dunbar | db7a36c | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 646 | OS.append(Ptr, Size); |
Chris Lattner | 205af96 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | //===----------------------------------------------------------------------===// |
| 650 | // raw_svector_ostream |
| 651 | //===----------------------------------------------------------------------===// |
| 652 | |
Yaron Keren | 3d1173b | 2015-08-13 06:19:52 +0000 | [diff] [blame] | 653 | uint64_t raw_svector_ostream::current_pos() const { return OS.size(); } |
Daniel Dunbar | b090bf4 | 2009-08-19 17:54:29 +0000 | [diff] [blame] | 654 | |
Yaron Keren | 3d1173b | 2015-08-13 06:19:52 +0000 | [diff] [blame] | 655 | void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) { |
| 656 | OS.append(Ptr, Ptr + Size); |
Nick Lewycky | e2f6fb5 | 2015-08-13 18:10:19 +0000 | [diff] [blame] | 657 | } |
Daniel Dunbar | d882f4b | 2009-08-18 20:07:36 +0000 | [diff] [blame] | 658 | |
Rafael Espindola | 4ba9af1 | 2015-04-20 13:04:30 +0000 | [diff] [blame] | 659 | void raw_svector_ostream::pwrite_impl(const char *Ptr, size_t Size, |
| 660 | uint64_t Offset) { |
Yaron Keren | 3d1173b | 2015-08-13 06:19:52 +0000 | [diff] [blame] | 661 | memcpy(OS.data() + Offset, Ptr, Size); |
Daniel Dunbar | e813cba | 2009-08-19 18:40:58 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Daniel Dunbar | 95a551a | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 664 | //===----------------------------------------------------------------------===// |
| 665 | // raw_null_ostream |
| 666 | //===----------------------------------------------------------------------===// |
| 667 | |
Dan Gohman | 4b66b47 | 2009-07-27 21:46:02 +0000 | [diff] [blame] | 668 | raw_null_ostream::~raw_null_ostream() { |
| 669 | #ifndef NDEBUG |
| 670 | // ~raw_ostream asserts that the buffer is empty. This isn't necessary |
| 671 | // with raw_null_ostream, but it's better to have raw_null_ostream follow |
| 672 | // the rules than to change the rules just for raw_null_ostream. |
| 673 | flush(); |
| 674 | #endif |
| 675 | } |
| 676 | |
Daniel Dunbar | 95a551a | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 677 | void raw_null_ostream::write_impl(const char *Ptr, size_t Size) { |
| 678 | } |
| 679 | |
Chris Lattner | dd3e9aa | 2009-12-19 01:38:42 +0000 | [diff] [blame] | 680 | uint64_t raw_null_ostream::current_pos() const { |
Daniel Dunbar | 95a551a | 2009-07-16 21:17:53 +0000 | [diff] [blame] | 681 | return 0; |
| 682 | } |
Rafael Espindola | 37b7015 | 2015-04-14 15:00:34 +0000 | [diff] [blame] | 683 | |
Rafael Espindola | 4ba9af1 | 2015-04-20 13:04:30 +0000 | [diff] [blame] | 684 | void raw_null_ostream::pwrite_impl(const char *Ptr, size_t Size, |
| 685 | uint64_t Offset) {} |