GCC Middle and Back End API Reference
diagnostic-manager.h
Go to the documentation of this file.
1/* Classes for saving, deduplicating, and emitting analyzer diagnostics.
2 Copyright (C) 2019-2025 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#ifndef GCC_ANALYZER_DIAGNOSTIC_MANAGER_H
22#define GCC_ANALYZER_DIAGNOSTIC_MANAGER_H
23
24namespace ana {
25
26class epath_finder;
27
28/* A to-be-emitted diagnostic stored within diagnostic_manager. */
29
31{
32public:
34 const pending_location &ploc,
35 tree var, const svalue *sval,
37 std::unique_ptr<pending_diagnostic> d,
38 unsigned idx);
39
40 bool operator== (const saved_diagnostic &other) const;
41
42 void add_note (std::unique_ptr<pending_note> pn);
43 void add_event (std::unique_ptr<checker_event> event);
44
45 std::unique_ptr<json::object> to_json () const;
46
47 void dump_dot_id (pretty_printer *pp) const;
49
51 {
52 return m_problem.get ();
53 }
54
55 bool calc_best_epath (epath_finder *pf);
56 const exploded_path *get_best_epath () const { return m_best_epath.get (); }
57 unsigned get_epath_length () const;
58
60 unsigned get_num_dupes () const { return m_duplicates.length (); }
61
62 unsigned get_index () const { return m_idx; }
63
64 bool supercedes_p (const saved_diagnostic &other) const;
65
67
68 void emit_any_notes () const;
69
70 void maybe_add_sarif_properties (sarif_object &result_obj) const;
71
72 //private:
76 const gimple *m_stmt;
77 std::unique_ptr<stmt_finder> m_stmt_finder;
78 location_t m_loc;
80 const svalue *m_sval;
82 std::unique_ptr<pending_diagnostic> m_d;
84
85private:
87
88 unsigned m_idx;
89 std::unique_ptr<exploded_path> m_best_epath;
90 std::unique_ptr<feasibility_problem> m_problem;
91
93 auto_delete_vec <pending_note> m_notes;
94
95 /* Optionally: additional context-dependent events to be emitted
96 immediately before the warning_event, giving more details of what
97 operation was being simulated when a diagnostic was saved
98 e.g. "looking for null terminator in param 2 of 'foo'". */
99 auto_delete_vec <checker_event> m_saved_events;
100};
101
102class path_builder;
103
104/* A bundle of information capturing where a pending_diagnostic should
105 be emitted. */
106
108{
109public:
111 const supernode *snode,
112 const gimple *stmt,
113 const stmt_finder *finder)
114 : m_enode (enode),
115 m_snode (snode),
116 m_stmt (stmt),
117 m_finder (finder),
119 {
121 }
122
123 /* ctor for cases where we have a location_t but there isn't any
124 gimple stmt associated with the diagnostic. */
125
127 const supernode *snode,
128 location_t loc)
129 : m_enode (enode),
130 m_snode (snode),
131 m_stmt (nullptr),
132 m_finder (nullptr),
133 m_loc (loc)
134 {
135 }
136
141 location_t m_loc;
142};
143
144/* A class with responsibility for saving pending diagnostics, so that
145 they can be emitted after the exploded_graph is complete.
146 This lets us de-duplicate diagnostics, and find the shortest path
147 for each similar diagnostic, potentially using edges that might
148 not have been found when each diagnostic was first saved.
149
150 This also lets us compute shortest_paths once, rather than
151 per-diagnostic. */
152
154{
155public:
156 diagnostic_manager (logger *logger, engine *eng, int verbosity);
157
158 engine *get_engine () const { return m_eng; }
159
160 std::unique_ptr<json::object> to_json () const;
161
163 const pending_location &ploc,
164 tree var,
165 const svalue *sval,
167 std::unique_ptr<pending_diagnostic> d);
168
170 std::unique_ptr<pending_diagnostic> d);
171
172 void add_note (std::unique_ptr<pending_note> pn);
173 void add_event (std::unique_ptr<checker_event> event);
174
176
178 saved_diagnostic &sd);
179
180 unsigned get_num_diagnostics () const
181 {
182 return m_saved_diagnostics.length ();
183 }
185 {
186 return m_saved_diagnostics[idx];
187 }
188 const saved_diagnostic *get_saved_diagnostic (unsigned idx) const
189 {
190 return m_saved_diagnostics[idx];
191 }
192
193private:
196
197 void build_emission_path (const path_builder &pb,
198 const exploded_path &epath,
199 checker_path *emission_path) const;
200
201 void add_event_on_final_node (const path_builder &pb,
202 const exploded_node *final_enode,
203 checker_path *emission_path,
204 interesting_t *interest) const;
205
206 void add_events_for_eedge (const path_builder &pb,
207 const exploded_edge &eedge,
208 checker_path *emission_path,
209 interesting_t *interest) const;
210
211 bool significant_edge_p (const path_builder &pb,
212 const exploded_edge &eedge) const;
213
214 void add_events_for_superedge (const path_builder &pb,
215 const exploded_edge &eedge,
216 checker_path *emission_path) const;
217
219 const state_machine *sm,
220 const svalue *sval,
222
224 const state_machine *sm,
225 tree var,
228 const state_machine *sm,
229 const svalue *sval,
237
240 const int m_verbosity;
242};
243
244} // namespace ana
245
246#endif /* GCC_ANALYZER_DIAGNOSTIC_MANAGER_H */
Definition checker-path.h:32
void build_emission_path(const path_builder &pb, const exploded_path &epath, checker_path *emission_path) const
unsigned get_num_diagnostics() const
Definition diagnostic-manager.h:180
std::unique_ptr< json::object > to_json() const
void consolidate_conditions(checker_path *path) const
void add_event_on_final_node(const path_builder &pb, const exploded_node *final_enode, checker_path *emission_path, interesting_t *interest) const
bool add_diagnostic(const state_machine *sm, const pending_location &ploc, tree var, const svalue *sval, state_machine::state_t state, std::unique_ptr< pending_diagnostic > d)
void update_for_unsuitable_sm_exprs(tree *expr) const
void add_note(std::unique_ptr< pending_note > pn)
const int m_verbosity
Definition diagnostic-manager.h:240
diagnostic_manager(logger *logger, engine *eng, int verbosity)
void emit_saved_diagnostics(const exploded_graph &eg)
void emit_saved_diagnostic(const exploded_graph &eg, saved_diagnostic &sd)
void prune_path(checker_path *path, const state_machine *sm, const svalue *sval, state_machine::state_t state) const
const logical_location_manager & get_logical_location_manager() const
int m_num_disabled_diagnostics
Definition diagnostic-manager.h:241
bool significant_edge_p(const path_builder &pb, const exploded_edge &eedge) const
void prune_for_sm_diagnostic(checker_path *path, const state_machine *sm, const svalue *sval, state_machine::state_t state) const
const saved_diagnostic * get_saved_diagnostic(unsigned idx) const
Definition diagnostic-manager.h:188
void prune_for_sm_diagnostic(checker_path *path, const state_machine *sm, tree var, state_machine::state_t state) const
void add_event(std::unique_ptr< checker_event > event)
void finish_pruning(checker_path *path) const
void add_events_for_superedge(const path_builder &pb, const exploded_edge &eedge, checker_path *emission_path) const
auto_delete_vec< saved_diagnostic > m_saved_diagnostics
Definition diagnostic-manager.h:239
void add_events_for_eedge(const path_builder &pb, const exploded_edge &eedge, checker_path *emission_path, interesting_t *interest) const
engine * m_eng
Definition diagnostic-manager.h:238
void consolidate_unwind_events(checker_path *path) const
bool add_diagnostic(const pending_location &ploc, std::unique_ptr< pending_diagnostic > d)
saved_diagnostic * get_saved_diagnostic(unsigned idx)
Definition diagnostic-manager.h:184
void prune_system_headers(checker_path *path) const
void prune_interproc_events(checker_path *path) const
engine * get_engine() const
Definition diagnostic-manager.h:158
Definition region-model.h:1338
Definition exploded-graph.h:394
Definition exploded-graph.h:804
Definition exploded-graph.h:203
Definition exploded-graph.h:965
Definition exploded-graph.h:993
log_user(logger *logger)
Definition analyzer-logging.h:34
Definition diagnostic-manager.h:31
void add_any_saved_events(checker_path &dst_path)
void add_duplicate(saved_diagnostic *other)
const state_machine * m_sm
Definition diagnostic-manager.h:73
void emit_any_notes() const
void maybe_add_sarif_properties(sarif_object &result_obj) const
std::unique_ptr< exploded_path > m_best_epath
Definition diagnostic-manager.h:89
const exploded_path * get_best_epath() const
Definition diagnostic-manager.h:56
const svalue * m_sval
Definition diagnostic-manager.h:80
void dump_dot_id(pretty_printer *pp) const
auto_delete_vec< pending_note > m_notes
Definition diagnostic-manager.h:93
const feasibility_problem * get_feasibility_problem() const
Definition diagnostic-manager.h:50
std::unique_ptr< feasibility_problem > m_problem
Definition diagnostic-manager.h:90
void add_note(std::unique_ptr< pending_note > pn)
state_machine::state_t m_state
Definition diagnostic-manager.h:81
const gimple * m_stmt
Definition diagnostic-manager.h:76
void add_event(std::unique_ptr< checker_event > event)
unsigned m_idx
Definition diagnostic-manager.h:88
saved_diagnostic(const state_machine *sm, const pending_location &ploc, tree var, const svalue *sval, state_machine::state_t state, std::unique_ptr< pending_diagnostic > d, unsigned idx)
tree m_var
Definition diagnostic-manager.h:79
DISABLE_COPY_AND_ASSIGN(saved_diagnostic)
bool calc_best_epath(epath_finder *pf)
bool supercedes_p(const saved_diagnostic &other) const
void dump_as_dot_node(pretty_printer *pp) const
std::unique_ptr< pending_diagnostic > m_d
Definition diagnostic-manager.h:82
const exploded_edge * m_trailing_eedge
Definition diagnostic-manager.h:83
std::unique_ptr< json::object > to_json() const
auto_vec< const saved_diagnostic * > m_duplicates
Definition diagnostic-manager.h:92
auto_delete_vec< checker_event > m_saved_events
Definition diagnostic-manager.h:99
unsigned get_epath_length() const
bool operator==(const saved_diagnostic &other) const
const exploded_node * m_enode
Definition diagnostic-manager.h:74
unsigned get_num_dupes() const
Definition diagnostic-manager.h:60
const supernode * m_snode
Definition diagnostic-manager.h:75
std::unique_ptr< stmt_finder > m_stmt_finder
Definition diagnostic-manager.h:77
location_t m_loc
Definition diagnostic-manager.h:78
unsigned get_index() const
Definition diagnostic-manager.h:62
Definition sm.h:40
const state_machine::state * state_t
Definition sm.h:60
Definition exploded-graph.h:1051
Definition supergraph.h:239
Definition svalue.h:92
Definition vec.h:1813
Definition vec.h:1667
Definition genmatch.cc:1506
Definition logical-location.h:91
Definition pretty-print.h:241
Definition diagnostic-format-sarif.h:137
static struct path_prefix cpath path
Definition collect2.cc:514
union tree_node * tree
Definition coretypes.h:97
#define UNKNOWN_LOCATION
Definition input.h:32
Definition access-diagram.h:30
@ stmt
Definition checker-event.h:37
Definition pending-diagnostic.h:38
Definition diagnostic-manager.h:108
exploded_node * m_enode
Definition diagnostic-manager.h:137
const gimple * m_stmt
Definition diagnostic-manager.h:139
pending_location(exploded_node *enode, const supernode *snode, const gimple *stmt, const stmt_finder *finder)
Definition diagnostic-manager.h:110
const stmt_finder * m_finder
Definition diagnostic-manager.h:140
location_t m_loc
Definition diagnostic-manager.h:141
const supernode * m_snode
Definition diagnostic-manager.h:138
pending_location(exploded_node *enode, const supernode *snode, location_t loc)
Definition diagnostic-manager.h:126
Definition gimple.h:221
Definition genautomata.cc:669
#define gcc_assert(EXPR)
Definition system.h:814