public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>, Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH 1/3] gdb: add gdb::make_unique function
Date: Mon, 14 Aug 2023 14:42:32 +0100	[thread overview]
Message-ID: <a0ff6c73f21eb6d9c5da31ba8a17ac58d4315a02.1692019711.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1692019711.git.aburgess@redhat.com>

While GDB is still C++11, lets add a gdb::make_unique template
function that can be used to create std::unique_ptr objects, just like
the C++14 std::make_unique.

When we move to C++14 we can either alias gdb::make_unique to
std::make_unique, or just replace the 'gdb::' prefix throughout.

I've make use of this function in all the places I think this can
easily be used, though I'm sure I've probably missed some.

Should be no user visible changes after this commit.
---
 gdb/addrmap.c                          | 2 +-
 gdb/break-catch-load.c                 | 5 ++---
 gdb/compile/compile-c-support.c        | 2 +-
 gdb/cp-name-parser.y                   | 2 +-
 gdb/cp-support.c                       | 2 +-
 gdb/dwarf2/frame.c                     | 2 +-
 gdb/dwarf2/read-debug-names.c          | 2 +-
 gdb/dwarf2/read-gdb-index.c            | 2 +-
 gdb/dwarf2/read.c                      | 2 +-
 gdb/gdbtypes.c                         | 2 +-
 gdb/python/py-varobj.c                 | 4 +---
 gdb/ui-out.c                           | 8 ++++----
 gdb/unittests/parallel-for-selftests.c | 4 ++--
 gdb/varobj.c                           | 2 +-
 gdbsupport/gdb_unique_ptr.h            | 9 +++++++++
 15 files changed, 28 insertions(+), 22 deletions(-)

diff --git a/gdb/addrmap.c b/gdb/addrmap.c
index 33dc7762d91..d16775d49d4 100644
--- a/gdb/addrmap.c
+++ b/gdb/addrmap.c
@@ -428,7 +428,7 @@ test_addrmap ()
 
   /* Create mutable addrmap.  */
   auto_obstack temp_obstack;
-  std::unique_ptr<struct addrmap_mutable> map (new addrmap_mutable);
+  auto map = gdb::make_unique<struct addrmap_mutable> ();
   SELF_CHECK (map != nullptr);
 
   /* Check initial state.  */
diff --git a/gdb/break-catch-load.c b/gdb/break-catch-load.c
index 440b42852bb..94d8b420d32 100644
--- a/gdb/break-catch-load.c
+++ b/gdb/break-catch-load.c
@@ -230,9 +230,8 @@ add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
   if (*arg == '\0')
     arg = nullptr;
 
-  std::unique_ptr<solib_catchpoint> c (new solib_catchpoint (gdbarch, is_temp,
-							     nullptr,
-							     is_load, arg));
+  auto c = gdb::make_unique<solib_catchpoint> (gdbarch, is_temp, nullptr,
+					       is_load, arg);
 
   c->enable_state = enabled ? bp_enabled : bp_disabled;
 
diff --git a/gdb/compile/compile-c-support.c b/gdb/compile/compile-c-support.c
index f9b32205b7a..53b7285b366 100644
--- a/gdb/compile/compile-c-support.c
+++ b/gdb/compile/compile-c-support.c
@@ -118,7 +118,7 @@ get_compile_context (const char *fe_libcc, const char *fe_context,
     error (_("The loaded version of GCC does not support the required version "
 	     "of the API."));
 
-  return std::unique_ptr<compile_instance> (new INSTTYPE (context));
+  return gdb::make_unique<INSTTYPE> (context);
 }
 
 /* A C-language implementation of get_compile_context.  */
