Line data Source code
1 : /* Subclasses of custom_edge_info for describing outcomes of function calls.
2 : Copyright (C) 2021-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 "ordered-hash-map.h"
24 : #include "cfg.h"
25 : #include "digraph.h"
26 : #include "sbitmap.h"
27 : #include "diagnostics/event-id.h"
28 :
29 : #include "analyzer/analyzer-logging.h"
30 : #include "analyzer/supergraph.h"
31 : #include "analyzer/call-string.h"
32 : #include "analyzer/program-point.h"
33 : #include "analyzer/store.h"
34 : #include "analyzer/region-model.h"
35 : #include "analyzer/constraint-manager.h"
36 : #include "analyzer/sm.h"
37 : #include "analyzer/pending-diagnostic.h"
38 : #include "analyzer/region-model-reachability.h"
39 : #include "analyzer/analyzer-selftests.h"
40 : #include "analyzer/program-state.h"
41 : #include "analyzer/checker-path.h"
42 : #include "analyzer/diagnostic-manager.h"
43 : #include "analyzer/exploded-graph.h"
44 : #include "analyzer/call-details.h"
45 : #include "analyzer/call-info.h"
46 : #include "analyzer/state-transition.h"
47 :
48 : #if ENABLE_ANALYZER
49 :
50 : namespace ana {
51 :
52 : /* class custom_edge_info. */
53 :
54 : bool
55 8453 : custom_edge_info::update_state (program_state *state,
56 : const exploded_edge *eedge,
57 : region_model_context *ctxt) const
58 : {
59 8453 : return update_model (state->m_region_model, eedge, ctxt);
60 : }
61 :
62 : void
63 6 : custom_edge_info::get_dot_attrs (const char *&out_style,
64 : const char *&out_color) const
65 : {
66 6 : out_color = "red";
67 6 : out_style = "\"dotted\"";
68 6 : }
69 :
70 : /* Base implementation of custom_edge_info::create_enode vfunc. */
71 :
72 : exploded_node *
73 2734 : custom_edge_info::create_enode (exploded_graph &eg,
74 : const program_point &point,
75 : program_state &&state,
76 : exploded_node *enode_for_diag,
77 : region_model_context *) const
78 : {
79 2734 : return eg.get_or_create_node (point, state, enode_for_diag);
80 : }
81 :
82 : /* class call_info : public custom_edge_info. */
83 :
84 : /* Implementation of custom_edge_info::print vfunc for call_info. */
85 :
86 : void
87 0 : call_info::print (pretty_printer *pp) const
88 : {
89 0 : gcc_assert (pp);
90 0 : print_desc (*pp);
91 0 : }
92 :
93 : /* Implementation of custom_edge_info::add_events_to_path vfunc for
94 : call_info: add a custom_event using call_info::get_desc as its
95 : description. */
96 :
97 : void
98 364 : call_info::add_events_to_path (checker_path *emission_path,
99 : const exploded_edge &eedge,
100 : pending_diagnostic &,
101 : const state_transition *) const
102 : {
103 0 : class call_event : public custom_event
104 : {
105 : public:
106 364 : call_event (const event_loc_info &loc_info,
107 : const call_info *call_info)
108 364 : : custom_event (loc_info),
109 364 : m_call_info (call_info)
110 : {}
111 :
112 711 : void print_desc (pretty_printer &pp) const final override
113 : {
114 711 : m_call_info->print_desc (pp);
115 711 : }
116 :
117 : private:
118 : const call_info *m_call_info;
119 : };
120 :
121 364 : const exploded_node *src_node = eedge.m_src;
122 364 : const program_point &src_point = src_node->get_point ();
123 364 : tree caller_fndecl = src_point.get_fndecl ();
124 364 : const int stack_depth = src_point.get_stack_depth ();
125 :
126 : /* TODO: we don't yet make use of the state_transition (if any), as
127 : doing so presumably requires a combinatorial explosion of
128 : known functions vs pending diagnostics. */
129 :
130 364 : emission_path->add_event
131 364 : (std::make_unique<call_event> (event_loc_info (get_call_stmt ().location,
132 : caller_fndecl,
133 364 : stack_depth),
134 364 : this));
135 364 : }
136 :
137 : /* Recreate a call_details instance from this call_info. */
138 :
139 : call_details
140 5527 : call_info::get_call_details (region_model *model,
141 : region_model_context *ctxt) const
142 : {
143 5527 : return call_details (m_call_stmt, model, ctxt);
144 : }
145 :
146 : /* call_info's ctor.
147 :
148 : The call_info instance will outlive the call_details instance;
149 : call_details instances are typically created on the stack. */
150 :
151 3401 : call_info::call_info (const call_details &cd)
152 3401 : : m_call_stmt (cd.get_call_stmt ()),
153 3401 : m_fndecl (cd.get_fndecl_for_call ())
154 : {
155 3401 : gcc_assert (m_fndecl);
156 3401 : }
157 :
158 1414 : call_info::call_info (const call_details &cd,
159 1414 : const function &called_fn)
160 1414 : : m_call_stmt (cd.get_call_stmt ()),
161 1414 : m_fndecl (called_fn.decl)
162 : {
163 1414 : gcc_assert (m_fndecl);
164 1414 : }
165 :
166 : /* class succeed_or_fail_call_info : public call_info. */
167 :
168 : void
169 373 : succeed_or_fail_call_info::print_desc (pretty_printer &pp) const
170 : {
171 373 : if (m_success)
172 261 : pp_printf (&pp, "when %qE succeeds", get_fndecl ());
173 : else
174 112 : pp_printf (&pp, "when %qE fails", get_fndecl ());
175 373 : }
176 :
177 : } // namespace ana
178 :
179 : #endif /* #if ENABLE_ANALYZER */
|