raw_ostream.cpp revision 218893
1//===--- raw_ostream.cpp - Implement the raw_ostream classes --------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements support for bulk buffered stream output.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/raw_ostream.h"
15#include "llvm/Support/Format.h"
16#include "llvm/Support/Program.h"
17#include "llvm/Support/Process.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/Config/config.h"
21#include "llvm/Support/Compiler.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/ADT/STLExtras.h"
24#include <cctype>
25#include <cerrno>
26#include <sys/stat.h>
27#include <sys/types.h>
28
29#if defined(HAVE_UNISTD_H)
30# include <unistd.h>
31#endif
32#if defined(HAVE_FCNTL_H)
33# include <fcntl.h>
34#endif
35#if defined(HAVE_SYS_UIO_H) && defined(HAVE_WRITEV)
36#  include <sys/uio.h>
37#endif
38
39#if defined(__CYGWIN__)
40#include <io.h>
41#endif
42
43#if defined(_MSC_VER)
44#include <io.h>
45#include <fcntl.h>
46#ifndef STDIN_FILENO
47# define STDIN_FILENO 0
48#endif
49#ifndef STDOUT_FILENO
50# define STDOUT_FILENO 1
51#endif
52#ifndef STDERR_FILENO
53# define STDERR_FILENO 2
54#endif
55#endif
56
57using namespace llvm;
58
59raw_ostream::~raw_ostream() {
60  // raw_ostream's subclasses should take care to flush the buffer
61  // in their destructors.
62  assert(OutBufCur == OutBufStart &&
63         "raw_ostream destructor called with non-empty buffer!");
64
65  if (BufferMode == InternalBuffer)
66    delete [] OutBufStart;
67}
68
69// An out of line virtual method to provide a home for the class vtable.
70void raw_ostream::handle() {}
71
72size_t raw_ostream::preferred_buffer_size() const {
73  // BUFSIZ is intended to be a reasonable default.
74  return BUFSIZ;
75}
76
77void raw_ostream::SetBuffered() {
78  // Ask the subclass to determine an appropriate buffer size.
79  if (size_t Size = preferred_buffer_size())
80    SetBufferSize(Size);
81  else
82    // It may return 0, meaning this stream should be unbuffered.
83    SetUnbuffered();
84}
85
86void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size,
87                                    BufferKind Mode) {
88  assert(((Mode == Unbuffered && BufferStart == 0 && Size == 0) ||
89          (Mode != Unbuffered && BufferStart && Size)) &&
90         "stream must be unbuffered or have at least one byte");
91  // Make sure the current buffer is free of content (we can't flush here; the
92  // child buffer management logic will be in write_impl).
93  assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!");
94
95  if (BufferMode == InternalBuffer)
96    delete [] OutBufStart;
97  OutBufStart = BufferStart;
98  OutBufEnd = OutBufStart+Size;
99  OutBufCur = OutBufStart;
100  BufferMode = Mode;
101
102  assert(OutBufStart <= OutBufEnd && "Invalid size!");
103}
104
105raw_ostream &raw_ostream::operator<<(unsigned long N) {
106  // Zero is a special case.
107  if (N == 0)
108    return *this << '0';
109
110  char NumberBuffer[20];
111  char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
112  char *CurPtr = EndPtr;
113
114  while (N) {
115    *--CurPtr = '0' + char(N % 10);
116    N /= 10;
117  }
118  return write(CurPtr, EndPtr-CurPtr);
119}
120
121raw_ostream &raw_ostream::operator<<(long N) {
122  if (N <  0) {
123    *this << '-';
124    N = -N;
125  }
126
127  return this->operator<<(static_cast<unsigned long>(N));
128}
129
130raw_ostream &raw_ostream::operator<<(unsigned long long N) {
131  // Output using 32-bit div/mod when possible.
132  if (N == static_cast<unsigned long>(N))
133    return this->operator<<(static_cast<unsigned long>(N));
134
135  char NumberBuffer[20];
136  char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
137  char *CurPtr = EndPtr;
138
139  while (N) {
140    *--CurPtr = '0' + char(N % 10);
141    N /= 10;
142  }
143  return write(CurPtr, EndPtr-CurPtr);
144}
145
146raw_ostream &raw_ostream::operator<<(long long N) {
147  if (N < 0) {
148    *this << '-';
149    // Avoid undefined behavior on INT64_MIN with a cast.
150    N = -(unsigned long long)N;
151  }
152
153  return this->operator<<(static_cast<unsigned long long>(N));
154}
155
156raw_ostream &raw_ostream::write_hex(unsigned long long N) {
157  // Zero is a special case.
158  if (N == 0)
159    return *this << '0';
160
161  char NumberBuffer[20];
162  char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
163  char *CurPtr = EndPtr;
164
165  while (N) {
166    uintptr_t x = N % 16;
167    *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10);
168    N /= 16;
169  }
170
171  return write(CurPtr, EndPtr-CurPtr);
172}
173
174raw_ostream &raw_ostream::write_escaped(StringRef Str,
175                                        bool UseHexEscapes) {
176  for (unsigned i = 0, e = Str.size(); i != e; ++i) {
177    unsigned char c = Str[i];
178
179    switch (c) {
180    case '\\':
181      *this << '\\' << '\\';
182      break;
183    case '\t':
184      *this << '\\' << 't';
185      break;
186    case '\n':
187      *this << '\\' << 'n';
188      break;
189    case '"':
190      *this << '\\' << '"';
191      break;
192    default:
193      if (std::isprint(c)) {
194        *this << c;
195        break;
196      }
197
198      // Write out the escaped representation.
199      if (UseHexEscapes) {
200        *this << '\\' << 'x';
201        *this << hexdigit((c >> 4 & 0xF));
202        *this << hexdigit((c >> 0) & 0xF);
203      } else {
204        // Always use a full 3-character octal escape.
205        *this << '\\';
206        *this << char('0' + ((c >> 6) & 7));
207        *this << char('0' + ((c >> 3) & 7));
208        *this << char('0' + ((c >> 0) & 7));
209      }
210    }
211  }
212
213  return *this;
214}
215
216raw_ostream &raw_ostream::operator<<(const void *P) {
217  *this << '0' << 'x';
218
219  return write_hex((uintptr_t) P);
220}
221
222raw_ostream &raw_ostream::operator<<(double N) {
223  return this->operator<<(format("%e", N));
224}
225
226
227
228void raw_ostream::flush_nonempty() {
229  assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty.");
230  size_t Length = OutBufCur - OutBufStart;
231  OutBufCur = OutBufStart;
232  write_impl(OutBufStart, Length);
233}
234
235raw_ostream &raw_ostream::write(unsigned char C) {
236  // Group exceptional cases into a single branch.
237  if (BUILTIN_EXPECT(OutBufCur >= OutBufEnd, false)) {
238    if (BUILTIN_EXPECT(!OutBufStart, false)) {
239      if (BufferMode == Unbuffered) {
240        write_impl(reinterpret_cast<char*>(&C), 1);
241        return *this;
242      }
243      // Set up a buffer and start over.
244      SetBuffered();
245      return write(C);
246    }
247
248    flush_nonempty();
249  }
250
251  *OutBufCur++ = C;
252  return *this;
253}
254
255raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
256  // Group exceptional cases into a single branch.
257  if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) {
258    if (BUILTIN_EXPECT(!OutBufStart, false)) {
259      if (BufferMode == Unbuffered) {
260        write_impl(Ptr, Size);
261        return *this;
262      }
263      // Set up a buffer and start over.
264      SetBuffered();
265      return write(Ptr, Size);
266    }
267
268    // Write out the data in buffer-sized blocks until the remainder
269    // fits within the buffer.
270    do {
271      size_t NumBytes = OutBufEnd - OutBufCur;
272      copy_to_buffer(Ptr, NumBytes);
273      flush_nonempty();
274      Ptr += NumBytes;
275      Size -= NumBytes;
276    } while (OutBufCur+Size > OutBufEnd);
277  }
278
279  copy_to_buffer(Ptr, Size);
280
281  return *this;
282}
283
284void raw_ostream::copy_to_buffer(const char *Ptr, size_t Size) {
285  assert(Size <= size_t(OutBufEnd - OutBufCur) && "Buffer overrun!");
286
287  // Handle short strings specially, memcpy isn't very good at very short
288  // strings.
289  switch (Size) {
290  case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
291  case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
292  case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
293  case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
294  case 0: break;
295  default:
296    memcpy(OutBufCur, Ptr, Size);
297    break;
298  }
299
300  OutBufCur += Size;
301}
302
303// Formatted output.
304raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
305  // If we have more than a few bytes left in our output buffer, try
306  // formatting directly onto its end.
307  size_t NextBufferSize = 127;
308  size_t BufferBytesLeft = OutBufEnd - OutBufCur;
309  if (BufferBytesLeft > 3) {
310    size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
311
312    // Common case is that we have plenty of space.
313    if (BytesUsed <= BufferBytesLeft) {
314      OutBufCur += BytesUsed;
315      return *this;
316    }
317
318    // Otherwise, we overflowed and the return value tells us the size to try
319    // again with.
320    NextBufferSize = BytesUsed;
321  }
322
323  // If we got here, we didn't have enough space in the output buffer for the
324  // string.  Try printing into a SmallVector that is resized to have enough
325  // space.  Iterate until we win.
326  SmallVector<char, 128> V;
327
328  while (1) {
329    V.resize(NextBufferSize);
330
331    // Try formatting into the SmallVector.
332    size_t BytesUsed = Fmt.print(V.data(), NextBufferSize);
333
334    // If BytesUsed fit into the vector, we win.
335    if (BytesUsed <= NextBufferSize)
336      return write(V.data(), BytesUsed);
337
338    // Otherwise, try again with a new size.
339    assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
340    NextBufferSize = BytesUsed;
341  }
342}
343
344/// indent - Insert 'NumSpaces' spaces.
345raw_ostream &raw_ostream::indent(unsigned NumSpaces) {
346  static const char Spaces[] = "                                "
347                               "                                "
348                               "                ";
349
350  // Usually the indentation is small, handle it with a fastpath.
351  if (NumSpaces < array_lengthof(Spaces))
352    return write(Spaces, NumSpaces);
353
354  while (NumSpaces) {
355    unsigned NumToWrite = std::min(NumSpaces,
356                                   (unsigned)array_lengthof(Spaces)-1);
357    write(Spaces, NumToWrite);
358    NumSpaces -= NumToWrite;
359  }
360  return *this;
361}
362
363
364//===----------------------------------------------------------------------===//
365//  Formatted Output
366//===----------------------------------------------------------------------===//
367
368// Out of line virtual method.
369void format_object_base::home() {
370}
371
372//===----------------------------------------------------------------------===//
373//  raw_fd_ostream
374//===----------------------------------------------------------------------===//
375
376/// raw_fd_ostream - Open the specified file for writing. If an error
377/// occurs, information about the error is put into ErrorInfo, and the
378/// stream should be immediately destroyed; the string will be empty
379/// if no error occurred.
380raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
381                               unsigned Flags)
382  : Error(false), UseAtomicWrites(false), pos(0)
383{
384  assert(Filename != 0 && "Filename is null");
385  // Verify that we don't have both "append" and "excl".
386  assert((!(Flags & F_Excl) || !(Flags & F_Append)) &&
387         "Cannot specify both 'excl' and 'append' file creation flags!");
388
389  ErrorInfo.clear();
390
391  // Handle "-" as stdout. Note that when we do this, we consider ourself
392  // the owner of stdout. This means that we can do things like close the
393  // file descriptor when we're done and set the "binary" flag globally.
394  if (Filename[0] == '-' && Filename[1] == 0) {
395    FD = STDOUT_FILENO;
396    // If user requested binary then put stdout into binary mode if
397    // possible.
398    if (Flags & F_Binary)
399      sys::Program::ChangeStdoutToBinary();
400    // Close stdout when we're done, to detect any output errors.
401    ShouldClose = true;
402    return;
403  }
404
405  int OpenFlags = O_WRONLY|O_CREAT;
406#ifdef O_BINARY
407  if (Flags & F_Binary)
408    OpenFlags |= O_BINARY;
409#endif
410
411  if (Flags & F_Append)
412    OpenFlags |= O_APPEND;
413  else
414    OpenFlags |= O_TRUNC;
415  if (Flags & F_Excl)
416    OpenFlags |= O_EXCL;
417
418  while ((FD = open(Filename, OpenFlags, 0664)) < 0) {
419    if (errno != EINTR) {
420      ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
421      ShouldClose = false;
422      return;
423    }
424  }
425
426  // Ok, we successfully opened the file, so it'll need to be closed.
427  ShouldClose = true;
428}
429
430/// raw_fd_ostream ctor - FD is the file descriptor that this writes to.  If
431/// ShouldClose is true, this closes the file when the stream is destroyed.
432raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered)
433  : raw_ostream(unbuffered), FD(fd),
434    ShouldClose(shouldClose), Error(false), UseAtomicWrites(false) {
435#ifdef O_BINARY
436  // Setting STDOUT and STDERR to binary mode is necessary in Win32
437  // to avoid undesirable linefeed conversion.
438  if (fd == STDOUT_FILENO || fd == STDERR_FILENO)
439    setmode(fd, O_BINARY);
440#endif
441
442  // Get the starting position.
443  off_t loc = ::lseek(FD, 0, SEEK_CUR);
444  if (loc == (off_t)-1)
445    pos = 0;
446  else
447    pos = static_cast<uint64_t>(loc);
448}
449
450raw_fd_ostream::~raw_fd_ostream() {
451  if (FD >= 0) {
452    flush();
453    if (ShouldClose)
454      while (::close(FD) != 0)
455        if (errno != EINTR) {
456          error_detected();
457          break;
458        }
459  }
460
461  // If there are any pending errors, report them now. Clients wishing
462  // to avoid report_fatal_error calls should check for errors with
463  // has_error() and clear the error flag with clear_error() before
464  // destructing raw_ostream objects which may have errors.
465  if (has_error())
466    report_fatal_error("IO failure on output stream.");
467}
468
469
470void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
471  assert(FD >= 0 && "File already closed.");
472  pos += Size;
473
474  do {
475    ssize_t ret;
476
477    // Check whether we should attempt to use atomic writes.
478    if (BUILTIN_EXPECT(!UseAtomicWrites, true)) {
479      ret = ::write(FD, Ptr, Size);
480    } else {
481      // Use ::writev() where available.
482#if defined(HAVE_WRITEV)
483      struct iovec IOV = { (void*) Ptr, Size };
484      ret = ::writev(FD, &IOV, 1);
485#else
486      ret = ::write(FD, Ptr, Size);
487#endif
488    }
489
490    if (ret < 0) {
491      // If it's a recoverable error, swallow it and retry the write.
492      //
493      // Ideally we wouldn't ever see EAGAIN or EWOULDBLOCK here, since
494      // raw_ostream isn't designed to do non-blocking I/O. However, some
495      // programs, such as old versions of bjam, have mistakenly used
496      // O_NONBLOCK. For compatibility, emulate blocking semantics by
497      // spinning until the write succeeds. If you don't want spinning,
498      // don't use O_NONBLOCK file descriptors with raw_ostream.
499      if (errno == EINTR || errno == EAGAIN
500#ifdef EWOULDBLOCK
501          || errno == EWOULDBLOCK
502#endif
503          )
504        continue;
505
506      // Otherwise it's a non-recoverable error. Note it and quit.
507      error_detected();
508      break;
509    }
510
511    // The write may have written some or all of the data. Update the
512    // size and buffer pointer to reflect the remainder that needs
513    // to be written. If there are no bytes left, we're done.
514    Ptr += ret;
515    Size -= ret;
516  } while (Size > 0);
517}
518
519void raw_fd_ostream::close() {
520  assert(ShouldClose);
521  ShouldClose = false;
522  flush();
523  while (::close(FD) != 0)
524    if (errno != EINTR) {
525      error_detected();
526      break;
527    }
528  FD = -1;
529}
530
531uint64_t raw_fd_ostream::seek(uint64_t off) {
532  flush();
533  pos = ::lseek(FD, off, SEEK_SET);
534  if (pos != off)
535    error_detected();
536  return pos;
537}
538
539size_t raw_fd_ostream::preferred_buffer_size() const {
540#if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__minix)
541  // Windows and Minix have no st_blksize.
542  assert(FD >= 0 && "File not yet open!");
543  struct stat statbuf;
544  if (fstat(FD, &statbuf) != 0)
545    return 0;
546
547  // If this is a terminal, don't use buffering. Line buffering
548  // would be a more traditional thing to do, but it's not worth
549  // the complexity.
550  if (S_ISCHR(statbuf.st_mode) && isatty(FD))
551    return 0;
552  // Return the preferred block size.
553  return statbuf.st_blksize;
554#else
555  return raw_ostream::preferred_buffer_size();
556#endif
557}
558
559raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold,
560                                         bool bg) {
561  if (sys::Process::ColorNeedsFlush())
562    flush();
563  const char *colorcode =
564    (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg)
565    : sys::Process::OutputColor(colors, bold, bg);
566  if (colorcode) {
567    size_t len = strlen(colorcode);
568    write(colorcode, len);
569    // don't account colors towards output characters
570    pos -= len;
571  }
572  return *this;
573}
574
575raw_ostream &raw_fd_ostream::resetColor() {
576  if (sys::Process::ColorNeedsFlush())
577    flush();
578  const char *colorcode = sys::Process::ResetColor();
579  if (colorcode) {
580    size_t len = strlen(colorcode);
581    write(colorcode, len);
582    // don't account colors towards output characters
583    pos -= len;
584  }
585  return *this;
586}
587
588bool raw_fd_ostream::is_displayed() const {
589  return sys::Process::FileDescriptorIsDisplayed(FD);
590}
591
592//===----------------------------------------------------------------------===//
593//  outs(), errs(), nulls()
594//===----------------------------------------------------------------------===//
595
596/// outs() - This returns a reference to a raw_ostream for standard output.
597/// Use it like: outs() << "foo" << "bar";
598raw_ostream &llvm::outs() {
599  // Set buffer settings to model stdout behavior.
600  // Delete the file descriptor when the program exists, forcing error
601  // detection. If you don't want this behavior, don't use outs().
602  static raw_fd_ostream S(STDOUT_FILENO, true);
603  return S;
604}
605
606/// errs() - This returns a reference to a raw_ostream for standard error.
607/// Use it like: errs() << "foo" << "bar";
608raw_ostream &llvm::errs() {
609  // Set standard error to be unbuffered by default.
610  static raw_fd_ostream S(STDERR_FILENO, false, true);
611  return S;
612}
613
614/// nulls() - This returns a reference to a raw_ostream which discards output.
615raw_ostream &llvm::nulls() {
616  static raw_null_ostream S;
617  return S;
618}
619
620
621//===----------------------------------------------------------------------===//
622//  raw_string_ostream
623//===----------------------------------------------------------------------===//
624
625raw_string_ostream::~raw_string_ostream() {
626  flush();
627}
628
629void raw_string_ostream::write_impl(const char *Ptr, size_t Size) {
630  OS.append(Ptr, Size);
631}
632
633//===----------------------------------------------------------------------===//
634//  raw_svector_ostream
635//===----------------------------------------------------------------------===//
636
637// The raw_svector_ostream implementation uses the SmallVector itself as the
638// buffer for the raw_ostream. We guarantee that the raw_ostream buffer is
639// always pointing past the end of the vector, but within the vector
640// capacity. This allows raw_ostream to write directly into the correct place,
641// and we only need to set the vector size when the data is flushed.
642
643raw_svector_ostream::raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {
644  // Set up the initial external buffer. We make sure that the buffer has at
645  // least 128 bytes free; raw_ostream itself only requires 64, but we want to
646  // make sure that we don't grow the buffer unnecessarily on destruction (when
647  // the data is flushed). See the FIXME below.
648  OS.reserve(OS.size() + 128);
649  SetBuffer(OS.end(), OS.capacity() - OS.size());
650}
651
652raw_svector_ostream::~raw_svector_ostream() {
653  // FIXME: Prevent resizing during this flush().
654  flush();
655}
656
657/// resync - This is called when the SmallVector we're appending to is changed
658/// outside of the raw_svector_ostream's control.  It is only safe to do this
659/// if the raw_svector_ostream has previously been flushed.
660void raw_svector_ostream::resync() {
661  assert(GetNumBytesInBuffer() == 0 && "Didn't flush before mutating vector");
662
663  if (OS.capacity() - OS.size() < 64)
664    OS.reserve(OS.capacity() * 2);
665  SetBuffer(OS.end(), OS.capacity() - OS.size());
666}
667
668void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) {
669  // If we're writing bytes from the end of the buffer into the smallvector, we
670  // don't need to copy the bytes, just commit the bytes because they are
671  // already in the right place.
672  if (Ptr == OS.end()) {
673    assert(OS.size() + Size <= OS.capacity() && "Invalid write_impl() call!");
674    OS.set_size(OS.size() + Size);
675  } else {
676    assert(GetNumBytesInBuffer() == 0 &&
677           "Should be writing from buffer if some bytes in it");
678    // Otherwise, do copy the bytes.
679    OS.append(Ptr, Ptr+Size);
680  }
681
682  // Grow the vector if necessary.
683  if (OS.capacity() - OS.size() < 64)
684    OS.reserve(OS.capacity() * 2);
685
686  // Update the buffer position.
687  SetBuffer(OS.end(), OS.capacity() - OS.size());
688}
689
690uint64_t raw_svector_ostream::current_pos() const {
691   return OS.size();
692}
693
694StringRef raw_svector_ostream::str() {
695  flush();
696  return StringRef(OS.begin(), OS.size());
697}
698
699//===----------------------------------------------------------------------===//
700//  raw_null_ostream
701//===----------------------------------------------------------------------===//
702
703raw_null_ostream::~raw_null_ostream() {
704#ifndef NDEBUG
705  // ~raw_ostream asserts that the buffer is empty. This isn't necessary
706  // with raw_null_ostream, but it's better to have raw_null_ostream follow
707  // the rules than to change the rules just for raw_null_ostream.
708  flush();
709#endif
710}
711
712void raw_null_ostream::write_impl(const char *Ptr, size_t Size) {
713}
714
715uint64_t raw_null_ostream::current_pos() const {
716  return 0;
717}
718