Debian Codegraph @

gcc/analyzer/analyzer.cc

raw
1 /* Utility functions for the analyzer.
2 Copyright (C) 2019-2025 Free Software Foundation, Inc.
3 Contributed by David Malcolm <[email protected]>.
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 "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "function.h"
26 #include "basic-block.h"
27 #include "gimple.h"
28 #include "diagnostic.h"
29 #include "intl.h"
30 #include "analyzer/analyzer.h"
31 #include "tree-pretty-print.h"
32 #include "diagnostic-event-id.h"
33 #include "tree-dfa.h"
34 #include "make-unique.h"
35
36 #if ENABLE_ANALYZER
37
38 namespace ana {
39
40 /* Workaround for missing location information for some stmts,
41 which ultimately should be solved by fixing the frontends
42 to provide the locations (TODO). */
43
44 location_t
45 get_stmt_location (const gimple *stmt, function *fun)
46 {
47 if (!stmt)
48 return UNKNOWN_LOCATION;
49 if (get_pure_location (stmt->location) == UNKNOWN_LOCATION)
50 {
51 /* Workaround for missing location information for clobber
52 stmts, which seem to lack location information in the C frontend
53 at least. Created by gimplify_bind_expr, which uses the
54 BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr))
55 but this is never set up when the block is created in
56 c_end_compound_stmt's pop_scope.
57 TODO: fix this missing location information.
58
59 For now, as a hackish workaround, use the location of the end of
60 the function. */
61 if (gimple_clobber_p (stmt) && fun)
62 return fun->function_end_locus;
63 }
64
65 return stmt->location;
66 }
67
68 static tree
69 fixup_tree_for_diagnostic_1 (tree expr, hash_set<tree> *visited);
70
71 /* Attemp to generate a tree for the LHS of ASSIGN_STMT.
72 VISITED must be non-NULL; it is used to ensure termination. */
73
74 static tree
75 get_diagnostic_tree_for_gassign_1 (const gassign *assign_stmt,
76 hash_set<tree> *visited)
77 {
78 enum tree_code code = gimple_assign_rhs_code (assign_stmt);
79
80 /* Reverse the effect of extract_ops_from_tree during
81 gimplification. */
82 switch (get_gimple_rhs_class (code))
83 {
84 default:
85 case GIMPLE_INVALID_RHS:
86 gcc_unreachable ();
87 case GIMPLE_TERNARY_RHS:
88 case GIMPLE_BINARY_RHS:
89 case GIMPLE_UNARY_RHS:
90 {
91 tree t = make_node (code);
92 TREE_TYPE (t) = TREE_TYPE (gimple_assign_lhs (assign_stmt));
93 unsigned num_rhs_args = gimple_num_ops (assign_stmt) - 1;
94 for (unsigned i = 0; i < num_rhs_args; i++)
95 {
96 tree op = gimple_op (assign_stmt, i + 1);
97 if (op)
98 {
99 op = fixup_tree_for_diagnostic_1 (op, visited);
100 if (op == NULL_TREE)
101 return NULL_TREE;
102 }
103 TREE_OPERAND (t, i) = op;
104 }
105 return t;
106 }
107 case GIMPLE_SINGLE_RHS:
108 {
109 tree op = gimple_op (assign_stmt, 1);
110 op = fixup_tree_for_diagnostic_1 (op, visited);
111 return op;
112 }
113 }
114 }
115
116 /* Subroutine of fixup_tree_for_diagnostic_1, called on SSA names.
117 Attempt to reconstruct a tree expression for SSA_NAME
118 based on its def-stmt.
119 SSA_NAME must be non-NULL.
120 VISITED must be non-NULL; it is used to ensure termination.
121
122 Return NULL_TREE if there is a problem. */
123
124 static tree
125 maybe_reconstruct_from_def_stmt (tree ssa_name,
126 hash_set<tree> *visited)
127 {
128 /* Ensure termination. */
129 if (visited->contains (ssa_name))
130 return NULL_TREE;
131 visited->add (ssa_name);
132
133 gimple *def_stmt = SSA_NAME_DEF_STMT (ssa_name);
134
135 switch (gimple_code (def_stmt))
136 {
137 default:
138 gcc_unreachable ();
139 case GIMPLE_ASM:
140 case GIMPLE_NOP:
141 case GIMPLE_PHI:
142 /* Can't handle these. */
143 return NULL_TREE;
144 case GIMPLE_ASSIGN:
145 return get_diagnostic_tree_for_gassign_1
146 (as_a <const gassign *> (def_stmt), visited);
147 case GIMPLE_CALL:
148 {
149 gcall *call_stmt = as_a <gcall *> (def_stmt);
150 tree return_type = gimple_call_return_type (call_stmt);
151 tree fn = fixup_tree_for_diagnostic_1 (gimple_call_fn (call_stmt),
152 visited);
153 if (fn == NULL_TREE)
154 return NULL_TREE;
155 unsigned num_args = gimple_call_num_args (call_stmt);
156 auto_vec<tree> args (num_args);
157 for (unsigned i = 0; i < num_args; i++)
158 {
159 tree arg = gimple_call_arg (call_stmt, i);
160 arg = fixup_tree_for_diagnostic_1 (arg, visited);
161 if (arg == NULL_TREE)
162 return NULL_TREE;
163 args.quick_push (arg);
164 }
165 gcc_assert (fn);
166 return build_call_array_loc (gimple_location (call_stmt),
167 return_type, fn,
168 num_args, args.address ());
169 }
170 break;
171 }
172 }
173
174 /* Subroutine of fixup_tree_for_diagnostic: attempt to fixup EXPR,
175 which can be NULL.
176 VISITED must be non-NULL; it is used to ensure termination. */
177
178 static tree
179 fixup_tree_for_diagnostic_1 (tree expr, hash_set<tree> *visited)
180 {
181 if (expr
182 && TREE_CODE (expr) == SSA_NAME
183 && (SSA_NAME_VAR (expr) == NULL_TREE
184 || DECL_ARTIFICIAL (SSA_NAME_VAR (expr))))
185 {
186 if (tree var = SSA_NAME_VAR (expr))
187 if (VAR_P (var) && DECL_HAS_DEBUG_EXPR_P (var))
188 return DECL_DEBUG_EXPR (var);
189 if (tree expr2 = maybe_reconstruct_from_def_stmt (expr, visited))
190 return expr2;
191 }
192 return expr;
193 }
194
195 /* We don't want to print '<unknown>' in our diagnostics (PR analyzer/99771),
196 but sometimes we generate diagnostics involving an ssa name for a
197 temporary.
198
199 Work around this by attempting to reconstruct a tree expression for
200 such temporaries based on their def-stmts.
201
202 Otherwise return EXPR.
203
204 EXPR can be NULL. */
205
206 tree
207 fixup_tree_for_diagnostic (tree expr)
208 {
209 hash_set<tree> visited;
210 return fixup_tree_for_diagnostic_1 (expr, &visited);
211 }
212
213 /* Attempt to generate a tree for the LHS of ASSIGN_STMT. */
214
215 tree
216 get_diagnostic_tree_for_gassign (const gassign *assign_stmt)
217 {
218 hash_set<tree> visited;
219 return get_diagnostic_tree_for_gassign_1 (assign_stmt, &visited);
220 }
221
222 /* Generate a JSON value for NODE, which can be NULL_TREE.
223 This is intended for debugging the analyzer rather than serialization and
224 thus is a string (or null, for NULL_TREE). */
225
226 std::unique_ptr<json::value>
227 tree_to_json (tree node)
228 {
229 if (!node)
230 return ::make_unique<json::literal> (json::JSON_NULL);
231
232 pretty_printer pp;
233 dump_generic_node (&pp, node, 0, TDF_VOPS|TDF_MEMSYMS, false);
234 return ::make_unique<json::string> (pp_formatted_text (&pp));
235 }
236
237 /* Generate a JSON value for EVENT_ID.
238 This is intended for debugging the analyzer rather than serialization and
239 thus is a string matching those seen in event messags (or null,
240 for unknown). */
241
242 std::unique_ptr<json::value>
243 diagnostic_event_id_to_json (const diagnostic_event_id_t &event_id)
244 {
245 if (event_id.known_p ())
246 {
247 pretty_printer pp;
248 pp_printf (&pp, "%@", &event_id);
249 return ::make_unique<json::string> (pp_formatted_text (&pp));
250 }
251 else
252 return ::make_unique<json::literal> (json::JSON_NULL);
253 }
254
255 /* Generate a JSON value for OFFSET.
256 This is intended for debugging the analyzer rather than serialization and
257 thus is a string. */
258
259 std::unique_ptr<json::value>
260 bit_offset_to_json (const bit_offset_t &offset)
261 {
262 pretty_printer pp;
263 pp_wide_int_large (&pp, offset, SIGNED);
264 return ::make_unique<json::string> (pp_formatted_text (&pp));
265 }
266
267 /* Generate a JSON value for OFFSET.
268 This is intended for debugging the analyzer rather than serialization and
269 thus is a string. */
270
271 std::unique_ptr<json::value>
272 byte_offset_to_json (const byte_offset_t &offset)
273 {
274 pretty_printer pp;
275 pp_wide_int_large (&pp, offset, SIGNED);
276 return ::make_unique<json::string> (pp_formatted_text (&pp));
277 }
278
279 /* Workaround for lack of const-correctness of ssa_default_def. */
280
281 tree
282 get_ssa_default_def (const function &fun, tree var)
283 {
284 return ssa_default_def (const_cast <function *> (&fun), var);
285 }
286
287 } // namespace ana
288
289 /* Helper function for checkers. Is the CALL to the given function name,
290 and with the given number of arguments?
291
292 This doesn't resolve function pointers via the region model;
293 is_named_call_p should be used instead, using a fndecl from
294 get_fndecl_for_call; this function should only be used for special cases
295 where it's not practical to get at the region model, or for special
296 analyzer functions such as __analyzer_dump.
297
298 If LOOK_IN_STD is true, then also look for within std:: for the name. */
299
300 bool
301 is_special_named_call_p (const gcall *call, const char *funcname,
302 unsigned int num_args, bool look_in_std)
303 {
304 gcc_assert (funcname);
305
306 tree fndecl = gimple_call_fndecl (call);
307 if (!fndecl)
308 return false;
309
310 if (is_named_call_p (fndecl, funcname, call, num_args))
311 return true;
312 if (look_in_std)
313 if (is_std_named_call_p (fndecl, funcname, call, num_args))
314 return true;
315 return false;
316 }
317
318 /* Helper function for checkers. Is FNDECL an extern fndecl at file scope
319 that has the given FUNCNAME?
320
321 Compare with special_function_p in calls.cc. */
322
323 bool
324 is_named_call_p (const_tree fndecl, const char *funcname)
325 {
326 gcc_assert (fndecl);
327 gcc_assert (funcname);
328
329 if (!maybe_special_function_p (fndecl))
330 return false;
331
332 tree identifier = DECL_NAME (fndecl);
333 const char *name = IDENTIFIER_POINTER (identifier);
334 const char *tname = name;
335
336 /* Potentially disregard prefix _ or __ in FNDECL's name, but not if
337 FUNCNAME itself has leading underscores (e.g. when looking for
338 "__analyzer_eval"). */
339 if (funcname[0] != '_' && name[0] == '_')
340 {
341 if (name[1] == '_')
342 tname += 2;
343 else
344 tname += 1;
345 }
346
347 return 0 == strcmp (tname, funcname);
348 }
349
350 /* Return true if FNDECL is within the namespace "std".
351 Compare with cp/typeck.cc: decl_in_std_namespace_p, but this doesn't
352 rely on being the C++ FE (or handle inline namespaces inside of std). */
353
354 bool
355 is_std_function_p (const_tree fndecl)
356 {
357 tree name_decl = DECL_NAME (fndecl);
358 if (!name_decl)
359 return false;
360 if (!DECL_CONTEXT (fndecl))
361 return false;
362 if (TREE_CODE (DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
363 return false;
364 tree ns = DECL_CONTEXT (fndecl);
365 if (!(DECL_CONTEXT (ns) == NULL_TREE
366 || TREE_CODE (DECL_CONTEXT (ns)) == TRANSLATION_UNIT_DECL))
367 return false;
368 if (!DECL_NAME (ns))
369 return false;
370 return id_equal ("std", DECL_NAME (ns));
371 }
372
373 /* Like is_named_call_p, but look for std::FUNCNAME. */
374
375 bool
376 is_std_named_call_p (const_tree fndecl, const char *funcname)
377 {
378 gcc_assert (fndecl);
379 gcc_assert (funcname);
380
381 if (!is_std_function_p (fndecl))
382 return false;
383
384 tree identifier = DECL_NAME (fndecl);
385 const char *name = IDENTIFIER_POINTER (identifier);
386 const char *tname = name;
387
388 /* Don't disregard prefix _ or __ in FNDECL's name. */
389
390 return 0 == strcmp (tname, funcname);
391 }
392
393 /* Helper function for checkers. Is FNDECL an extern fndecl at file scope
394 that has the given FUNCNAME, and does CALL have the given number of
395 arguments? */
396
397 bool
398 is_named_call_p (const_tree fndecl, const char *funcname,
399 const gcall *call, unsigned int num_args)
400 {
401 gcc_assert (fndecl);
402 gcc_assert (funcname);
403
404 if (!is_named_call_p (fndecl, funcname))
405 return false;
406
407 if (gimple_call_num_args (call) != num_args)
408 return false;
409
410 return true;
411 }
412
413 /* Like is_named_call_p, but check for std::FUNCNAME. */
414
415 bool
416 is_std_named_call_p (const_tree fndecl, const char *funcname,
417 const gcall *call, unsigned int num_args)
418 {
419 gcc_assert (fndecl);
420 gcc_assert (funcname);
421
422 if (!is_std_named_call_p (fndecl, funcname))
423 return false;
424
425 if (gimple_call_num_args (call) != num_args)
426 return false;
427
428 return true;
429 }
430
431 /* Return true if stmt is a setjmp or sigsetjmp call. */
432
433 bool
434 is_setjmp_call_p (const gcall *call)
435 {
436 if (is_special_named_call_p (call, "setjmp", 1)
437 || is_special_named_call_p (call, "sigsetjmp", 2))
438 /* region_model::on_setjmp requires a pointer. */
439 if (POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
440 return true;
441
442 return false;
443 }
444
445 /* Return true if stmt is a longjmp or siglongjmp call. */
446
447 bool
448 is_longjmp_call_p (const gcall *call)
449 {
450 if (is_special_named_call_p (call, "longjmp", 2)
451 || is_special_named_call_p (call, "siglongjmp", 2))
452 /* exploded_node::on_longjmp requires a pointer for the initial
453 argument. */
454 if (POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
455 return true;
456
457 return false;
458 }
459
460 /* For a CALL that matched is_special_named_call_p or is_named_call_p for
461 some name, return a name for the called function suitable for use in
462 diagnostics (stripping the leading underscores). */
463
464 const char *
465 get_user_facing_name (const gcall *call)
466 {
467 tree fndecl = gimple_call_fndecl (call);
468 gcc_assert (fndecl);
469
470 tree identifier = DECL_NAME (fndecl);
471 gcc_assert (identifier);
472
473 const char *name = IDENTIFIER_POINTER (identifier);
474
475 /* Strip prefix _ or __ in FNDECL's name. */
476 if (name[0] == '_')
477 {
478 if (name[1] == '_')
479 return name + 2;
480 else
481 return name + 1;
482 }
483
484 return name;
485 }
486
487 /* Generate a label_text instance by formatting FMT, using a
488 temporary clone of the global_dc's printer (thus using its
489 formatting callbacks).
490
491 Colorize if the global_dc supports colorization and CAN_COLORIZE is
492 true. */
493
494 label_text
495 make_label_text (bool can_colorize, const char *fmt, ...)
496 {
497 std::unique_ptr<pretty_printer> pp (global_dc->clone_printer ());
498 pp_clear_output_area (pp.get ());
499
500 if (!can_colorize)
501 pp_show_color (pp.get ()) = false;
502
503 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
504
505 va_list ap;
506
507 va_start (ap, fmt);
508
509 text_info ti (_(fmt), &ap, 0, NULL, &rich_loc);
510 pp_format (pp.get (), &ti);
511 pp_output_formatted_text (pp.get ());
512
513 va_end (ap);
514
515 label_text result = label_text::take (xstrdup (pp_formatted_text (pp.get ())));
516 return result;
517 }
518
519 /* As above, but with singular vs plural. */
520
521 label_text
522 make_label_text_n (bool can_colorize, unsigned HOST_WIDE_INT n,
523 const char *singular_fmt,
524 const char *plural_fmt, ...)
525 {
526 std::unique_ptr<pretty_printer> pp (global_dc->clone_printer ());
527 pp_clear_output_area (pp.get ());
528
529 if (!can_colorize)
530 pp_show_color (pp.get ()) = false;
531
532 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
533
534 va_list ap;
535
536 va_start (ap, plural_fmt);
537
538 const char *fmt = ngettext (singular_fmt, plural_fmt, n);
539
540 text_info ti (fmt, &ap, 0, NULL, &rich_loc);
541
542 pp_format (pp.get (), &ti);
543 pp_output_formatted_text (pp.get ());
544
545 va_end (ap);
546
547 label_text result
548 = label_text::take (xstrdup (pp_formatted_text (pp.get ())));
549 return result;
550 }
551
552 #endif /* #if ENABLE_ANALYZER */