raw_ostream.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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/STLArrayExtras.h"
  14. #include "llvm/ADT/StringExtras.h"
  15. #include "llvm/Config/config.h"
  16. #include "llvm/Support/Compiler.h"
  17. #include "llvm/Support/Duration.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 <cerrno>
  28. #include <cstdio>
  29. #include <sys/stat.h>
  30. // <fcntl.h> may provide O_BINARY.
  31. #if defined(HAVE_FCNTL_H)
  32. # include <fcntl.h>
  33. #endif
  34. #if defined(HAVE_UNISTD_H)
  35. # include <unistd.h>
  36. #endif
  37. #if defined(__CYGWIN__)
  38. #include <io.h>
  39. #endif
  40. #if defined(_MSC_VER)
  41. #include <io.h>
  42. #ifndef STDIN_FILENO
  43. # define STDIN_FILENO 0
  44. #endif
  45. #ifndef STDOUT_FILENO
  46. # define STDOUT_FILENO 1
  47. #endif
  48. #ifndef STDERR_FILENO
  49. # define STDERR_FILENO 2
  50. #endif
  51. #endif
  52. #ifdef _WIN32
  53. #include "llvm/Support/ConvertUTF.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]; LLVM_FALLTHROUGH;
  249. case 3: OutBufCur[2] = Ptr[2]; LLVM_FALLTHROUGH;
  250. case 2: OutBufCur[1] = Ptr[1]; LLVM_FALLTHROUGH;
  251. case 1: OutBufCur[0] = Ptr[0]; LLVM_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.hasValue()) {
  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.hasValue()) {
  374. uint64_t Offset = FB.FirstByteOffset.getValue();
  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 < array_lengthof(Chars))
  419. return OS.write(Chars, NumChars);
  420. while (NumChars) {
  421. unsigned NumToWrite = std::min(NumChars,
  422. (unsigned)array_lengthof(Chars)-1);
  423. OS.write(Chars, NumToWrite);
  424. NumChars -= NumToWrite;
  425. }
  426. return OS;
  427. }
  428. /// indent - Insert 'NumSpaces' spaces.
  429. raw_ostream &raw_ostream::indent(unsigned NumSpaces) {
  430. return write_padding<' '>(*this, NumSpaces);
  431. }
  432. /// write_zeros - Insert 'NumZeros' nulls.
  433. raw_ostream &raw_ostream::write_zeros(unsigned NumZeros) {
  434. return write_padding<'\0'>(*this, NumZeros);
  435. }
  436. bool raw_ostream::prepare_colors() {
  437. // Colors were explicitly disabled.
  438. if (!ColorEnabled)
  439. return false;
  440. // Colors require changing the terminal but this stream is not going to a
  441. // terminal.
  442. if (sys::Process::ColorNeedsFlush() && !is_displayed())
  443. return false;
  444. if (sys::Process::ColorNeedsFlush())
  445. flush();
  446. return true;
  447. }
  448. raw_ostream &raw_ostream::changeColor(enum Colors colors, bool bold, bool bg) {
  449. if (!prepare_colors())
  450. return *this;
  451. const char *colorcode =
  452. (colors == SAVEDCOLOR)
  453. ? sys::Process::OutputBold(bg)
  454. : sys::Process::OutputColor(static_cast<char>(colors), bold, bg);
  455. if (colorcode)
  456. write(colorcode, strlen(colorcode));
  457. return *this;
  458. }
  459. raw_ostream &raw_ostream::resetColor() {
  460. if (!prepare_colors())
  461. return *this;
  462. if (const char *colorcode = sys::Process::ResetColor())
  463. write(colorcode, strlen(colorcode));
  464. return *this;
  465. }
  466. raw_ostream &raw_ostream::reverseColor() {
  467. if (!prepare_colors())
  468. return *this;
  469. if (const char *colorcode = sys::Process::OutputReverse())
  470. write(colorcode, strlen(colorcode));
  471. return *this;
  472. }
  473. void raw_ostream::anchor() {}
  474. //===----------------------------------------------------------------------===//
  475. // Formatted Output
  476. //===----------------------------------------------------------------------===//
  477. // Out of line virtual method.
  478. void format_object_base::home() {
  479. }
  480. //===----------------------------------------------------------------------===//
  481. // raw_fd_ostream
  482. //===----------------------------------------------------------------------===//
  483. static int getFD(StringRef Filename, std::error_code &EC,
  484. sys::fs::CreationDisposition Disp, sys::fs::FileAccess Access,
  485. sys::fs::OpenFlags Flags) {
  486. assert((Access & sys::fs::FA_Write) &&
  487. "Cannot make a raw_ostream from a read-only descriptor!");
  488. // Handle "-" as stdout. Note that when we do this, we consider ourself
  489. // the owner of stdout and may set the "binary" flag globally based on Flags.
  490. if (Filename == "-") {
  491. EC = std::error_code();
  492. // Change stdout's text/binary mode based on the Flags.
  493. sys::ChangeStdoutMode(Flags);
  494. return STDOUT_FILENO;
  495. }
  496. int FD;
  497. if (Access & sys::fs::FA_Read)
  498. EC = sys::fs::openFileForReadWrite(Filename, FD, Disp, Flags);
  499. else
  500. EC = sys::fs::openFileForWrite(Filename, FD, Disp, Flags);
  501. if (EC)
  502. return -1;
  503. return FD;
  504. }
  505. raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC)
  506. : raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, sys::fs::FA_Write,
  507. sys::fs::OF_None) {}
  508. raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
  509. sys::fs::CreationDisposition Disp)
  510. : raw_fd_ostream(Filename, EC, Disp, sys::fs::FA_Write, sys::fs::OF_None) {}
  511. raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
  512. sys::fs::FileAccess Access)
  513. : raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, Access,
  514. sys::fs::OF_None) {}
  515. raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
  516. sys::fs::OpenFlags Flags)
  517. : raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, sys::fs::FA_Write,
  518. Flags) {}
  519. raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
  520. sys::fs::CreationDisposition Disp,
  521. sys::fs::FileAccess Access,
  522. sys::fs::OpenFlags Flags)
  523. : raw_fd_ostream(getFD(Filename, EC, Disp, Access, Flags), true) {}
  524. /// FD is the file descriptor that this writes to. If ShouldClose is true, this
  525. /// closes the file when the stream is destroyed.
  526. raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered,
  527. OStreamKind K)
  528. : raw_pwrite_stream(unbuffered, K), FD(fd), ShouldClose(shouldClose) {
  529. if (FD < 0 ) {
  530. ShouldClose = false;
  531. return;
  532. }
  533. enable_colors(true);
  534. // Do not attempt to close stdout or stderr. We used to try to maintain the
  535. // property that tools that support writing file to stdout should not also
  536. // write informational output to stdout, but in practice we were never able to
  537. // maintain this invariant. Many features have been added to LLVM and clang
  538. // (-fdump-record-layouts, optimization remarks, etc) that print to stdout, so
  539. // users must simply be aware that mixed output and remarks is a possibility.
  540. if (FD <= STDERR_FILENO)
  541. ShouldClose = false;
  542. #ifdef _WIN32
  543. // Check if this is a console device. This is not equivalent to isatty.
  544. IsWindowsConsole =
  545. ::GetFileType((HANDLE)::_get_osfhandle(fd)) == FILE_TYPE_CHAR;
  546. #endif
  547. // Get the starting position.
  548. off_t loc = ::lseek(FD, 0, SEEK_CUR);
  549. sys::fs::file_status Status;
  550. std::error_code EC = status(FD, Status);
  551. IsRegularFile = Status.type() == sys::fs::file_type::regular_file;
  552. #ifdef _WIN32
  553. // MSVCRT's _lseek(SEEK_CUR) doesn't return -1 for pipes.
  554. SupportsSeeking = !EC && IsRegularFile;
  555. #else
  556. SupportsSeeking = !EC && loc != (off_t)-1;
  557. #endif
  558. if (!SupportsSeeking)
  559. pos = 0;
  560. else
  561. pos = static_cast<uint64_t>(loc);
  562. }
  563. raw_fd_ostream::~raw_fd_ostream() {
  564. if (FD >= 0) {
  565. flush();
  566. if (ShouldClose) {
  567. if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
  568. error_detected(EC);
  569. }
  570. }
  571. #ifdef __MINGW32__
  572. // On mingw, global dtors should not call exit().
  573. // report_fatal_error() invokes exit(). We know report_fatal_error()
  574. // might not write messages to stderr when any errors were detected
  575. // on FD == 2.
  576. if (FD == 2) return;
  577. #endif
  578. // If there are any pending errors, report them now. Clients wishing
  579. // to avoid report_fatal_error calls should check for errors with
  580. // has_error() and clear the error flag with clear_error() before
  581. // destructing raw_ostream objects which may have errors.
  582. if (has_error())
  583. report_fatal_error(Twine("IO failure on output stream: ") +
  584. error().message(),
  585. /*gen_crash_diag=*/false);
  586. }
  587. #if defined(_WIN32)
  588. // The most reliable way to print unicode in a Windows console is with
  589. // WriteConsoleW. To use that, first transcode from UTF-8 to UTF-16. This
  590. // assumes that LLVM programs always print valid UTF-8 to the console. The data
  591. // might not be UTF-8 for two major reasons:
  592. // 1. The program is printing binary (-filetype=obj -o -), in which case it
  593. // would have been gibberish anyway.
  594. // 2. The program is printing text in a semi-ascii compatible codepage like
  595. // shift-jis or cp1252.
  596. //
  597. // Most LLVM programs don't produce non-ascii text unless they are quoting
  598. // user source input. A well-behaved LLVM program should either validate that
  599. // the input is UTF-8 or transcode from the local codepage to UTF-8 before
  600. // quoting it. If they don't, this may mess up the encoding, but this is still
  601. // probably the best compromise we can make.
  602. static bool write_console_impl(int FD, StringRef Data) {
  603. SmallVector<wchar_t, 256> WideText;
  604. // Fall back to ::write if it wasn't valid UTF-8.
  605. if (auto EC = sys::windows::UTF8ToUTF16(Data, WideText))
  606. return false;
  607. // On Windows 7 and earlier, WriteConsoleW has a low maximum amount of data
  608. // that can be written to the console at a time.
  609. size_t MaxWriteSize = WideText.size();
  610. if (!RunningWindows8OrGreater())
  611. MaxWriteSize = 32767;
  612. size_t WCharsWritten = 0;
  613. do {
  614. size_t WCharsToWrite =
  615. std::min(MaxWriteSize, WideText.size() - WCharsWritten);
  616. DWORD ActuallyWritten;
  617. bool Success =
  618. ::WriteConsoleW((HANDLE)::_get_osfhandle(FD), &WideText[WCharsWritten],
  619. WCharsToWrite, &ActuallyWritten,
  620. /*Reserved=*/nullptr);
  621. // The most likely reason for WriteConsoleW to fail is that FD no longer
  622. // points to a console. Fall back to ::write. If this isn't the first loop
  623. // iteration, something is truly wrong.
  624. if (!Success)
  625. return false;
  626. WCharsWritten += ActuallyWritten;
  627. } while (WCharsWritten != WideText.size());
  628. return true;
  629. }
  630. #endif
  631. void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
  632. assert(FD >= 0 && "File already closed.");
  633. pos += Size;
  634. #if defined(_WIN32)
  635. // If this is a Windows console device, try re-encoding from UTF-8 to UTF-16
  636. // and using WriteConsoleW. If that fails, fall back to plain write().
  637. if (IsWindowsConsole)
  638. if (write_console_impl(FD, StringRef(Ptr, Size)))
  639. return;
  640. #endif
  641. // The maximum write size is limited to INT32_MAX. A write
  642. // greater than SSIZE_MAX is implementation-defined in POSIX,
  643. // and Windows _write requires 32 bit input.
  644. size_t MaxWriteSize = INT32_MAX;
  645. #if defined(__linux__)
  646. // It is observed that Linux returns EINVAL for a very large write (>2G).
  647. // Make it a reasonably small value.
  648. MaxWriteSize = 1024 * 1024 * 1024;
  649. #endif
  650. do {
  651. size_t ChunkSize = std::min(Size, MaxWriteSize);
  652. ssize_t ret = ::write(FD, Ptr, ChunkSize);
  653. if (ret < 0) {
  654. // If it's a recoverable error, swallow it and retry the write.
  655. //
  656. // Ideally we wouldn't ever see EAGAIN or EWOULDBLOCK here, since
  657. // raw_ostream isn't designed to do non-blocking I/O. However, some
  658. // programs, such as old versions of bjam, have mistakenly used
  659. // O_NONBLOCK. For compatibility, emulate blocking semantics by
  660. // spinning until the write succeeds. If you don't want spinning,
  661. // don't use O_NONBLOCK file descriptors with raw_ostream.
  662. if (errno == EINTR || errno == EAGAIN
  663. #ifdef EWOULDBLOCK
  664. || errno == EWOULDBLOCK
  665. #endif
  666. )
  667. continue;
  668. // Otherwise it's a non-recoverable error. Note it and quit.
  669. error_detected(std::error_code(errno, std::generic_category()));
  670. break;
  671. }
  672. // The write may have written some or all of the data. Update the
  673. // size and buffer pointer to reflect the remainder that needs
  674. // to be written. If there are no bytes left, we're done.
  675. Ptr += ret;
  676. Size -= ret;
  677. } while (Size > 0);
  678. }
  679. void raw_fd_ostream::close() {
  680. assert(ShouldClose);
  681. ShouldClose = false;
  682. flush();
  683. if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
  684. error_detected(EC);
  685. FD = -1;
  686. }
  687. uint64_t raw_fd_ostream::seek(uint64_t off) {
  688. assert(SupportsSeeking && "Stream does not support seeking!");
  689. flush();
  690. #ifdef _WIN32
  691. pos = ::_lseeki64(FD, off, SEEK_SET);
  692. #elif defined(HAVE_LSEEK64)
  693. pos = ::lseek64(FD, off, SEEK_SET);
  694. #else
  695. pos = ::lseek(FD, off, SEEK_SET);
  696. #endif
  697. if (pos == (uint64_t)-1)
  698. error_detected(std::error_code(errno, std::generic_category()));
  699. return pos;
  700. }
  701. void raw_fd_ostream::pwrite_impl(const char *Ptr, size_t Size,
  702. uint64_t Offset) {
  703. uint64_t Pos = tell();
  704. seek(Offset);
  705. write(Ptr, Size);
  706. seek(Pos);
  707. }
  708. size_t raw_fd_ostream::preferred_buffer_size() const {
  709. #if defined(_WIN32)
  710. // Disable buffering for console devices. Console output is re-encoded from
  711. // UTF-8 to UTF-16 on Windows, and buffering it would require us to split the
  712. // buffer on a valid UTF-8 codepoint boundary. Terminal buffering is disabled
  713. // below on most other OSs, so do the same thing on Windows and avoid that
  714. // complexity.
  715. if (IsWindowsConsole)
  716. return 0;
  717. return raw_ostream::preferred_buffer_size();
  718. #elif !defined(__minix)
  719. // Minix has no st_blksize.
  720. assert(FD >= 0 && "File not yet open!");
  721. struct stat statbuf;
  722. if (fstat(FD, &statbuf) != 0)
  723. return 0;
  724. // If this is a terminal, don't use buffering. Line buffering
  725. // would be a more traditional thing to do, but it's not worth
  726. // the complexity.
  727. if (S_ISCHR(statbuf.st_mode) && is_displayed())
  728. return 0;
  729. // Return the preferred block size.
  730. return statbuf.st_blksize;
  731. #else
  732. return raw_ostream::preferred_buffer_size();
  733. #endif
  734. }
  735. bool raw_fd_ostream::is_displayed() const {
  736. return sys::Process::FileDescriptorIsDisplayed(FD);
  737. }
  738. bool raw_fd_ostream::has_colors() const {
  739. if (!HasColors)
  740. HasColors = sys::Process::FileDescriptorHasColors(FD);
  741. return *HasColors;
  742. }
  743. Expected<sys::fs::FileLocker> raw_fd_ostream::lock() {
  744. std::error_code EC = sys::fs::lockFile(FD);
  745. if (!EC)
  746. return sys::fs::FileLocker(FD);
  747. return errorCodeToError(EC);
  748. }
  749. Expected<sys::fs::FileLocker>
  750. raw_fd_ostream::tryLockFor(Duration const& Timeout) {
  751. std::error_code EC = sys::fs::tryLockFile(FD, Timeout.getDuration());
  752. if (!EC)
  753. return sys::fs::FileLocker(FD);
  754. return errorCodeToError(EC);
  755. }
  756. void raw_fd_ostream::anchor() {}
  757. //===----------------------------------------------------------------------===//
  758. // outs(), errs(), nulls()
  759. //===----------------------------------------------------------------------===//
  760. raw_fd_ostream &llvm::outs() {
  761. // Set buffer settings to model stdout behavior.
  762. std::error_code EC;
  763. static raw_fd_ostream S("-", EC, sys::fs::OF_None);
  764. assert(!EC);
  765. return S;
  766. }
  767. raw_fd_ostream &llvm::errs() {
  768. // Set standard error to be unbuffered and tied to outs() by default.
  769. static raw_fd_ostream S(STDERR_FILENO, false, true);
  770. return S;
  771. }
  772. /// nulls() - This returns a reference to a raw_ostream which discards output.
  773. raw_ostream &llvm::nulls() {
  774. static raw_null_ostream S;
  775. return S;
  776. }
  777. //===----------------------------------------------------------------------===//
  778. // File Streams
  779. //===----------------------------------------------------------------------===//
  780. raw_fd_stream::raw_fd_stream(StringRef Filename, std::error_code &EC)
  781. : raw_fd_ostream(getFD(Filename, EC, sys::fs::CD_CreateAlways,
  782. sys::fs::FA_Write | sys::fs::FA_Read,
  783. sys::fs::OF_None),
  784. true, false, OStreamKind::OK_FDStream) {
  785. if (EC)
  786. return;
  787. if (!isRegularFile())
  788. EC = std::make_error_code(std::errc::invalid_argument);
  789. }
  790. ssize_t raw_fd_stream::read(char *Ptr, size_t Size) {
  791. assert(get_fd() >= 0 && "File already closed.");
  792. ssize_t Ret = ::read(get_fd(), (void *)Ptr, Size);
  793. if (Ret >= 0)
  794. inc_pos(Ret);
  795. else
  796. error_detected(std::error_code(errno, std::generic_category()));
  797. return Ret;
  798. }
  799. bool raw_fd_stream::classof(const raw_ostream *OS) {
  800. return OS->get_kind() == OStreamKind::OK_FDStream;
  801. }
  802. //===----------------------------------------------------------------------===//
  803. // raw_string_ostream
  804. //===----------------------------------------------------------------------===//
  805. void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
  806. OS.append(Ptr, Size);
  807. }
  808. //===----------------------------------------------------------------------===//
  809. // raw_svector_ostream
  810. //===----------------------------------------------------------------------===//
  811. uint64_t raw_svector_ostream::current_pos() const { return OS.size(); }
  812. void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
  813. OS.append(Ptr, Ptr + Size);
  814. }
  815. void raw_svector_ostream::pwrite_impl(const char *Ptr, size_t Size,
  816. uint64_t Offset) {
  817. memcpy(OS.data() + Offset, Ptr, Size);
  818. }
  819. //===----------------------------------------------------------------------===//
  820. // raw_null_ostream
  821. //===----------------------------------------------------------------------===//
  822. raw_null_ostream::~raw_null_ostream() {
  823. #ifndef NDEBUG
  824. // ~raw_ostream asserts that the buffer is empty. This isn't necessary
  825. // with raw_null_ostream, but it's better to have raw_null_ostream follow
  826. // the rules than to change the rules just for raw_null_ostream.
  827. flush();
  828. #endif
  829. }
  830. void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
  831. }
  832. uint64_t raw_null_ostream::current_pos() const {
  833. return 0;
  834. }
  835. void raw_null_ostream::pwrite_impl(const char *Ptr, size_t Size,
  836. uint64_t Offset) {}
  837. void raw_pwrite_stream::anchor() {}
  838. void buffer_ostream::anchor() {}
  839. void buffer_unique_ostream::anchor() {}
  840. Error llvm::writeToOutput(StringRef OutputFileName,
  841. std::function<Error(raw_ostream &)> Write) {
  842. if (OutputFileName == "-")
  843. return Write(outs());
  844. if (OutputFileName == "/dev/null") {
  845. raw_null_ostream Out;
  846. return Write(Out);
  847. }
  848. unsigned Mode = sys::fs::all_read | sys::fs::all_write | sys::fs::all_exe;
  849. Expected<sys::fs::TempFile> Temp =
  850. sys::fs::TempFile::create(OutputFileName + ".temp-stream-%%%%%%", Mode);
  851. if (!Temp)
  852. return createFileError(OutputFileName, Temp.takeError());
  853. raw_fd_ostream Out(Temp->FD, false);
  854. if (Error E = Write(Out)) {
  855. if (Error DiscardError = Temp->discard())
  856. return joinErrors(std::move(E), std::move(DiscardError));
  857. return E;
  858. }
  859. Out.flush();
  860. return Temp->keep(OutputFileName);
  861. }