LCOV - code coverage report
Current view: top level - gcc/rust/metadata - rust-export-metadata.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 43.9 % 132 58
Test Date: 2026-11-14 16:39:50 Functions: 69.2 % 13 9
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // Copyright (C) 2020-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-export-metadata.h"
      20              : #include "rust-hir-visitor.h"
      21              : #include "rust-hir-full.h"
      22              : #include "rust-hir-map.h"
      23              : #include "rust-ast-dump.h"
      24              : #include "rust-abi.h"
      25              : #include "rust-item.h"
      26              : #include "rust-macro.h"
      27              : #include "rust-object-export.h"
      28              : 
      29              : #include "md5.h"
      30              : #include "rust-system.h"
      31              : 
      32              : namespace Rust {
      33              : namespace Metadata {
      34              : 
      35              : static const std::string extension_path = ".rox";
      36              : 
      37         4628 : ExportContext::ExportContext () : mappings (Analysis::Mappings::get ()) {}
      38              : 
      39         4628 : ExportContext::~ExportContext () {}
      40              : 
      41              : void
      42         4628 : ExportContext::emit_crate (AST::Crate &c)
      43              : {
      44         4628 :   std::stringstream oss;
      45         4628 :   using AST::Dump;
      46         4628 :   AST::Dump dumper (
      47              :     oss,
      48              :     Dump::Configuration{Dump::Configuration::InternalComment::Hide,
      49              :                         Dump::Configuration::NodeDescription::Hide,
      50              :                         Dump::Configuration::Comment::Hide,
      51              :                         Dump::Configuration::Newline::Hide,
      52              :                         Dump::Configuration::Indentation::None},
      53         4628 :     {});
      54         4628 :   dumper.process (c);
      55              : 
      56         9256 :   public_interface_buffer += oss.str ();
      57         4628 : }
      58              : void
      59            0 : ExportContext::emit_macro (AST::MacroRulesDefinition &macro)
      60              : {
      61            0 :   std::stringstream oss;
      62            0 :   AST::Dump dumper (oss);
      63              : 
      64            0 :   dumper.go (macro);
      65              : 
      66            0 :   public_interface_buffer += oss.str ();
      67            0 : }
      68              : 
      69              : const std::string &
      70         4628 : ExportContext::get_interface_buffer () const
      71              : {
      72         4628 :   return public_interface_buffer;
      73              : }
      74              : 
      75         4628 : PublicInterface::PublicInterface (HIR::Crate &crate)
      76         4628 :   : crate (crate), mappings (Analysis::Mappings::get ()), context ()
      77         4628 : {}
      78              : 
      79              : void
      80            0 : PublicInterface::Export (HIR::Crate &crate)
      81              : {
      82            0 :   PublicInterface interface (crate);
      83            0 :   interface.gather_export_data ();
      84            0 :   interface.write_to_object_file ();
      85            0 : }
      86              : 
      87              : void
      88         4628 : PublicInterface::ExportTo (HIR::Crate &crate, const std::string &output_path)
      89              : {
      90         4628 :   PublicInterface interface (crate);
      91         4628 :   interface.gather_export_data ();
      92         4628 :   interface.write_to_path (output_path);
      93         4628 : }
      94              : 
      95              : void
      96         4628 : PublicInterface::gather_export_data ()
      97              : {
      98         4628 :   auto crate_num
      99         4628 :     = mappings.crate.lookup_crate_num (crate.get_mappings ().get_nodeid ());
     100         4628 :   auto &ast_crate = mappings.crate.get_ast_crate (crate_num.value ());
     101         4628 :   context.emit_crate (ast_crate);
     102         4628 : }
     103              : 
     104              : void
     105            0 : PublicInterface::write_to_object_file () const
     106              : {
     107              :   // done
     108            0 :   const auto &buf = context.get_interface_buffer ();
     109            0 :   std::string size_buffer = std::to_string (buf.size ());
     110              : 
     111              :   // md5 this
     112            0 :   struct md5_ctx chksm;
     113            0 :   unsigned char checksum[16];
     114              : 
     115            0 :   md5_init_ctx (&chksm);
     116            0 :   md5_process_bytes (buf.c_str (), buf.size (), &chksm);
     117            0 :   md5_finish_ctx (&chksm, checksum);
     118              : 
     119              :   // MAGIC MD5 DLIM  DLIM buffer-size DELIM contents
     120            0 :   const std::string current_crate_name
     121            0 :     = mappings.crate.get_current_crate_name ();
     122              : 
     123              :   // extern void
     124            0 :   rust_write_export_data (kMagicHeader, sizeof (kMagicHeader));
     125            0 :   rust_write_export_data ((const char *) checksum, sizeof (checksum));
     126            0 :   rust_write_export_data (kSzDelim, sizeof (kSzDelim));
     127            0 :   rust_write_export_data (current_crate_name.c_str (),
     128            0 :                           current_crate_name.size ());
     129            0 :   rust_write_export_data (kSzDelim, sizeof (kSzDelim));
     130            0 :   rust_write_export_data (size_buffer.c_str (), size_buffer.size ());
     131            0 :   rust_write_export_data (kSzDelim, sizeof (kSzDelim));
     132            0 :   rust_write_export_data (buf.c_str (), buf.size ());
     133            0 : }
     134              : 
     135              : void
     136         4628 : PublicInterface::write_to_path (const std::string &path) const
     137              : {
     138              :   // validate path contains correct extension
     139         4628 :   const std::string expected_file_name = expected_metadata_filename ();
     140         4628 :   const char *path_base_name = lbasename (path.c_str ());
     141         4628 :   if (strcmp (path_base_name, expected_file_name.c_str ()) != 0)
     142              :     {
     143            0 :       rust_error_at (UNDEF_LOCATION,
     144              :                      "expected metadata-output path to have base file name of: "
     145              :                      "%qs got %qs",
     146              :                      expected_file_name.c_str (), path_base_name);
     147            0 :       return;
     148              :     }
     149              : 
     150              :   // done
     151         4628 :   const auto &buf = context.get_interface_buffer ();
     152         4628 :   std::string size_buffer = std::to_string (buf.size ());
     153              : 
     154              :   // md5 this
     155         4628 :   struct md5_ctx chksm;
     156         4628 :   unsigned char checksum[16];
     157              : 
     158         4628 :   md5_init_ctx (&chksm);
     159         4628 :   md5_process_bytes (buf.c_str (), buf.size (), &chksm);
     160         4628 :   md5_finish_ctx (&chksm, checksum);
     161              : 
     162              :   // MAGIC MD5 DLIM  DLIM buffer-size DELIM contents
     163         4628 :   const std::string current_crate_name
     164         4628 :     = mappings.crate.get_current_crate_name ();
     165              : 
     166              :   // write to path
     167         4628 :   FILE *nfd = fopen (path.c_str (), "wb");
     168         4628 :   if (nfd == NULL)
     169              :     {
     170            0 :       rust_error_at (UNDEF_LOCATION, "failed to open file %qs for writing: %s",
     171            0 :                      path.c_str (), xstrerror (errno));
     172            0 :       return;
     173              :     }
     174              : 
     175              :   // write data
     176         4628 :   if (fwrite (kMagicHeader, sizeof (kMagicHeader), 1, nfd) < 1)
     177              :     {
     178            0 :       rust_error_at (UNDEF_LOCATION, "failed to write to file %qs: %s",
     179            0 :                      path.c_str (), xstrerror (errno));
     180            0 :       fclose (nfd);
     181            0 :       return;
     182              :     }
     183              : 
     184         4628 :   if (fwrite (checksum, sizeof (checksum), 1, nfd) < 1)
     185              :     {
     186            0 :       rust_error_at (UNDEF_LOCATION, "failed to write to file %qs: %s",
     187            0 :                      path.c_str (), xstrerror (errno));
     188            0 :       fclose (nfd);
     189            0 :       return;
     190              :     }
     191              : 
     192         4628 :   if (fwrite (kSzDelim, sizeof (kSzDelim), 1, nfd) < 1)
     193              :     {
     194            0 :       rust_error_at (UNDEF_LOCATION, "failed to write to file %qs: %s",
     195            0 :                      path.c_str (), xstrerror (errno));
     196            0 :       fclose (nfd);
     197            0 :       return;
     198              :     }
     199              : 
     200         4628 :   if (fwrite (current_crate_name.c_str (), current_crate_name.size (), 1, nfd)
     201              :       < 1)
     202              :     {
     203            0 :       rust_error_at (UNDEF_LOCATION, "failed to write to file %qs: %s",
     204            0 :                      path.c_str (), xstrerror (errno));
     205            0 :       fclose (nfd);
     206            0 :       return;
     207              :     }
     208              : 
     209         4628 :   if (fwrite (kSzDelim, sizeof (kSzDelim), 1, nfd) < 1)
     210              :     {
     211            0 :       rust_error_at (UNDEF_LOCATION, "failed to write to file %qs: %s",
     212            0 :                      path.c_str (), xstrerror (errno));
     213            0 :       fclose (nfd);
     214            0 :       return;
     215              :     }
     216              : 
     217         4628 :   if (fwrite (size_buffer.c_str (), size_buffer.size (), 1, nfd) < 1)
     218              :     {
     219            0 :       rust_error_at (UNDEF_LOCATION, "failed to write to file %qs: %s",
     220            0 :                      path.c_str (), xstrerror (errno));
     221            0 :       fclose (nfd);
     222            0 :       return;
     223              :     }
     224              : 
     225         4628 :   if (fwrite (kSzDelim, sizeof (kSzDelim), 1, nfd) < 1)
     226              :     {
     227            0 :       rust_error_at (UNDEF_LOCATION, "failed to write to file %qs: %s",
     228            0 :                      path.c_str (), xstrerror (errno));
     229            0 :       fclose (nfd);
     230            0 :       return;
     231              :     }
     232              : 
     233         4628 :   if (!buf.empty ())
     234         4628 :     if (fwrite (buf.c_str (), buf.size (), 1, nfd) < 1)
     235              :       {
     236            0 :         rust_error_at (UNDEF_LOCATION, "failed to write to file %qs: %s",
     237            0 :                        path.c_str (), xstrerror (errno));
     238            0 :         fclose (nfd);
     239            0 :         return;
     240              :       }
     241              : 
     242              :   // done
     243         4628 :   fclose (nfd);
     244         4628 : }
     245              : 
     246              : bool
     247            0 : PublicInterface::is_crate_public (const HIR::VisItem &item)
     248              : {
     249            0 :   const HIR::Visibility &visibility = item.get_visibility ();
     250              : 
     251            0 :   bool is_public
     252            0 :     = visibility.get_vis_type () == HIR::Visibility::VisType::Public;
     253            0 :   bool has_path = !visibility.get_path ().is_error ();
     254              : 
     255              :   // FIXME this might be pub(crate)
     256              :   // Arthur magic required here
     257              : 
     258            0 :   return is_public && !has_path;
     259              : }
     260              : 
     261              : std::string
     262         9256 : PublicInterface::expected_metadata_filename ()
     263              : {
     264         9256 :   auto &mappings = Analysis::Mappings::get ();
     265              : 
     266         9256 :   const std::string current_crate_name
     267         9256 :     = mappings.crate.get_current_crate_name ();
     268         9256 :   return current_crate_name + extension_path;
     269         9256 : }
     270              : 
     271              : } // namespace Metadata
     272              : } // 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.