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 namespace GraphProgram {
80 void DisplayGraph(const sys::Path& Filename, bool wait=true, GraphProgram::Name program = GraphProgram::DOT);
82 template<typename GraphType>
88 typedef DOTGraphTraits<GraphType> DOTTraits;
89 typedef GraphTraits<GraphType> GTraits;
90 typedef typename GTraits::NodeType NodeType;
91 typedef typename GTraits::nodes_iterator node_iterator;
92 typedef typename GTraits::ChildIteratorType child_iterator;
94 GraphWriter(std::ostream &o, const GraphType &g, bool SN) :
95 O(o), G(g), ShortNames(SN) {}
97 void writeHeader(const std::string &Name) {
98 std::string GraphName = DOTTraits::getGraphName(G);
101 O << "digraph \"" << DOT::EscapeString(Name) << "\" {\n";
102 else if (!GraphName.empty())
103 O << "digraph \"" << DOT::EscapeString(GraphName) << "\" {\n";
105 O << "digraph unnamed {\n";
107 if (DOTTraits::renderGraphFromBottomUp())
108 O << "\trankdir=\"BT\";\n";
111 O << "\tlabel=\"" << DOT::EscapeString(Name) << "\";\n";
112 else if (!GraphName.empty())
113 O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
114 O << DOTTraits::getGraphProperties(G);
119 // Finish off the graph
124 // Loop over the graph, printing it out...
125 for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
130 void writeNode(NodeType& Node) {
134 void writeNode(NodeType *const *Node) {
138 void writeNode(NodeType *Node) {
139 std::string NodeAttributes = DOTTraits::getNodeAttributes(Node, G);
141 O << "\tNode" << static_cast<const void*>(Node) << " [shape=record,";
142 if (!NodeAttributes.empty()) O << NodeAttributes << ",";
145 if (!DOTTraits::renderGraphFromBottomUp()) {
146 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G, ShortNames));
148 // If we should include the address of the node in the label, do so now.
149 if (DOTTraits::hasNodeAddressLabel(Node, G))
150 O << "|" << (void*)Node;
153 // Print out the fields of the current node...
154 child_iterator EI = GTraits::child_begin(Node);
155 child_iterator EE = GTraits::child_end(Node);
157 if (!DOTTraits::renderGraphFromBottomUp()) O << "|";
160 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
162 O << "<s" << i << ">" << DOTTraits::getEdgeSourceLabel(Node, EI);
166 O << "|<s64>truncated...";
168 if (DOTTraits::renderGraphFromBottomUp()) O << "|";
171 if (DOTTraits::renderGraphFromBottomUp()) {
172 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G, ShortNames));
174 // If we should include the address of the node in the label, do so now.
175 if (DOTTraits::hasNodeAddressLabel(Node, G))
176 O << "|" << (void*)Node;
179 if (DOTTraits::hasEdgeDestLabels()) {
182 unsigned i = 0, e = DOTTraits::numEdgeDestLabels(Node);
183 for (; i != e && i != 64; ++i) {
185 O << "<d" << i << ">" << DOTTraits::getEdgeDestLabel(Node, i);
189 O << "|<d64>truncated...";
193 O << "}\"];\n"; // Finish printing the "node" line
195 // Output all of the edges now
196 EI = GTraits::child_begin(Node);
197 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
198 writeEdge(Node, i, EI);
199 for (; EI != EE; ++EI)
200 writeEdge(Node, 64, EI);
203 void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) {
204 if (NodeType *TargetNode = *EI) {
206 if (DOTTraits::edgeTargetsEdgeSource(Node, EI)) {
207 child_iterator TargetIt = DOTTraits::getEdgeTarget(Node, EI);
209 // Figure out which edge this targets...
211 (unsigned)std::distance(GTraits::child_begin(TargetNode), TargetIt);
212 DestPort = static_cast<int>(Offset);
215 emitEdge(static_cast<const void*>(Node), edgeidx,
216 static_cast<const void*>(TargetNode), DestPort,
217 DOTTraits::getEdgeAttributes(Node, EI));
221 /// emitSimpleNode - Outputs a simple (non-record) node
222 void emitSimpleNode(const void *ID, const std::string &Attr,
223 const std::string &Label, unsigned NumEdgeSources = 0,
224 const std::vector<std::string> *EdgeSourceLabels = 0) {
225 O << "\tNode" << ID << "[ ";
229 if (NumEdgeSources) O << "{";
230 O << DOT::EscapeString(Label);
231 if (NumEdgeSources) {
234 for (unsigned i = 0; i != NumEdgeSources; ++i) {
236 O << "<s" << i << ">";
237 if (EdgeSourceLabels) O << (*EdgeSourceLabels)[i];
244 /// emitEdge - Output an edge from a simple node into the graph...
245 void emitEdge(const void *SrcNodeID, int SrcNodePort,
246 const void *DestNodeID, int DestNodePort,
247 const std::string &Attrs) {
248 if (SrcNodePort > 64) return; // Eminating from truncated part?
249 if (DestNodePort > 64) DestNodePort = 64; // Targetting the truncated part?
251 O << "\tNode" << SrcNodeID;
252 if (SrcNodePort >= 0)
253 O << ":s" << SrcNodePort;
254 O << " -> Node" << DestNodeID;
255 if (DestNodePort >= 0) {
256 if (DOTTraits::hasEdgeDestLabels())
257 O << ":d" << DestNodePort;
259 O << ":s" << DestNodePort;
263 O << "[" << Attrs << "]";
268 template<typename GraphType>
269 std::ostream &WriteGraph(std::ostream &O, const GraphType &G,
270 bool ShortNames = false,
271 const std::string &Name = "",
272 const std::string &Title = "") {
273 // Start the graph emission process...
274 GraphWriter<GraphType> W(O, G, ShortNames);
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
290 template<typename GraphType>
291 sys::Path WriteGraph(const GraphType &G,
292 const std::string& Name,
293 bool ShortNames = false,
294 const std::string& Title = "") {
296 sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
297 if (Filename.isEmpty()) {
298 cerr << "Error: " << ErrMsg << "\n";
301 Filename.appendComponent(Name + ".dot");
302 if (Filename.makeUnique(true,&ErrMsg)) {
303 cerr << "Error: " << ErrMsg << "\n";
307 cerr << "Writing '" << Filename << "'... ";
309 std::ofstream O(Filename.c_str());
312 WriteGraph(O, G, ShortNames, Name, Title);
317 cerr << "error opening file for writing!\n";
324 /// ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
325 /// then cleanup. For use from the debugger.
327 template<typename GraphType>
328 void ViewGraph(const GraphType& G,
329 const std::string& Name,
330 bool ShortNames = false,
331 const std::string& Title = "",
332 GraphProgram::Name Program = GraphProgram::DOT) {
333 sys::Path Filename = WriteGraph(G, Name, ShortNames, Title);
335 if (Filename.isEmpty()) {
339 DisplayGraph(Filename, true, Program);
342 } // End llvm namespace