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"
36 namespace DOT { // Private functions...
37 inline std::string EscapeString(const std::string &Label) {
38 std::string Str(Label);
39 for (unsigned i = 0; i != Str.length(); ++i)
42 Str.insert(Str.begin()+i, '\\'); // Escape character...
47 Str.insert(Str.begin()+i, ' '); // Convert to two spaces
52 if (i+1 != Str.length())
54 case 'l': continue; // don't disturb \l
55 case '|': case '{': case '}':
56 Str.erase(Str.begin()+i); continue;
62 Str.insert(Str.begin()+i, '\\'); // Escape character...
63 ++i; // don't infinite loop
70 void DisplayGraph(const sys::Path& Filename, bool wait=true);
72 template<typename GraphType>
78 typedef DOTGraphTraits<GraphType> DOTTraits;
79 typedef GraphTraits<GraphType> GTraits;
80 typedef typename GTraits::NodeType NodeType;
81 typedef typename GTraits::nodes_iterator node_iterator;
82 typedef typename GTraits::ChildIteratorType child_iterator;
84 GraphWriter(std::ostream &o, const GraphType &g, bool SN) :
85 O(o), G(g), ShortNames(SN) {}
87 void writeHeader(const std::string &Name) {
88 std::string GraphName = DOTTraits::getGraphName(G);
91 O << "digraph \"" << DOT::EscapeString(Name) << "\" {\n";
92 else if (!GraphName.empty())
93 O << "digraph \"" << DOT::EscapeString(GraphName) << "\" {\n";
95 O << "digraph unnamed {\n";
97 if (DOTTraits::renderGraphFromBottomUp())
98 O << "\trankdir=\"BT\";\n";
101 O << "\tlabel=\"" << DOT::EscapeString(Name) << "\";\n";
102 else if (!GraphName.empty())
103 O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
104 O << DOTTraits::getGraphProperties(G);
109 // Finish off the graph
114 // Loop over the graph, printing it out...
115 for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
120 void writeNode(NodeType& Node) {
124 void writeNode(NodeType *const *Node) {
128 void writeNode(NodeType *Node) {
129 std::string NodeAttributes = DOTTraits::getNodeAttributes(Node, G);
131 O << "\tNode" << static_cast<const void*>(Node) << " [shape=record,";
132 if (!NodeAttributes.empty()) O << NodeAttributes << ",";
135 if (!DOTTraits::renderGraphFromBottomUp()) {
136 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G, ShortNames));
138 // If we should include the address of the node in the label, do so now.
139 if (DOTTraits::hasNodeAddressLabel(Node, G))
140 O << "|" << (void*)Node;
143 // Print out the fields of the current node...
144 child_iterator EI = GTraits::child_begin(Node);
145 child_iterator EE = GTraits::child_end(Node);
147 if (!DOTTraits::renderGraphFromBottomUp()) O << "|";
150 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
152 O << "<s" << i << ">" << DOTTraits::getEdgeSourceLabel(Node, EI);
156 O << "|<s64>truncated...";
158 if (DOTTraits::renderGraphFromBottomUp()) O << "|";
161 if (DOTTraits::renderGraphFromBottomUp()) {
162 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G, ShortNames));
164 // If we should include the address of the node in the label, do so now.
165 if (DOTTraits::hasNodeAddressLabel(Node, G))
166 O << "|" << (void*)Node;
169 if (DOTTraits::hasEdgeDestLabels()) {
172 unsigned i = 0, e = DOTTraits::numEdgeDestLabels(Node);
173 for (; i != e && i != 64; ++i) {
175 O << "<d" << i << ">" << DOTTraits::getEdgeDestLabel(Node, i);
179 O << "|<d64>truncated...";
183 O << "}\"];\n"; // Finish printing the "node" line
185 // Output all of the edges now
186 EI = GTraits::child_begin(Node);
187 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
188 writeEdge(Node, i, EI);
189 for (; EI != EE; ++EI)
190 writeEdge(Node, 64, EI);
193 void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) {
194 if (NodeType *TargetNode = *EI) {
196 if (DOTTraits::edgeTargetsEdgeSource(Node, EI)) {
197 child_iterator TargetIt = DOTTraits::getEdgeTarget(Node, EI);
199 // Figure out which edge this targets...
201 (unsigned)std::distance(GTraits::child_begin(TargetNode), TargetIt);
202 DestPort = static_cast<int>(Offset);
205 emitEdge(static_cast<const void*>(Node), edgeidx,
206 static_cast<const void*>(TargetNode), DestPort,
207 DOTTraits::getEdgeAttributes(Node, EI));
211 /// emitSimpleNode - Outputs a simple (non-record) node
212 void emitSimpleNode(const void *ID, const std::string &Attr,
213 const std::string &Label, unsigned NumEdgeSources = 0,
214 const std::vector<std::string> *EdgeSourceLabels = 0) {
215 O << "\tNode" << ID << "[ ";
219 if (NumEdgeSources) O << "{";
220 O << DOT::EscapeString(Label);
221 if (NumEdgeSources) {
224 for (unsigned i = 0; i != NumEdgeSources; ++i) {
226 O << "<g" << i << ">";
227 if (EdgeSourceLabels) O << (*EdgeSourceLabels)[i];
234 /// emitEdge - Output an edge from a simple node into the graph...
235 void emitEdge(const void *SrcNodeID, int SrcNodePort,
236 const void *DestNodeID, int DestNodePort,
237 const std::string &Attrs) {
238 if (SrcNodePort > 64) return; // Eminating from truncated part?
239 if (DestNodePort > 64) DestNodePort = 64; // Targetting the truncated part?
241 O << "\tNode" << SrcNodeID;
242 if (SrcNodePort >= 0)
243 O << ":s" << SrcNodePort;
244 O << " -> Node" << DestNodeID;
245 if (DestNodePort >= 0)
246 O << ":d" << DestNodePort;
249 O << "[" << Attrs << "]";
254 template<typename GraphType>
255 std::ostream &WriteGraph(std::ostream &O, const GraphType &G,
256 bool ShortNames = false,
257 const std::string &Name = "",
258 const std::string &Title = "") {
259 // Start the graph emission process...
260 GraphWriter<GraphType> W(O, G, ShortNames);
262 // Output the header for the graph...
263 W.writeHeader(Title);
265 // Emit all of the nodes in the graph...
268 // Output any customizations on the graph
269 DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W);
271 // Output the end of the graph
276 template<typename GraphType>
277 sys::Path WriteGraph(const GraphType &G,
278 const std::string& Name,
279 bool ShortNames = false,
280 const std::string& Title = "") {
282 sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
283 if (Filename.isEmpty()) {
284 cerr << "Error: " << ErrMsg << "\n";
287 Filename.appendComponent(Name + ".dot");
288 if (Filename.makeUnique(true,&ErrMsg)) {
289 cerr << "Error: " << ErrMsg << "\n";
293 cerr << "Writing '" << Filename << "'... ";
295 std::ofstream O(Filename.c_str());
298 WriteGraph(O, G, ShortNames, Name, Title);
303 cerr << "error opening file for writing!\n";
310 /// ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
311 /// then cleanup. For use from the debugger.
313 template<typename GraphType>
314 void ViewGraph(const GraphType& G,
315 const std::string& Name,
316 bool ShortNames = false,
317 const std::string& Title = "") {
318 sys::Path Filename = WriteGraph(G, Name, ShortNames, Title);
320 if (Filename.isEmpty()) {
324 DisplayGraph(Filename);
327 } // End llvm namespace