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