gdb/dwarf: remove redundant DW_AT_containing_type checks

Message ID 20260106195457.192841-1-simon.marchi@efficios.com
State New
Headers
Series gdb/dwarf: remove redundant DW_AT_containing_type checks |

Commit Message

Simon Marchi Jan. 6, 2026, 7:54 p.m. UTC
  I noticed that some places first check if a DIE has a
DW_AT_containing_type attribute, like so:

  if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
    return NULL;

and then call function die_containing_type, which does the same check,
erroring out if the attribute does not exist.  The second check is
redundant in these cases.  There is only one call site that does not do
a check before, for which the error might be relevant.

Remove the error call from die_containing_type, making it return nullptr
if the DIE does not have a DW_AT_containing_type attribute, and remove
the redundant checks in all but that one call site.

For that one call site, error out if the return value of
die_containing_type is nullptr.  I changed the error message to be a
little more precise.

There is no expected behavior change, apart from the content of that
error message.

Change-Id: I99e89bd89d4fffef73f00e7ecc9d6ba11c0bd085
---
 gdb/dwarf2/read.c | 48 +++++++++++++++++++++++------------------------
 1 file changed, 24 insertions(+), 24 deletions(-)


base-commit: c5f2899b2b08fede7063ec2df61235d7e6e10a6e
  

Comments

Tom Tromey Jan. 6, 2026, 8:28 p.m. UTC | #1
>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:

Simon> For that one call site, error out if the return value of
Simon> die_containing_type is nullptr.  I changed the error message to be a
Simon> little more precise.

Nice, thanks.

Simon> @@ -10695,8 +10693,9 @@ dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
Simon>  	       dwarf2_full_name (fieldname, die, cu));
 
Simon>    /* Get fcontext from DW_AT_containing_type if present.  */
Simon> -  if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
Simon> -    fnp->fcontext = die_containing_type (die, cu);
Simon> +  if (struct type *containing_type = die_containing_type (die, cu);
Simon> +      containing_type != nullptr)
Simon> +    fnp->fcontext = containing_type;

This could just be a direct assignment to fnp->fcontext, I think the
'if' isn't really needed.

Approved-By: Tom Tromey <tom@tromey.com>

Tom
  
Simon Marchi Jan. 6, 2026, 8:34 p.m. UTC | #2
On 1/6/26 3:28 PM, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:
> 
> Simon> For that one call site, error out if the return value of
> Simon> die_containing_type is nullptr.  I changed the error message to be a
> Simon> little more precise.
> 
> Nice, thanks.
> 
> Simon> @@ -10695,8 +10693,9 @@ dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
> Simon>  	       dwarf2_full_name (fieldname, die, cu));
>  
> Simon>    /* Get fcontext from DW_AT_containing_type if present.  */
> Simon> -  if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
> Simon> -    fnp->fcontext = die_containing_type (die, cu);
> Simon> +  if (struct type *containing_type = die_containing_type (die, cu);
> Simon> +      containing_type != nullptr)
> Simon> +    fnp->fcontext = containing_type;
> 
> This could just be a direct assignment to fnp->fcontext, I think the
> 'if' isn't really needed.
> 
> Approved-By: Tom Tromey <tom@tromey.com>
> 
> Tom

Good point, pushed with that fixed.

Simon
  

Patch

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 05a285728da4..4822fe3209ad 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -9101,8 +9101,6 @@  rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
   if (type_die == NULL)
     return NULL;
 
-  if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
-    return NULL;
   return die_containing_type (type_die, type_cu);
 }
 
@@ -10695,8 +10693,9 @@  dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
 	       dwarf2_full_name (fieldname, die, cu));
 
   /* Get fcontext from DW_AT_containing_type if present.  */
-  if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
-    fnp->fcontext = die_containing_type (die, cu);
+  if (struct type *containing_type = die_containing_type (die, cu);
+      containing_type != nullptr)
+    fnp->fcontext = containing_type;
 
   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
@@ -11495,21 +11494,21 @@  process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
 	     class from the DW_AT_containing_type attribute.  This use of
 	     DW_AT_containing_type is a GNU extension.  */
 
-	  if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
+	  if (struct type *containing_type = die_containing_type (die, cu);
+	      containing_type != nullptr)
 	    {
-	      struct type *t = die_containing_type (die, cu);
-
-	      set_type_vptr_basetype (type, t);
-	      if (type == t)
+	      set_type_vptr_basetype (type, containing_type);
+	      if (type == containing_type)
 		{
 		  int i;
 
 		  /* Our own class provides vtbl ptr.  */
-		  for (i = t->num_fields () - 1;
-		       i >= TYPE_N_BASECLASSES (t);
+		  for (i = containing_type->num_fields () - 1;
+		       i >= TYPE_N_BASECLASSES (containing_type);
 		       --i)
 		    {
-		      const char *fieldname = t->field (i).name ();
+		      const char *fieldname
+			= containing_type->field (i).name ();
 
 		      if (is_vtable_name (fieldname, cu))
 			{
@@ -11519,15 +11518,14 @@  process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
 		    }
 
 		  /* Complain if virtual function table field not found.  */
-		  if (i < TYPE_N_BASECLASSES (t))
+		  if (i < TYPE_N_BASECLASSES (containing_type))
 		    complaint (_("virtual function table pointer "
 				 "not found when defining class '%s'"),
 			       type->name () ? type->name () : "");
 		}
 	      else
-		{
-		  set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
-		}
+		set_type_vptr_fieldno (type,
+				       TYPE_VPTR_FIELDNO (containing_type));
 	    }
 	  else if (cu->producer_is_xlc ())
 	    {
@@ -12909,6 +12907,13 @@  read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
   to_type = die_type (die, cu);
   domain = die_containing_type (die, cu);
 
+  if (domain == nullptr)
+    error (_(DWARF_ERROR_PREFIX
+	     "Missing DW_AT_containing_type attribute on "
+	     "DW_TAG_ptr_to_member_type DIE at %s [in module %s]"),
+	   sect_offset_str (die->sect_off),
+	   objfile_name (cu->per_objfile->objfile));
+
   /* The calls above may have already set the type for this DIE.  */
   type = get_die_type (die, cu);
   if (type)
@@ -16816,15 +16821,10 @@  set_descriptive_type (struct type *type, struct die_info *die,
 static struct type *
 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct attribute *type_attr;
-  struct objfile *objfile = cu->per_objfile->objfile;
+  attribute *type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
 
-  type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
-  if (!type_attr)
-    error (_(DWARF_ERROR_PREFIX
-	     "Problem turning containing type into gdb type "
-	     "[in module %s]"),
-	   objfile_name (objfile));
+  if (type_attr == nullptr)
+    return nullptr;
 
   return lookup_die_type (die, type_attr, cu);
 }