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/DOTGraphTraits.h"
27 #include "llvm/Support/Streams.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) {
85 std::string GraphName = DOTTraits::getGraphName(G);
88 O << "digraph " << Name << " {\n";
89 else if (!GraphName.empty())
90 O << "digraph \"" << DOT::EscapeString(GraphName) << "\" {\n";
92 O << "digraph unnamed {\n";
94 if (DOTTraits::renderGraphFromBottomUp())
95 O << "\trankdir=\"BT\";\n";
97 if (!GraphName.empty())
98 O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
99 O << DOTTraits::getGraphProperties(G);
104 // Finish off the graph
109 // Loop over the graph, printing it out...
110 for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
115 void writeNode(NodeType& Node) {
119 void writeNode(NodeType *const *Node) {
123 void writeNode(NodeType *Node) {
124 std::string NodeAttributes = DOTTraits::getNodeAttributes(Node, G);
126 O << "\tNode" << reinterpret_cast<const void*>(Node) << " [shape=record,";
127 if (!NodeAttributes.empty()) O << NodeAttributes << ",";
130 if (!DOTTraits::renderGraphFromBottomUp()) {
131 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G));
133 // If we should include the address of the node in the label, do so now.
134 if (DOTTraits::hasNodeAddressLabel(Node, G))
135 O << "|" << (void*)Node;
138 // Print out the fields of the current node...
139 child_iterator EI = GTraits::child_begin(Node);
140 child_iterator EE = GTraits::child_end(Node);
142 if (!DOTTraits::renderGraphFromBottomUp()) O << "|";
145 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
147 O << "<g" << i << ">" << DOTTraits::getEdgeSourceLabel(Node, EI);
151 O << "|<g64>truncated...";
153 if (DOTTraits::renderGraphFromBottomUp()) O << "|";
156 if (DOTTraits::renderGraphFromBottomUp()) {
157 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G));
159 // If we should include the address of the node in the label, do so now.
160 if (DOTTraits::hasNodeAddressLabel(Node, G))
161 O << "|" << (void*)Node;
164 O << "}\"];\n"; // Finish printing the "node" line
166 // Output all of the edges now
167 EI = GTraits::child_begin(Node);
168 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
169 writeEdge(Node, i, EI);
170 for (; EI != EE; ++EI)
171 writeEdge(Node, 64, EI);
174 void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) {
175 if (NodeType *TargetNode = *EI) {
177 if (DOTTraits::edgeTargetsEdgeSource(Node, EI)) {
178 child_iterator TargetIt = DOTTraits::getEdgeTarget(Node, EI);
180 // Figure out which edge this targets...
182 (unsigned)std::distance(GTraits::child_begin(TargetNode), TargetIt);
183 DestPort = static_cast<int>(Offset);
186 emitEdge(reinterpret_cast<const void*>(Node), edgeidx,
187 reinterpret_cast<const void*>(TargetNode), DestPort,
188 DOTTraits::getEdgeAttributes(Node, EI));
192 /// emitSimpleNode - Outputs a simple (non-record) node
193 void emitSimpleNode(const void *ID, const std::string &Attr,
194 const std::string &Label, unsigned NumEdgeSources = 0,
195 const std::vector<std::string> *EdgeSourceLabels = 0) {
196 O << "\tNode" << ID << "[ ";
200 if (NumEdgeSources) O << "{";
201 O << DOT::EscapeString(Label);
202 if (NumEdgeSources) {
205 for (unsigned i = 0; i != NumEdgeSources; ++i) {
207 O << "<g" << i << ">";
208 if (EdgeSourceLabels) O << (*EdgeSourceLabels)[i];
215 /// emitEdge - Output an edge from a simple node into the graph...
216 void emitEdge(const void *SrcNodeID, int SrcNodePort,
217 const void *DestNodeID, int DestNodePort,
218 const std::string &Attrs) {
219 if (SrcNodePort > 64) return; // Eminating from truncated part?
220 if (DestNodePort > 64) DestNodePort = 64; // Targetting the truncated part?
222 O << "\tNode" << SrcNodeID;
223 if (SrcNodePort >= 0)
224 O << ":g" << SrcNodePort;
225 O << " -> Node" << reinterpret_cast<const void*>(DestNodeID);
226 if (DestNodePort >= 0)
227 O << ":g" << DestNodePort;
230 O << "[" << Attrs << "]";
235 template<typename GraphType>
236 std::ostream &WriteGraph(std::ostream &O, const GraphType &G,
237 const std::string &Name = "") {
238 // Start the graph emission process...
239 GraphWriter<GraphType> W(O, G);
241 // Output the header for the graph...
244 // Emit all of the nodes in the graph...
247 // Output any customizations on the graph
248 DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W);
250 // Output the end of the graph
255 template<typename GraphType>
256 sys::Path WriteGraph(const GraphType &G,
257 const std::string& Name,
258 const std::string& Title = "") {
260 sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
261 if (Filename.isEmpty()) {
262 cerr << "Error: " << ErrMsg << "\n";
265 Filename.appendComponent(Name + ".dot");
266 if (Filename.makeUnique(true,&ErrMsg)) {
267 cerr << "Error: " << ErrMsg << "\n";
271 cerr << "Writing '" << Filename << "'... ";
273 std::ofstream O(Filename.c_str());
276 // Start the graph emission process...
277 GraphWriter<GraphType> W(O, G);
279 // Output the header for the graph...
280 W.writeHeader(Title);
282 // Emit all of the nodes in the graph...
285 // Output any customizations on the graph
286 DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W);
288 // Output the end of the graph
295 cerr << "error opening file for writing!\n";
302 /// ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
303 /// then cleanup. For use from the debugger.
305 template<typename GraphType>
306 void ViewGraph(const GraphType& G,
307 const std::string& Name,
308 const std::string& Title = "") {
309 sys::Path Filename = WriteGraph(G, Name, Title);
311 if (Filename.isEmpty()) {
315 DisplayGraph(Filename);
318 } // End llvm namespace