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 | 42f77ab | 2008-08-23 20:34:06 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Format.h" |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 16 | #include "llvm/System/Program.h" |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 17 | #include "llvm/System/Process.h" |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 19 | #include "llvm/Config/config.h" |
Daniel Dunbar | b372c11 | 2009-03-17 21:15:18 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
Daniel Dunbar | 579fb87 | 2009-07-15 08:11:46 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 22 | #include <ostream> |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 24 | #if defined(HAVE_UNISTD_H) |
| 25 | # include <unistd.h> |
| 26 | #endif |
| 27 | #if defined(HAVE_FCNTL_H) |
| 28 | # include <fcntl.h> |
| 29 | #endif |
Argyrios Kyrtzidis | b68dc36 | 2008-08-17 09:25:21 +0000 | [diff] [blame] | 30 | |
| 31 | #if defined(_MSC_VER) |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 32 | #include <io.h> |
Cedric Venet | a3f343f | 2008-08-24 11:56:40 +0000 | [diff] [blame] | 33 | #include <fcntl.h> |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 34 | #ifndef STDIN_FILENO |
| 35 | # define STDIN_FILENO 0 |
| 36 | #endif |
| 37 | #ifndef STDOUT_FILENO |
| 38 | # define STDOUT_FILENO 1 |
| 39 | #endif |
| 40 | #ifndef STDERR_FILENO |
| 41 | # define STDERR_FILENO 2 |
| 42 | #endif |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | |
Dan Gohman | e87b2ab | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 47 | raw_ostream::~raw_ostream() { |
| 48 | delete [] OutBufStart; |
| 49 | |
| 50 | // If there are any pending errors, report them now. Clients wishing |
| 51 | // to avoid llvm_report_error calls should check for errors with |
| 52 | // has_error() and clear the error flag with clear_error() before |
| 53 | // destructing raw_ostream objects which may have errors. |
| 54 | if (Error) |
| 55 | llvm_report_error("IO failure on output stream."); |
| 56 | } |
Chris Lattner | 969a46a | 2008-08-22 15:45:00 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 58 | // An out of line virtual method to provide a home for the class vtable. |
| 59 | void raw_ostream::handle() {} |
| 60 | |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 61 | raw_ostream &raw_ostream::operator<<(unsigned long N) { |
| 62 | // Zero is a special case. |
| 63 | if (N == 0) |
| 64 | return *this << '0'; |
| 65 | |
| 66 | char NumberBuffer[20]; |
| 67 | char *EndPtr = NumberBuffer+sizeof(NumberBuffer); |
| 68 | char *CurPtr = EndPtr; |
| 69 | |
| 70 | while (N) { |
| 71 | *--CurPtr = '0' + char(N % 10); |
| 72 | N /= 10; |
| 73 | } |
| 74 | return write(CurPtr, EndPtr-CurPtr); |
| 75 | } |
| 76 | |
| 77 | raw_ostream &raw_ostream::operator<<(long N) { |
| 78 | if (N < 0) { |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 79 | *this << '-'; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 80 | N = -N; |
| 81 | } |
| 82 | |
| 83 | return this->operator<<(static_cast<unsigned long>(N)); |
| 84 | } |
| 85 | |
| 86 | raw_ostream &raw_ostream::operator<<(unsigned long long N) { |
| 87 | // Zero is a special case. |
| 88 | if (N == 0) |
| 89 | return *this << '0'; |
| 90 | |
| 91 | char NumberBuffer[20]; |
| 92 | char *EndPtr = NumberBuffer+sizeof(NumberBuffer); |
| 93 | char *CurPtr = EndPtr; |
| 94 | |
| 95 | while (N) { |
| 96 | *--CurPtr = '0' + char(N % 10); |
| 97 | N /= 10; |
| 98 | } |
| 99 | return write(CurPtr, EndPtr-CurPtr); |
| 100 | } |
| 101 | |
| 102 | raw_ostream &raw_ostream::operator<<(long long N) { |
| 103 | if (N < 0) { |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 104 | *this << '-'; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 105 | N = -N; |
| 106 | } |
| 107 | |
| 108 | return this->operator<<(static_cast<unsigned long long>(N)); |
| 109 | } |
| 110 | |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 111 | raw_ostream &raw_ostream::operator<<(const void *P) { |
Daniel Dunbar | b451a0c | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 112 | uintptr_t N = (uintptr_t) P; |
| 113 | *this << '0' << 'x'; |
| 114 | |
| 115 | // Zero is a special case. |
| 116 | if (N == 0) |
| 117 | return *this << '0'; |
| 118 | |
| 119 | char NumberBuffer[20]; |
| 120 | char *EndPtr = NumberBuffer+sizeof(NumberBuffer); |
| 121 | char *CurPtr = EndPtr; |
| 122 | |
| 123 | while (N) { |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame^] | 124 | uintptr_t x = N % 16; |
Daniel Dunbar | b451a0c | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 125 | *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10); |
| 126 | N /= 16; |
| 127 | } |
| 128 | |
| 129 | return write(CurPtr, EndPtr-CurPtr); |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame] | 132 | void raw_ostream::flush_nonempty() { |
| 133 | assert(OutBufCur > OutBufStart && "Invalid call to flush_nonempty."); |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 134 | write_impl(OutBufStart, OutBufCur - OutBufStart); |
Daniel Dunbar | cf2a2c6 | 2009-03-16 22:55:06 +0000 | [diff] [blame] | 135 | OutBufCur = OutBufStart; |
| 136 | } |
| 137 | |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 138 | raw_ostream &raw_ostream::write(unsigned char C) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 139 | // Group exceptional cases into a single branch. |
| 140 | if (OutBufCur >= OutBufEnd) { |
| 141 | if (Unbuffered) { |
| 142 | write_impl(reinterpret_cast<char*>(&C), 1); |
| 143 | return *this; |
| 144 | } |
| 145 | |
| 146 | if (!OutBufStart) |
| 147 | SetBufferSize(); |
| 148 | else |
| 149 | flush_nonempty(); |
Daniel Dunbar | d17d74b | 2009-03-17 01:13:35 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 152 | *OutBufCur++ = C; |
Daniel Dunbar | b451a0c | 2009-03-16 22:08:44 +0000 | [diff] [blame] | 153 | return *this; |
Daniel Dunbar | de75d7f | 2009-03-16 22:00:17 +0000 | [diff] [blame] | 154 | } |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 155 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame^] | 156 | raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 157 | // Group exceptional cases into a single branch. |
Daniel Dunbar | b372c11 | 2009-03-17 21:15:18 +0000 | [diff] [blame] | 158 | if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 159 | if (Unbuffered) { |
| 160 | write_impl(Ptr, Size); |
| 161 | return *this; |
| 162 | } |
Daniel Dunbar | d17d74b | 2009-03-17 01:13:35 +0000 | [diff] [blame] | 163 | |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 164 | if (!OutBufStart) |
| 165 | SetBufferSize(); |
| 166 | else |
| 167 | flush_nonempty(); |
| 168 | } |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 169 | |
| 170 | // Handle short strings specially, memcpy isn't very good at very short |
| 171 | // strings. |
| 172 | switch (Size) { |
| 173 | case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH |
| 174 | case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH |
| 175 | case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH |
| 176 | case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH |
| 177 | case 0: break; |
| 178 | default: |
| 179 | // Normally the string to emit is shorter than the buffer. |
David Greene | 7184781 | 2009-07-14 20:18:05 +0000 | [diff] [blame] | 180 | if (Size <= unsigned(OutBufEnd-OutBufCur)) { |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 181 | memcpy(OutBufCur, Ptr, Size); |
| 182 | break; |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 183 | } |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 184 | |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 185 | // Otherwise we are emitting a string larger than our buffer. We |
| 186 | // know we already flushed, so just write it out directly. |
| 187 | write_impl(Ptr, Size); |
| 188 | Size = 0; |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 189 | break; |
| 190 | } |
| 191 | OutBufCur += Size; |
Daniel Dunbar | e77e434 | 2009-03-10 16:21:55 +0000 | [diff] [blame] | 192 | |
Owen Anderson | 66b17ba | 2008-08-21 20:58:52 +0000 | [diff] [blame] | 193 | return *this; |
| 194 | } |
| 195 | |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 196 | // Formatted output. |
| 197 | raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) { |
Daniel Dunbar | 262541b | 2009-03-17 01:36:56 +0000 | [diff] [blame] | 198 | // If we have more than a few bytes left in our output buffer, try |
| 199 | // formatting directly onto its end. |
| 200 | // |
| 201 | // FIXME: This test is a bit silly, since if we don't have enough |
| 202 | // space in the buffer we will have to flush the formatted output |
| 203 | // anyway. We should just flush upfront in such cases, and use the |
| 204 | // whole buffer as our scratch pad. Note, however, that this case is |
| 205 | // also necessary for correctness on unbuffered streams. |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame^] | 206 | size_t NextBufferSize = 127; |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 207 | if (OutBufEnd-OutBufCur > 3) { |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame^] | 208 | size_t BufferBytesLeft = OutBufEnd-OutBufCur; |
| 209 | size_t BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft); |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 210 | |
| 211 | // Common case is that we have plenty of space. |
| 212 | if (BytesUsed < BufferBytesLeft) { |
| 213 | OutBufCur += BytesUsed; |
| 214 | return *this; |
| 215 | } |
| 216 | |
| 217 | // Otherwise, we overflowed and the return value tells us the size to try |
| 218 | // again with. |
| 219 | NextBufferSize = BytesUsed; |
| 220 | } |
| 221 | |
| 222 | // If we got here, we didn't have enough space in the output buffer for the |
| 223 | // string. Try printing into a SmallVector that is resized to have enough |
| 224 | // space. Iterate until we win. |
| 225 | SmallVector<char, 128> V; |
| 226 | |
| 227 | while (1) { |
| 228 | V.resize(NextBufferSize); |
| 229 | |
| 230 | // Try formatting into the SmallVector. |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame^] | 231 | size_t BytesUsed = Fmt.print(&V[0], NextBufferSize); |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 232 | |
| 233 | // If BytesUsed fit into the vector, we win. |
Chris Lattner | f61ca1e | 2008-10-26 19:20:47 +0000 | [diff] [blame] | 234 | if (BytesUsed <= NextBufferSize) |
Chris Lattner | 097af7f | 2008-08-23 19:23:10 +0000 | [diff] [blame] | 235 | return write(&V[0], BytesUsed); |
| 236 | |
| 237 | // Otherwise, try again with a new size. |
| 238 | assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?"); |
| 239 | NextBufferSize = BytesUsed; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | //===----------------------------------------------------------------------===// |
| 244 | // Formatted Output |
| 245 | //===----------------------------------------------------------------------===// |
| 246 | |
| 247 | // Out of line virtual method. |
| 248 | void format_object_base::home() { |
| 249 | } |
| 250 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 251 | //===----------------------------------------------------------------------===// |
| 252 | // raw_fd_ostream |
| 253 | //===----------------------------------------------------------------------===// |
| 254 | |
Daniel Dunbar | 48534b3 | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 255 | /// raw_fd_ostream - Open the specified file for writing. If an error |
| 256 | /// occurs, information about the error is put into ErrorInfo, and the |
| 257 | /// stream should be immediately destroyed; the string will be empty |
| 258 | /// if no error occurred. |
Dan Gohman | a1bdced | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 259 | raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, bool Force, |
Ted Kremenek | d75ba1c | 2008-12-04 22:51:11 +0000 | [diff] [blame] | 260 | std::string &ErrorInfo) : pos(0) { |
Daniel Dunbar | 48534b3 | 2008-10-21 19:53:10 +0000 | [diff] [blame] | 261 | ErrorInfo.clear(); |
| 262 | |
Chris Lattner | c52b128 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 263 | // Handle "-" as stdout. |
| 264 | if (Filename[0] == '-' && Filename[1] == 0) { |
| 265 | FD = STDOUT_FILENO; |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 266 | // If user requested binary then put stdout into binary mode if |
| 267 | // possible. |
| 268 | if (Binary) |
| 269 | sys::Program::ChangeStdoutToBinary(); |
Chris Lattner | c52b128 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 270 | ShouldClose = false; |
| 271 | return; |
| 272 | } |
| 273 | |
Daniel Dunbar | 0d9eb9b | 2008-11-13 05:01:07 +0000 | [diff] [blame] | 274 | int Flags = O_WRONLY|O_CREAT|O_TRUNC; |
| 275 | #ifdef O_BINARY |
| 276 | if (Binary) |
| 277 | Flags |= O_BINARY; |
| 278 | #endif |
Dan Gohman | a1bdced | 2009-07-15 17:29:42 +0000 | [diff] [blame] | 279 | if (!Force) |
| 280 | Flags |= O_EXCL; |
Dan Gohman | 9f79d3e | 2009-07-15 16:39:40 +0000 | [diff] [blame] | 281 | FD = open(Filename, Flags, 0664); |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 282 | if (FD < 0) { |
| 283 | ErrorInfo = "Error opening output file '" + std::string(Filename) + "'"; |
| 284 | ShouldClose = false; |
| 285 | } else { |
| 286 | ShouldClose = true; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | raw_fd_ostream::~raw_fd_ostream() { |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 291 | if (FD >= 0) { |
| 292 | flush(); |
| 293 | if (ShouldClose) |
Dan Gohman | d3a974f | 2009-07-15 16:43:01 +0000 | [diff] [blame] | 294 | if (::close(FD) != 0) |
Dan Gohman | e87b2ab | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 295 | error_detected(); |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 296 | } |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame^] | 299 | void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) { |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 300 | assert (FD >= 0 && "File already closed."); |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 301 | pos += Size; |
Daniel Dunbar | 579fb87 | 2009-07-15 08:11:46 +0000 | [diff] [blame] | 302 | if (::write(FD, Ptr, Size) != (ssize_t) Size) |
Dan Gohman | e87b2ab | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 303 | error_detected(); |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 306 | void raw_fd_ostream::close() { |
| 307 | assert (ShouldClose); |
| 308 | ShouldClose = false; |
| 309 | flush(); |
Dan Gohman | d3a974f | 2009-07-15 16:43:01 +0000 | [diff] [blame] | 310 | if (::close(FD) != 0) |
Dan Gohman | e87b2ab | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 311 | error_detected(); |
Ted Kremenek | 43d1f02 | 2008-10-23 23:49:09 +0000 | [diff] [blame] | 312 | FD = -1; |
| 313 | } |
| 314 | |
Ted Kremenek | 9c27886 | 2009-01-26 21:42:04 +0000 | [diff] [blame] | 315 | uint64_t raw_fd_ostream::seek(uint64_t off) { |
| 316 | flush(); |
Daniel Dunbar | 579fb87 | 2009-07-15 08:11:46 +0000 | [diff] [blame] | 317 | pos = ::lseek(FD, off, SEEK_SET); |
Dan Gohman | d3a974f | 2009-07-15 16:43:01 +0000 | [diff] [blame] | 318 | if (pos != off) |
Dan Gohman | e87b2ab | 2009-07-15 23:25:33 +0000 | [diff] [blame] | 319 | error_detected(); |
Ted Kremenek | 9c27886 | 2009-01-26 21:42:04 +0000 | [diff] [blame] | 320 | return pos; |
| 321 | } |
| 322 | |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 323 | raw_ostream &raw_fd_ostream::changeColor(enum Colors colors, bool bold, |
| 324 | bool bg) { |
| 325 | if (sys::Process::ColorNeedsFlush()) |
| 326 | flush(); |
| 327 | const char *colorcode = |
| 328 | (colors == SAVEDCOLOR) ? sys::Process::OutputBold(bg) |
| 329 | : sys::Process::OutputColor(colors, bold, bg); |
| 330 | if (colorcode) { |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame^] | 331 | size_t len = strlen(colorcode); |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 332 | write(colorcode, len); |
| 333 | // don't account colors towards output characters |
| 334 | pos -= len; |
| 335 | } |
| 336 | return *this; |
| 337 | } |
| 338 | |
| 339 | raw_ostream &raw_fd_ostream::resetColor() { |
| 340 | if (sys::Process::ColorNeedsFlush()) |
| 341 | flush(); |
| 342 | const char *colorcode = sys::Process::ResetColor(); |
| 343 | if (colorcode) { |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame^] | 344 | size_t len = strlen(colorcode); |
Torok Edwin | e8ebb0f | 2009-06-04 07:09:50 +0000 | [diff] [blame] | 345 | write(colorcode, len); |
| 346 | // don't account colors towards output characters |
| 347 | pos -= len; |
| 348 | } |
| 349 | return *this; |
| 350 | } |
| 351 | |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 352 | //===----------------------------------------------------------------------===// |
| 353 | // raw_stdout/err_ostream |
| 354 | //===----------------------------------------------------------------------===// |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 355 | |
| 356 | raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {} |
Daniel Dunbar | e77e434 | 2009-03-10 16:21:55 +0000 | [diff] [blame] | 357 | raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false, |
| 358 | true) {} |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 359 | |
| 360 | // An out of line virtual method to provide a home for the class vtable. |
| 361 | void raw_stdout_ostream::handle() {} |
| 362 | void raw_stderr_ostream::handle() {} |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 363 | |
| 364 | /// outs() - This returns a reference to a raw_ostream for standard output. |
| 365 | /// Use it like: outs() << "foo" << "bar"; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 366 | raw_ostream &llvm::outs() { |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 367 | static raw_stdout_ostream S; |
| 368 | return S; |
| 369 | } |
| 370 | |
| 371 | /// errs() - This returns a reference to a raw_ostream for standard error. |
| 372 | /// Use it like: errs() << "foo" << "bar"; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 373 | raw_ostream &llvm::errs() { |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 374 | static raw_stderr_ostream S; |
| 375 | return S; |
| 376 | } |
| 377 | |
| 378 | //===----------------------------------------------------------------------===// |
| 379 | // raw_os_ostream |
| 380 | //===----------------------------------------------------------------------===// |
| 381 | |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 382 | raw_os_ostream::~raw_os_ostream() { |
| 383 | flush(); |
| 384 | } |
| 385 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame^] | 386 | void raw_os_ostream::write_impl(const char *Ptr, size_t Size) { |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 387 | OS.write(Ptr, Size); |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 388 | } |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 389 | |
Douglas Gregor | 8f7be47 | 2009-04-20 07:34:17 +0000 | [diff] [blame] | 390 | uint64_t raw_os_ostream::current_pos() { return OS.tellp(); } |
| 391 | |
| 392 | uint64_t raw_os_ostream::tell() { |
| 393 | return (uint64_t)OS.tellp() + GetNumBytesInBuffer(); |
| 394 | } |
| 395 | |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 396 | //===----------------------------------------------------------------------===// |
| 397 | // raw_string_ostream |
| 398 | //===----------------------------------------------------------------------===// |
| 399 | |
| 400 | raw_string_ostream::~raw_string_ostream() { |
| 401 | flush(); |
| 402 | } |
| 403 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame^] | 404 | void raw_string_ostream::write_impl(const char *Ptr, size_t Size) { |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 405 | OS.append(Ptr, Size); |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | //===----------------------------------------------------------------------===// |
| 409 | // raw_svector_ostream |
| 410 | //===----------------------------------------------------------------------===// |
| 411 | |
| 412 | raw_svector_ostream::~raw_svector_ostream() { |
| 413 | flush(); |
| 414 | } |
| 415 | |
Dan Gohman | ad60f66 | 2009-07-16 15:24:40 +0000 | [diff] [blame^] | 416 | void raw_svector_ostream::write_impl(const char *Ptr, size_t Size) { |
Daniel Dunbar | 89a66a9 | 2009-03-16 23:29:31 +0000 | [diff] [blame] | 417 | OS.append(Ptr, Ptr + Size); |
Chris Lattner | 78a2812 | 2008-08-23 22:43:04 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Douglas Gregor | 8f7be47 | 2009-04-20 07:34:17 +0000 | [diff] [blame] | 420 | uint64_t raw_svector_ostream::current_pos() { return OS.size(); } |
| 421 | |
| 422 | uint64_t raw_svector_ostream::tell() { |
| 423 | return OS.size() + GetNumBytesInBuffer(); |
| 424 | } |