Ninja
graphviz.cc
Go to the documentation of this file.
00001 // Copyright 2011 Google Inc. All Rights Reserved.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //     http://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "graphviz.h"
00016 
00017 #include <stdio.h>
00018 
00019 #include "graph.h"
00020 
00021 void GraphViz::AddTarget(Node* node) {
00022   if (visited_nodes_.find(node) != visited_nodes_.end())
00023     return;
00024 
00025   printf("\"%p\" [label=\"%s\"]\n", node, node->path().c_str());
00026   visited_nodes_.insert(node);
00027 
00028   Edge* edge = node->in_edge();
00029 
00030   if (!edge) {
00031     // Leaf node.
00032     // Draw as a rect?
00033     return;
00034   }
00035 
00036   if (visited_edges_.find(edge) != visited_edges_.end())
00037     return;
00038   visited_edges_.insert(edge);
00039 
00040   if (edge->inputs_.size() == 1 && edge->outputs_.size() == 1) {
00041     // Can draw simply.
00042     // Note extra space before label text -- this is cosmetic and feels
00043     // like a graphviz bug.
00044     printf("\"%p\" -> \"%p\" [label=\" %s\"]\n",
00045            edge->inputs_[0], edge->outputs_[0], edge->rule_->name().c_str());
00046   } else {
00047     printf("\"%p\" [label=\"%s\", shape=ellipse]\n",
00048            edge, edge->rule_->name().c_str());
00049     for (vector<Node*>::iterator out = edge->outputs_.begin();
00050          out != edge->outputs_.end(); ++out) {
00051       printf("\"%p\" -> \"%p\"\n", edge, *out);
00052     }
00053     for (vector<Node*>::iterator in = edge->inputs_.begin();
00054          in != edge->inputs_.end(); ++in) {
00055       const char* order_only = "";
00056       if (edge->is_order_only(in - edge->inputs_.begin()))
00057         order_only = " style=dotted";
00058       printf("\"%p\" -> \"%p\" [arrowhead=none%s]\n", (*in), edge, order_only);
00059     }
00060   }
00061 
00062   for (vector<Node*>::iterator in = edge->inputs_.begin();
00063        in != edge->inputs_.end(); ++in) {
00064     AddTarget(*in);
00065   }
00066 }
00067 
00068 void GraphViz::Start() {
00069   printf("digraph ninja {\n");
00070   printf("rankdir=\"LR\"\n");
00071   printf("node [fontsize=10, shape=box, height=0.25]\n");
00072   printf("edge [fontsize=10]\n");
00073 }
00074 
00075 void GraphViz::Finish() {
00076   printf("}\n");
00077 }