- Allow printing generic LLVM graphs to 'dot' files, so they can be
[oota-llvm.git] / include / Support / GraphWriter.h
1 //===-- Support/GraphWriter.h - Write a graph to a .dot file ---*- C++ -*--===//
2 //
3 // This file defines a simple interface that can be used to print out generic
4 // LLVM graphs to ".dot" files.  "dot" is a tool that is part of the AT&T
5 // graphviz package (http://www.research.att.com/sw/tools/graphviz/) which can
6 // be used to turn the files output by this interface into a variety of
7 // different graphics formats.
8 //
9 // Graphs do not need to implement any interface past what is already required
10 // by the GraphTraits template, but they can choose to implement specializations
11 // of the DOTGraphTraits template if they want to customize the graphs output in
12 // any way.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef SUPPORT_GRAPHWRITER_H
17 #define SUPPORT_GRAPHWRITER_H
18
19 #include "Support/DOTGraphTraits.h"
20 #include "Support/DepthFirstIterator.h"
21 #include <ostream>
22
23 namespace DOT {  // Private functions...
24   inline std::string EscapeString(const std::string &Label) {
25     std::string Str(Label);
26     for (unsigned i = 0; i != Str.length(); ++i)
27       switch (Str[i]) {
28       case '\n':
29         Str.insert(Str.begin()+i, '\\');  // Escape character...
30         ++i;
31         Str[i] = 'n';
32         break;
33       case '\t':
34         Str.insert(Str.begin()+i, ' ');  // Convert to two spaces
35         ++i;
36         Str[i] = ' ';
37         break;
38       case '\\':
39         if (i+1 != Str.length() && Str[i+1] == 'l')
40           break;  // don't disturb \l
41       case '{': case '}':
42       case '<': case '>':
43         Str.insert(Str.begin()+i, '\\');  // Escape character...
44         ++i;  // don't infinite loop
45         break;
46       }
47     return Str;
48   }
49 }
50
51 template<typename GraphType>
52 std::ostream &WriteGraph(std::ostream &O, const GraphType &G) {
53   typedef DOTGraphTraits<GraphType>  DOTTraits;
54   typedef GraphTraits<GraphType>     GTraits;
55   typedef typename GTraits::NodeType NodeType;
56
57   O << "digraph foo {\n"         // Graph name doesn't matter
58     << "\tsize=\"10,7.5\";\n"    // Size to fit on a page
59     << "\trotate=\"90\";\n";     // Orient correctly
60
61   std::string GraphName = DOTTraits::getGraphName(G);
62   if (!GraphName.empty())
63     O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
64   O << "\n";
65
66   // Loop over the graph in DFO, printing it out...
67   NodeType *Root = GTraits::getEntryNode(G);
68   for (df_iterator<GraphType> I = df_begin(G), E = df_end(G); I != E; ++I) {
69     NodeType *Node = *I;
70
71     std::string NodeAttributes = DOTTraits::getNodeAttributes(Node);
72
73     O << "\tNode" << (void*)Node << " [";
74     if (!NodeAttributes.empty()) O << NodeAttributes << ",";
75     O << "shape=record,label=\"{"
76       << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G));
77     
78     // Print out the fields of the current node...
79     typename GTraits::ChildIteratorType EI = GTraits::child_begin(Node);
80     typename GTraits::ChildIteratorType EE = GTraits::child_end(Node);
81     if (EI != EE) {
82       O << "|{";
83
84       for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
85         if (i) O << "|";
86         O << "<g" << i << ">" << DOTTraits::getEdgeSourceLabel(Node, EI);
87       }
88
89       if (EI != EE)
90         O << "|truncated...";
91       O << "}";
92     }
93     O << "}\"];\n";   // Finish printing the "node" line
94
95     // Output all of the edges now
96     EI = GTraits::child_begin(Node);
97     for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
98       NodeType *TargetNode = *EI;
99       O << "\tNode" << (void*)Node << ":g" << i << " -> Node"
100         << (void*)TargetNode;
101       if (DOTTraits::edgeTargetsEdgeSource(Node, EI)) {
102         typename GTraits::ChildIteratorType TargetIt =
103           DOTTraits::getEdgeTarget(Node, EI);
104         // Figure out which edge this targets...
105         unsigned Offset = std::distance(GTraits::child_begin(TargetNode),
106                                         TargetIt);
107         if (Offset > 64) Offset = 64;  // Targetting the trancated part?
108         O << ":g" << Offset;        
109       }
110
111       std::string EdgeAttributes = DOTTraits::getEdgeAttributes(Node, EI);
112       if (!EdgeAttributes.empty())
113         O << "[" << EdgeAttributes << "]";
114       O << ";\n";
115     }
116   }
117
118   // Finish off the graph
119   O << "}\n";
120   return O;
121 }
122
123 #endif