1 /* Subclasses of diagnostic_path and diagnostic_event for analyzer diagnostics.
2 Copyright (C) 2019-2022 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_START_CONSOLIDATED_CFG_EDGES
,
42 EK_END_CONSOLIDATED_CFG_EDGES
,
44 EK_REWIND_FROM_LONGJMP
,
49 extern const char *event_kind_to_string (enum event_kind ek
);
53 The class hierarchy looks like this (using indentation to show
54 inheritance, and with event_kinds shown for the concrete subclasses):
58 debug_event (EK_DEBUG)
59 custom_event (EK_CUSTOM)
60 precanned_custom_event
61 statement_event (EK_STMT)
62 region_creation_event (EK_REGION_CREATION)
63 function_entry_event (EK_FUNCTION_ENTRY)
64 state_change_event (EK_STATE_CHANGE)
67 start_cfg_edge_event (EK_START_CFG_EDGE)
68 end_cfg_edge_event (EK_END_CFG_EDGE)
69 call_event (EK_CALL_EDGE)
70 return_edge (EK_RETURN_EDGE)
71 start_consolidated_cfg_edges_event (EK_START_CONSOLIDATED_CFG_EDGES)
72 end_consolidated_cfg_edges_event (EK_END_CONSOLIDATED_CFG_EDGES)
73 setjmp_event (EK_SETJMP)
75 rewind_from_longjmp_event (EK_REWIND_FROM_LONGJMP)
76 rewind_to_setjmp_event (EK_REWIND_TO_SETJMP)
77 warning_event (EK_WARNING). */
79 /* Abstract subclass of diagnostic_event; the base class for use in
80 checker_path (the analyzer's diagnostic_path subclass). */
82 class checker_event
: public diagnostic_event
85 checker_event (enum event_kind kind
,
86 location_t loc
, tree fndecl
, int depth
)
87 : m_kind (kind
), m_loc (loc
), m_fndecl (fndecl
), m_depth (depth
),
88 m_pending_diagnostic (NULL
), m_emission_id ()
92 /* Implementation of diagnostic_event. */
94 location_t
get_location () const final override
{ return m_loc
; }
95 tree
get_fndecl () const final override
{ return m_fndecl
; }
96 int get_stack_depth () const final override
{ return m_depth
; }
98 /* Additional functionality. */
100 virtual void prepare_for_emission (checker_path
*,
101 pending_diagnostic
*pd
,
102 diagnostic_event_id_t emission_id
);
103 virtual bool is_call_p () const { return false; }
104 virtual bool is_function_entry_p () const { return false; }
105 virtual bool is_return_p () const { return false; }
107 /* For use with %@. */
108 const diagnostic_event_id_t
*get_id_ptr () const
110 return &m_emission_id
;
113 void dump (pretty_printer
*pp
) const;
115 void set_location (location_t loc
) { m_loc
= loc
; }
118 const enum event_kind m_kind
;
123 pending_diagnostic
*m_pending_diagnostic
;
124 diagnostic_event_id_t m_emission_id
; // only set once all pruning has occurred
127 /* A concrete event subclass for a purely textual event, for use in
128 debugging path creation and filtering. */
130 class debug_event
: public checker_event
133 debug_event (location_t loc
, tree fndecl
, int depth
,
135 : checker_event (EK_DEBUG
, loc
, fndecl
, depth
),
136 m_desc (xstrdup (desc
))
144 label_text
get_desc (bool) const final override
;
150 /* An abstract event subclass for custom events. These are not filtered,
151 as they are likely to be pertinent to the diagnostic. */
153 class custom_event
: public checker_event
156 custom_event (location_t loc
, tree fndecl
, int depth
)
157 : checker_event (EK_CUSTOM
, loc
, fndecl
, depth
)
162 /* A concrete custom_event subclass with a precanned message. */
164 class precanned_custom_event
: public custom_event
167 precanned_custom_event (location_t loc
, tree fndecl
, int depth
,
169 : custom_event (loc
, fndecl
, depth
),
170 m_desc (xstrdup (desc
))
173 ~precanned_custom_event ()
178 label_text
get_desc (bool) const final override
;
184 /* A concrete event subclass describing the execution of a gimple statement,
185 for use at high verbosity levels when debugging paths. */
187 class statement_event
: public checker_event
190 statement_event (const gimple
*stmt
, tree fndecl
, int depth
,
191 const program_state
&dst_state
);
193 label_text
get_desc (bool) const final override
;
195 const gimple
* const m_stmt
;
196 const program_state m_dst_state
;
199 /* A concrete event subclass describing the creation of a region that
200 is significant for a diagnostic e.g. "region created on stack here". */
202 class region_creation_event
: public checker_event
205 region_creation_event (const region
*reg
,
206 location_t loc
, tree fndecl
, int depth
);
208 label_text
get_desc (bool) const final override
;
214 /* An event subclass describing the entry to a function. */
216 class function_entry_event
: public checker_event
219 function_entry_event (location_t loc
, tree fndecl
, int depth
)
220 : checker_event (EK_FUNCTION_ENTRY
, loc
, fndecl
, depth
)
224 label_text
get_desc (bool can_colorize
) const final override
;
226 bool is_function_entry_p () const final override
{ return true; }
229 /* Subclass of checker_event describing a state change. */
231 class state_change_event
: public checker_event
234 state_change_event (const supernode
*node
, const gimple
*stmt
,
236 const state_machine
&sm
,
238 state_machine::state_t from
,
239 state_machine::state_t to
,
240 const svalue
*origin
,
241 const program_state
&dst_state
);
243 label_text
get_desc (bool can_colorize
) const final override
;
245 function
*get_dest_function () const
247 return m_dst_state
.get_current_function ();
250 const supernode
*m_node
;
251 const gimple
*m_stmt
;
252 const state_machine
&m_sm
;
253 const svalue
*m_sval
;
254 state_machine::state_t m_from
;
255 state_machine::state_t m_to
;
256 const svalue
*m_origin
;
257 program_state m_dst_state
;
260 /* Subclass of checker_event; parent class for subclasses that relate to
263 class superedge_event
: public checker_event
266 /* Mark this edge event as being either an interprocedural call or
267 return in which VAR is in STATE, and that this is critical to the
268 diagnostic (so that get_desc can attempt to get a better description
269 from any pending_diagnostic). */
270 void record_critical_state (tree var
, state_machine::state_t state
)
273 m_critical_state
= state
;
276 const callgraph_superedge
& get_callgraph_superedge () const;
278 bool should_filter_p (int verbosity
) const;
281 superedge_event (enum event_kind kind
, const exploded_edge
&eedge
,
282 location_t loc
, tree fndecl
, int depth
);
285 const exploded_edge
&m_eedge
;
286 const superedge
*m_sedge
;
288 state_machine::state_t m_critical_state
;
291 /* An abstract event subclass for when a CFG edge is followed; it has two
292 subclasses, representing the start of the edge and the end of the
293 edge, which come in pairs. */
295 class cfg_edge_event
: public superedge_event
298 const cfg_superedge
& get_cfg_superedge () const;
301 cfg_edge_event (enum event_kind kind
, const exploded_edge
&eedge
,
302 location_t loc
, tree fndecl
, int depth
);
305 /* A concrete event subclass for the start of a CFG edge
306 e.g. "following 'false' branch...'. */
308 class start_cfg_edge_event
: public cfg_edge_event
311 start_cfg_edge_event (const exploded_edge
&eedge
,
312 location_t loc
, tree fndecl
, int depth
)
313 : cfg_edge_event (EK_START_CFG_EDGE
, eedge
, loc
, fndecl
, depth
)
317 label_text
get_desc (bool can_colorize
) const final override
;
320 label_text
maybe_describe_condition (bool can_colorize
) const;
322 static label_text
maybe_describe_condition (bool can_colorize
,
326 static bool should_print_expr_p (tree
);
329 /* A concrete event subclass for the end of a CFG edge
330 e.g. "...to here'. */
332 class end_cfg_edge_event
: public cfg_edge_event
335 end_cfg_edge_event (const exploded_edge
&eedge
,
336 location_t loc
, tree fndecl
, int depth
)
337 : cfg_edge_event (EK_END_CFG_EDGE
, eedge
, loc
, fndecl
, depth
)
341 label_text
get_desc (bool /*can_colorize*/) const final override
343 return label_text::borrow ("...to here");
347 /* A concrete event subclass for an interprocedural call. */
349 class call_event
: public superedge_event
352 call_event (const exploded_edge
&eedge
,
353 location_t loc
, tree fndecl
, int depth
);
355 label_text
get_desc (bool can_colorize
) const override
;
357 bool is_call_p () const final override
;
360 tree
get_caller_fndecl () const;
361 tree
get_callee_fndecl () const;
363 const supernode
*m_src_snode
;
364 const supernode
*m_dest_snode
;
367 /* A concrete event subclass for an interprocedural return. */
369 class return_event
: public superedge_event
372 return_event (const exploded_edge
&eedge
,
373 location_t loc
, tree fndecl
, int depth
);
375 label_text
get_desc (bool can_colorize
) const final override
;
377 bool is_return_p () const final override
;
379 const supernode
*m_src_snode
;
380 const supernode
*m_dest_snode
;
383 /* A concrete event subclass for the start of a consolidated run of CFG
384 edges all either TRUE or FALSE e.g. "following 'false' branch...'. */
386 class start_consolidated_cfg_edges_event
: public checker_event
389 start_consolidated_cfg_edges_event (location_t loc
, tree fndecl
, int depth
,
391 : checker_event (EK_START_CONSOLIDATED_CFG_EDGES
, loc
, fndecl
, depth
),
392 m_edge_sense (edge_sense
)
396 label_text
get_desc (bool can_colorize
) const final override
;
402 /* A concrete event subclass for the end of a consolidated run of
403 CFG edges e.g. "...to here'. */
405 class end_consolidated_cfg_edges_event
: public checker_event
408 end_consolidated_cfg_edges_event (location_t loc
, tree fndecl
, int depth
)
409 : checker_event (EK_END_CONSOLIDATED_CFG_EDGES
, loc
, fndecl
, depth
)
413 label_text
get_desc (bool /*can_colorize*/) const final override
415 return label_text::borrow ("...to here");
419 /* A concrete event subclass for a setjmp or sigsetjmp call. */
421 class setjmp_event
: public checker_event
424 setjmp_event (location_t loc
, const exploded_node
*enode
,
425 tree fndecl
, int depth
, const gcall
*setjmp_call
)
426 : checker_event (EK_SETJMP
, loc
, fndecl
, depth
),
427 m_enode (enode
), m_setjmp_call (setjmp_call
)
431 label_text
get_desc (bool can_colorize
) const final override
;
433 void prepare_for_emission (checker_path
*path
,
434 pending_diagnostic
*pd
,
435 diagnostic_event_id_t emission_id
) final override
;
438 const exploded_node
*m_enode
;
439 const gcall
*m_setjmp_call
;
442 /* An abstract event subclass for rewinding from a longjmp to a setjmp
443 (or siglongjmp to sigsetjmp).
445 Base class for two from/to subclasses, showing the two halves of the
448 class rewind_event
: public checker_event
451 tree
get_longjmp_caller () const;
452 tree
get_setjmp_caller () const;
453 const exploded_edge
*get_eedge () const { return m_eedge
; }
456 rewind_event (const exploded_edge
*eedge
,
457 enum event_kind kind
,
458 location_t loc
, tree fndecl
, int depth
,
459 const rewind_info_t
*rewind_info
);
460 const rewind_info_t
*m_rewind_info
;
463 const exploded_edge
*m_eedge
;
466 /* A concrete event subclass for rewinding from a longjmp to a setjmp,
467 showing the longjmp (or siglongjmp). */
469 class rewind_from_longjmp_event
: public rewind_event
472 rewind_from_longjmp_event (const exploded_edge
*eedge
,
473 location_t loc
, tree fndecl
, int depth
,
474 const rewind_info_t
*rewind_info
)
475 : rewind_event (eedge
, EK_REWIND_FROM_LONGJMP
, loc
, fndecl
, depth
,
480 label_text
get_desc (bool can_colorize
) const final override
;
483 /* A concrete event subclass for rewinding from a longjmp to a setjmp,
484 showing the setjmp (or sigsetjmp). */
486 class rewind_to_setjmp_event
: public rewind_event
489 rewind_to_setjmp_event (const exploded_edge
*eedge
,
490 location_t loc
, tree fndecl
, int depth
,
491 const rewind_info_t
*rewind_info
)
492 : rewind_event (eedge
, EK_REWIND_TO_SETJMP
, loc
, fndecl
, depth
,
497 label_text
get_desc (bool can_colorize
) const final override
;
499 void prepare_for_emission (checker_path
*path
,
500 pending_diagnostic
*pd
,
501 diagnostic_event_id_t emission_id
) final override
;
504 diagnostic_event_id_t m_original_setjmp_event_id
;
507 /* Concrete subclass of checker_event for use at the end of a path:
508 a repeat of the warning message at the end of the path (perhaps with
509 references to pertinent events that occurred on the way), at the point
510 where the problem occurs. */
512 class warning_event
: public checker_event
515 warning_event (location_t loc
, tree fndecl
, int depth
,
516 const state_machine
*sm
,
517 tree var
, state_machine::state_t state
)
518 : checker_event (EK_WARNING
, loc
, fndecl
, depth
),
519 m_sm (sm
), m_var (var
), m_state (state
)
523 label_text
get_desc (bool can_colorize
) const final override
;
526 const state_machine
*m_sm
;
528 state_machine::state_t m_state
;
531 /* Subclass of diagnostic_path for analyzer diagnostics. */
533 class checker_path
: public diagnostic_path
536 checker_path () : diagnostic_path () {}
538 /* Implementation of diagnostic_path vfuncs. */
540 unsigned num_events () const final override
542 return m_events
.length ();
545 const diagnostic_event
& get_event (int idx
) const final override
547 return *m_events
[idx
];
550 checker_event
*get_checker_event (int idx
)
552 return m_events
[idx
];
555 void dump (pretty_printer
*pp
) const;
558 void maybe_log (logger
*logger
, const char *desc
) const;
560 void add_event (checker_event
*event
)
562 m_events
.safe_push (event
);
565 void delete_event (int idx
)
567 checker_event
*event
= m_events
[idx
];
568 m_events
.ordered_remove (idx
);
572 void delete_events (unsigned start_idx
, unsigned len
)
574 for (unsigned i
= start_idx
; i
< start_idx
+ len
; i
++)
576 m_events
.block_remove (start_idx
, len
);
579 void replace_event (unsigned idx
, checker_event
*new_event
)
581 delete m_events
[idx
];
582 m_events
[idx
] = new_event
;
585 void add_region_creation_event (const region
*reg
,
587 tree fndecl
, int depth
);
589 void add_final_event (const state_machine
*sm
,
590 const exploded_node
*enode
, const gimple
*stmt
,
591 tree var
, state_machine::state_t state
);
593 /* After all event-pruning, a hook for notifying each event what
594 its ID will be. The events are notified in order, allowing
595 for later events to refer to the IDs of earlier events in
596 their descriptions. */
597 void prepare_for_emission (pending_diagnostic
*pd
)
601 FOR_EACH_VEC_ELT (m_events
, i
, e
)
602 e
->prepare_for_emission (this, pd
, diagnostic_event_id_t (i
));
605 void fixup_locations (pending_diagnostic
*pd
);
607 void record_setjmp_event (const exploded_node
*enode
,
608 diagnostic_event_id_t setjmp_emission_id
)
610 m_setjmp_event_ids
.put (enode
, setjmp_emission_id
);
613 bool get_setjmp_event (const exploded_node
*enode
,
614 diagnostic_event_id_t
*out_emission_id
)
616 if (diagnostic_event_id_t
*emission_id
= m_setjmp_event_ids
.get (enode
))
618 *out_emission_id
= *emission_id
;
624 bool cfg_edge_pair_at_p (unsigned idx
) const;
627 DISABLE_COPY_AND_ASSIGN(checker_path
);
629 /* The events that have occurred along this path. */
630 auto_delete_vec
<checker_event
> m_events
;
632 /* During prepare_for_emission (and after), the setjmp_event for each
633 exploded_node *, so that rewind events can refer to them in their
635 hash_map
<const exploded_node
*, diagnostic_event_id_t
> m_setjmp_event_ids
;
640 #endif /* GCC_ANALYZER_CHECKER_PATH_H */