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/DOTGraphTraits.h"
27 #include "llvm/ADT/GraphTraits.h"
33 namespace DOT { // Private functions...
34 inline std::string EscapeString(const std::string &Label) {
35 std::string Str(Label);
36 for (unsigned i = 0; i != Str.length(); ++i)
39 Str.insert(Str.begin()+i, '\\'); // Escape character...
44 Str.insert(Str.begin()+i, ' '); // Convert to two spaces
49 if (i+1 != Str.length() && Str[i+1] == 'l')
50 break; // don't disturb \l
54 Str.insert(Str.begin()+i, '\\'); // Escape character...
55 ++i; // don't infinite loop
62 template<typename GraphType>
67 typedef DOTGraphTraits<GraphType> DOTTraits;
68 typedef GraphTraits<GraphType> GTraits;
69 typedef typename GTraits::NodeType NodeType;
70 typedef typename GTraits::nodes_iterator node_iterator;
71 typedef typename GTraits::ChildIteratorType child_iterator;
73 GraphWriter(std::ostream &o, const GraphType &g) : O(o), G(g) {}
75 void writeHeader(const std::string &Name) {
77 O << "digraph foo {\n"; // Graph name doesn't matter
79 O << "digraph " << Name << " {\n";
81 if (DOTTraits::renderGraphFromBottomUp())
82 O << "\trankdir=\"BT\";\n";
84 std::string GraphName = DOTTraits::getGraphName(G);
85 if (!GraphName.empty())
86 O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
87 O << DOTTraits::getGraphProperties(G);
92 // Finish off the graph
97 // Loop over the graph, printing it out...
98 for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
103 void writeNode(NodeType *const *Node) {
107 void writeNode(NodeType *Node) {
108 std::string NodeAttributes = DOTTraits::getNodeAttributes(Node);
110 O << "\tNode" << reinterpret_cast<const void*>(Node) << " [shape=record,";
111 if (!NodeAttributes.empty()) O << NodeAttributes << ",";
114 if (!DOTTraits::renderGraphFromBottomUp()) {
115 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G));
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 << "<g" << i << ">" << DOTTraits::getEdgeSourceLabel(Node, EI);
135 O << "|<g64>truncated...";
137 if (DOTTraits::renderGraphFromBottomUp()) O << "|";
140 if (DOTTraits::renderGraphFromBottomUp()) {
141 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G));
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 O << "}\"];\n"; // Finish printing the "node" line
150 // Output all of the edges now
151 EI = GTraits::child_begin(Node);
152 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
153 writeEdge(Node, i, EI);
154 for (; EI != EE; ++EI)
155 writeEdge(Node, 64, EI);
158 void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) {
159 if (NodeType *TargetNode = *EI) {
161 if (DOTTraits::edgeTargetsEdgeSource(Node, EI)) {
162 child_iterator TargetIt = DOTTraits::getEdgeTarget(Node, EI);
164 // Figure out which edge this targets...
165 unsigned Offset = std::distance(GTraits::child_begin(TargetNode),
167 DestPort = static_cast<int>(Offset);
170 emitEdge(reinterpret_cast<const void*>(Node), edgeidx,
171 reinterpret_cast<const void*>(TargetNode), DestPort,
172 DOTTraits::getEdgeAttributes(Node, EI));
176 /// emitSimpleNode - Outputs a simple (non-record) node
177 void emitSimpleNode(const void *ID, const std::string &Attr,
178 const std::string &Label, unsigned NumEdgeSources = 0,
179 const std::vector<std::string> *EdgeSourceLabels = 0) {
180 O << "\tNode" << ID << "[ ";
184 if (NumEdgeSources) O << "{";
185 O << DOT::EscapeString(Label);
186 if (NumEdgeSources) {
189 for (unsigned i = 0; i != NumEdgeSources; ++i) {
191 O << "<g" << i << ">";
192 if (EdgeSourceLabels) O << (*EdgeSourceLabels)[i];
199 /// emitEdge - Output an edge from a simple node into the graph...
200 void emitEdge(const void *SrcNodeID, int SrcNodePort,
201 const void *DestNodeID, int DestNodePort,
202 const std::string &Attrs) {
203 if (SrcNodePort > 64) return; // Eminating from truncated part?
204 if (DestNodePort > 64) DestNodePort = 64; // Targetting the truncated part?
206 O << "\tNode" << SrcNodeID;
207 if (SrcNodePort >= 0)
208 O << ":g" << SrcNodePort;
209 O << " -> Node" << reinterpret_cast<const void*>(DestNodeID);
210 if (DestNodePort >= 0)
211 O << ":g" << DestNodePort;
214 O << "[" << Attrs << "]";
219 template<typename GraphType>
220 std::ostream &WriteGraph(std::ostream &O, const GraphType &G,
221 const std::string &Name = "") {
222 // Start the graph emission process...
223 GraphWriter<GraphType> W(O, G);
225 // Output the header for the graph...
228 // Emit all of the nodes in the graph...
231 // Output any customizations on the graph
232 DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W);
234 // Output the end of the graph
239 } // End llvm namespace