[binutils-gdb] gdb/dwarf: use enumerate in index-write.c

Simon Marchi simark@sourceware.org
Thu Apr 16 19:52:24 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=5533fec14c492e7a3718abd7ec1888caec15e5c0

commit 5533fec14c492e7a3718abd7ec1888caec15e5c0
Author: Simon Marchi <simon.marchi@polymtl.ca>
Date:   Fri Feb 20 20:18:59 2026 -0500

    gdb/dwarf: use enumerate in index-write.c
    
    Use the new enumerate util to get rid of some manual counters.
    
    Change-Id: I0573f0890e9b775a5181d8c0d435bb7ba661cae4

Diff:
---
 gdb/dwarf2/index-write.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)

diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c
index 82c4645a265..0f940920862 100644
--- a/gdb/dwarf2/index-write.c
+++ b/gdb/dwarf2/index-write.c
@@ -24,6 +24,7 @@
 #include "cli/cli-decode.h"
 #include "exceptions.h"
 #include "gdbsupport/byte-vector.h"
+#include "gdbsupport/enumerate.h"
 #include "gdbsupport/filestuff.h"
 #include "gdbsupport/gdb_unlinker.h"
 #include "gdbsupport/pathstuff.h"
@@ -1496,32 +1497,28 @@ write_debug_names (dwarf2_per_bfd *per_bfd, cooked_index *table,
   unit_lists units = get_unit_lists (*per_bfd);
   debug_names nametable (per_bfd, dwarf5_is_dwarf64, dwarf5_byte_order);
   data_buf comp_unit_list;
-  int comp_unit_counter = 0;
 
-  for (const auto per_cu : units.comp)
+  for (auto [i, per_cu] : gdb::ranges::views::enumerate (units.comp))
     {
-      nametable.add_cu (per_cu, comp_unit_counter);
+      nametable.add_cu (per_cu, i);
       comp_unit_list.append_uint (nametable.dwarf5_offset_size (),
 				  dwarf5_byte_order,
 				  to_underlying (per_cu->sect_off ()));
-      comp_unit_counter++;
     }
 
   data_buf type_unit_list;
-  int type_unit_counter = 0;
 
-  for (const auto per_cu : units.type)
+  for (auto [i, per_cu] : gdb::ranges::views::enumerate (units.type))
     {
-      nametable.add_cu (per_cu, type_unit_counter);
+      nametable.add_cu (per_cu, i);
       type_unit_list.append_uint (nametable.dwarf5_offset_size (),
 				  dwarf5_byte_order,
 				  to_underlying (per_cu->sect_off ()));
-      type_unit_counter++;
     }
 
-   /* Verify that all units are represented.  */
-  gdb_assert (comp_unit_counter == per_bfd->num_comp_units);
-  gdb_assert (type_unit_counter == per_bfd->num_type_units);
+  /* Verify that all units are represented.  */
+  gdb_assert (units.comp.size () == per_bfd->num_comp_units);
+  gdb_assert (units.type.size () == per_bfd->num_type_units);
 
   for (const cooked_index_entry *entry : table->all_entries ())
     nametable.insert (entry);
@@ -1560,11 +1557,11 @@ write_debug_names (dwarf2_per_bfd *per_bfd, cooked_index *table,
   header.append_uint (2, dwarf5_byte_order, 0);
 
   /* comp_unit_count - The number of CUs in the CU list.  */
-  header.append_uint (4, dwarf5_byte_order, comp_unit_counter);
+  header.append_uint (4, dwarf5_byte_order, units.comp.size ());
 
   /* local_type_unit_count - The number of TUs in the local TU
      list.  */
-  header.append_uint (4, dwarf5_byte_order, type_unit_counter);
+  header.append_uint (4, dwarf5_byte_order, units.type.size ());
 
   /* foreign_type_unit_count - The number of TUs in the foreign TU
      list.  */


More information about the Gdb-cvs mailing list