LCOV - code coverage report
Current view: top level - gcc/rust/expand - rust-expand-format-args.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 91.9 % 62 57
Test Date: 2025-01-18 13:11:24 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) 2024-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-expand-format-args.h"
      20                 :             : #include "rust-ast-fragment.h"
      21                 :             : #include "rust-ast.h"
      22                 :             : #include "rust-builtin-ast-nodes.h"
      23                 :             : #include "rust-ast-builder.h"
      24                 :             : #include "rust-diagnostics.h"
      25                 :             : #include "rust-expr.h"
      26                 :             : #include "rust-fmt.h"
      27                 :             : #include "rust-path.h"
      28                 :             : #include "rust-system.h"
      29                 :             : #include "rust-token.h"
      30                 :             : 
      31                 :             : namespace Rust {
      32                 :             : namespace Fmt {
      33                 :             : 
      34                 :             : static std::unique_ptr<AST::Expr>
      35                 :           1 : format_arg (const AST::Builder &builder, std::unique_ptr<AST::Expr> &&to_format,
      36                 :             :             const std::string &trait)
      37                 :             : {
      38                 :           1 :   auto formatter_fn = std::unique_ptr<AST::Expr> (new AST::PathInExpression (
      39                 :           5 :     builder.path_in_expression ({"core", "fmt", trait, "fmt"})));
      40                 :             : 
      41                 :           1 :   auto path = std::unique_ptr<AST::Expr> (new AST::PathInExpression (
      42                 :           1 :     builder.path_in_expression ({"core", "fmt", "ArgumentV1", "new"})));
      43                 :             : 
      44                 :           1 :   auto args = std::vector<std::unique_ptr<AST::Expr>> ();
      45                 :           1 :   args.emplace_back (std::move (to_format));
      46                 :           1 :   args.emplace_back (std::move (formatter_fn));
      47                 :             : 
      48                 :           1 :   return builder.call (std::move (path), std::move (args));
      49                 :           1 : }
      50                 :             : 
      51                 :             : const std::string &
      52                 :           1 : get_trait_name (ffi::FormatSpec format_specifier)
      53                 :             : {
      54                 :           1 :   static const std::unordered_map<std::string, std::string> spec_map = {
      55                 :             :     {"", "Display"},   {"?", "Debug"},    {"e", "LowerExp"},
      56                 :             :     {"E", "UpperExp"}, {"o", "Octal"},    {"p", "Pointer"},
      57                 :             :     {"b", "Binary"},   {"x", "LowerHex"}, {"X", "UpperHex"},
      58                 :          10 :   };
      59                 :             : 
      60                 :           1 :   auto it = spec_map.find (format_specifier.ty.to_string ());
      61                 :             : 
      62                 :           1 :   if (it == spec_map.end ())
      63                 :           0 :     rust_unreachable ();
      64                 :             : 
      65                 :           1 :   return it->second;
      66                 :             : }
      67                 :             : 
      68                 :             : tl::optional<AST::Fragment>
      69                 :           1 : expand_format_args (AST::FormatArgs &fmt,
      70                 :             :                     std::vector<std::unique_ptr<AST::Token>> &&tokens)
      71                 :             : {
      72                 :           1 :   auto loc = fmt.get_locus ();
      73                 :           1 :   auto builder = AST::Builder (loc);
      74                 :           1 :   auto &arguments = fmt.get_arguments ();
      75                 :             : 
      76                 :           1 :   auto static_pieces = std::vector<std::unique_ptr<AST::Expr>> ();
      77                 :           1 :   auto args
      78                 :           1 :     = std::vector<std::pair<std::unique_ptr<AST::Expr>, ffi::FormatSpec>> ();
      79                 :             : 
      80                 :           3 :   for (const auto &node : fmt.get_template ().get_pieces ())
      81                 :             :     {
      82                 :           2 :       switch (node.tag)
      83                 :             :         {
      84                 :           1 :         case ffi::Piece::Tag::String:
      85                 :           1 :           static_pieces.emplace_back (
      86                 :           2 :             builder.literal_string (node.string._0.to_string ()));
      87                 :           1 :           break;
      88                 :           1 :           case ffi::Piece::Tag::NextArgument: {
      89                 :           1 :             auto next_argument = node.next_argument._0;
      90                 :           1 :             switch (node.next_argument._0.position.tag)
      91                 :             :               {
      92                 :           1 :                 case ffi::Position::Tag::ArgumentImplicitlyIs: {
      93                 :           1 :                   auto idx = next_argument.position.argument_implicitly_is._0;
      94                 :           1 :                   auto trait = next_argument.format;
      95                 :           1 :                   auto arg = arguments.at (idx);
      96                 :             : 
      97                 :             :                   // FIXME(Arthur): This API sucks
      98                 :           1 :                   rust_assert (arg.get_kind ().kind
      99                 :             :                                == AST::FormatArgumentKind::Kind::Normal);
     100                 :             : 
     101                 :           1 :                   args.push_back ({arg.get_expr ().clone_expr (), trait});
     102                 :           1 :                 }
     103                 :           1 :                 break;
     104                 :           0 :               case ffi::Position::Tag::ArgumentIs:
     105                 :           0 :               case ffi::Position::Tag::ArgumentNamed:
     106                 :           0 :                 rust_sorry_at (loc, "unhandled argument position specifier");
     107                 :           0 :                 break;
     108                 :             :               }
     109                 :             :           }
     110                 :           1 :           break;
     111                 :             :         }
     112                 :             :     }
     113                 :             : 
     114                 :           1 :   auto args_array = std::vector<std::unique_ptr<AST::Expr>> ();
     115                 :           2 :   for (auto &&arg : args)
     116                 :           2 :     args_array.emplace_back (format_arg (builder,
     117                 :           2 :                                          builder.ref (std::move (arg.first)),
     118                 :             :                                          get_trait_name (arg.second)));
     119                 :             : 
     120                 :           1 :   auto pieces = builder.ref (builder.array (std::move (static_pieces)));
     121                 :           1 :   auto args_slice = builder.ref (builder.array (std::move (args_array)));
     122                 :             : 
     123                 :           1 :   auto final_path = make_unique<AST::PathInExpression> (
     124                 :           1 :     builder.path_in_expression ({"core", "fmt", "Arguments", "new_v1"}));
     125                 :           1 :   auto final_args = std::vector<std::unique_ptr<AST::Expr>> ();
     126                 :           1 :   final_args.emplace_back (std::move (pieces));
     127                 :           1 :   final_args.emplace_back (std::move (args_slice));
     128                 :             : 
     129                 :           1 :   auto final_call
     130                 :           1 :     = builder.call (std::move (final_path), std::move (final_args));
     131                 :             : 
     132                 :           1 :   auto node = AST::SingleASTNode (std::move (final_call));
     133                 :             : 
     134                 :           3 :   return AST::Fragment ({node}, std::move (tokens));
     135                 :           1 : }
     136                 :             : 
     137                 :             : } // namespace Fmt
     138                 :             : } // 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.