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>
56 typedef DOTGraphTraits<GraphType> DOTTraits;
57 typedef GraphTraits<GraphType> GTraits;
58 typedef typename GTraits::NodeType NodeType;
59 typedef typename GTraits::nodes_iterator node_iterator;
60 typedef typename GTraits::ChildIteratorType child_iterator;
62 GraphWriter(std::ostream &o, const GraphType &g,
63 const std::string &Name) : O(o), G(g) {
65 O << "digraph foo {\n"; // Graph name doesn't matter
67 O << "digraph " << Name << " {\n";
69 std::string GraphName = DOTTraits::getGraphName(G);
70 if (!GraphName.empty())
71 O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
72 O << DOTTraits::getGraphProperties(G);
75 // Emit all of the nodes in the graph...
78 // Output any customizations on the graph
79 DOTTraits::addCustomGraphFeatures(G, *this);
83 // Finish off the graph
88 // Loop over the graph, printing it out...
89 for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
94 void writeNode(NodeType *Node) {
95 std::string NodeAttributes = DOTTraits::getNodeAttributes(Node);
97 O << "\tNode" << (void*)Node << " [shape=record,";
98 if (!NodeAttributes.empty()) O << NodeAttributes << ",";
100 << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G));
102 // Print out the fields of the current node...
103 child_iterator EI = GTraits::child_begin(Node);
104 child_iterator EE = GTraits::child_end(Node);
108 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
110 O << "<g" << i << ">" << DOTTraits::getEdgeSourceLabel(Node, EI);
114 O << "|truncated...";
117 O << "}\"];\n"; // Finish printing the "node" line
119 // Output all of the edges now
120 EI = GTraits::child_begin(Node);
121 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
122 writeEdge(Node, i, EI);
125 void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) {
126 if (NodeType *TargetNode = *EI) {
128 if (DOTTraits::edgeTargetsEdgeSource(Node, EI)) {
129 child_iterator TargetIt = DOTTraits::getEdgeTarget(Node, EI);
131 // Figure out which edge this targets...
132 unsigned Offset = std::distance(GTraits::child_begin(TargetNode),
134 if (Offset > 64) Offset = 64; // Targetting the truncated part?
135 DestPort = (int)Offset;
138 emitEdge((void *)Node, edgeidx, (void*)TargetNode, DestPort,
139 DOTTraits::getEdgeAttributes(Node, EI));
143 /// emitSimpleNode - Outputs a simple (non-record) node
144 void emitSimpleNode(void *ID, const std::string &Attr,
145 const std::string &Label) {
146 O << "\tNode" << ID << "[ ";
149 O << " label =\"" << DOT::EscapeString(Label) << "\"];\n";
152 /// emitEdge - Output an edge from a simple node into the graph...
153 void emitEdge(void *SrcNodeID, int SrcNodePort,
154 void *DestNodeID, int DestNodePort, const std::string &Attrs) {
155 O << "\tNode" << SrcNodeID;
156 if (SrcNodePort >= 0)
157 O << ":g" << SrcNodePort;
158 O << " -> Node" << (void*)DestNodeID;
159 if (DestNodePort >= 0)
160 O << ":g" << DestNodePort;
163 O << "[" << Attrs << "]";
168 template<typename GraphType>
169 std::ostream &WriteGraph(std::ostream &O, const GraphType &G,
170 const std::string &Name = "") {
171 // Start the graph emission process...
172 GraphWriter<GraphType> W(O, G, Name);