Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 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" |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame^] | 15 | #include "llvm/Config/config.h" |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 16 | #include <ostream> |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 17 | |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame^] | 18 | #if defined(HAVE_UNISTD_H) |
| 19 | # include <unistd.h> |
| 20 | #endif |
| 21 | #if defined(HAVE_FCNTL_H) |
| 22 | # include <fcntl.h> |
| 23 | #endif |
Argyrios Kyrtzidis | b68dc36 | 2008-08-17 09:25:21 +0000 | [diff] [blame] | 24 | |
| 25 | #if defined(_MSC_VER) |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 26 | #include <io.h> |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 27 | #ifndef STDIN_FILENO |
| 28 | # define STDIN_FILENO 0 |
| 29 | #endif |
| 30 | #ifndef STDOUT_FILENO |
| 31 | # define STDOUT_FILENO 1 |
| 32 | #endif |
| 33 | #ifndef STDERR_FILENO |
| 34 | # define STDERR_FILENO 2 |
| 35 | #endif |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame^] | 38 | using namespace llvm; |
| 39 | |
| 40 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 41 | // An out of line virtual method to provide a home for the class vtable. |
| 42 | void raw_ostream::handle() {} |
| 43 | |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 44 | raw_ostream &raw_ostream::operator<<(unsigned long N) { |
| 45 | // Zero is a special case. |
| 46 | if (N == 0) |
| 47 | return *this << '0'; |
| 48 | |
| 49 | char NumberBuffer[20]; |
| 50 | char *EndPtr = NumberBuffer+sizeof(NumberBuffer); |
| 51 | char *CurPtr = EndPtr; |
| 52 | |
| 53 | while (N) { |
| 54 | *--CurPtr = '0' + char(N % 10); |
| 55 | N /= 10; |
| 56 | } |
| 57 | return write(CurPtr, EndPtr-CurPtr); |
| 58 | } |
| 59 | |
| 60 | raw_ostream &raw_ostream::operator<<(long N) { |
| 61 | if (N < 0) { |
| 62 | if (OutBufCur >= OutBufEnd) |
| 63 | flush_impl(); |
| 64 | *OutBufCur++ = '-'; |
| 65 | |
| 66 | N = -N; |
| 67 | } |
| 68 | |
| 69 | return this->operator<<(static_cast<unsigned long>(N)); |
| 70 | } |
| 71 | |
| 72 | raw_ostream &raw_ostream::operator<<(unsigned long long N) { |
| 73 | // Zero is a special case. |
| 74 | if (N == 0) |
| 75 | return *this << '0'; |
| 76 | |
| 77 | char NumberBuffer[20]; |
| 78 | char *EndPtr = NumberBuffer+sizeof(NumberBuffer); |
| 79 | char *CurPtr = EndPtr; |
| 80 | |
| 81 | while (N) { |
| 82 | *--CurPtr = '0' + char(N % 10); |
| 83 | N /= 10; |
| 84 | } |
| 85 | return write(CurPtr, EndPtr-CurPtr); |
| 86 | } |
| 87 | |
| 88 | raw_ostream &raw_ostream::operator<<(long long N) { |
| 89 | if (N < 0) { |
| 90 | if (OutBufCur >= OutBufEnd) |
| 91 | flush_impl(); |
| 92 | *OutBufCur++ = '-'; |
| 93 | |
| 94 | N = -N; |
| 95 | } |
| 96 | |
| 97 | return this->operator<<(static_cast<unsigned long long>(N)); |
| 98 | } |
| 99 | |
| 100 | raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) { |
| 101 | if (OutBufCur+Size > OutBufEnd) |
| 102 | flush_impl(); |
| 103 | |
| 104 | // Handle short strings specially, memcpy isn't very good at very short |
| 105 | // strings. |
| 106 | switch (Size) { |
| 107 | case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH |
| 108 | case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH |
| 109 | case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH |
| 110 | case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH |
| 111 | case 0: break; |
| 112 | default: |
| 113 | // Normally the string to emit is shorter than the buffer. |
| 114 | if (Size <= unsigned(OutBufEnd-OutBufStart)) { |
| 115 | memcpy(OutBufCur, Ptr, Size); |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | // If emitting a string larger than our buffer, emit in chunks. In this |
| 120 | // case we know that we just flushed the buffer. |
| 121 | while (Size) { |
| 122 | unsigned NumToEmit = OutBufEnd-OutBufStart; |
| 123 | if (Size < NumToEmit) NumToEmit = Size; |
| 124 | assert(OutBufCur == OutBufStart); |
| 125 | memcpy(OutBufStart, Ptr, NumToEmit); |
| 126 | Ptr += NumToEmit; |
Owen Anderson | fd2a053 | 2008-08-21 22:39:33 +0000 | [diff] [blame] | 127 | Size -= NumToEmit; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 128 | OutBufCur = OutBufStart + NumToEmit; |
| 129 | flush_impl(); |
| 130 | } |
| 131 | break; |
| 132 | } |
| 133 | OutBufCur += Size; |
| 134 | return *this; |
| 135 | } |
| 136 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 137 | //===----------------------------------------------------------------------===// |
| 138 | // raw_fd_ostream |
| 139 | //===----------------------------------------------------------------------===// |
| 140 | |
| 141 | /// raw_fd_ostream - Open the specified file for writing. If an error occurs, |
| 142 | /// information about the error is put into ErrorInfo, and the stream should |
| 143 | /// be immediately destroyed. |
| 144 | raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo) { |
Chris Lattner | c52b128 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 145 | // Handle "-" as stdout. |
| 146 | if (Filename[0] == '-' && Filename[1] == 0) { |
| 147 | FD = STDOUT_FILENO; |
| 148 | ShouldClose = false; |
| 149 | return; |
| 150 | } |
| 151 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 152 | FD = open(Filename, O_WRONLY|O_CREAT|O_TRUNC, 0644); |
| 153 | if (FD < 0) { |
| 154 | ErrorInfo = "Error opening output file '" + std::string(Filename) + "'"; |
| 155 | ShouldClose = false; |
| 156 | } else { |
| 157 | ShouldClose = true; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | raw_fd_ostream::~raw_fd_ostream() { |
| 162 | flush(); |
| 163 | if (ShouldClose) |
| 164 | close(FD); |
| 165 | } |
| 166 | |
| 167 | void raw_fd_ostream::flush_impl() { |
| 168 | if (OutBufCur-OutBufStart) |
Chris Lattner | d497df5 | 2008-08-17 01:46:05 +0000 | [diff] [blame] | 169 | ::write(FD, OutBufStart, OutBufCur-OutBufStart); |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 170 | HandleFlush(); |
| 171 | } |
| 172 | |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 173 | //===----------------------------------------------------------------------===// |
| 174 | // raw_stdout/err_ostream |
| 175 | //===----------------------------------------------------------------------===// |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 176 | |
| 177 | raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {} |
| 178 | raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false) {} |
| 179 | |
| 180 | // An out of line virtual method to provide a home for the class vtable. |
| 181 | void raw_stdout_ostream::handle() {} |
| 182 | void raw_stderr_ostream::handle() {} |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 183 | |
| 184 | /// outs() - This returns a reference to a raw_ostream for standard output. |
| 185 | /// Use it like: outs() << "foo" << "bar"; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 186 | raw_ostream &llvm::outs() { |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 187 | static raw_stdout_ostream S; |
| 188 | return S; |
| 189 | } |
| 190 | |
| 191 | /// errs() - This returns a reference to a raw_ostream for standard error. |
| 192 | /// Use it like: errs() << "foo" << "bar"; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 193 | raw_ostream &llvm::errs() { |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 194 | static raw_stderr_ostream S; |
| 195 | return S; |
| 196 | } |
| 197 | |
| 198 | //===----------------------------------------------------------------------===// |
| 199 | // raw_os_ostream |
| 200 | //===----------------------------------------------------------------------===// |
| 201 | |
| 202 | /// flush_impl - The is the piece of the class that is implemented by |
| 203 | /// subclasses. This outputs the currently buffered data and resets the |
| 204 | /// buffer to empty. |
| 205 | void raw_os_ostream::flush_impl() { |
| 206 | if (OutBufCur-OutBufStart) |
| 207 | OS.write(OutBufStart, OutBufCur-OutBufStart); |
| 208 | HandleFlush(); |
| 209 | } |