blob: c0e210bcd5e7cafa2764b2e8f6f42db0175efe91 [file] [log] [blame]
Chris Lattner84b94f72008-08-17 01:35:29 +00001//===--- 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 Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/STLExtras.h"
Chris Lattner22b52c92008-08-23 19:23:10 +000016#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/StringExtras.h"
Chris Lattnerd564f292008-08-22 15:45:00 +000018#include "llvm/Config/config.h"
Daniel Dunbar437b8a52009-03-17 21:15:18 +000019#include "llvm/Support/Compiler.h"
Daniel Dunbardcb50b92009-07-15 08:11:46 +000020#include "llvm/Support/ErrorHandling.h"
Rafael Espindola6d354812013-07-16 19:44:17 +000021#include "llvm/Support/FileSystem.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Support/Format.h"
Zachary Turner11db2642016-11-11 23:57:40 +000023#include "llvm/Support/FormatVariadic.h"
Nick Kledzike6480372014-09-25 20:30:58 +000024#include "llvm/Support/MathExtras.h"
Zachary Turner733be512016-10-11 19:24:45 +000025#include "llvm/Support/NativeFormatting.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Support/Process.h"
27#include "llvm/Support/Program.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000028#include <algorithm>
Benjamin Krameref14f802010-01-29 15:19:06 +000029#include <cctype>
Benjamin Kramerce84a252010-05-05 15:17:47 +000030#include <cerrno>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000031#include <cstdio>
32#include <iterator>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000033#include <sys/stat.h>
Zachary Turner733be512016-10-11 19:24:45 +000034#include <system_error>
Chris Lattner84b94f72008-08-17 01:35:29 +000035
NAKAMURA Takumi212c80a2013-07-17 02:21:10 +000036// <fcntl.h> may provide O_BINARY.
37#if defined(HAVE_FCNTL_H)
38# include <fcntl.h>
39#endif
40
Chris Lattnerd564f292008-08-22 15:45:00 +000041#if defined(HAVE_UNISTD_H)
42# include <unistd.h>
43#endif
Argyrios Kyrtzidisb97ff822008-08-17 09:25:21 +000044
NAKAMURA Takumife84f392010-10-19 01:21:55 +000045#if defined(__CYGWIN__)
46#include <io.h>
47#endif
48
Argyrios Kyrtzidisb97ff822008-08-17 09:25:21 +000049#if defined(_MSC_VER)
Chris Lattner84b94f72008-08-17 01:35:29 +000050#include <io.h>
Owen Anderson93719642008-08-21 00:14:44 +000051#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 Lattner84b94f72008-08-17 01:35:29 +000060#endif
61
Yunzhong Gaofb2a9c42016-01-06 00:50:06 +000062#ifdef LLVM_ON_WIN32
63#include "Windows/WindowsSupport.h"
64#endif
65
Chris Lattnerd564f292008-08-22 15:45:00 +000066using namespace llvm;
67
Dan Gohman58fcef92009-07-15 23:25:33 +000068raw_ostream::~raw_ostream() {
Dan Gohman1b763292009-07-27 20:49:44 +000069 // 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 Dunbar317a6cd2009-08-18 23:42:36 +000074 if (BufferMode == InternalBuffer)
75 delete [] OutBufStart;
Dan Gohman58fcef92009-07-15 23:25:33 +000076}
Chris Lattnerd564f292008-08-22 15:45:00 +000077
Chris Lattner84b94f72008-08-17 01:35:29 +000078// An out of line virtual method to provide a home for the class vtable.
79void raw_ostream::handle() {}
80
Chris Lattnerdd3e9aa2009-12-19 01:38:42 +000081size_t raw_ostream::preferred_buffer_size() const {
Dan Gohman84487b92009-08-13 17:27:29 +000082 // BUFSIZ is intended to be a reasonable default.
83 return BUFSIZ;
84}
85
86void raw_ostream::SetBuffered() {
87 // Ask the subclass to determine an appropriate buffer size.
Dan Gohman17710222009-08-15 02:05:19 +000088 if (size_t Size = preferred_buffer_size())
89 SetBufferSize(Size);
90 else
91 // It may return 0, meaning this stream should be unbuffered.
92 SetUnbuffered();
Dan Gohman84487b92009-08-13 17:27:29 +000093}
94
Dan Gohmanb452d4e2010-03-24 19:38:02 +000095void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size,
Nick Lewycky7bfd86d2011-08-28 03:30:02 +000096 BufferKind Mode) {
Craig Topper2617dcc2014-04-15 06:32:26 +000097 assert(((Mode == Unbuffered && !BufferStart && Size == 0) ||
98 (Mode != Unbuffered && BufferStart && Size != 0)) &&
Daniel Dunbar316b4a02009-09-15 20:31:46 +000099 "stream must be unbuffered or have at least one byte");
Daniel Dunbar317a6cd2009-08-18 23:42:36 +0000100 // Make sure the current buffer is free of content (we can't flush here; the
101 // child buffer management logic will be in write_impl).
102 assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!");
Dan Gohman54401d42009-08-13 15:58:55 +0000103
Daniel Dunbar317a6cd2009-08-18 23:42:36 +0000104 if (BufferMode == InternalBuffer)
105 delete [] OutBufStart;
106 OutBufStart = BufferStart;
Dan Gohman54401d42009-08-13 15:58:55 +0000107 OutBufEnd = OutBufStart+Size;
108 OutBufCur = OutBufStart;
Daniel Dunbar317a6cd2009-08-18 23:42:36 +0000109 BufferMode = Mode;
Daniel Dunbarb090bf42009-08-19 17:54:29 +0000110
111 assert(OutBufStart <= OutBufEnd && "Invalid size!");
Dan Gohman54401d42009-08-13 15:58:55 +0000112}
113
Owen Andersond2850532008-08-21 20:58:52 +0000114raw_ostream &raw_ostream::operator<<(unsigned long N) {
Zachary Turner11db2642016-11-11 23:57:40 +0000115 write_integer(*this, static_cast<uint64_t>(N), 0, IntegerStyle::Integer);
Zachary Turner733be512016-10-11 19:24:45 +0000116 return *this;
Owen Andersond2850532008-08-21 20:58:52 +0000117}
118
119raw_ostream &raw_ostream::operator<<(long N) {
Zachary Turner11db2642016-11-11 23:57:40 +0000120 write_integer(*this, static_cast<int64_t>(N), 0, IntegerStyle::Integer);
Zachary Turner733be512016-10-11 19:24:45 +0000121 return *this;
Owen Andersond2850532008-08-21 20:58:52 +0000122}
123
124raw_ostream &raw_ostream::operator<<(unsigned long long N) {
Zachary Turner11db2642016-11-11 23:57:40 +0000125 write_integer(*this, static_cast<uint64_t>(N), 0, IntegerStyle::Integer);
Zachary Turner733be512016-10-11 19:24:45 +0000126 return *this;
Owen Andersond2850532008-08-21 20:58:52 +0000127}
128
129raw_ostream &raw_ostream::operator<<(long long N) {
Zachary Turner11db2642016-11-11 23:57:40 +0000130 write_integer(*this, static_cast<int64_t>(N), 0, IntegerStyle::Integer);
Zachary Turner733be512016-10-11 19:24:45 +0000131 return *this;
Owen Andersond2850532008-08-21 20:58:52 +0000132}
133
Daniel Dunbar6c9629b2009-07-30 18:21:23 +0000134raw_ostream &raw_ostream::write_hex(unsigned long long N) {
Zachary Turner5b2243e2016-10-29 00:27:22 +0000135 llvm::write_hex(*this, N, HexPrintStyle::Lower);
Zachary Turner733be512016-10-11 19:24:45 +0000136 return *this;
Chris Lattner0c19df42008-08-23 22:23:09 +0000137}
138
Adrian Prantl3dcd1222017-09-13 18:22:59 +0000139raw_ostream &raw_ostream::write_uuid(const uuid_t UUID) {
140 for (int Idx = 0; Idx < 16; ++Idx) {
141 *this << format("%02" PRIX32, UUID[Idx]);
142 if (Idx == 3 || Idx == 5 || Idx == 7 || Idx == 9)
143 *this << "-";
144 }
145 return *this;
146}
147
148
Daniel Dunbar65dc8912010-11-27 07:59:50 +0000149raw_ostream &raw_ostream::write_escaped(StringRef Str,
150 bool UseHexEscapes) {
Craig Topper775fb732016-02-04 06:51:41 +0000151 for (unsigned char c : Str) {
Daniel Dunbar4108c432009-10-17 20:43:08 +0000152 switch (c) {
153 case '\\':
154 *this << '\\' << '\\';
155 break;
156 case '\t':
157 *this << '\\' << 't';
158 break;
159 case '\n':
160 *this << '\\' << 'n';
161 break;
162 case '"':
163 *this << '\\' << '"';
164 break;
165 default:
166 if (std::isprint(c)) {
167 *this << c;
168 break;
169 }
170
Daniel Dunbar65dc8912010-11-27 07:59:50 +0000171 // Write out the escaped representation.
172 if (UseHexEscapes) {
173 *this << '\\' << 'x';
174 *this << hexdigit((c >> 4 & 0xF));
175 *this << hexdigit((c >> 0) & 0xF);
176 } else {
177 // Always use a full 3-character octal escape.
178 *this << '\\';
179 *this << char('0' + ((c >> 6) & 7));
180 *this << char('0' + ((c >> 3) & 7));
181 *this << char('0' + ((c >> 0) & 7));
182 }
Daniel Dunbar4108c432009-10-17 20:43:08 +0000183 }
184 }
185
186 return *this;
187}
188
Daniel Dunbar6c9629b2009-07-30 18:21:23 +0000189raw_ostream &raw_ostream::operator<<(const void *P) {
Zachary Turner5b2243e2016-10-29 00:27:22 +0000190 llvm::write_hex(*this, (uintptr_t)P, HexPrintStyle::PrefixLower);
Zachary Turner733be512016-10-11 19:24:45 +0000191 return *this;
Daniel Dunbar6c9629b2009-07-30 18:21:23 +0000192}
193
Chris Lattner06fa1762009-08-24 03:52:50 +0000194raw_ostream &raw_ostream::operator<<(double N) {
Zachary Turner5b2243e2016-10-29 00:27:22 +0000195 llvm::write_double(*this, N, FloatStyle::Exponent);
Zachary Turner733be512016-10-11 19:24:45 +0000196 return *this;
Chris Lattner06fa1762009-08-24 03:52:50 +0000197}
198
Daniel Dunbard24535f2009-03-16 22:55:06 +0000199void raw_ostream::flush_nonempty() {
200 assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
Daniel Dunbar317a6cd2009-08-18 23:42:36 +0000201 size_t Length = OutBufCur - OutBufStart;
202 OutBufCur = OutBufStart;
203 write_impl(OutBufStart, Length);
Daniel Dunbard24535f2009-03-16 22:55:06 +0000204}
205
Daniel Dunbar7a9bb9e2009-03-16 22:00:17 +0000206raw_ostream &raw_ostream::write(unsigned char C) {
Daniel Dunbar64fa3862009-03-17 01:36:56 +0000207 // Group exceptional cases into a single branch.
Benjamin Kramerbd7f8d02012-08-29 22:57:00 +0000208 if (LLVM_UNLIKELY(OutBufCur >= OutBufEnd)) {
209 if (LLVM_UNLIKELY(!OutBufStart)) {
Daniel Dunbar317a6cd2009-08-18 23:42:36 +0000210 if (BufferMode == Unbuffered) {
Dan Gohman23f90c12009-08-18 20:09:59 +0000211 write_impl(reinterpret_cast<char*>(&C), 1);
212 return *this;
213 }
Daniel Dunbar0cf16862009-08-19 00:23:39 +0000214 // Set up a buffer and start over.
215 SetBuffered();
216 return write(C);
Dan Gohman23f90c12009-08-18 20:09:59 +0000217 }
Daniel Dunbar0cf16862009-08-19 00:23:39 +0000218
219 flush_nonempty();
Daniel Dunbar2d603da2009-03-17 01:13:35 +0000220 }
221
Daniel Dunbar7a9bb9e2009-03-16 22:00:17 +0000222 *OutBufCur++ = C;
Daniel Dunbar87862182009-03-16 22:08:44 +0000223 return *this;
Daniel Dunbar7a9bb9e2009-03-16 22:00:17 +0000224}
Chris Lattner0c19df42008-08-23 22:23:09 +0000225
Dan Gohmanf199ad62009-07-16 15:24:40 +0000226raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
Daniel Dunbar64fa3862009-03-17 01:36:56 +0000227 // Group exceptional cases into a single branch.
Benjamin Kramerbd7f8d02012-08-29 22:57:00 +0000228 if (LLVM_UNLIKELY(size_t(OutBufEnd - OutBufCur) < Size)) {
229 if (LLVM_UNLIKELY(!OutBufStart)) {
Daniel Dunbar317a6cd2009-08-18 23:42:36 +0000230 if (BufferMode == Unbuffered) {
Dan Gohman52022c22009-08-13 15:44:52 +0000231 write_impl(Ptr, Size);
232 return *this;
233 }
234 // Set up a buffer and start over.
Dan Gohman84487b92009-08-13 17:27:29 +0000235 SetBuffered();
Dan Gohman52022c22009-08-13 15:44:52 +0000236 return write(Ptr, Size);
237 }
Daniel Dunbar0cf16862009-08-19 00:23:39 +0000238
Benjamin Kramerdfb0ad32011-03-04 19:49:30 +0000239 size_t NumBytes = OutBufEnd - OutBufCur;
240
Benjamin Krameracf08422011-03-04 18:18:16 +0000241 // If the buffer is empty at this point we have a string that is larger
Benjamin Kramerdfb0ad32011-03-04 19:49:30 +0000242 // than the buffer. Directly write the chunk that is a multiple of the
243 // preferred buffer size and put the remainder in the buffer.
Benjamin Kramerbd7f8d02012-08-29 22:57:00 +0000244 if (LLVM_UNLIKELY(OutBufCur == OutBufStart)) {
Michael Ilseman5be22a12014-12-12 21:48:03 +0000245 assert(NumBytes != 0 && "undefined behavior");
Benjamin Kramerdfb0ad32011-03-04 19:49:30 +0000246 size_t BytesToWrite = Size - (Size % NumBytes);
247 write_impl(Ptr, BytesToWrite);
Matt Beaumont-Gaya182be82013-03-12 23:55:24 +0000248 size_t BytesRemaining = Size - BytesToWrite;
249 if (BytesRemaining > size_t(OutBufEnd - OutBufCur)) {
250 // Too much left over to copy into our buffer.
251 return write(Ptr + BytesToWrite, BytesRemaining);
252 }
253 copy_to_buffer(Ptr + BytesToWrite, BytesRemaining);
Benjamin Krameracf08422011-03-04 18:18:16 +0000254 return *this;
255 }
256
257 // We don't have enough space in the buffer to fit the string in. Insert as
258 // much as possible, flush and start over with the remainder.
Benjamin Krameracf08422011-03-04 18:18:16 +0000259 copy_to_buffer(Ptr, NumBytes);
260 flush_nonempty();
261 return write(Ptr + NumBytes, Size - NumBytes);
Daniel Dunbar64fa3862009-03-17 01:36:56 +0000262 }
Dan Gohman52022c22009-08-13 15:44:52 +0000263
264 copy_to_buffer(Ptr, Size);
265
266 return *this;
267}
268
269void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) {
Dan Gohmanc04a00a2009-08-13 20:32:03 +0000270 assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!");
Dan Gohman98205552009-08-13 18:38:15 +0000271
Owen Andersond2850532008-08-21 20:58:52 +0000272 // Handle short strings specially, memcpy isn't very good at very short
273 // strings.
274 switch (Size) {
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000275 case 4: OutBufCur[3] = Ptr[3]; LLVM_FALLTHROUGH;
276 case 3: OutBufCur[2] = Ptr[2]; LLVM_FALLTHROUGH;
277 case 2: OutBufCur[1] = Ptr[1]; LLVM_FALLTHROUGH;
278 case 1: OutBufCur[0] = Ptr[0]; LLVM_FALLTHROUGH;
Owen Andersond2850532008-08-21 20:58:52 +0000279 case 0: break;
280 default:
Dan Gohman52022c22009-08-13 15:44:52 +0000281 memcpy(OutBufCur, Ptr, Size);
Owen Andersond2850532008-08-21 20:58:52 +0000282 break;
283 }
Daniel Dunbar3da6a702009-03-10 16:21:55 +0000284
Dan Gohman52022c22009-08-13 15:44:52 +0000285 OutBufCur += Size;
Owen Andersond2850532008-08-21 20:58:52 +0000286}
287
Chris Lattner22b52c92008-08-23 19:23:10 +0000288// Formatted output.
289raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
Daniel Dunbar64fa3862009-03-17 01:36:56 +0000290 // If we have more than a few bytes left in our output buffer, try
291 // formatting directly onto its end.
Dan Gohmanf199ad62009-07-16 15:24:40 +0000292 size_t NextBufferSize = 127;
Daniel Dunbar47a309c2009-08-23 20:31:39 +0000293 size_t BufferBytesLeft = OutBufEnd - OutBufCur;
294 if (BufferBytesLeft > 3) {
Dan Gohmanf199ad62009-07-16 15:24:40 +0000295 size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000296
Chris Lattner22b52c92008-08-23 19:23:10 +0000297 // Common case is that we have plenty of space.
Daniel Dunbar47a309c2009-08-23 20:31:39 +0000298 if (BytesUsed <= BufferBytesLeft) {
Chris Lattner22b52c92008-08-23 19:23:10 +0000299 OutBufCur += BytesUsed;
300 return *this;
301 }
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000302
Chris Lattner22b52c92008-08-23 19:23:10 +0000303 // Otherwise, we overflowed and the return value tells us the size to try
304 // again with.
305 NextBufferSize = BytesUsed;
306 }
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000307
Chris Lattner22b52c92008-08-23 19:23:10 +0000308 // If we got here, we didn't have enough space in the output buffer for the
309 // string. Try printing into a SmallVector that is resized to have enough
310 // space. Iterate until we win.
311 SmallVector<char, 128> V;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000312
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000313 while (true) {
Chris Lattner22b52c92008-08-23 19:23:10 +0000314 V.resize(NextBufferSize);
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000315
Chris Lattner22b52c92008-08-23 19:23:10 +0000316 // Try formatting into the SmallVector.
Daniel Dunbar47a309c2009-08-23 20:31:39 +0000317 size_t BytesUsed = Fmt.print(V.data(), NextBufferSize);
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000318
Chris Lattner22b52c92008-08-23 19:23:10 +0000319 // If BytesUsed fit into the vector, we win.
Chris Lattner2bfc72e2008-10-26 19:20:47 +0000320 if (BytesUsed <= NextBufferSize)
Daniel Dunbar47a309c2009-08-23 20:31:39 +0000321 return write(V.data(), BytesUsed);
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000322
Chris Lattner22b52c92008-08-23 19:23:10 +0000323 // Otherwise, try again with a new size.
324 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
325 NextBufferSize = BytesUsed;
326 }
327}
328
Zachary Turner11db2642016-11-11 23:57:40 +0000329raw_ostream &raw_ostream::operator<<(const formatv_object_base &Obj) {
330 SmallString<128> S;
331 Obj.format(*this);
332 return *this;
333}
334
Nick Kledzike6480372014-09-25 20:30:58 +0000335raw_ostream &raw_ostream::operator<<(const FormattedString &FS) {
Frederich Munch5e9d6d02017-07-13 16:11:08 +0000336 if (FS.Str.size() >= FS.Width || FS.Justify == FormattedString::JustifyNone) {
337 this->operator<<(FS.Str);
338 return *this;
339 }
340 const size_t Difference = FS.Width - FS.Str.size();
341 switch (FS.Justify) {
342 case FormattedString::JustifyLeft:
343 this->operator<<(FS.Str);
344 this->indent(Difference);
345 break;
346 case FormattedString::JustifyRight:
347 this->indent(Difference);
348 this->operator<<(FS.Str);
349 break;
350 case FormattedString::JustifyCenter: {
351 int PadAmount = Difference / 2;
Nick Kledzike6480372014-09-25 20:30:58 +0000352 this->indent(PadAmount);
Frederich Munch5e9d6d02017-07-13 16:11:08 +0000353 this->operator<<(FS.Str);
354 this->indent(Difference - PadAmount);
355 break;
356 }
357 default:
358 llvm_unreachable("Bad Justification");
359 }
Nick Kledzike6480372014-09-25 20:30:58 +0000360 return *this;
361}
362
363raw_ostream &raw_ostream::operator<<(const FormattedNumber &FN) {
364 if (FN.Hex) {
Zachary Turner5b2243e2016-10-29 00:27:22 +0000365 HexPrintStyle Style;
366 if (FN.Upper && FN.HexPrefix)
367 Style = HexPrintStyle::PrefixUpper;
368 else if (FN.Upper && !FN.HexPrefix)
369 Style = HexPrintStyle::Upper;
370 else if (!FN.Upper && FN.HexPrefix)
371 Style = HexPrintStyle::PrefixLower;
372 else
373 Style = HexPrintStyle::Lower;
374 llvm::write_hex(*this, FN.HexValue, Style, FN.Width);
Nick Kledzike6480372014-09-25 20:30:58 +0000375 } else {
Zachary Turner5b2243e2016-10-29 00:27:22 +0000376 llvm::SmallString<16> Buffer;
377 llvm::raw_svector_ostream Stream(Buffer);
Zachary Turner11db2642016-11-11 23:57:40 +0000378 llvm::write_integer(Stream, FN.DecValue, 0, IntegerStyle::Integer);
Zachary Turner5b2243e2016-10-29 00:27:22 +0000379 if (Buffer.size() < FN.Width)
380 indent(FN.Width - Buffer.size());
381 (*this) << Buffer;
Nick Kledzike6480372014-09-25 20:30:58 +0000382 }
Zachary Turner733be512016-10-11 19:24:45 +0000383 return *this;
Nick Kledzike6480372014-09-25 20:30:58 +0000384}
385
Zachary Turner4a86af02016-11-10 20:16:45 +0000386raw_ostream &raw_ostream::operator<<(const FormattedBytes &FB) {
387 if (FB.Bytes.empty())
388 return *this;
389
Greg Claytonbde0a162016-11-09 00:15:54 +0000390 size_t LineIndex = 0;
Zachary Turner4a86af02016-11-10 20:16:45 +0000391 auto Bytes = FB.Bytes;
392 const size_t Size = Bytes.size();
393 HexPrintStyle HPS = FB.Upper ? HexPrintStyle::Upper : HexPrintStyle::Lower;
394 uint64_t OffsetWidth = 0;
395 if (FB.FirstByteOffset.hasValue()) {
396 // Figure out how many nibbles are needed to print the largest offset
397 // represented by this data set, so that we can align the offset field
398 // to the right width.
399 size_t Lines = Size / FB.NumPerLine;
400 uint64_t MaxOffset = *FB.FirstByteOffset + Lines * FB.NumPerLine;
401 unsigned Power = 0;
402 if (MaxOffset > 0)
403 Power = llvm::Log2_64_Ceil(MaxOffset);
Zachary Turner805d43a2016-11-10 20:35:21 +0000404 OffsetWidth = std::max<uint64_t>(4, llvm::alignTo(Power, 4) / 4);
Zachary Turner4a86af02016-11-10 20:16:45 +0000405 }
406
407 // The width of a block of data including all spaces for group separators.
408 unsigned NumByteGroups =
409 alignTo(FB.NumPerLine, FB.ByteGroupSize) / FB.ByteGroupSize;
410 unsigned BlockCharWidth = FB.NumPerLine * 2 + NumByteGroups - 1;
411
412 while (!Bytes.empty()) {
413 indent(FB.IndentLevel);
414
Greg Claytonbde0a162016-11-09 00:15:54 +0000415 if (FB.FirstByteOffset.hasValue()) {
416 uint64_t Offset = FB.FirstByteOffset.getValue();
Zachary Turner4a86af02016-11-10 20:16:45 +0000417 llvm::write_hex(*this, Offset + LineIndex, HPS, OffsetWidth);
Greg Claytonbde0a162016-11-09 00:15:54 +0000418 *this << ": ";
419 }
Zachary Turner4a86af02016-11-10 20:16:45 +0000420
421 auto Line = Bytes.take_front(FB.NumPerLine);
422
423 size_t CharsPrinted = 0;
424 // Print the hex bytes for this line in groups
425 for (size_t I = 0; I < Line.size(); ++I, CharsPrinted += 2) {
426 if (I && (I % FB.ByteGroupSize) == 0) {
427 ++CharsPrinted;
Greg Claytonbde0a162016-11-09 00:15:54 +0000428 *this << " ";
Zachary Turner4a86af02016-11-10 20:16:45 +0000429 }
430 llvm::write_hex(*this, Line[I], HPS, 2);
Greg Claytonbde0a162016-11-09 00:15:54 +0000431 }
Zachary Turner4a86af02016-11-10 20:16:45 +0000432
Greg Claytonbde0a162016-11-09 00:15:54 +0000433 if (FB.ASCII) {
434 // Print any spaces needed for any bytes that we didn't print on this
435 // line so that the ASCII bytes are correctly aligned.
Zachary Turner4a86af02016-11-10 20:16:45 +0000436 assert(BlockCharWidth >= CharsPrinted);
437 indent(BlockCharWidth - CharsPrinted + 2);
438 *this << "|";
439
Greg Claytonbde0a162016-11-09 00:15:54 +0000440 // Print the ASCII char values for each byte on this line
Zachary Turner4a86af02016-11-10 20:16:45 +0000441 for (uint8_t Byte : Line) {
442 if (isprint(Byte))
443 *this << static_cast<char>(Byte);
Greg Claytonbde0a162016-11-09 00:15:54 +0000444 else
445 *this << '.';
446 }
447 *this << '|';
448 }
Zachary Turner4a86af02016-11-10 20:16:45 +0000449
450 Bytes = Bytes.drop_front(Line.size());
451 LineIndex += Line.size();
Greg Claytonbde0a162016-11-09 00:15:54 +0000452 if (LineIndex < Size)
453 *this << '\n';
454 }
455 return *this;
456}
457
Chris Lattnere6db2c32009-08-22 23:10:29 +0000458/// indent - Insert 'NumSpaces' spaces.
459raw_ostream &raw_ostream::indent(unsigned NumSpaces) {
Dan Gohmanf88f52e2009-08-24 04:13:01 +0000460 static const char Spaces[] = " "
461 " "
462 " ";
Chris Lattnere6db2c32009-08-22 23:10:29 +0000463
464 // Usually the indentation is small, handle it with a fastpath.
Dan Gohmanc9fa0512009-08-24 04:43:38 +0000465 if (NumSpaces < array_lengthof(Spaces))
Chris Lattnere6db2c32009-08-22 23:10:29 +0000466 return write(Spaces, NumSpaces);
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000467
Chris Lattnere6db2c32009-08-22 23:10:29 +0000468 while (NumSpaces) {
Dan Gohmanc9fa0512009-08-24 04:43:38 +0000469 unsigned NumToWrite = std::min(NumSpaces,
470 (unsigned)array_lengthof(Spaces)-1);
Chris Lattnere6db2c32009-08-22 23:10:29 +0000471 write(Spaces, NumToWrite);
472 NumSpaces -= NumToWrite;
473 }
474 return *this;
475}
476
Weiming Zhao1bd40002018-04-11 23:09:20 +0000477void raw_ostream::anchor() {}
478
Chris Lattner22b52c92008-08-23 19:23:10 +0000479//===----------------------------------------------------------------------===//
480// Formatted Output
481//===----------------------------------------------------------------------===//
482
483// Out of line virtual method.
484void format_object_base::home() {
485}
486
Chris Lattner84b94f72008-08-17 01:35:29 +0000487//===----------------------------------------------------------------------===//
488// raw_fd_ostream
489//===----------------------------------------------------------------------===//
490
Rafael Espindola79be4cc2015-04-13 10:28:56 +0000491static int getFD(StringRef Filename, std::error_code &EC,
492 sys::fs::OpenFlags Flags) {
Dan Gohmanc825cee2010-08-18 22:26:19 +0000493 // Handle "-" as stdout. Note that when we do this, we consider ourself
Yaron Keren80745c52017-03-31 12:08:45 +0000494 // the owner of stdout and may set the "binary" flag globally based on Flags.
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000495 if (Filename == "-") {
Rafael Espindola79be4cc2015-04-13 10:28:56 +0000496 EC = std::error_code();
Daniel Dunbared90e702008-11-13 05:01:07 +0000497 // If user requested binary then put stdout into binary mode if
498 // possible.
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000499 if (!(Flags & sys::fs::F_Text))
Rafael Espindolacb2eca02013-06-12 20:58:35 +0000500 sys::ChangeStdoutToBinary();
Yaron Keren9d27c472017-03-30 19:30:51 +0000501 return STDOUT_FILENO;
Chris Lattnerd5bc0682008-08-17 03:53:23 +0000502 }
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000503
Rafael Espindola79be4cc2015-04-13 10:28:56 +0000504 int FD;
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000505 EC = sys::fs::openFileForWrite(Filename, FD, Flags);
Rafael Espindola79be4cc2015-04-13 10:28:56 +0000506 if (EC)
507 return -1;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000508
Rafael Espindola79be4cc2015-04-13 10:28:56 +0000509 return FD;
Chris Lattner84b94f72008-08-17 01:35:29 +0000510}
511
Rafael Espindola79be4cc2015-04-13 10:28:56 +0000512raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
513 sys::fs::OpenFlags Flags)
514 : raw_fd_ostream(getFD(Filename, EC, Flags), true) {}
515
Rafael Espindola62e6ec02015-04-09 16:59:07 +0000516/// FD is the file descriptor that this writes to. If ShouldClose is true, this
517/// closes the file when the stream is destroyed.
Francois Picheta3037c32010-10-14 20:30:58 +0000518raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered)
Bob Haarman9ce2d032017-10-24 01:26:22 +0000519 : raw_pwrite_stream(unbuffered), FD(fd), ShouldClose(shouldClose) {
Rafael Espindola79be4cc2015-04-13 10:28:56 +0000520 if (FD < 0 ) {
521 ShouldClose = false;
522 return;
523 }
Reid Kleckner02aeadc2017-08-04 01:39:23 +0000524
525 // Do not attempt to close stdout or stderr. We used to try to maintain the
526 // property that tools that support writing file to stdout should not also
527 // write informational output to stdout, but in practice we were never able to
528 // maintain this invariant. Many features have been added to LLVM and clang
529 // (-fdump-record-layouts, optimization remarks, etc) that print to stdout, so
530 // users must simply be aware that mixed output and remarks is a possibility.
Yaron Keren9d27c472017-03-30 19:30:51 +0000531 if (FD <= STDERR_FILENO)
532 ShouldClose = false;
Michael J. Spencerb7479902011-01-17 15:53:12 +0000533
534 // Get the starting position.
535 off_t loc = ::lseek(FD, 0, SEEK_CUR);
Rafael Espindolaa9b84ab2015-04-13 11:09:48 +0000536#ifdef LLVM_ON_WIN32
537 // MSVCRT's _lseek(SEEK_CUR) doesn't return -1 for pipes.
538 sys::fs::file_status Status;
539 std::error_code EC = status(FD, Status);
540 SupportsSeeking = !EC && Status.type() == sys::fs::file_type::regular_file;
541#else
Rafael Espindola74d2f612015-04-10 18:15:51 +0000542 SupportsSeeking = loc != (off_t)-1;
Rafael Espindolaa9b84ab2015-04-13 11:09:48 +0000543#endif
Rafael Espindola74d2f612015-04-10 18:15:51 +0000544 if (!SupportsSeeking)
Michael J. Spencerb7479902011-01-17 15:53:12 +0000545 pos = 0;
546 else
547 pos = static_cast<uint64_t>(loc);
Francois Picheta3037c32010-10-14 20:30:58 +0000548}
549
Chris Lattner84b94f72008-08-17 01:35:29 +0000550raw_fd_ostream::~raw_fd_ostream() {
Dan Gohman38adfdd2010-08-20 16:34:20 +0000551 if (FD >= 0) {
552 flush();
Bob Haarman9ce2d032017-10-24 01:26:22 +0000553 if (ShouldClose) {
554 if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
555 error_detected(EC);
556 }
Dan Gohman38adfdd2010-08-20 16:34:20 +0000557 }
558
NAKAMURA Takumi76e68ea2011-03-16 02:53:39 +0000559#ifdef __MINGW32__
560 // On mingw, global dtors should not call exit().
561 // report_fatal_error() invokes exit(). We know report_fatal_error()
562 // might not write messages to stderr when any errors were detected
563 // on FD == 2.
564 if (FD == 2) return;
565#endif
566
Dan Gohman38adfdd2010-08-20 16:34:20 +0000567 // If there are any pending errors, report them now. Clients wishing
568 // to avoid report_fatal_error calls should check for errors with
569 // has_error() and clear the error flag with clear_error() before
570 // destructing raw_ostream objects which may have errors.
571 if (has_error())
Bob Haarman9ce2d032017-10-24 01:26:22 +0000572 report_fatal_error("IO failure on output stream: " + error().message(),
573 /*GenCrashDiag=*/false);
Chris Lattnerce3b2c32010-08-17 23:11:56 +0000574}
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000575
Dan Gohmanf199ad62009-07-16 15:24:40 +0000576void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000577 assert(FD >= 0 && "File already closed.");
Daniel Dunbardb7a36c2009-03-16 23:29:31 +0000578 pos += Size;
Dan Gohmanef969f32010-05-06 01:27:36 +0000579
Rui Ueyama412b29e2017-10-31 17:37:20 +0000580 // The maximum write size is limited to SSIZE_MAX because a write
581 // greater than SSIZE_MAX is implementation-defined in POSIX.
582 // Since SSIZE_MAX is not portable, we use SIZE_MAX >> 1 instead.
583 size_t MaxWriteSize = SIZE_MAX >> 1;
584
Saleem Abdulrasool8199dad2017-06-20 20:51:51 +0000585#if defined(__linux__)
Rui Ueyama412b29e2017-10-31 17:37:20 +0000586 // It is observed that Linux returns EINVAL for a very large write (>2G).
587 // Make it a reasonably small value.
588 MaxWriteSize = 1024 * 1024 * 1024;
589#elif defined(LLVM_ON_WIN32)
Yunzhong Gaofb2a9c42016-01-06 00:50:06 +0000590 // Writing a large size of output to Windows console returns ENOMEM. It seems
591 // that, prior to Windows 8, WriteFile() is redirecting to WriteConsole(), and
592 // the latter has a size limit (66000 bytes or less, depending on heap usage).
Rui Ueyama412b29e2017-10-31 17:37:20 +0000593 if (::_isatty(FD) && !RunningWindows8OrGreater())
594 MaxWriteSize = 32767;
Yunzhong Gaofb2a9c42016-01-06 00:50:06 +0000595#endif
596
Benjamin Kramerce84a252010-05-05 15:17:47 +0000597 do {
Rui Ueyama412b29e2017-10-31 17:37:20 +0000598 size_t ChunkSize = std::min(Size, MaxWriteSize);
Yunzhong Gaofb2a9c42016-01-06 00:50:06 +0000599 ssize_t ret = ::write(FD, Ptr, ChunkSize);
Dan Gohmanef969f32010-05-06 01:27:36 +0000600
601 if (ret < 0) {
602 // If it's a recoverable error, swallow it and retry the write.
Dan Gohmandea53102010-05-18 15:25:14 +0000603 //
604 // Ideally we wouldn't ever see EAGAIN or EWOULDBLOCK here, since
605 // raw_ostream isn't designed to do non-blocking I/O. However, some
606 // programs, such as old versions of bjam, have mistakenly used
607 // O_NONBLOCK. For compatibility, emulate blocking semantics by
608 // spinning until the write succeeds. If you don't want spinning,
609 // don't use O_NONBLOCK file descriptors with raw_ostream.
Dan Gohmand3511162010-05-06 02:06:20 +0000610 if (errno == EINTR || errno == EAGAIN
611#ifdef EWOULDBLOCK
612 || errno == EWOULDBLOCK
613#endif
614 )
Dan Gohmanef969f32010-05-06 01:27:36 +0000615 continue;
616
617 // Otherwise it's a non-recoverable error. Note it and quit.
Bob Haarman9ce2d032017-10-24 01:26:22 +0000618 error_detected(std::error_code(errno, std::generic_category()));
Dan Gohmanef969f32010-05-06 01:27:36 +0000619 break;
620 }
621
622 // The write may have written some or all of the data. Update the
623 // size and buffer pointer to reflect the remainder that needs
624 // to be written. If there are no bytes left, we're done.
625 Ptr += ret;
626 Size -= ret;
627 } while (Size > 0);
Chris Lattner84b94f72008-08-17 01:35:29 +0000628}
629
Dan Gohman44790e72010-08-18 01:34:52 +0000630void raw_fd_ostream::close() {
631 assert(ShouldClose);
632 ShouldClose = false;
633 flush();
Bob Haarman9ce2d032017-10-24 01:26:22 +0000634 if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
635 error_detected(EC);
Dan Gohman44790e72010-08-18 01:34:52 +0000636 FD = -1;
637}
638
Ted Kremeneka2669922009-01-26 21:42:04 +0000639uint64_t raw_fd_ostream::seek(uint64_t off) {
Yaron Keren7cf7f802016-02-23 07:17:58 +0000640 assert(SupportsSeeking && "Stream does not support seeking!");
Ted Kremeneka2669922009-01-26 21:42:04 +0000641 flush();
Peter Collingbourne8db7e5e2016-12-09 05:20:43 +0000642#ifdef LLVM_ON_WIN32
643 pos = ::_lseeki64(FD, off, SEEK_SET);
644#elif defined(HAVE_LSEEK64)
645 pos = ::lseek64(FD, off, SEEK_SET);
646#else
Peter Collingbournef74fcdd2016-12-09 05:04:30 +0000647 pos = ::lseek(FD, off, SEEK_SET);
Peter Collingbourne8db7e5e2016-12-09 05:20:43 +0000648#endif
Rafael Espindola642a2212015-04-14 22:54:16 +0000649 if (pos == (uint64_t)-1)
Bob Haarman9ce2d032017-10-24 01:26:22 +0000650 error_detected(std::error_code(errno, std::generic_category()));
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000651 return pos;
Ted Kremeneka2669922009-01-26 21:42:04 +0000652}
653
Rafael Espindola4ba9af12015-04-20 13:04:30 +0000654void raw_fd_ostream::pwrite_impl(const char *Ptr, size_t Size,
655 uint64_t Offset) {
Rafael Espindola37b70152015-04-14 15:00:34 +0000656 uint64_t Pos = tell();
657 seek(Offset);
658 write(Ptr, Size);
659 seek(Pos);
660}
661
Chris Lattnerdd3e9aa2009-12-19 01:38:42 +0000662size_t raw_fd_ostream::preferred_buffer_size() const {
Chris Lattnerca97c922010-07-07 15:52:27 +0000663#if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__minix)
Chris Lattnerc86cdc72010-04-09 20:45:04 +0000664 // Windows and Minix have no st_blksize.
Dan Gohman84487b92009-08-13 17:27:29 +0000665 assert(FD >= 0 && "File not yet open!");
666 struct stat statbuf;
Chris Lattnerdd3e9aa2009-12-19 01:38:42 +0000667 if (fstat(FD, &statbuf) != 0)
668 return 0;
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000669
Chris Lattnerdd3e9aa2009-12-19 01:38:42 +0000670 // If this is a terminal, don't use buffering. Line buffering
671 // would be a more traditional thing to do, but it's not worth
672 // the complexity.
673 if (S_ISCHR(statbuf.st_mode) && isatty(FD))
674 return 0;
675 // Return the preferred block size.
676 return statbuf.st_blksize;
Dan Gohmanfeaeb362010-05-28 16:50:01 +0000677#else
Dan Gohman84487b92009-08-13 17:27:29 +0000678 return raw_ostream::preferred_buffer_size();
Dan Gohmanfeaeb362010-05-28 16:50:01 +0000679#endif
Dan Gohman84487b92009-08-13 17:27:29 +0000680}
681
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000682raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
683 bool bg) {
684 if (sys::Process::ColorNeedsFlush())
685 flush();
686 const char *colorcode =
687 (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
688 : sys::Process::OutputColor(colors, bold, bg);
689 if (colorcode) {
Dan Gohmanf199ad62009-07-16 15:24:40 +0000690 size_t len = strlen(colorcode);
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000691 write(colorcode, len);
692 // don't account colors towards output characters
693 pos -= len;
694 }
695 return *this;
696}
697
698raw_ostream &raw_fd_ostream::resetColor() {
699 if (sys::Process::ColorNeedsFlush())
700 flush();
701 const char *colorcode = sys::Process::ResetColor();
702 if (colorcode) {
Dan Gohmanf199ad62009-07-16 15:24:40 +0000703 size_t len = strlen(colorcode);
Torok Edwin9b5a47f2009-06-04 07:09:50 +0000704 write(colorcode, len);
705 // don't account colors towards output characters
706 pos -= len;
707 }
708 return *this;
709}
710
Benjamin Kramer13d16f32012-04-16 08:56:50 +0000711raw_ostream &raw_fd_ostream::reverseColor() {
712 if (sys::Process::ColorNeedsFlush())
713 flush();
714 const char *colorcode = sys::Process::OutputReverse();
715 if (colorcode) {
716 size_t len = strlen(colorcode);
717 write(colorcode, len);
718 // don't account colors towards output characters
719 pos -= len;
720 }
721 return *this;
722}
723
Dan Gohmane5929232009-09-11 20:46:33 +0000724bool raw_fd_ostream::is_displayed() const {
725 return sys::Process::FileDescriptorIsDisplayed(FD);
726}
727
Daniel Dunbar04b45832012-07-20 18:29:41 +0000728bool raw_fd_ostream::has_colors() const {
729 return sys::Process::FileDescriptorHasColors(FD);
730}
731
Weiming Zhao1bd40002018-04-11 23:09:20 +0000732void raw_fd_ostream::anchor() {}
733
Chris Lattnerd3723fc2008-08-17 04:13:37 +0000734//===----------------------------------------------------------------------===//
Dan Gohmane9a46912010-08-20 16:44:56 +0000735// outs(), errs(), nulls()
Chris Lattnerd3723fc2008-08-17 04:13:37 +0000736//===----------------------------------------------------------------------===//
Chris Lattner84b94f72008-08-17 01:35:29 +0000737
Chris Lattnerd3723fc2008-08-17 04:13:37 +0000738/// outs() - This returns a reference to a raw_ostream for standard output.
739/// Use it like: outs() << "foo" << "bar";
Owen Anderson93719642008-08-21 00:14:44 +0000740raw_ostream &llvm::outs() {
Reid Kleckner02aeadc2017-08-04 01:39:23 +0000741 // Set buffer settings to model stdout behavior.
Rafael Espindola79be4cc2015-04-13 10:28:56 +0000742 std::error_code EC;
743 static raw_fd_ostream S("-", EC, sys::fs::F_None);
744 assert(!EC);
Chris Lattnerd3723fc2008-08-17 04:13:37 +0000745 return S;
746}
747
748/// errs() - This returns a reference to a raw_ostream for standard error.
749/// Use it like: errs() << "foo" << "bar";
Owen Anderson93719642008-08-21 00:14:44 +0000750raw_ostream &llvm::errs() {
Dan Gohman443f2d62010-08-20 16:39:41 +0000751 // Set standard error to be unbuffered by default.
752 static raw_fd_ostream S(STDERR_FILENO, false, true);
Chris Lattnerd3723fc2008-08-17 04:13:37 +0000753 return S;
754}
755
Daniel Dunbar95a551a2009-07-16 21:17:53 +0000756/// nulls() - This returns a reference to a raw_ostream which discards output.
757raw_ostream &llvm::nulls() {
758 static raw_null_ostream S;
759 return S;
760}
761
Chris Lattner205af962008-08-23 22:43:04 +0000762//===----------------------------------------------------------------------===//
763// raw_string_ostream
764//===----------------------------------------------------------------------===//
765
Daniel Dunbard882f4b2009-08-18 20:07:36 +0000766raw_string_ostream::~raw_string_ostream() {
767 flush();
768}
769
Dan Gohmanf199ad62009-07-16 15:24:40 +0000770void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
Daniel Dunbardb7a36c2009-03-16 23:29:31 +0000771 OS.append(Ptr, Size);
Chris Lattner205af962008-08-23 22:43:04 +0000772}
773
774//===----------------------------------------------------------------------===//
775// raw_svector_ostream
776//===----------------------------------------------------------------------===//
777
Yaron Keren3d1173b2015-08-13 06:19:52 +0000778uint64_t raw_svector_ostream::current_pos() const { return OS.size(); }
Daniel Dunbarb090bf42009-08-19 17:54:29 +0000779
Yaron Keren3d1173b2015-08-13 06:19:52 +0000780void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
781 OS.append(Ptr, Ptr + Size);
Nick Lewyckye2f6fb52015-08-13 18:10:19 +0000782}
Daniel Dunbard882f4b2009-08-18 20:07:36 +0000783
Rafael Espindola4ba9af12015-04-20 13:04:30 +0000784void raw_svector_ostream::pwrite_impl(const char *Ptr, size_t Size,
785 uint64_t Offset) {
Yaron Keren3d1173b2015-08-13 06:19:52 +0000786 memcpy(OS.data() + Offset, Ptr, Size);
Daniel Dunbare813cba2009-08-19 18:40:58 +0000787}
788
Daniel Dunbar95a551a2009-07-16 21:17:53 +0000789//===----------------------------------------------------------------------===//
790// raw_null_ostream
791//===----------------------------------------------------------------------===//
792
Dan Gohman4b66b472009-07-27 21:46:02 +0000793raw_null_ostream::~raw_null_ostream() {
794#ifndef NDEBUG
795 // ~raw_ostream asserts that the buffer is empty. This isn't necessary
796 // with raw_null_ostream, but it's better to have raw_null_ostream follow
797 // the rules than to change the rules just for raw_null_ostream.
798 flush();
799#endif
800}
801
Daniel Dunbar95a551a2009-07-16 21:17:53 +0000802void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
803}
804
Chris Lattnerdd3e9aa2009-12-19 01:38:42 +0000805uint64_t raw_null_ostream::current_pos() const {
Daniel Dunbar95a551a2009-07-16 21:17:53 +0000806 return 0;
807}
Rafael Espindola37b70152015-04-14 15:00:34 +0000808
Rafael Espindola4ba9af12015-04-20 13:04:30 +0000809void raw_null_ostream::pwrite_impl(const char *Ptr, size_t Size,
810 uint64_t Offset) {}
Weiming Zhao1bd40002018-04-11 23:09:20 +0000811
812void raw_pwrite_stream::anchor() {}