Add virtual dtor, expose a debug impl
[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/GraphTraits.h"
21 #include <iostream>
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 class GraphWriter {
53   std::ostream &O;
54   const GraphType &G;
55
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;
61 public:
62   GraphWriter(std::ostream &o, const GraphType &g) : O(o), G(g) {}
63
64   void writeHeader(const std::string &Name) {
65     if (Name.empty())
66       O << "digraph foo {\n";        // Graph name doesn't matter
67     else
68       O << "digraph " << Name << " {\n";
69
70     std::string GraphName = DOTTraits::getGraphName(G);
71     if (!GraphName.empty())
72       O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
73     O << DOTTraits::getGraphProperties(G);
74     O << "\n";
75   }
76
77   void writeFooter() {
78     // Finish off the graph
79     O << "}\n";
80   }
81
82   void writeNodes() {
83     // Loop over the graph, printing it out...
84     for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
85          I != E; ++I)
86       writeNode(&*I);
87   }
88
89   void writeNode(NodeType *Node) {
90     std::string NodeAttributes = DOTTraits::getNodeAttributes(Node);
91       
92     O << "\tNode" << (void*)Node << " [shape=record,";
93     if (!NodeAttributes.empty()) O << NodeAttributes << ",";
94     O << "label=\"{"
95       << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G));
96     
97     // Print out the fields of the current node...
98     child_iterator EI = GTraits::child_begin(Node);
99     child_iterator EE = GTraits::child_end(Node);
100     if (EI != EE) {
101       O << "|{";
102       
103       for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
104         if (i) O << "|";
105         O << "<g" << i << ">" << DOTTraits::getEdgeSourceLabel(Node, EI);
106       }
107       
108       if (EI != EE)
109         O << "|truncated...";
110       O << "}";
111     }
112     O << "}\"];\n";   // Finish printing the "node" line
113     
114     // Output all of the edges now
115     EI = GTraits::child_begin(Node);
116     for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
117       writeEdge(Node, i, EI);
118   }
119
120   void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) {
121     if (NodeType *TargetNode = *EI) {
122       int DestPort = -1;
123       if (DOTTraits::edgeTargetsEdgeSource(Node, EI)) {
124         child_iterator TargetIt = DOTTraits::getEdgeTarget(Node, EI);
125
126         // Figure out which edge this targets...
127         unsigned Offset = std::distance(GTraits::child_begin(TargetNode),
128                                         TargetIt);
129         DestPort = (int)Offset;
130       }
131
132       emitEdge((void *)Node, edgeidx, (void*)TargetNode, DestPort,
133                DOTTraits::getEdgeAttributes(Node, EI));
134     }
135   }
136
137   /// emitSimpleNode - Outputs a simple (non-record) node
138   void emitSimpleNode(const void *ID, const std::string &Attr,
139                       const std::string &Label, unsigned NumEdgeSources = 0) {
140     O << "\tNode" << ID << "[ ";
141     if (!Attr.empty())
142       O << Attr << ",";
143     O << " label =\"";
144     if (NumEdgeSources) O << "{";
145     O << DOT::EscapeString(Label);
146     if (NumEdgeSources) {
147       O << "|{";
148       
149       for (unsigned i = 0; i != NumEdgeSources; ++i) {
150         if (i) O << "|";
151         O << "<g" << i << ">";
152       }
153       O << "}}";
154     }
155     O << "\"];\n";
156   }
157
158   /// emitEdge - Output an edge from a simple node into the graph...
159   void emitEdge(const void *SrcNodeID, int SrcNodePort,
160                 const void *DestNodeID, int DestNodePort,
161                 const std::string &Attrs) {
162     if (SrcNodePort  > 64) return;             // Eminating from truncated part?
163     if (DestNodePort > 64) DestNodePort = 64;  // Targetting the truncated part?
164
165     O << "\tNode" << SrcNodeID;
166     if (SrcNodePort >= 0)
167       O << ":g" << SrcNodePort;
168     O << " -> Node" << (void*)DestNodeID;
169     if (DestNodePort >= 0)
170       O << ":g" << DestNodePort;    
171
172     if (!Attrs.empty())
173       O << "[" << Attrs << "]";
174     O << ";\n";
175   }
176 };
177
178 template<typename GraphType>
179 std::ostream &WriteGraph(std::ostream &O, const GraphType &G,
180                          const std::string &Name = "") {
181   // Start the graph emission process...
182   GraphWriter<GraphType> W(O, G);
183
184   // Output the header for the graph...
185   W.writeHeader(Name);
186
187   // Emit all of the nodes in the graph...
188   W.writeNodes();
189
190   // Output any customizations on the graph
191   DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W);
192
193   // Output the end of the graph
194   W.writeFooter();
195   return O;
196 }
197
198 #endif