public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-7816] gccrs: import: Change raw pointer to unique_ptr
@ 2024-01-16 18:01 Arthur Cohen
0 siblings, 0 replies; only message in thread
From: Arthur Cohen @ 2024-01-16 18:01 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:dd73caa394b1c08f11c7af92e323838f9eaa0348
commit r14-7816-gdd73caa394b1c08f11c7af92e323838f9eaa0348
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date: Thu Jun 15 15:32:38 2023 +0200
gccrs: 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>
Diff:
---
gcc/rust/metadata/rust-import-archive.cc | 31 ++++++++++----------
gcc/rust/metadata/rust-imports.cc | 50 +++++++++++++++++---------------
gcc/rust/metadata/rust-imports.h | 21 +++++++-------
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 5678d486f17..b9fc0e4c923 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 fc3a588f816..669cecb29c3 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 37727f5ab15..9707a78a037 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 d67e884da30..572ecfad49b 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;
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2024-01-16 18:01 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-16 18:01 [gcc r14-7816] gccrs: import: Change raw pointer to unique_ptr Arthur Cohen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).