Use reference result of emplace_back

Message ID 20240204181849.3345797-1-tom@tromey.com
State New
Headers
Series Use reference result of emplace_back |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed

Commit Message

Tom Tromey Feb. 4, 2024, 6:18 p.m. UTC
  Starting with C++17, emplace_back returns a reference to the new
object.  This patch changes code that uses emplace_back followed by a
call to back() to simply use this reference instead.
---
 gdb/ada-lex.l     |  4 ++--
 gdb/btrace.c      |  4 ++--
 gdb/buildsym.c    |  6 ++----
 gdb/dwarf2/expr.c |  3 +--
 gdb/dwarf2/read.c | 30 ++++++++----------------------
 gdb/macroexp.c    |  7 ++-----
 gdb/remote.c      |  3 +--
 gdb/reverse.c     |  3 +--
 gdb/tracepoint.c  |  3 +--
 gdb/ui-out.c      |  9 +++------
 gdb/windows-nat.c |  3 +--
 gdb/xcoffread.c   |  3 +--
 12 files changed, 25 insertions(+), 53 deletions(-)
  

Comments

Simon Marchi Feb. 4, 2024, 7:01 p.m. UTC | #1
On 2024-02-04 13:18, Tom Tromey wrote:
> Starting with C++17, emplace_back returns a reference to the new
> object.  This patch changes code that uses emplace_back followed by a
> call to back() to simply use this reference instead.

Nice change, it was so awkward not to have that.

> @@ -906,8 +906,7 @@ buffer_group::write (const char *buf, long length_buf, ui_file *stream)
>  	  m_buffered_output.back ().m_msg.append (msg);
>  	else
>  	  {
> -	    m_buffered_output.emplace_back (msg);
> -	    m_buffered_output.back ().m_stream = stream;
> +	    m_buffered_output.emplace_back (msg).m_stream = stream;
>  	  }

Remove curly braces here.

Approved-By: Simon Marchi <simon.marchi@efficios.com>

Simon
  

Patch

diff --git a/gdb/ada-lex.l b/gdb/ada-lex.l
index bb43673edfd..828ff9a9215 100644
--- a/gdb/ada-lex.l
+++ b/gdb/ada-lex.l
@@ -462,8 +462,8 @@  processInt (struct parser_state *par_state, const char *base0,
       return FLOAT;
     }
 
-  int_storage.emplace_back (new gdb_mpz (std::move (result)));
-  const gdb_mpz *value = int_storage.back ().get ();
+  const gdb_mpz *value
+    = int_storage.emplace_back (new gdb_mpz (std::move (result))).get ();
 
   int int_bits = gdbarch_int_bit (par_state->gdbarch ());
   int long_bits = gdbarch_long_bit (par_state->gdbarch ());
diff --git a/gdb/btrace.c b/gdb/btrace.c
index a2aaa200a75..b5a047b245b 100644
--- a/gdb/btrace.c
+++ b/gdb/btrace.c
@@ -255,8 +255,8 @@  ftrace_new_function (struct btrace_thread_info *btinfo,
       insn_offset = prev->insn_offset + ftrace_call_num_insn (prev);
     }
 
-  btinfo->functions.emplace_back (mfun, fun, number, insn_offset, level);
-  return &btinfo->functions.back ();
+  return &btinfo->functions.emplace_back (mfun, fun, number, insn_offset,
+					  level);
 }
 
 /* Update the UP field of a function segment.  */
diff --git a/gdb/buildsym.c b/gdb/buildsym.c
index a963219a0d2..506110989bf 100644
--- a/gdb/buildsym.c
+++ b/gdb/buildsym.c
@@ -661,8 +661,7 @@  buildsym_compunit::record_line (struct subfile *subfile, int line,
 	return;
     }
 
-  subfile->line_vector_entries.emplace_back ();
-  linetable_entry &e = subfile->line_vector_entries.back ();
+  linetable_entry &e = subfile->line_vector_entries.emplace_back ();
   e.line = line;
   e.is_stmt = (flags & LEF_IS_STMT) != 0;
   e.set_unrelocated_pc (pc);
