switch from std::string to SmallString + raw_svector_ostream.
[oota-llvm.git] / lib / CompilerDriver / CompilationGraph.cpp
index 6112cfe1b52b5b9f15609a046a1cd78c3f36ddfd..bb0eb7bcf197fe2a48f7a96acb285799921c17dc 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/CompilerDriver/BuiltinOptions.h"
 #include "llvm/CompilerDriver/CompilationGraph.h"
 #include "llvm/CompilerDriver/Error.h"
 
 #include "llvm/ADT/STLExtras.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/DOTGraphTraits.h"
 #include "llvm/Support/GraphWriter.h"
+#include "llvm/Support/raw_ostream.h"
 
 #include <algorithm>
 #include <cstring>
-#include <iostream>
 #include <iterator>
 #include <limits>
 #include <queue>
@@ -30,9 +30,6 @@
 using namespace llvm;
 using namespace llvmc;
 
-extern cl::list<std::string> InputFilenames;
-extern cl::list<std::string> Languages;
-
 namespace llvmc {
 
   const std::string& LanguageMap::GetLanguage(const sys::Path& File) const {
@@ -349,8 +346,8 @@ int CompilationGraph::CheckLanguageNames() const {
 
         if (!N2.ToolPtr) {
           ++ret;
-          std::cerr << "Error: there is an edge from '" << N1.ToolPtr->Name()
-                    << "' back to the root!\n\n";
+          errs() << "Error: there is an edge from '" << N1.ToolPtr->Name()
+                 << "' back to the root!\n\n";
           continue;
         }
 
@@ -366,17 +363,17 @@ int CompilationGraph::CheckLanguageNames() const {
 
         if (!eq) {
           ++ret;
-          std::cerr << "Error: Output->input language mismatch in the edge '" <<
-            N1.ToolPtr->Name() << "' -> '" << N2.ToolPtr->Name() << "'!\n";
-
-          std::cerr << "Expected one of { ";
+          errs() << "Error: Output->input language mismatch in the edge '"
+                 << N1.ToolPtr->Name() << "' -> '" << N2.ToolPtr->Name()
+                 << "'!\n"
+                 << "Expected one of { ";
 
           InLangs = N2.ToolPtr->InputLanguages();
           for (;*InLangs; ++InLangs) {
-            std::cerr << '\'' << *InLangs << (*(InLangs+1) ? "', " : "'");
+            errs() << '\'' << *InLangs << (*(InLangs+1) ? "', " : "'");
           }
 
-          std::cerr << " }, but got '" << OutLang << "'!\n\n";
+          errs() << " }, but got '" << OutLang << "'!\n\n";
         }
 
       }
@@ -409,9 +406,8 @@ int CompilationGraph::CheckMultipleDefaultEdges() const {
       }
       else if (EdgeWeight == MaxWeight) {
         ++ret;
-        std::cerr
-          << "Error: there are multiple maximal edges stemming from the '"
-          << N.ToolPtr->Name() << "' node!\n\n";
+        errs() << "Error: there are multiple maximal edges stemming from the '"
+               << N.ToolPtr->Name() << "' node!\n\n";
         break;
       }
     }
@@ -443,9 +439,9 @@ int CompilationGraph::CheckCycles() {
   }
 
   if (deleted != NodesMap.size()) {
-    std::cerr << "Error: there are cycles in the compilation graph!\n"
-              << "Try inspecting the diagram produced by "
-      "'llvmc --view-graph'.\n\n";
+    errs() << "Error: there are cycles in the compilation graph!\n"
+           << "Try inspecting the diagram produced by "
+           << "'llvmc --view-graph'.\n\n";
     return 1;
   }
 
@@ -465,9 +461,6 @@ int CompilationGraph::Check () {
   // Check for cycles.
   ret += this->CheckCycles();
 
-  if (!ret)
-    std::cerr << "check-graph: no errors found.\n";
-
   return ret;
 }
 
@@ -480,7 +473,8 @@ namespace llvm {
   {
 
     template<typename GraphType>
-    static std::string getNodeLabel(const Node* N, const GraphType&)
+    static std::string getNodeLabel(const Node* N, const GraphType&,
+                                    bool ShortNames)
     {
       if (N->ToolPtr)
         if (N->ToolPtr->IsJoin())
@@ -519,18 +513,18 @@ namespace llvm {
 
 }
 
-void CompilationGraph::writeGraph() {
-  std::ofstream O("compilation-graph.dot");
+void CompilationGraph::writeGraph(const std::string& OutputFilename) {
+  std::string ErrorInfo;
+  raw_fd_ostream O(OutputFilename.c_str(), ErrorInfo);
 
-  if (O.good()) {
-    std::cerr << "Writing 'compilation-graph.dot' file...";
+  if (ErrorInfo.empty()) {
+    errs() << "Writing '"<< OutputFilename << "' file...";
     llvm::WriteGraph(O, this);
-    std::cerr << "done.\n";
-    O.close();
+    errs() << "done.\n";
   }
   else {
-    throw std::runtime_error("Error opening file 'compilation-graph.dot'"
-                             " for writing!");
+    throw std::runtime_error("Error opening file '" + OutputFilename
+                             + "' for writing!");
   }
 }