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>
56 typedef DOTGraphTraits<GraphType> DOTTraits;
57 typedef GraphTraits<GraphType> GTraits;
58 typedef typename GTraits::NodeType NodeType;
59 typedef typename GTraits::nodes_iterator node_iterator;
60 typedef typename GTraits::ChildIteratorType child_iterator;
63 // Writes the edge labels of the node to O and returns true if there are any
64 // edge labels not equal to the empty string "".
65 bool getEdgeSourceLabels(raw_ostream &O, NodeType *Node) {
66 child_iterator EI = GTraits::child_begin(Node);
67 child_iterator EE = GTraits::child_end(Node);
68 bool hasEdgeSourceLabels = false;
70 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
71 std::string label = DTraits.getEdgeSourceLabel(Node, EI);
76 hasEdgeSourceLabels = true;
81 O << "<s" << i << ">" << DTraits.getEdgeSourceLabel(Node, EI);
84 if (EI != EE && hasEdgeSourceLabels)
85 O << "|<s64>truncated...";
87 return hasEdgeSourceLabels;
91 GraphWriter(raw_ostream &o, const GraphType &g, bool SN) : O(o), G(g) {
92 DTraits = DOTTraits(SN);
95 void writeHeader(const std::string &Name) {
96 std::string GraphName = DTraits.getGraphName(G);
99 O << "digraph \"" << DOT::EscapeString(Name) << "\" {\n";
100 else if (!GraphName.empty())
101 O << "digraph \"" << DOT::EscapeString(GraphName) << "\" {\n";
103 O << "digraph unnamed {\n";
105 if (DTraits.renderGraphFromBottomUp())
106 O << "\trankdir=\"BT\";\n";
109 O << "\tlabel=\"" << DOT::EscapeString(Name) << "\";\n";
110 else if (!GraphName.empty())
111 O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
112 O << DTraits.getGraphProperties(G);
117 // Finish off the graph
122 // Loop over the graph, printing it out...
123 for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
128 void writeNode(NodeType& Node) {
132 void writeNode(NodeType *const *Node) {
136 void writeNode(NodeType *Node) {
137 std::string NodeAttributes = DTraits.getNodeAttributes(Node, G);
139 O << "\tNode" << static_cast<const void*>(Node) << " [shape=record,";
140 if (!NodeAttributes.empty()) O << NodeAttributes << ",";
143 if (!DTraits.renderGraphFromBottomUp()) {
144 O << DOT::EscapeString(DTraits.getNodeLabel(Node, G));
146 // If we should include the address of the node in the label, do so now.
147 if (DTraits.hasNodeAddressLabel(Node, G))
148 O << "|" << (void*)Node;
151 std::string edgeSourceLabels;
152 raw_string_ostream EdgeSourceLabels(edgeSourceLabels);
153 bool hasEdgeSourceLabels = getEdgeSourceLabels(EdgeSourceLabels, Node);
155 if (hasEdgeSourceLabels) {
156 if (!DTraits.renderGraphFromBottomUp()) O << "|";
158 O << "{" << EdgeSourceLabels.str() << "}";
160 if (DTraits.renderGraphFromBottomUp()) O << "|";
163 if (DTraits.renderGraphFromBottomUp()) {
164 O << DOT::EscapeString(DTraits.getNodeLabel(Node, G));
166 // If we should include the address of the node in the label, do so now.
167 if (DTraits.hasNodeAddressLabel(Node, G))
168 O << "|" << (void*)Node;
171 if (DTraits.hasEdgeDestLabels()) {
174 unsigned i = 0, e = DTraits.numEdgeDestLabels(Node);
175 for (; i != e && i != 64; ++i) {
177 O << "<d" << i << ">" << DTraits.getEdgeDestLabel(Node, i);
181 O << "|<d64>truncated...";
185 O << "}\"];\n"; // Finish printing the "node" line
187 // Output all of the edges now
188 child_iterator EI = GTraits::child_begin(Node);
189 child_iterator EE = GTraits::child_end(Node);
190 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
191 writeEdge(Node, i, EI);
192 for (; EI != EE; ++EI)
193 writeEdge(Node, 64, EI);
196 void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) {
197 if (NodeType *TargetNode = *EI) {
199 if (DTraits.edgeTargetsEdgeSource(Node, EI)) {
200 child_iterator TargetIt = DTraits.getEdgeTarget(Node, EI);
202 // Figure out which edge this targets...
204 (unsigned)std::distance(GTraits::child_begin(TargetNode), TargetIt);
205 DestPort = static_cast<int>(Offset);
208 if (DTraits.getEdgeSourceLabel(Node, EI) == "")
211 emitEdge(static_cast<const void*>(Node), edgeidx,
212 static_cast<const void*>(TargetNode), DestPort,
213 DTraits.getEdgeAttributes(Node, EI));
217 /// emitSimpleNode - Outputs a simple (non-record) node
218 void emitSimpleNode(const void *ID, const std::string &Attr,
219 const std::string &Label, unsigned NumEdgeSources = 0,
220 const std::vector<std::string> *EdgeSourceLabels = 0) {
221 O << "\tNode" << ID << "[ ";
225 if (NumEdgeSources) O << "{";
226 O << DOT::EscapeString(Label);
227 if (NumEdgeSources) {
230 for (unsigned i = 0; i != NumEdgeSources; ++i) {
232 O << "<s" << i << ">";
233 if (EdgeSourceLabels) O << (*EdgeSourceLabels)[i];
240 /// emitEdge - Output an edge from a simple node into the graph...
241 void emitEdge(const void *SrcNodeID, int SrcNodePort,
242 const void *DestNodeID, int DestNodePort,
243 const std::string &Attrs) {
244 if (SrcNodePort > 64) return; // Eminating from truncated part?
245 if (DestNodePort > 64) DestNodePort = 64; // Targetting the truncated part?
247 O << "\tNode" << SrcNodeID;
248 if (SrcNodePort >= 0)
249 O << ":s" << SrcNodePort;
250 O << " -> Node" << DestNodeID;
251 if (DestNodePort >= 0 && DTraits.hasEdgeDestLabels())
252 O << ":d" << DestNodePort;
255 O << "[" << Attrs << "]";
260 template<typename GraphType>
261 raw_ostream &WriteGraph(raw_ostream &O, const GraphType &G,
262 bool ShortNames = false,
263 const std::string &Name = "",
264 const std::string &Title = "") {
265 // Start the graph emission process...
266 GraphWriter<GraphType> W(O, G, ShortNames);
268 // Output the header for the graph...
269 W.writeHeader(Title);
271 // Emit all of the nodes in the graph...
274 // Output any customizations on the graph
275 DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W);
277 // Output the end of the graph
282 template<typename GraphType>
283 sys::Path WriteGraph(const GraphType &G, const std::string &Name,
284 bool ShortNames = false, const std::string &Title = "") {
286 sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
287 if (Filename.isEmpty()) {
288 errs() << "Error: " << ErrMsg << "\n";
291 Filename.appendComponent(Name + ".dot");
292 if (Filename.makeUnique(true,&ErrMsg)) {
293 errs() << "Error: " << ErrMsg << "\n";
297 errs() << "Writing '" << Filename.str() << "'... ";
299 std::string ErrorInfo;
300 raw_fd_ostream O(Filename.c_str(), ErrorInfo);
302 if (ErrorInfo.empty()) {
303 WriteGraph(O, G, ShortNames, Name, Title);
304 errs() << " done. \n";
306 errs() << "error opening file '" << Filename.str() << "' for writing!\n";
313 /// ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
314 /// then cleanup. For use from the debugger.
316 template<typename GraphType>
317 void ViewGraph(const GraphType &G, const std::string &Name,
318 bool ShortNames = false, const std::string &Title = "",
319 GraphProgram::Name Program = GraphProgram::DOT) {
320 sys::Path Filename = WriteGraph(G, Name, ShortNames, Title);
322 if (Filename.isEmpty())
325 DisplayGraph(Filename, true, Program);
328 } // End llvm namespace