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