1 //===--- raw_ostream.cpp - Implement the raw_ostream classes --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements support for bulk buffered stream output.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/raw_ostream.h"
15 #include "llvm/Support/Format.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/Config/config.h"
20 #if defined(HAVE_UNISTD_H)
23 #if defined(HAVE_FCNTL_H)
31 # define STDIN_FILENO 0
34 # define STDOUT_FILENO 1
37 # define STDERR_FILENO 2
44 // An out of line virtual method to provide a home for the class vtable.
45 void raw_ostream::handle() {}
47 raw_ostream &raw_ostream::operator<<(unsigned long N) {
48 // Zero is a special case.
52 char NumberBuffer[20];
53 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
54 char *CurPtr = EndPtr;
57 *--CurPtr = '0' + char(N % 10);
60 return write(CurPtr, EndPtr-CurPtr);
63 raw_ostream &raw_ostream::operator<<(long N) {
65 if (OutBufCur >= OutBufEnd)
72 return this->operator<<(static_cast<unsigned long>(N));
75 raw_ostream &raw_ostream::operator<<(unsigned long long N) {
76 // Zero is a special case.
80 char NumberBuffer[20];
81 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
82 char *CurPtr = EndPtr;
85 *--CurPtr = '0' + char(N % 10);
88 return write(CurPtr, EndPtr-CurPtr);
91 raw_ostream &raw_ostream::operator<<(long long N) {
93 if (OutBufCur >= OutBufEnd)
100 return this->operator<<(static_cast<unsigned long long>(N));
103 raw_ostream &raw_ostream::operator<<(const void *P) {
104 // FIXME: This could be much faster if it matters.
105 return *this << format("%p", P);
109 raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) {
110 if (OutBufCur+Size > OutBufEnd)
113 // Handle short strings specially, memcpy isn't very good at very short
116 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
117 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
118 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
119 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
122 // Normally the string to emit is shorter than the buffer.
123 if (Size <= unsigned(OutBufEnd-OutBufStart)) {
124 memcpy(OutBufCur, Ptr, Size);
128 // If emitting a string larger than our buffer, emit in chunks. In this
129 // case we know that we just flushed the buffer.
131 unsigned NumToEmit = OutBufEnd-OutBufStart;
132 if (Size < NumToEmit) NumToEmit = Size;
133 assert(OutBufCur == OutBufStart);
134 memcpy(OutBufStart, Ptr, NumToEmit);
137 OutBufCur = OutBufStart + NumToEmit;
147 raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
148 // If we have more than a few bytes left in our output buffer, try formatting
149 // directly onto its end.
150 unsigned NextBufferSize = 127;
151 if (OutBufEnd-OutBufCur > 3) {
152 unsigned BufferBytesLeft = OutBufEnd-OutBufCur;
153 unsigned BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
155 // Common case is that we have plenty of space.
156 if (BytesUsed < BufferBytesLeft) {
157 OutBufCur += BytesUsed;
161 // Otherwise, we overflowed and the return value tells us the size to try
163 NextBufferSize = BytesUsed;
166 // If we got here, we didn't have enough space in the output buffer for the
167 // string. Try printing into a SmallVector that is resized to have enough
168 // space. Iterate until we win.
169 SmallVector<char, 128> V;
172 V.resize(NextBufferSize);
174 // Try formatting into the SmallVector.
175 unsigned BytesUsed = Fmt.print(&V[0], NextBufferSize);
177 // If BytesUsed fit into the vector, we win.
178 if (BytesUsed < NextBufferSize)
179 return write(&V[0], BytesUsed);
181 // Otherwise, try again with a new size.
182 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
183 NextBufferSize = BytesUsed;
187 //===----------------------------------------------------------------------===//
189 //===----------------------------------------------------------------------===//
191 // Out of line virtual method.
192 void format_object_base::home() {
195 //===----------------------------------------------------------------------===//
197 //===----------------------------------------------------------------------===//
199 /// raw_fd_ostream - Open the specified file for writing. If an error occurs,
200 /// information about the error is put into ErrorInfo, and the stream should
201 /// be immediately destroyed.
202 raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo) {
203 // Handle "-" as stdout.
204 if (Filename[0] == '-' && Filename[1] == 0) {
210 FD = open(Filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
212 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
219 raw_fd_ostream::~raw_fd_ostream() {
225 void raw_fd_ostream::flush_impl() {
226 if (OutBufCur-OutBufStart)
227 ::write(FD, OutBufStart, OutBufCur-OutBufStart);
231 //===----------------------------------------------------------------------===//
232 // raw_stdout/err_ostream
233 //===----------------------------------------------------------------------===//
235 raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
236 raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false) {}
238 // An out of line virtual method to provide a home for the class vtable.
239 void raw_stdout_ostream::handle() {}
240 void raw_stderr_ostream::handle() {}
242 /// outs() - This returns a reference to a raw_ostream for standard output.
243 /// Use it like: outs() << "foo" << "bar";
244 raw_ostream &llvm::outs() {
245 static raw_stdout_ostream S;
249 /// errs() - This returns a reference to a raw_ostream for standard error.
250 /// Use it like: errs() << "foo" << "bar";
251 raw_ostream &llvm::errs() {
252 static raw_stderr_ostream S;
256 //===----------------------------------------------------------------------===//
258 //===----------------------------------------------------------------------===//
260 raw_os_ostream::~raw_os_ostream() {
264 /// flush_impl - The is the piece of the class that is implemented by
265 /// subclasses. This outputs the currently buffered data and resets the
267 void raw_os_ostream::flush_impl() {
268 if (OutBufCur-OutBufStart)
269 OS.write(OutBufStart, OutBufCur-OutBufStart);
273 //===----------------------------------------------------------------------===//
274 // raw_string_ostream
275 //===----------------------------------------------------------------------===//
277 raw_string_ostream::~raw_string_ostream() {
281 /// flush_impl - The is the piece of the class that is implemented by
282 /// subclasses. This outputs the currently buffered data and resets the
284 void raw_string_ostream::flush_impl() {
285 if (OutBufCur-OutBufStart)
286 OS.append(OutBufStart, OutBufCur-OutBufStart);
290 //===----------------------------------------------------------------------===//
291 // raw_svector_ostream
292 //===----------------------------------------------------------------------===//
294 raw_svector_ostream::~raw_svector_ostream() {
298 /// flush_impl - The is the piece of the class that is implemented by
299 /// subclasses. This outputs the currently buffered data and resets the
301 void raw_svector_ostream::flush_impl() {
302 if (OutBufCur-OutBufStart)
303 OS.append(OutBufStart, OutBufCur);