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