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