]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: import: Change raw pointer to unique_ptr
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Thu, 15 Jun 2023 13:32:38 +0000 (15:32 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:55:58 +0000 (18:55 +0100)
Replace Stream raw pointer with a smart pointer.

gcc/rust/ChangeLog:

* metadata/rust-import-archive.cc (Stream_concatenate::do_peek):
Remove deletion.
(Stream_concatenate::do_advance): Likewise.
(Import::find_archive_export_data): Replace with a smart
pointer.
* metadata/rust-imports.cc (add_search_path): Change return type
to smart pointer.
(Import::open_package): Likewise.
(Import::try_package_in_directory): Likewise.
(Import::find_export_data): Likewise.
(Import::find_object_export_data): Likewise.
(Import::Import): Change constructor to accept unique_ptr.
* metadata/rust-imports.h: Change constructor prototype.
* rust-session-manager.cc (Session::load_extern_crate): Change
pointer to smart pointer.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/metadata/rust-import-archive.cc
gcc/rust/metadata/rust-imports.cc
gcc/rust/metadata/rust-imports.h
gcc/rust/rust-session-manager.cc

index 5678d486f17d38b78a5f87fbbb8194b4b9095492..b9fc0e4c923577ae60ce522bd47e00492f5789e2 100644 (file)
@@ -7,6 +7,7 @@
 #include "rust-system.h"
 #include "rust-diagnostics.h"
 #include "rust-imports.h"
+#include "rust-make-unique.h"
 
 #ifndef O_BINARY
 #define O_BINARY 0
@@ -782,7 +783,10 @@ public:
   Stream_concatenate () : inputs_ () {}
 
   // Add a new stream.
-  void add (Import::Stream *is) { this->inputs_.push_back (is); }
+  void add (std::unique_ptr<Import::Stream> is)
+  {
+    this->inputs_.push_back (std::move (is));
+  }
 
 protected:
   bool do_peek (size_t, const char **);
@@ -790,7 +794,7 @@ protected:
   void do_advance (size_t);
 
 private:
-  std::list<Import::Stream *> inputs_;
+  std::list<std::unique_ptr<Import::Stream>> inputs_;
 };
 
 // Peek ahead.
@@ -804,7 +808,6 @@ Stream_concatenate::do_peek (size_t length, const char **bytes)
        return false;
       if (this->inputs_.front ()->peek (length, bytes))
        return true;
-      delete this->inputs_.front ();
       this->inputs_.pop_front ();
     }
 }
@@ -826,7 +829,6 @@ Stream_concatenate::do_advance (size_t skip)
          this->inputs_.front ()->advance (skip);
          return;
        }
-      delete this->inputs_.front ();
       this->inputs_.pop_front ();
     }
 }
@@ -834,15 +836,15 @@ Stream_concatenate::do_advance (size_t skip)
 // Import data from an archive.  We walk through the archive and
 // import data from each member.
 
-Import::Stream *
+std::unique_ptr<Import::Stream>
 Import::find_archive_export_data (const std::string &filename, int fd,
                                  Location location)
 {
   Archive_file afile (filename, fd, location);
   if (!afile.initialize ())
-    return NULL;
+    return nullptr;
 
-  Stream_concatenate *ret = new Stream_concatenate;
+  auto ret = Rust::make_unique<Stream_concatenate> ();
 
   bool any_data = false;
   bool any_members = false;
@@ -855,14 +857,14 @@ Import::find_archive_export_data (const std::string &filename, int fd,
       std::string member_name;
       if (!afile.get_file_and_offset (p->off, p->name, p->nested_off,
                                      &member_fd, &member_off, &member_name))
-       return NULL;
+       return nullptr;
 
-      Import::Stream *is
+      std::unique_ptr<Import::Stream> is
        = Import::find_object_export_data (member_name, member_fd, member_off,
                                           location);
-      if (is != NULL)
+      if (is != nullptr)
        {
-         ret->add (is);
+         ret->add (std::move (is));
          any_data = true;
        }
     }
@@ -870,16 +872,15 @@ Import::find_archive_export_data (const std::string &filename, int fd,
   if (!any_members)
     {
       // It's normal to have an empty archive file when using gobuild.
-      return new Stream_from_string ("");
+      return Rust::make_unique<Stream_from_string> ("");
     }
 
   if (!any_data)
     {
-      delete ret;
-      return NULL;
+      return nullptr;
     }
 
-  return ret;
+  return std::unique_ptr<Stream>{static_cast<Stream *> (ret.release ())};
 }
 
 } // namespace Rust
