]>
Commit | Line | Data |
---|---|---|
757bf1df | 1 | /* Classes for analyzer diagnostics. |
a945c346 | 2 | Copyright (C) 2019-2024 Free Software Foundation, Inc. |
757bf1df DM |
3 | Contributed by David Malcolm <dmalcolm@redhat.com>. |
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" | |
6341f14e | 22 | #define INCLUDE_MEMORY |
97238e42 | 23 | #define INCLUDE_VECTOR |
757bf1df DM |
24 | #include "system.h" |
25 | #include "coretypes.h" | |
26 | #include "tree.h" | |
27 | #include "intl.h" | |
28 | #include "diagnostic.h" | |
757bf1df DM |
29 | #include "analyzer/analyzer.h" |
30 | #include "diagnostic-event-id.h" | |
31 | #include "analyzer/analyzer-logging.h" | |
32 | #include "analyzer/sm.h" | |
33 | #include "diagnostic-event-id.h" | |
34 | #include "analyzer/sm.h" | |
35 | #include "analyzer/pending-diagnostic.h" | |
2402dc6b | 36 | #include "analyzer/diagnostic-manager.h" |
00e7d024 DM |
37 | #include "analyzer/call-string.h" |
38 | #include "analyzer/program-point.h" | |
39 | #include "analyzer/store.h" | |
40 | #include "analyzer/region-model.h" | |
2402dc6b DM |
41 | #include "cpplib.h" |
42 | #include "digraph.h" | |
43 | #include "ordered-hash-map.h" | |
44 | #include "cfg.h" | |
45 | #include "basic-block.h" | |
46 | #include "gimple.h" | |
47 | #include "gimple-iterator.h" | |
48 | #include "cgraph.h" | |
49 | #include "analyzer/supergraph.h" | |
50 | #include "analyzer/program-state.h" | |
2402dc6b DM |
51 | #include "analyzer/exploded-graph.h" |
52 | #include "diagnostic-path.h" | |
53 | #include "analyzer/checker-path.h" | |
d60b40b8 | 54 | #include "make-unique.h" |
757bf1df DM |
55 | |
56 | #if ENABLE_ANALYZER | |
57 | ||
75038aa6 DM |
58 | namespace ana { |
59 | ||
00e7d024 DM |
60 | /* struct interesting_t. */ |
61 | ||
62 | /* Mark the creation of REG as being interesting. */ | |
63 | ||
64 | void | |
65 | interesting_t::add_region_creation (const region *reg) | |
66 | { | |
67 | gcc_assert (reg); | |
68 | m_region_creation.safe_push (reg); | |
69 | } | |
70 | ||
71 | void | |
72 | interesting_t::dump_to_pp (pretty_printer *pp, bool simple) const | |
73 | { | |
74 | pp_string (pp, "{ region creation: ["); | |
75 | unsigned i; | |
76 | const region *reg; | |
77 | FOR_EACH_VEC_ELT (m_region_creation, i, reg) | |
78 | { | |
79 | if (i > 0) | |
80 | pp_string (pp, ", "); | |
81 | reg->dump_to_pp (pp, simple); | |
82 | } | |
83 | pp_string (pp, "]}"); | |
84 | } | |
85 | ||
757bf1df DM |
86 | /* Generate a label_text by printing FMT. |
87 | ||
88 | Use a clone of the global_dc for formatting callbacks. | |
89 | ||
90 | Use this evdesc::event_desc's m_colorize flag to control colorization | |
91 | (so that e.g. we can disable it for JSON output). */ | |
92 | ||
93 | label_text | |
94 | evdesc::event_desc::formatted_print (const char *fmt, ...) const | |
95 | { | |
19363bf5 | 96 | pretty_printer *pp = global_dc->m_printer->clone (); |
757bf1df DM |
97 | |
98 | pp_show_color (pp) = m_colorize; | |
99 | ||
757bf1df DM |
100 | rich_location rich_loc (line_table, UNKNOWN_LOCATION); |
101 | va_list ap; | |
102 | va_start (ap, fmt); | |
c44ca7c0 | 103 | text_info ti (_(fmt), &ap, 0, nullptr, &rich_loc); |
757bf1df DM |
104 | pp_format (pp, &ti); |
105 | pp_output_formatted_text (pp); | |
106 | va_end (ap); | |
107 | ||
108 | label_text result = label_text::take (xstrdup (pp_formatted_text (pp))); | |
109 | delete pp; | |
110 | return result; | |
111 | } | |
112 | ||
12b67d1e DM |
113 | /* class diagnostic_emission_context. */ |
114 | ||
115 | /* Get the pending_diagnostic being emitted. */ | |
116 | ||
117 | const pending_diagnostic & | |
118 | diagnostic_emission_context::get_pending_diagnostic () const | |
119 | { | |
120 | return *m_sd.m_d.get (); | |
121 | } | |
122 | ||
123 | /* Emit a warning, using the rich_location, metadata, and the | |
124 | pending_diagnostic's option. */ | |
125 | ||
126 | bool | |
127 | diagnostic_emission_context::warn (const char *gmsgid, ...) | |
128 | { | |
129 | const pending_diagnostic &pd = get_pending_diagnostic (); | |
130 | auto_diagnostic_group d; | |
131 | va_list ap; | |
132 | va_start (ap, gmsgid); | |
b753ef8f DM |
133 | const bool result = emit_diagnostic_valist_meta (DK_WARNING, |
134 | &m_rich_loc, &m_metadata, | |
135 | pd.get_controlling_option (), | |
136 | gmsgid, &ap); | |
12b67d1e DM |
137 | va_end (ap); |
138 | return result; | |
139 | } | |
140 | ||
141 | /* Emit a note, using the rich_location and metadata (and the | |
142 | pending_diagnostic's option). */ | |
143 | ||
144 | void | |
145 | diagnostic_emission_context::inform (const char *gmsgid, ...) | |
146 | { | |
147 | const pending_diagnostic &pd = get_pending_diagnostic (); | |
148 | auto_diagnostic_group d; | |
149 | va_list ap; | |
150 | va_start (ap, gmsgid); | |
b753ef8f DM |
151 | emit_diagnostic_valist_meta (DK_NOTE, |
152 | &m_rich_loc, &m_metadata, | |
153 | pd.get_controlling_option (), | |
154 | gmsgid, &ap); | |
12b67d1e DM |
155 | va_end (ap); |
156 | } | |
157 | ||
14f9d7b9 DM |
158 | /* Return true if T1 and T2 are "the same" for the purposes of |
159 | diagnostic deduplication. */ | |
160 | ||
161 | bool | |
162 | pending_diagnostic::same_tree_p (tree t1, tree t2) | |
163 | { | |
164 | return simple_cst_equal (t1, t2) == 1; | |
165 | } | |
166 | ||
2402dc6b DM |
167 | /* Return true iff IDENT is STR. */ |
168 | ||
169 | static bool | |
170 | ht_ident_eq (ht_identifier ident, const char *str) | |
171 | { | |
172 | return (strlen (str) == ident.len | |
173 | && 0 == strcmp (str, (const char *)ident.str)); | |
174 | } | |
175 | ||
176 | /* Return true if we should show the expansion location rather than unwind | |
177 | within MACRO. */ | |
178 | ||
179 | static bool | |
180 | fixup_location_in_macro_p (cpp_hashnode *macro) | |
181 | { | |
182 | ht_identifier ident = macro->ident; | |
70d34f2a DM |
183 | |
184 | /* Don't unwind inside "alloca" macro, so that we don't suppress warnings | |
185 | from it (due to being in system headers). */ | |
186 | if (ht_ident_eq (ident, "alloca")) | |
187 | return true; | |
188 | ||
2402dc6b DM |
189 | /* Don't unwind inside <stdarg.h> macros, so that we don't suppress warnings |
190 | from them (due to being in system headers). */ | |
191 | if (ht_ident_eq (ident, "va_start") | |
192 | || ht_ident_eq (ident, "va_copy") | |
193 | || ht_ident_eq (ident, "va_arg") | |
194 | || ht_ident_eq (ident, "va_end")) | |
195 | return true; | |
196 | return false; | |
197 | } | |
198 | ||
199 | /* Base implementation of pending_diagnostic::fixup_location. | |
200 | Don't unwind inside macros for which fixup_location_in_macro_p is true. */ | |
201 | ||
202 | location_t | |
d777b38c | 203 | pending_diagnostic::fixup_location (location_t loc, bool) const |
2402dc6b DM |
204 | { |
205 | if (linemap_location_from_macro_expansion_p (line_table, loc)) | |
206 | { | |
207 | line_map *map | |
208 | = const_cast <line_map *> (linemap_lookup (line_table, loc)); | |
209 | const line_map_macro *macro_map = linemap_check_macro (map); | |
210 | if (fixup_location_in_macro_p (macro_map->macro)) | |
211 | loc = linemap_resolve_location (line_table, loc, | |
212 | LRK_MACRO_EXPANSION_POINT, NULL); | |
213 | } | |
214 | return loc; | |
215 | } | |
216 | ||
12c583a2 DM |
217 | /* Base implementation of pending_diagnostic::add_function_entry_event. |
218 | Add a function_entry_event to EMISSION_PATH. */ | |
219 | ||
220 | void | |
221 | pending_diagnostic::add_function_entry_event (const exploded_edge &eedge, | |
222 | checker_path *emission_path) | |
223 | { | |
224 | const exploded_node *dst_node = eedge.m_dest; | |
225 | const program_point &dst_point = dst_node->get_point (); | |
226 | emission_path->add_event (make_unique<function_entry_event> (dst_point)); | |
227 | } | |
228 | ||
2402dc6b DM |
229 | /* Base implementation of pending_diagnostic::add_call_event. |
230 | Add a call_event to EMISSION_PATH. */ | |
231 | ||
232 | void | |
233 | pending_diagnostic::add_call_event (const exploded_edge &eedge, | |
234 | checker_path *emission_path) | |
235 | { | |
236 | const exploded_node *src_node = eedge.m_src; | |
237 | const program_point &src_point = src_node->get_point (); | |
238 | const int src_stack_depth = src_point.get_stack_depth (); | |
239 | const gimple *last_stmt = src_point.get_supernode ()->get_last_stmt (); | |
240 | emission_path->add_event | |
d60b40b8 | 241 | (make_unique<call_event> (eedge, |
e24fe128 DM |
242 | event_loc_info (last_stmt |
243 | ? last_stmt->location | |
244 | : UNKNOWN_LOCATION, | |
245 | src_point.get_fndecl (), | |
246 | src_stack_depth))); | |
2402dc6b DM |
247 | } |
248 | ||
f5758fe5 DM |
249 | /* Base implementation of pending_diagnostic::add_region_creation_events. |
250 | See the comment for class region_creation_event. */ | |
251 | ||
252 | void | |
253 | pending_diagnostic::add_region_creation_events (const region *reg, | |
254 | tree capacity, | |
e24fe128 | 255 | const event_loc_info &loc_info, |
f5758fe5 DM |
256 | checker_path &emission_path) |
257 | { | |
258 | emission_path.add_event | |
259 | (make_unique<region_creation_event_memory_space> (reg->get_memory_space (), | |
e24fe128 | 260 | loc_info)); |
f5758fe5 DM |
261 | |
262 | if (capacity) | |
263 | emission_path.add_event | |
e24fe128 | 264 | (make_unique<region_creation_event_capacity> (capacity, loc_info)); |
f5758fe5 DM |
265 | } |
266 | ||
12c583a2 DM |
267 | /* Base implementation of pending_diagnostic::add_final_event. |
268 | Add a warning_event to the end of EMISSION_PATH. */ | |
269 | ||
270 | void | |
271 | pending_diagnostic::add_final_event (const state_machine *sm, | |
272 | const exploded_node *enode, | |
2b0a7fe3 | 273 | const event_loc_info &loc_info, |
12c583a2 DM |
274 | tree var, state_machine::state_t state, |
275 | checker_path *emission_path) | |
276 | { | |
277 | emission_path->add_event | |
e24fe128 | 278 | (make_unique<warning_event> |
2b0a7fe3 | 279 | (loc_info, |
0d6f7b1d | 280 | enode, |
e24fe128 | 281 | sm, var, state)); |
12c583a2 DM |
282 | } |
283 | ||
75038aa6 DM |
284 | } // namespace ana |
285 | ||
757bf1df | 286 | #endif /* #if ENABLE_ANALYZER */ |