1
//===--- raw_ostream.cpp - Implement the raw_ostream classes --------------===//
3
// The LLVM Compiler Infrastructure
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
10
// This implements support for bulk buffered stream output.
12
//===----------------------------------------------------------------------===//
14
#include "llvm/Support/raw_ostream.h"
15
#include "llvm/Support/Format.h"
16
#include "llvm/System/Program.h"
17
#include "llvm/System/Process.h"
18
#include "llvm/ADT/SmallVector.h"
19
#include "llvm/Config/config.h"
20
#include "llvm/Support/Compiler.h"
21
#include "llvm/Support/ErrorHandling.h"
22
#include "llvm/System/Signals.h"
23
#include "llvm/ADT/STLExtras.h"
27
#include <sys/types.h>
29
#if defined(HAVE_UNISTD_H)
32
#if defined(HAVE_FCNTL_H)
40
# define STDIN_FILENO 0
43
# define STDOUT_FILENO 1
46
# define STDERR_FILENO 2
52
raw_ostream::~raw_ostream() {
53
// raw_ostream's subclasses should take care to flush the buffer
54
// in their destructors.
55
assert(OutBufCur == OutBufStart &&
56
"raw_ostream destructor called with non-empty buffer!");
58
if (BufferMode == InternalBuffer)
59
delete [] OutBufStart;
62
// An out of line virtual method to provide a home for the class vtable.
63
void raw_ostream::handle() {}
65
size_t raw_ostream::preferred_buffer_size() const {
66
// BUFSIZ is intended to be a reasonable default.
70
void raw_ostream::SetBuffered() {
71
// Ask the subclass to determine an appropriate buffer size.
72
if (size_t Size = preferred_buffer_size())
75
// It may return 0, meaning this stream should be unbuffered.
79
void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size,
81
assert(((Mode == Unbuffered && BufferStart == 0 && Size == 0) ||
82
(Mode != Unbuffered && BufferStart && Size)) &&
83
"stream must be unbuffered or have at least one byte");
84
// Make sure the current buffer is free of content (we can't flush here; the
85
// child buffer management logic will be in write_impl).
86
assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!");
88
if (BufferMode == InternalBuffer)
89
delete [] OutBufStart;
90
OutBufStart = BufferStart;
91
OutBufEnd = OutBufStart+Size;
92
OutBufCur = OutBufStart;
95
assert(OutBufStart <= OutBufEnd && "Invalid size!");
98
raw_ostream &raw_ostream::operator<<(unsigned long N) {
99
// Zero is a special case.
103
char NumberBuffer[20];
104
char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
105
char *CurPtr = EndPtr;
108
*--CurPtr = '0' + char(N % 10);
111
return write(CurPtr, EndPtr-CurPtr);
114
raw_ostream &raw_ostream::operator<<(long N) {
120
return this->operator<<(static_cast<unsigned long>(N));
123
raw_ostream &raw_ostream::operator<<(unsigned long long N) {
124
// Output using 32-bit div/mod when possible.
125
if (N == static_cast<unsigned long>(N))
126
return this->operator<<(static_cast<unsigned long>(N));
128
char NumberBuffer[20];
129
char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
130
char *CurPtr = EndPtr;
133
*--CurPtr = '0' + char(N % 10);
136
return write(CurPtr, EndPtr-CurPtr);
139
raw_ostream &raw_ostream::operator<<(long long N) {
142
// Avoid undefined behavior on INT64_MIN with a cast.
143
N = -(unsigned long long)N;
146
return this->operator<<(static_cast<unsigned long long>(N));
149
raw_ostream &raw_ostream::write_hex(unsigned long long N) {
150
// Zero is a special case.
154
char NumberBuffer[20];
155
char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
156
char *CurPtr = EndPtr;
159
uintptr_t x = N % 16;
160
*--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10);
164
return write(CurPtr, EndPtr-CurPtr);
167
raw_ostream &raw_ostream::write_escaped(StringRef Str) {
168
for (unsigned i = 0, e = Str.size(); i != e; ++i) {
169
unsigned char c = Str[i];
173
*this << '\\' << '\\';
176
*this << '\\' << 't';
179
*this << '\\' << 'n';
182
*this << '\\' << '"';
185
if (std::isprint(c)) {
190
// Always expand to a 3-character octal escape.
192
*this << char('0' + ((c >> 6) & 7));
193
*this << char('0' + ((c >> 3) & 7));
194
*this << char('0' + ((c >> 0) & 7));
201
raw_ostream &raw_ostream::operator<<(const void *P) {
204
return write_hex((uintptr_t) P);
207
raw_ostream &raw_ostream::operator<<(double N) {
208
return this->operator<<(format("%e", N));
213
void raw_ostream::flush_nonempty() {
214
assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
215
size_t Length = OutBufCur - OutBufStart;
216
OutBufCur = OutBufStart;
217
write_impl(OutBufStart, Length);
220
raw_ostream &raw_ostream::write(unsigned char C) {
221
// Group exceptional cases into a single branch.
222
if (BUILTIN_EXPECT(OutBufCur >= OutBufEnd, false)) {
223
if (BUILTIN_EXPECT(!OutBufStart, false)) {
224
if (BufferMode == Unbuffered) {
225
write_impl(reinterpret_cast<char*>(&C), 1);
228
// Set up a buffer and start over.
240
raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
241
// Group exceptional cases into a single branch.
242
if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) {
243
if (BUILTIN_EXPECT(!OutBufStart, false)) {
244
if (BufferMode == Unbuffered) {
245
write_impl(Ptr, Size);
248
// Set up a buffer and start over.
250
return write(Ptr, Size);
253
// Write out the data in buffer-sized blocks until the remainder
254
// fits within the buffer.
256
size_t NumBytes = OutBufEnd - OutBufCur;
257
copy_to_buffer(Ptr, NumBytes);
261
} while (OutBufCur+Size > OutBufEnd);
264
copy_to_buffer(Ptr, Size);
269
void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) {
270
assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!");
272
// Handle short strings specially, memcpy isn't very good at very short
275
case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
276
case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
277
case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
278
case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
281
memcpy(OutBufCur, Ptr, Size);
289
raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
290
// If we have more than a few bytes left in our output buffer, try
291
// formatting directly onto its end.
292
size_t NextBufferSize = 127;
293
size_t BufferBytesLeft = OutBufEnd - OutBufCur;
294
if (BufferBytesLeft > 3) {
295
size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
297
// Common case is that we have plenty of space.
298
if (BytesUsed <= BufferBytesLeft) {
299
OutBufCur += BytesUsed;
303
// Otherwise, we overflowed and the return value tells us the size to try
305
NextBufferSize = BytesUsed;
308
// If we got here, we didn't have enough space in the output buffer for the
309
// string. Try printing into a SmallVector that is resized to have enough
310
// space. Iterate until we win.
311
SmallVector<char, 128> V;
314
V.resize(NextBufferSize);
316
// Try formatting into the SmallVector.
317
size_t BytesUsed = Fmt.print(V.data(), NextBufferSize);
319
// If BytesUsed fit into the vector, we win.
320
if (BytesUsed <= NextBufferSize)
321
return write(V.data(), BytesUsed);
323
// Otherwise, try again with a new size.
324
assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
325
NextBufferSize = BytesUsed;
329
/// indent - Insert 'NumSpaces' spaces.
330
raw_ostream &raw_ostream::indent(unsigned NumSpaces) {
331
static const char Spaces[] = " "
335
// Usually the indentation is small, handle it with a fastpath.
336
if (NumSpaces < array_lengthof(Spaces))
337
return write(Spaces, NumSpaces);
340
unsigned NumToWrite = std::min(NumSpaces,
341
(unsigned)array_lengthof(Spaces)-1);
342
write(Spaces, NumToWrite);
343
NumSpaces -= NumToWrite;
349
//===----------------------------------------------------------------------===//
351
//===----------------------------------------------------------------------===//
353
// Out of line virtual method.
354
void format_object_base::home() {
357
//===----------------------------------------------------------------------===//
359
//===----------------------------------------------------------------------===//
361
/// raw_fd_ostream - Open the specified file for writing. If an error
362
/// occurs, information about the error is put into ErrorInfo, and the
363
/// stream should be immediately destroyed; the string will be empty
364
/// if no error occurred.
365
raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
366
unsigned Flags) : Error(false), pos(0) {
367
assert(Filename != 0 && "Filename is null");
368
// Verify that we don't have both "append" and "excl".
369
assert((!(Flags & F_Excl) || !(Flags & F_Append)) &&
370
"Cannot specify both 'excl' and 'append' file creation flags!");
374
// Handle "-" as stdout. Note that when we do this, we consider ourself
375
// the owner of stdout. This means that we can do things like close the
376
// file descriptor when we're done and set the "binary" flag globally.
377
if (Filename[0] == '-' && Filename[1] == 0) {
379
// If user requested binary then put stdout into binary mode if
381
if (Flags & F_Binary)
382
sys::Program::ChangeStdoutToBinary();
383
// Close stdout when we're done, to detect any output errors.
388
int OpenFlags = O_WRONLY|O_CREAT;
390
if (Flags & F_Binary)
391
OpenFlags |= O_BINARY;
394
if (Flags & F_Append)
395
OpenFlags |= O_APPEND;
397
OpenFlags |= O_TRUNC;
401
while ((FD = open(Filename, OpenFlags, 0664)) < 0) {
402
if (errno != EINTR) {
403
ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
409
// Ok, we successfully opened the file, so it'll need to be closed.
413
raw_fd_ostream::~raw_fd_ostream() {
417
while (::close(FD) != 0)
418
if (errno != EINTR) {
424
// If there are any pending errors, report them now. Clients wishing
425
// to avoid report_fatal_error calls should check for errors with
426
// has_error() and clear the error flag with clear_error() before
427
// destructing raw_ostream objects which may have errors.
429
report_fatal_error("IO failure on output stream.");
433
void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
434
assert(FD >= 0 && "File already closed.");
438
ssize_t ret = ::write(FD, Ptr, Size);
441
// If it's a recoverable error, swallow it and retry the write.
443
// Ideally we wouldn't ever see EAGAIN or EWOULDBLOCK here, since
444
// raw_ostream isn't designed to do non-blocking I/O. However, some
445
// programs, such as old versions of bjam, have mistakenly used
446
// O_NONBLOCK. For compatibility, emulate blocking semantics by
447
// spinning until the write succeeds. If you don't want spinning,
448
// don't use O_NONBLOCK file descriptors with raw_ostream.
449
if (errno == EINTR || errno == EAGAIN
451
|| errno == EWOULDBLOCK
456
// Otherwise it's a non-recoverable error. Note it and quit.
461
// The write may have written some or all of the data. Update the
462
// size and buffer pointer to reflect the remainder that needs
463
// to be written. If there are no bytes left, we're done.
469
void raw_fd_ostream::close() {
473
while (::close(FD) != 0)
474
if (errno != EINTR) {
481
uint64_t raw_fd_ostream::seek(uint64_t off) {
483
pos = ::lseek(FD, off, SEEK_SET);
489
size_t raw_fd_ostream::preferred_buffer_size() const {
490
#if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__minix)
491
// Windows and Minix have no st_blksize.
492
assert(FD >= 0 && "File not yet open!");
494
if (fstat(FD, &statbuf) != 0)
497
// If this is a terminal, don't use buffering. Line buffering
498
// would be a more traditional thing to do, but it's not worth
500
if (S_ISCHR(statbuf.st_mode) && isatty(FD))
502
// Return the preferred block size.
503
return statbuf.st_blksize;
505
return raw_ostream::preferred_buffer_size();
509
raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
511
if (sys::Process::ColorNeedsFlush())
513
const char *colorcode =
514
(colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
515
: sys::Process::OutputColor(colors, bold, bg);
517
size_t len = strlen(colorcode);
518
write(colorcode, len);
519
// don't account colors towards output characters
525
raw_ostream &raw_fd_ostream::resetColor() {
526
if (sys::Process::ColorNeedsFlush())
528
const char *colorcode = sys::Process::ResetColor();
530
size_t len = strlen(colorcode);
531
write(colorcode, len);
532
// don't account colors towards output characters
538
bool raw_fd_ostream::is_displayed() const {
539
return sys::Process::FileDescriptorIsDisplayed(FD);
542
//===----------------------------------------------------------------------===//
543
// outs(), errs(), nulls()
544
//===----------------------------------------------------------------------===//
546
/// outs() - This returns a reference to a raw_ostream for standard output.
547
/// Use it like: outs() << "foo" << "bar";
548
raw_ostream &llvm::outs() {
549
// Set buffer settings to model stdout behavior.
550
// Delete the file descriptor when the program exists, forcing error
551
// detection. If you don't want this behavior, don't use outs().
552
static raw_fd_ostream S(STDOUT_FILENO, true);
556
/// errs() - This returns a reference to a raw_ostream for standard error.
557
/// Use it like: errs() << "foo" << "bar";
558
raw_ostream &llvm::errs() {
559
// Set standard error to be unbuffered by default.
560
static raw_fd_ostream S(STDERR_FILENO, false, true);
564
/// nulls() - This returns a reference to a raw_ostream which discards output.
565
raw_ostream &llvm::nulls() {
566
static raw_null_ostream S;
571
//===----------------------------------------------------------------------===//
572
// raw_string_ostream
573
//===----------------------------------------------------------------------===//
575
raw_string_ostream::~raw_string_ostream() {
579
void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
580
OS.append(Ptr, Size);
583
//===----------------------------------------------------------------------===//
584
// raw_svector_ostream
585
//===----------------------------------------------------------------------===//
587
// The raw_svector_ostream implementation uses the SmallVector itself as the
588
// buffer for the raw_ostream. We guarantee that the raw_ostream buffer is
589
// always pointing past the end of the vector, but within the vector
590
// capacity. This allows raw_ostream to write directly into the correct place,
591
// and we only need to set the vector size when the data is flushed.
593
raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {
594
// Set up the initial external buffer. We make sure that the buffer has at
595
// least 128 bytes free; raw_ostream itself only requires 64, but we want to
596
// make sure that we don't grow the buffer unnecessarily on destruction (when
597
// the data is flushed). See the FIXME below.
598
OS.reserve(OS.size() + 128);
599
SetBuffer(OS.end(), OS.capacity() - OS.size());
602
raw_svector_ostream::~raw_svector_ostream() {
603
// FIXME: Prevent resizing during this flush().
607
/// resync - This is called when the SmallVector we're appending to is changed
608
/// outside of the raw_svector_ostream's control. It is only safe to do this
609
/// if the raw_svector_ostream has previously been flushed.
610
void raw_svector_ostream::resync() {
611
assert(GetNumBytesInBuffer() == 0 && "Didn't flush before mutating vector");
613
if (OS.capacity() - OS.size() < 64)
614
OS.reserve(OS.capacity() * 2);
615
SetBuffer(OS.end(), OS.capacity() - OS.size());
618
void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
619
// If we're writing bytes from the end of the buffer into the smallvector, we
620
// don't need to copy the bytes, just commit the bytes because they are
621
// already in the right place.
622
if (Ptr == OS.end()) {
623
assert(OS.size() + Size <= OS.capacity() && "Invalid write_impl() call!");
624
OS.set_size(OS.size() + Size);
626
assert(GetNumBytesInBuffer() == 0 &&
627
"Should be writing from buffer if some bytes in it");
628
// Otherwise, do copy the bytes.
629
OS.append(Ptr, Ptr+Size);
632
// Grow the vector if necessary.
633
if (OS.capacity() - OS.size() < 64)
634
OS.reserve(OS.capacity() * 2);
636
// Update the buffer position.
637
SetBuffer(OS.end(), OS.capacity() - OS.size());
640
uint64_t raw_svector_ostream::current_pos() const {
644
StringRef raw_svector_ostream::str() {
646
return StringRef(OS.begin(), OS.size());
649
//===----------------------------------------------------------------------===//
651
//===----------------------------------------------------------------------===//
653
raw_null_ostream::~raw_null_ostream() {
655
// ~raw_ostream asserts that the buffer is empty. This isn't necessary
656
// with raw_null_ostream, but it's better to have raw_null_ostream follow
657
// the rules than to change the rules just for raw_null_ostream.
662
void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
665
uint64_t raw_null_ostream::current_pos() const {
669
//===----------------------------------------------------------------------===//
671
//===----------------------------------------------------------------------===//
673
tool_output_file::CleanupInstaller::CleanupInstaller(const char *filename)
674
: Filename(filename), Keep(false) {
675
// Arrange for the file to be deleted if the process is killed.
677
sys::RemoveFileOnSignal(sys::Path(Filename));
680
tool_output_file::CleanupInstaller::~CleanupInstaller() {
681
// Delete the file if the client hasn't told us not to.
682
if (!Keep && Filename != "-")
683
sys::Path(Filename).eraseFromDisk();
685
// Ok, the file is successfully written and closed, or deleted. There's no
686
// further need to clean it up on signals.
688
sys::DontRemoveFileOnSignal(sys::Path(Filename));
691
tool_output_file::tool_output_file(const char *filename, std::string &ErrorInfo,
693
: Installer(filename),
694
OS(filename, ErrorInfo, Flags) {
695
// If open fails, no cleanup is needed.
696
if (!ErrorInfo.empty())
697
Installer.Keep = true;