-std::ostream &WriteGraph(std::ostream &O, const GraphType &G) {
- typedef DOTGraphTraits<GraphType> DOTTraits;
- typedef GraphTraits<GraphType> GTraits;
- typedef typename GTraits::NodeType NodeType;
- typedef typename GTraits::nodes_iterator node_iterator;
-
- O << "digraph foo {\n"; // Graph name doesn't matter
- std::string GraphName = DOTTraits::getGraphName(G);
- if (!GraphName.empty())
- O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
- O << DOTTraits::getGraphProperties(G);
- O << "\n";
-
- // Loop over the graph in DFO, printing it out...
- for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
- I != E; ++I) {
- NodeType *Node = &*I;
-
- std::string NodeAttributes = DOTTraits::getNodeAttributes(Node);
-
- O << "\tNode" << (void*)Node << " [";
+class GraphWriter {
+ raw_ostream &O;
+ const GraphType &G;
+
+ typedef DOTGraphTraits<GraphType> DOTTraits;
+ typedef GraphTraits<GraphType> GTraits;
+ typedef typename GTraits::NodeType NodeType;
+ typedef typename GTraits::nodes_iterator node_iterator;
+ typedef typename GTraits::ChildIteratorType child_iterator;
+ DOTTraits DTraits;
+
+ // Writes the edge labels of the node to O and returns true if there are any
+ // edge labels not equal to the empty string "".
+ bool getEdgeSourceLabels(raw_ostream &O, NodeType *Node) {
+ child_iterator EI = GTraits::child_begin(Node);
+ child_iterator EE = GTraits::child_end(Node);
+ bool hasEdgeSourceLabels = false;
+
+ for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
+ std::string label = DTraits.getEdgeSourceLabel(Node, EI);
+
+ if (label.empty())
+ continue;
+
+ hasEdgeSourceLabels = true;
+
+ if (i)
+ O << "|";
+
+ O << "<s" << i << ">" << DOT::EscapeString(label);
+ }
+
+ if (EI != EE && hasEdgeSourceLabels)
+ O << "|<s64>truncated...";
+
+ return hasEdgeSourceLabels;
+ }
+
+public:
+ GraphWriter(raw_ostream &o, const GraphType &g, bool SN) : O(o), G(g) {
+ DTraits = DOTTraits(SN);
+ }
+
+ void writeGraph(const std::string &Title = "") {
+ // Output the header for the graph...
+ writeHeader(Title);
+
+ // Emit all of the nodes in the graph...
+ writeNodes();
+
+ // Output any customizations on the graph
+ DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, *this);
+
+ // Output the end of the graph
+ writeFooter();
+ }
+
+ void writeHeader(const std::string &Title) {
+ std::string GraphName = DTraits.getGraphName(G);
+
+ if (!Title.empty())
+ O << "digraph \"" << DOT::EscapeString(Title) << "\" {\n";
+ else if (!GraphName.empty())
+ O << "digraph \"" << DOT::EscapeString(GraphName) << "\" {\n";
+ else
+ O << "digraph unnamed {\n";
+
+ if (DTraits.renderGraphFromBottomUp())
+ O << "\trankdir=\"BT\";\n";
+
+ if (!Title.empty())
+ O << "\tlabel=\"" << DOT::EscapeString(Title) << "\";\n";
+ else if (!GraphName.empty())
+ O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
+ O << DTraits.getGraphProperties(G);
+ O << "\n";
+ }
+
+ void writeFooter() {
+ // Finish off the graph
+ O << "}\n";
+ }
+
+ void writeNodes() {
+ // Loop over the graph, printing it out...
+ for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
+ I != E; ++I)
+ if (!isNodeHidden(*I))
+ writeNode(*I);
+ }
+
+ bool isNodeHidden(NodeType &Node) {
+ return isNodeHidden(&Node);
+ }
+
+ bool isNodeHidden(NodeType *const *Node) {
+ return isNodeHidden(*Node);
+ }
+
+ bool isNodeHidden(NodeType *Node) {
+ return DTraits.isNodeHidden(Node);
+ }
+
+ void writeNode(NodeType& Node) {
+ writeNode(&Node);
+ }
+
+ void writeNode(NodeType *const *Node) {
+ writeNode(*Node);
+ }
+
+ void writeNode(NodeType *Node) {
+ std::string NodeAttributes = DTraits.getNodeAttributes(Node, G);
+
+ O << "\tNode" << static_cast<const void*>(Node) << " [shape=record,";