LCOV - code coverage report
Current view: top level - gcc/rust/expand - rust-derive-debug.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.7 % 57 54
Test Date: 2026-11-14 16:39:50 Functions: 90.0 % 10 9
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // Copyright (C) 2025-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-derive-debug.h"
      20              : #include "rust-ast.h"
      21              : #include "rust-hir-map.h"
      22              : #include "rust-system.h"
      23              : 
      24              : namespace Rust {
      25              : namespace AST {
      26              : 
      27         1432 : DeriveDebug::DeriveDebug (location_t loc, Builder::Source item_source)
      28         1432 :   : DeriveVisitor (loc, item_source), expanded (nullptr)
      29         1432 : {}
      30              : 
      31              : std::unique_ptr<Item>
      32         1432 : DeriveDebug::go (Item &item)
      33              : {
      34         1432 :   item.accept_vis (*this);
      35              : 
      36         1432 :   rust_assert (expanded);
      37              : 
      38         1432 :   return std::move (expanded);
      39              : }
      40              : 
      41              : std::unique_ptr<AssociatedItem>
      42         1432 : DeriveDebug::stub_debug_fn ()
      43              : {
      44         1432 :   auto unit_expr = builder.tuple ();
      45         1432 :   auto ok_expr
      46         1432 :     = ptrify (builder.path_in_expression (LangItem::Kind::RESULT_OK));
      47              : 
      48         2864 :   auto stub_return = builder.call (std::move (ok_expr), std::move (unit_expr));
      49              : 
      50              :   // we can't use builder.block() here as it returns a unique_ptr<Expr> and
      51              :   // Function's constructor expects a unique_ptr<BlockExpr>
      52         1432 :   auto block = std::unique_ptr<BlockExpr> (
      53         1432 :     new BlockExpr ({}, std::move (stub_return), {}, {}, tl::nullopt, loc, loc));
      54              : 
      55         1432 :   auto self = builder.self_ref_param ();
      56              : 
      57         1432 :   auto return_type
      58         7160 :     = ptrify (builder.type_path ({builder.get_path_start (), "fmt", "Result"},
      59         1432 :                                  builder.has_opening_scope ()));
      60              : 
      61         1432 :   auto mut_fmt_type_inner = ptrify (
      62         7160 :     builder.type_path ({builder.get_path_start (), "fmt", "Formatter"},
      63         1432 :                        builder.has_opening_scope ()));
      64              : 
      65         1432 :   auto mut_fmt_type
      66         1432 :     = builder.reference_type (std::move (mut_fmt_type_inner), true);
      67              : 
      68         2864 :   auto fmt = builder.function_param (builder.identifier_pattern ("_fmt"),
      69         1432 :                                      std::move (mut_fmt_type));
      70              : 
      71         1432 :   auto params = vec (std::move (self), std::move (fmt));
      72              : 
      73         4296 :   auto function = builder.function ("fmt", std::move (params),
      74         2864 :                                     std::move (return_type), std::move (block));
      75              : 
      76         1432 :   return function;
      77         1432 : }
      78              : 
      79              : std::unique_ptr<Item>
      80         1432 : DeriveDebug::stub_derive_impl (
      81              :   std::string name,
      82              :   const std::vector<std::unique_ptr<GenericParam>> &type_generics)
      83              : {
      84         1432 :   auto trait_items = vec (stub_debug_fn ());
      85              : 
      86         3320 :   auto debug = [this] () {
      87         9440 :     return builder.type_path ({builder.get_path_start (), "fmt", "Debug"},
      88         3776 :                               builder.has_opening_scope ());
      89         1432 :   };
      90         3320 :   auto generics = setup_impl_generics (name, type_generics, [&, this] () {
      91          456 :     return builder.trait_bound (debug ());
      92         1432 :   });
      93              : 
      94         2864 :   return builder.trait_impl (debug (), std::move (generics.self_type),
      95              :                              std::move (trait_items),
      96         2864 :                              std::move (generics.impl));
      97         1432 : }
      98              : 
      99              : void
     100          693 : DeriveDebug::visit_struct (StructStruct &struct_item)
     101              : {
     102         2079 :   expanded = stub_derive_impl (struct_item.get_identifier ().as_string (),
     103         1386 :                                struct_item.get_generic_params ());
     104          693 : }
     105              : 
     106              : void
     107          541 : DeriveDebug::visit_tuple (TupleStruct &tuple_item)
     108              : {
     109         1623 :   expanded = stub_derive_impl (tuple_item.get_identifier ().as_string (),
     110         1082 :                                tuple_item.get_generic_params ());
     111          541 : }
     112              : 
     113              : void
     114          198 : DeriveDebug::visit_enum (Enum &enum_item)
     115              : {
     116          594 :   expanded = stub_derive_impl (enum_item.get_identifier ().as_string (),
     117          396 :                                enum_item.get_generic_params ());
     118          198 : }
     119              : 
     120              : void
     121            0 : DeriveDebug::visit_union (Union &enum_item)
     122              : {
     123            0 :   rust_error_at (loc, "derive(Debug) cannot be derived for unions");
     124            0 : }
     125              : 
     126              : } // namespace AST
     127              : } // 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.