diff options
author | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2023-06-15 15:32:38 +0200 |
---|---|---|
committer | P-E-P <32375388+P-E-P@users.noreply.github.com> | 2023-07-21 09:06:09 +0000 |
commit | 071e02ed91dffe4d85bb1f21ef96ad8061486aa9 (patch) | |
tree | 150bd9ff8386283baa5105619689dcfa7ca935f8 /gcc | |
parent | 6b4f24c1ca6aabe95b24f3060503d419824ec78b (diff) | |
download | gcc-071e02ed91dffe4d85bb1f21ef96ad8061486aa9.zip gcc-071e02ed91dffe4d85bb1f21ef96ad8061486aa9.tar.gz gcc-071e02ed91dffe4d85bb1f21ef96ad8061486aa9.tar.bz2 |
import: Change raw pointer to unique_ptr
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>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/metadata/rust-import-archive.cc | 31 | ||||
-rw-r--r-- | gcc/rust/metadata/rust-imports.cc | 50 | ||||
-rw-r--r-- | gcc/rust/metadata/rust-imports.h | 21 | ||||
-rw-r--r-- | gcc/rust/rust-session-manager.cc | 3 |
4 files changed, 55 insertions, 50 deletions
diff --git a/gcc/rust/metadata/rust-import-archive.cc b/gcc/rust/metadata/rust-import-archive.cc index 5678d48..b9fc0e4 100644 --- a/gcc/rust/metadata/rust-import-archive.cc +++ b/gcc/rust/metadata/rust-import-archive.cc @@ -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 diff --git a/gcc/rust/metadata/rust-imports.cc b/gcc/rust/metadata/rust-imports.cc index 31d8afb..436c595 100644 --- a/gcc/rust/metadata/rust-imports.cc +++ b/gcc/rust/metadata/rust-imports.cc @@ -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. diff --git a/gcc/rust/metadata/rust-imports.h b/gcc/rust/metadata/rust-imports.h index 37727f5..9707a78 100644 --- a/gcc/rust/metadata/rust-imports.h +++ b/gcc/rust/metadata/rust-imports.h @@ -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_; }; diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index 72b82f6..de28f72 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc @@ -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; |