index fc3a588f816adbd8bccdc861764187d2ae32c9b6..669cecb29c3294b2f8ce9a6e7f2073cd72d3b5c8 100644 (file)
@@ -21,6 +21,7 @@
 #include "rust-imports.h"
 #include "rust-object-export.h"
 #include "rust-export-metadata.h"
+#include "rust-make-unique.h"
 
 #ifndef O_BINARY
 #define O_BINARY 0
@@ -62,7 +63,7 @@ add_search_path (const std::string &path)
 // stop; we do not keep looking for another file with the same name
 // later in the search path.
 
-std::pair<Import::Stream *, std::vector<ProcMacro::Procmacro>>
+std::pair<std::unique_ptr<Import::Stream>, std::vector<ProcMacro::Procmacro>>
 Import::open_package (const std::string &filename, Location location,
                      const std::string &relative_import_path)
 {
@@ -121,21 +122,21 @@ Import::open_package (const std::string &filename, Location location,
            indir += '/';
          indir += fn;
          auto s = Import::try_package_in_directory (indir, location);
-         if (s.first != NULL)
+         if (s.first != nullptr)
            return s;
        }
     }
 
   auto s = Import::try_package_in_directory (fn, location);
-  if (s.first != NULL)
+  if (s.first != nullptr)
     return s;
 
-  return std::make_pair (NULL, std::vector<ProcMacro::Procmacro>{});
+  return std::make_pair (nullptr, std::vector<ProcMacro::Procmacro>{});
 }
 
 // Try to find the export data for FILENAME.
 
