LCOV - code coverage report
Current view: top level - gcc/rust/checks/lints - rust-lint-unused-var.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 100.0 % 27 27
Test Date: 2024-04-13 14:00:49 Functions: 100.0 % 3 3
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2021-2024 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                 :       31219 : check_decl (tree *t)
      27                 :             : {
      28                 :       31219 :   rust_assert (TREE_CODE (*t) == VAR_DECL || TREE_CODE (*t) == PARM_DECL
      29                 :             :                || TREE_CODE (*t) == CONST_DECL);
      30                 :             : 
      31                 :       31219 :   tree var_name = DECL_NAME (*t);
      32                 :       31219 :   const char *var_name_ptr = IDENTIFIER_POINTER (var_name);
      33                 :       31219 :   bool starts_with_under_score = strncmp (var_name_ptr, "_", 1) == 0;
      34                 :             : 
      35                 :       31219 :   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                 :       31219 :   if (!TREE_USED (*t) && !DECL_ARTIFICIAL (*t) && !starts_with_under_score)
      47                 :             :     {
      48                 :        4058 :       warning_at (DECL_SOURCE_LOCATION (*t),
      49                 :             :                   is_constant ? OPT_Wunused_const_variable_
      50                 :             :                               : OPT_Wunused_variable,
      51                 :             :                   "unused name %qE", *t);
      52                 :             :     }
      53                 :       31219 : }
      54                 :             : 
      55                 :             : static tree
      56                 :      217521 : unused_var_walk_fn (tree *t, int *, void *)
      57                 :             : {
      58                 :      217521 :   switch (TREE_CODE (*t))
      59                 :             :     {
      60                 :       24131 :     case VAR_DECL:
      61                 :       24131 :     case CONST_DECL:
      62                 :       24131 :       check_decl (t);
      63                 :       24131 :       break;
      64                 :             : 
      65                 :             :     default:
      66                 :             :       break;
      67                 :             :     }
      68                 :      217521 :   return NULL_TREE;
      69                 :             : }
      70                 :             : 
      71                 :             : void
      72                 :        3279 : UnusedVariables::Lint (Compile::Context &ctx)
      73                 :             : {
      74                 :       12761 :   for (auto &fndecl : ctx.get_func_decls ())
      75                 :             :     {
      76                 :       16084 :       for (tree p = DECL_ARGUMENTS (fndecl); p != NULL_TREE; p = DECL_CHAIN (p))
      77                 :             :         {
      78                 :        6602 :           check_decl (&p);
      79                 :             :         }
      80                 :             : 
      81                 :        9482 :       walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
      82                 :             :                                     &unused_var_walk_fn, &ctx);
      83                 :             :     }
      84                 :             : 
      85                 :        3317 :   for (auto &var : ctx.get_var_decls ())
      86                 :             :     {
      87                 :          38 :       tree decl = var->get_decl ();
      88                 :          38 :       check_decl (&decl);
      89                 :             :     }
      90                 :             : 
      91                 :        3727 :   for (auto &const_decl : ctx.get_const_decls ())
      92                 :             :     {
      93                 :         448 :       check_decl (&const_decl);
      94                 :             :     }
      95                 :        3279 : }
      96                 :             : 
      97                 :             : } // namespace Analysis
      98                 :             : } // namespace Rust
        

Generated by: LCOV version 2.1-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.