[v2,10/21] libcc1: use unique_ptr more

Message ID 20210428010119.806184-11-tom@tromey.com
State New
Headers show
Series
  • C++11-based improvements for libcc1
Related show

Commit Message

Tom Tromey April 28, 2021, 1:01 a.m.
This changes libcc1 to use unique_ptr in a few more places, removing
some manual memory management.

libcc1/ChangeLog
2021-04-27  Tom Tromey  <tom@tromey.com>

	* libcp1.cc (struct libcp1) <connection, compilerp>: Use
	unique_ptr.
	(~libcp1): Remove.
	(libcp1_compile, libcp1_set_triplet_regexp)
	(libcp1_set_driver_filename): Update.
	* libcc1.cc (struct libcc1) <connection, compilerp>: Use
	unique_ptr.
	(~libcc1): Remove.
	(libcc1_set_triplet_regexp, libcc1_set_driver_filename)
	(libcc1_compile): Update.
---
 libcc1/ChangeLog | 13 +++++++++++++
 libcc1/libcc1.cc | 32 +++++++++++++-------------------
 libcc1/libcp1.cc | 32 +++++++++++++-------------------
 3 files changed, 39 insertions(+), 38 deletions(-)

-- 
2.26.2

Comments

Richard Sandiford via Gcc-patches April 29, 2021, 12:44 p.m. | #1
On 4/27/2021 7:01 PM, Tom Tromey wrote:
> This changes libcc1 to use unique_ptr in a few more places, removing

> some manual memory management.

>

> libcc1/ChangeLog

> 2021-04-27  Tom Tromey  <tom@tromey.com>

>

> 	* libcp1.cc (struct libcp1) <connection, compilerp>: Use

> 	unique_ptr.

> 	(~libcp1): Remove.

> 	(libcp1_compile, libcp1_set_triplet_regexp)

> 	(libcp1_set_driver_filename): Update.

> 	* libcc1.cc (struct libcc1) <connection, compilerp>: Use

> 	unique_ptr.

> 	(~libcc1): Remove.

> 	(libcc1_set_triplet_regexp, libcc1_set_driver_filename)

> 	(libcc1_compile): Update.


OK

Jeff

Patch

diff --git a/libcc1/libcc1.cc b/libcc1/libcc1.cc
index 3432f4e8b212..e00355955b6e 100644
--- a/libcc1/libcc1.cc
+++ b/libcc1/libcc1.cc
@@ -49,7 +49,6 @@  class libcc1_connection;
 struct libcc1 : public gcc_c_context
 {
   libcc1 (const gcc_base_vtable *, const gcc_c_fe_vtable *);
-  ~libcc1 ();
 
   // A convenience function to print something.
   void print (const char *str)
@@ -57,7 +56,7 @@  struct libcc1 : public gcc_c_context
     this->print_function (this->print_datum, str);
   }
 
-  libcc1_connection *connection;
+  std::unique_ptr<libcc1_connection> connection;
 
   gcc_c_oracle_function *binding_oracle;
   gcc_c_symbol_address_function *address_oracle;
@@ -85,7 +84,9 @@  struct libcc1 : public gcc_c_context
     virtual ~compiler ()
     {
     }
-  } *compilerp;
+  };
+
+  std::unique_ptr<compiler> compilerp;
 
   /* Compiler to set by set_triplet_regexp.  */
   class compiler_triplet_regexp : public compiler
@@ -142,8 +143,7 @@  public:
 
 libcc1::libcc1 (const gcc_base_vtable *v,
 		const gcc_c_fe_vtable *cv)
-  : connection (NULL),
-    binding_oracle (NULL),
+  : binding_oracle (NULL),
     address_oracle (NULL),
     oracle_datum (NULL),
     print_function (NULL),
@@ -157,12 +157,6 @@  libcc1::libcc1 (const gcc_base_vtable *v,
   c_ops = cv;
 }
 
-libcc1::~libcc1 ()
-{
-  delete connection;
-  delete compilerp;
-}
-
 
 
 // Enclose these functions in an anonymous namespace because they
@@ -220,7 +214,7 @@  R rpc (struct gcc_c_context *s, Arg... rest)
   libcc1 *self = (libcc1 *) s;
   R result;
 
-  if (!cc1_plugin::call (self->connection, NAME, &result, rest...))
+  if (!cc1_plugin::call (self->connection.get (), NAME, &result, rest...))
     return 0;
   return result;
 }
