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"
24 namespace DOT { // Private functions...
25 inline std::string EscapeString(const std::string &Label) {
26 std::string Str(Label);
27 for (unsigned i = 0; i != Str.length(); ++i)
30 Str.insert(Str.begin()+i, '\\'); // Escape character...
35 Str.insert(Str.begin()+i, ' '); // Convert to two spaces
40 if (i+1 != Str.length() && Str[i+1] == 'l')
41 break; // don't disturb \l
44 Str.insert(Str.begin()+i, '\\'); // Escape character...
45 ++i; // don't infinite loop
52 template<typename GraphType>
57 typedef DOTGraphTraits<GraphType> DOTTraits;
58 typedef GraphTraits<GraphType> GTraits;
59 typedef typename GTraits::NodeType NodeType;
60 typedef typename GTraits::nodes_iterator node_iterator;
61 typedef typename GTraits::ChildIteratorType child_iterator;
63 GraphWriter(std::ostream &o, const GraphType &g) : O(o), G(g) {}
65 void writeHeader(const std::string &Name) {
67 O << "digraph foo {\n"; // Graph name doesn't matter
69 O << "digraph " << Name << " {\n";
71 std::string GraphName = DOTTraits::getGraphName(G);
72 if (!GraphName.empty())
73 O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
74 O << DOTTraits::getGraphProperties(G);
79 // Finish off the graph
84 // Loop over the graph, printing it out...
85 for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
90 void writeNode(NodeType *Node) {
91 std::string NodeAttributes = DOTTraits::getNodeAttributes(Node);
93 O << "\tNode" << (void*)Node << " [shape=record,";
94 if (!NodeAttributes.empty()) O << NodeAttributes << ",";
96 << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G));
98 // Print out the fields of the current node...
99 child_iterator EI = GTraits::child_begin(Node);
100 child_iterator EE = GTraits::child_end(Node);
104 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
106 O << "<g" << i << ">" << DOTTraits::getEdgeSourceLabel(Node, EI);
110 O << "|truncated...";
113 O << "}\"];\n"; // Finish printing the "node" line
115 // Output all of the edges now
116 EI = GTraits::child_begin(Node);
117 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
118 writeEdge(Node, i, EI);
121 void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) {
122 if (NodeType *TargetNode = *EI) {
124 if (DOTTraits::edgeTargetsEdgeSource(Node, EI)) {
125 child_iterator TargetIt = DOTTraits::getEdgeTarget(Node, EI);
127 // Figure out which edge this targets...
128 unsigned Offset = std::distance(GTraits::child_begin(TargetNode),
130 DestPort = (int)Offset;
133 emitEdge((void *)Node, edgeidx, (void*)TargetNode, DestPort,
134 DOTTraits::getEdgeAttributes(Node, EI));
138 /// emitSimpleNode - Outputs a simple (non-record) node
139 void emitSimpleNode(const void *ID, const std::string &Attr,
140 const std::string &Label, unsigned NumEdgeSources = 0,
141 const std::vector<std::string> *EdgeSourceLabels = 0) {
142 O << "\tNode" << ID << "[ ";
146 if (NumEdgeSources) O << "{";
147 O << DOT::EscapeString(Label);
148 if (NumEdgeSources) {
151 for (unsigned i = 0; i != NumEdgeSources; ++i) {
153 O << "<g" << i << ">";
154 if (EdgeSourceLabels) O << (*EdgeSourceLabels)[i];
161 /// emitEdge - Output an edge from a simple node into the graph...
162 void emitEdge(const void *SrcNodeID, int SrcNodePort,
163 const void *DestNodeID, int DestNodePort,
164 const std::string &Attrs) {
165 if (SrcNodePort > 64) return; // Eminating from truncated part?
166 if (DestNodePort > 64) DestNodePort = 64; // Targetting the truncated part?
168 O << "\tNode" << SrcNodeID;
169 if (SrcNodePort >= 0)
170 O << ":g" << SrcNodePort;
171 O << " -> Node" << (void*)DestNodeID;
172 if (DestNodePort >= 0)
173 O << ":g" << DestNodePort;
176 O << "[" << Attrs << "]";
181 template<typename GraphType>
182 std::ostream &WriteGraph(std::ostream &O, const GraphType &G,
183 const std::string &Name = "") {
184 // Start the graph emission process...
185 GraphWriter<GraphType> W(O, G);
187 // Output the header for the graph...
190 // Emit all of the nodes in the graph...
193 // Output any customizations on the graph
194 DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W);
196 // Output the end of the graph