summaryrefslogtreecommitdiffstats
path: root/src/graphviz.h
blob: eb9ee2551bb0bb3df9f00c421cf55b2ec0ebf164 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <set>

// XXX deinline all this code so we don't need this include
#include "graph.h"
struct Node;

struct GraphViz {
  void Start();
  void AddTarget(Node* node);
  void Finish();

  set<Node*> visited_;
};

void GraphViz::AddTarget(Node* node) {
  if (visited_.find(node) != visited_.end())
    return;
  printf("\"%p\" [label=\"%s\"]\n", node, node->file_->path_.c_str());
  visited_.insert(node);

  if (!node->in_edge_) {
    // Leaf node.
    // Draw as a rect?
    return;
  }

  Edge* edge = node->in_edge_;

  if (edge->inputs_.size() == 1 && edge->outputs_.size() == 1) {
    // Can draw simply.
    // Note extra space before label text -- this is cosmetic and feels
    // like a graphviz bug.
    printf("\"%p\" -> \"%p\" [label=\" %s\"]\n",
           edge->inputs_[0], edge->outputs_[0], edge->rule_->name_.c_str());
  } else {
    printf("\"%p\" [label=\"%s\", shape=ellipse]\n",
           edge, edge->rule_->name_.c_str());
    for (vector<Node*>::iterator out = edge->outputs_.begin();
         out != edge->outputs_.end(); ++out) {
      printf("\"%p\" -> \"%p\"\n", edge, *out);
    }
    for (vector<Node*>::iterator in = edge->inputs_.begin();
         in != edge->inputs_.end(); ++in) {
      const char* order_only = "";
      if (edge->is_order_only(in - edge->inputs_.begin()))
        order_only = " style=dotted";
      printf("\"%p\" -> \"%p\" [arrowhead=none%s]\n", (*in), edge, order_only);
    }
  }

  for (vector<Node*>::iterator in = edge->inputs_.begin();
       in != edge->inputs_.end(); ++in) {
    AddTarget(*in);
  }
}

void GraphViz::Start() {
  printf("digraph ninja {\n");
  printf("node [fontsize=10, shape=box, height=0.25]\n");
  printf("edge [fontsize=10]\n");
}

void GraphViz::Finish() {
  printf("}\n");
}