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