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