1 /* Subclasses of diagnostic_path and diagnostic_event for analyzer diagnostics.
2 Copyright (C) 2019-2020 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
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)
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.
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/>. */
21 #ifndef GCC_ANALYZER_CHECKER_PATH_H
22 #define GCC_ANALYZER_CHECKER_PATH_H
26 /* An enum for discriminating between the concrete subclasses of
41 EK_REWIND_FROM_LONGJMP
,
46 extern const char *event_kind_to_string (enum event_kind ek
);
50 The class hierarchy looks like this (using indentation to show
51 inheritance, and with event_kinds shown for the concrete subclasses):
55 debug_event (EK_DEBUG)
56 custom_event (EK_CUSTOM)
57 statement_event (EK_STMT)
58 function_entry_event (EK_FUNCTION_ENTRY)
59 state_change_event (EK_STATE_CHANGE)
62 start_cfg_edge_event (EK_START_CFG_EDGE)
63 end_cfg_edge_event (EK_END_CFG_EDGE)
64 call_event (EK_CALL_EDGE)
65 return_edge (EK_RETURN_EDGE)
66 setjmp_event (EK_SETJMP)
68 rewind_from_longjmp_event (EK_REWIND_FROM_LONGJMP)
69 rewind_to_setjmp_event (EK_REWIND_TO_SETJMP)
70 warning_event (EK_WARNING). */
72 /* Abstract subclass of diagnostic_event; the base class for use in
73 checker_path (the analyzer's diagnostic_path subclass). */
75 class checker_event
: public diagnostic_event
78 checker_event (enum event_kind kind
,
79 location_t loc
, tree fndecl
, int depth
)
80 : m_kind (kind
), m_loc (loc
), m_fndecl (fndecl
), m_depth (depth
),
81 m_pending_diagnostic (NULL
), m_emission_id ()
85 /* Implementation of diagnostic_event. */
87 location_t
get_location () const FINAL OVERRIDE
{ return m_loc
; }
88 tree
get_fndecl () const FINAL OVERRIDE
{ return m_fndecl
; }
89 int get_stack_depth () const FINAL OVERRIDE
{ return m_depth
; }
91 /* Additional functionality. */
93 virtual void prepare_for_emission (checker_path
*,
94 pending_diagnostic
*pd
,
95 diagnostic_event_id_t emission_id
);
96 virtual bool is_call_p () const { return false; }
97 virtual bool is_function_entry_p () const { return false; }
98 virtual bool is_return_p () const { return false; }
100 void dump (pretty_printer
*pp
) const;
103 const enum event_kind m_kind
;
108 pending_diagnostic
*m_pending_diagnostic
;
109 diagnostic_event_id_t m_emission_id
; // only set once all pruning has occurred
112 /* A concrete event subclass for a purely textual event, for use in
113 debugging path creation and filtering. */
115 class debug_event
: public checker_event
118 debug_event (location_t loc
, tree fndecl
, int depth
,
120 : checker_event (EK_DEBUG
, loc
, fndecl
, depth
),
121 m_desc (xstrdup (desc
))
129 label_text
get_desc (bool) const FINAL OVERRIDE
;
135 /* A concrete event subclass for custom events. These are not filtered,
136 as they are likely to be pertinent to the diagnostic. */
138 class custom_event
: public checker_event
141 custom_event (location_t loc
, tree fndecl
, int depth
,
143 : checker_event (EK_CUSTOM
, loc
, fndecl
, depth
),
144 m_desc (xstrdup (desc
))
152 label_text
get_desc (bool) const FINAL OVERRIDE
;
158 /* A concrete event subclass describing the execution of a gimple statement,
159 for use at high verbosity levels when debugging paths. */
161 class statement_event
: public checker_event
164 statement_event (const gimple
*stmt
, tree fndecl
, int depth
,
165 const program_state
&dst_state
);
167 label_text
get_desc (bool) const FINAL OVERRIDE
;
169 const gimple
* const m_stmt
;
170 const program_state m_dst_state
;
173 /* An event subclass describing the entry to a function. */
175 class function_entry_event
: public checker_event
178 function_entry_event (location_t loc
, tree fndecl
, int depth
)
179 : checker_event (EK_FUNCTION_ENTRY
, loc
, fndecl
, depth
)
183 label_text
get_desc (bool can_colorize
) const FINAL OVERRIDE
;
185 bool is_function_entry_p () const FINAL OVERRIDE
{ return true; }
188 /* Subclass of checker_event describing a state change. */
190 class state_change_event
: public checker_event
193 state_change_event (const supernode
*node
, const gimple
*stmt
,
195 const state_machine
&sm
,
197 state_machine::state_t from
,
198 state_machine::state_t to
,
199 const svalue
*origin
,
200 const program_state
&dst_state
);
202 label_text
get_desc (bool can_colorize
) const FINAL OVERRIDE
;
204 function
*get_dest_function () const
206 return m_dst_state
.get_current_function ();
209 const supernode
*m_node
;
210 const gimple
*m_stmt
;
211 const state_machine
&m_sm
;
212 const svalue
*m_sval
;
213 state_machine::state_t m_from
;
214 state_machine::state_t m_to
;
215 const svalue
*m_origin
;
216 program_state m_dst_state
;
219 /* Subclass of checker_event; parent class for subclasses that relate to
222 class superedge_event
: public checker_event
225 /* Mark this edge event as being either an interprocedural call or
226 return in which VAR is in STATE, and that this is critical to the
227 diagnostic (so that get_desc can attempt to get a better description
228 from any pending_diagnostic). */
229 void record_critical_state (tree var
, state_machine::state_t state
)
232 m_critical_state
= state
;
235 const callgraph_superedge
& get_callgraph_superedge () const;
237 bool should_filter_p (int verbosity
) const;
240 superedge_event (enum event_kind kind
, const exploded_edge
&eedge
,
241 location_t loc
, tree fndecl
, int depth
);
244 const exploded_edge
&m_eedge
;
245 const superedge
*m_sedge
;
247 state_machine::state_t m_critical_state
;
250 /* An abstract event subclass for when a CFG edge is followed; it has two
251 subclasses, representing the start of the edge and the end of the
252 edge, which come in pairs. */
254 class cfg_edge_event
: public superedge_event
257 const cfg_superedge
& get_cfg_superedge () const;
260 cfg_edge_event (enum event_kind kind
, const exploded_edge
&eedge
,
261 location_t loc
, tree fndecl
, int depth
);
264 /* A concrete event subclass for the start of a CFG edge
265 e.g. "following 'false' branch...'. */
267 class start_cfg_edge_event
: public cfg_edge_event
270 start_cfg_edge_event (const exploded_edge
&eedge
,
271 location_t loc
, tree fndecl
, int depth
)
272 : cfg_edge_event (EK_START_CFG_EDGE
, eedge
, loc
, fndecl
, depth
)
276 label_text
get_desc (bool can_colorize
) const FINAL OVERRIDE
;
279 label_text
maybe_describe_condition (bool can_colorize
) const;
281 static label_text
maybe_describe_condition (bool can_colorize
,
285 static bool should_print_expr_p (tree
);
288 /* A concrete event subclass for the end of a CFG edge
289 e.g. "...to here'. */
291 class end_cfg_edge_event
: public cfg_edge_event
294 end_cfg_edge_event (const exploded_edge
&eedge
,
295 location_t loc
, tree fndecl
, int depth
)
296 : cfg_edge_event (EK_END_CFG_EDGE
, eedge
, loc
, fndecl
, depth
)
300 label_text
get_desc (bool /*can_colorize*/) const FINAL OVERRIDE
302 return label_text::borrow ("...to here");
306 /* A concrete event subclass for an interprocedural call. */
308 class call_event
: public superedge_event
311 call_event (const exploded_edge
&eedge
,
312 location_t loc
, tree fndecl
, int depth
);
314 label_text
get_desc (bool can_colorize
) const FINAL OVERRIDE
;
316 bool is_call_p () const FINAL OVERRIDE
;
319 /* A concrete event subclass for an interprocedural return. */
321 class return_event
: public superedge_event
324 return_event (const exploded_edge
&eedge
,
325 location_t loc
, tree fndecl
, int depth
);
327 label_text
get_desc (bool can_colorize
) const FINAL OVERRIDE
;
329 bool is_return_p () const FINAL OVERRIDE
;
332 /* A concrete event subclass for a setjmp or sigsetjmp call. */
334 class setjmp_event
: public checker_event
337 setjmp_event (location_t loc
, const exploded_node
*enode
,
338 tree fndecl
, int depth
, const gcall
*setjmp_call
)
339 : checker_event (EK_SETJMP
, loc
, fndecl
, depth
),
340 m_enode (enode
), m_setjmp_call (setjmp_call
)
344 label_text
get_desc (bool can_colorize
) const FINAL OVERRIDE
;
346 void prepare_for_emission (checker_path
*path
,
347 pending_diagnostic
*pd
,
348 diagnostic_event_id_t emission_id
) FINAL OVERRIDE
;
351 const exploded_node
*m_enode
;
352 const gcall
*m_setjmp_call
;
355 /* An abstract event subclass for rewinding from a longjmp to a setjmp
356 (or siglongjmp to sigsetjmp).
358 Base class for two from/to subclasses, showing the two halves of the
361 class rewind_event
: public checker_event
364 tree
get_longjmp_caller () const;
365 tree
get_setjmp_caller () const;
366 const exploded_edge
*get_eedge () const { return m_eedge
; }
369 rewind_event (const exploded_edge
*eedge
,
370 enum event_kind kind
,
371 location_t loc
, tree fndecl
, int depth
,
372 const rewind_info_t
*rewind_info
);
373 const rewind_info_t
*m_rewind_info
;
376 const exploded_edge
*m_eedge
;
379 /* A concrete event subclass for rewinding from a longjmp to a setjmp,
380 showing the longjmp (or siglongjmp). */
382 class rewind_from_longjmp_event
: public rewind_event
385 rewind_from_longjmp_event (const exploded_edge
*eedge
,
386 location_t loc
, tree fndecl
, int depth
,
387 const rewind_info_t
*rewind_info
)
388 : rewind_event (eedge
, EK_REWIND_FROM_LONGJMP
, loc
, fndecl
, depth
,
393 label_text
get_desc (bool can_colorize
) const FINAL OVERRIDE
;
396 /* A concrete event subclass for rewinding from a longjmp to a setjmp,
397 showing the setjmp (or sigsetjmp). */
399 class rewind_to_setjmp_event
: public rewind_event
402 rewind_to_setjmp_event (const exploded_edge
*eedge
,
403 location_t loc
, tree fndecl
, int depth
,
404 const rewind_info_t
*rewind_info
)
405 : rewind_event (eedge
, EK_REWIND_TO_SETJMP
, loc
, fndecl
, depth
,
410 label_text
get_desc (bool can_colorize
) const FINAL OVERRIDE
;
412 void prepare_for_emission (checker_path
*path
,
413 pending_diagnostic
*pd
,
414 diagnostic_event_id_t emission_id
) FINAL OVERRIDE
;
417 diagnostic_event_id_t m_original_setjmp_event_id
;
420 /* Concrete subclass of checker_event for use at the end of a path:
421 a repeat of the warning message at the end of the path (perhaps with
422 references to pertinent events that occurred on the way), at the point
423 where the problem occurs. */
425 class warning_event
: public checker_event
428 warning_event (location_t loc
, tree fndecl
, int depth
,
429 const state_machine
*sm
,
430 tree var
, state_machine::state_t state
)
431 : checker_event (EK_WARNING
, loc
, fndecl
, depth
),
432 m_sm (sm
), m_var (var
), m_state (state
)
436 label_text
get_desc (bool can_colorize
) const FINAL OVERRIDE
;
439 const state_machine
*m_sm
;
441 state_machine::state_t m_state
;
444 /* Subclass of diagnostic_path for analyzer diagnostics. */
446 class checker_path
: public diagnostic_path
449 checker_path () : diagnostic_path () {}
451 /* Implementation of diagnostic_path vfuncs. */
453 unsigned num_events () const FINAL OVERRIDE
455 return m_events
.length ();
458 const diagnostic_event
& get_event (int idx
) const FINAL OVERRIDE
460 return *m_events
[idx
];
463 checker_event
*get_checker_event (int idx
)
465 return m_events
[idx
];
468 void dump (pretty_printer
*pp
) const;
471 void maybe_log (logger
*logger
, const char *desc
) const;
473 void add_event (checker_event
*event
)
475 m_events
.safe_push (event
);
478 void delete_event (int idx
)
480 checker_event
*event
= m_events
[idx
];
481 m_events
.ordered_remove (idx
);
485 void add_final_event (const state_machine
*sm
,
486 const exploded_node
*enode
, const gimple
*stmt
,
487 tree var
, state_machine::state_t state
);
489 /* After all event-pruning, a hook for notifying each event what
490 its ID will be. The events are notified in order, allowing
491 for later events to refer to the IDs of earlier events in
492 their descriptions. */
493 void prepare_for_emission (pending_diagnostic
*pd
)
497 FOR_EACH_VEC_ELT (m_events
, i
, e
)
498 e
->prepare_for_emission (this, pd
, diagnostic_event_id_t (i
));
501 void record_setjmp_event (const exploded_node
*enode
,
502 diagnostic_event_id_t setjmp_emission_id
)
504 m_setjmp_event_ids
.put (enode
, setjmp_emission_id
);
507 bool get_setjmp_event (const exploded_node
*enode
,
508 diagnostic_event_id_t
*out_emission_id
)
510 if (diagnostic_event_id_t
*emission_id
= m_setjmp_event_ids
.get (enode
))
512 *out_emission_id
= *emission_id
;
519 DISABLE_COPY_AND_ASSIGN(checker_path
);
521 /* The events that have occurred along this path. */
522 auto_delete_vec
<checker_event
> m_events
;
524 /* During prepare_for_emission (and after), the setjmp_event for each
525 exploded_node *, so that rewind events can refer to them in their
527 hash_map
<const exploded_node
*, diagnostic_event_id_t
> m_setjmp_event_ids
;
532 #endif /* GCC_ANALYZER_CHECKER_PATH_H */