LCOV - code coverage report
Current view: top level - gcc/rust/checks/errors/privacy - rust-pub-restricted-visitor.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 90.9 % 66 60
Test Date: 2024-04-20 14:03:02 Functions: 88.2 % 17 15
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2020-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-pub-restricted-visitor.h"
      20                 :             : #include "rust-hir.h"
      21                 :             : #include "rust-hir-item.h"
      22                 :             : 
      23                 :             : namespace Rust {
      24                 :             : namespace Privacy {
      25                 :             : 
      26                 :             : bool
      27                 :       13384 : PubRestrictedVisitor::is_restriction_valid (NodeId item_id,
      28                 :             :                                             const location_t locus)
      29                 :             : {
      30                 :       13384 :   ModuleVisibility visibility;
      31                 :             : 
      32                 :             :   // If there is no visibility in the mappings, then the item is private and
      33                 :             :   // does not contain any restriction
      34                 :             :   // FIXME: Is that correct?
      35                 :       13384 :   if (!mappings.lookup_visibility (item_id, visibility))
      36                 :             :     return true;
      37                 :             : 
      38                 :        9112 :   for (auto mod = module_stack.rbegin (); mod != module_stack.rend (); mod++)
      39                 :        9327 :     if (*mod == visibility.get_module_id ())
      40                 :       13382 :       return true;
      41                 :             : 
      42                 :           2 :   rust_error_at (locus, "restricted path is not an ancestor of the "
      43                 :             :                         "current module");
      44                 :           2 :   return false;
      45                 :             : }
      46                 :             : 
      47                 :        3348 : PubRestrictedVisitor::PubRestrictedVisitor (Analysis::Mappings &mappings)
      48                 :        3348 :   : mappings (mappings)
      49                 :        3348 : {}
      50                 :             : 
      51                 :             : void
      52                 :        3348 : PubRestrictedVisitor::go (HIR::Crate &crate)
      53                 :             : {
      54                 :             :   // The `crate` module will always be present
      55                 :        3348 :   module_stack.emplace_back (crate.get_mappings ().get_defid ());
      56                 :             : 
      57                 :             :   // FIXME: When do we insert `super`? `self`?
      58                 :             :   // We need wrapper function for these
      59                 :             : 
      60                 :       16202 :   for (auto &item : crate.get_items ())
      61                 :             :     {
      62                 :       12854 :       if (item->get_hir_kind () == HIR::Node::VIS_ITEM)
      63                 :             :         {
      64                 :       12854 :           auto vis_item = static_cast<HIR::VisItem *> (item.get ());
      65                 :       12854 :           vis_item->accept_vis (*this);
      66                 :             :         }
      67                 :             :     }
      68                 :        3348 : }
      69                 :             : 
      70                 :             : void
      71                 :         425 : PubRestrictedVisitor::visit (HIR::Module &mod)
      72                 :             : {
      73                 :             :   // FIXME: We need to update `super` and `self` here
      74                 :         425 :   module_stack.push_back (mod.get_mappings ().get_defid ());
      75                 :             : 
      76                 :         425 :   is_restriction_valid (mod.get_mappings ().get_nodeid (), mod.get_locus ());
      77                 :             : 
      78                 :         955 :   for (auto &item : mod.get_items ())
      79                 :             :     {
      80                 :         530 :       if (item->get_hir_kind () == HIR::Node::VIS_ITEM)
      81                 :             :         {
      82                 :         530 :           auto vis_item = static_cast<HIR::VisItem *> (item.get ());
      83                 :         530 :           vis_item->accept_vis (*this);
      84                 :             :         }
      85                 :             :     }
      86                 :             : 
      87                 :         425 :   module_stack.pop_back ();
      88                 :         425 : }
      89                 :             : 
      90                 :             : void
      91                 :           0 : PubRestrictedVisitor::visit (HIR::ExternCrate &crate)
      92                 :             : {
      93                 :           0 :   is_restriction_valid (crate.get_mappings ().get_nodeid (),
      94                 :             :                         crate.get_locus ());
      95                 :           0 : }
      96                 :             : 
      97                 :             : void
      98                 :           0 : PubRestrictedVisitor::visit (HIR::UseDeclaration &use_decl)
      99                 :             : {
     100                 :           0 :   is_restriction_valid (use_decl.get_mappings ().get_nodeid (),
     101                 :             :                         use_decl.get_locus ());
     102                 :           0 : }
     103                 :             : 
     104                 :             : void
     105                 :        4817 : PubRestrictedVisitor::visit (HIR::Function &func)
     106                 :             : {
     107                 :        4817 :   is_restriction_valid (func.get_mappings ().get_nodeid (), func.get_locus ());
     108                 :        4817 : }
     109                 :             : 
     110                 :             : void
     111                 :          32 : PubRestrictedVisitor::visit (HIR::TypeAlias &type_alias)
     112                 :             : {
     113                 :          32 :   is_restriction_valid (type_alias.get_mappings ().get_nodeid (),
     114                 :             :                         type_alias.get_locus ());
     115                 :          32 : }
     116                 :             : 
     117                 :             : void
     118                 :         823 : PubRestrictedVisitor::visit (HIR::StructStruct &struct_item)
     119                 :             : {
     120                 :         823 :   is_restriction_valid (struct_item.get_mappings ().get_nodeid (),
     121                 :             :                         struct_item.get_locus ());
     122                 :             :   // FIXME: Check fields here as well
     123                 :         823 : }
     124                 :             : 
     125                 :             : void
     126                 :         655 : PubRestrictedVisitor::visit (HIR::TupleStruct &tuple_struct)
     127                 :             : {
     128                 :         655 :   is_restriction_valid (tuple_struct.get_mappings ().get_nodeid (),
     129                 :             :                         tuple_struct.get_locus ());
     130                 :             :   // FIXME: Check fields here as well
     131                 :         655 : }
     132                 :             : 
     133                 :             : void
     134                 :         155 : PubRestrictedVisitor::visit (HIR::Enum &enum_item)
     135                 :             : {
     136                 :         155 :   is_restriction_valid (enum_item.get_mappings ().get_nodeid (),
     137                 :             :                         enum_item.get_locus ());
     138                 :         155 : }
     139                 :             : 
     140                 :             : void
     141                 :          89 : PubRestrictedVisitor::visit (HIR::Union &union_item)
     142                 :             : {
     143                 :          89 :   is_restriction_valid (union_item.get_mappings ().get_nodeid (),
     144                 :             :                         union_item.get_locus ());
     145                 :          89 : }
     146                 :             : 
     147                 :             : void
     148                 :         371 : PubRestrictedVisitor::visit (HIR::ConstantItem &const_item)
     149                 :             : {
     150                 :         371 :   is_restriction_valid (const_item.get_mappings ().get_nodeid (),
     151                 :             :                         const_item.get_locus ());
     152                 :         371 : }
     153                 :             : 
     154                 :             : void
     155                 :          40 : PubRestrictedVisitor::visit (HIR::StaticItem &static_item)
     156                 :             : {
     157                 :          40 :   is_restriction_valid (static_item.get_mappings ().get_nodeid (),
     158                 :             :                         static_item.get_locus ());
     159                 :          40 : }
     160                 :             : 
     161                 :             : void
     162                 :        2002 : PubRestrictedVisitor::visit (HIR::Trait &trait)
     163                 :             : {
     164                 :        2002 :   is_restriction_valid (trait.get_mappings ().get_nodeid (),
     165                 :             :                         trait.get_locus ());
     166                 :        2002 : }
     167                 :             : 
     168                 :             : void
     169                 :        2970 : PubRestrictedVisitor::visit (HIR::ImplBlock &impl)
     170                 :             : {
     171                 :        2970 :   is_restriction_valid (impl.get_mappings ().get_nodeid (), impl.get_locus ());
     172                 :        2970 : }
     173                 :             : 
     174                 :             : void
     175                 :        1005 : PubRestrictedVisitor::visit (HIR::ExternBlock &block)
     176                 :             : {
     177                 :        1005 :   is_restriction_valid (block.get_mappings ().get_nodeid (),
     178                 :             :                         block.get_locus ());
     179                 :        1005 : }
     180                 :             : 
     181                 :             : } // namespace Privacy
     182                 :             : } // 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.