analyzer: use objects for state_machine::state_t
[gcc.git] / gcc / analyzer / sm.cc
1 /* Modeling API uses and misuses via state machines.
2 Copyright (C) 2019-2020 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "function.h"
26 #include "basic-block.h"
27 #include "gimple.h"
28 #include "options.h"
29 #include "function.h"
30 #include "diagnostic-core.h"
31 #include "pretty-print.h"
32 #include "analyzer/analyzer.h"
33 #include "analyzer/analyzer-logging.h"
34 #include "analyzer/sm.h"
35
36 #if ENABLE_ANALYZER
37
38 namespace ana {
39
40 /* Return true if VAR has pointer or reference type. */
41
42 bool
43 any_pointer_p (tree var)
44 {
45 return POINTER_TYPE_P (TREE_TYPE (var));
46 }
47
48
49 /* class state_machine::state. */
50
51 /* Base implementation of dump_to_pp vfunc. */
52
53 void
54 state_machine::state::dump_to_pp (pretty_printer *pp) const
55 {
56 pp_string (pp, m_name);
57 }
58
59 /* class state_machine. */
60
61 /* state_machine's ctor. */
62
63 state_machine::state_machine (const char *name, logger *logger)
64 : log_user (logger), m_name (name), m_next_state_id (0),
65 m_start (add_state ("start"))
66 {
67 }
68
69 /* Add a state with name NAME to this state_machine.
70 The string is required to outlive the state_machine.
71
72 Return the state_t for the new state. */
73
74 state_machine::state_t
75 state_machine::add_state (const char *name)
76 {
77 state *s = new state (name, alloc_state_id ());
78 m_states.safe_push (s);
79 return s;
80 }
81
82 /* Get the state with name NAME, which must exist.
83 This is purely intended for use in selftests. */
84
85 state_machine::state_t
86 state_machine::get_state_by_name (const char *name) const
87 {
88 unsigned i;
89 state *s;
90 FOR_EACH_VEC_ELT (m_states, i, s)
91 if (!strcmp (name, s->get_name ()))
92 return s;
93 /* Name not found. */
94 gcc_unreachable ();
95 }
96
97 /* Dump a multiline representation of this state machine to PP. */
98
99 void
100 state_machine::dump_to_pp (pretty_printer *pp) const
101 {
102 unsigned i;
103 state *s;
104 FOR_EACH_VEC_ELT (m_states, i, s)
105 {
106 pp_printf (pp, " state %i: ", i);
107 s->dump_to_pp (pp);
108 pp_newline (pp);
109 }
110 }
111
112 /* Create instances of the various state machines, each using LOGGER,
113 and populate OUT with them. */
114
115 void
116 make_checkers (auto_delete_vec <state_machine> &out, logger *logger)
117 {
118 out.safe_push (make_malloc_state_machine (logger));
119 out.safe_push (make_fileptr_state_machine (logger));
120 /* The "taint" checker must be explicitly enabled (as it currently
121 leads to state explosions that stop the other checkers working). */
122 if (flag_analyzer_checker)
123 out.safe_push (make_taint_state_machine (logger));
124 out.safe_push (make_sensitive_state_machine (logger));
125 out.safe_push (make_signal_state_machine (logger));
126
127 /* We only attempt to run the pattern tests if it might have been manually
128 enabled (for DejaGnu purposes). */
129 if (flag_analyzer_checker)
130 out.safe_push (make_pattern_test_state_machine (logger));
131
132 if (flag_analyzer_checker)
133 {
134 unsigned read_index, write_index;
135 state_machine **sm;
136
137 /* TODO: this leaks the machines
138 Would be nice to log the things that were removed. */
139 VEC_ORDERED_REMOVE_IF (out, read_index, write_index, sm,
140 0 != strcmp (flag_analyzer_checker,
141 (*sm)->get_name ()));
142 }
143 }
144
145 } // namespace ana
146
147 #endif /* #if ENABLE_ANALYZER */