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: 721 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp1390060wro; Wed, 24 Aug 2022 05:17:59 -0700 (PDT) X-Google-Smtp-Source: AA6agR5RYsotZS4kPqxop/fineSszlnN29pKNyn1mIGQk/7c7CZNcZZg4Jaa/qv9L8uaMQG9/sfv X-Received: by 2002:a17:907:7632:b0:73d:c346:de57 with SMTP id jy18-20020a170907763200b0073dc346de57mr771167ejc.647.1661343479400; Wed, 24 Aug 2022 05:17:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661343479; cv=none; d=google.com; s=arc-20160816; b=SEh9k2GMuHQa7mFNU1/s4mad3vuzwkYtdxVsxbRGLz7q97wa9nnWwA5nmwxV+njXTI 89dWOonUMQwR5wSxV3ZejkMczsWjRlM5L0Z4ImbIAxuZBsSFDFpDGrf+zp9Ngl4gzYmh HCiuV5hog5liYLd2596zXqPWYOyI35YcWIg2GDzYACcIz676EJ4CxihWDHboZ+3ewAT5 /PUARJejgNp2ideCDXQkc4qGNaLUDQqUN6SVWEJkbnbEc3lIMoZ/1teGgnj3aZFLXqZE zxtSeCTdSJk0l3N/cmYsPMmc7pKWavmK3hbYvkNG3cLZGlfPKNz2qbLBsbWm4H8CbXIe By7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:dkim-signature:dmarc-filter :delivered-to; bh=2+xQFlaMBp9eqjhcZ8kafuZJmQUKrkM4HDGSADjdv04=; b=lMIMN2uJw9mbgEOFBeKOEsLcBZ1BUCr/E0CwLbpqCDixUc2eJ4IexIUxy839DVEAU1 ZhRJ67TXPy+/atAgaTBtIIMWYEDHsaDSBg0kvAux2XVolwkjLU1fBQxiZUbQ/oN2hFuw f/6xIWd5kJLzrsPzWJRLgg6xAKnkrfIdfMeDnXjNqWVeTi/JkwUi/GUI/vYS67yDMIXn EdYfnJ3/4QkqDq/tXwz9U+7LOV9eBntAEMLA7C8JIq4/yR2/chkoOQRxJybxL5UKQKR6 XywJvSLQ8hEZjxL4+oixuUpy3frxsHM294XshWz+8xq+JfnRpDAX++KQbPqjm4bcKQ0u EMDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@googlemail.com header.s=20210112 header.b=GZNqsoOg; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id oz6-20020a1709077d8600b006ff49b183e9si1938256ejc.971.2022.08.24.05.17.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Aug 2022 05:17:59 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@googlemail.com header.s=20210112 header.b=GZNqsoOg; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 231C73831771 for ; Wed, 24 Aug 2022 12:06:49 +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-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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1742044900294215422?= X-GMAIL-MSGID: =?utf-8?q?1742044900294215422?= 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