CBMC
class_hierarchy.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Class Hierarchy
4 
5 Author: Daniel Kroening
6 
7 Date: April 2016
8 
9 \*******************************************************************/
10 
13 
14 #ifndef CPROVER_GOTO_PROGRAMS_CLASS_HIERARCHY_H
15 #define CPROVER_GOTO_PROGRAMS_CLASS_HIERARCHY_H
16 
17 #include <iosfwd>
18 #include <map>
19 #include <unordered_map>
20 
21 #include <util/graph.h>
22 #include <util/irep.h>
23 
24 // clang-format off
25 #define OPT_SHOW_CLASS_HIERARCHY \
26  "(show-class-hierarchy)"
27 
28 #define HELP_SHOW_CLASS_HIERARCHY \
29  " --show-class-hierarchy show the class hierarchy\n"
30 // clang-format on
31 
32 class symbol_tablet;
33 class json_stream_arrayt;
35 
43 {
44 public:
45  typedef std::vector<irep_idt> idst;
46 
47  class entryt
48  {
49  public:
52  };
53 
54  typedef std::map<irep_idt, entryt> class_mapt;
56 
57  void operator()(const symbol_tablet &);
58 
59  class_hierarchyt() = default;
60  explicit class_hierarchyt(const symbol_tablet &symbol_table)
61  {
62  (*this)(symbol_table);
63  }
64  class_hierarchyt(const class_hierarchyt &) = delete;
65  class_hierarchyt &operator=(const class_hierarchyt &) = delete;
66 
67  // transitively gets all children
68  idst get_children_trans(const irep_idt &id) const
69  {
70  idst result;
71  get_children_trans_rec(id, result);
72  return result;
73  }
74 
75  // transitively gets all parents
76  idst get_parents_trans(const irep_idt &id) const
77  {
78  idst result;
79  get_parents_trans_rec(id, result);
80  return result;
81  }
82 
83  void output(std::ostream &, bool children_only) const;
84  void output_dot(std::ostream &) const;
85  void output(json_stream_arrayt &, bool children_only) const;
86 
87 protected:
88  void get_children_trans_rec(const irep_idt &, idst &) const;
89  void get_parents_trans_rec(const irep_idt &, idst &) const;
90 };
91 
93 class class_hierarchy_graph_nodet : public graph_nodet<empty_edget>
94 {
95 public:
98 };
99 
102 class class_hierarchy_grapht : public grapht<class_hierarchy_graph_nodet>
103 {
104 public:
105  typedef std::vector<irep_idt> idst;
106 
108  typedef std::unordered_map<irep_idt, node_indext> nodes_by_namet;
109 
110  void populate(const symbol_tablet &);
111 
115  {
116  return nodes_by_name;
117  }
118 
119  idst get_direct_children(const irep_idt &c) const;
120 
121  idst get_children_trans(const irep_idt &c) const;
122 
123  idst get_parents_trans(const irep_idt &c) const;
124 
125 private:
128 
129  idst ids_from_indices(const std::vector<node_indext> &nodes) const;
130 
131  idst get_other_reachable_ids(const irep_idt &c, bool forwards) const;
132 };
133 
139  const class_hierarchyt &hierarchy,
140  ui_message_handlert &message_handler,
141  bool children_only = false);
142 
143 #endif // CPROVER_GOTO_PROGRAMS_CLASS_HIERARCHY_H
class_hierarchy_grapht::idst
std::vector< irep_idt > idst
Definition: class_hierarchy.h:105
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
class_hierarchy_grapht::get_parents_trans
idst get_parents_trans(const irep_idt &c) const
Get all the classes that class c inherits from (directly or indirectly).
Definition: class_hierarchy.cpp:124
symbol_tablet
The symbol table.
Definition: symbol_table.h:13
class_hierarchy_grapht::ids_from_indices
idst ids_from_indices(const std::vector< node_indext > &nodes) const
Helper function that converts a vector of node_indext to a vector of ids that are stored in the corre...
Definition: class_hierarchy.cpp:67
class_hierarchyt
Non-graph-based representation of the class hierarchy.
Definition: class_hierarchy.h:42
ui_message_handlert
Definition: ui_message.h:21
class_hierarchyt::operator()
void operator()(const symbol_tablet &)
Looks for all the struct types in the symbol table and construct a map from class names to a data str...
Definition: class_hierarchy.cpp:150
class_hierarchyt::output
void output(std::ostream &, bool children_only) const
Output the class hierarchy in plain text.
Definition: class_hierarchy.cpp:199
class_hierarchyt::get_children_trans_rec
void get_children_trans_rec(const irep_idt &, idst &) const
Definition: class_hierarchy.cpp:129
grapht
A generic directed graph with a parametric node type.
Definition: graph.h:166
class_hierarchyt::get_parents_trans_rec
void get_parents_trans_rec(const irep_idt &, idst &) const
Get all the classes that class c inherits from (directly or indirectly).
Definition: class_hierarchy.cpp:179
class_hierarchy_grapht::get_children_trans
idst get_children_trans(const irep_idt &c) const
Get all the classes that inherit (directly or indirectly) from class c.
Definition: class_hierarchy.cpp:115
class_hierarchyt::class_hierarchyt
class_hierarchyt()=default
class_hierarchyt::operator=
class_hierarchyt & operator=(const class_hierarchyt &)=delete
class_hierarchyt::entryt
Definition: class_hierarchy.h:47
class_hierarchy_graph_nodet::class_identifier
irep_idt class_identifier
Class ID for this node.
Definition: class_hierarchy.h:97
class_hierarchy_grapht::nodes_by_namet
std::unordered_map< irep_idt, node_indext > nodes_by_namet
Maps class identifiers onto node indices.
Definition: class_hierarchy.h:108
class_hierarchyt::entryt::children
idst children
Definition: class_hierarchy.h:50
class_hierarchy_grapht::nodes_by_name
nodes_by_namet nodes_by_name
Maps class identifiers onto node indices.
Definition: class_hierarchy.h:127
json_stream_arrayt
Provides methods for streaming JSON arrays.
Definition: json_stream.h:92
class_hierarchyt::class_hierarchyt
class_hierarchyt(const symbol_tablet &symbol_table)
Definition: class_hierarchy.h:60
show_class_hierarchy
void show_class_hierarchy(const class_hierarchyt &hierarchy, ui_message_handlert &message_handler, bool children_only=false)
Output the class hierarchy.
Definition: class_hierarchy.cpp:262
class_hierarchyt::idst
std::vector< irep_idt > idst
Definition: class_hierarchy.h:45
grapht< class_hierarchy_graph_nodet >::nodes
nodest nodes
Definition: graph.h:176
class_hierarchyt::class_map
class_mapt class_map
Definition: class_hierarchy.h:55
class_hierarchy_grapht::get_nodes_by_class_identifier
const nodes_by_namet & get_nodes_by_class_identifier() const
Get map from class identifier to node index.
Definition: class_hierarchy.h:114
class_hierarchyt::class_mapt
std::map< irep_idt, entryt > class_mapt
Definition: class_hierarchy.h:54
graph.h
graph_nodet
This class represents a node in a directed graph.
Definition: graph.h:34
class_hierarchy_grapht
Class hierarchy, represented using grapht and therefore suitable for use with generic graph algorithm...
Definition: class_hierarchy.h:102
class_hierarchy_grapht::get_other_reachable_ids
idst get_other_reachable_ids(const irep_idt &c, bool forwards) const
Helper function for get_children_trans and get_parents_trans
Definition: class_hierarchy.cpp:93
class_hierarchy_graph_nodet
Class hierarchy graph node: simply contains a class identifier.
Definition: class_hierarchy.h:93
class_hierarchyt::output_dot
void output_dot(std::ostream &) const
Output class hierarchy in Graphviz DOT format.
Definition: class_hierarchy.cpp:218
class_hierarchyt::get_parents_trans
idst get_parents_trans(const irep_idt &id) const
Definition: class_hierarchy.h:76
class_hierarchyt::entryt::parents
idst parents
Definition: class_hierarchy.h:50
class_hierarchy_grapht::get_direct_children
idst get_direct_children(const irep_idt &c) const
Get all the classes that directly (i.e.
Definition: class_hierarchy.cpp:85
class_hierarchyt::get_children_trans
idst get_children_trans(const irep_idt &id) const
Definition: class_hierarchy.h:68
class_hierarchyt::entryt::is_abstract
bool is_abstract
Definition: class_hierarchy.h:51
irep.h
class_hierarchy_grapht::populate
void populate(const symbol_tablet &)
Populate the class hierarchy graph, such that there is a node for every struct type in the symbol tab...
Definition: class_hierarchy.cpp:29