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/raw_ostream.h"
28 #include "llvm/ADT/GraphTraits.h"
29 #include "llvm/System/Path.h"
35 namespace DOT { // Private functions...
36 std::string EscapeString(const std::string &Label);
39 namespace GraphProgram {
49 void DisplayGraph(const sys::Path& Filename, bool wait=true, GraphProgram::Name program = GraphProgram::DOT);
51 template<typename GraphType>
57 typedef DOTGraphTraits<GraphType> DOTTraits;
58 typedef GraphTraits<GraphType> GTraits;
59 typedef typename GTraits::NodeType NodeType;
60 typedef typename GTraits::nodes_iterator node_iterator;
61 typedef typename GTraits::ChildIteratorType child_iterator;
63 GraphWriter(raw_ostream &o, const GraphType &g, bool SN) :
64 O(o), G(g), ShortNames(SN) {}
66 void writeHeader(const std::string &Name) {
67 std::string GraphName = DOTTraits::getGraphName(G);
70 O << "digraph \"" << DOT::EscapeString(Name) << "\" {\n";
71 else if (!GraphName.empty())
72 O << "digraph \"" << DOT::EscapeString(GraphName) << "\" {\n";
74 O << "digraph unnamed {\n";
76 if (DOTTraits::renderGraphFromBottomUp())
77 O << "\trankdir=\"BT\";\n";
80 O << "\tlabel=\"" << DOT::EscapeString(Name) << "\";\n";
81 else if (!GraphName.empty())
82 O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
83 O << DOTTraits::getGraphProperties(G);
88 // Finish off the graph
93 // Loop over the graph, printing it out...
94 for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
99 void writeNode(NodeType& Node) {
103 void writeNode(NodeType *const *Node) {
107 void writeNode(NodeType *Node) {
108 std::string NodeAttributes = DOTTraits::getNodeAttributes(Node, G);
110 O << "\tNode" << static_cast<const void*>(Node) << " [shape=record,";
111 if (!NodeAttributes.empty()) O << NodeAttributes << ",";
114 if (!DOTTraits::renderGraphFromBottomUp()) {
115 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G, ShortNames));
117 // If we should include the address of the node in the label, do so now.
118 if (DOTTraits::hasNodeAddressLabel(Node, G))
119 O << "|" << (void*)Node;
122 // Print out the fields of the current node...
123 child_iterator EI = GTraits::child_begin(Node);
124 child_iterator EE = GTraits::child_end(Node);
126 if (!DOTTraits::renderGraphFromBottomUp()) O << "|";
129 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
131 O << "<s" << i << ">" << DOTTraits::getEdgeSourceLabel(Node, EI);
135 O << "|<s64>truncated...";
137 if (DOTTraits::renderGraphFromBottomUp()) O << "|";
140 if (DOTTraits::renderGraphFromBottomUp()) {
141 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G, ShortNames));
143 // If we should include the address of the node in the label, do so now.
144 if (DOTTraits::hasNodeAddressLabel(Node, G))
145 O << "|" << (void*)Node;
148 if (DOTTraits::hasEdgeDestLabels()) {
151 unsigned i = 0, e = DOTTraits::numEdgeDestLabels(Node);
152 for (; i != e && i != 64; ++i) {
154 O << "<d" << i << ">" << DOTTraits::getEdgeDestLabel(Node, i);
158 O << "|<d64>truncated...";
162 O << "}\"];\n"; // Finish printing the "node" line
164 // Output all of the edges now
165 EI = GTraits::child_begin(Node);
166 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
167 writeEdge(Node, i, EI);
168 for (; EI != EE; ++EI)
169 writeEdge(Node, 64, EI);
172 void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) {
173 if (NodeType *TargetNode = *EI) {
175 if (DOTTraits::edgeTargetsEdgeSource(Node, EI)) {
176 child_iterator TargetIt = DOTTraits::getEdgeTarget(Node, EI);
178 // Figure out which edge this targets...
180 (unsigned)std::distance(GTraits::child_begin(TargetNode), TargetIt);
181 DestPort = static_cast<int>(Offset);
184 emitEdge(static_cast<const void*>(Node), edgeidx,
185 static_cast<const void*>(TargetNode), DestPort,
186 DOTTraits::getEdgeAttributes(Node, EI));
190 /// emitSimpleNode - Outputs a simple (non-record) node
191 void emitSimpleNode(const void *ID, const std::string &Attr,
192 const std::string &Label, unsigned NumEdgeSources = 0,
193 const std::vector<std::string> *EdgeSourceLabels = 0) {
194 O << "\tNode" << ID << "[ ";
198 if (NumEdgeSources) O << "{";
199 O << DOT::EscapeString(Label);
200 if (NumEdgeSources) {
203 for (unsigned i = 0; i != NumEdgeSources; ++i) {
205 O << "<s" << i << ">";
206 if (EdgeSourceLabels) O << (*EdgeSourceLabels)[i];
213 /// emitEdge - Output an edge from a simple node into the graph...
214 void emitEdge(const void *SrcNodeID, int SrcNodePort,
215 const void *DestNodeID, int DestNodePort,
216 const std::string &Attrs) {
217 if (SrcNodePort > 64) return; // Eminating from truncated part?
218 if (DestNodePort > 64) DestNodePort = 64; // Targetting the truncated part?
220 O << "\tNode" << SrcNodeID;
221 if (SrcNodePort >= 0)
222 O << ":s" << SrcNodePort;
223 O << " -> Node" << DestNodeID;
224 if (DestNodePort >= 0) {
225 if (DOTTraits::hasEdgeDestLabels())
226 O << ":d" << DestNodePort;
228 O << ":s" << DestNodePort;
232 O << "[" << Attrs << "]";
237 template<typename GraphType>
238 raw_ostream &WriteGraph(raw_ostream &O, const GraphType &G,
239 bool ShortNames = false,
240 const std::string &Name = "",
241 const std::string &Title = "") {
242 // Start the graph emission process...
243 GraphWriter<GraphType> W(O, G, ShortNames);
245 // Output the header for the graph...
246 W.writeHeader(Title);
248 // Emit all of the nodes in the graph...
251 // Output any customizations on the graph
252 DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W);
254 // Output the end of the graph
259 template<typename GraphType>
260 sys::Path WriteGraph(const GraphType &G, const std::string &Name,
261 bool ShortNames = false, const std::string &Title = "") {
263 sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
264 if (Filename.isEmpty()) {
265 errs() << "Error: " << ErrMsg << "\n";
268 Filename.appendComponent(Name + ".dot");
269 if (Filename.makeUnique(true,&ErrMsg)) {
270 errs() << "Error: " << ErrMsg << "\n";
274 errs() << "Writing '" << Filename.str() << "'... ";
276 std::string ErrorInfo;
277 raw_fd_ostream O(Filename.c_str(), ErrorInfo);
279 if (ErrorInfo.empty()) {
280 WriteGraph(O, G, ShortNames, Name, Title);
281 errs() << " done. \n";
283 errs() << "error opening file '" << Filename.str() << "' 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, const std::string &Name,
295 bool ShortNames = false, const std::string &Title = "",
296 GraphProgram::Name Program = GraphProgram::DOT) {
297 sys::Path Filename = WriteGraph(G, Name, ShortNames, Title);
299 if (Filename.isEmpty())
302 DisplayGraph(Filename, true, Program);
305 } // End llvm namespace