@@ -380,8 +374,8 @@  libcc1_set_triplet_regexp (struct gcc_base_context *s,
 {
   libcc1 *self = (libcc1 *) s;
 
-  delete self->compilerp;
-  self->compilerp = new libcc1::compiler_triplet_regexp (self, triplet_regexp);
+  self->compilerp.reset (new libcc1::compiler_triplet_regexp (self,
+							      triplet_regexp));
   return NULL;
 }
 
@@ -391,9 +385,8 @@  libcc1_set_driver_filename (struct gcc_base_context *s,
 {
   libcc1 *self = (libcc1 *) s;
 
-  delete self->compilerp;
-  self->compilerp = new libcc1::compiler_driver_filename (self,
-							  driver_filename);
+  self->compilerp.reset (new libcc1::compiler_driver_filename (self,
+							       driver_filename));
   return NULL;
 }
 
@@ -464,7 +457,8 @@  fork_exec (libcc1 *self, char **argv, int spair_fds[2], int stderr_fds[2])
 
       cc1_plugin::status result = cc1_plugin::FAIL;
       if (self->connection->send ('H')
-	  && ::cc1_plugin::marshall (self->connection, GCC_C_FE_VERSION_1))
+	  && ::cc1_plugin::marshall (self->connection.get (),
+				     GCC_C_FE_VERSION_1))
 	result = self->connection->wait_for_query ();
 
       close (spair_fds[0]);
@@ -527,7 +521,7 @@  libcc1_compile (struct gcc_base_context *s,
   if (self->verbose)
     self->args.push_back ("-v");
 
-  self->connection = new libcc1_connection (fds[0], stderr_fds[0], self);
+  self->connection.reset (new libcc1_connection (fds[0], stderr_fds[0], self));
 
   cc1_plugin::callback_ftype *fun
     = cc1_plugin::callback<int,
diff --git a/libcc1/libcp1.cc b/libcc1/libcp1.cc
index 4273f8d83826..4bd8c6b00b65 100644
--- a/libcc1/libcp1.cc
+++ b/libcc1/libcp1.cc
@@ -48,7 +48,6 @@  class libcp1_connection;
 struct libcp1 : public gcc_cp_context
 {
   libcp1 (const gcc_base_vtable *, const gcc_cp_fe_vtable *);
-  ~libcp1 ();
 
   // A convenience function to print something.
   void print (const char *str)
@@ -56,7 +55,7 @@  struct libcp1 : public gcc_cp_context
     this->print_function (this->print_datum, str);
   }
 
-  libcp1_connection *connection;
+  std::unique_ptr<libcp1_connection> connection;
 
   gcc_cp_oracle_function *binding_oracle;
   gcc_cp_symbol_address_function *address_oracle;
@@ -86,7 +85,9 @@  struct libcp1 : public gcc_cp_context
     virtual ~compiler ()
     {
     }
-  } *compilerp;
+  };
+
+  std::unique_ptr<compiler> compilerp;
 
   /* Compiler to set by set_triplet_regexp.  */
   class compiler_triplet_regexp : public compiler
@@ -143,8 +144,7 @@  public:
 
 libcp1::libcp1 (const gcc_base_vtable *v,
 		  const gcc_cp_fe_vtable *cv)
-  : connection (NULL),
-    binding_oracle (NULL),
+  : binding_oracle (NULL),
     address_oracle (NULL),
     oracle_datum (NULL),
     print_function (NULL),
@@ -158,12 +158,6 @@  libcp1::libcp1 (const gcc_base_vtable *v,
   cp_ops = cv;
 }
 
-libcp1::~libcp1 ()
-{
-  delete connection;
-  delete compilerp;
-}
-
 
 
 // Enclose these functions in an anonymous namespace because they
@@ -243,7 +237,7 @@  R rpc (struct gcc_cp_context *s, Arg... rest)
   libcp1 *self = (libcp1 *) s;
   R result;
 
-  if (!cc1_plugin::call (self->connection, NAME, &result, rest...))
+  if (!cc1_plugin::call (self->connection.get (), NAME, &result, rest...))
     return 0;
   return result;
 }
@@ -403,8 +397,8 @@  libcp1_set_triplet_regexp (struct gcc_base_context *s,
 {
   libcp1 *self = (libcp1 *) s;
 
-  delete self->compilerp;
-  self->compilerp = new libcp1::compiler_triplet_regexp (self, triplet_regexp);
+  self->compilerp.reset (new libcp1::compiler_triplet_regexp (self,
+							      triplet_regexp));
   return NULL;
 }
 
@@ -414,9 +408,8 @@  libcp1_set_driver_filename (struct gcc_base_context *s,
 {
   libcp1 *self = (libcp1 *) s;
 
-  delete self->compilerp;
-  self->compilerp = new libcp1::compiler_driver_filename (self,
-							  driver_filename);
+  self->compilerp.reset (new libcp1::compiler_driver_filename (self,
+							       driver_filename));
   return NULL;
 }
 
@@ -487,7 +480,8 @@  fork_exec (libcp1 *self, char **argv, int spair_fds[2], int stderr_fds[2])
 
       cc1_plugin::status result = cc1_plugin::FAIL;
       if (self->connection->send ('H')
-	  && ::cc1_plugin::marshall (self->connection, GCC_CP_FE_VERSION_0))
+	  && ::cc1_plugin::marshall (self->connection.get (),
+				     GCC_CP_FE_VERSION_0))
 	result = self->connection->wait_for_query ();
 
       close (spair_fds[0]);
@@ -550,7 +544,7 @@  libcp1_compile (struct gcc_base_context *s,
   if (self->verbose)
     self->args.push_back ("-v");
 
-  self->connection = new libcp1_connection (fds[0], stderr_fds[0], self);
+  self->connection.reset (new libcp1_connection (fds[0], stderr_fds[0], self));
 
   cc1_plugin::callback_ftype *fun
     = cc1_plugin::callback<int,