[10/10] Use block::block_and_superblocks_in_fn

Message ID 20260501124504.2233495-11-tdevries@suse.de
State New
Headers
Series Add superblocks range loops |

Commit Message

Tom de Vries May 1, 2026, 12:45 p.m. UTC
  Add uses of block::block_and_superblocks_in_fn.
---
 gdb/ada-lang.c                  |  43 ++++-----
 gdb/compile/compile-c-symbols.c |  10 +--
 gdb/f-valprint.c                |  19 ++--
 gdb/mi/mi-cmd-stack.c           | 155 +++++++++++++++-----------------
 gdb/python/py-unwind.c          |   2 +-
 gdb/stack.c                     |  15 ++--
 gdb/symtab.c                    |  11 +--
 gdb/tracepoint.c                |   9 +-
 8 files changed, 114 insertions(+), 150 deletions(-)
  

Patch

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 6eb928b445d..400760be1ba 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -13118,32 +13118,27 @@  ada_add_exceptions_from_frame (compiled_regex *preg,
 			       const frame_info_ptr &frame,
 			       std::vector<ada_exc_info> *exceptions)
 {
-  const struct block *block = get_frame_block (frame, 0);
+  const struct block *frame_block = get_frame_block (frame, 0);
 
-  while (block != 0)
-    {
-      for (struct symbol *sym : block_iterator_range (block))
-	{
-	  switch (sym->loc_class ())
-	    {
-	    case LOC_TYPEDEF:
-	    case LOC_BLOCK:
-	    case LOC_CONST:
-	      break;
-	    default:
-	      if (ada_is_exception_sym (sym))
-		{
-		  struct ada_exc_info info = {sym->print_name (),
-					      sym->value_address ()};
+  for (auto block : block::block_and_superblocks_in_fn (frame_block))
+    for (struct symbol *sym : block_iterator_range (block))
+      {
+	switch (sym->loc_class ())
+	  {
+	  case LOC_TYPEDEF:
+	  case LOC_BLOCK:
+	  case LOC_CONST:
+	    break;
+	  default:
+	    if (ada_is_exception_sym (sym))
+	      {
+		struct ada_exc_info info = {sym->print_name (),
+					    sym->value_address ()};
 
-		  exceptions->push_back (info);
-		}
-	    }
-	}
-      if (block->function () != NULL)
-	break;
-      block = block->superblock ();
-    }
+		exceptions->push_back (info);
+	      }
+	  }
+      }
 }
 
 /* Add all exceptions defined globally whose name name match
diff --git a/gdb/compile/compile-c-symbols.c b/gdb/compile/compile-c-symbols.c
index dc0487d8f82..da8c6719fe5 100644
--- a/gdb/compile/compile-c-symbols.c
+++ b/gdb/compile/compile-c-symbols.c
@@ -588,20 +588,14 @@  generate_c_for_variable_locations (compile_instance *compiler,
      reality of shadowing.  */
   gdb::unordered_set<std::string_view> symset;
 
-  while (1)
+  for (auto b : block->block_and_superblocks_in_fn ())
     {
       /* Iterate over symbols in this block, generating code to
 	 compute the location of each local variable.  */
-      for (struct symbol *sym : block_iterator_range (block))
+      for (struct symbol *sym : block_iterator_range (b))
 	if (symset.insert (sym->natural_name ()).second)
 	  generate_c_for_for_one_variable (compiler, stream, gdbarch,
 					   registers_used, pc, sym);
-
-      /* If we just finished the outermost block of a function, we're
-	 done.  */
-      if (block->function () != NULL)
-	break;
-      block = block->superblock ();
     }
 
   return registers_used;
