1 //===-- Support/GraphWriter.h - Write a graph to a .dot file ---*- C++ -*--===//
3 // This file defines a simple interface that can be used to print out generic
4 // LLVM graphs to ".dot" files. "dot" is a tool that is part of the AT&T
5 // graphviz package (http://www.research.att.com/sw/tools/graphviz/) which can
6 // be used to turn the files output by this interface into a variety of
7 // different graphics formats.
9 // Graphs do not need to implement any interface past what is already required
10 // by the GraphTraits template, but they can choose to implement specializations
11 // of the DOTGraphTraits template if they want to customize the graphs output in
14 //===----------------------------------------------------------------------===//
16 #ifndef SUPPORT_GRAPHWRITER_H
17 #define SUPPORT_GRAPHWRITER_H
19 #include "Support/DOTGraphTraits.h"
20 #include "Support/GraphTraits.h"
23 namespace DOT { // Private functions...
24 inline std::string EscapeString(const std::string &Label) {
25 std::string Str(Label);
26 for (unsigned i = 0; i != Str.length(); ++i)
29 Str.insert(Str.begin()+i, '\\'); // Escape character...
34 Str.insert(Str.begin()+i, ' '); // Convert to two spaces
39 if (i+1 != Str.length() && Str[i+1] == 'l')
40 break; // don't disturb \l
43 Str.insert(Str.begin()+i, '\\'); // Escape character...
44 ++i; // don't infinite loop
51 template<typename GraphType>
52 std::ostream &WriteGraph(std::ostream &O, const GraphType &G) {
53 typedef DOTGraphTraits<GraphType> DOTTraits;
54 typedef GraphTraits<GraphType> GTraits;
55 typedef typename GTraits::NodeType NodeType;
56 typedef typename GTraits::nodes_iterator node_iterator;
58 O << "digraph foo {\n"; // Graph name doesn't matter
59 std::string GraphName = DOTTraits::getGraphName(G);
60 if (!GraphName.empty())
61 O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
62 O << DOTTraits::getGraphProperties(G);
65 // Loop over the graph in DFO, printing it out...
66 for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
70 std::string NodeAttributes = DOTTraits::getNodeAttributes(Node);
72 O << "\tNode" << (void*)Node << " [shape=record,";
73 if (!NodeAttributes.empty()) O << NodeAttributes << ",";
75 << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G));
77 // Print out the fields of the current node...
78 typename GTraits::ChildIteratorType EI = GTraits::child_begin(Node);
79 typename GTraits::ChildIteratorType EE = GTraits::child_end(Node);
83 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
85 O << "<g" << i << ">" << DOTTraits::getEdgeSourceLabel(Node, EI);
92 O << "}\"];\n"; // Finish printing the "node" line
94 // Output all of the edges now
95 EI = GTraits::child_begin(Node);
96 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
97 if (NodeType *TargetNode = *EI) {
98 O << "\tNode" << (void*)Node << ":g" << i << " -> Node"
100 if (DOTTraits::edgeTargetsEdgeSource(Node, EI)) {
101 typename GTraits::ChildIteratorType TargetIt =
102 DOTTraits::getEdgeTarget(Node, EI);
103 // Figure out which edge this targets...
104 unsigned Offset = std::distance(GTraits::child_begin(TargetNode),
106 if (Offset > 64) Offset = 64; // Targetting the trancated part?
110 std::string EdgeAttributes = DOTTraits::getEdgeAttributes(Node, EI);
111 if (!EdgeAttributes.empty())
112 O << "[" << EdgeAttributes << "]";
118 // Finish off the graph