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
24 #include "tree-logical-location.h"
28 /* An enum for discriminating between the concrete subclasses of
43 EK_START_CONSOLIDATED_CFG_EDGES
,
44 EK_END_CONSOLIDATED_CFG_EDGES
,
47 EK_REWIND_FROM_LONGJMP
,
52 extern const char *event_kind_to_string (enum event_kind ek
);
56 The class hierarchy looks like this (using indentation to show
57 inheritance, and with event_kinds shown for the concrete subclasses):
61 debug_event (EK_DEBUG)
62 custom_event (EK_CUSTOM)
63 precanned_custom_event
64 statement_event (EK_STMT)
65 region_creation_event (EK_REGION_CREATION)
66 function_entry_event (EK_FUNCTION_ENTRY)
67 state_change_event (EK_STATE_CHANGE)
70 start_cfg_edge_event (EK_START_CFG_EDGE)
71 end_cfg_edge_event (EK_END_CFG_EDGE)
72 call_event (EK_CALL_EDGE)
73 return_edge (EK_RETURN_EDGE)
74 start_consolidated_cfg_edges_event (EK_START_CONSOLIDATED_CFG_EDGES)
75 end_consolidated_cfg_edges_event (EK_END_CONSOLIDATED_CFG_EDGES)
76 inlined_call_event (EK_INLINED_CALL)
77 setjmp_event (EK_SETJMP)
79 rewind_from_longjmp_event (EK_REWIND_FROM_LONGJMP)
80 rewind_to_setjmp_event (EK_REWIND_TO_SETJMP)
81 warning_event (EK_WARNING). */
83 /* Abstract subclass of diagnostic_event; the base class for use in
84 checker_path (the analyzer's diagnostic_path subclass). */
86 class checker_event
: public diagnostic_event
89 /* Implementation of diagnostic_event. */
91 location_t
get_location () const final override
{ return m_loc
; }
92 tree
get_fndecl () const final override
{ return m_effective_fndecl
; }
93 int get_stack_depth () const final override
{ return m_effective_depth
; }
94 const logical_location
*get_logical_location () const final override
96 if (m_effective_fndecl
)
97 return &m_logical_loc
;
101 meaning
get_meaning () const override
;
103 /* Additional functionality. */
105 int get_original_stack_depth () const { return m_original_depth
; }
107 virtual void prepare_for_emission (checker_path
*,
108 pending_diagnostic
*pd
,
109 diagnostic_event_id_t emission_id
);
110 virtual bool is_call_p () const { return false; }
111 virtual bool is_function_entry_p () const { return false; }
112 virtual bool is_return_p () const { return false; }
114 /* For use with %@. */
115 const diagnostic_event_id_t
*get_id_ptr () const
117 return &m_emission_id
;
120 void dump (pretty_printer
*pp
) const;
122 void set_location (location_t loc
) { m_loc
= loc
; }
125 checker_event (enum event_kind kind
,
126 location_t loc
, tree fndecl
, int depth
);
129 const enum event_kind m_kind
;
132 tree m_original_fndecl
;
133 tree m_effective_fndecl
;
134 int m_original_depth
;
135 int m_effective_depth
;
136 pending_diagnostic
*m_pending_diagnostic
;
137 diagnostic_event_id_t m_emission_id
; // only set once all pruning has occurred
138 tree_logical_location m_logical_loc
;
141 /* A concrete event subclass for a purely textual event, for use in
142 debugging path creation and filtering. */
144 class debug_event
: public checker_event
147 debug_event (location_t loc
, tree fndecl
, int depth
,
149 : checker_event (EK_DEBUG
, loc
, fndecl
, depth
),
150 m_desc (xstrdup (desc
))
158 label_text
get_desc (bool) const final override
;
164 /* An abstract event subclass for custom events. These are not filtered,
165 as they are likely to be pertinent to the diagnostic. */
167 class custom_event
: public checker_event
170 custom_event (location_t loc
, tree fndecl
, int depth
)
171 : checker_event (EK_CUSTOM
, loc
, fndecl
, depth
)
176 /* A concrete custom_event subclass with a precanned message. */
178 class precanned_custom_event
: public custom_event
181 precanned_custom_event (location_t loc
, tree fndecl
, int depth
,
183 : custom_event (loc
, fndecl
, depth
),
184 m_desc (xstrdup (desc
))
187 ~precanned_custom_event ()
192 label_text
get_desc (bool) const final override
;
198 /* A concrete event subclass describing the execution of a gimple statement,
199 for use at high verbosity levels when debugging paths. */
201 class statement_event
: public checker_event
204 statement_event (const gimple
*stmt
, tree fndecl
, int depth
,
205 const program_state
&dst_state
);
207 label_text
get_desc (bool) const final override
;
209 const gimple
* const m_stmt
;
210 const program_state m_dst_state
;
213 /* A concrete event subclass describing the creation of a region that
214 is significant for a diagnostic e.g. "region created on stack here". */
216 class region_creation_event
: public checker_event
219 region_creation_event (const region
*reg
,
220 location_t loc
, tree fndecl
, int depth
);
222 label_text
get_desc (bool) const final override
;
228 /* An event subclass describing the entry to a function. */
230 class function_entry_event
: public checker_event
233 function_entry_event (location_t loc
, tree fndecl
, int depth
)
234 : checker_event (EK_FUNCTION_ENTRY
, loc
, fndecl
, depth
)
238 label_text
get_desc (bool can_colorize
) const final override
;
239 meaning
get_meaning () const override
;
241 bool is_function_entry_p () const final override
{ return true; }
244 /* Subclass of checker_event describing a state change. */
246 class state_change_event
: public checker_event
249 state_change_event (const supernode
*node
, const gimple
*stmt
,
251 const state_machine
&sm
,
253 state_machine::state_t from
,
254 state_machine::state_t to
,
255 const svalue
*origin
,
256 const program_state
&dst_state
);
258 label_text
get_desc (bool can_colorize
) const final override
;
259 meaning
get_meaning () const override
;
261 function
*get_dest_function () const
263 return m_dst_state
.get_current_function ();
266 const supernode
*m_node
;
267 const gimple
*m_stmt
;
268 const state_machine
&m_sm
;
269 const svalue
*m_sval
;
270 state_machine::state_t m_from
;
271 state_machine::state_t m_to
;
272 const svalue
*m_origin
;
273 program_state m_dst_state
;
276 /* Subclass of checker_event; parent class for subclasses that relate to
279 class superedge_event
: public checker_event
282 /* Mark this edge event as being either an interprocedural call or
283 return in which VAR is in STATE, and that this is critical to the
284 diagnostic (so that get_desc can attempt to get a better description
285 from any pending_diagnostic). */
286 void record_critical_state (tree var
, state_machine::state_t state
)
289 m_critical_state
= state
;
292 const callgraph_superedge
& get_callgraph_superedge () const;
294 bool should_filter_p (int verbosity
) const;
297 superedge_event (enum event_kind kind
, const exploded_edge
&eedge
,
298 location_t loc
, tree fndecl
, int depth
);
301 const exploded_edge
&m_eedge
;
302 const superedge
*m_sedge
;
304 state_machine::state_t m_critical_state
;
307 /* An abstract event subclass for when a CFG edge is followed; it has two
308 subclasses, representing the start of the edge and the end of the
309 edge, which come in pairs. */
311 class cfg_edge_event
: public superedge_event
314 meaning
get_meaning () const override
;
316 const cfg_superedge
& get_cfg_superedge () const;
319 cfg_edge_event (enum event_kind kind
, const exploded_edge
&eedge
,
320 location_t loc
, tree fndecl
, int depth
);
323 /* A concrete event subclass for the start of a CFG edge
324 e.g. "following 'false' branch...'. */
326 class start_cfg_edge_event
: public cfg_edge_event
329 start_cfg_edge_event (const exploded_edge
&eedge
,
330 location_t loc
, tree fndecl
, int depth
)
331 : cfg_edge_event (EK_START_CFG_EDGE
, eedge
, loc
, fndecl
, depth
)
335 label_text
get_desc (bool can_colorize
) const final override
;
338 label_text
maybe_describe_condition (bool can_colorize
) const;
340 static label_text
maybe_describe_condition (bool can_colorize
,
344 static bool should_print_expr_p (tree
);
347 /* A concrete event subclass for the end of a CFG edge
348 e.g. "...to here'. */
350 class end_cfg_edge_event
: public cfg_edge_event
353 end_cfg_edge_event (const exploded_edge
&eedge
,
354 location_t loc
, tree fndecl
, int depth
)
355 : cfg_edge_event (EK_END_CFG_EDGE
, eedge
, loc
, fndecl
, depth
)
359 label_text
get_desc (bool /*can_colorize*/) const final override
361 return label_text::borrow ("...to here");
365 /* A concrete event subclass for an interprocedural call. */
367 class call_event
: public superedge_event
370 call_event (const exploded_edge
&eedge
,
371 location_t loc
, tree fndecl
, int depth
);
373 label_text
get_desc (bool can_colorize
) const override
;
374 meaning
get_meaning () const override
;
376 bool is_call_p () const final override
;
379 tree
get_caller_fndecl () const;
380 tree
get_callee_fndecl () const;
382 const supernode
*m_src_snode
;
383 const supernode
*m_dest_snode
;
386 /* A concrete event subclass for an interprocedural return. */
388 class return_event
: public superedge_event
391 return_event (const exploded_edge
&eedge
,
392 location_t loc
, tree fndecl
, int depth
);
394 label_text
get_desc (bool can_colorize
) const final override
;
395 meaning
get_meaning () const override
;
397 bool is_return_p () const final override
;
399 const supernode
*m_src_snode
;
400 const supernode
*m_dest_snode
;
403 /* A concrete event subclass for the start of a consolidated run of CFG
404 edges all either TRUE or FALSE e.g. "following 'false' branch...'. */
406 class start_consolidated_cfg_edges_event
: public checker_event
409 start_consolidated_cfg_edges_event (location_t loc
, tree fndecl
, int depth
,
411 : checker_event (EK_START_CONSOLIDATED_CFG_EDGES
, loc
, fndecl
, depth
),
412 m_edge_sense (edge_sense
)
416 label_text
get_desc (bool can_colorize
) const final override
;
417 meaning
get_meaning () const override
;
423 /* A concrete event subclass for the end of a consolidated run of
424 CFG edges e.g. "...to here'. */
426 class end_consolidated_cfg_edges_event
: public checker_event
429 end_consolidated_cfg_edges_event (location_t loc
, tree fndecl
, int depth
)
430 : checker_event (EK_END_CONSOLIDATED_CFG_EDGES
, loc
, fndecl
, depth
)
434 label_text
get_desc (bool /*can_colorize*/) const final override
436 return label_text::borrow ("...to here");
440 /* A concrete event subclass for describing an inlined call event
441 e.g. "inlined call to 'callee' from 'caller'". */
443 class inlined_call_event
: public checker_event
446 inlined_call_event (location_t loc
,
447 tree apparent_callee_fndecl
,
448 tree apparent_caller_fndecl
,
450 int stack_depth_adjustment
)
451 : checker_event (EK_INLINED_CALL
, loc
,
452 apparent_caller_fndecl
,
453 actual_depth
+ stack_depth_adjustment
),
454 m_apparent_callee_fndecl (apparent_callee_fndecl
),
455 m_apparent_caller_fndecl (apparent_caller_fndecl
)
457 gcc_assert (LOCATION_BLOCK (loc
) == NULL
);
460 label_text
get_desc (bool /*can_colorize*/) const final override
;
461 meaning
get_meaning () const override
;
464 tree m_apparent_callee_fndecl
;
465 tree m_apparent_caller_fndecl
;
468 /* A concrete event subclass for a setjmp or sigsetjmp call. */
470 class setjmp_event
: public checker_event
473 setjmp_event (location_t loc
, const exploded_node
*enode
,
474 tree fndecl
, int depth
, const gcall
*setjmp_call
)
475 : checker_event (EK_SETJMP
, loc
, fndecl
, depth
),
476 m_enode (enode
), m_setjmp_call (setjmp_call
)
480 label_text
get_desc (bool can_colorize
) const final override
;
482 void prepare_for_emission (checker_path
*path
,
483 pending_diagnostic
*pd
,
484 diagnostic_event_id_t emission_id
) final override
;
487 const exploded_node
*m_enode
;
488 const gcall
*m_setjmp_call
;
491 /* An abstract event subclass for rewinding from a longjmp to a setjmp
492 (or siglongjmp to sigsetjmp).
494 Base class for two from/to subclasses, showing the two halves of the
497 class rewind_event
: public checker_event
500 tree
get_longjmp_caller () const;
501 tree
get_setjmp_caller () const;
502 const exploded_edge
*get_eedge () const { return m_eedge
; }
505 rewind_event (const exploded_edge
*eedge
,
506 enum event_kind kind
,
507 location_t loc
, tree fndecl
, int depth
,
508 const rewind_info_t
*rewind_info
);
509 const rewind_info_t
*m_rewind_info
;
512 const exploded_edge
*m_eedge
;
515 /* A concrete event subclass for rewinding from a longjmp to a setjmp,
516 showing the longjmp (or siglongjmp). */
518 class rewind_from_longjmp_event
: public rewind_event
521 rewind_from_longjmp_event (const exploded_edge
*eedge
,
522 location_t loc
, tree fndecl
, int depth
,
523 const rewind_info_t
*rewind_info
)
524 : rewind_event (eedge
, EK_REWIND_FROM_LONGJMP
, loc
, fndecl
, depth
,
529 label_text
get_desc (bool can_colorize
) const final override
;
532 /* A concrete event subclass for rewinding from a longjmp to a setjmp,
533 showing the setjmp (or sigsetjmp). */
535 class rewind_to_setjmp_event
: public rewind_event
538 rewind_to_setjmp_event (const exploded_edge
*eedge
,
539 location_t loc
, tree fndecl
, int depth
,
540 const rewind_info_t
*rewind_info
)
541 : rewind_event (eedge
, EK_REWIND_TO_SETJMP
, loc
, fndecl
, depth
,
546 label_text
get_desc (bool can_colorize
) const final override
;
548 void prepare_for_emission (checker_path
*path
,
549 pending_diagnostic
*pd
,
550 diagnostic_event_id_t emission_id
) final override
;
553 diagnostic_event_id_t m_original_setjmp_event_id
;
556 /* Concrete subclass of checker_event for use at the end of a path:
557 a repeat of the warning message at the end of the path (perhaps with
558 references to pertinent events that occurred on the way), at the point
559 where the problem occurs. */
561 class warning_event
: public checker_event
564 warning_event (location_t loc
, tree fndecl
, int depth
,
565 const state_machine
*sm
,
566 tree var
, state_machine::state_t state
)
567 : checker_event (EK_WARNING
, loc
, fndecl
, depth
),
568 m_sm (sm
), m_var (var
), m_state (state
)
572 label_text
get_desc (bool can_colorize
) const final override
;
573 meaning
get_meaning () const override
;
576 const state_machine
*m_sm
;
578 state_machine::state_t m_state
;
581 /* Subclass of diagnostic_path for analyzer diagnostics. */
583 class checker_path
: public diagnostic_path
586 checker_path () : diagnostic_path () {}
588 /* Implementation of diagnostic_path vfuncs. */
590 unsigned num_events () const final override
592 return m_events
.length ();
595 const diagnostic_event
& get_event (int idx
) const final override
597 return *m_events
[idx
];
600 checker_event
*get_checker_event (int idx
)
602 return m_events
[idx
];
605 void dump (pretty_printer
*pp
) const;
608 void maybe_log (logger
*logger
, const char *desc
) const;
610 void add_event (checker_event
*event
)
612 m_events
.safe_push (event
);
615 void delete_event (int idx
)
617 checker_event
*event
= m_events
[idx
];
618 m_events
.ordered_remove (idx
);
622 void delete_events (unsigned start_idx
, unsigned len
)
624 for (unsigned i
= start_idx
; i
< start_idx
+ len
; i
++)
626 m_events
.block_remove (start_idx
, len
);
629 void replace_event (unsigned idx
, checker_event
*new_event
)
631 delete m_events
[idx
];
632 m_events
[idx
] = new_event
;
635 void add_region_creation_event (const region
*reg
,
637 tree fndecl
, int depth
);
639 void add_final_event (const state_machine
*sm
,
640 const exploded_node
*enode
, const gimple
*stmt
,
641 tree var
, state_machine::state_t state
);
643 /* After all event-pruning, a hook for notifying each event what
644 its ID will be. The events are notified in order, allowing
645 for later events to refer to the IDs of earlier events in
646 their descriptions. */
647 void prepare_for_emission (pending_diagnostic
*pd
)
651 FOR_EACH_VEC_ELT (m_events
, i
, e
)
652 e
->prepare_for_emission (this, pd
, diagnostic_event_id_t (i
));
655 void fixup_locations (pending_diagnostic
*pd
);
657 void record_setjmp_event (const exploded_node
*enode
,
658 diagnostic_event_id_t setjmp_emission_id
)
660 m_setjmp_event_ids
.put (enode
, setjmp_emission_id
);
663 bool get_setjmp_event (const exploded_node
*enode
,
664 diagnostic_event_id_t
*out_emission_id
)
666 if (diagnostic_event_id_t
*emission_id
= m_setjmp_event_ids
.get (enode
))
668 *out_emission_id
= *emission_id
;
674 bool cfg_edge_pair_at_p (unsigned idx
) const;
676 void inject_any_inlined_call_events (logger
*logger
);
679 DISABLE_COPY_AND_ASSIGN(checker_path
);
681 /* The events that have occurred along this path. */
682 auto_delete_vec
<checker_event
> m_events
;
684 /* During prepare_for_emission (and after), the setjmp_event for each
685 exploded_node *, so that rewind events can refer to them in their
687 hash_map
<const exploded_node
*, diagnostic_event_id_t
> m_setjmp_event_ids
;
692 #endif /* GCC_ANALYZER_CHECKER_PATH_H */