LCOV - code coverage report
Current view: top level - gcc/rust/checks/lints - rust-lint-marklive.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.8 % 135 128
Test Date: 2025-06-14 16:15:35 Functions: 100.0 % 13 13
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2021-2025 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                 :             : // The idea is that all reachable symbols are live, codes called
      20                 :             : // from live codes are live, and everything else is dead.
      21                 :             : 
      22                 :             : #include "rust-lint-marklive.h"
      23                 :             : #include "options.h"
      24                 :             : #include "rust-hir-full.h"
      25                 :             : #include "rust-hir-map.h"
      26                 :             : #include "rust-hir-path.h"
      27                 :             : #include "rust-name-resolver.h"
      28                 :             : #include "rust-immutable-name-resolution-context.h"
      29                 :             : #include "rust-system.h"
      30                 :             : 
      31                 :             : namespace Rust {
      32                 :             : namespace Analysis {
      33                 :             : 
      34                 :             : // This class trys to find the live symbols which can be used as
      35                 :             : // seeds in MarkLive
      36                 :             : //
      37                 :             : // 1. TODO: explicit live
      38                 :             : //    - Attribute like #[allow(dead_code)]
      39                 :             : //    - Attribute like #[lang=".."], it's not a intra-crate item.
      40                 :             : // 2. TODO: foreign item
      41                 :        8406 : class FindEntryPoint : public MarkLiveBase
      42                 :             : {
      43                 :             :   using Rust::Analysis::MarkLiveBase::visit;
      44                 :             : 
      45                 :             : public:
      46                 :        4203 :   static std::vector<HirId> find (HIR::Crate &crate)
      47                 :             :   {
      48                 :        8406 :     FindEntryPoint findEntryPoint;
      49                 :       19779 :     for (auto &it : crate.get_items ())
      50                 :       15576 :       it->accept_vis (findEntryPoint);
      51                 :        4203 :     return findEntryPoint.getEntryPoint ();
      52                 :        4203 :   }
      53                 :             : 
      54                 :             :   // TODO not only fn main can be a entry point.
      55                 :        5694 :   void visit (HIR::Function &function) override
      56                 :             :   {
      57                 :        5694 :     if (function.get_function_name ().as_string () == "main")
      58                 :             :       {
      59                 :        3702 :         entryPoints.push_back (function.get_mappings ().get_hirid ());
      60                 :             :       }
      61                 :        5694 :   }
      62                 :             : 
      63                 :             : private:
      64                 :        4203 :   FindEntryPoint () : MarkLiveBase () {}
      65                 :             :   std::vector<HirId> entryPoints;
      66                 :        4203 :   std::vector<HirId> getEntryPoint () { return entryPoints; }
      67                 :             : };
      68                 :             : 
      69                 :             : std::set<HirId>
      70                 :        4203 : MarkLive::Analysis (HIR::Crate &crate)
      71                 :             : {
      72                 :        4203 :   MarkLive marklive (FindEntryPoint::find (crate));
      73                 :        4203 :   marklive.go (crate);
      74                 :             : 
      75                 :        4203 :   return marklive.liveSymbols;
      76                 :        4203 : }
      77                 :             : 
      78                 :             : // pop a live symbol from worklist every iteration,
      79                 :             : // if it's a function then walk the function body, and
      80                 :             : // 1. save all the live symbols in worklist which is
      81                 :             : //    visited first time
      82                 :             : // 2. save all the live symbols in liveSymbols
      83                 :             : void
      84                 :        4203 : MarkLive::go (HIR::Crate &)
      85                 :             : {
      86                 :       38097 :   while (!worklist.empty ())
      87                 :             :     {
      88                 :       33894 :       HirId hirId = worklist.back ();
      89                 :       33894 :       worklist.pop_back ();
      90                 :       33894 :       scannedSymbols.emplace (hirId);
      91                 :       33894 :       liveSymbols.emplace (hirId);
      92                 :       33894 :       if (auto item = mappings.lookup_hir_item (hirId))
      93                 :       14166 :         item.value ()->accept_vis (*this);
      94                 :       19728 :       else if (auto implItem = mappings.lookup_hir_implitem (hirId))
      95                 :         956 :         implItem->first->accept_vis (*this);
      96                 :             :     }
      97                 :        4203 : }
      98                 :             : 
      99                 :             : void
     100                 :       37749 : MarkLive::visit (HIR::PathInExpression &expr)
     101                 :             : {
     102                 :             :   // We should iterate every path segment in order to mark the struct which
     103                 :             :   // is used in expression like Foo::bar(), we should mark the Foo alive.
     104                 :       37749 :   if (!expr.is_lang_item ())
     105                 :       37695 :     expr.iterate_path_segments ([&] (HIR::PathExprSegment &seg) -> bool {
     106                 :       40713 :       return visit_path_segment (seg);
     107                 :             :     });
     108                 :             : 
     109                 :             :   // after iterate the path segments, we should mark functions and associated
     110                 :             :   // functions alive.
     111                 :       37749 :   NodeId ast_node_id = expr.get_mappings ().get_nodeid ();
     112                 :       37749 :   NodeId ref_node_id = UNKNOWN_NODEID;
     113                 :             : 
     114                 :       37749 :   if (expr.is_lang_item ())
     115                 :          54 :     ref_node_id
     116                 :          54 :       = Analysis::Mappings::get ().get_lang_item_node (expr.get_lang_item ());
     117                 :             :   else
     118                 :       37695 :     find_ref_node_id (ast_node_id, ref_node_id);
     119                 :             : 
     120                 :             :   // node back to HIR
     121                 :       37749 :   tl::optional<HirId> hid = mappings.lookup_node_to_hir (ref_node_id);
     122                 :       37749 :   rust_assert (hid.has_value ());
     123                 :       37749 :   auto ref = hid.value ();
     124                 :             : 
     125                 :             :   // it must resolve to some kind of HIR::Item or HIR::InheritImplItem
     126                 :       37749 :   if (auto resolved_item = mappings.lookup_hir_item (ref))
     127                 :        5866 :     mark_hir_id (resolved_item.value ()->get_mappings ().get_hirid ());
     128                 :       31883 :   else if (auto resolved_item = mappings.lookup_hir_implitem (ref))
     129                 :         506 :     mark_hir_id (resolved_item->first->get_impl_mappings ().get_hirid ());
     130                 :       37749 : }
     131                 :             : 
     132                 :             : void
     133                 :        1033 : MarkLive::visit (HIR::MethodCallExpr &expr)
     134                 :             : {
     135                 :        1033 :   expr.get_receiver ().accept_vis (*this);
     136                 :        1033 :   visit_path_segment (expr.get_method_name ());
     137                 :        1331 :   for (auto &argument : expr.get_arguments ())
     138                 :         298 :     argument->accept_vis (*this);
     139                 :             : 
     140                 :             :   // Trying to find the method definition and mark it alive.
     141                 :        1033 :   NodeId ast_node_id = expr.get_mappings ().get_nodeid ();
     142                 :        1033 :   NodeId ref_node_id = UNKNOWN_NODEID;
     143                 :        1033 :   find_ref_node_id (ast_node_id, ref_node_id);
     144                 :             : 
     145                 :             :   // node back to HIR
     146                 :        1033 :   if (auto hid = mappings.lookup_node_to_hir (ref_node_id))
     147                 :        1033 :     mark_hir_id (*hid);
     148                 :             :   else
     149                 :           0 :     rust_unreachable ();
     150                 :        1033 : }
     151                 :             : 
     152                 :             : bool
     153                 :       41746 : MarkLive::visit_path_segment (HIR::PathExprSegment seg)
     154                 :             : {
     155                 :       41746 :   NodeId ast_node_id = seg.get_mappings ().get_nodeid ();
     156                 :       41746 :   NodeId ref_node_id = UNKNOWN_NODEID;
     157                 :             : 
     158                 :             :   // There are two different kinds of segment for us.
     159                 :             :   // 1. function segment
     160                 :             :   //      like the symbol "foo" in expression `foo()`.
     161                 :             :   // 2. type segment
     162                 :             :   //      like the symbol "Foo" in expression `Foo{a: 1, b: 2}`
     163                 :             :   //
     164                 :             :   // We should mark them alive all and ignoring other kind of segments.
     165                 :             :   // If the segment we dont care then just return false is fine
     166                 :       41746 :   if (flag_name_resolution_2_0)
     167                 :             :     {
     168                 :        3923 :       auto &nr_ctx
     169                 :        3923 :         = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
     170                 :             : 
     171                 :        3923 :       if (auto id = nr_ctx.lookup (ast_node_id))
     172                 :        3740 :         ref_node_id = *id;
     173                 :             :       else
     174                 :         183 :         return false;
     175                 :             :     }
     176                 :       37823 :   else if (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id))
     177                 :             :     {
     178                 :        4990 :       if (!resolver->lookup_resolved_type (ast_node_id, &ref_node_id))
     179                 :             :         return false;
     180                 :             :     }
     181                 :       40006 :   if (auto hid = mappings.lookup_node_to_hir (ref_node_id))
     182                 :             :     {
     183                 :       40006 :       mark_hir_id (*hid);
     184                 :       40006 :       return true;
     185                 :             :     }
     186                 :           0 :   rust_unreachable ();
     187                 :             : }
     188                 :             : 
     189                 :             : void
     190                 :        3296 : MarkLive::visit (HIR::FieldAccessExpr &expr)
     191                 :             : {
     192                 :             :   // visit receiver at first
     193                 :        3296 :   expr.get_receiver_expr ().accept_vis (*this);
     194                 :             : 
     195                 :             :   // resolve the receiver back to ADT type
     196                 :        3296 :   TyTy::BaseType *receiver = nullptr;
     197                 :        3296 :   if (!tyctx->lookup_type (
     198                 :        3296 :         expr.get_receiver_expr ().get_mappings ().get_hirid (), &receiver))
     199                 :             :     {
     200                 :           0 :       rust_error_at (expr.get_receiver_expr ().get_locus (),
     201                 :             :                      "unresolved type for receiver");
     202                 :             :     }
     203                 :             : 
     204                 :        3296 :   TyTy::ADTType *adt = nullptr;
     205                 :        3296 :   if (receiver->get_kind () == TyTy::TypeKind::ADT)
     206                 :             :     {
     207                 :        3218 :       adt = static_cast<TyTy::ADTType *> (receiver);
     208                 :             :     }
     209                 :          78 :   else if (receiver->get_kind () == TyTy::TypeKind::REF)
     210                 :             :     {
     211                 :          78 :       TyTy::ReferenceType *r = static_cast<TyTy::ReferenceType *> (receiver);
     212                 :          78 :       TyTy::BaseType *b = r->get_base ();
     213                 :          78 :       rust_assert (b->get_kind () == TyTy::TypeKind::ADT);
     214                 :             : 
     215                 :             :       adt = static_cast<TyTy::ADTType *> (b);
     216                 :             :     }
     217                 :             : 
     218                 :        3218 :   rust_assert (adt != nullptr);
     219                 :        3296 :   rust_assert (!adt->is_enum ());
     220                 :        3296 :   rust_assert (adt->number_of_variants () == 1);
     221                 :             : 
     222                 :        3296 :   TyTy::VariantDef *variant = adt->get_variants ().at (0);
     223                 :             : 
     224                 :             :   // get the field index
     225                 :        3296 :   size_t index;
     226                 :        3296 :   TyTy::StructFieldType *field;
     227                 :        3296 :   bool ok = variant->lookup_field (expr.get_field_name ().as_string (), &field,
     228                 :             :                                    &index);
     229                 :        3296 :   rust_assert (ok);
     230                 :        3296 :   if (index >= variant->num_fields ())
     231                 :             :     {
     232                 :           0 :       rust_error_at (expr.get_receiver_expr ().get_locus (),
     233                 :             :                      "cannot access struct %s by index: %lu",
     234                 :           0 :                      adt->get_name ().c_str (), (unsigned long) index);
     235                 :           0 :       return;
     236                 :             :     }
     237                 :             : 
     238                 :             :   // get the field hir id
     239                 :        3296 :   HirId field_id = field->get_ref ();
     240                 :        3296 :   mark_hir_id (field_id);
     241                 :             : }
     242                 :             : 
     243                 :             : void
     244                 :         587 : MarkLive::visit (HIR::TupleIndexExpr &expr)
     245                 :             : {
     246                 :             :   // TODO: unused tuple field detection
     247                 :         587 :   expr.get_tuple_expr ().accept_vis (*this);
     248                 :         587 : }
     249                 :             : 
     250                 :             : void
     251                 :          16 : MarkLive::visit (HIR::TypeAlias &alias)
     252                 :             : {
     253                 :          16 :   NodeId ast_node_id = UNKNOWN_NODEID;
     254                 :          16 :   if (flag_name_resolution_2_0)
     255                 :             :     {
     256                 :           2 :       auto &nr_ctx
     257                 :           2 :         = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
     258                 :             : 
     259                 :           2 :       if (auto id = nr_ctx.lookup (
     260                 :           4 :             alias.get_type_aliased ().get_mappings ().get_nodeid ()))
     261                 :           2 :         ast_node_id = *id;
     262                 :             :     }
     263                 :             :   else
     264                 :             :     {
     265                 :          14 :       resolver->lookup_resolved_type (
     266                 :          14 :         alias.get_type_aliased ().get_mappings ().get_nodeid (), &ast_node_id);
     267                 :             :     }
     268                 :             : 
     269                 :          16 :   if (auto hid = mappings.lookup_node_to_hir (ast_node_id))
     270                 :          16 :     mark_hir_id (*hid);
     271                 :             :   else
     272                 :           0 :     rust_unreachable ();
     273                 :          16 : }
     274                 :             : 
     275                 :             : void
     276                 :       50723 : MarkLive::mark_hir_id (HirId id)
     277                 :             : {
     278                 :       50723 :   if (scannedSymbols.find (id) == scannedSymbols.end ())
     279                 :             :     {
     280                 :       30192 :       worklist.push_back (id);
     281                 :             :     }
     282                 :       50723 :   liveSymbols.emplace (id);
     283                 :       50723 : }
     284                 :             : 
     285                 :             : void
     286                 :       38728 : MarkLive::find_ref_node_id (NodeId ast_node_id, NodeId &ref_node_id)
     287                 :             : {
     288                 :       38728 :   if (flag_name_resolution_2_0)
     289                 :             :     {
     290                 :        3408 :       auto &nr_ctx
     291                 :        3408 :         = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
     292                 :             : 
     293                 :        3408 :       nr_ctx.lookup (ast_node_id).map ([&ref_node_id] (NodeId resolved) {
     294                 :        3408 :         ref_node_id = resolved;
     295                 :             :       });
     296                 :             :     }
     297                 :             :   else
     298                 :             :     {
     299                 :       35320 :       if (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id))
     300                 :             :         {
     301                 :        2259 :           if (!resolver->lookup_resolved_type (ast_node_id, &ref_node_id))
     302                 :             :             {
     303                 :           2 :               bool ok
     304                 :           2 :                 = resolver->lookup_resolved_misc (ast_node_id, &ref_node_id);
     305                 :           2 :               rust_assert (ok);
     306                 :             :             }
     307                 :             :         }
     308                 :             :     }
     309                 :       38728 : }
     310                 :             : 
     311                 :             : } // namespace Analysis
     312                 :             : } // 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.