raw_ostream.cpp 31 KB

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