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