Update baseline symbols for hppa-linux.
[official-gcc.git] / gcc / analyzer / analyzer.cc
blob94c5cf242b2fd3a7b1cfae037c8a4c01b2e02c98
1 /* Utility functions for the analyzer.
2 Copyright (C) 2019-2023 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)
10 any later version.
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 "config.h"
22 #define INCLUDE_MEMORY
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tree.h"
26 #include "function.h"
27 #include "basic-block.h"
28 #include "gimple.h"
29 #include "diagnostic.h"
30 #include "intl.h"
31 #include "analyzer/analyzer.h"
33 #if ENABLE_ANALYZER
35 namespace ana {
37 /* Workaround for missing location information for some stmts,
38 which ultimately should be solved by fixing the frontends
39 to provide the locations (TODO). */
41 location_t
42 get_stmt_location (const gimple *stmt, function *fun)
44 if (!stmt)
45 return UNKNOWN_LOCATION;
46 if (get_pure_location (stmt->location) == UNKNOWN_LOCATION)
48 /* Workaround for missing location information for clobber
49 stmts, which seem to lack location information in the C frontend
50 at least. Created by gimplify_bind_expr, which uses the
51 BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr))
52 but this is never set up when the block is created in
53 c_end_compound_stmt's pop_scope.
54 TODO: fix this missing location information.
56 For now, as a hackish workaround, use the location of the end of
57 the function. */
58 if (gimple_clobber_p (stmt) && fun)
59 return fun->function_end_locus;
62 return stmt->location;
65 static tree
66 fixup_tree_for_diagnostic_1 (tree expr, hash_set<tree> *visited);
68 /* Attemp to generate a tree for the LHS of ASSIGN_STMT.
69 VISITED must be non-NULL; it is used to ensure termination. */
71 static tree
72 get_diagnostic_tree_for_gassign_1 (const gassign *assign_stmt,
73 hash_set<tree> *visited)
75 enum tree_code code = gimple_assign_rhs_code (assign_stmt);
77 /* Reverse the effect of extract_ops_from_tree during
78 gimplification. */
79 switch (get_gimple_rhs_class (code))
81 default:
82 case GIMPLE_INVALID_RHS:
83 gcc_unreachable ();
84 case GIMPLE_TERNARY_RHS:
85 case GIMPLE_BINARY_RHS:
86 case GIMPLE_UNARY_RHS:
88 tree t = make_node (code);
89 TREE_TYPE (t) = TREE_TYPE (gimple_assign_lhs (assign_stmt));
90 unsigned num_rhs_args = gimple_num_ops (assign_stmt) - 1;
91 for (unsigned i = 0; i < num_rhs_args; i++)
93 tree op = gimple_op (assign_stmt, i + 1);
94 if (op)
96 op = fixup_tree_for_diagnostic_1 (op, visited);
97 if (op == NULL_TREE)
98 return NULL_TREE;
100 TREE_OPERAND (t, i) = op;
102 return t;
104 case GIMPLE_SINGLE_RHS:
106 tree op = gimple_op (assign_stmt, 1);
107 op = fixup_tree_for_diagnostic_1 (op, visited);
108 return op;
113 /* Subroutine of fixup_tree_for_diagnostic_1, called on SSA names.
114 Attempt to reconstruct a tree expression for SSA_NAME
115 based on its def-stmt.
116 SSA_NAME must be non-NULL.
117 VISITED must be non-NULL; it is used to ensure termination.
119 Return NULL_TREE if there is a problem. */
121 static tree
122 maybe_reconstruct_from_def_stmt (tree ssa_name,
123 hash_set<tree> *visited)
125 /* Ensure termination. */
126 if (visited->contains (ssa_name))
127 return NULL_TREE;
128 visited->add (ssa_name);
130 gimple *def_stmt = SSA_NAME_DEF_STMT (ssa_name);
132 switch (gimple_code (def_stmt))
134 default:
135 gcc_unreachable ();
136 case GIMPLE_ASM:
137 case GIMPLE_NOP:
138 case GIMPLE_PHI:
139 /* Can't handle these. */
140 return NULL_TREE;
141 case GIMPLE_ASSIGN:
142 return get_diagnostic_tree_for_gassign_1
143 (as_a <const gassign *> (def_stmt), visited);
144 case GIMPLE_CALL:
146 gcall *call_stmt = as_a <gcall *> (def_stmt);
147 tree return_type = gimple_call_return_type (call_stmt);
148 tree fn = fixup_tree_for_diagnostic_1 (gimple_call_fn (call_stmt),
149 visited);
150 if (fn == NULL_TREE)
151 return NULL_TREE;
152 unsigned num_args = gimple_call_num_args (call_stmt);
153 auto_vec<tree> args (num_args);
154 for (unsigned i = 0; i < num_args; i++)
156 tree arg = gimple_call_arg (call_stmt, i);
157 arg = fixup_tree_for_diagnostic_1 (arg, visited);
158 if (arg == NULL_TREE)
159 return NULL_TREE;
160 args.quick_push (arg);
162 gcc_assert (fn);
163 return build_call_array_loc (gimple_location (call_stmt),
164 return_type, fn,
165 num_args, args.address ());
167 break;
171 /* Subroutine of fixup_tree_for_diagnostic: attempt to fixup EXPR,
172 which can be NULL.
173 VISITED must be non-NULL; it is used to ensure termination. */
175 static tree
176 fixup_tree_for_diagnostic_1 (tree expr, hash_set<tree> *visited)
178 if (expr
179 && TREE_CODE (expr) == SSA_NAME
180 && (SSA_NAME_VAR (expr) == NULL_TREE
181 || DECL_ARTIFICIAL (SSA_NAME_VAR (expr))))
183 if (tree var = SSA_NAME_VAR (expr))
184 if (VAR_P (var) && DECL_HAS_DEBUG_EXPR_P (var))
185 return DECL_DEBUG_EXPR (var);
186 if (tree expr2 = maybe_reconstruct_from_def_stmt (expr, visited))
187 return expr2;
189 return expr;
192 /* We don't want to print '<unknown>' in our diagnostics (PR analyzer/99771),
193 but sometimes we generate diagnostics involving an ssa name for a
194 temporary.
196 Work around this by attempting to reconstruct a tree expression for
197 such temporaries based on their def-stmts.
199 Otherwise return EXPR.
201 EXPR can be NULL. */
203 tree
204 fixup_tree_for_diagnostic (tree expr)
206 hash_set<tree> visited;
207 return fixup_tree_for_diagnostic_1 (expr, &visited);
210 /* Attempt to generate a tree for the LHS of ASSIGN_STMT. */
212 tree
213 get_diagnostic_tree_for_gassign (const gassign *assign_stmt)
215 hash_set<tree> visited;
216 return get_diagnostic_tree_for_gassign_1 (assign_stmt, &visited);
219 } // namespace ana
221 /* Helper function for checkers. Is the CALL to the given function name,
222 and with the given number of arguments?
224 This doesn't resolve function pointers via the region model;
225 is_named_call_p should be used instead, using a fndecl from
226 get_fndecl_for_call; this function should only be used for special cases
227 where it's not practical to get at the region model, or for special
228 analyzer functions such as __analyzer_dump. */
230 bool
231 is_special_named_call_p (const gcall *call, const char *funcname,
232 unsigned int num_args)
234 gcc_assert (funcname);
236 tree fndecl = gimple_call_fndecl (call);
237 if (!fndecl)
238 return false;
240 return is_named_call_p (fndecl, funcname, call, num_args);
243 /* Helper function for checkers. Is FNDECL an extern fndecl at file scope
244 that has the given FUNCNAME?
246 Compare with special_function_p in calls.cc. */
248 bool
249 is_named_call_p (const_tree fndecl, const char *funcname)
251 gcc_assert (fndecl);
252 gcc_assert (funcname);
254 if (!maybe_special_function_p (fndecl))
255 return false;
257 tree identifier = DECL_NAME (fndecl);
258 const char *name = IDENTIFIER_POINTER (identifier);
259 const char *tname = name;
261 /* Potentially disregard prefix _ or __ in FNDECL's name, but not if
262 FUNCNAME itself has leading underscores (e.g. when looking for
263 "__analyzer_eval"). */
264 if (funcname[0] != '_' && name[0] == '_')
266 if (name[1] == '_')
267 tname += 2;
268 else
269 tname += 1;
272 return 0 == strcmp (tname, funcname);
275 /* Return true if FNDECL is within the namespace "std".
276 Compare with cp/typeck.cc: decl_in_std_namespace_p, but this doesn't
277 rely on being the C++ FE (or handle inline namespaces inside of std). */
279 static inline bool
280 is_std_function_p (const_tree fndecl)
282 tree name_decl = DECL_NAME (fndecl);
283 if (!name_decl)
284 return false;
285 if (!DECL_CONTEXT (fndecl))
286 return false;
287 if (TREE_CODE (DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
288 return false;
289 tree ns = DECL_CONTEXT (fndecl);
290 if (!(DECL_CONTEXT (ns) == NULL_TREE
291 || TREE_CODE (DECL_CONTEXT (ns)) == TRANSLATION_UNIT_DECL))
292 return false;
293 if (!DECL_NAME (ns))
294 return false;
295 return id_equal ("std", DECL_NAME (ns));
298 /* Like is_named_call_p, but look for std::FUNCNAME. */
300 bool
301 is_std_named_call_p (const_tree fndecl, const char *funcname)
303 gcc_assert (fndecl);
304 gcc_assert (funcname);
306 if (!is_std_function_p (fndecl))
307 return false;
309 tree identifier = DECL_NAME (fndecl);
310 const char *name = IDENTIFIER_POINTER (identifier);
311 const char *tname = name;
313 /* Don't disregard prefix _ or __ in FNDECL's name. */
315 return 0 == strcmp (tname, funcname);
318 /* Helper function for checkers. Is FNDECL an extern fndecl at file scope
319 that has the given FUNCNAME, and does CALL have the given number of
320 arguments? */
322 bool
323 is_named_call_p (const_tree fndecl, const char *funcname,
324 const gcall *call, unsigned int num_args)
326 gcc_assert (fndecl);
327 gcc_assert (funcname);
329 if (!is_named_call_p (fndecl, funcname))
330 return false;
332 if (gimple_call_num_args (call) != num_args)
333 return false;
335 return true;
338 /* Like is_named_call_p, but check for std::FUNCNAME. */
340 bool
341 is_std_named_call_p (const_tree fndecl, const char *funcname,
342 const gcall *call, unsigned int num_args)
344 gcc_assert (fndecl);
345 gcc_assert (funcname);
347 if (!is_std_named_call_p (fndecl, funcname))
348 return false;
350 if (gimple_call_num_args (call) != num_args)
351 return false;
353 return true;
356 /* Return true if stmt is a setjmp or sigsetjmp call. */
358 bool
359 is_setjmp_call_p (const gcall *call)
361 if (is_special_named_call_p (call, "setjmp", 1)
362 || is_special_named_call_p (call, "sigsetjmp", 2))
363 /* region_model::on_setjmp requires a pointer. */
364 if (POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
365 return true;
367 return false;
370 /* Return true if stmt is a longjmp or siglongjmp call. */
372 bool
373 is_longjmp_call_p (const gcall *call)
375 if (is_special_named_call_p (call, "longjmp", 2)
376 || is_special_named_call_p (call, "siglongjmp", 2))
377 /* exploded_node::on_longjmp requires a pointer for the initial
378 argument. */
379 if (POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
380 return true;
382 return false;
385 /* For a CALL that matched is_special_named_call_p or is_named_call_p for
386 some name, return a name for the called function suitable for use in
387 diagnostics (stripping the leading underscores). */
389 const char *
390 get_user_facing_name (const gcall *call)
392 tree fndecl = gimple_call_fndecl (call);
393 gcc_assert (fndecl);
395 tree identifier = DECL_NAME (fndecl);
396 gcc_assert (identifier);
398 const char *name = IDENTIFIER_POINTER (identifier);
400 /* Strip prefix _ or __ in FNDECL's name. */
401 if (name[0] == '_')
403 if (name[1] == '_')
404 return name + 2;
405 else
406 return name + 1;
409 return name;
412 /* Generate a label_text instance by formatting FMT, using a
413 temporary clone of the global_dc's printer (thus using its
414 formatting callbacks).
416 Colorize if the global_dc supports colorization and CAN_COLORIZE is
417 true. */
419 label_text
420 make_label_text (bool can_colorize, const char *fmt, ...)
422 pretty_printer *pp = global_dc->printer->clone ();
423 pp_clear_output_area (pp);
425 if (!can_colorize)
426 pp_show_color (pp) = false;
428 text_info ti;
429 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
431 va_list ap;
433 va_start (ap, fmt);
435 ti.format_spec = _(fmt);
436 ti.args_ptr = &ap;
437 ti.err_no = 0;
438 ti.x_data = NULL;
439 ti.m_richloc = &rich_loc;
441 pp_format (pp, &ti);
442 pp_output_formatted_text (pp);
444 va_end (ap);
446 label_text result = label_text::take (xstrdup (pp_formatted_text (pp)));
447 delete pp;
448 return result;
451 /* As above, but with singular vs plural. */
453 label_text
454 make_label_text_n (bool can_colorize, unsigned HOST_WIDE_INT n,
455 const char *singular_fmt,
456 const char *plural_fmt, ...)
458 pretty_printer *pp = global_dc->printer->clone ();
459 pp_clear_output_area (pp);
461 if (!can_colorize)
462 pp_show_color (pp) = false;
464 text_info ti;
465 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
467 va_list ap;
469 va_start (ap, plural_fmt);
471 const char *fmt = ngettext (singular_fmt, plural_fmt, n);
473 ti.format_spec = fmt;
474 ti.args_ptr = &ap;
475 ti.err_no = 0;
476 ti.x_data = NULL;
477 ti.m_richloc = &rich_loc;
479 pp_format (pp, &ti);
480 pp_output_formatted_text (pp);
482 va_end (ap);
484 label_text result = label_text::take (xstrdup (pp_formatted_text (pp)));
485 delete pp;
486 return result;
489 #endif /* #if ENABLE_ANALYZER */