LCOV - code coverage report
Current view: top level - gcc/rust/checks/errors - rust-feature-gate.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 100.0 % 62 62
Test Date: 2024-04-20 14:03:02 Functions: 100.0 % 8 8
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-feature-gate.h"
      20                 :             : #include "rust-abi.h"
      21                 :             : #include "rust-ast-visitor.h"
      22                 :             : 
      23                 :             : namespace Rust {
      24                 :             : 
      25                 :             : void
      26                 :        3599 : FeatureGate::check (AST::Crate &crate)
      27                 :             : {
      28                 :        3599 :   visit (crate);
      29                 :        3599 : }
      30                 :             : 
      31                 :             : void
      32                 :        3599 : FeatureGate::visit (AST::Crate &crate)
      33                 :             : {
      34                 :        3599 :   valid_features.clear ();
      35                 :             : 
      36                 :        4072 :   for (const auto &attr : crate.inner_attrs)
      37                 :             :     {
      38                 :         473 :       if (attr.get_path ().as_string () == "feature")
      39                 :             :         {
      40                 :         354 :           const auto &attr_input = attr.get_attr_input ();
      41                 :         354 :           auto type = attr_input.get_attr_input_type ();
      42                 :         354 :           if (type == AST::AttrInput::AttrInputType::TOKEN_TREE)
      43                 :             :             {
      44                 :         354 :               const auto &option = static_cast<const AST::DelimTokenTree &> (
      45                 :         354 :                 attr.get_attr_input ());
      46                 :         354 :               std::unique_ptr<AST::AttrInputMetaItemContainer> meta_item (
      47                 :         354 :                 option.parse_to_meta_item ());
      48                 :         708 :               for (const auto &item : meta_item->get_items ())
      49                 :             :                 {
      50                 :         354 :                   const auto &name_str = item->as_string ();
      51                 :         354 :                   auto tname = Feature::as_name (name_str);
      52                 :         354 :                   if (tname.has_value ())
      53                 :             :                     {
      54                 :         351 :                       auto name = tname.value ();
      55                 :         351 :                       valid_features.insert (name);
      56                 :             :                     }
      57                 :             : 
      58                 :             :                   else
      59                 :           3 :                     rust_error_at (item->get_locus (), ErrorCode::E0635,
      60                 :             :                                    "unknown feature %qs", name_str.c_str ());
      61                 :         354 :                 }
      62                 :         354 :             }
      63                 :             :         }
      64                 :             :     }
      65                 :             : 
      66                 :        3599 :   AST::DefaultASTVisitor::visit (crate);
      67                 :        3599 : }
      68                 :             : 
      69                 :             : void
      70                 :         260 : FeatureGate::gate (Feature::Name name, location_t loc,
      71                 :             :                    const std::string &error_msg)
      72                 :             : {
      73                 :         260 :   if (!valid_features.count (name))
      74                 :             :     {
      75                 :           5 :       auto feature = Feature::create (name);
      76                 :           5 :       auto issue = feature.issue ();
      77                 :           5 :       if (issue > 0)
      78                 :             :         {
      79                 :           1 :           const char *fmt_str
      80                 :             :             = "%s. see issue %u "
      81                 :             :               "<https://github.com/rust-lang/rust/issues/%u> for more "
      82                 :             :               "information. add `#![feature(%s)]` to the crate attributes to "
      83                 :             :               "enable.";
      84                 :           1 :           rust_error_at (loc, ErrorCode::E0658, fmt_str, error_msg.c_str (),
      85                 :           1 :                          issue, issue, feature.as_string ().c_str ());
      86                 :             :         }
      87                 :             :       else
      88                 :             :         {
      89                 :           4 :           const char *fmt_str
      90                 :             :             = "%s. add `#![feature(%s)]` to the crate attributes to enable.";
      91                 :           4 :           rust_error_at (loc, ErrorCode::E0658, fmt_str, error_msg.c_str (),
      92                 :           4 :                          feature.as_string ().c_str ());
      93                 :             :         }
      94                 :           5 :     }
      95                 :         260 : }
      96                 :             : 
      97                 :             : void
      98                 :         902 : FeatureGate::visit (AST::ExternBlock &block)
      99                 :             : {
     100                 :         902 :   if (block.has_abi ())
     101                 :             :     {
     102                 :         902 :       const auto abi = block.get_abi ();
     103                 :             : 
     104                 :         902 :       if (get_abi_from_string (abi) == ABI::INTRINSIC)
     105                 :         152 :         gate (Feature::Name::INTRINSICS, block.get_locus (),
     106                 :             :               "intrinsics are subject to change");
     107                 :         902 :     }
     108                 :         902 :   AST::DefaultASTVisitor::visit (block);
     109                 :         902 : }
     110                 :             : 
     111                 :             : void
     112                 :        8964 : FeatureGate::check_rustc_attri (const std::vector<AST::Attribute> &attributes)
     113                 :             : {
     114                 :        9353 :   for (const AST::Attribute &attr : attributes)
     115                 :             :     {
     116                 :         389 :       auto name = attr.get_path ().as_string ();
     117                 :         389 :       if (name.rfind ("rustc_", 0) == 0)
     118                 :             :         {
     119                 :         107 :           gate (Feature::Name::RUSTC_ATTRS, attr.get_locus (),
     120                 :             :                 "internal implementation detail");
     121                 :             :         }
     122                 :         389 :     }
     123                 :        8964 : }
     124                 :             : 
     125                 :             : void
     126                 :         595 : FeatureGate::visit (AST::MacroRulesDefinition &rules_def)
     127                 :             : {
     128                 :         595 :   check_rustc_attri (rules_def.get_outer_attrs ());
     129                 :         595 : }
     130                 :             : 
     131                 :             : void
     132                 :        8369 : FeatureGate::visit (AST::Function &function)
     133                 :             : {
     134                 :        8369 :   check_rustc_attri (function.get_outer_attrs ());
     135                 :        8369 : }
     136                 :             : 
     137                 :             : void
     138                 :           1 : FeatureGate::visit (AST::ExternalTypeItem &item)
     139                 :             : {
     140                 :             :   // TODO(mxlol233): The gating needs a complete visiting chain to activate
     141                 :             :   // `AST::ExternalTypeItem`.
     142                 :           1 :   gate (Feature::Name::EXTERN_TYPES, item.get_locus (),
     143                 :             :         "extern types are experimental");
     144                 :           1 : }
     145                 :             : 
     146                 :             : } // 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.