LCOV - code coverage report
Current view: top level - gcc/rust/expand - rust-macro-builtins-format-args.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 90.9 % 66 60
Test Date: 2025-11-22 14:42:49 Functions: 100.0 % 3 3
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                 :             : #include "rust-ast-fragment.h"
      19                 :             : #include "rust-fmt.h"
      20                 :             : #include "rust-macro-builtins-helpers.h"
      21                 :             : #include "rust-expand-format-args.h"
      22                 :             : 
      23                 :             : namespace Rust {
      24                 :             : 
      25                 :             : struct FormatArgsInput
      26                 :             : {
      27                 :             :   std::string format_str;
      28                 :             :   AST::FormatArguments args;
      29                 :             :   // bool is_literal?
      30                 :             : };
      31                 :             : 
      32                 :             : struct FormatArgsParseError
      33                 :             : {
      34                 :             :   enum class Kind
      35                 :             :   {
      36                 :             :     MissingArguments
      37                 :             :   } kind;
      38                 :             : };
      39                 :             : 
      40                 :             : static inline tl::expected<std::string, AST::Fragment>
      41                 :          79 : format_args_parse_expr (location_t invoc_locus, AST::MacroInvocData &invoc,
      42                 :             :                         Parser<MacroInvocLexer> &parser,
      43                 :             :                         BuiltinMacro macro_kind)
      44                 :             : {
      45                 :          79 :   std::unique_ptr<AST::Expr> format_expr = parser.parse_expr ();
      46                 :          79 :   rust_assert (format_expr);
      47                 :             : 
      48                 :          79 :   if (format_expr->get_expr_kind () == AST::Expr::Kind::MacroInvocation)
      49                 :             :     {
      50                 :           3 :       std::vector<std::unique_ptr<AST::MacroInvocation>> pending;
      51                 :           3 :       pending.emplace_back (
      52                 :           3 :         static_cast<AST::MacroInvocation *> (format_expr.release ()));
      53                 :           6 :       return tl::unexpected<AST::Fragment> (
      54                 :           6 :         make_eager_builtin_invocation (macro_kind, invoc_locus,
      55                 :           3 :                                        invoc.get_delim_tok_tree (),
      56                 :           3 :                                        std::move (pending)));
      57                 :           3 :     }
      58                 :             : 
      59                 :             :   // TODO(Arthur): Clean this up - if we haven't parsed a string literal but a
      60                 :             :   // macro invocation, what do we do here? return a tl::unexpected?
      61                 :          76 :   rust_assert (format_expr->is_literal ());
      62                 :          76 :   return static_cast<AST::LiteralExpr &> (*format_expr)
      63                 :          76 :     .get_literal ()
      64                 :          76 :     .as_string ();
      65                 :          79 : }
      66                 :             : 
      67                 :             : static inline tl::expected<AST::FormatArguments, FormatArgsParseError>
      68                 :          76 : format_args_parse_arguments (AST::MacroInvocData &invoc,
      69                 :             :                              Parser<MacroInvocLexer> &parser,
      70                 :             :                              TokenId last_token_id)
      71                 :             : {
      72                 :             :   // TODO: check if EOF - return that format_args!() requires at least one
      73                 :             :   // argument
      74                 :             : 
      75                 :          76 :   auto args = AST::FormatArguments ();
      76                 :             :   // TODO: Allow implicit captures ONLY if the the first arg is a string literal
      77                 :             :   // and not a macro invocation
      78                 :             : 
      79                 :             :   // TODO: How to consume all of the arguments until the delimiter?
      80                 :             : 
      81                 :             :   // TODO: What we then want to do is as follows:
      82                 :             :   // for each token, check if it is an identifier
      83                 :             :   //     yes? is the next token an equal sign (=)
      84                 :             :   //          yes?
      85                 :             :   //              -> if that identifier is already present in our map, error
      86                 :             :   //              out
      87                 :             :   //              -> parse an expression, return a FormatArgument::Named
      88                 :             :   //     no?
      89                 :             :   //         -> if there have been named arguments before, error out
      90                 :             :   //         (positional after named error)
      91                 :             :   //         -> parse an expression, return a FormatArgument::Normal
      92                 :         396 :   while (parser.peek_current_token ()->get_id () != last_token_id)
      93                 :             :     {
      94                 :         126 :       parser.skip_token (COMMA);
      95                 :             : 
      96                 :             :       // Check in case of an extraneous comma in the args list, which is
      97                 :             :       // allowed - format_args!("fmt", arg, arg2,)
      98                 :         252 :       if (parser.peek_current_token ()->get_id () == last_token_id)
      99                 :             :         break;
     100                 :             : 
     101                 :         122 :       if (parser.peek_current_token ()->get_id () == IDENTIFIER
     102                 :         226 :           && parser.peek (1)->get_id () == EQUAL)
     103                 :             :         {
     104                 :             :           // FIXME: This is ugly - just add a parser.parse_identifier()?
     105                 :           1 :           auto ident_tok = parser.peek_current_token ();
     106                 :           2 :           auto ident = Identifier (ident_tok);
     107                 :             : 
     108                 :           1 :           parser.skip_token (IDENTIFIER);
     109                 :           1 :           parser.skip_token (EQUAL);
     110                 :             : 
     111                 :           1 :           auto expr = parser.parse_expr ();
     112                 :             : 
     113                 :             :           // TODO: Handle graciously
     114                 :           1 :           if (!expr)
     115                 :           0 :             rust_unreachable ();
     116                 :             : 
     117                 :           1 :           args.push (AST::FormatArgument::named (ident, std::move (expr)));
     118                 :           2 :         }
     119                 :             :       else
     120                 :             :         {
     121                 :         121 :           auto expr = parser.parse_expr ();
     122                 :             : 
     123                 :             :           // TODO: Handle graciously
     124                 :         121 :           if (!expr)
     125                 :           0 :             rust_unreachable ();
     126                 :             : 
     127                 :         121 :           args.push (AST::FormatArgument::normal (std::move (expr)));
     128                 :         121 :         }
     129                 :             :       // we need to skip commas, don't we?
     130                 :             :     }
     131                 :             : 
     132                 :          76 :   return args;
     133                 :          76 : }
     134                 :             : 
     135                 :             : tl::optional<AST::Fragment>
     136                 :          79 : MacroBuiltin::format_args_handler (location_t invoc_locus,
     137                 :             :                                    AST::MacroInvocData &invoc,
     138                 :             :                                    AST::InvocKind semicolon,
     139                 :             :                                    AST::FormatArgs::Newline nl)
     140                 :             : {
     141                 :          79 :   MacroInvocLexer lex (invoc.get_delim_tok_tree ().to_token_stream ());
     142                 :          79 :   Parser<MacroInvocLexer> parser (lex);
     143                 :             : 
     144                 :          79 :   auto last_token_id = macro_end_token (invoc.get_delim_tok_tree (), parser);
     145                 :             : 
     146                 :          79 :   auto format_str = format_args_parse_expr (invoc_locus, invoc, parser,
     147                 :             :                                             nl == AST::FormatArgs::Newline::Yes
     148                 :             :                                               ? BuiltinMacro::FormatArgsNl
     149                 :         158 :                                               : BuiltinMacro::FormatArgs);
     150                 :             : 
     151                 :          79 :   if (!format_str)
     152                 :             :     {
     153                 :           3 :       return std::move (format_str.error ());
     154                 :             :     }
     155                 :             : 
     156                 :          76 :   auto args = format_args_parse_arguments (invoc, parser, last_token_id);
     157                 :             : 
     158                 :          76 :   if (!args)
     159                 :             :     {
     160                 :           0 :       rust_error_at (invoc_locus,
     161                 :             :                      "could not parse arguments to %<format_args!()%>");
     162                 :           0 :       return tl::nullopt;
     163                 :             :     }
     164                 :             : 
     165                 :             :   // TODO(Arthur): We need to handle this
     166                 :             :   // // if it is not a literal, it's an eager macro invocation - return it
     167                 :             :   // if (!fmt_expr->is_literal ())
     168                 :             :   //   {
     169                 :             :   //     auto token_tree = invoc.get_delim_tok_tree ();
     170                 :             :   //     return AST::Fragment ({AST::SingleASTNode (std::move (fmt_expr))},
     171                 :             :   //        token_tree.to_token_stream ());
     172                 :             :   //   }
     173                 :             : 
     174                 :             :   // TODO(Arthur): Handle this as well - raw strings are special for the
     175                 :             :   // format_args parser auto fmt_str = static_cast<AST::LiteralExpr &>
     176                 :             :   // (*fmt_arg.get ()); Switch on the format string to know if the string is raw
     177                 :             :   // or cooked switch (fmt_str.get_lit_type ())
     178                 :             :   //   {
     179                 :             :   //   // case AST::Literal::RAW_STRING:
     180                 :             :   //   case AST::Literal::STRING:
     181                 :             :   //     break;
     182                 :             :   //   case AST::Literal::CHAR:
     183                 :             :   //   case AST::Literal::BYTE:
     184                 :             :   //   case AST::Literal::BYTE_STRING:
     185                 :             :   //   case AST::Literal::INT:
     186                 :             :   //   case AST::Literal::FLOAT:
     187                 :             :   //   case AST::Literal::BOOL:
     188                 :             :   //   case AST::Literal::ERROR:
     189                 :             :   //     rust_unreachable ();
     190                 :             :   //   }
     191                 :             : 
     192                 :          76 :   bool append_newline = nl == AST::FormatArgs::Newline::Yes;
     193                 :             : 
     194                 :          76 :   auto fmt_str = std::move (format_str.value ());
     195                 :          76 :   if (append_newline)
     196                 :           0 :     fmt_str += '\n';
     197                 :             : 
     198                 :          76 :   auto pieces = Fmt::Pieces::collect (fmt_str, append_newline,
     199                 :          76 :                                       Fmt::ffi::ParseMode::Format);
     200                 :             : 
     201                 :             :   // TODO:
     202                 :             :   // do the transformation into an AST::FormatArgs node
     203                 :             :   // return that
     204                 :             :   // expand it during lowering
     205                 :             : 
     206                 :             :   // TODO: we now need to take care of creating `unfinished_literal`? this is
     207                 :             :   // for creating the `template`
     208                 :             : 
     209                 :          76 :   auto fmt_args_node = AST::FormatArgs (invoc_locus, std::move (pieces),
     210                 :          76 :                                         std::move (args.value ()));
     211                 :             : 
     212                 :          76 :   auto expanded
     213                 :             :     = Fmt::expand_format_args (fmt_args_node,
     214                 :          76 :                                invoc.get_delim_tok_tree ().to_token_stream ());
     215                 :             : 
     216                 :          76 :   if (!expanded.has_value ())
     217                 :           0 :     return AST::Fragment::create_error ();
     218                 :             : 
     219                 :          76 :   return *expanded;
     220                 :             : 
     221                 :             :   // auto node = std::unique_ptr<AST::Expr> (fmt_args_node);
     222                 :             :   // auto single_node = AST::SingleASTNode (std::move (node));
     223                 :             : 
     224                 :             :   // return AST::Fragment ({std::move (single_node)},
     225                 :             :   //    invoc.get_delim_tok_tree ().to_token_stream ());
     226                 :         310 : }
     227                 :             : 
     228                 :             : } // 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.