From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by sourceware.org (Postfix) with ESMTPS id 5BC52396E031 for ; Tue, 6 Dec 2022 10:13:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5BC52396E031 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wm1-x32f.google.com with SMTP id o7-20020a05600c510700b003cffc0b3374so12320534wms.0 for ; Tue, 06 Dec 2022 02:13:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=NpwpGiMjp9LDnIcvQdNjWxhM+9FQICs4f/AdDBj7jhg=; b=MnNQVjI/rKmBwrsyQhOb3uZBsSsUAtw4EoplTUopFJ2HMqgB2bsJfPvrwtFMIYYG1o GrOLQn/QL3+AOarSE09saxrYsCyzYY9pHda3Lqirp55BgUBH+h29IdJIp8Im+CWy/8zn EKcucPYBdgpX3bdoo/fwbTMlu7Yb7BbKC5LWCBc2PU+f02xqeirVG+Bc39s45wluQal+ PibjqFFE11UZJssx3CXxXVW17qbm5QM5rUXAutsPS82uDR5TfT3wqwALqSKMq7reAFSt fGIbY335oaYLXSsNGQ/g91LMmCAaL7yewqDjAhfhidwi69otsDrk5Eyy0haewMBmEZHy L2gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=NpwpGiMjp9LDnIcvQdNjWxhM+9FQICs4f/AdDBj7jhg=; b=eqWRQPMEfYpwRlKJQt55NMKnWFhx/1oArHsCzNcEtmYtpNDAZ9ELCkqOG0SJgpv6Ed PpuXksOYm2w/2EMI83VG3rdGDH7R+uu6uOEXbddh8v6HodJWx4OWKdlcs12Hvj82vKm5 bEGaYyNvoGFYYXOQzbSyyW3cvKhIMxNmC3qv/jf6y7YJ8Pzn1RPMi32f7WOi09AO5JYQ rMhGNiTIbcN8oVguRBVsywJFoV6x8tZ3g0G1rZJI2Plje9krtOD2yvEILRl8DGJvJVFN GJtKNQub4hYB+kez6RcoLwTVNse4DnZbQbeujfM40VO+7IjaA7xoH2R5JE0UjB/8waeE Bk/Q== X-Gm-Message-State: ANoB5pnjDkc9AQCoyEAd9nxobKzFPKhEdDt56lhXoHMPq/iCVmM1l1yf HEMMHSLh5u9EEkeN7Wp8mZOxRXJ98uf9fNl3cw== X-Google-Smtp-Source: AA0mqf6+EuDNEENCmFBFWLCvkYRBSkbiCwNdHwyNBXC6Hymw11tXra9UFHj4GFKFmnnjpgTdZb5ncQ== X-Received: by 2002:a1c:f401:0:b0:3c7:84d:72d with SMTP id z1-20020a1cf401000000b003c7084d072dmr49000499wma.181.1670321589046; Tue, 06 Dec 2022 02:13:09 -0800 (PST) Received: from platypus.lan ([2001:861:5e4c:3bb0:6424:328a:1734:3249]) by smtp.googlemail.com with ESMTPSA id r10-20020a05600c458a00b003cfd4a50d5asm27052699wmo.34.2022.12.06.02.13.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 02:13:08 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, richard.guenther@gmail.com, jakub@redhat.com, Philip Herron Subject: [PATCH Rust front-end v4 34/46] gccrs: Add unused variable scan Date: Tue, 6 Dec 2022 11:14:07 +0100 Message-Id: <20221206101417.778807-35-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221206101417.778807-1-arthur.cohen@embecosm.com> References: <20221206101417.778807-1-arthur.cohen@embecosm.com> Reply-To: arthur.cohen@embecosm.com MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-17.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Philip Herron This is a simple walk_tree which acts on the monomorphized code by walking the compiled translation unit of functions. --- gcc/rust/checks/lints/rust-lint-unused-var.cc | 98 +++++++++++++++++++ gcc/rust/checks/lints/rust-lint-unused-var.h | 36 +++++++ 2 files changed, 134 insertions(+) create mode 100644 gcc/rust/checks/lints/rust-lint-unused-var.cc create mode 100644 gcc/rust/checks/lints/rust-lint-unused-var.h diff --git a/gcc/rust/checks/lints/rust-lint-unused-var.cc b/gcc/rust/checks/lints/rust-lint-unused-var.cc new file mode 100644 index 00000000000..d4317e53280 --- /dev/null +++ b/gcc/rust/checks/lints/rust-lint-unused-var.cc @@ -0,0 +1,98 @@ +// Copyright (C) 2021-2022 Free Software Foundation, Inc. + +// This file is part of GCC. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// . + +#include "rust-lint-unused-var.h" +#include "print-tree.h" + +namespace Rust { +namespace Analysis { + +static void +check_decl (tree *t) +{ + rust_assert (TREE_CODE (*t) == VAR_DECL || TREE_CODE (*t) == PARM_DECL + || TREE_CODE (*t) == CONST_DECL); + + tree var_name = DECL_NAME (*t); + const char *var_name_ptr = IDENTIFIER_POINTER (var_name); + bool starts_with_under_score = strncmp (var_name_ptr, "_", 1) == 0; + + bool is_constant = TREE_CODE (*t) == CONST_DECL; + // if (!is_constant) + // { + // debug_tree (*t); + // rust_debug ("found var-decl: used %s artifical %s underscore %s name + // %s", + // TREE_USED (*t) ? "true" : "false", + // DECL_ARTIFICIAL (*t) ? "true" : "false", + // starts_with_under_score ? "true" : "false", var_name_ptr); + // } + + if (!TREE_USED (*t) && !DECL_ARTIFICIAL (*t) && !starts_with_under_score) + { + warning_at (DECL_SOURCE_LOCATION (*t), + is_constant ? OPT_Wunused_const_variable_ + : OPT_Wunused_variable, + "unused name %qE", *t); + } +} + +static tree +unused_var_walk_fn (tree *t, int *walk_subtrees, void *closure) +{ + switch (TREE_CODE (*t)) + { + case VAR_DECL: + case CONST_DECL: + check_decl (t); + break; + + default: + break; + } + return NULL_TREE; +} + +void +UnusedVariables::Lint (Compile::Context &ctx) +{ + for (auto &fndecl : ctx.get_func_decls ()) + { + for (tree p = DECL_ARGUMENTS (fndecl); p != NULL_TREE; p = DECL_CHAIN (p)) + { + check_decl (&p); + } + + walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl), + &unused_var_walk_fn, &ctx); + } + + for (auto &var : ctx.get_var_decls ()) + { + tree t = ctx.get_backend ()->var_expression (var, Location ()); + check_decl (&t); + } + + for (auto &const_decl : ctx.get_const_decls ()) + { + check_decl (&const_decl); + } +} + +} // namespace Analysis +} // namespace Rust diff --git a/gcc/rust/checks/lints/rust-lint-unused-var.h b/gcc/rust/checks/lints/rust-lint-unused-var.h new file mode 100644 index 00000000000..6fabfeff01b --- /dev/null +++ b/gcc/rust/checks/lints/rust-lint-unused-var.h @@ -0,0 +1,36 @@ +// Copyright (C) 2021-2022 Free Software Foundation, Inc. + +// This file is part of GCC. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// . + +#ifndef RUST_LINT_UNUSED_VAR +#define RUST_LINT_UNUSED_VAR + +#include "rust-compile-context.h" + +namespace Rust { +namespace Analysis { + +class UnusedVariables +{ +public: + static void Lint (Compile::Context &ctx); +}; + +} // namespace Analysis +} // namespace Rust + +#endif // RUST_LINT_UNUSED_VAR -- 2.38.1