[llvm-commits] [llvm] r78837 - /llvm/trunk/lib/VMCore/AsmWriter.cpp

Dan Gohman gohman at apple.com
Wed Aug 12 13:56:04 PDT 2009


Author: djg
Date: Wed Aug 12 15:56:03 2009
New Revision: 78837

URL: http://llvm.org/viewvc/llvm-project?rev=78837&view=rev
Log:
Make AsmWriter more careful with formatted_raw_ostream so that
it doesn't leave the underlying stream in unbuffered mode when
the stream was originally buffered.

Also, change WriteAsOperand back to plain raw_ostream. This
lets it work for either formatted_raw_ostream or plain
raw_ostream, so that it doesn't have to force a buffer flush
on a plain raw_ostream.

Modified:
    llvm/trunk/lib/VMCore/AsmWriter.cpp

Modified: llvm/trunk/lib/VMCore/AsmWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/AsmWriter.cpp?rev=78837&r1=78836&r2=78837&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/AsmWriter.cpp (original)
+++ llvm/trunk/lib/VMCore/AsmWriter.cpp Wed Aug 12 15:56:03 2009
@@ -67,7 +67,7 @@
 // PrintEscapedString - Print each character of the specified string, escaping
 // it if it is not printable or if it is an escape char.
 static void PrintEscapedString(const StringRef &Name,
-                               formatted_raw_ostream &Out) {
+                               raw_ostream &Out) {
   for (unsigned i = 0, e = Name.size(); i != e; ++i) {
     unsigned char C = Name[i];
     if (isprint(C) && C != '\\' && C != '"')
@@ -87,7 +87,7 @@
 /// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
 /// prefixed with % (if the string only contains simple characters) or is
 /// surrounded with ""'s (if it has special chars in it).  Print it out.
-static void PrintLLVMName(formatted_raw_ostream &OS, const StringRef &Name,
+static void PrintLLVMName(raw_ostream &OS, const StringRef &Name,
                           PrefixType Prefix) {
   assert(Name.data() && "Cannot get empty name!");
   switch (Prefix) {
@@ -126,7 +126,7 @@
 /// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
 /// prefixed with % (if the string only contains simple characters) or is
 /// surrounded with ""'s (if it has special chars in it).  Print it out.
-static void PrintLLVMName(formatted_raw_ostream &OS, const Value *V) {
+static void PrintLLVMName(raw_ostream &OS, const Value *V) {
   PrintLLVMName(OS, V->getName(), 
                 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
 }
@@ -817,7 +817,7 @@
 // AsmWriter Implementation
 //===----------------------------------------------------------------------===//
 
-static void WriteAsOperandInternal(formatted_raw_ostream &Out, const Value *V,
+static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
                                    TypePrinting &TypePrinter,
                                    SlotTracker *Machine);
 
@@ -889,7 +889,7 @@
   }
 }
 
-static void WriteOptimizationInfo(formatted_raw_ostream &Out, const User *U) {
+static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
   if (const OverflowingBinaryOperator *OBO =
         dyn_cast<OverflowingBinaryOperator>(U)) {
     if (OBO->hasNoUnsignedOverflow())
@@ -905,7 +905,7 @@
   }
 }
 
-static void WriteConstantInt(formatted_raw_ostream &Out, const Constant *CV,
+static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
                              TypePrinting &TypePrinter, SlotTracker *Machine) {
   if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
     if (CI->getType() == Type::Int1Ty) {
@@ -1146,7 +1146,7 @@
 /// ostream.  This can be useful when you just want to print int %reg126, not
 /// the whole instruction that generated it.
 ///
-static void WriteAsOperandInternal(formatted_raw_ostream &Out, const Value *V,
+static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
                                    TypePrinting &TypePrinter,
                                    SlotTracker *Machine) {
   if (V->hasName()) {
@@ -1224,8 +1224,8 @@
   WriteAsOperand(OS, V, PrintType, Context);
 }
 
-void llvm::WriteAsOperand(raw_ostream &Out, const Value *V, bool PrintType,
-                          const Module *Context) {
+void llvm::WriteAsOperand(raw_ostream &Out, const Value *V,
+                          bool PrintType, const Module *Context) {
   if (Context == 0) Context = getModuleFromVal(V);
 
   TypePrinting TypePrinter;
@@ -1236,11 +1236,9 @@
     Out << ' ';
   }
 
-  formatted_raw_ostream FOut(Out);
-  WriteAsOperandInternal(FOut, V, TypePrinter, 0);
+  WriteAsOperandInternal(Out, V, TypePrinter, 0);
 }
 
-
 namespace {
 
 class AssemblyWriter {
@@ -2003,9 +2001,14 @@
 }
 void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
   SlotTracker SlotTable(this);
+  size_t OldBufferSize = ROS.GetBufferSize();
   formatted_raw_ostream OS(ROS);
   AssemblyWriter W(OS, SlotTable, this, AAW);
   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 {
@@ -2022,11 +2025,12 @@
 }
 
 void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
-  formatted_raw_ostream OS(ROS);
   if (this == 0) {
-    OS << "printing a <null> value\n";
+    ROS << "printing a <null> value\n";
     return;
   }
+  size_t OldBufferSize = ROS.GetBufferSize();
+  formatted_raw_ostream OS(ROS);
   if (const Instruction *I = dyn_cast<Instruction>(this)) {
     const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
     SlotTracker SlotTable(F);
@@ -2081,6 +2085,10 @@
   } else {
     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 {





More information about the llvm-commits mailing list