digraph.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* Template classes for directed graphs.
  2. Copyright (C) 2019-2020 Free Software Foundation, Inc.
  3. Contributed by David Malcolm <dmalcolm@redhat.com>.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef GCC_DIGRAPH_H
  17. #define GCC_DIGRAPH_H
  18. #include "diagnostic.h"
  19. #include "tree-diagnostic.h" /* for default_tree_printer. */
  20. #include "graphviz.h"
  21. /* Templates for a family of classes: digraph, node, edge, and cluster.
  22. This assumes a traits type with the following typedefs:
  23. node_t: the node class
  24. edge_t: the edge class
  25. dump_args_t: additional args for dot-dumps
  26. cluster_t: the cluster class (for use when generating .dot files).
  27. Using a template allows for typesafe nodes and edges: a node's
  28. predecessor and successor edges can be of a node-specific edge
  29. subclass, without needing casting. */
  30. /* Abstract base class for a node in a directed graph. */
  31. template <typename GraphTraits>
  32. class dnode
  33. {
  34. public:
  35. typedef typename GraphTraits::edge_t edge_t;
  36. typedef typename GraphTraits::dump_args_t dump_args_t;
  37. virtual ~dnode () {}
  38. virtual void dump_dot (graphviz_out *gv, const dump_args_t &args) const = 0;
  39. auto_vec<edge_t *> m_preds;
  40. auto_vec<edge_t *> m_succs;
  41. };
  42. /* Abstract base class for an edge in a directed graph. */
  43. template <typename GraphTraits>
  44. class dedge
  45. {
  46. public:
  47. typedef typename GraphTraits::node_t node_t;
  48. typedef typename GraphTraits::dump_args_t dump_args_t;
  49. dedge (node_t *src, node_t *dest)
  50. : m_src (src), m_dest (dest) {}
  51. virtual ~dedge () {}
  52. virtual void dump_dot (graphviz_out *gv, const dump_args_t &args) const = 0;
  53. node_t *const m_src;
  54. node_t *const m_dest;
  55. };
  56. /* Abstract base class for a directed graph.
  57. This class maintains the vectors of nodes and edges,
  58. and owns the nodes and edges. */
  59. template <typename GraphTraits>
  60. class digraph
  61. {
  62. public:
  63. typedef typename GraphTraits::node_t node_t;
  64. typedef typename GraphTraits::edge_t edge_t;
  65. typedef typename GraphTraits::dump_args_t dump_args_t;
  66. typedef typename GraphTraits::cluster_t cluster_t;
  67. digraph () {}
  68. virtual ~digraph () {}
  69. void dump_dot_to_pp (pretty_printer *pp,
  70. cluster_t *root_cluster,
  71. const dump_args_t &args) const;
  72. void dump_dot_to_file (FILE *fp,
  73. cluster_t *root_cluster,
  74. const dump_args_t &args) const;
  75. void dump_dot (const char *path,
  76. cluster_t *root_cluster,
  77. const dump_args_t &args) const;
  78. void add_node (node_t *node);
  79. void add_edge (edge_t *edge);
  80. auto_delete_vec<node_t> m_nodes;
  81. auto_delete_vec<edge_t> m_edges;
  82. };
  83. /* Abstract base class for splitting dnodes into hierarchical clusters
  84. in the generated .dot file.
  85. See "Subgraphs and Clusters" within
  86. https://www.graphviz.org/doc/info/lang.html
  87. and e.g.
  88. https://graphviz.gitlab.io/_pages/Gallery/directed/cluster.html
  89. If a root_cluster is passed to dump_dot*, then all nodes will be
  90. added to it at the start of dumping, via calls to add_node.
  91. The root cluster can organize the nodes into a hierarchy of
  92. child clusters.
  93. After all nodes are added to the root cluster, dump_dot will then
  94. be called on it (and not on the nodes themselves). */
  95. template <typename GraphTraits>
  96. class cluster
  97. {
  98. public:
  99. typedef typename GraphTraits::node_t node_t;
  100. typedef typename GraphTraits::dump_args_t dump_args_t;
  101. virtual ~cluster () {}
  102. virtual void add_node (node_t *node) = 0;
  103. /* Recursively dump the cluster, all nodes, and child clusters. */
  104. virtual void dump_dot (graphviz_out *gv, const dump_args_t &) const = 0;
  105. };
  106. /* Write .dot information for this graph to PP, passing ARGS to the nodes
  107. and edges.
  108. If ROOT_CLUSTER is non-NULL, use it to organize the nodes into clusters. */
  109. template <typename GraphTraits>
  110. inline void
  111. digraph<GraphTraits>::dump_dot_to_pp (pretty_printer *pp,
  112. cluster_t *root_cluster,
  113. const dump_args_t &args) const
  114. {
  115. graphviz_out gv (pp);
  116. pp_string (pp, "digraph \"");
  117. pp_string (pp, "base");
  118. pp_string (pp, "\" {\n");
  119. gv.indent ();
  120. pp_string (pp, "overlap=false;\n");
  121. pp_string (pp, "compound=true;\n");
  122. /* If using clustering, emit all nodes via clusters. */
  123. if (root_cluster)
  124. {
  125. int i;
  126. node_t *n;
  127. FOR_EACH_VEC_ELT (m_nodes, i, n)
  128. root_cluster->add_node (n);
  129. root_cluster->dump_dot (&gv, args);
  130. }
  131. else
  132. {
  133. /* Otherwise, display all nodes at top level. */
  134. int i;
  135. node_t *n;
  136. FOR_EACH_VEC_ELT (m_nodes, i, n)
  137. n->dump_dot (&gv, args);
  138. }
  139. /* Edges. */
  140. int i;
  141. edge_t *e;
  142. FOR_EACH_VEC_ELT (m_edges, i, e)
  143. e->dump_dot (&gv, args);
  144. /* Terminate "digraph" */
  145. gv.outdent ();
  146. pp_string (pp, "}");
  147. pp_newline (pp);
  148. }
  149. /* Write .dot information for this graph to FP, passing ARGS to the nodes
  150. and edges.
  151. If ROOT_CLUSTER is non-NULL, use it to organize the nodes into clusters. */
  152. template <typename GraphTraits>
  153. inline void
  154. digraph<GraphTraits>::dump_dot_to_file (FILE *fp,
  155. cluster_t *root_cluster,
  156. const dump_args_t &args) const
  157. {
  158. pretty_printer pp;
  159. // TODO:
  160. pp_format_decoder (&pp) = default_tree_printer;
  161. pp.buffer->stream = fp;
  162. dump_dot_to_pp (&pp, root_cluster, args);
  163. pp_flush (&pp);
  164. }
  165. /* Write .dot information for this graph to a file at PATH, passing ARGS
  166. to the nodes and edges.
  167. If ROOT_CLUSTER is non-NULL, use it to organize the nodes into clusters. */
  168. template <typename GraphTraits>
  169. inline void
  170. digraph<GraphTraits>::dump_dot (const char *path,
  171. cluster_t *root_cluster,
  172. const dump_args_t &args) const
  173. {
  174. FILE *fp = fopen (path, "w");
  175. dump_dot_to_file (fp, root_cluster, args);
  176. fclose (fp);
  177. }
  178. /* Add NODE to this DIGRAPH, taking ownership. */
  179. template <typename GraphTraits>
  180. inline void
  181. digraph<GraphTraits>::add_node (node_t *node)
  182. {
  183. m_nodes.safe_push (node);
  184. }
  185. /* Add EDGE to this digraph, and to the preds/succs of its endpoints.
  186. Take ownership of EDGE. */
  187. template <typename GraphTraits>
  188. inline void
  189. digraph<GraphTraits>::add_edge (edge_t *edge)
  190. {
  191. m_edges.safe_push (edge);
  192. edge->m_dest->m_preds.safe_push (edge);
  193. edge->m_src->m_succs.safe_push (edge);
  194. }
  195. #endif /* GCC_DIGRAPH_H */