1 //===-- llvm/Support/GraphWriter.h - Write graph to a .dot file -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a simple interface that can be used to print out generic
11 // LLVM graphs to ".dot" files. "dot" is a tool that is part of the AT&T
12 // graphviz package (http://www.research.att.com/sw/tools/graphviz/) which can
13 // be used to turn the files output by this interface into a variety of
14 // different graphics formats.
16 // Graphs do not need to implement any interface past what is already required
17 // by the GraphTraits template, but they can choose to implement specializations
18 // of the DOTGraphTraits template if they want to customize the graphs output in
21 //===----------------------------------------------------------------------===//
23 #ifndef LLVM_SUPPORT_GRAPHWRITER_H
24 #define LLVM_SUPPORT_GRAPHWRITER_H
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/DOTGraphTraits.h"
28 #include "llvm/ADT/GraphTraits.h"
29 #include "llvm/System/Path.h"
35 namespace DOT { // Private functions...
36 inline std::string EscapeString(const std::string &Label) {
37 std::string Str(Label);
38 for (unsigned i = 0; i != Str.length(); ++i)
41 Str.insert(Str.begin()+i, '\\'); // Escape character...
46 Str.insert(Str.begin()+i, ' '); // Convert to two spaces
51 if (i+1 != Str.length())
53 case 'l': continue; // don't disturb \l
54 case '|': case '{': case '}':
55 Str.erase(Str.begin()+i); continue;
61 Str.insert(Str.begin()+i, '\\'); // Escape character...
62 ++i; // don't infinite loop
69 void DisplayGraph(const sys::Path& Filename);
71 template<typename GraphType>
76 typedef DOTGraphTraits<GraphType> DOTTraits;
77 typedef GraphTraits<GraphType> GTraits;
78 typedef typename GTraits::NodeType NodeType;
79 typedef typename GTraits::nodes_iterator node_iterator;
80 typedef typename GTraits::ChildIteratorType child_iterator;
82 GraphWriter(std::ostream &o, const GraphType &g) : O(o), G(g) {}
84 void writeHeader(const std::string &Name) {
86 O << "digraph foo {\n"; // Graph name doesn't matter
88 O << "digraph " << Name << " {\n";
90 if (DOTTraits::renderGraphFromBottomUp())
91 O << "\trankdir=\"BT\";\n";
93 std::string GraphName = DOTTraits::getGraphName(G);
94 if (!GraphName.empty())
95 O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
96 O << DOTTraits::getGraphProperties(G);
101 // Finish off the graph
106 // Loop over the graph, printing it out...
107 for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
112 void writeNode(NodeType& Node) {
116 void writeNode(NodeType *const *Node) {
120 void writeNode(NodeType *Node) {
121 std::string NodeAttributes = DOTTraits::getNodeAttributes(Node, G);
123 O << "\tNode" << reinterpret_cast<const void*>(Node) << " [shape=record,";
124 if (!NodeAttributes.empty()) O << NodeAttributes << ",";
127 if (!DOTTraits::renderGraphFromBottomUp()) {
128 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G));
130 // If we should include the address of the node in the label, do so now.
131 if (DOTTraits::hasNodeAddressLabel(Node, G))
132 O << "|" << (void*)Node;
135 // Print out the fields of the current node...
136 child_iterator EI = GTraits::child_begin(Node);
137 child_iterator EE = GTraits::child_end(Node);
139 if (!DOTTraits::renderGraphFromBottomUp()) O << "|";
142 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
144 O << "<g" << i << ">" << DOTTraits::getEdgeSourceLabel(Node, EI);
148 O << "|<g64>truncated...";
150 if (DOTTraits::renderGraphFromBottomUp()) O << "|";
153 if (DOTTraits::renderGraphFromBottomUp()) {
154 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G));
156 // If we should include the address of the node in the label, do so now.
157 if (DOTTraits::hasNodeAddressLabel(Node, G))
158 O << "|" << (void*)Node;
161 O << "}\"];\n"; // Finish printing the "node" line
163 // Output all of the edges now
164 EI = GTraits::child_begin(Node);
165 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
166 writeEdge(Node, i, EI);
167 for (; EI != EE; ++EI)
168 writeEdge(Node, 64, EI);
171 void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) {
172 if (NodeType *TargetNode = *EI) {
174 if (DOTTraits::edgeTargetsEdgeSource(Node, EI)) {
175 child_iterator TargetIt = DOTTraits::getEdgeTarget(Node, EI);
177 // Figure out which edge this targets...
178 unsigned Offset = std::distance(GTraits::child_begin(TargetNode),
180 DestPort = static_cast<int>(Offset);
183 emitEdge(reinterpret_cast<const void*>(Node), edgeidx,
184 reinterpret_cast<const void*>(TargetNode), DestPort,
185 DOTTraits::getEdgeAttributes(Node, EI));
189 /// emitSimpleNode - Outputs a simple (non-record) node
190 void emitSimpleNode(const void *ID, const std::string &Attr,
191 const std::string &Label, unsigned NumEdgeSources = 0,
192 const std::vector<std::string> *EdgeSourceLabels = 0) {
193 O << "\tNode" << ID << "[ ";
197 if (NumEdgeSources) O << "{";
198 O << DOT::EscapeString(Label);
199 if (NumEdgeSources) {
202 for (unsigned i = 0; i != NumEdgeSources; ++i) {
204 O << "<g" << i << ">";
205 if (EdgeSourceLabels) O << (*EdgeSourceLabels)[i];
212 /// emitEdge - Output an edge from a simple node into the graph...
213 void emitEdge(const void *SrcNodeID, int SrcNodePort,
214 const void *DestNodeID, int DestNodePort,
215 const std::string &Attrs) {
216 if (SrcNodePort > 64) return; // Eminating from truncated part?
217 if (DestNodePort > 64) DestNodePort = 64; // Targetting the truncated part?
219 O << "\tNode" << SrcNodeID;
220 if (SrcNodePort >= 0)
221 O << ":g" << SrcNodePort;
222 O << " -> Node" << reinterpret_cast<const void*>(DestNodeID);
223 if (DestNodePort >= 0)
224 O << ":g" << DestNodePort;
227 O << "[" << Attrs << "]";
232 template<typename GraphType>
233 std::ostream &WriteGraph(std::ostream &O, const GraphType &G,
234 const std::string &Name = "") {
235 // Start the graph emission process...
236 GraphWriter<GraphType> W(O, G);
238 // Output the header for the graph...
241 // Emit all of the nodes in the graph...
244 // Output any customizations on the graph
245 DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W);
247 // Output the end of the graph
252 template<typename GraphType>
253 sys::Path WriteGraph(const GraphType &G,
254 const std::string& Name,
255 const std::string& Title = "") {
257 sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
258 if (Filename.isEmpty()) {
259 cerr << "Error: " << ErrMsg << "\n";
262 Filename.appendComponent(Name + ".dot");
263 if (Filename.makeUnique(true,&ErrMsg)) {
264 cerr << "Error: " << ErrMsg << "\n";
268 cerr << "Writing '" << Filename << "'... ";
270 std::ofstream O(Filename.c_str());
273 // Start the graph emission process...
274 GraphWriter<GraphType> W(O, G);
276 // Output the header for the graph...
277 W.writeHeader(Title);
279 // Emit all of the nodes in the graph...
282 // Output any customizations on the graph
283 DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W);
285 // Output the end of the graph
292 cerr << "error opening file for writing!\n";
299 /// ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
300 /// then cleanup. For use from the debugger.
302 template<typename GraphType>
303 void ViewGraph(const GraphType& G,
304 const std::string& Name,
305 const std::string& Title = "") {
306 sys::Path Filename = WriteGraph(G, Name, Title);
308 if (Filename.isEmpty()) {
312 DisplayGraph(Filename);
315 } // End llvm namespace