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)
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/>. */
22 #define INCLUDE_MEMORY
24 #include "coretypes.h"
27 #include "basic-block.h"
29 #include "diagnostic.h"
31 #include "analyzer/analyzer.h"
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). */
42 get_stmt_location (const gimple
*stmt
, function
*fun
)
44 if (get_pure_location (stmt
->location
) == UNKNOWN_LOCATION
)
46 /* Workaround for missing location information for clobber
47 stmts, which seem to lack location information in the C frontend
48 at least. Created by gimplify_bind_expr, which uses the
49 BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr))
50 but this is never set up when the block is created in
51 c_end_compound_stmt's pop_scope.
52 TODO: fix this missing location information.
54 For now, as a hackish workaround, use the location of the end of
56 if (gimple_clobber_p (stmt
) && fun
)
57 return fun
->function_end_locus
;
60 return stmt
->location
;
64 fixup_tree_for_diagnostic_1 (tree expr
, hash_set
<tree
> *visited
);
66 /* Attemp to generate a tree for the LHS of ASSIGN_STMT.
67 VISITED must be non-NULL; it is used to ensure termination. */
70 get_diagnostic_tree_for_gassign_1 (const gassign
*assign_stmt
,
71 hash_set
<tree
> *visited
)
73 enum tree_code code
= gimple_assign_rhs_code (assign_stmt
);
75 /* Reverse the effect of extract_ops_from_tree during
77 switch (get_gimple_rhs_class (code
))
80 case GIMPLE_INVALID_RHS
:
82 case GIMPLE_TERNARY_RHS
:
83 case GIMPLE_BINARY_RHS
:
84 case GIMPLE_UNARY_RHS
:
86 tree t
= make_node (code
);
87 TREE_TYPE (t
) = TREE_TYPE (gimple_assign_lhs (assign_stmt
));
88 unsigned num_rhs_args
= gimple_num_ops (assign_stmt
) - 1;
89 for (unsigned i
= 0; i
< num_rhs_args
; i
++)
91 tree op
= gimple_op (assign_stmt
, i
+ 1);
94 op
= fixup_tree_for_diagnostic_1 (op
, visited
);
98 TREE_OPERAND (t
, i
) = op
;
102 case GIMPLE_SINGLE_RHS
:
104 tree op
= gimple_op (assign_stmt
, 1);
105 op
= fixup_tree_for_diagnostic_1 (op
, visited
);
111 /* Subroutine of fixup_tree_for_diagnostic_1, called on SSA names.
112 Attempt to reconstruct a tree expression for SSA_NAME
113 based on its def-stmt.
114 SSA_NAME must be non-NULL.
115 VISITED must be non-NULL; it is used to ensure termination.
117 Return NULL_TREE if there is a problem. */
120 maybe_reconstruct_from_def_stmt (tree ssa_name
,
121 hash_set
<tree
> *visited
)
123 /* Ensure termination. */
124 if (visited
->contains (ssa_name
))
126 visited
->add (ssa_name
);
128 gimple
*def_stmt
= SSA_NAME_DEF_STMT (ssa_name
);
130 switch (gimple_code (def_stmt
))
137 /* Can't handle these. */
140 return get_diagnostic_tree_for_gassign_1
141 (as_a
<const gassign
*> (def_stmt
), visited
);
144 gcall
*call_stmt
= as_a
<gcall
*> (def_stmt
);
145 tree return_type
= gimple_call_return_type (call_stmt
);
146 tree fn
= fixup_tree_for_diagnostic_1 (gimple_call_fn (call_stmt
),
150 unsigned num_args
= gimple_call_num_args (call_stmt
);
151 auto_vec
<tree
> args (num_args
);
152 for (unsigned i
= 0; i
< num_args
; i
++)
154 tree arg
= gimple_call_arg (call_stmt
, i
);
155 arg
= fixup_tree_for_diagnostic_1 (arg
, visited
);
156 if (arg
== NULL_TREE
)
158 args
.quick_push (arg
);
161 return build_call_array_loc (gimple_location (call_stmt
),
163 num_args
, args
.address ());
169 /* Subroutine of fixup_tree_for_diagnostic: attempt to fixup EXPR,
171 VISITED must be non-NULL; it is used to ensure termination. */
174 fixup_tree_for_diagnostic_1 (tree expr
, hash_set
<tree
> *visited
)
177 && TREE_CODE (expr
) == SSA_NAME
178 && (SSA_NAME_VAR (expr
) == NULL_TREE
179 || DECL_ARTIFICIAL (SSA_NAME_VAR (expr
))))
181 if (tree var
= SSA_NAME_VAR (expr
))
182 if (VAR_P (var
) && DECL_HAS_DEBUG_EXPR_P (var
))
183 return DECL_DEBUG_EXPR (var
);
184 if (tree expr2
= maybe_reconstruct_from_def_stmt (expr
, visited
))
190 /* We don't want to print '<unknown>' in our diagnostics (PR analyzer/99771),
191 but sometimes we generate diagnostics involving an ssa name for a
194 Work around this by attempting to reconstruct a tree expression for
195 such temporaries based on their def-stmts.
197 Otherwise return EXPR.
202 fixup_tree_for_diagnostic (tree expr
)
204 hash_set
<tree
> visited
;
205 return fixup_tree_for_diagnostic_1 (expr
, &visited
);
208 /* Attempt to generate a tree for the LHS of ASSIGN_STMT. */
211 get_diagnostic_tree_for_gassign (const gassign
*assign_stmt
)
213 hash_set
<tree
> visited
;
214 return get_diagnostic_tree_for_gassign_1 (assign_stmt
, &visited
);
219 /* Helper function for checkers. Is the CALL to the given function name,
220 and with the given number of arguments?
222 This doesn't resolve function pointers via the region model;
223 is_named_call_p should be used instead, using a fndecl from
224 get_fndecl_for_call; this function should only be used for special cases
225 where it's not practical to get at the region model, or for special
226 analyzer functions such as __analyzer_dump. */
229 is_special_named_call_p (const gcall
*call
, const char *funcname
,
230 unsigned int num_args
)
232 gcc_assert (funcname
);
234 tree fndecl
= gimple_call_fndecl (call
);
238 return is_named_call_p (fndecl
, funcname
, call
, num_args
);
241 /* Helper function for checkers. Is FNDECL an extern fndecl at file scope
242 that has the given FUNCNAME?
244 Compare with special_function_p in calls.cc. */
247 is_named_call_p (const_tree fndecl
, const char *funcname
)
250 gcc_assert (funcname
);
252 if (!maybe_special_function_p (fndecl
))
255 tree identifier
= DECL_NAME (fndecl
);
256 const char *name
= IDENTIFIER_POINTER (identifier
);
257 const char *tname
= name
;
259 /* Potentially disregard prefix _ or __ in FNDECL's name, but not if
260 FUNCNAME itself has leading underscores (e.g. when looking for
261 "__analyzer_eval"). */
262 if (funcname
[0] != '_' && name
[0] == '_')
270 return 0 == strcmp (tname
, funcname
);
273 /* Return true if FNDECL is within the namespace "std".
274 Compare with cp/typeck.cc: decl_in_std_namespace_p, but this doesn't
275 rely on being the C++ FE (or handle inline namespaces inside of std). */
278 is_std_function_p (const_tree fndecl
)
280 tree name_decl
= DECL_NAME (fndecl
);
283 if (!DECL_CONTEXT (fndecl
))
285 if (TREE_CODE (DECL_CONTEXT (fndecl
)) != NAMESPACE_DECL
)
287 tree ns
= DECL_CONTEXT (fndecl
);
288 if (!(DECL_CONTEXT (ns
) == NULL_TREE
289 || TREE_CODE (DECL_CONTEXT (ns
)) == TRANSLATION_UNIT_DECL
))
293 return id_equal ("std", DECL_NAME (ns
));
296 /* Like is_named_call_p, but look for std::FUNCNAME. */
299 is_std_named_call_p (const_tree fndecl
, const char *funcname
)
302 gcc_assert (funcname
);
304 if (!is_std_function_p (fndecl
))
307 tree identifier
= DECL_NAME (fndecl
);
308 const char *name
= IDENTIFIER_POINTER (identifier
);
309 const char *tname
= name
;
311 /* Don't disregard prefix _ or __ in FNDECL's name. */
313 return 0 == strcmp (tname
, funcname
);
316 /* Helper function for checkers. Is FNDECL an extern fndecl at file scope
317 that has the given FUNCNAME, and does CALL have the given number of
321 is_named_call_p (const_tree fndecl
, const char *funcname
,
322 const gcall
*call
, unsigned int num_args
)
325 gcc_assert (funcname
);
327 if (!is_named_call_p (fndecl
, funcname
))
330 if (gimple_call_num_args (call
) != num_args
)
336 /* Like is_named_call_p, but check for std::FUNCNAME. */
339 is_std_named_call_p (const_tree fndecl
, const char *funcname
,
340 const gcall
*call
, unsigned int num_args
)
343 gcc_assert (funcname
);
345 if (!is_std_named_call_p (fndecl
, funcname
))
348 if (gimple_call_num_args (call
) != num_args
)
354 /* Return true if stmt is a setjmp or sigsetjmp call. */
357 is_setjmp_call_p (const gcall
*call
)
359 if (is_special_named_call_p (call
, "setjmp", 1)
360 || is_special_named_call_p (call
, "sigsetjmp", 2))
361 /* region_model::on_setjmp requires a pointer. */
362 if (POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call
, 0))))
368 /* Return true if stmt is a longjmp or siglongjmp call. */
371 is_longjmp_call_p (const gcall
*call
)
373 if (is_special_named_call_p (call
, "longjmp", 2)
374 || is_special_named_call_p (call
, "siglongjmp", 2))
375 /* exploded_node::on_longjmp requires a pointer for the initial
377 if (POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call
, 0))))
383 /* For a CALL that matched is_special_named_call_p or is_named_call_p for
384 some name, return a name for the called function suitable for use in
385 diagnostics (stripping the leading underscores). */
388 get_user_facing_name (const gcall
*call
)
390 tree fndecl
= gimple_call_fndecl (call
);
393 tree identifier
= DECL_NAME (fndecl
);
394 gcc_assert (identifier
);
396 const char *name
= IDENTIFIER_POINTER (identifier
);
398 /* Strip prefix _ or __ in FNDECL's name. */
410 /* Generate a label_text instance by formatting FMT, using a
411 temporary clone of the global_dc's printer (thus using its
412 formatting callbacks).
414 Colorize if the global_dc supports colorization and CAN_COLORIZE is
418 make_label_text (bool can_colorize
, const char *fmt
, ...)
420 pretty_printer
*pp
= global_dc
->printer
->clone ();
421 pp_clear_output_area (pp
);
424 pp_show_color (pp
) = false;
427 rich_location
rich_loc (line_table
, UNKNOWN_LOCATION
);
433 ti
.format_spec
= _(fmt
);
437 ti
.m_richloc
= &rich_loc
;
440 pp_output_formatted_text (pp
);
444 label_text result
= label_text::take (xstrdup (pp_formatted_text (pp
)));
449 /* As above, but with singular vs plural. */
452 make_label_text_n (bool can_colorize
, unsigned HOST_WIDE_INT n
,
453 const char *singular_fmt
,
454 const char *plural_fmt
, ...)
456 pretty_printer
*pp
= global_dc
->printer
->clone ();
457 pp_clear_output_area (pp
);
460 pp_show_color (pp
) = false;
463 rich_location
rich_loc (line_table
, UNKNOWN_LOCATION
);
467 va_start (ap
, plural_fmt
);
469 const char *fmt
= ngettext (singular_fmt
, plural_fmt
, n
);
471 ti
.format_spec
= fmt
;
475 ti
.m_richloc
= &rich_loc
;
478 pp_output_formatted_text (pp
);
482 label_text result
= label_text::take (xstrdup (pp_formatted_text (pp
)));
487 #endif /* #if ENABLE_ANALYZER */