2010-02-15 19:55:04 +01:00
|
|
|
//===-- llvm/Support/FormattedStream.h - Formatted streams ------*- C++ -*-===//
|
2009-07-10 23:14:44 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains raw_ostream implementations for streams to do
|
|
|
|
// things like pretty-print comments.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-13 18:49:27 +02:00
|
|
|
#ifndef LLVM_SUPPORT_FORMATTEDSTREAM_H
|
|
|
|
#define LLVM_SUPPORT_FORMATTEDSTREAM_H
|
2009-07-10 23:14:44 +02:00
|
|
|
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
namespace llvm
|
|
|
|
{
|
2009-07-13 18:49:27 +02:00
|
|
|
/// formatted_raw_ostream - Formatted raw_fd_ostream to handle
|
|
|
|
/// asm-specific constructs.
|
2009-07-10 23:14:44 +02:00
|
|
|
///
|
|
|
|
class formatted_raw_ostream : public raw_ostream {
|
2009-07-14 22:18:05 +02:00
|
|
|
public:
|
|
|
|
/// DELETE_STREAM - Tell the destructor to delete the held stream.
|
|
|
|
///
|
2009-09-06 15:10:36 +02:00
|
|
|
static const bool DELETE_STREAM = true;
|
2009-07-24 01:21:10 +02:00
|
|
|
|
2009-07-14 22:18:05 +02:00
|
|
|
/// PRESERVE_STREAM - Tell the destructor to not delete the held
|
|
|
|
/// stream.
|
|
|
|
///
|
2009-09-06 15:10:36 +02:00
|
|
|
static const bool PRESERVE_STREAM = false;
|
2009-07-24 01:21:10 +02:00
|
|
|
|
2009-07-10 23:14:44 +02:00
|
|
|
private:
|
2009-07-16 03:32:46 +02:00
|
|
|
/// TheStream - The real stream we output to. We set it to be
|
|
|
|
/// unbuffered, since we're already doing our own buffering.
|
2009-07-10 23:14:44 +02:00
|
|
|
///
|
2009-07-14 22:33:33 +02:00
|
|
|
raw_ostream *TheStream;
|
2009-07-24 01:21:10 +02:00
|
|
|
|
2009-07-14 22:18:05 +02:00
|
|
|
/// DeleteStream - Do we need to delete TheStream in the
|
|
|
|
/// destructor?
|
|
|
|
///
|
|
|
|
bool DeleteStream;
|
2009-07-10 23:14:44 +02:00
|
|
|
|
2009-07-29 18:08:27 +02:00
|
|
|
/// ColumnScanned - The current output column of the data that's
|
|
|
|
/// been flushed and the portion of the buffer that's been
|
|
|
|
/// scanned. The column scheme is zero-based.
|
2009-07-10 23:14:44 +02:00
|
|
|
///
|
2009-07-29 18:08:27 +02:00
|
|
|
unsigned ColumnScanned;
|
|
|
|
|
|
|
|
/// Scanned - This points to one past the last character in the
|
|
|
|
/// buffer we've scanned.
|
|
|
|
///
|
2009-08-19 01:36:04 +02:00
|
|
|
const char *Scanned;
|
2009-07-10 23:14:44 +02:00
|
|
|
|
2009-08-15 04:01:04 +02:00
|
|
|
virtual void write_impl(const char *Ptr, size_t Size);
|
2009-07-10 23:14:44 +02:00
|
|
|
|
|
|
|
/// current_pos - Return the current position within the stream,
|
|
|
|
/// not counting the bytes currently in the buffer.
|
2009-12-19 02:38:42 +01:00
|
|
|
virtual uint64_t current_pos() const {
|
2009-07-10 23:14:44 +02:00
|
|
|
// This has the same effect as calling TheStream.current_pos(),
|
|
|
|
// but that interface is private.
|
2009-07-14 22:33:33 +02:00
|
|
|
return TheStream->tell() - TheStream->GetNumBytesInBuffer();
|
2009-07-10 23:14:44 +02:00
|
|
|
}
|
|
|
|
|
2009-08-19 01:36:04 +02:00
|
|
|
/// ComputeColumn - Examine the given output buffer and figure out which
|
|
|
|
/// column we end up in after output.
|
2009-07-10 23:14:44 +02:00
|
|
|
///
|
2009-08-19 01:36:04 +02:00
|
|
|
void ComputeColumn(const char *Ptr, size_t size);
|
2009-07-10 23:14:44 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// formatted_raw_ostream - Open the specified file for
|
|
|
|
/// writing. If an error occurs, information about the error is
|
|
|
|
/// put into ErrorInfo, and the stream should be immediately
|
|
|
|
/// destroyed; the string will be empty if no error occurred.
|
|
|
|
///
|
2009-07-16 03:32:46 +02:00
|
|
|
/// As a side effect, the given Stream is set to be Unbuffered.
|
|
|
|
/// This is because formatted_raw_ostream does its own buffering,
|
|
|
|
/// so it doesn't want another layer of buffering to be happening
|
|
|
|
/// underneath it.
|
|
|
|
///
|
2009-07-14 22:18:05 +02:00
|
|
|
formatted_raw_ostream(raw_ostream &Stream, bool Delete = false)
|
2009-07-29 18:08:27 +02:00
|
|
|
: raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) {
|
2009-07-16 17:37:26 +02:00
|
|
|
setStream(Stream, Delete);
|
2009-07-16 03:32:46 +02:00
|
|
|
}
|
|
|
|
explicit formatted_raw_ostream()
|
2009-07-29 18:08:27 +02:00
|
|
|
: raw_ostream(), TheStream(0), DeleteStream(false), ColumnScanned(0) {
|
2009-08-19 01:36:04 +02:00
|
|
|
Scanned = 0;
|
2009-07-29 18:08:27 +02:00
|
|
|
}
|
2009-07-14 22:18:05 +02:00
|
|
|
|
|
|
|
~formatted_raw_ostream() {
|
2009-07-29 18:08:27 +02:00
|
|
|
flush();
|
2009-08-14 01:16:59 +02:00
|
|
|
releaseStream();
|
2009-07-14 22:18:05 +02:00
|
|
|
}
|
2009-08-14 01:16:59 +02:00
|
|
|
|
2009-07-14 22:33:33 +02:00
|
|
|
void setStream(raw_ostream &Stream, bool Delete = false) {
|
2009-08-14 01:16:59 +02:00
|
|
|
releaseStream();
|
2009-07-16 17:37:26 +02:00
|
|
|
|
2009-07-14 22:33:33 +02:00
|
|
|
TheStream = &Stream;
|
|
|
|
DeleteStream = Delete;
|
2009-07-16 03:32:46 +02:00
|
|
|
|
2009-07-16 17:37:26 +02:00
|
|
|
// This formatted_raw_ostream inherits from raw_ostream, so it'll do its
|
|
|
|
// own buffering, and it doesn't need or want TheStream to do another
|
|
|
|
// layer of buffering underneath. Resize the buffer to what TheStream
|
|
|
|
// had been using, and tell TheStream not to do its own buffering.
|
2009-08-12 22:52:45 +02:00
|
|
|
if (size_t BufferSize = TheStream->GetBufferSize())
|
2009-07-16 03:32:46 +02:00
|
|
|
SetBufferSize(BufferSize);
|
2009-08-14 01:16:59 +02:00
|
|
|
else
|
2009-09-22 18:33:42 +02:00
|
|
|
SetUnbuffered();
|
2009-07-16 03:32:46 +02:00
|
|
|
TheStream->SetUnbuffered();
|
2009-07-29 18:08:27 +02:00
|
|
|
|
2009-08-19 01:36:04 +02:00
|
|
|
Scanned = 0;
|
2009-07-14 22:33:33 +02:00
|
|
|
}
|
2009-07-10 23:14:44 +02:00
|
|
|
|
2009-08-17 17:48:08 +02:00
|
|
|
/// PadToColumn - Align the output to some column number. If the current
|
|
|
|
/// column is already equal to or more than NewCol, PadToColumn inserts one
|
|
|
|
/// space.
|
2009-07-10 23:14:44 +02:00
|
|
|
///
|
2009-07-13 18:49:27 +02:00
|
|
|
/// \param NewCol - The column to move to.
|
2010-02-15 03:17:50 +01:00
|
|
|
formatted_raw_ostream &PadToColumn(unsigned NewCol);
|
2009-08-14 01:16:59 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
void releaseStream() {
|
|
|
|
// Delete the stream if needed. Otherwise, transfer the buffer
|
|
|
|
// settings from this raw_ostream back to the underlying stream.
|
|
|
|
if (!TheStream)
|
|
|
|
return;
|
|
|
|
if (DeleteStream)
|
|
|
|
delete TheStream;
|
|
|
|
else if (size_t BufferSize = GetBufferSize())
|
|
|
|
TheStream->SetBufferSize(BufferSize);
|
|
|
|
else
|
|
|
|
TheStream->SetUnbuffered();
|
|
|
|
}
|
2009-07-10 23:14:44 +02:00
|
|
|
};
|
2009-07-14 22:18:05 +02:00
|
|
|
|
|
|
|
/// fouts() - This returns a reference to a formatted_raw_ostream for
|
|
|
|
/// standard output. Use it like: fouts() << "foo" << "bar";
|
|
|
|
formatted_raw_ostream &fouts();
|
|
|
|
|
|
|
|
/// ferrs() - This returns a reference to a formatted_raw_ostream for
|
|
|
|
/// standard error. Use it like: ferrs() << "foo" << "bar";
|
|
|
|
formatted_raw_ostream &ferrs();
|
|
|
|
|
2010-01-05 02:36:30 +01:00
|
|
|
/// fdbgs() - This returns a reference to a formatted_raw_ostream for
|
|
|
|
/// debug output. Use it like: fdbgs() << "foo" << "bar";
|
|
|
|
formatted_raw_ostream &fdbgs();
|
|
|
|
|
2009-07-14 22:18:05 +02:00
|
|
|
} // end llvm namespace
|
|
|
|
|
2009-07-10 23:14:44 +02:00
|
|
|
|
|
|
|
#endif
|