convert LoopInfo.h and GraphWriter.h to use raw_ostream
authorChris Lattner <sabre@nondot.org>
Sun, 23 Aug 2009 07:19:13 +0000 (07:19 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 23 Aug 2009 07:19:13 +0000 (07:19 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79836 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Analysis/LoopInfo.h
include/llvm/Support/Casting.h
include/llvm/Support/GraphWriter.h
lib/Analysis/CFGPrinter.cpp
lib/Analysis/LoopPass.cpp
lib/CodeGen/AsmPrinter/AsmPrinter.cpp
lib/CodeGen/PostRASchedulerList.cpp
lib/CompilerDriver/CompilationGraph.cpp
lib/Support/GraphWriter.cpp
lib/Transforms/IPO/LoopExtractor.cpp

index 3a09e7b058586ecd8b546a6d6d6d76ec82d6b201..a31ace76e152b0f4ddb4ce2352092ea8439d787f 100644 (file)
@@ -36,9 +36,8 @@
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Analysis/Dominators.h"
 #include "llvm/Support/CFG.h"
-#include "llvm/Support/Streams.h"
+#include "llvm/Support/raw_ostream.h"
 #include <algorithm>
-#include <ostream>
 
 namespace llvm {
 
index 48988f8a6bb8354c71326e634364acbb2895b4b2..35fb29ec6cb3bc60aa376c23c868f2ed47cba8e1 100644 (file)
@@ -235,7 +235,7 @@ inline typename cast_retty<X, Y>::ret_type dyn_cast_or_null(const Y &Val) {
 
 
 #ifdef DEBUG_CAST_OPERATORS
-#include "llvm/Support/Streams.h"
+#include "llvm/Support/raw_ostream.h"
 
 struct bar {
   bar() {}
@@ -251,7 +251,7 @@ struct foo {
 };
 
 template <> inline bool isa_impl<foo,bar>(const bar &Val) {
-  cerr << "Classof: " << &Val << "\n";
+  errs() << "Classof: " << &Val << "\n";
   return true;
 }
 
index 0bb362f8673528db4c3e9b3141349606c1f30d01..f77b3db673cbe76f8fb80b8abd640e098e354ae6 100644 (file)
 #define LLVM_SUPPORT_GRAPHWRITER_H
 
 #include "llvm/Support/DOTGraphTraits.h"
-#include "llvm/Support/Streams.h"
+#include "llvm/Support/raw_ostream.h"
 #include "llvm/ADT/GraphTraits.h"
 #include "llvm/System/Path.h"
-#include <fstream>
 #include <vector>
 #include <cassert>
 
 namespace llvm {
 
 namespace DOT {  // Private functions...
-  inline std::string EscapeString(const std::string &Label) {
-    std::string Str(Label);
-    for (unsigned i = 0; i != Str.length(); ++i)
-      switch (Str[i]) {
-      case '\n':
-        Str.insert(Str.begin()+i, '\\');  // Escape character...
-        ++i;
-        Str[i] = 'n';
-        break;
-      case '\t':
-        Str.insert(Str.begin()+i, ' ');  // Convert to two spaces
-        ++i;
-        Str[i] = ' ';
-        break;
-      case '\\':
-        if (i+1 != Str.length())
-          switch (Str[i+1]) {
-            case 'l': continue; // don't disturb \l
-            case '|': case '{': case '}':
-               Str.erase(Str.begin()+i); continue;
-            default: break;
-          }
-      case '{': case '}':
-      case '<': case '>':
-      case '|': case '"':
-        Str.insert(Str.begin()+i, '\\');  // Escape character...
-        ++i;  // don't infinite loop
-        break;
-      }
-    return Str;
-  }
+  std::string EscapeString(const std::string &Label);
 }
 
 namespace GraphProgram {
@@ -81,7 +50,7 @@ void DisplayGraph(const sys::Path& Filename, bool wait=true, GraphProgram::Name
 
 template<typename GraphType>
 class GraphWriter {
-  std::ostream &O;
+  raw_ostream &O;
   const GraphType &G;
   bool ShortNames;
 
@@ -91,7 +60,7 @@ class GraphWriter {
   typedef typename GTraits::nodes_iterator    node_iterator;
   typedef typename GTraits::ChildIteratorType child_iterator;
 public:
-  GraphWriter(std::ostream &o, const GraphType &g, bool SN) :
+  GraphWriter(raw_ostream &o, const GraphType &g, bool SN) :
     O(o), G(g), ShortNames(SN) {}
 
   void writeHeader(const std::string &Name) {
@@ -266,10 +235,10 @@ public:
 };
 
 template<typename GraphType>
-std::ostream &WriteGraph(std::ostream &O, const GraphType &G,
-                         bool ShortNames = false,
-                         const std::string &Name = "",
-                         const std::string &Title = "") {
+raw_ostream &WriteGraph(raw_ostream &O, const GraphType &G,
+                        bool ShortNames = false,
+                        const std::string &Name = "",
+                        const std::string &Title = "") {
   // Start the graph emission process...
   GraphWriter<GraphType> W(O, G, ShortNames);
 
@@ -295,26 +264,25 @@ sys::Path WriteGraph(const GraphType &G,
   std::string ErrMsg;
   sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
   if (Filename.isEmpty()) {
-    cerr << "Error: " << ErrMsg << "\n";
+    errs() << "Error: " << ErrMsg << "\n";
     return Filename;
   }
   Filename.appendComponent(Name + ".dot");
   if (Filename.makeUnique(true,&ErrMsg)) {
-    cerr << "Error: " << ErrMsg << "\n";
+    errs() << "Error: " << ErrMsg << "\n";
     return sys::Path();
   }
 
-  cerr << "Writing '" << Filename << "'... ";
+  errs() << "Writing '" << Filename << "'... ";
 
-  std::ofstream O(Filename.c_str());
+  std::string ErrorInfo;
+  raw_fd_ostream O(Filename.c_str(), ErrorInfo, raw_fd_ostream::F_Force);
 
-  if (O.good()) {
+  if (ErrorInfo.empty()) {
     WriteGraph(O, G, ShortNames, Name, Title);
-    cerr << " done. \n";
-
-    O.close();
+    errs() << " done. \n";
   } else {
-    cerr << "error opening file for writing!\n";
+    errs() << "error opening file '" << Filename << "' for writing!\n";
     Filename.clear();
   }
 
index f6ecfc596f55000eb069133e29701c03c6c7ace2..4ac6b8d8e5dd27d67582ac82381efcd80a972e4f 100644 (file)
@@ -25,7 +25,6 @@
 #include "llvm/Support/CFG.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/GraphWriter.h"
-#include "llvm/Config/config.h"
 using namespace llvm;
 
 namespace llvm {
@@ -136,14 +135,16 @@ namespace {
 
     virtual bool runOnFunction(Function &F) {
       std::string Filename = "cfg." + F.getNameStr() + ".dot";
-      cerr << "Writing '" << Filename << "'...";
-      std::ofstream File(Filename.c_str());
+      errs() << "Writing '" << Filename << "'...";
+      
+      std::string ErrorInfo;
+      raw_fd_ostream File(Filename.c_str(), ErrorInfo, raw_fd_ostream::F_Force);
 
-      if (File.good())
+      if (ErrorInfo.empty())
         WriteGraph(File, (const Function*)&F);
       else
-        cerr << "  error opening file for writing!";
-      cerr << "\n";
+        errs() << "  error opening file for writing!";
+      errs() << "\n";
       return false;
     }
 
@@ -166,14 +167,16 @@ namespace {
     explicit CFGOnlyPrinter(void *pid) : FunctionPass(pid) {}
     virtual bool runOnFunction(Function &F) {
       std::string Filename = "cfg." + F.getNameStr() + ".dot";
-      cerr << "Writing '" << Filename << "'...";
-      std::ofstream File(Filename.c_str());
+      errs() << "Writing '" << Filename << "'...";
 
-      if (File.good())
+      std::string ErrorInfo;
+      raw_fd_ostream File(Filename.c_str(), ErrorInfo, raw_fd_ostream::F_Force);
+      
+      if (ErrorInfo.empty())
         WriteGraph(File, (const Function*)&F, true);
       else
-        cerr << "  error opening file for writing!";
-      cerr << "\n";
+        errs() << "  error opening file for writing!";
+      errs() << "\n";
       return false;
     }
     void print(raw_ostream &OS, const Module* = 0) const {}
index ee03556f27412d98f3e1fafa9afcec7ec65ffeaf..0007aded2b51f68caed7b9be26c0f226cc5f5151 100644 (file)
@@ -272,7 +272,7 @@ bool LPPassManager::runOnFunction(Function &F) {
 
 /// Print passes managed by this manager
 void LPPassManager::dumpPassStructure(unsigned Offset) {
-  llvm::cerr << std::string(Offset*2, ' ') << "Loop Pass Manager\n";
+  errs().indent(Offset*2) << "Loop Pass Manager\n";
   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
     Pass *P = getContainedPass(Index);
     P->dumpPassStructure(Offset + 1);
index c3175dd67509c5c6f206caca704b581efb5c4df1..c8099c8b18880dfe92e9aca5f1df66efcbe278f7 100644 (file)
@@ -1724,7 +1724,7 @@ GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
       return GMP;
     }
   
-  cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
+  errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
   llvm_unreachable(0);
 }
 
index 320e6b4eec13b10c66270cd608398e2c7b2f6593..5042415c2cf37ced76d12ef9a9fd9cef25379f18 100644 (file)
@@ -765,9 +765,9 @@ void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
   
 #ifndef NDEBUG
   if (SuccSU->NumPredsLeft < 0) {
-    cerr << "*** Scheduling failed! ***\n";
+    errs() << "*** Scheduling failed! ***\n";
     SuccSU->dump(this);
-    cerr << " has been released too many times!\n";
+    errs() << " has been released too many times!\n";
     llvm_unreachable(0);
   }
 #endif
index f3039433b0314b11c470da857d096ae776ef0da9..bb0eb7bcf197fe2a48f7a96acb285799921c17dc 100644 (file)
@@ -514,13 +514,13 @@ namespace llvm {
 }
 
 void CompilationGraph::writeGraph(const std::string& OutputFilename) {
-  std::ofstream O(OutputFilename.c_str());
+  std::string ErrorInfo;
+  raw_fd_ostream O(OutputFilename.c_str(), ErrorInfo);
 
-  if (O.good()) {
+  if (ErrorInfo.empty()) {
     errs() << "Writing '"<< OutputFilename << "' file...";
     llvm::WriteGraph(O, this);
     errs() << "done.\n";
-    O.close();
   }
   else {
     throw std::runtime_error("Error opening file '" + OutputFilename
index 4ec0bf86e5691e9c926a7a4fe8763d1ab5ab6648..9d72db1bd2d29ec40e759d0a21ff4e1ff634684d 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/GraphWriter.h"
-#include "llvm/Support/Streams.h"
 #include "llvm/System/Path.h"
 #include "llvm/System/Program.h"
 #include "llvm/Config/config.h"
 using namespace llvm;
 
+std::string llvm::DOT::EscapeString(const std::string &Label) {
+  std::string Str(Label);
+  for (unsigned i = 0; i != Str.length(); ++i)
+  switch (Str[i]) {
+    case '\n':
+      Str.insert(Str.begin()+i, '\\');  // Escape character...
+      ++i;
+      Str[i] = 'n';
+      break;
+    case '\t':
+      Str.insert(Str.begin()+i, ' ');  // Convert to two spaces
+      ++i;
+      Str[i] = ' ';
+      break;
+    case '\\':
+      if (i+1 != Str.length())
+        switch (Str[i+1]) {
+          case 'l': continue; // don't disturb \l
+          case '|': case '{': case '}':
+            Str.erase(Str.begin()+i); continue;
+          default: break;
+        }
+    case '{': case '}':
+    case '<': case '>':
+    case '|': case '"':
+      Str.insert(Str.begin()+i, '\\');  // Escape character...
+      ++i;  // don't infinite loop
+      break;
+  }
+  return Str;
+}
+
+
+
 void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
                         GraphProgram::Name program) {
   std::string ErrMsg;
@@ -29,13 +62,11 @@ void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
   args.push_back(Filename.c_str());
   args.push_back(0);
   
-  cerr << "Running 'Graphviz' program... ";
-  if (sys::Program::ExecuteAndWait(Graphviz, &args[0],0,0,0,0,&ErrMsg)) {
-     cerr << "Error viewing graph " << Filename << ": " << ErrMsg << "\n";
-  }
-  else {
+  errs() << "Running 'Graphviz' program... ";
+  if (sys::Program::ExecuteAndWait(Graphviz, &args[0],0,0,0,0,&ErrMsg))
+     errs() << "Error viewing graph " << Filename << ": " << ErrMsg << "\n";
+  else
      Filename.eraseFromDisk();
-  }
 
 #elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
                    HAVE_TWOPI || HAVE_CIRCO))
@@ -98,12 +129,12 @@ void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
   args.push_back(PSFilename.c_str());
   args.push_back(0);
   
-  cerr << "Running '" << prog << "' program... ";
+  errs() << "Running '" << prog << "' program... ";
 
   if (sys::Program::ExecuteAndWait(prog, &args[0],0,0,0,0,&ErrMsg)) {
-     cerr << "Error viewing graph " << Filename << ": '" << ErrMsg << "\n";
+     errs() << "Error viewing graph " << Filename << ": '" << ErrMsg << "\n";
   } else {
-    cerr << " done. \n";
+    errs() << " done. \n";
 
     sys::Path gv(LLVM_PATH_GV);
     args.clear();
@@ -114,15 +145,15 @@ void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
     
     ErrMsg.clear();
     if (wait) {
-       if (sys::Program::ExecuteAndWait(gv, &args[0],0,0,0,0,&ErrMsg)) {
-          cerr << "Error viewing graph: " << ErrMsg << "\n";
-       }
+       if (sys::Program::ExecuteAndWait(gv, &args[0],0,0,0,0,&ErrMsg))
+          errs() << "Error viewing graph: " << ErrMsg << "\n";
        Filename.eraseFromDisk();
        PSFilename.eraseFromDisk();
     }
     else {
        sys::Program::ExecuteNoWait(gv, &args[0],0,0,0,&ErrMsg);
-       cerr << "Remember to erase graph files: " << Filename << " " << PSFilename << "\n";
+       errs() << "Remember to erase graph files: " << Filename << " "
+              << PSFilename << "\n";
     }
   }
 #elif HAVE_DOTTY
@@ -133,9 +164,9 @@ void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
   args.push_back(Filename.c_str());
   args.push_back(0);
   
-  cerr << "Running 'dotty' program... ";
+  errs() << "Running 'dotty' program... ";
   if (sys::Program::ExecuteAndWait(dotty, &args[0],0,0,0,0,&ErrMsg)) {
-     cerr << "Error viewing graph " << Filename << ": " << ErrMsg << "\n";
+     errs() << "Error viewing graph " << Filename << ": " << ErrMsg << "\n";
   } else {
 #ifdef __MINGW32__ // Dotty spawns another app and doesn't wait until it returns
     return;
index 0c654438d5086f79a0d55305cc44dc8b5701b759..4b0988441d98de33abe96f1e3e88bccfb84a181f 100644 (file)
@@ -193,8 +193,8 @@ void BlockExtractorPass::LoadFile(const char *Filename) {
   // Load the BlockFile...
   std::ifstream In(Filename);
   if (!In.good()) {
-    cerr << "WARNING: BlockExtractor couldn't load file '" << Filename
-         << "'!\n";
+    errs() << "WARNING: BlockExtractor couldn't load file '" << Filename
+           << "'!\n";
     return;
   }
   while (In) {