Skip to content

Commit 7820ff8

Browse files
committed
Remove old unused code pass this was too generic
This now uses the TREE_USED fields on GCC tree's to track the usage of VAR_DECLS, PARM_DECLS and CONST_DECLS. The code does a pass over the body and parameters of functions as a lint pass. Fixes #676
1 parent e00311a commit 7820ff8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+263
-267
lines changed

gcc/rust/Make-lang.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ GRS_OBJS = \
101101
rust/rust-autoderef.o \
102102
rust/rust-substitution-mapper.o \
103103
rust/rust-lint-marklive.o \
104+
rust/rust-lint-unused-var.o \
104105
rust/rust-hir-type-check-path.o \
105106
rust/rust-compile-intrinsic.o \
106107
rust/rust-compile-pattern.o \

gcc/rust/backend/rust-compile-context.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ class Context
307307
return mangler.mangle_item (ty, path);
308308
}
309309

310+
std::vector<tree> &get_type_decls () { return type_decls; }
311+
std::vector<::Bvariable *> &get_var_decls () { return var_decls; }
312+
std::vector<tree> &get_const_decls () { return const_decls; }
313+
std::vector<tree> &get_func_decls () { return func_decls; }
314+
310315
private:
311316
::Backend *backend;
312317
Resolver::Resolver *resolver;

gcc/rust/backend/rust-compile-expr.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,18 +1339,22 @@ CompileExpr::visit (HIR::IdentifierExpr &expr)
13391339
Bvariable *var = nullptr;
13401340
if (ctx->lookup_const_decl (ref, &translated))
13411341
{
1342+
TREE_USED (translated) = 1;
13421343
return;
13431344
}
13441345
else if (ctx->lookup_function_decl (ref, &fn))
13451346
{
1347+
TREE_USED (fn) = 1;
13461348
translated = address_expression (fn, expr.get_locus ());
13471349
}
13481350
else if (ctx->lookup_var_decl (ref, &var))
13491351
{
1352+
// TREE_USED is setup in the gcc abstraction here
13501353
translated = ctx->get_backend ()->var_expression (var, expr.get_locus ());
13511354
}
13521355
else if (ctx->lookup_pattern_binding (ref, &translated))
13531356
{
1357+
TREE_USED (translated) = 1;
13541358
return;
13551359
}
13561360
else
@@ -1371,6 +1375,11 @@ CompileExpr::visit (HIR::IdentifierExpr &expr)
13711375
else
13721376
translated = CompileItem::compile (resolved_item, ctx, lookup, true,
13731377
expr.get_locus ());
1378+
1379+
if (translated != error_mark_node)
1380+
{
1381+
TREE_USED (translated) = 1;
1382+
}
13741383
}
13751384
}
13761385

gcc/rust/backend/rust-compile-resolve-path.cc

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "rust-hir-trait-resolve.h"
2424
#include "rust-hir-path-probe.h"
2525

26+
#include "print-tree.h"
27+
2628
namespace Rust {
2729
namespace Compile {
2830

@@ -117,12 +119,18 @@ ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
117119
// might be a constant
118120
tree constant_expr;
119121
if (ctx->lookup_const_decl (ref, &constant_expr))
120-
return constant_expr;
122+
{
123+
TREE_USED (constant_expr) = 1;
124+
return constant_expr;
125+
}
121126

122127
// this might be a variable reference or a function reference
123128
Bvariable *var = nullptr;
124129
if (ctx->lookup_var_decl (ref, &var))
125-
return ctx->get_backend ()->var_expression (var, expr_locus);
130+
{
131+
// TREE_USED is setup in the gcc abstraction here
132+
return ctx->get_backend ()->var_expression (var, expr_locus);
133+
}
126134

127135
// it might be a function call
128136
if (lookup->get_kind () == TyTy::TypeKind::FNDEF)
@@ -131,13 +139,19 @@ ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
131139
tree fn = NULL_TREE;
132140
if (ctx->lookup_function_decl (fntype->get_ty_ref (), &fn))
133141
{
142+
TREE_USED (fn) = 1;
134143
return address_expression (fn, expr_locus);
135144
}
136145
}
137146

138147
// let the query system figure it out
139-
return query_compile (ref, lookup, final_segment, mappings, expr_locus,
140-
is_qualified_path);
148+
tree resolved_item = query_compile (ref, lookup, final_segment, mappings,
149+
expr_locus, is_qualified_path);
150+
if (resolved_item != error_mark_node)
151+
{
152+
TREE_USED (resolved_item) = 1;
153+
}
154+
return resolved_item;
141155
}
142156

