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/System/Program.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/Config/config.h"
21 #if defined(HAVE_UNISTD_H)
24 #if defined(HAVE_FCNTL_H)
32 # define STDIN_FILENO 0
35 # define STDOUT_FILENO 1
38 # define STDERR_FILENO 2
45 // An out of line virtual method to provide a home for the class vtable.
46 void raw_ostream::handle() {}
48 raw_ostream &raw_ostream::operator<<(unsigned long N) {
49 // Zero is a special case.
53 char NumberBuffer[20];
54 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
55 char *CurPtr = EndPtr;
58 *--CurPtr = '0' + char(N % 10);
61 return write(CurPtr, EndPtr-CurPtr);
64 raw_ostream &raw_ostream::operator<<(long N) {
66 if (OutBufCur >= OutBufEnd)
73 return this->operator<<(static_cast<unsigned long>(N));
76 raw_ostream &raw_ostream::operator<<(unsigned long long N) {
77 // Zero is a special case.
81 char NumberBuffer[20];
82 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
83 char *CurPtr = EndPtr;
86 *--CurPtr = '0' + char(N % 10);
89 return write(CurPtr, EndPtr-CurPtr);
92 raw_ostream &raw_ostream::operator<<(long long N) {
94 if (OutBufCur >= OutBufEnd)
101 return this->operator<<(static_cast<unsigned long long>(N));
104 raw_ostream &raw_ostream::operator<<(const void *P) {
105 // FIXME: This could be much faster if it matters.
106 return *this << format("%p", P);
110 raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) {
111 if (OutBufCur+Size > OutBufEnd)
114 // Handle short strings specially, memcpy isn't very good at very short
117 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
118 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
119 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
120 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
123 // Normally the string to emit is shorter than the buffer.
124 if (Size <= unsigned(OutBufEnd-OutBufStart)) {
125 memcpy(OutBufCur, Ptr, Size);
129 // If emitting a string larger than our buffer, emit in chunks. In this
130 // case we know that we just flushed the buffer.
132 unsigned NumToEmit = OutBufEnd-OutBufStart;
133 if (Size < NumToEmit) NumToEmit = Size;
134 assert(OutBufCur == OutBufStart);
135 memcpy(OutBufStart, Ptr, NumToEmit);
138 OutBufCur = OutBufStart + NumToEmit;
151 raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
152 // If we have more than a few bytes left in our output buffer, try formatting
153 // directly onto its end.
154 unsigned NextBufferSize = 127;
155 if (OutBufEnd-OutBufCur > 3) {
156 unsigned BufferBytesLeft = OutBufEnd-OutBufCur;
157 unsigned BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
159 // Common case is that we have plenty of space.
160 if (BytesUsed < BufferBytesLeft) {
161 OutBufCur += BytesUsed;
165 // Otherwise, we overflowed and the return value tells us the size to try
167 NextBufferSize = BytesUsed;
170 // If we got here, we didn't have enough space in the output buffer for the
171 // string. Try printing into a SmallVector that is resized to have enough
172 // space. Iterate until we win.
173 SmallVector<char, 128> V;
176 V.resize(NextBufferSize);
178 // Try formatting into the SmallVector.
179 unsigned BytesUsed = Fmt.print(&V[0], NextBufferSize);
181 // If BytesUsed fit into the vector, we win.
182 if (BytesUsed <= NextBufferSize)
183 return write(&V[0], BytesUsed);
185 // Otherwise, try again with a new size.
186 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
187 NextBufferSize = BytesUsed;
191 //===----------------------------------------------------------------------===//
193 //===----------------------------------------------------------------------===//
195 // Out of line virtual method.
196 void format_object_base::home() {
199 //===----------------------------------------------------------------------===//
201 //===----------------------------------------------------------------------===//
203 /// raw_fd_ostream - Open the specified file for writing. If an error
204 /// occurs, information about the error is put into ErrorInfo, and the
205 /// stream should be immediately destroyed; the string will be empty
206 /// if no error occurred.
207 raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary,
208 std::string &ErrorInfo) : pos(0) {
211 // Handle "-" as stdout.
212 if (Filename[0] == '-' && Filename[1] == 0) {
214 // If user requested binary then put stdout into binary mode if
217 sys::Program::ChangeStdoutToBinary();
222 int Flags = O_WRONLY|O_CREAT|O_TRUNC;
227 FD = open(Filename, Flags, 0644);
229 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
236 raw_fd_ostream::~raw_fd_ostream() {
244 void raw_fd_ostream::flush_impl() {
245 assert (FD >= 0 && "File already closed.");
246 if (OutBufCur-OutBufStart) {
247 pos += (OutBufCur - OutBufStart);
248 ::write(FD, OutBufStart, OutBufCur-OutBufStart);
253 void raw_fd_ostream::close() {
254 assert (ShouldClose);
261 uint64_t raw_fd_ostream::seek(uint64_t off) {
263 pos = lseek(FD, off, SEEK_SET);
267 //===----------------------------------------------------------------------===//
268 // raw_stdout/err_ostream
269 //===----------------------------------------------------------------------===//
271 raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
272 raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
275 // An out of line virtual method to provide a home for the class vtable.
276 void raw_stdout_ostream::handle() {}
277 void raw_stderr_ostream::handle() {}
279 /// outs() - This returns a reference to a raw_ostream for standard output.
280 /// Use it like: outs() << "foo" << "bar";
281 raw_ostream &llvm::outs() {
282 static raw_stdout_ostream S;
286 /// errs() - This returns a reference to a raw_ostream for standard error.
287 /// Use it like: errs() << "foo" << "bar";
288 raw_ostream &llvm::errs() {
289 static raw_stderr_ostream S;
293 //===----------------------------------------------------------------------===//
295 //===----------------------------------------------------------------------===//
297 raw_os_ostream::~raw_os_ostream() {
301 /// flush_impl - The is the piece of the class that is implemented by
302 /// subclasses. This outputs the currently buffered data and resets the
304 void raw_os_ostream::flush_impl() {
305 if (OutBufCur-OutBufStart)
306 OS.write(OutBufStart, OutBufCur-OutBufStart);
310 //===----------------------------------------------------------------------===//
311 // raw_string_ostream
312 //===----------------------------------------------------------------------===//
314 raw_string_ostream::~raw_string_ostream() {
318 /// flush_impl - The is the piece of the class that is implemented by
319 /// subclasses. This outputs the currently buffered data and resets the
321 void raw_string_ostream::flush_impl() {
322 if (OutBufCur-OutBufStart)
323 OS.append(OutBufStart, OutBufCur-OutBufStart);
327 //===----------------------------------------------------------------------===//
328 // raw_svector_ostream
329 //===----------------------------------------------------------------------===//
331 raw_svector_ostream::~raw_svector_ostream() {
335 /// flush_impl - The is the piece of the class that is implemented by
336 /// subclasses. This outputs the currently buffered data and resets the
338 void raw_svector_ostream::flush_impl() {
339 if (OutBufCur-OutBufStart)
340 OS.append(OutBufStart, OutBufCur);