Line data Source code
1 : /* Classes for representing locations within the program.
2 : Copyright (C) 2019-2026 Free Software Foundation, Inc.
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 "analyzer/common.h"
22 :
23 : #include "diagnostics/event-id.h"
24 : #include "gcc-rich-location.h"
25 : #include "gimple-pretty-print.h"
26 : #include "sbitmap.h"
27 : #include "selftest.h"
28 : #include "shortest-paths.h"
29 :
30 : #include "analyzer/analyzer-logging.h"
31 : #include "analyzer/call-string.h"
32 : #include "analyzer/supergraph.h"
33 : #include "analyzer/program-point.h"
34 : #include "analyzer/store.h"
35 : #include "analyzer/region-model.h"
36 : #include "analyzer/sm.h"
37 : #include "analyzer/program-state.h"
38 : #include "analyzer/pending-diagnostic.h"
39 : #include "analyzer/diagnostic-manager.h"
40 : #include "analyzer/exploded-graph.h"
41 : #include "analyzer/analysis-plan.h"
42 : #include "analyzer/inlining-iterator.h"
43 :
44 : #if ENABLE_ANALYZER
45 :
46 : namespace ana {
47 :
48 : /* A subclass of diagnostics::context for use by
49 : program_point::print_source_line. */
50 :
51 : class debug_diagnostic_context : public diagnostics::context
52 : {
53 : public:
54 647 : debug_diagnostic_context ()
55 647 : {
56 647 : diagnostic_initialize (this, 0);
57 647 : auto &source_printing_opts = get_source_printing_options ();
58 647 : source_printing_opts.show_line_numbers_p = true;
59 647 : source_printing_opts.enabled = true;
60 647 : }
61 647 : ~debug_diagnostic_context ()
62 : {
63 1294 : diagnostic_finish (this);
64 647 : }
65 : };
66 :
67 : /* class program_point. */
68 :
69 : /* Print the source line (if any) for this program_point to PP. */
70 :
71 : void
72 647 : program_point::print_source_line (pretty_printer *pp) const
73 : {
74 1294 : if (!useful_location_p (get_location ()))
75 0 : return;
76 647 : debug_diagnostic_context tmp_dc;
77 1294 : gcc_rich_location richloc (get_location ());
78 647 : diagnostics::source_print_policy source_policy (tmp_dc);
79 647 : gcc_assert (pp);
80 647 : source_policy.print (*pp, richloc, diagnostics::kind::error, nullptr);
81 647 : pp_string (pp, pp_formatted_text (tmp_dc.get_reference_printer ()));
82 1294 : }
83 :
84 : /* Print this program_point to PP. */
85 :
86 : void
87 2218 : program_point::print (pretty_printer *pp, const format &f) const
88 : {
89 2218 : pp_string (pp, "callstring: ");
90 2218 : m_call_string->print (pp);
91 2218 : f.spacer (pp);
92 :
93 2218 : if (m_snode)
94 : {
95 2179 : pp_printf (pp, "sn: %i", m_snode->m_id);
96 2179 : if (f.m_newlines)
97 : {
98 647 : pp_newline (pp);
99 647 : print_source_line (pp);
100 : }
101 : }
102 : else
103 39 : pp_string (pp, "origin");
104 2218 : }
105 :
106 : /* Dump this point to stderr. */
107 :
108 : DEBUG_FUNCTION void
109 0 : program_point::dump () const
110 : {
111 0 : symbolic::dump_node_tree_to_stderr<program_point> (*this);
112 0 : }
113 :
114 : std::unique_ptr<text_art::tree_widget>
115 0 : program_point::make_dump_widget (symbolic::node_dump_state &nds) const
116 : {
117 0 : using text_art::tree_widget;
118 :
119 0 : pretty_printer pp;
120 0 : pp_string (&pp, "program_point: ");
121 0 : if (m_snode)
122 0 : pp_printf (&pp, "sn: %i", m_snode->m_id);
123 : else
124 0 : pp_string (&pp, "origin");
125 :
126 0 : auto point_widget = tree_widget::make (nds, &pp);
127 :
128 0 : if (!m_call_string->empty_p ())
129 0 : point_widget->add_child (m_call_string->make_dump_widget (nds));
130 :
131 : /* Add child quoting source text, if possible. */
132 0 : {
133 0 : pretty_printer pp;
134 0 : print_source_line (&pp);
135 0 : if (strlen (pp_formatted_text (&pp)) > 0)
136 0 : point_widget->add_child (tree_widget::make (nds, &pp));
137 0 : }
138 :
139 0 : return point_widget;
140 0 : }
141 :
142 : /* Return a new json::object of the form
143 : {"snode_idx" : int (optional), the index of the supernode,
144 : "call_string": object for the call_string}. */
145 :
146 : std::unique_ptr<json::object>
147 0 : program_point::to_json () const
148 : {
149 0 : auto point_obj = std::make_unique<json::object> ();
150 :
151 0 : if (get_supernode ())
152 0 : point_obj->set_integer ("snode_idx", get_supernode ()->m_id);
153 0 : point_obj->set ("call_string", m_call_string->to_json ());
154 :
155 0 : return point_obj;
156 : }
157 :
158 : /* Pop the topmost call from the current callstack. */
159 : void
160 1671 : program_point::pop_from_call_stack ()
161 : {
162 1671 : m_call_string = m_call_string->get_parent ();
163 1671 : gcc_assert (m_call_string);
164 1671 : }
165 :
166 : /* Generate a hash value for this program_point. */
167 :
168 : hashval_t
169 4731308 : program_point::hash () const
170 : {
171 4731308 : inchash::hash hstate;
172 4731308 : hstate.add_ptr (m_snode);
173 4731308 : hstate.add_ptr (m_call_string);
174 4731308 : return hstate.end ();
175 : }
176 :
177 : /* Get the function * at DEPTH within the call stack. */
178 :
179 : function *
180 1192937 : program_point::get_function_at_depth (unsigned depth) const
181 : {
182 1881096 : gcc_assert (depth <= m_call_string->length ());
183 1192937 : if (depth == m_call_string->length ())
184 740218 : return m_snode->get_function ();
185 : else
186 452719 : return get_call_string ()[depth].get_caller_function ();
187 : }
188 :
189 : /* Assert that this object is sane. */
190 :
191 : void
192 747360 : program_point::validate () const
193 : {
194 : /* Skip this in a release build. */
195 : #if !CHECKING_P
196 : return;
197 : #endif
198 :
199 747360 : m_call_string->validate ();
200 : /* The "callee" of the final entry in the callstring should be the
201 : function of the m_function_point. */
202 747360 : if (m_call_string->length () > 0)
203 470880 : gcc_assert
204 : ((*m_call_string)[m_call_string->length () - 1].get_callee_function ()
205 : == get_function ());
206 747360 : }
207 :
208 : /* class program_point. */
209 :
210 : program_point
211 3625 : program_point::origin (const region_model_manager &mgr)
212 : {
213 3625 : return program_point (nullptr,
214 3625 : mgr.get_empty_call_string ());
215 : }
216 :
217 : program_point
218 11530 : program_point::from_function_entry (const region_model_manager &mgr,
219 : const supergraph &sg,
220 : const function &fun)
221 : {
222 11530 : return program_point (sg.get_node_for_function_entry (fun),
223 11530 : mgr.get_empty_call_string ());
224 : }
225 :
226 : /* Return true iff POINT_A and POINT_B share the same function and
227 : call_string, both directly, and when attempting to undo inlining
228 : information. */
229 :
230 : bool
231 125 : program_point::effectively_intraprocedural_p (const program_point &point_a,
232 : const program_point &point_b)
233 : {
234 : /* First, compare without considering inlining info. */
235 250 : if (point_a.get_function ()
236 125 : != point_b.get_function ())
237 : return false;
238 125 : if (&point_a.get_call_string ()
239 125 : != &point_b.get_call_string ())
240 : return false;
241 :
242 : /* Consider inlining info; they must have originally come from
243 : the same function and have been inlined in the same way. */
244 125 : location_t loc_a = point_a.get_location ();
245 125 : location_t loc_b = point_b.get_location ();
246 125 : inlining_iterator iter_a (loc_a);
247 125 : inlining_iterator iter_b (loc_b);
248 361 : while (!(iter_a.done_p () || iter_b.done_p ()))
249 : {
250 125 : if (iter_a.done_p () || iter_b.done_p ())
251 : return false;
252 :
253 125 : if (iter_a.get_fndecl () != iter_b.get_fndecl ())
254 : return false;
255 125 : if (iter_a.get_callsite () != iter_b.get_callsite ())
256 : return false;
257 125 : if (iter_a.get_block () != iter_b.get_block ())
258 : return false;
259 :
260 111 : iter_a.next ();
261 111 : iter_b.next ();
262 : }
263 :
264 : return true;
265 : }
266 :
267 : #if CHECKING_P
268 :
269 : namespace selftest {
270 :
271 : /* Verify that program_point::operator== works as expected. */
272 :
273 : static void
274 4 : test_program_point_equality ()
275 : {
276 4 : symbolic::node_manager node_mgr;
277 4 : auto &mgr = node_mgr.get_region_model_manager ();
278 :
279 4 : const supernode *snode = nullptr;
280 :
281 4 : const call_string &cs = mgr.get_empty_call_string ();
282 :
283 4 : program_point a = program_point (snode, cs);
284 4 : program_point b = program_point (snode, cs);
285 4 : ASSERT_EQ (a, b);
286 : // TODO: verify with non-empty callstrings, with different snodes
287 4 : }
288 :
289 : /* Run all of the selftests within this file. */
290 :
291 : void
292 4 : analyzer_program_point_cc_tests ()
293 : {
294 4 : test_program_point_equality ();
295 4 : }
296 :
297 : } // namespace selftest
298 :
299 : #endif /* CHECKING_P */
300 :
301 : } // namespace ana
302 :
303 : #endif /* #if ENABLE_ANALYZER */
|