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