[06/27] Don't allow NULL as an argument to block_global_block

Message ID 20230120214618.3236224-7-tom@tromey.com
State New
Headers
Series C++-ify struct block |

Commit Message

Tom Tromey Jan. 20, 2023, 9:45 p.m. UTC
  block_global_block has special behavior when the block is NULL.
Remove this and patch up the callers instead.
---
 gdb/block.c      |  6 +-----
 gdb/cp-support.c | 15 ++++++++-------
 gdb/symtab.c     |  5 +++--
 3 files changed, 12 insertions(+), 14 deletions(-)
  

Patch

diff --git a/gdb/block.c b/gdb/block.c
index d21729f069d..5751a6b6f84 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -378,15 +378,11 @@  block_static_block (const struct block *block)
   return block;
 }
 
-/* Return the static block associated to BLOCK.  Return NULL if block
-   is NULL.  */
+/* Return the static block associated to BLOCK.  */
 
 const struct block *
 block_global_block (const struct block *block)
 {
-  if (block == NULL)
-    return NULL;
-
   while (block->superblock () != NULL)
     block = block->superblock ();
 
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index 08f7c2b4140..38225a6d9ba 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -1308,14 +1308,15 @@  add_symbol_overload_list_namespace (const char *func_name,
   /* Look in the static block.  */
   block = get_selected_block (0);
   block = block == nullptr ? nullptr : block_static_block (block);
-  if (block)
-    add_symbol_overload_list_block (name, block, overload_list);
-
-  /* Look in the global block.  */
-  block = block_global_block (block);
-  if (block)
-    add_symbol_overload_list_block (name, block, overload_list);
+  if (block != nullptr)
+    {
+      add_symbol_overload_list_block (name, block, overload_list);
 
+      /* Look in the global block.  */
+      block = block_global_block (block);
+      if (block)
+	add_symbol_overload_list_block (name, block, overload_list);
+    }
 }
 
 /* Search the namespace of the given type and namespace of and public
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 0e522b2c7f9..924bd5d8ec0 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -2623,7 +2623,8 @@  lookup_global_symbol (const char *name,
   /* If a block was passed in, we want to search the corresponding
      global block first.  This yields "more expected" behavior, and is
      needed to support 'FILENAME'::VARIABLE lookups.  */
-  const struct block *global_block = block_global_block (block);
+  const struct block *global_block
+    = block == nullptr ? nullptr : block_global_block (block);
   symbol *sym = NULL;
   if (global_block != nullptr)
     {
@@ -5861,7 +5862,7 @@  default_collect_symbol_completion_matches_break_on
 
   b = get_selected_block (0);
   surrounding_static_block = b == nullptr ? nullptr : block_static_block (b);
-  surrounding_global_block = block_global_block (b);
+  surrounding_global_block = b == nullptr : nullptr : block_global_block (b);
   if (surrounding_static_block != NULL)
     while (b != surrounding_static_block)
       {