From patchwork Wed Aug 24 11:59:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: herron.philip@googlemail.com X-Patchwork-Id: 56999 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 40C84382EA0C for ; Wed, 24 Aug 2022 12:06:40 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id F008438A90BB; Wed, 24 Aug 2022 12:01:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F008438A90BB Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=googlemail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=googlemail.com Received: by mail-wr1-x429.google.com with SMTP id b5so16204991wrr.5; Wed, 24 Aug 2022 05:01:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20210112; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc; bh=2+xQFlaMBp9eqjhcZ8kafuZJmQUKrkM4HDGSADjdv04=; b=GZNqsoOgQfNE4rBgpsa3+uo3sOGAKonslZh0l3f085wFsg07FLowUeH++23afzfmZu AiFrprlswp3pX2ppTI9UERXaSCZKUth5/isqji5tUsm3/Os0+rJG+jG5sWjKjsQUvyR8 e5Q27guQsOzfZXkZl1xTwYR6nVlbSOy9/41mja9dP3X6oRtxVTic83zCjTZD4YncUTBS 1Q9achwiSg9422SjCKv7L1RcW7CVo5vNvBC1qIm0hIa09ereLOii5oU43U8r6liHgaAe PZ4np0z0G9JF5uKPLwpwXxJrtquZuDXQroBvjLVZk/QkJNoIxmIo9MGlUmRpwMdfo+Xv eSew== 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; bh=2+xQFlaMBp9eqjhcZ8kafuZJmQUKrkM4HDGSADjdv04=; b=lQSgUMy0M/LKuOdwI0YTYuYMXK84+sbtIBA9iKWX6N2DC46epWVAeqPc+AwySyN73Y ssr+9jq464H7o+Sd3gsRIwMjAnApBvvWTv1OaGmJJRFjYOvdRCICPkSBa0fQohrqu/Yu M5fQgspvpUKwx11XYlx9+xOnnQ9FlHyrJduGus/SEi+84Dr3FAVY6ejAp7raXYzOu7yU tJitw91I0afGlaa4AXRdkZm+tOSWFcVAtj/H20evFB/dChykVcPXqr79UQDPFOoToyNP XWYDiBrYaGcR/BscCpu257ZtPNz2y0kymcf2hP0SedFr8/1jXKEoR1iI52dpd03qfgmk E/sg== X-Gm-Message-State: ACgBeo1PhP88AZB5zWxcE2cOBFlVNc2evVhcbEJ/vCd5vZbM1VdeUw1m UWs64BsqmZv97yobfXiSh6uOSjROSIk= X-Google-Smtp-Source: AA6agR78ZWsdlA0w/wYJiZ7veJLbcMN8CAMQ2eFCW/l3DqXQksRx/i3gIi89rfeyZHi9k1cJuY2cCA== X-Received: by 2002:a05:6000:168f:b0:225:603a:cdb1 with SMTP id y15-20020a056000168f00b00225603acdb1mr6127923wrd.569.1661342469355; Wed, 24 Aug 2022 05:01:09 -0700 (PDT) Received: from localhost.localdomain ([86.14.124.218]) by smtp.gmail.com with ESMTPSA id cc19-20020a5d5c13000000b0022571d43d32sm1697676wrb.21.2022.08.24.05.01.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Aug 2022 05:01:08 -0700 (PDT) From: herron.philip@googlemail.com X-Google-Original-From: philip.herron@embecosm.com To: gcc-patches@gcc.gnu.org Subject: [PATCH Rust front-end v2 27/37] gccrs: Add unused variable scan Date: Wed, 24 Aug 2022 12:59:46 +0100 Message-Id: <20220824115956.737931-28-philip.herron@embecosm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220824115956.737931-1-philip.herron@embecosm.com> References: <20220824115956.737931-1-philip.herron@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: philip.herron@embecosm.com Cc: gcc-rust@gcc.gnu.org, Philip Herron Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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