@@ -1134,8 +1133,7 @@  buildsym_compunit::augment_type_symtab ()
 struct context_stack *
 buildsym_compunit::push_context (int desc, CORE_ADDR valu)
 {
-  m_context_stack.emplace_back ();
-  struct context_stack *newobj = &m_context_stack.back ();
+  struct context_stack *newobj = &m_context_stack.emplace_back ();
 
   newobj->depth = desc;
   newobj->locals = m_local_symbols;
diff --git a/gdb/dwarf2/expr.c b/gdb/dwarf2/expr.c
index 01a4f53f213..9cfae104f31 100644
--- a/gdb/dwarf2/expr.c
+++ b/gdb/dwarf2/expr.c
@@ -1200,8 +1200,7 @@  dwarf_expr_context::stack_empty_p () const
 void
 dwarf_expr_context::add_piece (ULONGEST size, ULONGEST offset)
 {
-  this->m_pieces.emplace_back ();
-  dwarf_expr_piece &p = this->m_pieces.back ();
+  dwarf_expr_piece &p = this->m_pieces.emplace_back ();
 
   p.location = this->m_location;
   p.size = size;
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index e873d9cc440..eab56ec505d 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -11552,15 +11552,9 @@  dwarf2_add_field (struct field_info *fip, struct die_info *die,
   const char *fieldname = "";
 
   if (die->tag == DW_TAG_inheritance)
-    {
-      fip->baseclasses.emplace_back ();
-      new_field = &fip->baseclasses.back ();
-    }
+    new_field = &fip->baseclasses.emplace_back ();
   else
-    {
-      fip->fields.emplace_back ();
-      new_field = &fip->fields.back ();
-    }
+    new_field = &fip->fields.emplace_back ();
 
   new_field->offset = die->sect_off;
 
@@ -12071,16 +12065,14 @@  dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
   /* Create a new fnfieldlist if necessary.  */
   if (flp == nullptr)
     {
-      fip->fnfieldlists.emplace_back ();
-      flp = &fip->fnfieldlists.back ();
+      flp = &fip->fnfieldlists.emplace_back ();
       flp->name = fieldname;
       i = fip->fnfieldlists.size () - 1;
     }
 
   /* Create a new member function field and add it to the vector of
      fnfieldlists.  */
-  flp->fnfields.emplace_back ();
-  fnp = &flp->fnfields.back ();
+  fnp = &flp->fnfields.emplace_back ();
 
   /* Delay processing of the physname until later.  */
   if (cu->lang () == language_cplus)
@@ -12681,10 +12673,7 @@  handle_variant_part (struct die_info *die, struct type *type,
 {
   variant_part_builder *new_part;
   if (fi->current_variant_part == nullptr)
-    {
-      fi->variant_parts.emplace_back ();
-      new_part = &fi->variant_parts.back ();
-    }
+    new_part = &fi->variant_parts.emplace_back ();
   else if (!fi->current_variant_part->processing_variant)
     {
       complaint (_("nested DW_TAG_variant_part seen "
@@ -12696,8 +12685,7 @@  handle_variant_part (struct die_info *die, struct type *type,
   else
     {
       variant_field &current = fi->current_variant_part->variants.back ();
-      current.variant_parts.emplace_back ();
-      new_part = &current.variant_parts.back ();
+      new_part = &current.variant_parts.emplace_back ();
     }
 
   /* When we recurse, we want callees to add to this new variant
@@ -12761,8 +12749,7 @@  handle_variant (struct die_info *die, struct type *type,
     = make_scoped_restore (&fi->current_variant_part->processing_variant,
 			   true);
 
-  fi->current_variant_part->variants.emplace_back ();
-  variant_field &variant = fi->current_variant_part->variants.back ();
+  variant_field &variant = fi->current_variant_part->variants.emplace_back ();
   variant.first_field = fi->fields.size ();
 
   /* In a variant we want to get the discriminant and also add a
@@ -13157,8 +13144,7 @@  update_enumeration_type_from_children (struct die_info *die,
 	    flag_enum = 0;
 	}
 
-      fields.emplace_back ();
-      struct field &field = fields.back ();
+      struct field &field = fields.emplace_back ();
       field.set_name (dwarf2_physname (name, child_die, cu));
       field.set_loc_enumval (value);
     }
diff --git a/gdb/macroexp.c b/gdb/macroexp.c
index 9523550e6f6..b8a9b29db24 100644
--- a/gdb/macroexp.c
+++ b/gdb/macroexp.c
@@ -789,12 +789,10 @@  gather_arguments (const char *name, shared_macro_buffer *src, int nargs,
 
   for (;;)
     {
-      shared_macro_buffer *arg;
       int depth;
 
       /* Initialize the next argument.  */
-      args.emplace_back ();
-      arg = &args.back ();
+      shared_macro_buffer *arg = &args.emplace_back ();
       set_token (arg, src->text, src->text);
 
       /* Gather the argument's tokens.  */
@@ -819,8 +817,7 @@  gather_arguments (const char *name, shared_macro_buffer *src, int nargs,
 		     missing.  Add an empty argument in this case.  */
 		  if (nargs != -1 && args.size () == nargs - 1)
 		    {
-		      args.emplace_back ();
-		      arg = &args.back ();
+		      arg = &args.emplace_back ();
 		      set_token (arg, src->text, src->text);
 		    }
 
diff --git a/gdb/remote.c b/gdb/remote.c
index 72f14e28f54..f77548427a4 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -4027,8 +4027,7 @@  start_thread (struct gdb_xml_parser *parser,
   char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
   ptid_t ptid = read_ptid (id, NULL);
 
-  data->items.emplace_back (ptid);
-  thread_item &item = data->items.back ();
+  thread_item &item = data->items.emplace_back (ptid);
 
   attr = xml_find_attribute (attributes, "core");
   if (attr != NULL)
diff --git a/gdb/reverse.c b/gdb/reverse.c
index 035623aba9f..9a011631a32 100644
--- a/gdb/reverse.c
+++ b/gdb/reverse.c
@@ -123,8 +123,7 @@  save_bookmark_command (const char *args, int from_tty)
     error (_("target_get_bookmark failed."));
 
   /* Set up a bookmark struct.  */
-  all_bookmarks.emplace_back ();
-  bookmark &b = all_bookmarks.back ();
+  bookmark &b = all_bookmarks.emplace_back ();
   b.number = ++bookmark_count;
   b.pc = regcache_read_pc (regcache);
   b.sal = find_pc_line (b.pc, 0);
diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index abe8d2b45f4..7005ca83455 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -253,8 +253,7 @@  set_traceframe_context (frame_info_ptr trace_frame)
 struct trace_state_variable *
 create_trace_state_variable (const char *name)
 {
-  tvariables.emplace_back (name, next_tsv_number++);
-  return &tvariables.back ();
+  return &tvariables.emplace_back (name, next_tsv_number++);
 }
 
 /* Look for a trace state variable of the given name.  */
diff --git a/gdb/ui-out.c b/gdb/ui-out.c
index 23f4ce0fbb1..f48f6783e0f 100644
--- a/gdb/ui-out.c
+++ b/gdb/ui-out.c
@@ -906,8 +906,7 @@  buffer_group::write (const char *buf, long length_buf, ui_file *stream)
 	  m_buffered_output.back ().m_msg.append (msg);
 	else
 	  {
-	    m_buffered_output.emplace_back (msg);
-	    m_buffered_output.back ().m_stream = stream;
+	    m_buffered_output.emplace_back (msg).m_stream = stream;
 	  }
 	prev = cur + 1;
       }
@@ -918,8 +917,7 @@  buffer_group::write (const char *buf, long length_buf, ui_file *stream)
 void
 buffer_group::wrap_here (int indent, ui_file *stream)
 {
-  m_buffered_output.emplace_back ("", indent);
-  m_buffered_output.back ().m_stream = stream;
+  m_buffered_output.emplace_back ("", indent).m_stream = stream;
 }
 
 /* See ui-out.h.  */
@@ -927,8 +925,7 @@  buffer_group::wrap_here (int indent, ui_file *stream)
 void
 buffer_group::flush_here (ui_file *stream)
 {
-  m_buffered_output.emplace_back ("", -1, true);
-  m_buffered_output.back ().m_stream = stream;
+  m_buffered_output.emplace_back ("", -1, true).m_stream = stream;
 }
 
 /* See ui-out.h.  */
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 6fdd1f3a151..48b0d10d24c 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -887,8 +887,7 @@  windows_make_so (const char *name, LPVOID load_addr)
 	}
     }
 #endif
-  windows_process.solibs.emplace_back ();
-  windows_solib *so = &windows_process.solibs.back ();
+  windows_solib *so = &windows_process.solibs.emplace_back ();
   so->load_addr = load_addr;
   so->original_name = name;
 #ifndef __CYGWIN__
diff --git a/gdb/xcoffread.c b/gdb/xcoffread.c
index 589208cda68..e324f4ef8ca 100644
--- a/gdb/xcoffread.c
+++ b/gdb/xcoffread.c
@@ -428,8 +428,7 @@  arrange_linetable (std::vector<linetable_entry> &old_linetable)
       if (old_linetable[ii].line == 0)
 	{
 	  /* Function entry found.  */
-	  fentries.emplace_back ();
-	  linetable_entry &e = fentries.back ();
+	  linetable_entry &e = fentries.emplace_back ();
 	  e.line = ii;
 	  e.is_stmt = true;
 	  e.set_unrelocated_pc (old_linetable[ii].unrelocated_pc ());