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 *const *Node) {
111 void writeNode(NodeType *Node) {
112 std::string NodeAttributes = DOTTraits::getNodeAttributes(Node, G);
114 O << "\tNode" << reinterpret_cast<const void*>(Node) << " [shape=record,";
115 if (!NodeAttributes.empty()) O << NodeAttributes << ",";
118 if (!DOTTraits::renderGraphFromBottomUp()) {
119 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G));
121 // If we should include the address of the node in the label, do so now.
122 if (DOTTraits::hasNodeAddressLabel(Node, G))
123 O << "|" << (void*)Node;
126 // Print out the fields of the current node...
127 child_iterator EI = GTraits::child_begin(Node);
128 child_iterator EE = GTraits::child_end(Node);
130 if (!DOTTraits::renderGraphFromBottomUp()) O << "|";
133 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
135 O << "<g" << i << ">" << DOTTraits::getEdgeSourceLabel(Node, EI);
139 O << "|<g64>truncated...";
141 if (DOTTraits::renderGraphFromBottomUp()) O << "|";
144 if (DOTTraits::renderGraphFromBottomUp()) {
145 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G));
147 // If we should include the address of the node in the label, do so now.
148 if (DOTTraits::hasNodeAddressLabel(Node, G))
149 O << "|" << (void*)Node;
152 O << "}\"];\n"; // Finish printing the "node" line
154 // Output all of the edges now
155 EI = GTraits::child_begin(Node);
156 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
157 writeEdge(Node, i, EI);
158 for (; EI != EE; ++EI)
159 writeEdge(Node, 64, EI);
162 void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) {
163 if (NodeType *TargetNode = *EI) {
165 if (DOTTraits::edgeTargetsEdgeSource(Node, EI)) {
166 child_iterator TargetIt = DOTTraits::getEdgeTarget(Node, EI);
168 // Figure out which edge this targets...
169 unsigned Offset = std::distance(GTraits::child_begin(TargetNode),
171 DestPort = static_cast<int>(Offset);
174 emitEdge(reinterpret_cast<const void*>(Node), edgeidx,
175 reinterpret_cast<const void*>(TargetNode), DestPort,
176 DOTTraits::getEdgeAttributes(Node, EI));
180 /// emitSimpleNode - Outputs a simple (non-record) node
181 void emitSimpleNode(const void *ID, const std::string &Attr,
182 const std::string &Label, unsigned NumEdgeSources = 0,
183 const std::vector<std::string> *EdgeSourceLabels = 0) {
184 O << "\tNode" << ID << "[ ";
188 if (NumEdgeSources) O << "{";
189 O << DOT::EscapeString(Label);
190 if (NumEdgeSources) {
193 for (unsigned i = 0; i != NumEdgeSources; ++i) {
195 O << "<g" << i << ">";
196 if (EdgeSourceLabels) O << (*EdgeSourceLabels)[i];
203 /// emitEdge - Output an edge from a simple node into the graph...
204 void emitEdge(const void *SrcNodeID, int SrcNodePort,
205 const void *DestNodeID, int DestNodePort,
206 const std::string &Attrs) {
207 if (SrcNodePort > 64) return; // Eminating from truncated part?
208 if (DestNodePort > 64) DestNodePort = 64; // Targetting the truncated part?
210 O << "\tNode" << SrcNodeID;
211 if (SrcNodePort >= 0)
212 O << ":g" << SrcNodePort;
213 O << " -> Node" << reinterpret_cast<const void*>(DestNodeID);
214 if (DestNodePort >= 0)
215 O << ":g" << DestNodePort;
218 O << "[" << Attrs << "]";
223 template<typename GraphType>
224 std::ostream &WriteGraph(std::ostream &O, const GraphType &G,
225 const std::string &Name = "") {
226 // Start the graph emission process...
227 GraphWriter<GraphType> W(O, G);
229 // Output the header for the graph...
232 // Emit all of the nodes in the graph...
235 // Output any customizations on the graph
236 DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W);
238 // Output the end of the graph
243 template<typename GraphType>
244 sys::Path WriteGraph(const GraphType &G,
245 const std::string& Name,
246 const std::string& Title = "") {
248 sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
249 if (Filename.isEmpty()) {
250 cerr << "Error: " << ErrMsg << "\n";
253 Filename.appendComponent(Name + ".dot");
254 if (Filename.makeUnique(true,&ErrMsg)) {
255 cerr << "Error: " << ErrMsg << "\n";
259 cerr << "Writing '" << Filename << "'... ";
261 std::ofstream O(Filename.c_str());
264 // Start the graph emission process...
265 GraphWriter<GraphType> W(O, G);
267 // Output the header for the graph...
268 W.writeHeader(Title);
270 // Emit all of the nodes in the graph...
273 // Output any customizations on the graph
274 DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W);
276 // Output the end of the graph
283 cerr << "error opening file for writing!\n";
290 /// ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
291 /// then cleanup. For use from the debugger.
293 template<typename GraphType>
294 void ViewGraph(const GraphType& G,
295 const std::string& Name,
296 const std::string& Title = "") {
297 sys::Path Filename = WriteGraph(G, Name, Title);
299 if (Filename.isEmpty()) {
303 DisplayGraph(Filename);
306 } // End llvm namespace