Add block::function_block
authorTom Tromey <tromey@adacore.com>
Tue, 24 Oct 2023 13:53:29 +0000 (07:53 -0600)
committerTom Tromey <tromey@adacore.com>
Tue, 14 Nov 2023 15:43:34 +0000 (08:43 -0700)
This adds the method block::function_block, to easily access a block's
enclosing function block.

gdb/block.c
gdb/block.h

index 6ada69c388f15e80adf2511eb4fba63f0089a66c..e588a68aa2f37a2fd6e3dd88311910661914da9d 100644 (file)
@@ -378,6 +378,19 @@ block::global_block () const
 
 /* See block.h.  */
 
+const struct block *
+block::function_block () const
+{
+  const block *block = this;
+
+  while (block != nullptr && block->function () == nullptr)
+    block = block->superblock ();
+
+  return block;
+}
+
+/* See block.h.  */
+
 void
 block::set_compunit_symtab (struct compunit_symtab *cu)
 {
index 9fccbe02b9902aa923d186626d06144a6c3f8cd6..a29298517b0cd1216659f65234f5429fb48f9fe7 100644 (file)
@@ -273,6 +273,12 @@ struct block : public allocate_on_obstack
   bool is_global_block () const
   { return superblock () == nullptr; }
 
+  /* Return the function block for this block.  Returns nullptr if
+     there is no enclosing function, i.e., if this block is a static
+     or global block.  */
+
+  const struct block *function_block () const;
+
   /* Set the compunit of this block, which must be a global block.  */
 
   void set_compunit_symtab (struct compunit_symtab *);