1 //===-- llvm/Support/GraphWriter.h - Write graph to a .dot file -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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() && Str[i+1] == 'l')
52 break; // don't disturb \l
56 Str.insert(Str.begin()+i, '\\'); // Escape character...
57 ++i; // don't infinite loop
64 void DisplayGraph(const sys::Path& Filename);
66 template<typename GraphType>
71 typedef DOTGraphTraits<GraphType> DOTTraits;
72 typedef GraphTraits<GraphType> GTraits;
73 typedef typename GTraits::NodeType NodeType;
74 typedef typename GTraits::nodes_iterator node_iterator;
75 typedef typename GTraits::ChildIteratorType child_iterator;
77 GraphWriter(std::ostream &o, const GraphType &g) : O(o), G(g) {}
79 void writeHeader(const std::string &Name) {
81 O << "digraph foo {\n"; // Graph name doesn't matter
83 O << "digraph " << Name << " {\n";
85 if (DOTTraits::renderGraphFromBottomUp())
86 O << "\trankdir=\"BT\";\n";
88 std::string GraphName = DOTTraits::getGraphName(G);
89 if (!GraphName.empty())
90 O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
91 O << DOTTraits::getGraphProperties(G);
96 // Finish off the graph
101 // Loop over the graph, printing it out...
102 for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
107 void writeNode(NodeType& Node) {
111 void writeNode(NodeType *const *Node) {
115 void writeNode(NodeType *Node) {
116 std::string NodeAttributes = DOTTraits::getNodeAttributes(Node, G);
118 O << "\tNode" << reinterpret_cast<const void*>(Node) << " [shape=record,";
119 if (!NodeAttributes.empty()) O << NodeAttributes << ",";
122 if (!DOTTraits::renderGraphFromBottomUp()) {
123 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G));
125 // If we should include the address of the node in the label, do so now.
126 if (DOTTraits::hasNodeAddressLabel(Node, G))
127 O << "|" << (void*)Node;
130 // Print out the fields of the current node...
131 child_iterator EI = GTraits::child_begin(Node);
132 child_iterator EE = GTraits::child_end(Node);
134 if (!DOTTraits::renderGraphFromBottomUp()) O << "|";
137 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
139 O << "<g" << i << ">" << DOTTraits::getEdgeSourceLabel(Node, EI);
143 O << "|<g64>truncated...";
145 if (DOTTraits::renderGraphFromBottomUp()) O << "|";
148 if (DOTTraits::renderGraphFromBottomUp()) {
149 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G));
151 // If we should include the address of the node in the label, do so now.
152 if (DOTTraits::hasNodeAddressLabel(Node, G))
153 O << "|" << (void*)Node;
156 O << "}\"];\n"; // Finish printing the "node" line
158 // Output all of the edges now
159 EI = GTraits::child_begin(Node);
160 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
161 writeEdge(Node, i, EI);
162 for (; EI != EE; ++EI)
163 writeEdge(Node, 64, EI);
166 void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) {
167 if (NodeType *TargetNode = *EI) {
169 if (DOTTraits::edgeTargetsEdgeSource(Node, EI)) {
170 child_iterator TargetIt = DOTTraits::getEdgeTarget(Node, EI);
172 // Figure out which edge this targets...
173 unsigned Offset = std::distance(GTraits::child_begin(TargetNode),
175 DestPort = static_cast<int>(Offset);
178 emitEdge(reinterpret_cast<const void*>(Node), edgeidx,
179 reinterpret_cast<const void*>(TargetNode), DestPort,
180 DOTTraits::getEdgeAttributes(Node, EI));
184 /// emitSimpleNode - Outputs a simple (non-record) node
185 void emitSimpleNode(const void *ID, const std::string &Attr,
186 const std::string &Label, unsigned NumEdgeSources = 0,
187 const std::vector<std::string> *EdgeSourceLabels = 0) {
188 O << "\tNode" << ID << "[ ";
192 if (NumEdgeSources) O << "{";
193 O << DOT::EscapeString(Label);
194 if (NumEdgeSources) {
197 for (unsigned i = 0; i != NumEdgeSources; ++i) {
199 O << "<g" << i << ">";
200 if (EdgeSourceLabels) O << (*EdgeSourceLabels)[i];
207 /// emitEdge - Output an edge from a simple node into the graph...
208 void emitEdge(const void *SrcNodeID, int SrcNodePort,
209 const void *DestNodeID, int DestNodePort,
210 const std::string &Attrs) {
211 if (SrcNodePort > 64) return; // Eminating from truncated part?
212 if (DestNodePort > 64) DestNodePort = 64; // Targetting the truncated part?
214 O << "\tNode" << SrcNodeID;
215 if (SrcNodePort >= 0)
216 O << ":g" << SrcNodePort;
217 O << " -> Node" << reinterpret_cast<const void*>(DestNodeID);
218 if (DestNodePort >= 0)
219 O << ":g" << DestNodePort;
222 O << "[" << Attrs << "]";
227 template<typename GraphType>
228 std::ostream &WriteGraph(std::ostream &O, const GraphType &G,
229 const std::string &Name = "") {
230 // Start the graph emission process...
231 GraphWriter<GraphType> W(O, G);
233 // Output the header for the graph...
236 // Emit all of the nodes in the graph...
239 // Output any customizations on the graph
240 DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W);
242 // Output the end of the graph
247 template<typename GraphType>
248 sys::Path WriteGraph(const GraphType &G,
249 const std::string& Name,
250 const std::string& Title = "") {
252 sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
253 if (Filename.isEmpty()) {
254 cerr << "Error: " << ErrMsg << "\n";
257 Filename.appendComponent(Name + ".dot");
258 if (Filename.makeUnique(true,&ErrMsg)) {
259 cerr << "Error: " << ErrMsg << "\n";
263 cerr << "Writing '" << Filename << "'... ";
265 std::ofstream O(Filename.c_str());
268 // Start the graph emission process...
269 GraphWriter<GraphType> W(O, G);
271 // Output the header for the graph...
272 W.writeHeader(Title);
274 // Emit all of the nodes in the graph...
277 // Output any customizations on the graph
278 DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W);
280 // Output the end of the graph
287 cerr << "error opening file for writing!\n";
294 /// ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
295 /// then cleanup. For use from the debugger.
297 template<typename GraphType>
298 void ViewGraph(const GraphType& G,
299 const std::string& Name,
300 const std::string& Title = "") {
301 sys::Path Filename = WriteGraph(G, Name, Title);
303 if (Filename.isEmpty()) {
307 DisplayGraph(Filename);
310 } // End llvm namespace