[binutils-gdb] [gdb] Fix hard-coded constants in buildsym_compunit::make_blockvector

Tom de Vries via Gdb-cvs <[email protected]> Fri, 17 Jul 2026 14:30:19 +0000 (GMT)
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D3296caafa312=
64bf2ea8f2ebd01efcd3caeb6a58

commit 3296caafa31264bf2ea8f2ebd01efcd3caeb6a58
Author: Tom de Vries <[email protected]>
Date:   Fri Jul 17 16:30:15 2026 +0200

    [gdb] Fix hard-coded constants in buildsym_compunit::make_blockvector
   =20
    I came across some code in buildsym_compunit::make_blockvector that uses
    hardcoded constants 0 and 1:
    ...
          gdb_assert (blockvector->block (0)->is_global_block ());
          gdb_assert (blockvector->block (1)->is_static_block ());
    ...
   =20
    Fix this by instead using the symbolic constants GLOBAL_BLOCK and
    STATIC_BLOCK.
   =20
    The same function has an odd-looking for loop that uses a hard-coded '1=
' to
    skip the global block:
    ...
           /* The 'J > 1' here is so that we don't place the global block i=
nto
             the map.  For CU with gaps, the static block will reflect the
             gaps, while the global block will just reflect the full extent=
 of
             the range.  */
          for (int j =3D num_blocks; j > 1; )
            {
              --j;
              struct block *b =3D blockvector->block (j);
    ...
   =20
    Fix this by rewriting it into an ordinary descending for loop, and using
    symbolic constant GLOBAL_BLOCK to avoid the global block:
    ...
          for (int j =3D num_blocks - 1; j > GLOBAL_BLOCK; --j)
            {
              struct block *b =3D blockvector->block (j);
    ...
   =20
    Approved-By: Tom Tromey <[email protected]>

Diff:
---
 gdb/buildsym.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/gdb/buildsym.c b/gdb/buildsym.c
index b543cd10eb5..2b0a9a39b81 100644
--- a/gdb/buildsym.c
+++ b/gdb/buildsym.c
@@ -363,16 +363,15 @@ buildsym_compunit::make_blockvector ()
       gdb_assert (num_blocks > 1);
=20
       /* Assert our understanding of how the blocks are laid out.  */
-      gdb_assert (blockvector->block (0)->is_global_block ());
-      gdb_assert (blockvector->block (1)->is_static_block ());
+      gdb_assert (blockvector->block (GLOBAL_BLOCK)->is_global_block ());
+      gdb_assert (blockvector->block (STATIC_BLOCK)->is_static_block ());
=20
-      /* The 'J > 1' here is so that we don't place the global block into
-	 the map.  For CU with gaps, the static block will reflect the
-	 gaps, while the global block will just reflect the full extent of
+      /* The 'J > GLOBAL_BLOCK' here is so that we don't place the global
+	 block into the map.  For CU with gaps, the static block will reflect
+	 the gaps, while the global block will just reflect the full extent of
 	 the range.  */
-      for (int j =3D num_blocks; j > 1; )
+      for (int j =3D num_blocks - 1; j > GLOBAL_BLOCK; --j)
 	{
-	  --j;
 	  struct block *b =3D blockvector->block (j);
=20
 	  gdb_assert (!b->is_global_block ());