diff --git a/gdb/cp-name-parser.y b/gdb/cp-name-parser.y
index 80188074202..324166a03ff 100644
--- a/gdb/cp-name-parser.y
+++ b/gdb/cp-name-parser.y
@@ -2038,7 +2038,7 @@ cp_demangled_name_to_comp (const char *demangled_name,
 
   state.demangle_info = allocate_info ();
 
-  std::unique_ptr<demangle_parse_info> result (new demangle_parse_info);
+  auto result = gdb::make_unique<demangle_parse_info> ();
   result->info = state.demangle_info;
 
   if (yyparse (&state))
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index 0300727434d..2af0218dba0 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -674,7 +674,7 @@ mangled_name_to_comp (const char *mangled_name, int options,
 					      options, memory);
       if (ret)
 	{
-	  std::unique_ptr<demangle_parse_info> info (new demangle_parse_info);
+	  auto info = gdb::make_unique<demangle_parse_info> ();
 	  info->tree = ret;
 	  *demangled_p = NULL;
 	  return info;
diff --git a/gdb/dwarf2/frame.c b/gdb/dwarf2/frame.c
index 940a01e9612..abc8d613482 100644
--- a/gdb/dwarf2/frame.c
+++ b/gdb/dwarf2/frame.c
@@ -2126,7 +2126,7 @@ dwarf2_build_frame_info (struct objfile *objfile)
   struct gdbarch *gdbarch = objfile->arch ();
 
   /* Build a minimal decoding of the DWARF2 compilation unit.  */
-  std::unique_ptr<comp_unit> unit (new comp_unit (objfile));
+  auto unit = gdb::make_unique<comp_unit> (objfile);
 
   if (objfile->separate_debug_objfile_backlink == NULL)
     {
diff --git a/gdb/dwarf2/read-debug-names.c b/gdb/dwarf2/read-debug-names.c
index 3d96bf476f8..2e5067efb3d 100644
--- a/gdb/dwarf2/read-debug-names.c
+++ b/gdb/dwarf2/read-debug-names.c
@@ -462,7 +462,7 @@ create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
 bool
 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
 {
-  std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
+  auto map = gdb::make_unique<mapped_debug_names> ();
   mapped_debug_names dwz_map;
   struct objfile *objfile = per_objfile->objfile;
   dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
diff --git a/gdb/dwarf2/read-gdb-index.c b/gdb/dwarf2/read-gdb-index.c
index 1127643e53a..9bfc5302b0e 100644
--- a/gdb/dwarf2/read-gdb-index.c
+++ b/gdb/dwarf2/read-gdb-index.c
@@ -778,7 +778,7 @@ dwarf2_read_gdb_index
   if (main_index_contents.empty ())
     return 0;
 
-  std::unique_ptr<mapped_gdb_index> map (new mapped_gdb_index);
+  auto map = gdb::make_unique<mapped_gdb_index> ();
   if (!read_gdb_index_from_buffer (objfile_name (objfile),
 				   use_deprecated_index_sections,
 				   main_index_contents, map.get (), &cu_list,
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index a64f82bd24a..7d27ff14128 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -4535,7 +4535,7 @@ allocate_type_unit_groups_table ()
 static std::unique_ptr<type_unit_group>
 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
 {
-  std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
+  auto tu_group = gdb::make_unique<type_unit_group> ();
 
   tu_group->hash.dwo_unit = cu->dwo_unit;
   tu_group->hash.line_sect_off = line_offset_struct;
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index d7db7beb554..939fcc23b82 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -5853,7 +5853,7 @@ static const struct registry<objfile>::key<fixed_point_type_storage>
 void
 allocate_fixed_point_type_info (struct type *type)
 {
-  std::unique_ptr<fixed_point_type_info> up (new fixed_point_type_info);
+  auto up = gdb::make_unique<fixed_point_type_info> ();
   fixed_point_type_info *info;
 
   if (type->is_objfile_owned ())
diff --git a/gdb/python/py-varobj.c b/gdb/python/py-varobj.c
index 08e790b7e7e..98603cec009 100644
--- a/gdb/python/py-varobj.c
+++ b/gdb/python/py-varobj.c
@@ -170,7 +170,5 @@ py_varobj_get_iterator (struct varobj *var, PyObject *printer,
       error (_("Could not get children iterator"));
     }
 
-  return std::unique_ptr<varobj_iter> (new py_varobj_iter (var,
-							   std::move (iter),
-							   opts));
+  return gdb::make_unique<py_varobj_iter> (var, std::move (iter), opts);
 }
diff --git a/gdb/ui-out.c b/gdb/ui-out.c
index 9380630c320..b4b8e486cb8 100644
--- a/gdb/ui-out.c
+++ b/gdb/ui-out.c
@@ -236,9 +236,9 @@ void ui_out_table::append_header (int width, ui_align alignment,
     internal_error (_("table header must be specified after table_begin and "
 		      "before table_body."));
 
-  std::unique_ptr<ui_out_hdr> header (new ui_out_hdr (m_headers.size () + 1,
-							width, alignment,
-							col_name, col_hdr));
+  auto header = gdb::make_unique<ui_out_hdr> (m_headers.size () + 1,
+					      width, alignment,
+					      col_name, col_hdr);
 
   m_headers.push_back (std::move (header));
 }
@@ -328,7 +328,7 @@ ui_out::current_level () const
 void
 ui_out::push_level (ui_out_type type)
 {
-  std::unique_ptr<ui_out_level> level (new ui_out_level (type));
+  auto level = gdb::make_unique<ui_out_level> (type);
 
   m_levels.push_back (std::move (level));
 }
diff --git a/gdb/unittests/parallel-for-selftests.c b/gdb/unittests/parallel-for-selftests.c
index 15a095ae62b..1ad7eaa701c 100644
--- a/gdb/unittests/parallel-for-selftests.c
+++ b/gdb/unittests/parallel-for-selftests.c
@@ -160,7 +160,7 @@ TEST (int n_threads)
 	      {
 		if (start == end)
 		  any_empty_tasks = true;
-		return std::unique_ptr<int> (new int (end - start));
+		return gdb::make_unique<int> (end - start);
 	      });
   SELF_CHECK (!any_empty_tasks);
   SELF_CHECK (std::all_of (intresults.begin (),
@@ -178,7 +178,7 @@ TEST (int n_threads)
 	      {
 		if (start == end)
 		  any_empty_tasks = true;
-		return std::unique_ptr<int> (new int (end - start));
+		return gdb::make_unique<int> (end - start);
 	      },
 	    task_size_one);
   SELF_CHECK (!any_empty_tasks);
diff --git a/gdb/varobj.c b/gdb/varobj.c
index 81b8e61f304..e7323bed127 100644
--- a/gdb/varobj.c
+++ b/gdb/varobj.c
@@ -261,7 +261,7 @@ varobj_create (const char *objname,
 	       const char *expression, CORE_ADDR frame, enum varobj_type type)
 {
   /* Fill out a varobj structure for the (root) variable being constructed.  */
-  std::unique_ptr<varobj> var (new varobj (new varobj_root));
+  auto var = gdb::make_unique<varobj> (new varobj_root);
 
   if (expression != NULL)
     {
diff --git a/gdbsupport/gdb_unique_ptr.h b/gdbsupport/gdb_unique_ptr.h
index a3ab62405d4..e25e5e37efe 100644
--- a/gdbsupport/gdb_unique_ptr.h
+++ b/gdbsupport/gdb_unique_ptr.h
@@ -56,6 +56,15 @@ struct noop_deleter
   void operator() (T *ptr) const { }
 };
 
+/* Create simple std::unique_ptr<T> objects.  */
+
+template<typename T, typename... Arg>
+std::unique_ptr<T>
+make_unique (Arg... args)
+{
+  return std::unique_ptr<T> (new T (std::forward<Arg> (args)...));
+}
+
 } /* namespace gdb */
 
 /* Dup STR and return a unique_xmalloc_ptr for the result.  */
-- 
2.25.4


  reply	other threads:[~2023-08-14 13:42 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-14 13:42 [PATCH 0/3] New gdb::make_unique and more std::unique_ptr use Andrew Burgess
2023-08-14 13:42 ` Andrew Burgess [this message]
2023-08-14 15:38   ` [PATCH 1/3] gdb: add gdb::make_unique function Tom Tromey
2023-08-17 10:26     ` Andrew Burgess
2023-08-17 18:07       ` Tom Tromey
2023-08-21 10:02         ` Andrew Burgess
2023-08-14 13:42 ` [PATCH 2/3] gdb: have mi_out_new return std::unique_ptr Andrew Burgess
2023-08-14 13:42 ` [PATCH 3/3] gdb: remove mi_parse::make functions Andrew Burgess
2023-08-22 15:33 ` [PATCH 0/3] New gdb::make_unique and more std::unique_ptr use Tom Tromey
2023-08-23  8:52   ` Andrew Burgess

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a0ff6c73f21eb6d9c5da31ba8a17ac58d4315a02.1692019711.git.aburgess@redhat.com \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tromey@adacore.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).