-std::pair<Import::Stream *, std::vector<ProcMacro::Procmacro>>
+std::pair<std::unique_ptr<Import::Stream>, std::vector<ProcMacro::Procmacro>>
 Import::try_package_in_directory (const std::string &filename,
                                  Location location)
 {
@@ -160,13 +161,14 @@ Import::try_package_in_directory (const std::string &filename,
 
       fd = Import::try_suffixes (&found_filename);
       if (fd < 0)
-       return std::make_pair (NULL, std::vector<ProcMacro::Procmacro>{});
+       return std::make_pair (nullptr, std::vector<ProcMacro::Procmacro>{});
     }
 
   // The export data may not be in this file.
-  Stream *s = Import::find_export_data (found_filename, fd, location);
-  if (s != NULL)
-    return std::make_pair (s, std::vector<ProcMacro::Procmacro>{});
+  std::unique_ptr<Stream> s
+    = Import::find_export_data (found_filename, fd, location);
+  if (s != nullptr)
+    return std::make_pair (std::move (s), std::vector<ProcMacro::Procmacro>{});
 
   close (fd);
 
@@ -223,27 +225,27 @@ Import::try_suffixes (std::string *pfilename)
 
 // Look for export data in the file descriptor FD.
 
-Import::Stream *
+std::unique_ptr<Import::Stream>
 Import::find_export_data (const std::string &filename, int fd,
                          Location location)
 {
   // See if we can read this as an object file.
-  Import::Stream *stream
+  std::unique_ptr<Import::Stream> stream
     = Import::find_object_export_data (filename, fd, 0, location);
-  if (stream != NULL)
+  if (stream != nullptr)
     return stream;
 
   const int len = sizeof (Metadata::kMagicHeader);
   if (lseek (fd, 0, SEEK_SET) < 0)
     {
       rust_error_at (location, "lseek %s failed: %m", filename.c_str ());
-      return NULL;
+      return nullptr;
     }
 
   char buf[len];
   ssize_t c = ::read (fd, buf, len);
   if (c < len)
-    return NULL;
+    return nullptr;
 
   // Check for a file containing nothing but Rust export data.
   // if (memcmp (buf, Export::cur_magic, Export::magic_len) == 0
@@ -254,18 +256,18 @@ Import::find_export_data (const std::string &filename, int fd,
   //
   if (memcmp (buf, Metadata::kMagicHeader, sizeof (Metadata::kMagicHeader))
       == 0)
-    return new Stream_from_file (fd);
+    return Rust::make_unique<Stream_from_file> (fd);
 
   // See if we can read this as an archive.
   if (Import::is_archive_magic (buf))
     return Import::find_archive_export_data (filename, fd, location);
 
-  return NULL;
+  return nullptr;
 }
 
 // Look for export data in an object file.
 
-Import::Stream *
+std::unique_ptr<Import::Stream>
 Import::find_object_export_data (const std::string &filename, int fd,
                                 off_t offset, Location location)
 {
@@ -273,20 +275,20 @@ Import::find_object_export_data (const std::string &filename, int fd,
   size_t len;
   int err;
   const char *errmsg = rust_read_export_data (fd, offset, &buf, &len, &err);
-  if (errmsg != NULL)
+  if (errmsg != nullptr)
     {
       if (err == 0)
        rust_error_at (location, "%s: %s", filename.c_str (), errmsg);
       else
        rust_error_at (location, "%s: %s: %s", filename.c_str (), errmsg,
                       xstrerror (err));
-      return NULL;
+      return nullptr;
     }
 
-  if (buf == NULL)
-    return NULL;
+  if (buf == nullptr)
+    return nullptr;
 
-  return new Stream_from_buffer (buf, len);
+  return Rust::make_unique<Stream_from_buffer> (buf, len);
 }
 
 // Class Import.
@@ -294,8 +296,8 @@ Import::find_object_export_data (const std::string &filename, int fd,
 // Construct an Import object.  We make the builtin_types_ vector
 // large enough to hold all the builtin types.
 
-Import::Import (Stream *stream, Location location)
-  : stream_ (stream), location_ (location)
+Import::Import (std::unique_ptr<Stream> stream, Location location)
+  : stream_ (std::move (stream)), location_ (location)
 {}
 
 // Import the data in the associated stream.
index 37727f5ab15fa6eb85dd01c39e0b29fcf402f814..9707a78a037c7fd57c394209528f02d3ecb8d3e9 100644 (file)
@@ -110,15 +110,15 @@ public:
   // returns a pointer to a Stream object to read the data that it
   // exports.  LOCATION is the location of the import statement.
   // RELATIVE_IMPORT_PATH is used as a prefix for a relative import.
-  static std::pair<Stream *, std::vector<ProcMacro::Procmacro>>
+  static std::pair<std::unique_ptr<Stream>, std::vector<ProcMacro::Procmacro>>
   open_package (const std::string &filename, Location location,
                const std::string &relative_import_path);
 
-  static std::pair<Stream *, std::vector<ProcMacro::Procmacro>>
+  static std::pair<std::unique_ptr<Stream>, std::vector<ProcMacro::Procmacro>>
   try_package_in_directory (const std::string &, Location);
 
   // Constructor.
-  Import (Stream *, Location);
+  Import (std::unique_ptr<Stream>, Location);
 
   // The location of the import statement.
   Location location () const { return this->location_; }
@@ -160,19 +160,20 @@ public:
 private:
   static int try_suffixes (std::string *);
 
-  static Stream *find_export_data (const std::string &filename, int fd,
-                                  Location);
+  static std::unique_ptr<Stream> find_export_data (const std::string &filename,
+                                                  int fd, Location);
 
-  static Stream *find_object_export_data (const std::string &filename, int fd,
-                                         off_t offset, Location);
+  static std::unique_ptr<Stream>
+  find_object_export_data (const std::string &filename, int fd, off_t offset,
+                          Location);
 
   static bool is_archive_magic (const char *);
 
-  static Stream *find_archive_export_data (const std::string &filename, int fd,
-                                          Location);
+  static std::unique_ptr<Stream>
+  find_archive_export_data (const std::string &filename, int fd, Location);
 
   // The stream from which to read import data.
-  Stream *stream_;
+  std::unique_ptr<Stream> stream_;
   // The location of the import statement we are processing.
   Location location_;
 };
index d67e884da30f537b378f494ab507c8ca1fb362d3..572ecfad49bd88df36dd352dee06c71a8024b442 100644 (file)
@@ -979,7 +979,8 @@ Session::load_extern_crate (const std::string &crate_name, location_t locus)
   // -frust-extern
   auto cli_extern_crate = extern_crates.find (crate_name);
 
-  std::pair<Import::Stream *, std::vector<ProcMacro::Procmacro>> s;
+  std::pair<std::unique_ptr<Import::Stream>, std::vector<ProcMacro::Procmacro>>
+    s;
   if (cli_extern_crate != extern_crates.end ())
     {
       auto path = cli_extern_crate->second;