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) {
70 return this->operator<<(static_cast<unsigned long>(N));
73 raw_ostream &raw_ostream::operator<<(unsigned long long N) {
74 // Zero is a special case.
78 char NumberBuffer[20];
79 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
80 char *CurPtr = EndPtr;
83 *--CurPtr = '0' + char(N % 10);
86 return write(CurPtr, EndPtr-CurPtr);
89 raw_ostream &raw_ostream::operator<<(long long N) {
95 return this->operator<<(static_cast<unsigned long long>(N));
98 raw_ostream &raw_ostream::operator<<(const void *P) {
99 uintptr_t N = (uintptr_t) P;
102 // Zero is a special case.
106 char NumberBuffer[20];
107 char *EndPtr = NumberBuffer+sizeof(NumberBuffer);
108 char *CurPtr = EndPtr;
112 *--CurPtr = (x < 10 ? '0' + x : 'a' + x - 10);
116 return write(CurPtr, EndPtr-CurPtr);
119 raw_ostream &raw_ostream::write(unsigned char C) {
120 if (OutBufCur >= OutBufEnd)
127 raw_ostream &raw_ostream::write(const char *Ptr, unsigned Size) {
128 if (OutBufCur+Size > OutBufEnd)
131 // Handle short strings specially, memcpy isn't very good at very short
134 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
135 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
136 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
137 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
140 // Normally the string to emit is shorter than the buffer.
141 if (Size <= unsigned(OutBufEnd-OutBufStart)) {
142 memcpy(OutBufCur, Ptr, Size);
146 // If emitting a string larger than our buffer, emit in chunks. In this
147 // case we know that we just flushed the buffer.
149 unsigned NumToEmit = OutBufEnd-OutBufStart;
150 if (Size < NumToEmit) NumToEmit = Size;
151 assert(OutBufCur == OutBufStart);
152 memcpy(OutBufStart, Ptr, NumToEmit);
155 OutBufCur = OutBufStart + NumToEmit;
168 raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) {
169 // If we have more than a few bytes left in our output buffer, try formatting
170 // directly onto its end.
171 unsigned NextBufferSize = 127;
172 if (OutBufEnd-OutBufCur > 3) {
173 unsigned BufferBytesLeft = OutBufEnd-OutBufCur;
174 unsigned BytesUsed = Fmt.print(OutBufCur, BufferBytesLeft);
176 // Common case is that we have plenty of space.
177 if (BytesUsed < BufferBytesLeft) {
178 OutBufCur += BytesUsed;
182 // Otherwise, we overflowed and the return value tells us the size to try
184 NextBufferSize = BytesUsed;
187 // If we got here, we didn't have enough space in the output buffer for the
188 // string. Try printing into a SmallVector that is resized to have enough
189 // space. Iterate until we win.
190 SmallVector<char, 128> V;
193 V.resize(NextBufferSize);
195 // Try formatting into the SmallVector.
196 unsigned BytesUsed = Fmt.print(&V[0], NextBufferSize);
198 // If BytesUsed fit into the vector, we win.
199 if (BytesUsed <= NextBufferSize)
200 return write(&V[0], BytesUsed);
202 // Otherwise, try again with a new size.
203 assert(BytesUsed > NextBufferSize && "Didn't grow buffer!?");
204 NextBufferSize = BytesUsed;
208 //===----------------------------------------------------------------------===//
210 //===----------------------------------------------------------------------===//
212 // Out of line virtual method.
213 void format_object_base::home() {
216 //===----------------------------------------------------------------------===//
218 //===----------------------------------------------------------------------===//
220 /// raw_fd_ostream - Open the specified file for writing. If an error
221 /// occurs, information about the error is put into ErrorInfo, and the
222 /// stream should be immediately destroyed; the string will be empty
223 /// if no error occurred.
224 raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary,
225 std::string &ErrorInfo) : pos(0) {
228 // Handle "-" as stdout.
229 if (Filename[0] == '-' && Filename[1] == 0) {
231 // If user requested binary then put stdout into binary mode if
234 sys::Program::ChangeStdoutToBinary();
239 int Flags = O_WRONLY|O_CREAT|O_TRUNC;
244 FD = open(Filename, Flags, 0644);
246 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
253 raw_fd_ostream::~raw_fd_ostream() {
261 void raw_fd_ostream::flush_impl() {
262 assert (FD >= 0 && "File already closed.");
263 if (OutBufCur-OutBufStart) {
264 pos += (OutBufCur - OutBufStart);
265 ::write(FD, OutBufStart, OutBufCur-OutBufStart);
270 void raw_fd_ostream::close() {
271 assert (ShouldClose);
278 uint64_t raw_fd_ostream::seek(uint64_t off) {
280 pos = lseek(FD, off, SEEK_SET);
284 //===----------------------------------------------------------------------===//
285 // raw_stdout/err_ostream
286 //===----------------------------------------------------------------------===//
288 raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
289 raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
292 // An out of line virtual method to provide a home for the class vtable.
293 void raw_stdout_ostream::handle() {}
294 void raw_stderr_ostream::handle() {}
296 /// outs() - This returns a reference to a raw_ostream for standard output.
297 /// Use it like: outs() << "foo" << "bar";
298 raw_ostream &llvm::outs() {
299 static raw_stdout_ostream S;
303 /// errs() - This returns a reference to a raw_ostream for standard error.
304 /// Use it like: errs() << "foo" << "bar";
305 raw_ostream &llvm::errs() {
306 static raw_stderr_ostream S;
310 //===----------------------------------------------------------------------===//
312 //===----------------------------------------------------------------------===//
314 raw_os_ostream::~raw_os_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_os_ostream::flush_impl() {
322 if (OutBufCur-OutBufStart)
323 OS.write(OutBufStart, OutBufCur-OutBufStart);
327 //===----------------------------------------------------------------------===//
328 // raw_string_ostream
329 //===----------------------------------------------------------------------===//
331 raw_string_ostream::~raw_string_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_string_ostream::flush_impl() {
339 if (OutBufCur-OutBufStart)
340 OS.append(OutBufStart, OutBufCur-OutBufStart);
344 //===----------------------------------------------------------------------===//
345 // raw_svector_ostream
346 //===----------------------------------------------------------------------===//
348 raw_svector_ostream::~raw_svector_ostream() {
352 /// flush_impl - The is the piece of the class that is implemented by
353 /// subclasses. This outputs the currently buffered data and resets the
355 void raw_svector_ostream::flush_impl() {
356 if (OutBufCur-OutBufStart)
357 OS.append(OutBufStart, OutBufCur);