Debian Codegraph @

gcc/analyzer/checker-event.cc

raw
1 /* Subclasses of diagnostic_event for analyzer diagnostics.
2 Copyright (C) 2019-2025 Free Software Foundation, Inc.
3 Contributed by David Malcolm <[email protected]>.
4
5 This file is part of GCC.
6
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)
10 any later version.
11
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.
16
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/>. */
20
21 #include "config.h"
22 #define INCLUDE_VECTOR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tree.h"
26 #include "function.h"
27 #include "basic-block.h"
28 #include "gimple.h"
29 #include "diagnostic-core.h"
30 #include "gimple-pretty-print.h"
31 #include "fold-const.h"
32 #include "diagnostic-path.h"
33 #include "options.h"
34 #include "cgraph.h"
35 #include "cfg.h"
36 #include "digraph.h"
37 #include "diagnostic-event-id.h"
38 #include "analyzer/analyzer.h"
39 #include "analyzer/analyzer-logging.h"
40 #include "analyzer/sm.h"
41 #include "sbitmap.h"
42 #include "bitmap.h"
43 #include "ordered-hash-map.h"
44 #include "analyzer/call-string.h"
45 #include "analyzer/program-point.h"
46 #include "analyzer/store.h"
47 #include "analyzer/region-model.h"
48 #include "analyzer/program-state.h"
49 #include "analyzer/checker-path.h"
50 #include "gimple-iterator.h"
51 #include "inlining-iterator.h"
52 #include "analyzer/supergraph.h"
53 #include "analyzer/pending-diagnostic.h"
54 #include "analyzer/diagnostic-manager.h"
55 #include "analyzer/constraint-manager.h"
56 #include "analyzer/checker-event.h"
57 #include "analyzer/exploded-graph.h"
58 #include "diagnostic-format-sarif.h"
59 #include "tree-logical-location.h"
60
61 #if ENABLE_ANALYZER
62
63 namespace ana {
64
65 /* Get a string for EK. */
66
67 const char *
68 event_kind_to_string (enum event_kind ek)
69 {
70 switch (ek)
71 {
72 default:
73 gcc_unreachable ();
74 case EK_DEBUG:
75 return "EK_DEBUG";
76 case EK_CUSTOM:
77 return "EK_CUSTOM";
78 case EK_STMT:
79 return "EK_STMT";
80 case EK_REGION_CREATION:
81 return "EK_REGION_CREATION";
82 case EK_FUNCTION_ENTRY:
83 return "EK_FUNCTION_ENTRY";
84 case EK_STATE_CHANGE:
85 return "EK_STATE_CHANGE";
86 case EK_START_CFG_EDGE:
87 return "EK_START_CFG_EDGE";
88 case EK_END_CFG_EDGE:
89 return "EK_END_CFG_EDGE";
90 case EK_CALL_EDGE:
91 return "EK_CALL_EDGE";
92 case EK_RETURN_EDGE:
93 return "EK_RETURN_EDGE";
94 case EK_START_CONSOLIDATED_CFG_EDGES:
95 return "EK_START_CONSOLIDATED_CFG_EDGES";
96 case EK_END_CONSOLIDATED_CFG_EDGES:
97 return "EK_END_CONSOLIDATED_CFG_EDGES";
98 case EK_INLINED_CALL:
99 return "EK_INLINED_CALL";
100 case EK_SETJMP:
101 return "EK_SETJMP";
102 case EK_REWIND_FROM_LONGJMP:
103 return "EK_REWIND_FROM_LONGJMP";
104 case EK_REWIND_TO_SETJMP:
105 return "EK_REWIND_TO_SETJMP";
106 case EK_WARNING:
107 return "EK_WARNING";
108 }
109 }
110
111 /* class checker_event : public diagnostic_event. */
112
113 /* checker_event's ctor. */
114
115 checker_event::checker_event (enum event_kind kind,
116 const event_loc_info &loc_info)
117 : m_kind (kind), m_loc (loc_info.m_loc),
118 m_original_fndecl (loc_info.m_fndecl),
119 m_effective_fndecl (loc_info.m_fndecl),
120 m_original_depth (loc_info.m_depth),
121 m_effective_depth (loc_info.m_depth),
122 m_pending_diagnostic (NULL), m_emission_id (),
123 m_logical_loc (loc_info.m_fndecl)
124 {
125 /* Update effective fndecl and depth if inlining has been recorded. */
126 if (flag_analyzer_undo_inlining)
127 {
128 inlining_info info (m_loc);
129 if (info.get_inner_fndecl ())
130 {
131 m_effective_fndecl = info.get_inner_fndecl ();
132 m_effective_depth += info.get_extra_frames ();
133 m_logical_loc = tree_logical_location (m_effective_fndecl);
134 }
135 }
136 }
137
138 /* No-op implementation of diagnostic_event::get_meaning vfunc for
139 checker_event: checker events have no meaning by default. */
140
141 diagnostic_event::meaning
142 checker_event::get_meaning () const
143 {
144 return meaning ();
145 }
146
147 /* Implementation of diagnostic_event::maybe_add_sarif_properties
148 for checker_event. */
149
150 void
151 checker_event::
152 maybe_add_sarif_properties (sarif_object &thread_flow_loc_obj) const
153 {
154 sarif_property_bag &props = thread_flow_loc_obj.get_or_create_properties ();
155 #define PROPERTY_PREFIX "gcc/analyzer/checker_event/"
156 props.set (PROPERTY_PREFIX "emission_id",
157 diagnostic_event_id_to_json (m_emission_id));
158 props.set_string (PROPERTY_PREFIX "kind", event_kind_to_string (m_kind));
159
160 if (m_original_fndecl != m_effective_fndecl)
161 {
162 tree_logical_location logical_loc (m_original_fndecl);
163 props.set<sarif_logical_location>
164 (PROPERTY_PREFIX "original_fndecl",
165 make_sarif_logical_location_object (logical_loc));
166 }
167 if (m_original_depth != m_effective_depth)
168 props.set_integer (PROPERTY_PREFIX "original_depth", m_original_depth);
169 #undef PROPERTY_PREFIX
170 }
171
172 /* Dump this event to PP (for debugging/logging purposes). */
173
174 void
175 checker_event::dump (pretty_printer *pp) const
176 {
177 pp_character (pp, '"');
178 print_desc (*pp);
179 pp_printf (pp, "\" (depth %i", m_effective_depth);
180
181 if (m_effective_depth != m_original_depth)
182 pp_printf (pp, " corrected from %i",
183 m_original_depth);
184 if (m_effective_fndecl)
185 {
186 pp_printf (pp, ", fndecl %qE", m_effective_fndecl);
187 if (m_effective_fndecl != m_original_fndecl)
188 pp_printf (pp, " corrected from %qE", m_original_fndecl);
189 }
190 pp_printf (pp, ", m_loc=%llx)",
191 (unsigned long long) get_location ());
192 }
193
194 /* Dump this event to stderr (for debugging/logging purposes). */
195
196 DEBUG_FUNCTION void
197 checker_event::debug () const
198 {
199 tree_dump_pretty_printer pp (stderr);
200 dump (&pp);
201 pp_newline (&pp);
202 }
203
204 /* Hook for being notified when this event has its final id EMISSION_ID
205 and is about to emitted for PD.
206
207 Base implementation of checker_event::prepare_for_emission vfunc;
208 subclasses that override this should chain up to it.
209
210 Record PD and EMISSION_ID, and call the print_desc vfunc, so that any
211 side-effects of the call to print_desc take place before
212 pending_diagnostic::emit is called.
213
214 For example, state_change_event::print_desc can call
215 pending_diagnostic::describe_state_change; free_of_non_heap can use this
216 to tweak the message (TODO: would be neater to simply capture the
217 pertinent data within the sm-state). */
218
219 void
220 checker_event::prepare_for_emission (checker_path *,
221 pending_diagnostic *pd,
222 diagnostic_event_id_t emission_id)
223 {
224 m_pending_diagnostic = pd;
225 m_emission_id = emission_id;
226
227 auto pp = global_dc->clone_printer ();
228 print_desc (*pp.get ());
229 }
230
231 /* class debug_event : public checker_event. */
232
233 /* Implementation of diagnostic_event::print_desc vfunc for
234 debug_event.
235 Use the saved string as the event's description. */
236
237 void
238 debug_event::print_desc (pretty_printer &pp) const
239 {
240 pp_string (&pp, m_desc);
241 }
242
243 /* class precanned_custom_event : public custom_event. */
244
245 /* Implementation of diagnostic_event::print_desc vfunc for
246 precanned_custom_event.
247 Use the saved string as the event's description. */
248
249 void
250 precanned_custom_event::print_desc (pretty_printer &pp) const
251 {
252 pp_string (&pp, m_desc);
253 }
254
255 /* class statement_event : public checker_event. */
256
257 /* statement_event's ctor. */
258
259 statement_event::statement_event (const gimple *stmt, tree fndecl, int depth,
260 const program_state &dst_state)
261 : checker_event (EK_STMT,
262 event_loc_info (gimple_location (stmt), fndecl, depth)),
263 m_stmt (stmt),
264 m_dst_state (dst_state)
265 {
266 }
267
268 /* Implementation of diagnostic_event::print_desc vfunc for
269 statement_event.
270 Use the statement's dump form as the event's description. */
271
272 void
273 statement_event::print_desc (pretty_printer &pp) const
274 {
275 pp_string (&pp, "stmt: ");
276 pp_gimple_stmt_1 (&pp, m_stmt, 0, (dump_flags_t)0);
277 }
278
279 /* class region_creation_event : public checker_event. */
280
281 region_creation_event::region_creation_event (const event_loc_info &loc_info)
282 : checker_event (EK_REGION_CREATION, loc_info)
283 {
284 }
285
286 /* The various region_creation_event subclasses' print_desc
287 implementations. */
288
289 void
290 region_creation_event_memory_space::print_desc (pretty_printer &pp) const
291 {
292 switch (m_mem_space)
293 {
294 default:
295 pp_string (&pp, "region created here");
296 return;
297 case MEMSPACE_STACK:
298 pp_string (&pp, "region created on stack here");
299 return;
300 case MEMSPACE_HEAP:
301 pp_string (&pp, "region created on heap here");
302 return;
303 }
304 }
305
306 void
307 region_creation_event_capacity::print_desc (pretty_printer &pp) const
308 {
309 gcc_assert (m_capacity);
310 if (TREE_CODE (m_capacity) == INTEGER_CST)
311 {
312 unsigned HOST_WIDE_INT hwi = tree_to_uhwi (m_capacity);
313 return pp_printf_n (&pp,
314 hwi,
315 "capacity: %wu byte",
316 "capacity: %wu bytes",
317 hwi);
318 }
319 else
320 return pp_printf (&pp, "capacity: %qE bytes", m_capacity);
321 }
322
323 void
324 region_creation_event_allocation_size::print_desc (pretty_printer &pp) const
325 {
326 if (m_capacity)
327 {
328 if (TREE_CODE (m_capacity) == INTEGER_CST)
329 pp_printf_n (&pp,
330 tree_to_uhwi (m_capacity),
331 "allocated %E byte here",
332 "allocated %E bytes here",
333 m_capacity);
334 else
335 pp_printf (&pp,
336 "allocated %qE bytes here",
337 m_capacity);
338 }
339 pp_printf (&pp, "allocated here");
340 }
341
342 void
343 region_creation_event_debug::print_desc (pretty_printer &pp) const
344 {
345 pp_string (&pp, "region creation: ");
346 m_reg->dump_to_pp (&pp, true);
347 if (m_capacity)
348 pp_printf (&pp, " capacity: %qE", m_capacity);
349 }
350
351 /* class function_entry_event : public checker_event. */
352
353 function_entry_event::function_entry_event (const program_point &dst_point)
354 : checker_event (EK_FUNCTION_ENTRY,
355 event_loc_info (dst_point.get_supernode
356 ()->get_start_location (),
357 dst_point.get_fndecl (),
358 dst_point.get_stack_depth ()))
359 {
360 }
361
362 /* Implementation of diagnostic_event::print_desc vfunc for
363 function_entry_event.
364
365 Use a string such as "entry to 'foo'" as the event's description. */
366
367 void
368 function_entry_event::print_desc (pretty_printer &pp) const
369 {
370 pp_printf (&pp, "entry to %qE", m_effective_fndecl);
371 }
372
373 /* Implementation of diagnostic_event::get_meaning vfunc for
374 function entry. */
375
376 diagnostic_event::meaning
377 function_entry_event::get_meaning () const
378 {
379 return meaning (VERB_enter, NOUN_function);
380 }
381
382 /* class state_change_event : public checker_event. */
383
384 /* state_change_event's ctor. */
385
386 state_change_event::state_change_event (const supernode *node,
387 const gimple *stmt,
388 int stack_depth,
389 const state_machine &sm,
390 const svalue *sval,
391 state_machine::state_t from,
392 state_machine::state_t to,
393 const svalue *origin,
394 const program_state &dst_state,
395 const exploded_node *enode)
396 : checker_event (EK_STATE_CHANGE,
397 event_loc_info (stmt->location,
398 node->m_fun->decl,
399 stack_depth)),
400 m_node (node), m_stmt (stmt), m_sm (sm),
401 m_sval (sval), m_from (from), m_to (to),
402 m_origin (origin),
403 m_dst_state (dst_state),
404 m_enode (enode)
405 {
406 }
407
408 /* Implementation of diagnostic_event::print_desc vfunc for
409 state_change_event.
410
411 Attempt to generate a nicer human-readable description.
412 For greatest precision-of-wording, give the pending diagnostic
413 a chance to describe this state change (in terms of the
414 diagnostic).
415 Note that we only have a pending_diagnostic set on the event once
416 the diagnostic is about to being emitted, so the description for
417 an event can change. */
418
419 void
420 state_change_event::print_desc (pretty_printer &pp) const
421 {
422 if (m_pending_diagnostic)
423 {
424 region_model *model = m_dst_state.m_region_model;
425 tree var = model->get_representative_tree (m_sval);
426 tree origin = model->get_representative_tree (m_origin);
427 evdesc::state_change evd (var, origin,
428 m_from, m_to, m_emission_id, *this);
429 if (m_pending_diagnostic->describe_state_change (pp, evd))
430 {
431 if (flag_analyzer_verbose_state_changes)
432 {
433 /* Append debugging information about this event. */
434
435 if (var)
436 pp_printf (&pp, " (state of %qE: ", var);
437 else
438 pp_string (&pp, " (state: ");
439
440 pp_printf (&pp, "%qs -> %qs, ",
441 m_from->get_name (),
442 m_to->get_name ());
443
444 if (m_origin)
445 pp_printf (&pp, "origin: %qE", origin);
446 else
447 pp_string (&pp, "NULL origin");
448
449 /* Get any "meaning" of event. */
450 diagnostic_event::meaning meaning = get_meaning ();
451 pp_string (&pp, ", meaning: ");
452 meaning.dump_to_pp (&pp);
453 pp_string (&pp, ")");
454 }
455 return;
456 }
457 }
458
459 /* Fallback description. */
460 if (m_sval)
461 {
462 label_text sval_desc = m_sval->get_desc ();
463 pp_printf (&pp,
464 "state of %qs: %qs -> %qs",
465 sval_desc.get (),
466 m_from->get_name (),
467 m_to->get_name ());
468 if (m_origin)
469 {
470 label_text origin_desc = m_origin->get_desc ();
471 pp_printf (&pp, " (origin: %qs)",
472 origin_desc.get ());
473 }
474 else
475 pp_string (&pp, " (NULL origin)");
476 }
477 else
478 {
479 gcc_assert (m_origin == NULL);
480 pp_printf (&pp,
481 "global state: %qs -> %qs",
482 m_from->get_name (),
483 m_to->get_name ());
484 }
485 }
486
487 /* Implementation of diagnostic_event::get_meaning vfunc for
488 state change events: delegate to the pending_diagnostic to
489 get any meaning. */
490
491 diagnostic_event::meaning
492 state_change_event::get_meaning () const
493 {
494 if (m_pending_diagnostic)
495 {
496 region_model *model = m_dst_state.m_region_model;
497 tree var = model->get_representative_tree (m_sval);
498 tree origin = model->get_representative_tree (m_origin);
499 evdesc::state_change evd (var, origin,
500 m_from, m_to, m_emission_id, *this);
501 return m_pending_diagnostic->get_meaning_for_state_change (evd);
502 }
503 else
504 return meaning ();
505 }
506
507 /* class superedge_event : public checker_event. */
508
509 /* Implementation of diagnostic_event::maybe_add_sarif_properties
510 for superedge_event. */
511
512 void
513 superedge_event::maybe_add_sarif_properties (sarif_object &thread_flow_loc_obj)
514 const
515 {
516 checker_event::maybe_add_sarif_properties (thread_flow_loc_obj);
517 sarif_property_bag &props = thread_flow_loc_obj.get_or_create_properties ();
518 #define PROPERTY_PREFIX "gcc/analyzer/superedge_event/"
519 if (m_sedge)
520 props.set (PROPERTY_PREFIX "superedge", m_sedge->to_json ());
521 #undef PROPERTY_PREFIX
522 }
523
524 /* Get the callgraph_superedge for this superedge_event, which must be
525 for an interprocedural edge, rather than a CFG edge. */
526
527 const callgraph_superedge&
528 superedge_event::get_callgraph_superedge () const
529 {
530 gcc_assert (m_sedge->m_kind != SUPEREDGE_CFG_EDGE);
531 return *m_sedge->dyn_cast_callgraph_superedge ();
532 }
533
534 /* Determine if this event should be filtered at the given verbosity
535 level. */
536
537 bool
538 superedge_event::should_filter_p (int verbosity) const
539 {
540 switch (m_sedge->m_kind)
541 {
542 case SUPEREDGE_CFG_EDGE:
543 {
544 if (verbosity < 2)
545 return true;
546
547 if (verbosity < 4)
548 {
549 /* Filter events with empty descriptions. This ought to filter
550 FALLTHRU, but retain true/false/switch edges. */
551 auto pp = global_dc->clone_printer ();
552 print_desc (*pp.get ());
553 if (pp_formatted_text (pp.get ()) [0] == '\0')
554 return true;
555 }
556 }
557 break;
558
559 default:
560 break;
561 }
562 return false;
563 }
564
565 /* superedge_event's ctor. */
566
567 superedge_event::superedge_event (enum event_kind kind,
568 const exploded_edge &eedge,
569 const event_loc_info &loc_info)
570 : checker_event (kind, loc_info),
571 m_eedge (eedge), m_sedge (eedge.m_sedge),
572 m_var (NULL_TREE), m_critical_state (0)
573 {
574 /* Note that m_sedge can be nullptr for e.g. jumps through
575 function pointers. */
576 }
577
578 /* class cfg_edge_event : public superedge_event. */
579
580 /* Get the cfg_superedge for this cfg_edge_event. */
581
582 const cfg_superedge &
583 cfg_edge_event::get_cfg_superedge () const
584 {
585 return *m_sedge->dyn_cast_cfg_superedge ();
586 }
587
588 /* cfg_edge_event's ctor. */
589
590 cfg_edge_event::cfg_edge_event (enum event_kind kind,
591 const exploded_edge &eedge,
592 const event_loc_info &loc_info)
593 : superedge_event (kind, eedge, loc_info)
594 {
595 gcc_assert (eedge.m_sedge->m_kind == SUPEREDGE_CFG_EDGE);
596 }
597
598 /* Implementation of diagnostic_event::get_meaning vfunc for
599 CFG edge events. */
600
601 diagnostic_event::meaning
602 cfg_edge_event::get_meaning () const
603 {
604 const cfg_superedge& cfg_sedge = get_cfg_superedge ();
605 if (cfg_sedge.true_value_p ())
606 return meaning (VERB_branch, PROPERTY_true);
607 else if (cfg_sedge.false_value_p ())
608 return meaning (VERB_branch, PROPERTY_false);
609 else
610 return meaning ();
611 }
612
613 /* class start_cfg_edge_event : public cfg_edge_event. */
614
615 /* Implementation of diagnostic_event::print_desc vfunc for
616 start_cfg_edge_event.
617
618 If -fanalyzer-verbose-edges, then generate low-level descriptions, such
619 as
620 "taking 'true' edge SN:7 -> SN:8".
621
622 Otherwise, generate strings using the label of the underlying CFG if
623 any, such as:
624 "following 'true' branch..." or
625 "following 'case 3' branch..."
626 "following 'default' branch..."
627
628 For conditionals, attempt to supply a description of the condition that
629 holds, such as:
630 "following 'false' branch (when 'ptr' is non-NULL)..."
631
632 Failing that, print nothing (which will lead to this event
633 being filtered). */
634
635 void
636 start_cfg_edge_event::print_desc (pretty_printer &pp) const
637 {
638 bool user_facing = !flag_analyzer_verbose_edges;
639 label_text edge_desc (m_sedge->get_description (user_facing));
640 if (user_facing)
641 {
642 if (edge_desc.get () && strlen (edge_desc.get ()) > 0)
643 {
644 label_text cond_desc = maybe_describe_condition (pp_show_color (&pp));
645 label_text result;
646 if (cond_desc.get ())
647 pp_printf (&pp,
648 "following %qs branch (%s)...",
649 edge_desc.get (), cond_desc.get ());
650 else
651 pp_printf (&pp,
652 "following %qs branch...",
653 edge_desc.get ());
654 }
655 }
656 else
657 {
658 if (strlen (edge_desc.get ()) > 0)
659 return pp_printf (&pp,
660 "taking %qs edge SN:%i -> SN:%i",
661 edge_desc.get (),
662 m_sedge->m_src->m_index,
663 m_sedge->m_dest->m_index);
664 else
665 return pp_printf (&pp,
666 "taking edge SN:%i -> SN:%i",
667 m_sedge->m_src->m_index,
668 m_sedge->m_dest->m_index);
669 }
670 }
671
672 /* Attempt to generate a description of any condition that holds at this edge.
673
674 The intent is to make the user-facing messages more clear, especially for
675 cases where there's a single or double-negative, such as
676 when describing the false branch of an inverted condition.
677
678 For example, rather than printing just:
679
680 | if (!ptr)
681 | ~
682 | |
683 | (1) following 'false' branch...
684
685 it's clearer to spell out the condition that holds:
686
687 | if (!ptr)
688 | ~
689 | |
690 | (1) following 'false' branch (when 'ptr' is non-NULL)...
691 ^^^^^^^^^^^^^^^^^^^^^^
692
693 In the above example, this function would generate the highlighted
694 string: "when 'ptr' is non-NULL".
695
696 If the edge is not a condition, or it's not clear that a description of
697 the condition would be helpful to the user, return NULL. */
698
699 label_text
700 start_cfg_edge_event::maybe_describe_condition (bool can_colorize) const
701 {
702 const cfg_superedge& cfg_sedge = get_cfg_superedge ();
703
704 if (cfg_sedge.true_value_p () || cfg_sedge.false_value_p ())
705 {
706 const gimple *last_stmt = m_sedge->m_src->get_last_stmt ();
707 if (const gcond *cond_stmt = dyn_cast <const gcond *> (last_stmt))
708 {
709 enum tree_code op = gimple_cond_code (cond_stmt);
710 tree lhs = gimple_cond_lhs (cond_stmt);
711 tree rhs = gimple_cond_rhs (cond_stmt);
712 if (cfg_sedge.false_value_p ())
713 op = invert_tree_comparison (op, false /* honor_nans */);
714 return maybe_describe_condition (can_colorize,
715 lhs, op, rhs);
716 }
717 }
718 return label_text::borrow (NULL);
719 }
720
721 /* Subroutine of maybe_describe_condition above.
722
723 Attempt to generate a user-facing description of the condition
724 LHS OP RHS, but only if it is likely to make it easier for the
725 user to understand a condition. */
726
727 label_text
728 start_cfg_edge_event::maybe_describe_condition (bool can_colorize,
729 tree lhs,
730 enum tree_code op,
731 tree rhs)
732 {
733 /* In theory we could just build a tree via
734 fold_build2 (op, boolean_type_node, lhs, rhs)
735 and print it with %qE on it, but this leads to warts such as
736 parenthesizing vars, such as '(i) <= 9', and uses of '<unknown>'. */
737
738 /* Special-case: describe testing the result of strcmp, as figuring
739 out what the "true" or "false" path is can be confusing to the user. */
740 if (TREE_CODE (lhs) == SSA_NAME
741 && zerop (rhs))
742 {
743 if (gcall *call = dyn_cast <gcall *> (SSA_NAME_DEF_STMT (lhs)))
744 if (is_special_named_call_p (call, "strcmp", 2))
745 {
746 if (op == EQ_EXPR)
747 return label_text::borrow ("when the strings are equal");
748 if (op == NE_EXPR)
749 return label_text::borrow ("when the strings are non-equal");
750 }
751 }
752
753 /* Only attempt to generate text for sufficiently simple expressions. */
754 if (!should_print_expr_p (lhs))
755 return label_text::borrow (NULL);
756 if (!should_print_expr_p (rhs))
757 return label_text::borrow (NULL);
758
759 /* Special cases for pointer comparisons against NULL. */
760 if (POINTER_TYPE_P (TREE_TYPE (lhs))
761 && POINTER_TYPE_P (TREE_TYPE (rhs))
762 && zerop (rhs))
763 {
764 if (op == EQ_EXPR)
765 return make_label_text (can_colorize, "when %qE is NULL",
766 lhs);
767 if (op == NE_EXPR)
768 return make_label_text (can_colorize, "when %qE is non-NULL",
769 lhs);
770 }
771
772 return make_label_text (can_colorize, "when %<%E %s %E%>",
773 lhs, op_symbol_code (op), rhs);
774 }
775
776 /* Subroutine of maybe_describe_condition.
777
778 Return true if EXPR is we will get suitable user-facing output
779 from %E on it. */
780
781 bool
782 start_cfg_edge_event::should_print_expr_p (tree expr)
783 {
784 if (TREE_CODE (expr) == SSA_NAME)
785 {
786 if (SSA_NAME_VAR (expr))
787 return should_print_expr_p (SSA_NAME_VAR (expr));
788 else
789 return false;
790 }
791
792 if (DECL_P (expr))
793 return true;
794
795 if (CONSTANT_CLASS_P (expr))
796 return true;
797
798 return false;
799 }
800
801 /* class call_event : public superedge_event. */
802
803 /* call_event's ctor. */
804
805 call_event::call_event (const exploded_edge &eedge,
806 const event_loc_info &loc_info)
807 : superedge_event (EK_CALL_EDGE, eedge, loc_info)
808 {
809 if (eedge.m_sedge)
810 gcc_assert (eedge.m_sedge->m_kind == SUPEREDGE_CALL);
811
812 m_src_snode = eedge.m_src->get_supernode ();
813 m_dest_snode = eedge.m_dest->get_supernode ();
814 }
815
816 /* Implementation of diagnostic_event::print_desc vfunc for
817 call_event.
818
819 If this call event passes critical state for an sm-based warning,
820 allow the diagnostic to generate a precise description, such as:
821
822 "passing freed pointer 'ptr' in call to 'foo' from 'bar'"
823
824 Otherwise, generate a description of the form
825 "calling 'foo' from 'bar'". */
826
827 void
828 call_event::print_desc (pretty_printer &pp) const
829 {
830 if (m_critical_state && m_pending_diagnostic)
831 {
832 gcc_assert (m_var);
833 tree var = fixup_tree_for_diagnostic (m_var);
834 evdesc::call_with_state evd (m_src_snode->m_fun->decl,
835 m_dest_snode->m_fun->decl,
836 var,
837 m_critical_state);
838 if (m_pending_diagnostic->describe_call_with_state (pp, evd))
839 return;
840 }
841
842 pp_printf (&pp,
843 "calling %qE from %qE",
844 get_callee_fndecl (),
845 get_caller_fndecl ());
846 }
847
848 /* Implementation of diagnostic_event::get_meaning vfunc for
849 function call events. */
850
851 diagnostic_event::meaning
852 call_event::get_meaning () const
853 {
854 return meaning (VERB_call, NOUN_function);
855 }
856
857 /* Override of checker_event::is_call_p for calls. */
858
859 bool
860 call_event::is_call_p () const
861 {
862 return true;
863 }
864
865 tree
866 call_event::get_caller_fndecl () const
867 {
868 return m_src_snode->m_fun->decl;
869 }
870
871 tree
872 call_event::get_callee_fndecl () const
873 {
874 return m_dest_snode->m_fun->decl;
875 }
876
877 /* class return_event : public superedge_event. */
878
879 /* return_event's ctor. */
880
881 return_event::return_event (const exploded_edge &eedge,
882 const event_loc_info &loc_info)
883 : superedge_event (EK_RETURN_EDGE, eedge, loc_info)
884 {
885 if (eedge.m_sedge)
886 gcc_assert (eedge.m_sedge->m_kind == SUPEREDGE_RETURN);
887
888 m_src_snode = eedge.m_src->get_supernode ();
889 m_dest_snode = eedge.m_dest->get_supernode ();
890 }
891
892 /* Implementation of diagnostic_event::print_desc vfunc for
893 return_event.
894
895 If this return event returns critical state for an sm-based warning,
896 allow the diagnostic to generate a precise description, such as:
897
898 "possible of NULL to 'foo' from 'bar'"
899
900 Otherwise, generate a description of the form
901 "returning to 'foo' from 'bar'. */
902
903 void
904 return_event::print_desc (pretty_printer &pp) const
905 {
906 /* For greatest precision-of-wording, if this is returning the
907 state involved in the pending diagnostic, give the pending
908 diagnostic a chance to describe this return (in terms of
909 itself). */
910 if (m_critical_state && m_pending_diagnostic)
911 {
912 evdesc::return_of_state evd (m_dest_snode->m_fun->decl,
913 m_src_snode->m_fun->decl,
914 m_critical_state);
915 if (m_pending_diagnostic->describe_return_of_state (pp, evd))
916 return;
917 }
918 pp_printf (&pp,
919 "returning to %qE from %qE",
920 m_dest_snode->m_fun->decl,
921 m_src_snode->m_fun->decl);
922 }
923
924 /* Implementation of diagnostic_event::get_meaning vfunc for
925 function return events. */
926
927 diagnostic_event::meaning
928 return_event::get_meaning () const
929 {
930 return meaning (VERB_return, NOUN_function);
931 }
932
933 /* Override of checker_event::is_return_p for returns. */
934
935 bool
936 return_event::is_return_p () const
937 {
938 return true;
939 }
940
941 /* class start_consolidated_cfg_edges_event : public checker_event. */
942
943 void
944 start_consolidated_cfg_edges_event::print_desc (pretty_printer &pp) const
945 {
946 pp_printf (&pp,
947 "following %qs branch...",
948 m_edge_sense ? "true" : "false");
949 }
950
951 /* Implementation of diagnostic_event::get_meaning vfunc for
952 start_consolidated_cfg_edges_event. */
953
954 diagnostic_event::meaning
955 start_consolidated_cfg_edges_event::get_meaning () const
956 {
957 return meaning (VERB_branch,
958 (m_edge_sense ? PROPERTY_true : PROPERTY_false));
959 }
960
961 /* class inlined_call_event : public checker_event. */
962
963 void
964 inlined_call_event::print_desc (pretty_printer &pp) const
965 {
966 pp_printf (&pp,
967 "inlined call to %qE from %qE",
968 m_apparent_callee_fndecl,
969 m_apparent_caller_fndecl);
970 }
971
972 /* Implementation of diagnostic_event::get_meaning vfunc for
973 reconstructed inlined function calls. */
974
975 diagnostic_event::meaning
976 inlined_call_event::get_meaning () const
977 {
978 return meaning (VERB_call, NOUN_function);
979 }
980
981 /* class setjmp_event : public checker_event. */
982
983 /* Implementation of diagnostic_event::print_desc vfunc for
984 setjmp_event. */
985
986 void
987 setjmp_event::print_desc (pretty_printer &pp) const
988 {
989 pp_printf (&pp,
990 "%qs called here",
991 get_user_facing_name (m_setjmp_call));
992 }
993
994 /* Implementation of checker_event::prepare_for_emission vfunc for setjmp_event.
995
996 Record this setjmp's event ID into the path, so that rewind events can
997 use it. */
998
999 void
1000 setjmp_event::prepare_for_emission (checker_path *path,
1001 pending_diagnostic *pd,
1002 diagnostic_event_id_t emission_id)
1003 {
1004 checker_event::prepare_for_emission (path, pd, emission_id);
1005 path->record_setjmp_event (m_enode, emission_id);
1006 }
1007
1008 /* class rewind_event : public checker_event. */
1009
1010 /* Get the fndecl containing the site of the longjmp call. */
1011
1012 tree
1013 rewind_event::get_longjmp_caller () const
1014 {
1015 return m_eedge->m_src->get_function ()->decl;
1016 }
1017
1018 /* Get the fndecl containing the site of the setjmp call. */
1019
1020 tree
1021 rewind_event::get_setjmp_caller () const
1022 {
1023 return m_eedge->m_dest->get_function ()->decl;
1024 }
1025
1026 /* rewind_event's ctor. */
1027
1028 rewind_event::rewind_event (const exploded_edge *eedge,
1029 enum event_kind kind,
1030 const event_loc_info &loc_info,
1031 const rewind_info_t *rewind_info)
1032 : checker_event (kind, loc_info),
1033 m_rewind_info (rewind_info),
1034 m_eedge (eedge)
1035 {
1036 gcc_assert (m_eedge->m_custom_info.get () == m_rewind_info);
1037 }
1038
1039 /* class rewind_from_longjmp_event : public rewind_event. */
1040
1041 /* Implementation of diagnostic_event::print_desc vfunc for
1042 rewind_from_longjmp_event. */
1043
1044 void
1045 rewind_from_longjmp_event::print_desc (pretty_printer &pp) const
1046 {
1047 const char *src_name
1048 = get_user_facing_name (m_rewind_info->get_longjmp_call ());
1049
1050 if (get_longjmp_caller () == get_setjmp_caller ())
1051 /* Special-case: purely intraprocedural rewind. */
1052 pp_printf (&pp,
1053 "rewinding within %qE from %qs...",
1054 get_longjmp_caller (),
1055 src_name);
1056 else
1057 pp_printf (&pp,
1058 "rewinding from %qs in %qE...",
1059 src_name,
1060 get_longjmp_caller ());
1061 }
1062
1063 /* class rewind_to_setjmp_event : public rewind_event. */
1064
1065 /* Implementation of diagnostic_event::print_desc vfunc for
1066 rewind_to_setjmp_event. */
1067
1068 void
1069 rewind_to_setjmp_event::print_desc (pretty_printer &pp) const
1070 {
1071 const char *dst_name
1072 = get_user_facing_name (m_rewind_info->get_setjmp_call ());
1073
1074 /* If we can, identify the ID of the setjmp_event. */
1075 if (m_original_setjmp_event_id.known_p ())
1076 {
1077 if (get_longjmp_caller () == get_setjmp_caller ())
1078 /* Special-case: purely intraprocedural rewind. */
1079 pp_printf (&pp,
1080 "...to %qs (saved at %@)",
1081 dst_name,
1082 &m_original_setjmp_event_id);
1083 else
1084 pp_printf (&pp,
1085 "...to %qs in %qE (saved at %@)",
1086 dst_name,
1087 get_setjmp_caller (),
1088 &m_original_setjmp_event_id);
1089 }
1090 else
1091 {
1092 if (get_longjmp_caller () == get_setjmp_caller ())
1093 /* Special-case: purely intraprocedural rewind. */
1094 pp_printf (&pp,
1095 "...to %qs",
1096 dst_name);
1097 else
1098 pp_printf (&pp,
1099 "...to %qs in %qE",
1100 dst_name,
1101 get_setjmp_caller ());
1102 }
1103 }
1104
1105 /* Implementation of checker_event::prepare_for_emission vfunc for
1106 rewind_to_setjmp_event.
1107
1108 Attempt to look up the setjmp event ID that recorded the jmp_buf
1109 for this rewind. */
1110
1111 void
1112 rewind_to_setjmp_event::prepare_for_emission (checker_path *path,
1113 pending_diagnostic *pd,
1114 diagnostic_event_id_t emission_id)
1115 {
1116 checker_event::prepare_for_emission (path, pd, emission_id);
1117 path->get_setjmp_event (m_rewind_info->get_enode_origin (),
1118 &m_original_setjmp_event_id);
1119 }
1120
1121 /* class warning_event : public checker_event. */
1122
1123 /* Implementation of diagnostic_event::print_desc vfunc for
1124 warning_event.
1125
1126 If the pending diagnostic implements describe_final_event, use it,
1127 generating a precise description e.g.
1128 "second 'free' here; first 'free' was at (7)"
1129
1130 Otherwise generate a generic description. */
1131
1132 void
1133 warning_event::print_desc (pretty_printer &pp) const
1134 {
1135 if (m_pending_diagnostic)
1136 {
1137 tree var = fixup_tree_for_diagnostic (m_var);
1138 evdesc::final_event evd (var, m_state, *this);
1139 if (m_pending_diagnostic->describe_final_event (pp, evd))
1140 {
1141 if (m_sm && flag_analyzer_verbose_state_changes)
1142 {
1143 if (var)
1144 pp_printf (&pp, " (%qE is in state %qs)",
1145 var, m_state->get_name ());
1146 else
1147 pp_printf (&pp, " (in global state %qs)",
1148 m_state->get_name ());
1149 }
1150 return;
1151 }
1152 }
1153
1154 if (m_sm)
1155 {
1156 if (m_var)
1157 pp_printf (&pp, "here (%qE is in state %qs)",
1158 m_var, m_state->get_name ());
1159 else
1160 pp_printf (&pp, "here (in global state %qs)",
1161 m_state->get_name ());
1162 return;
1163 }
1164 else
1165 pp_string (&pp, "here");
1166 }
1167
1168 /* Implementation of diagnostic_event::get_meaning vfunc for
1169 warning_event. */
1170
1171 diagnostic_event::meaning
1172 warning_event::get_meaning () const
1173 {
1174 return meaning (VERB_danger, NOUN_unknown);
1175 }
1176
1177 } // namespace ana
1178
1179 #endif /* #if ENABLE_ANALYZER */