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/StringExtras.h" |
15 | #include "llvm/Config/config.h" |
16 | #include "llvm/Support/AutoConvert.h" |
17 | #include "llvm/Support/Compiler.h" |
18 | #include "llvm/Support/Duration.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 <cerrno> |
29 | #include <cstdio> |
30 | #include <sys/stat.h> |
31 | |
32 | // <fcntl.h> may provide O_BINARY. |
33 | #if defined(HAVE_FCNTL_H) |
34 | # include <fcntl.h> |
35 | #endif |
36 | |
37 | #if defined(HAVE_UNISTD_H) |
38 | # include <unistd.h> |
39 | #endif |
40 | |
41 | #if defined(__CYGWIN__) |
42 | #include <io.h> |
43 | #endif |
44 | |
45 | #if defined(_MSC_VER) |
46 | #include <io.h> |
47 | #ifndef STDIN_FILENO |
48 | # define STDIN_FILENO 0 |
49 | #endif |
50 | #ifndef STDOUT_FILENO |
51 | # define STDOUT_FILENO 1 |
52 | #endif |
53 | #ifndef STDERR_FILENO |
54 | # define STDERR_FILENO 2 |
55 | #endif |
56 | #endif |
57 | |
58 | #ifdef _WIN32 |
59 | #include "llvm/Support/ConvertUTF.h" |
60 | #include "llvm/Support/Signals.h" |
61 | #include "llvm/Support/Windows/WindowsSupport.h" |
62 | #endif |
63 | |
64 | using namespace llvm; |
65 | |
66 | constexpr raw_ostream::Colors raw_ostream::BLACK; |
67 | constexpr raw_ostream::Colors raw_ostream::RED; |
68 | constexpr raw_ostream::Colors raw_ostream::GREEN; |
69 | constexpr raw_ostream::Colors raw_ostream::YELLOW; |
70 | constexpr raw_ostream::Colors raw_ostream::BLUE; |
71 | constexpr raw_ostream::Colors raw_ostream::MAGENTA; |
72 | constexpr raw_ostream::Colors raw_ostream::CYAN; |
73 | constexpr raw_ostream::Colors raw_ostream::WHITE; |
74 | constexpr raw_ostream::Colors raw_ostream::SAVEDCOLOR; |
75 | constexpr raw_ostream::Colors raw_ostream::RESET; |
76 | |
77 | raw_ostream::~raw_ostream() { |
78 | // raw_ostream's subclasses should take care to flush the buffer |
79 | // in their destructors. |
80 | assert(OutBufCur == OutBufStart && |
81 | "raw_ostream destructor called with non-empty buffer!" ); |
82 | |
83 | if (BufferMode == BufferKind::InternalBuffer) |
84 | delete [] OutBufStart; |
85 | } |
86 | |
87 | size_t raw_ostream::preferred_buffer_size() const { |
88 | #ifdef _WIN32 |
89 | // On Windows BUFSIZ is only 512 which results in more calls to write. This |
90 | // overhead can cause significant performance degradation. Therefore use a |
91 | // better default. |
92 | return (16 * 1024); |
93 | #else |
94 | // BUFSIZ is intended to be a reasonable default. |
95 | return BUFSIZ; |
96 | #endif |
97 | } |
98 | |
99 | void raw_ostream::SetBuffered() { |
100 | // Ask the subclass to determine an appropriate buffer size. |
101 | if (size_t Size = preferred_buffer_size()) |
102 | SetBufferSize(Size); |
103 | else |
104 | // It may return 0, meaning this stream should be unbuffered. |
105 | SetUnbuffered(); |
106 | } |
107 | |
108 | void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size, |
109 | BufferKind Mode) { |
110 | assert(((Mode == BufferKind::Unbuffered && !BufferStart && Size == 0) || |
111 | (Mode != BufferKind::Unbuffered && BufferStart && Size != 0)) && |
112 | "stream must be unbuffered or have at least one byte" ); |
113 | // Make sure the current buffer is free of content (we can't flush here; the |
114 | // child buffer management logic will be in write_impl). |
115 | assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!" ); |
116 | |
117 | if (BufferMode == BufferKind::InternalBuffer) |
118 | delete [] OutBufStart; |
119 | OutBufStart = BufferStart; |
120 | OutBufEnd = OutBufStart+Size; |
121 | OutBufCur = OutBufStart; |
122 | BufferMode = Mode; |
123 | |
124 | assert(OutBufStart <= OutBufEnd && "Invalid size!" ); |
125 | } |
126 | |
127 | raw_ostream &raw_ostream::operator<<(unsigned long N) { |
128 | write_integer(S&: *this, N: static_cast<uint64_t>(N), MinDigits: 0, Style: IntegerStyle::Integer); |
129 | return *this; |
130 | } |
131 | |
132 | raw_ostream &raw_ostream::operator<<(long N) { |
133 | write_integer(S&: *this, N: static_cast<int64_t>(N), MinDigits: 0, Style: IntegerStyle::Integer); |
134 | return *this; |
135 | } |
136 | |
137 | raw_ostream &raw_ostream::operator<<(unsigned long long N) { |
138 | write_integer(S&: *this, N: static_cast<uint64_t>(N), MinDigits: 0, Style: IntegerStyle::Integer); |
139 | return *this; |
140 | } |
141 | |
142 | raw_ostream &raw_ostream::operator<<(long long N) { |
143 | write_integer(S&: *this, N: static_cast<int64_t>(N), MinDigits: 0, Style: IntegerStyle::Integer); |
144 | return *this; |
145 | } |
146 | |
147 | raw_ostream &raw_ostream::write_hex(unsigned long long N) { |
148 | llvm::write_hex(S&: *this, N, Style: 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(Color: 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(Fmt: "%02" PRIX32, Vals: 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: c)) { |
188 | *this << c; |
189 | break; |
190 | } |
191 | |
192 | // Write out the escaped representation. |
193 | if (UseHexEscapes) { |
194 | *this << '\\' << 'x'; |
195 | *this << hexdigit(X: (c >> 4) & 0xF); |
196 | *this << hexdigit(X: (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(S&: *this, N: (uintptr_t)P, Style: HexPrintStyle::PrefixLower); |
212 | return *this; |
213 | } |
214 | |
215 | raw_ostream &raw_ostream::operator<<(double N) { |
216 | llvm::write_double(S&: *this, D: N, Style: 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(Ptr: OutBufStart, Size: 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 == BufferKind::Unbuffered) { |
232 | write_impl(Ptr: reinterpret_cast<char *>(&C), Size: 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 == BufferKind::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, Size: 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: Ptr + BytesToWrite, Size: BytesRemaining); |
273 | } |
274 | copy_to_buffer(Ptr: Ptr + BytesToWrite, Size: 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, Size: NumBytes); |
281 | flush_nonempty(); |
282 | return write(Ptr: Ptr + NumBytes, Size: 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]; [[fallthrough]]; |
297 | case 3: OutBufCur[2] = Ptr[2]; [[fallthrough]]; |
298 | case 2: OutBufCur[1] = Ptr[1]; [[fallthrough]]; |
299 | case 1: OutBufCur[0] = Ptr[0]; [[fallthrough]]; |
300 | case 0: break; |
301 | default: |
302 | memcpy(dest: OutBufCur, src: Ptr, n: 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(Buffer: OutBufCur, BufferSize: 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(N: NextBufferSize); |
336 | |
337 | // Try formatting into the SmallVector. |
338 | size_t BytesUsed = Fmt.print(Buffer: V.data(), BufferSize: NextBufferSize); |
339 | |
340 | // If BytesUsed fit into the vector, we win. |
341 | if (BytesUsed <= NextBufferSize) |
342 | return write(Ptr: V.data(), Size: 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 | Obj.format(S&: *this); |
352 | return *this; |
353 | } |
354 | |
355 | raw_ostream &raw_ostream::operator<<(const FormattedString &FS) { |
356 | unsigned LeftIndent = 0; |
357 | unsigned RightIndent = 0; |
358 | const ssize_t Difference = FS.Width - FS.Str.size(); |
359 | if (Difference > 0) { |
360 | switch (FS.Justify) { |
361 | case FormattedString::JustifyNone: |
362 | break; |
363 | case FormattedString::JustifyLeft: |
364 | RightIndent = Difference; |
365 | break; |
366 | case FormattedString::JustifyRight: |
367 | LeftIndent = Difference; |
368 | break; |
369 | case FormattedString::JustifyCenter: |
370 | LeftIndent = Difference / 2; |
371 | RightIndent = Difference - LeftIndent; |
372 | break; |
373 | } |
374 | } |
375 | indent(NumSpaces: LeftIndent); |
376 | (*this) << FS.Str; |
377 | indent(NumSpaces: RightIndent); |
378 | return *this; |
379 | } |
380 | |
381 | raw_ostream &raw_ostream::operator<<(const FormattedNumber &FN) { |
382 | if (FN.Hex) { |
383 | HexPrintStyle Style; |
384 | if (FN.Upper && FN.HexPrefix) |
385 | Style = HexPrintStyle::PrefixUpper; |
386 | else if (FN.Upper && !FN.HexPrefix) |
387 | Style = HexPrintStyle::Upper; |
388 | else if (!FN.Upper && FN.HexPrefix) |
389 | Style = HexPrintStyle::PrefixLower; |
390 | else |
391 | Style = HexPrintStyle::Lower; |
392 | llvm::write_hex(S&: *this, N: FN.HexValue, Style, Width: FN.Width); |
393 | } else { |
394 | llvm::SmallString<16> Buffer; |
395 | llvm::raw_svector_ostream Stream(Buffer); |
396 | llvm::write_integer(S&: Stream, N: FN.DecValue, MinDigits: 0, Style: IntegerStyle::Integer); |
397 | if (Buffer.size() < FN.Width) |
398 | indent(NumSpaces: FN.Width - Buffer.size()); |
399 | (*this) << Buffer; |
400 | } |
401 | return *this; |
402 | } |
403 | |
404 | raw_ostream &raw_ostream::operator<<(const FormattedBytes &FB) { |
405 | if (FB.Bytes.empty()) |
406 | return *this; |
407 | |
408 | size_t LineIndex = 0; |
409 | auto Bytes = FB.Bytes; |
410 | const size_t Size = Bytes.size(); |
411 | HexPrintStyle HPS = FB.Upper ? HexPrintStyle::Upper : HexPrintStyle::Lower; |
412 | uint64_t OffsetWidth = 0; |
413 | if (FB.FirstByteOffset) { |
414 | // Figure out how many nibbles are needed to print the largest offset |
415 | // represented by this data set, so that we can align the offset field |
416 | // to the right width. |
417 | size_t Lines = Size / FB.NumPerLine; |
418 | uint64_t MaxOffset = *FB.FirstByteOffset + Lines * FB.NumPerLine; |
419 | unsigned Power = 0; |
420 | if (MaxOffset > 0) |
421 | Power = llvm::Log2_64_Ceil(Value: MaxOffset); |
422 | OffsetWidth = std::max<uint64_t>(a: 4, b: llvm::alignTo(Value: Power, Align: 4) / 4); |
423 | } |
424 | |
425 | // The width of a block of data including all spaces for group separators. |
426 | unsigned NumByteGroups = |
427 | alignTo(Value: FB.NumPerLine, Align: FB.ByteGroupSize) / FB.ByteGroupSize; |
428 | unsigned BlockCharWidth = FB.NumPerLine * 2 + NumByteGroups - 1; |
429 | |
430 | while (!Bytes.empty()) { |
431 | indent(NumSpaces: FB.IndentLevel); |
432 | |
433 | if (FB.FirstByteOffset) { |
434 | uint64_t Offset = *FB.FirstByteOffset; |
435 | llvm::write_hex(S&: *this, N: Offset + LineIndex, Style: HPS, Width: OffsetWidth); |
436 | *this << ": " ; |
437 | } |
438 | |
439 | auto Line = Bytes.take_front(N: FB.NumPerLine); |
440 | |
441 | size_t CharsPrinted = 0; |
442 | // Print the hex bytes for this line in groups |
443 | for (size_t I = 0; I < Line.size(); ++I, CharsPrinted += 2) { |
444 | if (I && (I % FB.ByteGroupSize) == 0) { |
445 | ++CharsPrinted; |
446 | *this << " " ; |
447 | } |
448 | llvm::write_hex(S&: *this, N: Line[I], Style: HPS, Width: 2); |
449 | } |
450 | |
451 | if (FB.ASCII) { |
452 | // Print any spaces needed for any bytes that we didn't print on this |
453 | // line so that the ASCII bytes are correctly aligned. |
454 | assert(BlockCharWidth >= CharsPrinted); |
455 | indent(NumSpaces: BlockCharWidth - CharsPrinted + 2); |
456 | *this << "|" ; |
457 | |
458 | // Print the ASCII char values for each byte on this line |
459 | for (uint8_t Byte : Line) { |
460 | if (isPrint(C: Byte)) |
461 | *this << static_cast<char>(Byte); |
462 | else |
463 | *this << '.'; |
464 | } |
465 | *this << '|'; |
466 | } |
467 | |
468 | Bytes = Bytes.drop_front(N: Line.size()); |
469 | LineIndex += Line.size(); |
470 | if (LineIndex < Size) |
471 | *this << '\n'; |
472 | } |
473 | return *this; |
474 | } |
475 | |
476 | template <char C> |
477 | static raw_ostream &write_padding(raw_ostream &OS, unsigned NumChars) { |
478 | static const char Chars[] = {C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, |
479 | C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, |
480 | C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, |
481 | C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, |
482 | C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C}; |
483 | |
484 | // Usually the indentation is small, handle it with a fastpath. |
485 | if (NumChars < std::size(Chars)) |
486 | return OS.write(Ptr: Chars, Size: NumChars); |
487 | |
488 | while (NumChars) { |
489 | unsigned NumToWrite = std::min(a: NumChars, b: (unsigned)std::size(Chars) - 1); |
490 | OS.write(Ptr: Chars, Size: NumToWrite); |
491 | NumChars -= NumToWrite; |
492 | } |
493 | return OS; |
494 | } |
495 | |
496 | /// indent - Insert 'NumSpaces' spaces. |
497 | raw_ostream &raw_ostream::indent(unsigned NumSpaces) { |
498 | return write_padding<' '>(OS&: *this, NumChars: NumSpaces); |
499 | } |
500 | |
501 | /// write_zeros - Insert 'NumZeros' nulls. |
502 | raw_ostream &raw_ostream::write_zeros(unsigned NumZeros) { |
503 | return write_padding<'\0'>(OS&: *this, NumChars: NumZeros); |
504 | } |
505 | |
506 | bool raw_ostream::prepare_colors() { |
507 | // Colors were explicitly disabled. |
508 | if (!ColorEnabled) |
509 | return false; |
510 | |
511 | // Colors require changing the terminal but this stream is not going to a |
512 | // terminal. |
513 | if (sys::Process::ColorNeedsFlush() && !is_displayed()) |
514 | return false; |
515 | |
516 | if (sys::Process::ColorNeedsFlush()) |
517 | flush(); |
518 | |
519 | return true; |
520 | } |
521 | |
522 | raw_ostream &raw_ostream::changeColor(enum Colors colors, bool bold, bool bg) { |
523 | if (!prepare_colors()) |
524 | return *this; |
525 | |
526 | const char *colorcode = |
527 | (colors == SAVEDCOLOR) |
528 | ? sys::Process::OutputBold(bg) |
529 | : sys::Process::OutputColor(c: static_cast<char>(colors), bold, bg); |
530 | if (colorcode) |
531 | write(Ptr: colorcode, Size: strlen(s: colorcode)); |
532 | return *this; |
533 | } |
534 | |
535 | raw_ostream &raw_ostream::resetColor() { |
536 | if (!prepare_colors()) |
537 | return *this; |
538 | |
539 | if (const char *colorcode = sys::Process::ResetColor()) |
540 | write(Ptr: colorcode, Size: strlen(s: colorcode)); |
541 | return *this; |
542 | } |
543 | |
544 | raw_ostream &raw_ostream::reverseColor() { |
545 | if (!prepare_colors()) |
546 | return *this; |
547 | |
548 | if (const char *colorcode = sys::Process::OutputReverse()) |
549 | write(Ptr: colorcode, Size: strlen(s: colorcode)); |
550 | return *this; |
551 | } |
552 | |
553 | void raw_ostream::anchor() {} |
554 | |
555 | //===----------------------------------------------------------------------===// |
556 | // Formatted Output |
557 | //===----------------------------------------------------------------------===// |
558 | |
559 | // Out of line virtual method. |
560 | void format_object_base::home() { |
561 | } |
562 | |
563 | //===----------------------------------------------------------------------===// |
564 | // raw_fd_ostream |
565 | //===----------------------------------------------------------------------===// |
566 | |
567 | static int getFD(StringRef Filename, std::error_code &EC, |
568 | sys::fs::CreationDisposition Disp, sys::fs::FileAccess Access, |
569 | sys::fs::OpenFlags Flags) { |
570 | assert((Access & sys::fs::FA_Write) && |
571 | "Cannot make a raw_ostream from a read-only descriptor!" ); |
572 | |
573 | // Handle "-" as stdout. Note that when we do this, we consider ourself |
574 | // the owner of stdout and may set the "binary" flag globally based on Flags. |
575 | if (Filename == "-" ) { |
576 | EC = std::error_code(); |
577 | // Change stdout's text/binary mode based on the Flags. |
578 | sys::ChangeStdoutMode(Flags); |
579 | return STDOUT_FILENO; |
580 | } |
581 | |
582 | int FD; |
583 | if (Access & sys::fs::FA_Read) |
584 | EC = sys::fs::openFileForReadWrite(Name: Filename, ResultFD&: FD, Disp, Flags); |
585 | else |
586 | EC = sys::fs::openFileForWrite(Name: Filename, ResultFD&: FD, Disp, Flags); |
587 | if (EC) |
588 | return -1; |
589 | |
590 | return FD; |
591 | } |
592 | |
593 | raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC) |
594 | : raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, sys::fs::FA_Write, |
595 | sys::fs::OF_None) {} |
596 | |
597 | raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC, |
598 | sys::fs::CreationDisposition Disp) |
599 | : raw_fd_ostream(Filename, EC, Disp, sys::fs::FA_Write, sys::fs::OF_None) {} |
600 | |
601 | raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC, |
602 | sys::fs::FileAccess Access) |
603 | : raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, Access, |
604 | sys::fs::OF_None) {} |
605 | |
606 | raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC, |
607 | sys::fs::OpenFlags Flags) |
608 | : raw_fd_ostream(Filename, EC, sys::fs::CD_CreateAlways, sys::fs::FA_Write, |
609 | Flags) {} |
610 | |
611 | raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC, |
612 | sys::fs::CreationDisposition Disp, |
613 | sys::fs::FileAccess Access, |
614 | sys::fs::OpenFlags Flags) |
615 | : raw_fd_ostream(getFD(Filename, EC, Disp, Access, Flags), true) {} |
616 | |
617 | /// FD is the file descriptor that this writes to. If ShouldClose is true, this |
618 | /// closes the file when the stream is destroyed. |
619 | raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered, |
620 | OStreamKind K) |
621 | : raw_pwrite_stream(unbuffered, K), FD(fd), ShouldClose(shouldClose) { |
622 | if (FD < 0 ) { |
623 | ShouldClose = false; |
624 | return; |
625 | } |
626 | |
627 | enable_colors(enable: true); |
628 | |
629 | // Do not attempt to close stdout or stderr. We used to try to maintain the |
630 | // property that tools that support writing file to stdout should not also |
631 | // write informational output to stdout, but in practice we were never able to |
632 | // maintain this invariant. Many features have been added to LLVM and clang |
633 | // (-fdump-record-layouts, optimization remarks, etc) that print to stdout, so |
634 | // users must simply be aware that mixed output and remarks is a possibility. |
635 | if (FD <= STDERR_FILENO) |
636 | ShouldClose = false; |
637 | |
638 | #ifdef _WIN32 |
639 | // Check if this is a console device. This is not equivalent to isatty. |
640 | IsWindowsConsole = |
641 | ::GetFileType((HANDLE)::_get_osfhandle(fd)) == FILE_TYPE_CHAR; |
642 | #endif |
643 | |
644 | // Get the starting position. |
645 | off_t loc = ::lseek(fd: FD, offset: 0, SEEK_CUR); |
646 | sys::fs::file_status Status; |
647 | std::error_code EC = status(FD, Result&: Status); |
648 | IsRegularFile = Status.type() == sys::fs::file_type::regular_file; |
649 | #ifdef _WIN32 |
650 | // MSVCRT's _lseek(SEEK_CUR) doesn't return -1 for pipes. |
651 | SupportsSeeking = !EC && IsRegularFile; |
652 | #else |
653 | SupportsSeeking = !EC && loc != (off_t)-1; |
654 | #endif |
655 | if (!SupportsSeeking) |
656 | pos = 0; |
657 | else |
658 | pos = static_cast<uint64_t>(loc); |
659 | } |
660 | |
661 | raw_fd_ostream::~raw_fd_ostream() { |
662 | if (FD >= 0) { |
663 | flush(); |
664 | if (ShouldClose) { |
665 | if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD)) |
666 | error_detected(EC); |
667 | } |
668 | } |
669 | |
670 | #ifdef __MINGW32__ |
671 | // On mingw, global dtors should not call exit(). |
672 | // report_fatal_error() invokes exit(). We know report_fatal_error() |
673 | // might not write messages to stderr when any errors were detected |
674 | // on FD == 2. |
675 | if (FD == 2) return; |
676 | #endif |
677 | |
678 | // If there are any pending errors, report them now. Clients wishing |
679 | // to avoid report_fatal_error calls should check for errors with |
680 | // has_error() and clear the error flag with clear_error() before |
681 | // destructing raw_ostream objects which may have errors. |
682 | if (has_error()) |
683 | report_fatal_error(reason: Twine("IO failure on output stream: " ) + |
684 | error().message(), |
685 | /*gen_crash_diag=*/false); |
686 | } |
687 | |
688 | #if defined(_WIN32) |
689 | // The most reliable way to print unicode in a Windows console is with |
690 | // WriteConsoleW. To use that, first transcode from UTF-8 to UTF-16. This |
691 | // assumes that LLVM programs always print valid UTF-8 to the console. The data |
692 | // might not be UTF-8 for two major reasons: |
693 | // 1. The program is printing binary (-filetype=obj -o -), in which case it |
694 | // would have been gibberish anyway. |
695 | // 2. The program is printing text in a semi-ascii compatible codepage like |
696 | // shift-jis or cp1252. |
697 | // |
698 | // Most LLVM programs don't produce non-ascii text unless they are quoting |
699 | // user source input. A well-behaved LLVM program should either validate that |
700 | // the input is UTF-8 or transcode from the local codepage to UTF-8 before |
701 | // quoting it. If they don't, this may mess up the encoding, but this is still |
702 | // probably the best compromise we can make. |
703 | static bool write_console_impl(int FD, StringRef Data) { |
704 | SmallVector<wchar_t, 256> WideText; |
705 | |
706 | // Fall back to ::write if it wasn't valid UTF-8. |
707 | if (auto EC = sys::windows::UTF8ToUTF16(Data, WideText)) |
708 | return false; |
709 | |
710 | // On Windows 7 and earlier, WriteConsoleW has a low maximum amount of data |
711 | // that can be written to the console at a time. |
712 | size_t MaxWriteSize = WideText.size(); |
713 | if (!RunningWindows8OrGreater()) |
714 | MaxWriteSize = 32767; |
715 | |
716 | size_t WCharsWritten = 0; |
717 | do { |
718 | size_t WCharsToWrite = |
719 | std::min(MaxWriteSize, WideText.size() - WCharsWritten); |
720 | DWORD ActuallyWritten; |
721 | bool Success = |
722 | ::WriteConsoleW((HANDLE)::_get_osfhandle(FD), &WideText[WCharsWritten], |
723 | WCharsToWrite, &ActuallyWritten, |
724 | /*Reserved=*/nullptr); |
725 | |
726 | // The most likely reason for WriteConsoleW to fail is that FD no longer |
727 | // points to a console. Fall back to ::write. If this isn't the first loop |
728 | // iteration, something is truly wrong. |
729 | if (!Success) |
730 | return false; |
731 | |
732 | WCharsWritten += ActuallyWritten; |
733 | } while (WCharsWritten != WideText.size()); |
734 | return true; |
735 | } |
736 | #endif |
737 | |
738 | void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) { |
739 | if (TiedStream) |
740 | TiedStream->flush(); |
741 | |
742 | assert(FD >= 0 && "File already closed." ); |
743 | pos += Size; |
744 | |
745 | #if defined(_WIN32) |
746 | // If this is a Windows console device, try re-encoding from UTF-8 to UTF-16 |
747 | // and using WriteConsoleW. If that fails, fall back to plain write(). |
748 | if (IsWindowsConsole) |
749 | if (write_console_impl(FD, StringRef(Ptr, Size))) |
750 | return; |
751 | #endif |
752 | |
753 | // The maximum write size is limited to INT32_MAX. A write |
754 | // greater than SSIZE_MAX is implementation-defined in POSIX, |
755 | // and Windows _write requires 32 bit input. |
756 | size_t MaxWriteSize = INT32_MAX; |
757 | |
758 | #if defined(__linux__) |
759 | // It is observed that Linux returns EINVAL for a very large write (>2G). |
760 | // Make it a reasonably small value. |
761 | MaxWriteSize = 1024 * 1024 * 1024; |
762 | #endif |
763 | |
764 | do { |
765 | size_t ChunkSize = std::min(a: Size, b: MaxWriteSize); |
766 | ssize_t ret = ::write(fd: FD, buf: Ptr, n: ChunkSize); |
767 | |
768 | if (ret < 0) { |
769 | // If it's a recoverable error, swallow it and retry the write. |
770 | // |
771 | // Ideally we wouldn't ever see EAGAIN or EWOULDBLOCK here, since |
772 | // raw_ostream isn't designed to do non-blocking I/O. However, some |
773 | // programs, such as old versions of bjam, have mistakenly used |
774 | // O_NONBLOCK. For compatibility, emulate blocking semantics by |
775 | // spinning until the write succeeds. If you don't want spinning, |
776 | // don't use O_NONBLOCK file descriptors with raw_ostream. |
777 | if (errno == EINTR || errno == EAGAIN |
778 | #ifdef EWOULDBLOCK |
779 | || errno == EWOULDBLOCK |
780 | #endif |
781 | ) |
782 | continue; |
783 | |
784 | #ifdef _WIN32 |
785 | // Windows equivalents of SIGPIPE/EPIPE. |
786 | DWORD WinLastError = GetLastError(); |
787 | if (WinLastError == ERROR_BROKEN_PIPE || |
788 | (WinLastError == ERROR_NO_DATA && errno == EINVAL)) { |
789 | llvm::sys::CallOneShotPipeSignalHandler(); |
790 | errno = EPIPE; |
791 | } |
792 | #endif |
793 | // Otherwise it's a non-recoverable error. Note it and quit. |
794 | error_detected(EC: errnoAsErrorCode()); |
795 | break; |
796 | } |
797 | |
798 | // The write may have written some or all of the data. Update the |
799 | // size and buffer pointer to reflect the remainder that needs |
800 | // to be written. If there are no bytes left, we're done. |
801 | Ptr += ret; |
802 | Size -= ret; |
803 | } while (Size > 0); |
804 | } |
805 | |
806 | void raw_fd_ostream::close() { |
807 | assert(ShouldClose); |
808 | ShouldClose = false; |
809 | flush(); |
810 | if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD)) |
811 | error_detected(EC); |
812 | FD = -1; |
813 | } |
814 | |
815 | uint64_t raw_fd_ostream::seek(uint64_t off) { |
816 | assert(SupportsSeeking && "Stream does not support seeking!" ); |
817 | flush(); |
818 | #ifdef _WIN32 |
819 | pos = ::_lseeki64(FD, off, SEEK_SET); |
820 | #else |
821 | pos = ::lseek(fd: FD, offset: off, SEEK_SET); |
822 | #endif |
823 | if (pos == (uint64_t)-1) |
824 | error_detected(EC: errnoAsErrorCode()); |
825 | return pos; |
826 | } |
827 | |
828 | void raw_fd_ostream::pwrite_impl(const char *Ptr, size_t Size, |
829 | uint64_t Offset) { |
830 | uint64_t Pos = tell(); |
831 | seek(off: Offset); |
832 | write(Ptr, Size); |
833 | seek(off: Pos); |
834 | } |
835 | |
836 | size_t raw_fd_ostream::preferred_buffer_size() const { |
837 | #if defined(_WIN32) |
838 | // Disable buffering for console devices. Console output is re-encoded from |
839 | // UTF-8 to UTF-16 on Windows, and buffering it would require us to split the |
840 | // buffer on a valid UTF-8 codepoint boundary. Terminal buffering is disabled |
841 | // below on most other OSs, so do the same thing on Windows and avoid that |
842 | // complexity. |
843 | if (IsWindowsConsole) |
844 | return 0; |
845 | return raw_ostream::preferred_buffer_size(); |
846 | #else |
847 | assert(FD >= 0 && "File not yet open!" ); |
848 | struct stat statbuf; |
849 | if (fstat(fd: FD, buf: &statbuf) != 0) |
850 | return 0; |
851 | |
852 | // If this is a terminal, don't use buffering. Line buffering |
853 | // would be a more traditional thing to do, but it's not worth |
854 | // the complexity. |
855 | if (S_ISCHR(statbuf.st_mode) && is_displayed()) |
856 | return 0; |
857 | // Return the preferred block size. |
858 | return statbuf.st_blksize; |
859 | #endif |
860 | } |
861 | |
862 | bool raw_fd_ostream::is_displayed() const { |
863 | return sys::Process::FileDescriptorIsDisplayed(fd: FD); |
864 | } |
865 | |
866 | bool raw_fd_ostream::has_colors() const { |
867 | if (!HasColors) |
868 | HasColors = sys::Process::FileDescriptorHasColors(fd: FD); |
869 | return *HasColors; |
870 | } |
871 | |
872 | Expected<sys::fs::FileLocker> raw_fd_ostream::lock() { |
873 | std::error_code EC = sys::fs::lockFile(FD); |
874 | if (!EC) |
875 | return sys::fs::FileLocker(FD); |
876 | return errorCodeToError(EC); |
877 | } |
878 | |
879 | Expected<sys::fs::FileLocker> |
880 | raw_fd_ostream::tryLockFor(Duration const& Timeout) { |
881 | std::error_code EC = sys::fs::tryLockFile(FD, Timeout: Timeout.getDuration()); |
882 | if (!EC) |
883 | return sys::fs::FileLocker(FD); |
884 | return errorCodeToError(EC); |
885 | } |
886 | |
887 | void raw_fd_ostream::anchor() {} |
888 | |
889 | //===----------------------------------------------------------------------===// |
890 | // outs(), errs(), nulls() |
891 | //===----------------------------------------------------------------------===// |
892 | |
893 | raw_fd_ostream &llvm::outs() { |
894 | // Set buffer settings to model stdout behavior. |
895 | std::error_code EC; |
896 | #ifdef __MVS__ |
897 | EC = enableAutoConversion(STDOUT_FILENO); |
898 | assert(!EC); |
899 | #endif |
900 | static raw_fd_ostream S("-" , EC, sys::fs::OF_None); |
901 | assert(!EC); |
902 | return S; |
903 | } |
904 | |
905 | raw_fd_ostream &llvm::errs() { |
906 | // Set standard error to be unbuffered. |
907 | #ifdef __MVS__ |
908 | std::error_code EC = enableAutoConversion(STDERR_FILENO); |
909 | assert(!EC); |
910 | #endif |
911 | static raw_fd_ostream S(STDERR_FILENO, false, true); |
912 | return S; |
913 | } |
914 | |
915 | /// nulls() - This returns a reference to a raw_ostream which discards output. |
916 | raw_ostream &llvm::nulls() { |
917 | static raw_null_ostream S; |
918 | return S; |
919 | } |
920 | |
921 | //===----------------------------------------------------------------------===// |
922 | // File Streams |
923 | //===----------------------------------------------------------------------===// |
924 | |
925 | raw_fd_stream::raw_fd_stream(StringRef Filename, std::error_code &EC) |
926 | : raw_fd_ostream(getFD(Filename, EC, Disp: sys::fs::CD_CreateAlways, |
927 | Access: sys::fs::FA_Write | sys::fs::FA_Read, |
928 | Flags: sys::fs::OF_None), |
929 | true, false, OStreamKind::OK_FDStream) { |
930 | if (EC) |
931 | return; |
932 | |
933 | if (!isRegularFile()) |
934 | EC = std::make_error_code(e: std::errc::invalid_argument); |
935 | } |
936 | |
937 | raw_fd_stream::raw_fd_stream(int fd, bool shouldClose) |
938 | : raw_fd_ostream(fd, shouldClose, false, OStreamKind::OK_FDStream) {} |
939 | |
940 | ssize_t raw_fd_stream::read(char *Ptr, size_t Size) { |
941 | assert(get_fd() >= 0 && "File already closed." ); |
942 | ssize_t Ret = ::read(fd: get_fd(), buf: (void *)Ptr, nbytes: Size); |
943 | if (Ret >= 0) |
944 | inc_pos(Delta: Ret); |
945 | else |
946 | error_detected(EC: errnoAsErrorCode()); |
947 | return Ret; |
948 | } |
949 | |
950 | bool raw_fd_stream::classof(const raw_ostream *OS) { |
951 | return OS->get_kind() == OStreamKind::OK_FDStream; |
952 | } |
953 | |
954 | //===----------------------------------------------------------------------===// |
955 | // raw_string_ostream |
956 | //===----------------------------------------------------------------------===// |
957 | |
958 | void raw_string_ostream::write_impl(const char *Ptr, size_t Size) { |
959 | OS.append(s: Ptr, n: Size); |
960 | } |
961 | |
962 | //===----------------------------------------------------------------------===// |
963 | // raw_svector_ostream |
964 | //===----------------------------------------------------------------------===// |
965 | |
966 | uint64_t raw_svector_ostream::current_pos() const { return OS.size(); } |
967 | |
968 | void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) { |
969 | OS.append(in_start: Ptr, in_end: Ptr + Size); |
970 | } |
971 | |
972 | void raw_svector_ostream::pwrite_impl(const char *Ptr, size_t Size, |
973 | uint64_t Offset) { |
974 | memcpy(dest: OS.data() + Offset, src: Ptr, n: Size); |
975 | } |
976 | |
977 | bool raw_svector_ostream::classof(const raw_ostream *OS) { |
978 | return OS->get_kind() == OStreamKind::OK_SVecStream; |
979 | } |
980 | |
981 | //===----------------------------------------------------------------------===// |
982 | // raw_null_ostream |
983 | //===----------------------------------------------------------------------===// |
984 | |
985 | raw_null_ostream::~raw_null_ostream() { |
986 | #ifndef NDEBUG |
987 | // ~raw_ostream asserts that the buffer is empty. This isn't necessary |
988 | // with raw_null_ostream, but it's better to have raw_null_ostream follow |
989 | // the rules than to change the rules just for raw_null_ostream. |
990 | flush(); |
991 | #endif |
992 | } |
993 | |
994 | void raw_null_ostream::write_impl(const char *Ptr, size_t Size) { |
995 | } |
996 | |
997 | uint64_t raw_null_ostream::current_pos() const { |
998 | return 0; |
999 | } |
1000 | |
1001 | void raw_null_ostream::pwrite_impl(const char *Ptr, size_t Size, |
1002 | uint64_t Offset) {} |
1003 | |
1004 | void raw_pwrite_stream::anchor() {} |
1005 | |
1006 | void buffer_ostream::anchor() {} |
1007 | |
1008 | void buffer_unique_ostream::anchor() {} |
1009 | |
1010 | Error llvm::writeToOutput(StringRef OutputFileName, |
1011 | std::function<Error(raw_ostream &)> Write) { |
1012 | if (OutputFileName == "-" ) |
1013 | return Write(outs()); |
1014 | |
1015 | if (OutputFileName == "/dev/null" ) { |
1016 | raw_null_ostream Out; |
1017 | return Write(Out); |
1018 | } |
1019 | |
1020 | unsigned Mode = sys::fs::all_read | sys::fs::all_write; |
1021 | Expected<sys::fs::TempFile> Temp = |
1022 | sys::fs::TempFile::create(Model: OutputFileName + ".temp-stream-%%%%%%" , Mode); |
1023 | if (!Temp) |
1024 | return createFileError(F: OutputFileName, E: Temp.takeError()); |
1025 | |
1026 | raw_fd_ostream Out(Temp->FD, false); |
1027 | |
1028 | if (Error E = Write(Out)) { |
1029 | if (Error DiscardError = Temp->discard()) |
1030 | return joinErrors(E1: std::move(E), E2: std::move(DiscardError)); |
1031 | return E; |
1032 | } |
1033 | Out.flush(); |
1034 | |
1035 | return Temp->keep(Name: OutputFileName); |
1036 | } |
1037 | |