LCOV - code coverage report
Current view: top level - gcc/rust/lex - rust-token.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 66.4 % 116 77
Test Date: 2025-11-22 14:42:49 Functions: 100.0 % 10 10
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2020-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                 :             : #include "rust-system.h"
      20                 :             : #include "rust-token.h"
      21                 :             : #include "rust-diagnostics.h"
      22                 :             : #include "rust-unicode.h"
      23                 :             : #include "rust-ast.h"
      24                 :             : 
      25                 :             : namespace Rust {
      26                 :             : // Hackily defined way to get token description for enum value using x-macros
      27                 :             : const char *
      28                 :     1783586 : get_token_description (TokenId id)
      29                 :             : {
      30                 :     1783586 :   switch (id)
      31                 :             :     {
      32                 :             : #define RS_TOKEN(name, descr)                                                  \
      33                 :             :   case name:                                                                   \
      34                 :             :     return descr;
      35                 :             : #define RS_TOKEN_KEYWORD_2015(x, y) RS_TOKEN (x, y)
      36                 :             : #define RS_TOKEN_KEYWORD_2018 RS_TOKEN_KEYWORD_2015
      37                 :     1783586 :       RS_TOKEN_LIST
      38                 :             : #undef RS_TOKEN_KEYWORD_2015
      39                 :             : #undef RS_TOKEN_KEYWORD_2018
      40                 :             : #undef RS_TOKEN
      41                 :           0 :     default:
      42                 :           0 :       rust_unreachable ();
      43                 :             :     }
      44                 :             : }
      45                 :             : 
      46                 :             : /* Hackily defined way to get token description as a string for enum value using
      47                 :             :  * x-macros */
      48                 :             : const char *
      49                 :           1 : token_id_to_str (TokenId id)
      50                 :             : {
      51                 :           1 :   switch (id)
      52                 :             :     {
      53                 :             : #define RS_TOKEN(name, _)                                                      \
      54                 :             :   case name:                                                                   \
      55                 :             :     return #name;
      56                 :             : #define RS_TOKEN_KEYWORD_2015(x, y) RS_TOKEN (x, y)
      57                 :             : #define RS_TOKEN_KEYWORD_2018 RS_TOKEN_KEYWORD_2015
      58                 :           1 :       RS_TOKEN_LIST
      59                 :             : #undef RS_TOKEN_KEYWORD_2015
      60                 :             : #undef RS_TOKEN_KEYWORD_2018
      61                 :             : #undef RS_TOKEN
      62                 :           0 :     default:
      63                 :           0 :       rust_unreachable ();
      64                 :             :     }
      65                 :             : }
      66                 :             : 
      67                 :             : /* checks if a token is a keyword */
      68                 :             : bool
      69                 :     2902617 : token_id_is_keyword (TokenId id)
      70                 :             : {
      71                 :     2902617 :   switch (id)
      72                 :             :     {
      73                 :             : #define RS_TOKEN_KEYWORD_2015(name, _) case name:
      74                 :             : #define RS_TOKEN_KEYWORD_2018 RS_TOKEN_KEYWORD_2015
      75                 :             : #define RS_TOKEN(a, b)
      76                 :             :       RS_TOKEN_LIST return true;
      77                 :             : #undef RS_TOKEN_KEYWORD_2015
      78                 :             : #undef RS_TOKEN_KEYWORD_2018
      79                 :             : #undef RS_TOKEN
      80                 :     2900549 :     default:
      81                 :     2900549 :       return false;
      82                 :             :     }
      83                 :             : }
      84                 :             : 
      85                 :             : /* gets the string associated with a keyword */
      86                 :             : const std::string &
      87                 :        1329 : token_id_keyword_string (TokenId id)
      88                 :             : {
      89                 :        1329 :   switch (id)
      90                 :             :     {
      91                 :             : #define RS_TOKEN_KEYWORD_2015(id, str_ptr)                                     \
      92                 :             :   case id:                                                                     \
      93                 :             :     {                                                                          \
      94                 :             :       static const std::string str (str_ptr);                                  \
      95                 :             :       return str;                                                              \
      96                 :             :     }                                                                          \
      97                 :             :     rust_unreachable ();
      98                 :             : #define RS_TOKEN_KEYWORD_2018 RS_TOKEN_KEYWORD_2015
      99                 :             : #define RS_TOKEN(a, b)
     100                 :        1329 :       RS_TOKEN_LIST
     101                 :             : #undef RS_TOKEN_KEYWORD_2015
     102                 :             : #undef RS_TOKEN_KEYWORD_2018
     103                 :             : #undef RS_TOKEN
     104                 :           0 :     default:
     105                 :           0 :       rust_unreachable ();
     106                 :             :     }
     107                 :             : }
     108                 :             : 
     109                 :             : const char *
     110                 :          80 : get_type_hint_string (PrimitiveCoreType type)
     111                 :             : {
     112                 :          80 :   switch (type)
     113                 :             :     {
     114                 :             :     case CORETYPE_BOOL:
     115                 :             :       return "bool";
     116                 :           0 :     case CORETYPE_CHAR:
     117                 :           0 :       return "char";
     118                 :           0 :     case CORETYPE_STR:
     119                 :           0 :       return "str";
     120                 :             :     // case CORETYPE_INT:
     121                 :           0 :     case CORETYPE_ISIZE:
     122                 :           0 :       return "isize";
     123                 :             :     // case CORETYPE_UINT:
     124                 :          32 :     case CORETYPE_USIZE:
     125                 :          32 :       return "usize";
     126                 :          42 :     case CORETYPE_F32:
     127                 :          42 :       return "f32";
     128                 :           0 :     case CORETYPE_F64:
     129                 :           0 :       return "f64";
     130                 :           0 :     case CORETYPE_I8:
     131                 :           0 :       return "i8";
     132                 :           0 :     case CORETYPE_I16:
     133                 :           0 :       return "i16";
     134                 :           1 :     case CORETYPE_I32:
     135                 :           1 :       return "i32";
     136                 :           0 :     case CORETYPE_I64:
     137                 :           0 :       return "i64";
     138                 :           0 :     case CORETYPE_I128:
     139                 :           0 :       return "i128";
     140                 :           2 :     case CORETYPE_U8:
     141                 :           2 :       return "u8";
     142                 :           2 :     case CORETYPE_U16:
     143                 :           2 :       return "u16";
     144                 :           1 :     case CORETYPE_U32:
     145                 :           1 :       return "u32";
     146                 :           0 :     case CORETYPE_U64:
     147                 :           0 :       return "u64";
     148                 :           0 :     case CORETYPE_U128:
     149                 :           0 :       return "u128";
     150                 :           0 :     case CORETYPE_PURE_DECIMAL:
     151                 :           0 :       return "pure_decimal";
     152                 :           0 :     case CORETYPE_UNKNOWN:
     153                 :           0 :     default:
     154                 :           0 :       return "unknown";
     155                 :             :     }
     156                 :             : }
     157                 :             : 
     158                 :             : const char *
     159                 :          80 : Token::get_type_hint_str () const
     160                 :             : {
     161                 :          80 :   return get_type_hint_string (type_hint);
     162                 :             : }
     163                 :             : 
     164                 :             : std::string
     165                 :      522898 : nfc_normalize_token_string (location_t loc, TokenId id, const std::string &str)
     166                 :             : {
     167                 :      522898 :   if (id == IDENTIFIER || id == LIFETIME)
     168                 :             :     {
     169                 :      358824 :       tl::optional<Utf8String> ustring = Utf8String::make_utf8_string (str);
     170                 :      358824 :       if (ustring.has_value ())
     171                 :      358824 :         return ustring.value ().nfc_normalize ().as_string ();
     172                 :             :       else
     173                 :           0 :         rust_internal_error_at (loc,
     174                 :             :                                 "identifier '%s' is not a valid UTF-8 string",
     175                 :             :                                 str.c_str ());
     176                 :      358824 :     }
     177                 :             :   else
     178                 :      164074 :     return str;
     179                 :             : }
     180                 :             : 
     181                 :             : namespace {
     182                 :             : enum class Context
     183                 :             : {
     184                 :             :   String,
     185                 :             :   Char
     186                 :             : };
     187                 :             : 
     188                 :             : const std::map<char, std::string> matches = {
     189                 :             :   {'\t', "\\t"}, {'\n', "\\n"},      {'\r', "\\r"},
     190                 :             :   {'\0', "\\0"}, {'\\', "\\\\"}, {'\v', "\\v"},
     191                 :             : };
     192                 :             : 
     193                 :             : std::string
     194                 :        3123 : escape_special_chars (const std::string &source, Context ctx)
     195                 :             : {
     196                 :        3123 :   std::stringstream stream;
     197                 :        3123 :   decltype (matches)::const_iterator result;
     198                 :       26118 :   for (char c : source)
     199                 :             :     {
     200                 :             :       // FIXME: #2411 Also replace escaped unicode values and \x digits
     201                 :       22995 :       if ((result = matches.find (c)) != matches.end ())
     202                 :         132 :         stream << result->second;
     203                 :       22863 :       else if (c == '\'' && ctx == Context::Char)
     204                 :           0 :         stream << "\\'";
     205                 :       22863 :       else if (c == '"' && ctx == Context::String)
     206                 :          16 :         stream << "\\\"";
     207                 :             :       else
     208                 :       22847 :         stream << c;
     209                 :             :     }
     210                 :             : 
     211                 :        3123 :   return stream.str ();
     212                 :        3123 : }
     213                 :             : 
     214                 :             : } // namespace
     215                 :             : 
     216                 :             : TokenPtr
     217                 :           1 : Token::make_identifier (const Identifier &ident)
     218                 :             : {
     219                 :           1 :   std::string str = ident;
     220                 :           1 :   return make_identifier (ident.get_locus (), std::move (str));
     221                 :           1 : }
     222                 :             : 
     223                 :             : std::string
     224                 :       73374 : Token::as_string () const
     225                 :             : {
     226                 :       73374 :   if (should_have_str ())
     227                 :             :     {
     228                 :       21922 :       switch (get_id ())
     229                 :             :         {
     230                 :        3032 :         case STRING_LITERAL:
     231                 :        6064 :           return "\"" + escape_special_chars (get_str (), Context::String)
     232                 :        3032 :                  + "\"";
     233                 :          14 :         case BYTE_STRING_LITERAL:
     234                 :          28 :           return "b\"" + escape_special_chars (get_str (), Context::String)
     235                 :          14 :                  + "\"";
     236                 :           0 :         case RAW_STRING_LITERAL:
     237                 :           0 :           return "r\"" + escape_special_chars (get_str (), Context::String)
     238                 :           0 :                  + "\"";
     239                 :          49 :         case CHAR_LITERAL:
     240                 :          98 :           return "'" + escape_special_chars (get_str (), Context::Char) + "'";
     241                 :          28 :         case BYTE_CHAR_LITERAL:
     242                 :          56 :           return "b'" + escape_special_chars (get_str (), Context::Char) + "'";
     243                 :         942 :         case LIFETIME:
     244                 :         942 :           return "'" + get_str ();
     245                 :           0 :         case SCOPE_RESOLUTION:
     246                 :           0 :           return "::";
     247                 :        1459 :         case INT_LITERAL:
     248                 :        1459 :           if (get_type_hint () == CORETYPE_UNKNOWN)
     249                 :        1379 :             return get_str ();
     250                 :             :           else
     251                 :          80 :             return get_str () + get_type_hint_str ();
     252                 :          16 :         case FLOAT_LITERAL:
     253                 :          16 :           if (get_type_hint () == CORETYPE_UNKNOWN)
     254                 :          16 :             return get_str ();
     255                 :             :           else
     256                 :           0 :             return get_str () + get_type_hint_str ();
     257                 :       16382 :         default:
     258                 :       16382 :           return get_str ();
     259                 :             :         }
     260                 :             :     }
     261                 :             :   else
     262                 :             :     {
     263                 :       51452 :       return get_token_description ();
     264                 :             :     }
     265                 :             : }
     266                 :             : } // 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.