diff --git a/gdb/f-valprint.c b/gdb/f-valprint.c
index 13921c03a48..44b24d788d3 100644
--- a/gdb/f-valprint.c
+++ b/gdb/f-valprint.c
@@ -682,7 +682,7 @@  static void
 info_common_command (const char *comname, int from_tty)
 {
   frame_info_ptr fi;
-  const struct block *block;
+  const struct block *frame_block;
   int values_printed = 0;
 
   /* We have been told to display the contents of F77 COMMON
@@ -695,22 +695,17 @@  info_common_command (const char *comname, int from_tty)
   /* The following is generally ripped off from stack.c's routine
      print_frame_info().  */
 
-  block = get_frame_block (fi, 0);
-  if (block == NULL)
+  frame_block = get_frame_block (fi, 0);
+  if (frame_block == nullptr)
     {
       gdb_printf (_("No symbol table info available.\n"));
       return;
     }
 
-  while (block)
-    {
-      info_common_command_for_block (block, comname, &values_printed);
-      /* After handling the function's top-level block, stop.  Don't
-	 continue to its superblock, the block of per-file symbols.  */
-      if (block->function ())
-	break;
-      block = block->superblock ();
-    }
+  /* After handling the function's top-level block, stop.  Don't
+     continue to its superblock, the block of per-file symbols.  */
+  for (auto block : block::block_and_superblocks_in_fn (frame_block))
+    info_common_command_for_block (block, comname, &values_printed);
 
   if (!values_printed)
     {
diff --git a/gdb/mi/mi-cmd-stack.c b/gdb/mi/mi-cmd-stack.c
index f2a4b3044b0..0f46033c9b4 100644
--- a/gdb/mi/mi-cmd-stack.c
+++ b/gdb/mi/mi-cmd-stack.c
@@ -573,11 +573,11 @@  list_args_or_locals (const frame_print_options &fp_opts,
 		     enum what_to_list what, enum print_values values,
 		     const frame_info_ptr &fi, int skip_unavailable)
 {
-  const struct block *block;
+  const struct block *frame_block;
   const char *name_of_result;
   struct ui_out *uiout = current_uiout;
 
-  block = get_frame_block (fi, 0);
+  frame_block = get_frame_block (fi, 0);
 
   switch (what)
     {
@@ -596,88 +596,81 @@  list_args_or_locals (const frame_print_options &fp_opts,
 
   ui_out_emit_list list_emitter (uiout, name_of_result);
 
-  while (block != 0)
-    {
-      for (struct symbol *sym : block_iterator_range (block))
-	{
-	  int print_me = 0;
-
-	  switch (sym->loc_class ())
-	    {
-	    default:
-	    case LOC_UNDEF:	/* catches errors        */
-	    case LOC_CONST:	/* constant              */
-	    case LOC_TYPEDEF:	/* local typedef         */
-	    case LOC_LABEL:	/* local label           */
-	    case LOC_BLOCK:	/* local function        */
-	    case LOC_CONST_BYTES:	/* loc. byte seq.        */
-	    case LOC_UNRESOLVED:	/* unresolved static     */
-	    case LOC_OPTIMIZED_OUT:	/* optimized out         */
-	      print_me = 0;
-	      break;
+  for (auto block : block::block_and_superblocks_in_fn (frame_block))
+    for (struct symbol *sym : block_iterator_range (block))
+      {
+	int print_me = 0;
+
+	switch (sym->loc_class ())
+	  {
+	  default:
+	  case LOC_UNDEF:		/* catches errors	 */
+	  case LOC_CONST:		/* constant		 */
+	  case LOC_TYPEDEF:		/* local typedef	 */
+	  case LOC_LABEL:		/* local label		 */
+	  case LOC_BLOCK:		/* local function	 */
+	  case LOC_CONST_BYTES:		/* loc. byte seq.	 */
+	  case LOC_UNRESOLVED:		/* unresolved static	 */
+	  case LOC_OPTIMIZED_OUT:	/* optimized out	 */
+	    print_me = 0;
+	    break;
 
-	    case LOC_ARG:	/* argument              */
-	    case LOC_REF_ARG:	/* reference arg         */
-	    case LOC_REGPARM_ADDR:	/* indirect register arg */
-	    case LOC_LOCAL:	/* stack local           */
-	    case LOC_STATIC:	/* static                */
-	    case LOC_REGISTER:	/* register              */
-	    case LOC_COMPUTED:	/* computed location     */
-	      if (what == all)
-		print_me = 1;
-	      else if (what == locals)
-		print_me = !sym->is_argument ();
-	      else
-		print_me = sym->is_argument ();
-	      break;
-	    }
-	  if (print_me)
-	    {
-	      struct symbol *sym2;
-	      struct frame_arg arg, entryarg;
-
-	      if (sym->is_argument ())
-		sym2 = (lookup_symbol_search_name
-			(sym->search_name (),
-			 block, SEARCH_VAR_DOMAIN).symbol);
-	      else
-		sym2 = sym;
-	      gdb_assert (sym2 != NULL);
-
-	      arg.sym = sym2;
-	      arg.entry_kind = print_entry_values_no;
-	      entryarg.sym = sym2;
-	      entryarg.entry_kind = print_entry_values_no;
-
-	      switch (values)
-		{
-		case PRINT_SIMPLE_VALUES:
-		  if (!mi_simple_type_p (sym2->type ()))
-		    break;
-		  [[fallthrough]];
-
-		case PRINT_ALL_VALUES:
-		  if (sym->is_argument ())
-		    read_frame_arg (fp_opts, sym2, fi, &arg, &entryarg);
-		  else
-		    read_frame_local (sym2, fi, &arg);
+	  case LOC_ARG:			/* argument		 */
+	  case LOC_REF_ARG:		/* reference arg	 */
+	  case LOC_REGPARM_ADDR:	/* indirect register arg */
+	  case LOC_LOCAL:		/* stack local		 */
+	  case LOC_STATIC:		/* static		 */
+	  case LOC_REGISTER:		/* register		 */
+	  case LOC_COMPUTED:		/* computed location	 */
+	    if (what == all)
+	      print_me = 1;
+	    else if (what == locals)
+	      print_me = !sym->is_argument ();
+	    else
+	      print_me = sym->is_argument ();
+	    break;
+	  }
+	if (print_me)
+	  {
+	    struct symbol *sym2;
+	    struct frame_arg arg, entryarg;
+
+	    if (sym->is_argument ())
+	      sym2 = (lookup_symbol_search_name
+		      (sym->search_name (),
+		       block, SEARCH_VAR_DOMAIN).symbol);
+	    else
+	      sym2 = sym;
+	    gdb_assert (sym2 != nullptr);
+
+	    arg.sym = sym2;
+	    arg.entry_kind = print_entry_values_no;
+	    entryarg.sym = sym2;
+	    entryarg.entry_kind = print_entry_values_no;
+
+	    switch (values)
+	      {
+	      case PRINT_SIMPLE_VALUES:
+		if (!mi_simple_type_p (sym2->type ()))
 		  break;
-		}
-
-	      if (arg.entry_kind != print_entry_values_only)
-		list_arg_or_local (&arg, what, values, skip_unavailable,
-				   fp_opts);
-	      if (entryarg.entry_kind != print_entry_values_no)
-		list_arg_or_local (&entryarg, what, values, skip_unavailable,
-				   fp_opts);
-	    }
-	}
+		[[fallthrough]];
 
-      if (block->function ())
-	break;
-      else
-	block = block->superblock ();
-    }
+	      case PRINT_ALL_VALUES:
+		if (sym->is_argument ())
+		  read_frame_arg (fp_opts, sym2, fi, &arg, &entryarg);
+		else
+		  read_frame_local (sym2, fi, &arg);
+		break;
+	      }
+
+	    if (arg.entry_kind != print_entry_values_only)
+	      list_arg_or_local (&arg, what, values, skip_unavailable,
+				 fp_opts);
+	    if (entryarg.entry_kind != print_entry_values_no)
+	      list_arg_or_local (&entryarg, what, values, skip_unavailable,
+				 fp_opts);
+	  }
+      }
 }
 
 /* Read a frame specification from FRAME_EXP and return the selected frame.
diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
index 6dc5e11cf87..cb19496d46b 100644
--- a/gdb/python/py-unwind.c
+++ b/gdb/python/py-unwind.c
@@ -635,7 +635,7 @@  pending_framepy_block (PyObject *self, PyObject *args)
   PENDING_FRAMEPY_REQUIRE_VALID (pending_frame);
 
   frame_info_ptr frame = pending_frame->frame_info;
-  const struct block *block = nullptr, *fn_block;
+  const struct block *block = nullptr;
 
   try
     {
diff --git a/gdb/stack.c b/gdb/stack.c
index 6fcc26417e2..e9a01d16a3b 100644
--- a/gdb/stack.c
+++ b/gdb/stack.c
@@ -2232,16 +2232,11 @@  void
 iterate_over_block_local_vars (const struct block *block,
 			       iterate_over_block_arg_local_vars_cb cb)
 {
-  while (block)
-    {
-      iterate_over_block_locals (block, cb);
-      /* After handling the function's top-level block, stop.  Don't
-	 continue to its superblock, the block of per-file
-	 symbols.  */
-      if (block->function ())
-	break;
-      block = block->superblock ();
-    }
+  /* After handling the function's top-level block, stop.  Don't
+     continue to its superblock, the block of per-file
+     symbols.  */
+  for (auto b : block::block_and_superblocks_in_fn (block))
+    iterate_over_block_locals (b, cb);
 }
 
 /* Data to be passed around in the calls to the locals and args
diff --git a/gdb/symtab.c b/gdb/symtab.c
index d8e4e6375d9..9836a7abb38 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -2002,22 +2002,19 @@  lookup_language_this (const struct language_defn *lang,
   lookup_name_info this_name (lang->name_of_this (),
 			      symbol_name_match_type::SEARCH_NAME);
 
-  while (block)
+  for (auto b : block::block_and_superblocks_in_fn (block))
     {
       struct symbol *sym;
 
-      sym = block_lookup_symbol (block, this_name, SEARCH_VFT);
+      sym = block_lookup_symbol (b, this_name, SEARCH_VFT);
       if (sym != NULL)
 	{
 	  symbol_lookup_debug_printf_v
 	    ("lookup_language_this (...) = %s (%s, block %s)",
 	     sym->print_name (), host_address_to_string (sym),
-	     host_address_to_string (block));
-	  return (struct block_symbol) {sym, block};
+	     host_address_to_string (b));
+	  return (struct block_symbol) {sym, b};
 	}
-      if (block->function ())
-	break;
-      block = block->superblock ();
     }
 
   symbol_lookup_debug_printf_v ("lookup_language_this (...) = NULL");
diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index 7f078684b14..5608f81211a 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -2456,7 +2456,6 @@  tfind_outside_command (const char *args, int from_tty)
 static void
 info_scope_command (const char *args_in, int from_tty)
 {
-  const struct block *block;
   const char *symname;
   const char *save_args = args_in;
   int j, count = 0;
@@ -2481,9 +2480,9 @@  info_scope_command (const char *args_in, int from_tty)
 
   /* Resolve line numbers to PC.  */
   resolve_sal_pc (&sals[0]);
-  block = block_for_pc (sals[0].pc);
+  const struct block *pc_block = block_for_pc (sals[0].pc);
 
-  while (block != 0)
+  for (auto block : block::block_and_superblocks_in_fn (pc_block))
     {
       QUIT;			/* Allow user to bail out with ^C.  */
       for (struct symbol *sym : block_iterator_range (block))
@@ -2607,10 +2606,6 @@  info_scope_command (const char *args_in, int from_tty)
 	      gdb_printf (", length %s.\n", pulongest (t->length ()));
 	    }
 	}
-      if (block->function ())
-	break;
-      else
-	block = block->superblock ();
     }
   if (count <= 0)
     gdb_printf ("Scope for %s contains no locals or arguments.\n",