1 /* Classes for analyzer diagnostics.
2 Copyright (C) 2019-2023 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/>. */
22 #define INCLUDE_MEMORY
24 #include "coretypes.h"
27 #include "diagnostic.h"
28 #include "analyzer/analyzer.h"
29 #include "diagnostic-event-id.h"
30 #include "analyzer/analyzer-logging.h"
31 #include "analyzer/sm.h"
32 #include "diagnostic-event-id.h"
33 #include "analyzer/sm.h"
34 #include "analyzer/pending-diagnostic.h"
35 #include "analyzer/diagnostic-manager.h"
36 #include "analyzer/call-string.h"
37 #include "analyzer/program-point.h"
38 #include "analyzer/store.h"
39 #include "analyzer/region-model.h"
42 #include "ordered-hash-map.h"
44 #include "basic-block.h"
46 #include "gimple-iterator.h"
48 #include "analyzer/supergraph.h"
49 #include "analyzer/program-state.h"
50 #include "analyzer/exploded-graph.h"
51 #include "diagnostic-path.h"
52 #include "analyzer/checker-path.h"
53 #include "make-unique.h"
59 /* struct interesting_t. */
61 /* Mark the creation of REG as being interesting. */
64 interesting_t::add_region_creation (const region
*reg
)
67 m_region_creation
.safe_push (reg
);
71 interesting_t::dump_to_pp (pretty_printer
*pp
, bool simple
) const
73 pp_string (pp
, "{ region creation: [");
76 FOR_EACH_VEC_ELT (m_region_creation
, i
, reg
)
80 reg
->dump_to_pp (pp
, simple
);
85 /* Generate a label_text by printing FMT.
87 Use a clone of the global_dc for formatting callbacks.
89 Use this evdesc::event_desc's m_colorize flag to control colorization
90 (so that e.g. we can disable it for JSON output). */
93 evdesc::event_desc::formatted_print (const char *fmt
, ...) const
95 pretty_printer
*pp
= global_dc
->printer
->clone ();
97 pp_show_color (pp
) = m_colorize
;
99 rich_location
rich_loc (line_table
, UNKNOWN_LOCATION
);
102 text_info
ti (_(fmt
), &ap
, 0, nullptr, &rich_loc
);
104 pp_output_formatted_text (pp
);
107 label_text result
= label_text::take (xstrdup (pp_formatted_text (pp
)));
112 /* Return true if T1 and T2 are "the same" for the purposes of
113 diagnostic deduplication. */
116 pending_diagnostic::same_tree_p (tree t1
, tree t2
)
118 return simple_cst_equal (t1
, t2
) == 1;
121 /* Return true iff IDENT is STR. */
124 ht_ident_eq (ht_identifier ident
, const char *str
)
126 return (strlen (str
) == ident
.len
127 && 0 == strcmp (str
, (const char *)ident
.str
));
130 /* Return true if we should show the expansion location rather than unwind
134 fixup_location_in_macro_p (cpp_hashnode
*macro
)
136 ht_identifier ident
= macro
->ident
;
138 /* Don't unwind inside "alloca" macro, so that we don't suppress warnings
139 from it (due to being in system headers). */
140 if (ht_ident_eq (ident
, "alloca"))
143 /* Don't unwind inside <stdarg.h> macros, so that we don't suppress warnings
144 from them (due to being in system headers). */
145 if (ht_ident_eq (ident
, "va_start")
146 || ht_ident_eq (ident
, "va_copy")
147 || ht_ident_eq (ident
, "va_arg")
148 || ht_ident_eq (ident
, "va_end"))
153 /* Base implementation of pending_diagnostic::fixup_location.
154 Don't unwind inside macros for which fixup_location_in_macro_p is true. */
157 pending_diagnostic::fixup_location (location_t loc
, bool) const
159 if (linemap_location_from_macro_expansion_p (line_table
, loc
))
162 = const_cast <line_map
*> (linemap_lookup (line_table
, loc
));
163 const line_map_macro
*macro_map
= linemap_check_macro (map
);
164 if (fixup_location_in_macro_p (macro_map
->macro
))
165 loc
= linemap_resolve_location (line_table
, loc
,
166 LRK_MACRO_EXPANSION_POINT
, NULL
);
171 /* Base implementation of pending_diagnostic::add_function_entry_event.
172 Add a function_entry_event to EMISSION_PATH. */
175 pending_diagnostic::add_function_entry_event (const exploded_edge
&eedge
,
176 checker_path
*emission_path
)
178 const exploded_node
*dst_node
= eedge
.m_dest
;
179 const program_point
&dst_point
= dst_node
->get_point ();
180 emission_path
->add_event (make_unique
<function_entry_event
> (dst_point
));
183 /* Base implementation of pending_diagnostic::add_call_event.
184 Add a call_event to EMISSION_PATH. */
187 pending_diagnostic::add_call_event (const exploded_edge
&eedge
,
188 checker_path
*emission_path
)
190 const exploded_node
*src_node
= eedge
.m_src
;
191 const program_point
&src_point
= src_node
->get_point ();
192 const int src_stack_depth
= src_point
.get_stack_depth ();
193 const gimple
*last_stmt
= src_point
.get_supernode ()->get_last_stmt ();
194 emission_path
->add_event
195 (make_unique
<call_event
> (eedge
,
196 event_loc_info (last_stmt
197 ? last_stmt
->location
199 src_point
.get_fndecl (),
203 /* Base implementation of pending_diagnostic::add_region_creation_events.
204 See the comment for class region_creation_event. */
207 pending_diagnostic::add_region_creation_events (const region
*reg
,
209 const event_loc_info
&loc_info
,
210 checker_path
&emission_path
)
212 emission_path
.add_event
213 (make_unique
<region_creation_event_memory_space
> (reg
->get_memory_space (),
217 emission_path
.add_event
218 (make_unique
<region_creation_event_capacity
> (capacity
, loc_info
));
221 /* Base implementation of pending_diagnostic::add_final_event.
222 Add a warning_event to the end of EMISSION_PATH. */
225 pending_diagnostic::add_final_event (const state_machine
*sm
,
226 const exploded_node
*enode
,
228 tree var
, state_machine::state_t state
,
229 checker_path
*emission_path
)
231 emission_path
->add_event
232 (make_unique
<warning_event
>
233 (event_loc_info (get_stmt_location (stmt
, enode
->get_function ()),
234 enode
->get_function ()->decl
,
235 enode
->get_stack_depth ()),
242 #endif /* #if ENABLE_ANALYZER */