From a118b5598bb93a31c989bfaaa56cef69bed5943d Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Tue, 6 Jan 2026 16:45:49 -0500 Subject: [PATCH] gdb/dwarf: add dwarf2_per_cu::as_signatured_type Add dwarf2_per_cu::as_signatured_type, which returns the dwarf2_per_cu cast to signatured_type if it is indeed a signatured type (type unit), and nullptr otherwise. It can replace a few spots where we use the pattern "check if it's a signatured_type and cast". Change-Id: I10326cd597a0306b15e2ffd7572b79e96068c81a Approved-By: Tom Tromey --- gdb/dwarf2/index-write.c | 6 ++-- gdb/dwarf2/read.c | 77 +++++++++++++++------------------------- gdb/dwarf2/read.h | 15 ++++++++ 3 files changed, 47 insertions(+), 51 deletions(-) diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c index ed62dd76605..d31e7b631b8 100644 --- a/gdb/dwarf2/index-write.c +++ b/gdb/dwarf2/index-write.c @@ -1304,9 +1304,9 @@ get_unit_lists (const dwarf2_per_bfd &per_bfd) std::vector type_units; for (const auto &unit : per_bfd.all_units) - if (unit->is_debug_types ()) - type_units.emplace_back (static_cast - (unit.get ())); + if (const signatured_type *sig_type = unit->as_signatured_type (); + sig_type != nullptr) + type_units.emplace_back (sig_type); else comp_units.emplace_back (unit.get ()); diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 3289f40eef1..2e222766579 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -938,7 +938,7 @@ static struct type *get_DW_AT_signature_type (struct die_info *, const struct attribute *, struct dwarf2_cu *); -static void load_full_type_unit (dwarf2_per_cu *per_cu, +static void load_full_type_unit (signatured_type *sig_type, dwarf2_per_objfile *per_objfile); static void read_signatured_type (signatured_type *sig_type, @@ -1040,8 +1040,9 @@ dwarf2_queue_item::~dwarf2_queue_item () void dwarf2_per_cu_deleter::operator() (dwarf2_per_cu *data) { - if (data->is_debug_types ()) - delete static_cast (data); + if (signatured_type *sig_type = data->as_signatured_type (); + sig_type != nullptr) + delete sig_type; else delete data; } @@ -1623,8 +1624,9 @@ static dwarf2_cu * load_cu (dwarf2_per_cu *per_cu, dwarf2_per_objfile *per_objfile, bool skip_partial) { - if (per_cu->is_debug_types ()) - load_full_type_unit (per_cu, per_objfile); + if (signatured_type *sig_type = per_cu->as_signatured_type (); + sig_type != nullptr) + load_full_type_unit (sig_type, per_objfile); else load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal); @@ -2950,13 +2952,11 @@ cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu *this_cu, dwarf2_cu *existing_cu, enum language pretend_language) { - struct signatured_type *sig_type; + signatured_type *sig_type = this_cu->as_signatured_type (); - /* Verify we can do the following downcast, and that we have the - data we need. */ - gdb_assert (this_cu->is_debug_types () && this_cu->reading_dwo_directly); - sig_type = (struct signatured_type *) this_cu; - gdb_assert (sig_type->dwo_unit != NULL); + gdb_assert (sig_type != nullptr); + gdb_assert (sig_type->reading_dwo_directly); + gdb_assert (sig_type->dwo_unit != nullptr); dwarf2_cu *cu; @@ -3007,7 +3007,6 @@ cutu_reader::cutu_reader (dwarf2_per_cu &this_cu, struct dwarf2_section_info *section = this_cu.section (); bfd *abfd = section->get_bfd_owner (); const gdb_byte *begin_info_ptr; - struct signatured_type *sig_type = NULL; struct dwarf2_section_info *abbrev_section; /* Non-zero if CU currently points to a DWO file and we need to reread it. When this happens we need to reread the skeleton die @@ -3076,15 +3075,13 @@ cutu_reader::cutu_reader (dwarf2_per_cu &this_cu, } else { - if (this_cu.is_debug_types ()) + if (signatured_type *sig_type = this_cu.as_signatured_type (); + sig_type != nullptr) { m_info_ptr = read_and_check_unit_head (&cu->header, section, abbrev_section, m_info_ptr, ruh_kind::TYPE); - /* Since per_cu is the first member of struct signatured_type, - we can go from a pointer to one to a pointer to the other. */ - sig_type = (struct signatured_type *) &this_cu; gdb_assert (sig_type->signature == cu->header.signature); gdb_assert (sig_type->type_offset_in_tu == cu->header.type_offset_in_tu); @@ -3578,9 +3575,9 @@ cooked_index_worker_debug_info::process_type_units sorted_by_abbrev.reserve (per_objfile->per_bfd->num_type_units); for (const auto &cu : per_objfile->per_bfd->all_units) - if (cu->is_debug_types ()) + if (signatured_type *sig_type = cu->as_signatured_type (); + sig_type != nullptr) { - auto sig_type = static_cast (cu.get ()); sect_offset abbrev_offset = read_abbrev_offset (per_objfile, sig_type->section (), sig_type->sect_off ()); @@ -4164,11 +4161,9 @@ process_queue (dwarf2_per_objfile *per_objfile) char buf[100]; std::optional> start_time; - if (per_cu->is_debug_types ()) + if (signatured_type *sig_type = per_cu->as_signatured_type (); + sig_type != nullptr) { - struct signatured_type *sig_type = - (struct signatured_type *) per_cu; - sprintf (buf, "TU %s at offset %s", hex_string (sig_type->signature), sect_offset_str (per_cu->sect_off ())); @@ -4961,10 +4956,9 @@ process_full_type_unit (dwarf2_cu *cu) { dwarf2_per_objfile *per_objfile = cu->per_objfile; struct compunit_symtab *cust; - struct signatured_type *sig_type; + signatured_type *sig_type = cu->per_cu->as_signatured_type (); - gdb_assert (cu->per_cu->is_debug_types ()); - sig_type = (struct signatured_type *) cu->per_cu; + gdb_assert (sig_type != nullptr); /* Clear the list here in case something was left over. */ cu->method_list.clear (); @@ -6198,10 +6192,9 @@ dwarf2_cu::setup_type_unit_groups (struct die_info *die) int first_time; struct attribute *attr; unsigned int i; - struct signatured_type *sig_type; + signatured_type *sig_type = per_cu->as_signatured_type (); - gdb_assert (per_cu->is_debug_types ()); - sig_type = (struct signatured_type *) per_cu; + gdb_assert (sig_type != nullptr); attr = dwarf2_attr (die, DW_AT_stmt_list, this); @@ -8171,9 +8164,9 @@ dwo_unit * cutu_reader::lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir) { - gdb_assert (cu->per_cu->is_debug_types ()); + signatured_type *sig_type = cu->per_cu->as_signatured_type (); - signatured_type *sig_type = (signatured_type *) cu->per_cu; + gdb_assert (sig_type != nullptr); return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1); } @@ -11908,12 +11901,9 @@ process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu) actually available. Note that we do not want to do this for all enums which are just declarations, because C++0x allows forward enum declarations. */ - if (cu->per_cu->is_debug_types () - && die_is_declaration (die, cu)) + if (signatured_type *sig_type = cu->per_cu->as_signatured_type (); + sig_type != nullptr && die_is_declaration (die, cu)) { - struct signatured_type *sig_type; - - sig_type = (struct signatured_type *) cu->per_cu; gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0); if (sig_type->type_offset_in_section != die->sect_off) return; @@ -18133,23 +18123,14 @@ get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr, } } -/* Load the DIEs associated with type unit PER_CU into memory. */ +/* Load the DIEs associated with type unit SIG_TYPE into memory. */ static void -load_full_type_unit (dwarf2_per_cu *per_cu, dwarf2_per_objfile *per_objfile) +load_full_type_unit (signatured_type *sig_type, dwarf2_per_objfile *per_objfile) { - struct signatured_type *sig_type; - - /* We have the per_cu, but we need the signatured_type. - Fortunately this is an easy translation. */ - gdb_assert (per_cu->is_debug_types ()); - sig_type = (struct signatured_type *) per_cu; - - gdb_assert (per_objfile->get_cu (per_cu) == nullptr); - + gdb_assert (per_objfile->get_cu (sig_type) == nullptr); read_signatured_type (sig_type, per_objfile); - - gdb_assert (per_objfile->get_cu (per_cu) != nullptr); + gdb_assert (per_objfile->get_cu (sig_type) != nullptr); } /* Read in a signatured type and build its CU and DIEs. diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h index 1a6d9767413..4e89f0ed1fc 100644 --- a/gdb/dwarf2/read.h +++ b/gdb/dwarf2/read.h @@ -284,6 +284,10 @@ public: bool is_debug_types () const { return m_is_debug_types; } + /* If this dwarf2_per_cu is a signatured_type, return "this" cast to + signatured_type. Otherwise, return nullptr. */ + signatured_type *as_signatured_type (); + dwarf2_per_bfd *per_bfd () const { return m_per_bfd; } @@ -459,6 +463,17 @@ struct signatured_type : public dwarf2_per_cu using signatured_type_up = std::unique_ptr; +/* See dwarf2_per_cu declaration. */ + +inline signatured_type * +dwarf2_per_cu::as_signatured_type () +{ + if (m_is_debug_types) + return static_cast (this); + + return nullptr; +} + /* Hash a signatured_type object based on its signature. */ struct signatured_type_hash