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