143157
tree

gcc/rust/lang.opt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ Wall
3838
Rust
3939
; Documented in c.opt
4040

41+
Wunused-variable
42+
Rust Var(warn_unused_variable) Init(1) Warning
43+
; documented in common.opt
44+
45+
Wunused-const-variable
46+
Rust Warning Alias(Wunused-const-variable=, 2, 0)
47+
Warn when a const variable is unused.
48+
49+
Wunused-const-variable=
50+
Rust Joined RejectNegative UInteger Var(warn_unused_const_variable) Init(1) Warning LangEnabledBy(Rust,Wunused-variable, 1, 0) IntegerRange(0, 2)
51+
Warn when a const variable is unused.
52+
4153
Wunused-result
4254
Rust Var(warn_unused_result) Init(1) Warning
4355
Warn if a caller of a function, marked with attribute warn_unused_result, does not use its return value.

gcc/rust/lint/rust-lint-unused-var.cc

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright (C) 2021-2022 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 "print-tree.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 *walk_subtrees, void *closure)
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 t = ctx.get_backend ()->var_expression (var, Location ());
88+
check_decl (&t);
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

gcc/rust/lint/rust-lint-unused-var.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (C) 2021-2022 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+
#ifndef RUST_LINT_UNUSED_VAR
20+
#define RUST_LINT_UNUSED_VAR
21+
22+
#include "rust-compile-context.h"
23+
24+
namespace Rust {
25+
namespace Analysis {
26+
27+
class UnusedVariables
28+
{
29+
public:
30+
static void Lint (Compile::Context &ctx);
31+
};
32+
33+
} // namespace Analysis
34+
} // namespace Rust
35+
36+
#endif // RUST_LINT_UNUSED_VAR

gcc/rust/resolve/rust-ast-resolve-unused.h

Lines changed: 0 additions & 61 deletions
This file was deleted.

gcc/rust/rust-gcc.cc

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,15 @@ class Bvariable
8686
tree
8787
Bvariable::get_tree (Location location) const
8888
{
89-
if (this->orig_type_ == NULL || this->t_ == error_mark_node
90-
|| TREE_TYPE (this->t_) == this->orig_type_)
91-
return this->t_;
89+
if (this->t_ == error_mark_node)
90+
return error_mark_node;
91+
92+
TREE_USED (this->t_) = 1;
93+
if (this->orig_type_ == NULL || TREE_TYPE (this->t_) == this->orig_type_)
94+
{
95+
return this->t_;
96+
}
97+
9298
// Return *(orig_type*)&decl. */
9399
tree t = build_fold_addr_expr_loc (location.gcc_location (), this->t_);
94100
t = fold_build1_loc (location.gcc_location (), NOP_EXPR,
@@ -1063,10 +1069,7 @@ Gcc_backend::zero_expression (tree t)
10631069
tree
10641070
Gcc_backend::var_expression (Bvariable *var, Location location)
10651071
{
1066-
tree ret = var->get_tree (location);
1067-
if (ret == error_mark_node)
1068-
return error_mark_node;
1069-
return ret;
1072+
return var->get_tree (location);
10701073
}
10711074

10721075
// An expression that indirectly references an expression.
@@ -2394,7 +2397,6 @@ Gcc_backend::local_variable (tree function, const std::string &name,
23942397
tree decl = build_decl (location.gcc_location (), VAR_DECL,
23952398
get_identifier_from_string (name), type_tree);
23962399
DECL_CONTEXT (decl) = function;
2397-
TREE_USED (decl) = 1;
23982400

23992401
if (decl_var != NULL)
24002402
{
@@ -2417,7 +2419,7 @@ Gcc_backend::parameter_variable (tree function, const std::string &name,
24172419
get_identifier_from_string (name), type_tree);
24182420
DECL_CONTEXT (decl) = function;
24192421
DECL_ARG_TYPE (decl) = type_tree;
2420-
TREE_USED (decl) = 1;
2422+
24212423
rust_preserve_from_gc (decl);
24222424
return new Bvariable (decl);
24232425
}

0 commit comments

Comments
 (0)