Make formatted_raw_ostream restore the buffer settings of the

underlying stream when it is finished, so that clients don't
have to do this manually.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78952 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman 2009-08-13 23:16:59 +00:00
parent 009fc9e5d9
commit 6c304f2314
2 changed files with 19 additions and 15 deletions

View File

@ -101,13 +101,11 @@ namespace llvm
~formatted_raw_ostream() { ~formatted_raw_ostream() {
flush(); flush();
if (DeleteStream) releaseStream();
delete TheStream;
} }
void setStream(raw_ostream &Stream, bool Delete = false) { void setStream(raw_ostream &Stream, bool Delete = false) {
if (DeleteStream) releaseStream();
delete TheStream;
TheStream = &Stream; TheStream = &Stream;
DeleteStream = Delete; DeleteStream = Delete;
@ -118,6 +116,8 @@ namespace llvm
// had been using, and tell TheStream not to do its own buffering. // had been using, and tell TheStream not to do its own buffering.
if (size_t BufferSize = TheStream->GetBufferSize()) if (size_t BufferSize = TheStream->GetBufferSize())
SetBufferSize(BufferSize); SetBufferSize(BufferSize);
else
SetUnbuffered();
TheStream->SetUnbuffered(); TheStream->SetUnbuffered();
Scanned = begin(); Scanned = begin();
@ -130,6 +130,20 @@ namespace llvm
/// recent I/O, even if the current column + minpad > newcol. /// recent I/O, even if the current column + minpad > newcol.
/// ///
void PadToColumn(unsigned NewCol, unsigned MinPad = 0); void PadToColumn(unsigned NewCol, unsigned MinPad = 0);
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();
}
}; };
/// fouts() - This returns a reference to a formatted_raw_ostream for /// fouts() - This returns a reference to a formatted_raw_ostream for

View File

@ -2005,14 +2005,9 @@ void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
} }
void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const { void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
SlotTracker SlotTable(this); SlotTracker SlotTable(this);
size_t OldBufferSize = ROS.GetBufferSize();
formatted_raw_ostream OS(ROS); formatted_raw_ostream OS(ROS);
AssemblyWriter W(OS, SlotTable, this, AAW); AssemblyWriter W(OS, SlotTable, this, AAW);
W.write(this); W.write(this);
// formatted_raw_ostream forces the underlying raw_ostream to be
// unbuffered. Reset it to its original buffer size.
if (OldBufferSize != 0)
ROS.SetBufferSize(OldBufferSize);
} }
void Type::print(std::ostream &o) const { void Type::print(std::ostream &o) const {
@ -2033,7 +2028,6 @@ void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
ROS << "printing a <null> value\n"; ROS << "printing a <null> value\n";
return; return;
} }
size_t OldBufferSize = ROS.GetBufferSize();
formatted_raw_ostream OS(ROS); formatted_raw_ostream OS(ROS);
if (const Instruction *I = dyn_cast<Instruction>(this)) { if (const Instruction *I = dyn_cast<Instruction>(this)) {
const Function *F = I->getParent() ? I->getParent()->getParent() : 0; const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
@ -2089,10 +2083,6 @@ void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
} else { } else {
llvm_unreachable("Unknown value to print out!"); llvm_unreachable("Unknown value to print out!");
} }
// formatted_raw_ostream forces the underlying raw_ostream to be
// unbuffered. Reset it to its original buffer size.
if (OldBufferSize != 0)
ROS.SetBufferSize(OldBufferSize);
} }
void Value::print(std::ostream &O, AssemblyAnnotationWriter *AAW) const { void Value::print(std::ostream &O, AssemblyAnnotationWriter *AAW) const {