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