Line data Source code
1 : /* Support for plugin-supplied behaviors of known functions.
2 : Copyright (C) 2022-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 "diagnostic-core.h"
24 : #include "stringpool.h"
25 :
26 : #include "analyzer/analyzer-logging.h"
27 : #include "analyzer/known-function-manager.h"
28 : #include "analyzer/region-model.h"
29 : #include "analyzer/call-details.h"
30 :
31 : #if ENABLE_ANALYZER
32 :
33 : namespace ana {
34 :
35 : /* class known_function_manager : public log_user. */
36 :
37 4409 : known_function_manager::known_function_manager (logger *logger)
38 4409 : : log_user (logger)
39 : {
40 4409 : memset (m_combined_fns_arr, 0, sizeof (m_combined_fns_arr));
41 4409 : }
42 :
43 4409 : known_function_manager::~known_function_manager ()
44 : {
45 : /* Delete all owned kfs. */
46 397546 : for (auto iter : m_map_id_to_kf)
47 393137 : delete iter.second;
48 75869 : for (auto iter : m_std_ns_map_id_to_kf)
49 71460 : delete iter.second;
50 10325878 : for (auto iter : m_combined_fns_arr)
51 10321469 : delete iter;
52 4409 : }
53 :
54 : void
55 393137 : known_function_manager::add (const char *name,
56 : std::unique_ptr<known_function> kf)
57 : {
58 393137 : LOG_FUNC_1 (get_logger (), "registering %s", name);
59 393137 : tree id = get_identifier (name);
60 393137 : m_map_id_to_kf.put (id, kf.release ());
61 393137 : }
62 :
63 : void
64 71460 : known_function_manager::add_std_ns (const char *name,
65 : std::unique_ptr<known_function> kf)
66 : {
67 71460 : LOG_FUNC_1 (get_logger (), "registering std::%s", name);
68 71460 : tree id = get_identifier (name);
69 71460 : m_std_ns_map_id_to_kf.put (id, kf.release ());
70 71460 : }
71 :
72 : void
73 357300 : known_function_manager::add (enum built_in_function name,
74 : std::unique_ptr<known_function> kf)
75 : {
76 357300 : gcc_assert (name < END_BUILTINS);
77 357300 : delete m_combined_fns_arr[name];
78 357300 : m_combined_fns_arr[name] = kf.release ();
79 357300 : }
80 :
81 : void
82 17865 : known_function_manager::add (enum internal_fn ifn,
83 : std::unique_ptr<known_function> kf)
84 : {
85 17865 : gcc_assert (ifn < IFN_LAST);
86 17865 : delete m_combined_fns_arr[ifn + int (END_BUILTINS)];
87 17865 : m_combined_fns_arr[ifn + int (END_BUILTINS)] = kf.release ();
88 17865 : }
89 :
90 : /* Get any known_function for FNDECL for call CD.
91 :
92 : The call must match all assumptions made by the known_function (such as
93 : e.g. "argument 1's type must be a pointer type").
94 :
95 : Return nullptr if no known_function is found, or it does not match the
96 : assumption(s). */
97 :
98 : const known_function *
99 352205 : known_function_manager::get_match (tree fndecl, const call_details &cd) const
100 : {
101 : /* Look for a matching built-in. */
102 352205 : if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
103 : {
104 319232 : if (const known_function *candidate
105 159616 : = get_normal_builtin (DECL_FUNCTION_CODE (fndecl)))
106 19211 : if (gimple_builtin_call_types_compatible_p (&cd.get_call_stmt (),
107 : fndecl))
108 : return candidate;
109 : }
110 :
111 : /* Look for a match by name. */
112 :
113 332994 : if (is_std_function_p (fndecl))
114 : {
115 1285 : if (tree identifier = DECL_NAME (fndecl))
116 2570 : if (const known_function *candidate
117 1285 : = get_by_identifier_in_std_ns (identifier))
118 120 : if (candidate->matches_call_types_p (cd))
119 : return candidate;
120 1165 : return nullptr;
121 : }
122 :
123 : /* Only match functions declared at global scope, or within namespace
124 : __cxxabiv1 (e.g. __dynamic_cast). */
125 331709 : if (!is_cxxabi_function_p (fndecl))
126 331046 : if (DECL_CONTEXT (fndecl)
127 331046 : && TREE_CODE (DECL_CONTEXT (fndecl)) != TRANSLATION_UNIT_DECL)
128 : return nullptr;
129 :
130 325853 : if (tree identifier = DECL_NAME (fndecl))
131 325853 : if (const known_function *candidate = get_by_identifier (identifier))
132 220683 : if (candidate->matches_call_types_p (cd))
133 220084 : return candidate;
134 :
135 : return nullptr;
136 : }
137 :
138 : /* Get any known_function for IFN, or nullptr. */
139 :
140 : const known_function *
141 4941 : known_function_manager::get_internal_fn (enum internal_fn ifn) const
142 : {
143 4941 : gcc_assert (ifn < IFN_LAST);
144 4941 : return m_combined_fns_arr[ifn + int (END_BUILTINS)];
145 : }
146 :
147 : /* Get any known_function for NAME, without type-checking.
148 : Return nullptr if there isn't one. */
149 :
150 : const known_function *
151 159616 : known_function_manager::get_normal_builtin (enum built_in_function name) const
152 : {
153 : /* The numbers for built-in functions in enum combined_fn are the same as
154 : for the built_in_function enum. */
155 159616 : gcc_assert (name < END_BUILTINS);
156 159616 : return m_combined_fns_arr[name];
157 : }
158 :
159 : const known_function *
160 0 : known_function_manager::
161 : get_normal_builtin (const builtin_known_function *builtin_kf) const
162 : {
163 0 : return get_normal_builtin (builtin_kf->builtin_code ());
164 : }
165 :
166 : /* Get any known_function matching IDENTIFIER, without type-checking.
167 : Return nullptr if there isn't one. */
168 :
169 : const known_function *
170 325853 : known_function_manager::get_by_identifier (tree identifier) const
171 : {
172 325853 : known_function_manager *mut_this = const_cast<known_function_manager *>(this);
173 325853 : known_function **slot = mut_this->m_map_id_to_kf.get (identifier);
174 325853 : if (slot)
175 220683 : return *slot;
176 : else
177 : return nullptr;
178 : }
179 :
180 : /* Get any known_function in C++ std:: namespace matching IDENTIFIER, without
181 : type-checking.
182 : Return nullptr if there isn't one. */
183 :
184 : const known_function *
185 1285 : known_function_manager::get_by_identifier_in_std_ns (tree identifier) const
186 : {
187 1285 : known_function_manager *mut_this = const_cast<known_function_manager *>(this);
188 1285 : known_function **slot = mut_this->m_std_ns_map_id_to_kf.get (identifier);
189 1285 : if (slot)
190 120 : return *slot;
191 : else
192 : return nullptr;
193 : }
194 :
195 : static std::unique_ptr<json::array>
196 2 : get_keys_as_json (const hash_map<tree, known_function *> &map)
197 : {
198 2 : std::vector<std::string> vec_keys;
199 132 : for (const auto &iter : map)
200 130 : vec_keys.push_back (IDENTIFIER_POINTER (iter.first));
201 :
202 2 : std::sort (vec_keys.begin (), vec_keys.end ());
203 :
204 2 : auto arr = std::make_unique<json::array> ();
205 132 : for (auto iter : vec_keys)
206 130 : arr->append_string (iter.c_str ());
207 4 : return arr;
208 2 : }
209 :
210 : std::unique_ptr<json::object>
211 1 : known_function_manager::to_json () const
212 : {
213 1 : auto obj = std::make_unique<json::object> ();
214 :
215 1 : obj->set ("root_ns", get_keys_as_json (m_map_id_to_kf));
216 1 : obj->set ("std_ns", get_keys_as_json (m_std_ns_map_id_to_kf));
217 :
218 : // Built-in functions
219 : // TODO: needs bulletproofing of combined_fn_name
220 1 : if (0)
221 : {
222 : std::vector<std::string> vec_keys;
223 : for (int i = 0; i < CFN_LAST; ++i)
224 : if (m_combined_fns_arr[i])
225 : vec_keys.push_back
226 : (combined_fn_name (static_cast<enum combined_fn> (i)));
227 :
228 : std::sort (vec_keys.begin (), vec_keys.end ());
229 :
230 : auto arr = std::make_unique<json::array> ();
231 : for (auto iter : vec_keys)
232 : arr->append_string (iter.c_str ());
233 : obj->set ("builtin_fns", std::move (arr));
234 : }
235 :
236 1 : return obj;
237 : }
238 :
239 :
240 : } // namespace ana
241 :
242 : #endif /* #if ENABLE_ANALYZER */
|