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-2026 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
24#include "analyzer/supergraph.h"
26
27namespace ana {
28
29class epath_finder;
30
31/* A bundle of information capturing where a pending_diagnostic should
32 be emitted. */
33
35{
36public:
38 {
39 public:
40 virtual ~fixer_for_epath () = default;
41
42 virtual void
44 pending_location &ploc) const = 0;
45 };
46
50 location_t loc);
51
52 location_t get_location () const
53 {
54 return m_event_loc_info.m_loc;
55 }
56
57 std::unique_ptr<json::object> to_json () const;
58
59 /* The enode with which a diagnostic is to be associated,
60 for tracking feasibility. */
62
63 /* Information to use for the location of the warning,
64 and for the location of the "final warning" in the path. */
66
67 /* Optional hook for use when eventually emitting the diagnostic
68 for fixing up m_event_loc_info based on the specific epath. */
69 std::unique_ptr<fixer_for_epath> m_fixer_for_epath;
70};
71
72extern std::unique_ptr<pending_location::fixer_for_epath>
74 tree var);
75
76/* A to-be-emitted diagnostic stored within diagnostic_manager. */
77
79{
80public:
82 pending_location &&ploc,
83 tree var, const svalue *sval,
85 std::unique_ptr<pending_diagnostic> d,
86 unsigned idx);
87
88 bool operator== (const saved_diagnostic &other) const;
89
90 void add_note (std::unique_ptr<pending_note> pn);
91 void add_event (std::unique_ptr<checker_event> event);
92
93 std::unique_ptr<json::object> to_json () const;
94
95 void dump_dot_id (pretty_printer *pp) const;
97
99 {
100 return m_problem.get ();
101 }
102
103 bool calc_best_epath (epath_finder *pf);
104 const exploded_path *get_best_epath () const { return m_best_epath.get (); }
105 unsigned get_epath_length () const;
106
108 unsigned get_num_dupes () const { return m_duplicates.length (); }
109
110 unsigned get_index () const { return m_idx; }
111
112 bool supercedes_p (const saved_diagnostic &other) const;
113
115
116 void emit_any_notes () const;
117
118 void
120
121 const supernode *get_supernode () const;
122
123 //private:
129 std::unique_ptr<pending_diagnostic> m_d;
131
132private:
134
135 unsigned m_idx;
136 std::unique_ptr<exploded_path> m_best_epath;
137 std::unique_ptr<feasibility_problem> m_problem;
138
140 auto_delete_vec <pending_note> m_notes;
141
142 /* Optionally: additional context-dependent events to be emitted
143 immediately before the warning_event, giving more details of what
144 operation was being simulated when a diagnostic was saved
145 e.g. "looking for null terminator in param 2 of 'foo'". */
146 auto_delete_vec <checker_event> m_saved_events;
147};
148
149class path_builder;
150
151/* A class with responsibility for saving pending diagnostics, so that
152 they can be emitted after the exploded_graph is complete.
153 This lets us de-duplicate diagnostics, and find the shortest path
154 for each similar diagnostic, potentially using edges that might
155 not have been found when each diagnostic was first saved.
156
157 This also lets us compute shortest_paths once, rather than
158 per-diagnostic. */
159
161{
162public:
163 diagnostic_manager (logger *logger, engine *eng, int verbosity);
164
165 engine *get_engine () const { return m_eng; }
166
167 std::unique_ptr<json::object> to_json () const;
168
170 pending_location &&ploc,
171 tree var,
172 const svalue *sval,
174 std::unique_ptr<pending_diagnostic> d);
175
177 std::unique_ptr<pending_diagnostic> d);
178
179 void add_note (std::unique_ptr<pending_note> pn);
180 void add_event (std::unique_ptr<checker_event> event);
181
183
185 saved_diagnostic &sd);
186
187 unsigned get_num_diagnostics () const
188 {
189 return m_saved_diagnostics.length ();
190 }
192 {
193 return m_saved_diagnostics[idx];
194 }
195 const saved_diagnostic *get_saved_diagnostic (unsigned idx) const
196 {
197 return m_saved_diagnostics[idx];
198 }
199
200private:
203
204 void build_emission_path (const path_builder &pb,
205 const exploded_path &epath,
206 checker_path *emission_path) const;
207
208 void add_event_on_final_node (const path_builder &pb,
209 const exploded_node *final_enode,
210 checker_path *emission_path,
211 interesting_t *interest) const;
212
213 void add_events_for_eedge (const path_builder &pb,
214 const exploded_edge &eedge,
215 checker_path *emission_path,
216 interesting_t *interest) const;
217
218 bool significant_edge_p (const path_builder &pb,
219 const exploded_edge &eedge) const;
220
222 const state_machine *sm,
223 const svalue *sval,
225
227 const state_machine *sm,
228 tree var,
231 const state_machine *sm,
232 const svalue *sval,
240
243 const int m_verbosity;
245};
246
247} // namespace ana
248
249#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:187
std::unique_ptr< json::object > to_json() const
const diagnostics::logical_locations::manager & get_logical_location_manager() 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
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:243
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
int m_num_disabled_diagnostics
Definition diagnostic-manager.h:244
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:195
void prune_for_sm_diagnostic(checker_path *path, const state_machine *sm, tree var, state_machine::state_t state) const
bool add_diagnostic(const state_machine *sm, pending_location &&ploc, tree var, const svalue *sval, state_machine::state_t state, std::unique_ptr< pending_diagnostic > d)
void add_event(std::unique_ptr< checker_event > event)
void finish_pruning(checker_path *path) const
auto_delete_vec< saved_diagnostic > m_saved_diagnostics
Definition diagnostic-manager.h:242
void add_events_for_eedge(const path_builder &pb, const exploded_edge &eedge, checker_path *emission_path, interesting_t *interest) const
bool add_diagnostic(pending_location &&ploc, std::unique_ptr< pending_diagnostic > d)
engine * m_eng
Definition diagnostic-manager.h:241
void consolidate_unwind_events(checker_path *path) const
saved_diagnostic * get_saved_diagnostic(unsigned idx)
Definition diagnostic-manager.h:191
void prune_system_headers(checker_path *path) const
void prune_interproc_events(checker_path *path) const
engine * get_engine() const
Definition diagnostic-manager.h:165
Definition region-model.h:1334
Definition exploded-graph.h:335
Definition exploded-graph.h:783
Definition exploded-graph.h:206
Definition exploded-graph.h:932
Definition exploded-graph.h:960
log_user(logger *logger)
Definition analyzer-logging.h:34
Definition diagnostic-manager.h:38
virtual void fixup_for_epath(const exploded_path &epath, pending_location &ploc) const =0
Definition diagnostic-manager.h:79
void maybe_add_sarif_properties(diagnostics::sarif_object &result_obj) const
void add_any_saved_events(checker_path &dst_path)
void add_duplicate(saved_diagnostic *other)
const state_machine * m_sm
Definition diagnostic-manager.h:124
void emit_any_notes() const
std::unique_ptr< exploded_path > m_best_epath
Definition diagnostic-manager.h:136
pending_location m_ploc
Definition diagnostic-manager.h:125
const exploded_path * get_best_epath() const
Definition diagnostic-manager.h:104
const svalue * m_sval
Definition diagnostic-manager.h:127
void dump_dot_id(pretty_printer *pp) const
auto_delete_vec< pending_note > m_notes
Definition diagnostic-manager.h:140
const feasibility_problem * get_feasibility_problem() const
Definition diagnostic-manager.h:98
std::unique_ptr< feasibility_problem > m_problem
Definition diagnostic-manager.h:137
void add_note(std::unique_ptr< pending_note > pn)
state_machine::state_t m_state
Definition diagnostic-manager.h:128
const supernode * get_supernode() const
void add_event(std::unique_ptr< checker_event > event)
unsigned m_idx
Definition diagnostic-manager.h:135
tree m_var
Definition diagnostic-manager.h:126
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:129
const exploded_edge * m_trailing_eedge
Definition diagnostic-manager.h:130
std::unique_ptr< json::object > to_json() const
auto_vec< const saved_diagnostic * > m_duplicates
Definition diagnostic-manager.h:139
auto_delete_vec< checker_event > m_saved_events
Definition diagnostic-manager.h:146
unsigned get_epath_length() const
bool operator==(const saved_diagnostic &other) const
unsigned get_num_dupes() const
Definition diagnostic-manager.h:108
saved_diagnostic(const state_machine *sm, pending_location &&ploc, tree var, const svalue *sval, state_machine::state_t state, std::unique_ptr< pending_diagnostic > d, unsigned idx)
unsigned get_index() const
Definition diagnostic-manager.h:110
Definition sm.h:43
const state_machine::state * state_t
Definition sm.h:63
Definition supergraph.h:224
Definition svalue.h:92
Definition vec.h:1813
Definition vec.h:1667
Definition logical-locations.h:147
Definition sarif-sink.h:151
Definition genmatch.cc:1506
Definition pretty-print.h:241
static struct path_prefix cpath path
Definition collect2.cc:514
union tree_node * tree
Definition coretypes.h:97
Definition access-diagram.h:30
std::unique_ptr< pending_location::fixer_for_epath > make_ploc_fixer_for_epath_for_leak_diagnostic(const exploded_graph &eg, tree var)
Definition event-loc-info.h:29
Definition pending-diagnostic.h:37
Definition diagnostic-manager.h:35
pending_location(exploded_node *enode, location_t loc)
exploded_node * m_enode
Definition diagnostic-manager.h:61
std::unique_ptr< json::object > to_json() const
event_loc_info m_event_loc_info
Definition diagnostic-manager.h:65
pending_location(exploded_node *enode)
std::unique_ptr< fixer_for_epath > m_fixer_for_epath
Definition diagnostic-manager.h:69
location_t get_location() const
Definition diagnostic-manager.h:52
Definition genautomata.cc:669