1 /* Classes for analyzer diagnostics.
2 Copyright (C) 2019-2025 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 #include "analyzer/common.h"
23 #include "diagnostics/event-id.h"
24 #include "diagnostics/logging.h"
27 #include "ordered-hash-map.h"
29 #include "gimple-iterator.h"
32 #include "analyzer/analyzer-logging.h"
33 #include "analyzer/sm.h"
34 #include "analyzer/sm.h"
35 #include "analyzer/pending-diagnostic.h"
36 #include "analyzer/diagnostic-manager.h"
37 #include "analyzer/call-string.h"
38 #include "analyzer/program-point.h"
39 #include "analyzer/store.h"
40 #include "analyzer/region-model.h"
41 #include "analyzer/supergraph.h"
42 #include "analyzer/program-state.h"
43 #include "analyzer/exploded-graph.h"
44 #include "analyzer/checker-path.h"
50 /* struct interesting_t. */
52 /* Mark the creation of REG as being interesting. */
55 interesting_t::add_region_creation (const region
*reg
)
58 m_region_creation
.safe_push (reg
);
62 interesting_t::dump_to_pp (pretty_printer
*pp
, bool simple
) const
64 pp_string (pp
, "{ region creation: [");
67 FOR_EACH_VEC_ELT (m_region_creation
, i
, reg
)
71 reg
->dump_to_pp (pp
, simple
);
76 /* class diagnostic_emission_context. */
78 /* Get the pending_diagnostic being emitted. */
80 const pending_diagnostic
&
81 diagnostic_emission_context::get_pending_diagnostic () const
83 return *m_sd
.m_d
.get ();
86 /* Emit a warning, using the rich_location, metadata, and the
87 pending_diagnostic's option. */
90 diagnostic_emission_context::warn (const char *gmsgid
, ...)
92 auto dc_logger
= global_dc
->get_logger ();
93 diagnostics::logging::log_function_params
94 (dc_logger
, "ana::diagnostic_emission_context::warn")
95 .log_param_string ("gmsgid", gmsgid
);
96 diagnostics::logging::auto_inc_depth
depth_sentinel (dc_logger
);
98 const pending_diagnostic
&pd
= get_pending_diagnostic ();
99 auto_diagnostic_group d
;
101 va_start (ap
, gmsgid
);
102 const bool result
= emit_diagnostic_valist_meta (diagnostics::kind::warning
,
103 &m_rich_loc
, &m_metadata
,
104 pd
.get_controlling_option (),
109 dc_logger
->log_bool_return ("ana::diagnostic_emission_context::warn",
115 /* Emit a note, using the rich_location and metadata (and the
116 pending_diagnostic's option). */
119 diagnostic_emission_context::inform (const char *gmsgid
, ...)
121 auto dc_logger
= global_dc
->get_logger ();
122 diagnostics::logging::log_function_params
123 (dc_logger
, "ana::diagnostic_emission_context::inform")
124 .log_param_string ("gmsgid", gmsgid
);
125 diagnostics::logging::auto_inc_depth
depth_sentinel (dc_logger
);
127 const pending_diagnostic
&pd
= get_pending_diagnostic ();
128 auto_diagnostic_group d
;
130 va_start (ap
, gmsgid
);
131 emit_diagnostic_valist_meta (diagnostics::kind::note
,
132 &m_rich_loc
, &m_metadata
,
133 pd
.get_controlling_option (),
138 /* Return true if T1 and T2 are "the same" for the purposes of
139 diagnostic deduplication. */
142 pending_diagnostic::same_tree_p (tree t1
, tree t2
)
144 return simple_cst_equal (t1
, t2
) == 1;
147 /* Return true iff IDENT is STR. */
150 ht_ident_eq (ht_identifier ident
, const char *str
)
152 return (strlen (str
) == ident
.len
153 && 0 == strcmp (str
, (const char *)ident
.str
));
156 /* Return true if we should show the expansion location rather than unwind
160 fixup_location_in_macro_p (cpp_hashnode
*macro
)
162 ht_identifier ident
= macro
->ident
;
164 /* Don't unwind inside "alloca" macro, so that we don't suppress warnings
165 from it (due to being in system headers). */
166 if (ht_ident_eq (ident
, "alloca"))
169 /* Don't unwind inside <stdarg.h> macros, so that we don't suppress warnings
170 from them (due to being in system headers). */
171 if (ht_ident_eq (ident
, "va_start")
172 || ht_ident_eq (ident
, "va_copy")
173 || ht_ident_eq (ident
, "va_arg")
174 || ht_ident_eq (ident
, "va_end"))
179 /* Base implementation of pending_diagnostic::fixup_location.
180 Don't unwind inside macros for which fixup_location_in_macro_p is true. */
183 pending_diagnostic::fixup_location (location_t loc
, bool) const
185 if (linemap_location_from_macro_expansion_p (line_table
, loc
))
188 = const_cast <line_map
*> (linemap_lookup (line_table
, loc
));
189 const line_map_macro
*macro_map
= linemap_check_macro (map
);
190 if (fixup_location_in_macro_p (macro_map
->macro
))
191 loc
= linemap_resolve_location (line_table
, loc
,
192 LRK_MACRO_EXPANSION_POINT
, nullptr);
197 /* Base implementation of pending_diagnostic::add_function_entry_event.
198 Add a function_entry_event to EMISSION_PATH. */
201 pending_diagnostic::add_function_entry_event (const exploded_edge
&eedge
,
202 checker_path
*emission_path
)
204 const exploded_node
*dst_node
= eedge
.m_dest
;
205 const program_point
&dst_point
= dst_node
->get_point ();
206 const program_state
&dst_state
= dst_node
->get_state ();
207 emission_path
->add_event
208 (std::make_unique
<function_entry_event
> (dst_point
,
212 /* Base implementation of pending_diagnostic::add_call_event.
213 Add a call_event to EMISSION_PATH. */
216 pending_diagnostic::add_call_event (const exploded_edge
&eedge
,
217 checker_path
*emission_path
)
219 const exploded_node
*src_node
= eedge
.m_src
;
220 const program_point
&src_point
= src_node
->get_point ();
221 const int src_stack_depth
= src_point
.get_stack_depth ();
222 const gimple
*last_stmt
= src_point
.get_supernode ()->get_last_stmt ();
223 emission_path
->add_event
224 (std::make_unique
<call_event
> (eedge
,
225 event_loc_info (last_stmt
226 ? last_stmt
->location
228 src_point
.get_fndecl (),
232 /* Base implementation of pending_diagnostic::add_region_creation_events.
233 See the comment for class region_creation_event. */
236 pending_diagnostic::add_region_creation_events (const region
*reg
,
238 const event_loc_info
&loc_info
,
239 checker_path
&emission_path
)
241 emission_path
.add_event
242 (std::make_unique
<region_creation_event_memory_space
>
243 (reg
->get_memory_space (),
247 emission_path
.add_event
248 (std::make_unique
<region_creation_event_capacity
> (capacity
, loc_info
));
251 /* Base implementation of pending_diagnostic::add_final_event.
252 Add a warning_event to the end of EMISSION_PATH. */
255 pending_diagnostic::add_final_event (const state_machine
*sm
,
256 const exploded_node
*enode
,
257 const event_loc_info
&loc_info
,
258 tree var
, state_machine::state_t state
,
259 checker_path
*emission_path
)
261 emission_path
->add_event
262 (std::make_unique
<warning_event
>
266 get_final_state ()));
271 #endif /* #if ENABLE_ANALYZER */