summaryrefslogtreecommitdiffstats
path: root/include/llvm/Support/GraphWriter.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/llvm/Support/GraphWriter.h')
-rw-r--r--include/llvm/Support/GraphWriter.h107
1 files changed, 43 insertions, 64 deletions
diff --git a/include/llvm/Support/GraphWriter.h b/include/llvm/Support/GraphWriter.h
index 01b44d0..bd3fcea 100644
--- a/include/llvm/Support/GraphWriter.h
+++ b/include/llvm/Support/GraphWriter.h
@@ -24,53 +24,33 @@
#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 {
+ enum Name {
+ DOT,
+ FDP,
+ NEATO,
+ TWOPI,
+ CIRCO
+ };
}
-void DisplayGraph(const sys::Path& Filename);
+void DisplayGraph(const sys::Path& Filename, bool wait=true, GraphProgram::Name program = GraphProgram::DOT);
template<typename GraphType>
class GraphWriter {
- std::ostream &O;
+ raw_ostream &O;
const GraphType &G;
bool ShortNames;
@@ -80,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) {
@@ -222,7 +202,7 @@ public:
for (unsigned i = 0; i != NumEdgeSources; ++i) {
if (i) O << "|";
- O << "<g" << i << ">";
+ O << "<s" << i << ">";
if (EdgeSourceLabels) O << (*EdgeSourceLabels)[i];
}
O << "}}";
@@ -241,8 +221,12 @@ public:
if (SrcNodePort >= 0)
O << ":s" << SrcNodePort;
O << " -> Node" << DestNodeID;
- if (DestNodePort >= 0)
- O << ":d" << DestNodePort;
+ if (DestNodePort >= 0) {
+ if (DOTTraits::hasEdgeDestLabels())
+ O << ":d" << DestNodePort;
+ else
+ O << ":s" << DestNodePort;
+ }
if (!Attrs.empty())
O << "[" << Attrs << "]";
@@ -251,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);
@@ -273,33 +257,30 @@ std::ostream &WriteGraph(std::ostream &O, const GraphType &G,
}
template<typename GraphType>
-sys::Path WriteGraph(const GraphType &G,
- const std::string& Name,
- bool ShortNames = false,
- const std::string& Title = "") {
+sys::Path WriteGraph(const GraphType &G, const std::string &Name,
+ bool ShortNames = false, const std::string &Title = "") {
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.str() << "'... ";
- std::ofstream O(Filename.c_str());
+ std::string ErrorInfo;
+ raw_fd_ostream O(Filename.c_str(), ErrorInfo);
- 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.str() << "' for writing!\n";
Filename.clear();
}
@@ -310,17 +291,15 @@ sys::Path WriteGraph(const GraphType &G,
/// then cleanup. For use from the debugger.
///
template<typename GraphType>
-void ViewGraph(const GraphType& G,
- const std::string& Name,
- bool ShortNames = false,
- const std::string& Title = "") {
- sys::Path Filename = WriteGraph(G, Name, ShortNames, Title);
+void ViewGraph(const GraphType &G, const std::string &Name,
+ bool ShortNames = false, const std::string &Title = "",
+ GraphProgram::Name Program = GraphProgram::DOT) {
+ sys::Path Filename = WriteGraph(G, Name, ShortNames, Title);
- if (Filename.isEmpty()) {
+ if (Filename.isEmpty())
return;
- }
- DisplayGraph(Filename);
+ DisplayGraph(Filename, true, Program);
}
} // End llvm namespace
OpenPOWER on IntegriCloud