LCOV - code coverage report
Current view: top level - gcc/rust/checks/errors/feature - rust-feature-collector.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 100.0 % 43 43
Test Date: 2026-11-14 16:39:50 Functions: 100.0 % 8 8
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // Copyright (C) 2026 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-feature-collector.h"
      20              : #include "rust-attribute-values.h"
      21              : #include "rust-session-manager.h"
      22              : 
      23              : namespace Rust {
      24              : namespace Features {
      25              : 
      26         5265 : FeatureCollector::FeatureCollector () : features (CrateFeatures{UNKNOWN_NODEID})
      27         5265 : {}
      28              : 
      29              : CrateFeatures
      30         5265 : FeatureCollector::collect (AST::Crate &crate)
      31              : {
      32         5265 :   features.valid_lang_features.clear ();
      33         5265 :   features.valid_lib_features.clear ();
      34         5265 :   features.crate_id = crate.get_node_id ();
      35              : 
      36         5265 :   visit (crate);
      37              : 
      38         5265 :   return features;
      39              : }
      40              : 
      41              : namespace {
      42              : bool
      43        14585 : is_feature_attribute (const AST::Attribute &attribute)
      44              : {
      45        14585 :   return Values::Attributes::FEATURE == attribute.get_path ().as_string ();
      46              : }
      47              : 
      48              : // check for empty feature, such as `#![feature], this is an error
      49              : bool
      50         8743 : is_valid_feature_attribute (const AST::Attribute &attribute)
      51              : {
      52         8743 :   return !attribute.empty_input ();
      53              : }
      54              : } // namespace
      55              : 
      56              : void
      57         8715 : FeatureCollector::add_features_from_token_tree (
      58              :   const AST::DelimTokenTree &delim_ttree)
      59              : {
      60         8715 :   std::unique_ptr<AST::AttrInputMetaItemContainer> meta_item (
      61         8715 :     delim_ttree.parse_to_meta_item ());
      62         8715 :   add_features_from_meta_item_container (*meta_item);
      63         8715 : }
      64              : 
      65              : void
      66         8742 : FeatureCollector::add_features_from_meta_item_container (
      67              :   const AST::AttrInputMetaItemContainer &meta_item_container)
      68              : {
      69        17982 :   for (const auto &item : meta_item_container.get_items ())
      70              :     {
      71         9240 :       const auto &name_str = item->as_string ();
      72              : 
      73              :       // TODO: detect duplicates
      74         9240 :       if (auto tname = Feature::as_name (name_str))
      75         8955 :         features.valid_lang_features.insert (*tname);
      76              :       else
      77          285 :         features.valid_lib_features.emplace (name_str, item->get_locus ());
      78         9240 :     }
      79         8742 : }
      80              : 
      81              : void
      82        14585 : FeatureCollector::identify_feature (const AST::Attribute &attribute)
      83              : {
      84        14585 :   if (is_feature_attribute (attribute))
      85              :     {
      86         8743 :       if (!is_valid_feature_attribute (attribute))
      87              :         {
      88            1 :           rust_error_at (attribute.get_locus (), ErrorCode::E0556,
      89              :                          "malformed %<feature%> attribute input");
      90            1 :           return;
      91              :         }
      92         8742 :       const auto &attr_input = attribute.get_attr_input ();
      93         8742 :       auto type = attr_input.get_attr_input_type ();
      94         8742 :       if (type == AST::AttrInput::AttrInputType::TOKEN_TREE)
      95              :         {
      96         8715 :           const auto &delim_ttree = static_cast<const AST::DelimTokenTree &> (
      97         8715 :             attribute.get_attr_input ());
      98         8715 :           add_features_from_token_tree (delim_ttree);
      99              :         }
     100           27 :       else if (type == AST::AttrInput::AttrInputType::META_ITEM)
     101              :         {
     102              :           // We can find a meta item in #[cfg(toto),feature(xxxx)]
     103           27 :           const auto &meta_item_container
     104              :             = static_cast<const AST::AttrInputMetaItemContainer &> (attr_input);
     105           27 :           add_features_from_meta_item_container (meta_item_container);
     106              :         }
     107              :     }
     108              : }
     109              : 
     110              : void
     111         5265 : FeatureCollector::visit (AST::Crate &crate)
     112              : {
     113        19850 :   for (const auto &attribute : crate.inner_attrs)
     114        14585 :     identify_feature (attribute);
     115         5265 : }
     116              : 
     117              : } // namespace Features
     118              : } // namespace Rust
        

Generated by: LCOV version 2.5.0-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.