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