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/ADT/GraphTraits.h"
27 #include "llvm/ADT/Twine.h"
28 #include "llvm/Support/DOTGraphTraits.h"
29 #include "llvm/Support/raw_ostream.h"
34 namespace DOT { // Private functions...
35 std::string EscapeString(const std::string &Label);
37 /// \brief Get a color string for this node number. Simply round-robin selects
38 /// from a reasonable number of colors.
39 StringRef getColorString(unsigned NodeNumber);
42 namespace GraphProgram {
52 bool DisplayGraph(StringRef Filename, bool wait = true,
53 GraphProgram::Name program = GraphProgram::DOT);
55 template<typename GraphType>
60 typedef DOTGraphTraits<GraphType> DOTTraits;
61 typedef GraphTraits<GraphType> GTraits;
62 typedef typename GTraits::NodeType NodeType;
63 typedef typename GTraits::nodes_iterator node_iterator;
64 typedef typename GTraits::ChildIteratorType child_iterator;
67 // Writes the edge labels of the node to O and returns true if there are any
68 // edge labels not equal to the empty string "".
69 bool getEdgeSourceLabels(raw_ostream &O, NodeType *Node) {
70 child_iterator EI = GTraits::child_begin(Node);
71 child_iterator EE = GTraits::child_end(Node);
72 bool hasEdgeSourceLabels = false;
74 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
75 std::string label = DTraits.getEdgeSourceLabel(Node, EI);
80 hasEdgeSourceLabels = true;
85 O << "<s" << i << ">" << DOT::EscapeString(label);
88 if (EI != EE && hasEdgeSourceLabels)
89 O << "|<s64>truncated...";
91 return hasEdgeSourceLabels;
95 GraphWriter(raw_ostream &o, const GraphType &g, bool SN) : O(o), G(g) {
96 DTraits = DOTTraits(SN);
99 void writeGraph(const std::string &Title = "") {
100 // Output the header for the graph...
103 // Emit all of the nodes in the graph...
106 // Output any customizations on the graph
107 DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, *this);
109 // Output the end of the graph
113 void writeHeader(const std::string &Title) {
114 std::string GraphName = DTraits.getGraphName(G);
117 O << "digraph \"" << DOT::EscapeString(Title) << "\" {\n";
118 else if (!GraphName.empty())
119 O << "digraph \"" << DOT::EscapeString(GraphName) << "\" {\n";
121 O << "digraph unnamed {\n";
123 if (DTraits.renderGraphFromBottomUp())
124 O << "\trankdir=\"BT\";\n";
127 O << "\tlabel=\"" << DOT::EscapeString(Title) << "\";\n";
128 else if (!GraphName.empty())
129 O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
130 O << DTraits.getGraphProperties(G);
135 // Finish off the graph
140 // Loop over the graph, printing it out...
141 for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
143 if (!isNodeHidden(*I))
147 bool isNodeHidden(NodeType &Node) {
148 return isNodeHidden(&Node);
151 bool isNodeHidden(NodeType *const *Node) {
152 return isNodeHidden(*Node);
155 bool isNodeHidden(NodeType *Node) {
156 return DTraits.isNodeHidden(Node);
159 void writeNode(NodeType& Node) {
163 void writeNode(NodeType *const *Node) {
167 void writeNode(NodeType *Node) {
168 std::string NodeAttributes = DTraits.getNodeAttributes(Node, G);
170 O << "\tNode" << static_cast<const void*>(Node) << " [shape=record,";
171 if (!NodeAttributes.empty()) O << NodeAttributes << ",";
174 if (!DTraits.renderGraphFromBottomUp()) {
175 O << DOT::EscapeString(DTraits.getNodeLabel(Node, G));
177 // If we should include the address of the node in the label, do so now.
178 std::string Id = DTraits.getNodeIdentifierLabel(Node, G);
180 O << "|" << DOT::EscapeString(Id);
182 std::string NodeDesc = DTraits.getNodeDescription(Node, G);
183 if (!NodeDesc.empty())
184 O << "|" << DOT::EscapeString(NodeDesc);
187 std::string edgeSourceLabels;
188 raw_string_ostream EdgeSourceLabels(edgeSourceLabels);
189 bool hasEdgeSourceLabels = getEdgeSourceLabels(EdgeSourceLabels, Node);
191 if (hasEdgeSourceLabels) {
192 if (!DTraits.renderGraphFromBottomUp()) O << "|";
194 O << "{" << EdgeSourceLabels.str() << "}";
196 if (DTraits.renderGraphFromBottomUp()) O << "|";
199 if (DTraits.renderGraphFromBottomUp()) {
200 O << DOT::EscapeString(DTraits.getNodeLabel(Node, G));
202 // If we should include the address of the node in the label, do so now.
203 std::string Id = DTraits.getNodeIdentifierLabel(Node, G);
205 O << "|" << DOT::EscapeString(Id);
207 std::string NodeDesc = DTraits.getNodeDescription(Node, G);
208 if (!NodeDesc.empty())
209 O << "|" << DOT::EscapeString(NodeDesc);
212 if (DTraits.hasEdgeDestLabels()) {
215 unsigned i = 0, e = DTraits.numEdgeDestLabels(Node);
216 for (; i != e && i != 64; ++i) {
218 O << "<d" << i << ">"
219 << DOT::EscapeString(DTraits.getEdgeDestLabel(Node, i));
223 O << "|<d64>truncated...";
227 O << "}\"];\n"; // Finish printing the "node" line
229 // Output all of the edges now
230 child_iterator EI = GTraits::child_begin(Node);
231 child_iterator EE = GTraits::child_end(Node);
232 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
233 if (!DTraits.isNodeHidden(*EI))
234 writeEdge(Node, i, EI);
235 for (; EI != EE; ++EI)
236 if (!DTraits.isNodeHidden(*EI))
237 writeEdge(Node, 64, EI);
240 void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) {
241 if (NodeType *TargetNode = *EI) {
243 if (DTraits.edgeTargetsEdgeSource(Node, EI)) {
244 child_iterator TargetIt = DTraits.getEdgeTarget(Node, EI);
246 // Figure out which edge this targets...
248 (unsigned)std::distance(GTraits::child_begin(TargetNode), TargetIt);
249 DestPort = static_cast<int>(Offset);
252 if (DTraits.getEdgeSourceLabel(Node, EI).empty())
255 emitEdge(static_cast<const void*>(Node), edgeidx,
256 static_cast<const void*>(TargetNode), DestPort,
257 DTraits.getEdgeAttributes(Node, EI, G));
261 /// emitSimpleNode - Outputs a simple (non-record) node
262 void emitSimpleNode(const void *ID, const std::string &Attr,
263 const std::string &Label, unsigned NumEdgeSources = 0,
264 const std::vector<std::string> *EdgeSourceLabels = nullptr) {
265 O << "\tNode" << ID << "[ ";
269 if (NumEdgeSources) O << "{";
270 O << DOT::EscapeString(Label);
271 if (NumEdgeSources) {
274 for (unsigned i = 0; i != NumEdgeSources; ++i) {
276 O << "<s" << i << ">";
277 if (EdgeSourceLabels) O << DOT::EscapeString((*EdgeSourceLabels)[i]);
284 /// emitEdge - Output an edge from a simple node into the graph...
285 void emitEdge(const void *SrcNodeID, int SrcNodePort,
286 const void *DestNodeID, int DestNodePort,
287 const std::string &Attrs) {
288 if (SrcNodePort > 64) return; // Eminating from truncated part?
289 if (DestNodePort > 64) DestNodePort = 64; // Targeting the truncated part?
291 O << "\tNode" << SrcNodeID;
292 if (SrcNodePort >= 0)
293 O << ":s" << SrcNodePort;
294 O << " -> Node" << DestNodeID;
295 if (DestNodePort >= 0 && DTraits.hasEdgeDestLabels())
296 O << ":d" << DestNodePort;
299 O << "[" << Attrs << "]";
303 /// getOStream - Get the raw output stream into the graph file. Useful to
304 /// write fancy things using addCustomGraphFeatures().
305 raw_ostream &getOStream() {
310 template<typename GraphType>
311 raw_ostream &WriteGraph(raw_ostream &O, const GraphType &G,
312 bool ShortNames = false,
313 const Twine &Title = "") {
314 // Start the graph emission process...
315 GraphWriter<GraphType> W(O, G, ShortNames);
318 W.writeGraph(Title.str());
323 std::string createGraphFilename(const Twine &Name, int &FD);
325 template <typename GraphType>
326 std::string WriteGraph(const GraphType &G, const Twine &Name,
327 bool ShortNames = false, const Twine &Title = "") {
329 // Windows can't always handle long paths, so limit the length of the name.
330 std::string N = Name.str();
331 N = N.substr(0, std::min<std::size_t>(N.size(), 140));
332 std::string Filename = createGraphFilename(N, FD);
333 raw_fd_ostream O(FD, /*shouldClose=*/ true);
336 errs() << "error opening file '" << Filename << "' for writing!\n";
340 llvm::WriteGraph(O, G, ShortNames, Title);
341 errs() << " done. \n";
346 /// ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
347 /// then cleanup. For use from the debugger.
349 template<typename GraphType>
350 void ViewGraph(const GraphType &G, const Twine &Name,
351 bool ShortNames = false, const Twine &Title = "",
352 GraphProgram::Name Program = GraphProgram::DOT) {
353 std::string Filename = llvm::WriteGraph(G, Name, ShortNames, Title);
355 if (Filename.empty())
358 DisplayGraph(Filename, false, Program);
361 } // End llvm namespace