From: Simon Marchi Date: Tue, 6 Jan 2026 19:54:52 +0000 (-0500) Subject: gdb/dwarf: remove redundant DW_AT_containing_type checks X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6a275e7ffe2c520c3225e75d6e4eb23d87207a7c;p=thirdparty%2Fbinutils-gdb.git gdb/dwarf: remove redundant DW_AT_containing_type checks 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 Approved-By: Tom Tromey --- diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 40cd78431df..6453c53e4ad 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,7 @@ 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); + fnp->fcontext = die_containing_type (die, cu); /* 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 +11492,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 +11516,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 +12905,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 +16819,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); }