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"
19 #include "llvm/Support/Compiler.h"
22 #if defined(HAVE_UNISTD_H)
25 #if defined(HAVE_FCNTL_H)
33 # define STDIN_FILENO 0
36 # define STDOUT_FILENO 1
39 # define STDERR_FILENO 2
46 // An out of line virtual method to provide a home for the class vtable.
47 void raw_ostream::handle() {}
49 raw_ostream
&raw_ostream::operator<<(unsigned long N
) {
50 // Zero is a special case.
54 char NumberBuffer
[20];
55 char *EndPtr
= NumberBuffer
+sizeof(NumberBuffer
);
56 char *CurPtr
= EndPtr
;
59 *--CurPtr
= '0' + char(N
% 10);
62 return write(CurPtr
, EndPtr
-CurPtr
);
65 raw_ostream
&raw_ostream::operator<<(long N
) {
71 return this->operator<<(static_cast<unsigned long>(N
));
74 raw_ostream
&raw_ostream::operator<<(unsigned long long N
) {
75 // Zero is a special case.
79 char NumberBuffer
[20];
80 char *EndPtr
= NumberBuffer
+sizeof(NumberBuffer
);
81 char *CurPtr
= EndPtr
;
84 *--CurPtr
= '0' + char(N
% 10);
87 return write(CurPtr
, EndPtr
-CurPtr
);
90 raw_ostream
&raw_ostream::operator<<(long long N
) {
96 return this->operator<<(static_cast<unsigned long long>(N
));
99 raw_ostream
&raw_ostream::operator<<(const void *P
) {
100 uintptr_t N
= (uintptr_t) P
;
103 // Zero is a special case.
107 char NumberBuffer
[20];
108 char *EndPtr
= NumberBuffer
+sizeof(NumberBuffer
);
109 char *CurPtr
= EndPtr
;
113 *--CurPtr
= (x
< 10 ? '0' + x
: 'a' + x
- 10);
117 return write(CurPtr
, EndPtr
-CurPtr
);
120 void raw_ostream::flush_nonempty() {
121 assert(OutBufCur
> OutBufStart
&& "Invalid call to flush_nonempty.");
122 write_impl(OutBufStart
, OutBufCur
- OutBufStart
);
123 OutBufCur
= OutBufStart
;
126 raw_ostream
&raw_ostream::write(unsigned char C
) {
127 // Group exceptional cases into a single branch.
128 if (OutBufCur
>= OutBufEnd
) {
130 write_impl(reinterpret_cast<char*>(&C
), 1);
144 raw_ostream
&raw_ostream::write(const char *Ptr
, unsigned Size
) {
145 // Group exceptional cases into a single branch.
146 if (BUILTIN_EXPECT(OutBufCur
+Size
> OutBufEnd
, false)) {
148 write_impl(Ptr
, Size
);
158 // Handle short strings specially, memcpy isn't very good at very short
161 case 4: OutBufCur
[3] = Ptr
[3]; // FALL THROUGH
162 case 3: OutBufCur
[2] = Ptr
[2]; // FALL THROUGH
163 case 2: OutBufCur
[1] = Ptr
[1]; // FALL THROUGH
164 case 1: OutBufCur
[0] = Ptr
[0]; // FALL THROUGH
167 // Normally the string to emit is shorter than the buffer.
168 if (Size
<= unsigned(OutBufEnd
-OutBufStart
)) {
169 memcpy(OutBufCur
, Ptr
, Size
);
173 // Otherwise we are emitting a string larger than our buffer. We
174 // know we already flushed, so just write it out directly.
175 write_impl(Ptr
, Size
);
185 raw_ostream
&raw_ostream::operator<<(const format_object_base
&Fmt
) {
186 // If we have more than a few bytes left in our output buffer, try
187 // formatting directly onto its end.
189 // FIXME: This test is a bit silly, since if we don't have enough
190 // space in the buffer we will have to flush the formatted output
191 // anyway. We should just flush upfront in such cases, and use the
192 // whole buffer as our scratch pad. Note, however, that this case is
193 // also necessary for correctness on unbuffered streams.
194 unsigned NextBufferSize
= 127;
195 if (OutBufEnd
-OutBufCur
> 3) {
196 unsigned BufferBytesLeft
= OutBufEnd
-OutBufCur
;
197 unsigned BytesUsed
= Fmt
.print(OutBufCur
, BufferBytesLeft
);
199 // Common case is that we have plenty of space.
200 if (BytesUsed
< BufferBytesLeft
) {
201 OutBufCur
+= BytesUsed
;
205 // Otherwise, we overflowed and the return value tells us the size to try
207 NextBufferSize
= BytesUsed
;
210 // If we got here, we didn't have enough space in the output buffer for the
211 // string. Try printing into a SmallVector that is resized to have enough
212 // space. Iterate until we win.
213 SmallVector
<char, 128> V
;
216 V
.resize(NextBufferSize
);
218 // Try formatting into the SmallVector.
219 unsigned BytesUsed
= Fmt
.print(&V
[0], NextBufferSize
);
221 // If BytesUsed fit into the vector, we win.
222 if (BytesUsed
<= NextBufferSize
)
223 return write(&V
[0], BytesUsed
);
225 // Otherwise, try again with a new size.
226 assert(BytesUsed
> NextBufferSize
&& "Didn't grow buffer!?");
227 NextBufferSize
= BytesUsed
;
231 //===----------------------------------------------------------------------===//
233 //===----------------------------------------------------------------------===//
235 // Out of line virtual method.
236 void format_object_base::home() {
239 //===----------------------------------------------------------------------===//
241 //===----------------------------------------------------------------------===//
243 /// raw_fd_ostream - Open the specified file for writing. If an error
244 /// occurs, information about the error is put into ErrorInfo, and the
245 /// stream should be immediately destroyed; the string will be empty
246 /// if no error occurred.
247 raw_fd_ostream::raw_fd_ostream(const char *Filename
, bool Binary
,
248 std::string
&ErrorInfo
) : pos(0) {
251 // Handle "-" as stdout.
252 if (Filename
[0] == '-' && Filename
[1] == 0) {
254 // If user requested binary then put stdout into binary mode if
257 sys::Program::ChangeStdoutToBinary();
262 int Flags
= O_WRONLY
|O_CREAT
|O_TRUNC
;
267 FD
= open(Filename
, Flags
, 0644);
269 ErrorInfo
= "Error opening output file '" + std::string(Filename
) + "'";
276 raw_fd_ostream::~raw_fd_ostream() {
284 void raw_fd_ostream::write_impl(const char *Ptr
, unsigned Size
) {
285 assert (FD
>= 0 && "File already closed.");
287 ::write(FD
, Ptr
, Size
);
290 void raw_fd_ostream::close() {
291 assert (ShouldClose
);
298 uint64_t raw_fd_ostream::seek(uint64_t off
) {
300 pos
= lseek(FD
, off
, SEEK_SET
);
304 //===----------------------------------------------------------------------===//
305 // raw_stdout/err_ostream
306 //===----------------------------------------------------------------------===//
308 raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO
, false) {}
309 raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO
, false,
312 // An out of line virtual method to provide a home for the class vtable.
313 void raw_stdout_ostream::handle() {}
314 void raw_stderr_ostream::handle() {}
316 /// outs() - This returns a reference to a raw_ostream for standard output.
317 /// Use it like: outs() << "foo" << "bar";
318 raw_ostream
&llvm::outs() {
319 static raw_stdout_ostream S
;
323 /// errs() - This returns a reference to a raw_ostream for standard error.
324 /// Use it like: errs() << "foo" << "bar";
325 raw_ostream
&llvm::errs() {
326 static raw_stderr_ostream S
;
330 //===----------------------------------------------------------------------===//
332 //===----------------------------------------------------------------------===//
334 raw_os_ostream::~raw_os_ostream() {
338 void raw_os_ostream::write_impl(const char *Ptr
, unsigned Size
) {
342 uint64_t raw_os_ostream::current_pos() { return OS
.tellp(); }
344 uint64_t raw_os_ostream::tell() {
345 return (uint64_t)OS
.tellp() + GetNumBytesInBuffer();
348 //===----------------------------------------------------------------------===//
349 // raw_string_ostream
350 //===----------------------------------------------------------------------===//
352 raw_string_ostream::~raw_string_ostream() {
356 void raw_string_ostream::write_impl(const char *Ptr
, unsigned Size
) {
357 OS
.append(Ptr
, Size
);
360 //===----------------------------------------------------------------------===//
361 // raw_svector_ostream
362 //===----------------------------------------------------------------------===//
364 raw_svector_ostream::~raw_svector_ostream() {
368 void raw_svector_ostream::write_impl(const char *Ptr
, unsigned Size
) {
369 OS
.append(Ptr
, Ptr
+ Size
);
372 uint64_t raw_svector_ostream::current_pos() { return OS
.size(); }
374 uint64_t raw_svector_ostream::tell() {
375 return OS
.size() + GetNumBytesInBuffer();