| 1 |
// Copyright (C) 2021-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
// This file is part of GCC. |
| 4 |
|
| 5 |
// GCC is free software; you can redistribute it and/or modify it under |
| 6 |
// the terms of the GNU General Public License as published by the Free |
| 7 |
// Software Foundation; either version 3, or (at your option) any later |
| 8 |
// version. |
| 9 |
|
| 10 |
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
// for more details. |
| 14 |
|
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with GCC; see the file COPYING3. If not see |
| 17 |
// <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
#include "rust-lint-unused-var.h" |
| 20 |
#include "rust-gcc.h" |
| 21 |
|
| 22 |
namespace Rust { |
| 23 |
namespace Analysis { |
| 24 |
|
| 25 |
static void |
| 26 |
check_decl (tree *t) |
| 27 |
{ |
| 28 |
rust_assert (TREE_CODE (*t) == VAR_DECL || TREE_CODE (*t) == PARM_DECL |
| 29 |
|| TREE_CODE (*t) == CONST_DECL); |
| 30 |
|
| 31 |
tree var_name = DECL_NAME (*t); |
| 32 |
const char *var_name_ptr = IDENTIFIER_POINTER (var_name); |
| 33 |
bool starts_with_under_score = strncmp (var_name_ptr, "_", 1) == 0; |
| 34 |
|
| 35 |
bool is_constant = TREE_CODE (*t) == CONST_DECL; |
| 36 |
// if (!is_constant) |
| 37 |
// { |
| 38 |
// debug_tree (*t); |
| 39 |
// rust_debug ("found var-decl: used %s artifical %s underscore %s name |
| 40 |
// %s", |
| 41 |
// TREE_USED (*t) ? "true" : "false", |
| 42 |
// DECL_ARTIFICIAL (*t) ? "true" : "false", |
| 43 |
// starts_with_under_score ? "true" : "false", var_name_ptr); |
| 44 |
// } |
| 45 |
|
| 46 |
if (!TREE_USED (*t) && !DECL_ARTIFICIAL (*t) && !starts_with_under_score) |
| 47 |
{ |
| 48 |
warning_at (DECL_SOURCE_LOCATION (*t), |
| 49 |
is_constant ? OPT_Wunused_const_variable_ |
| 50 |
: OPT_Wunused_variable, |
| 51 |
"unused name %qE", *t); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
static tree |
| 56 |
unused_var_walk_fn (tree *t, int *, void *) |
| 57 |
{ |
| 58 |
switch (TREE_CODE (*t)) |
| 59 |
{ |
| 60 |
case VAR_DECL: |
| 61 |
case CONST_DECL: |
| 62 |
check_decl (t); |
| 63 |
break; |
| 64 |
|
| 65 |
default: |
| 66 |
break; |
| 67 |
} |
| 68 |
return NULL_TREE; |
| 69 |
} |
| 70 |
|
| 71 |
void |
| 72 |
UnusedVariables::Lint (Compile::Context &ctx) |
| 73 |
{ |
| 74 |
for (auto &fndecl : ctx.get_func_decls ()) |
| 75 |
{ |
| 76 |
for (tree p = DECL_ARGUMENTS (fndecl); p != NULL_TREE; p = DECL_CHAIN (p)) |
| 77 |
{ |
| 78 |
check_decl (&p); |
| 79 |
} |
| 80 |
|
| 81 |
walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl), |
| 82 |
&unused_var_walk_fn, &ctx); |
| 83 |
} |
| 84 |
|
| 85 |
for (auto &var : ctx.get_var_decls ()) |
| 86 |
{ |
| 87 |
tree decl = var->get_decl (); |
| 88 |
check_decl (&decl); |
| 89 |
} |
| 90 |
|
| 91 |
for (auto &const_decl : ctx.get_const_decls ()) |
| 92 |
{ |
| 93 |
check_decl (&const_decl); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
} // namespace Analysis |
| 98 |
} // namespace Rust |