diff options
author | Tom Tromey <tromey@adacore.com> | 2022-11-03 13:49:17 -0600 |
---|---|---|
committer | Tom Tromey <tromey@adacore.com> | 2022-12-01 11:16:41 -0700 |
commit | 55fc1623f942fba10362cb199f9356d75ca5835b (patch) | |
tree | 239cf095969423507b4600dfbad3ad28f5948d7c /gdb/dwarf2 | |
parent | bed34ce7058b56d3a1e171de31df2a0a30afb8fd (diff) | |
download | binutils-gdb-55fc1623f942fba10362cb199f9356d75ca5835b.tar.gz |
Add name canonicalization for C
PR symtab/29105 shows a number of situations where symbol lookup can
result in the expansion of too many CUs.
What happens is that lookup_signed_typename will try to look up a type
like "signed int". In cooked_index_functions::expand_symtabs_matching,
when looping over languages, the C++ case will canonicalize this type
name to be "int" instead. Then this method will proceed to expand
every CU that has an entry for "int" -- i.e., nearly all of them. A
crucial component of this is that the caller, objfile::lookup_symbol,
does not do this canonicalization, so when it tries to find the symbol
for "signed int", it fails -- causing the loop to continue.
This patch fixes the problem by introducing name canonicalization for
C. The idea here is that, by making C and C++ agree on the canonical
name when a symbol name can have multiple spellings, we avoid the bad
behavior in objfile::lookup_symbol (and any other such code -- I don't
know if there is any).
Unlike C++, C only has a few situations where canonicalization is
needed. And, in particular, due to the lack of overloading (thus
avoiding any issues in linespec) and due to the way c-exp.y works, I
think that no canonicalization is needed during symbol lookup -- only
during symtab construction. This explains why lookup_name_info is not
touched.
The stabs reader is modified on a "best effort" basis.
The DWARF reader needed one small tweak in dwarf2_name to avoid a
regression in dw2-unusual-field-names.exp. I think this is adequately
explained by the comment, but basically this is a scenario that should
not occur in real code, only the gdb test suite.
lookup_signed_typename is simplified. It used to search for two
different type names, but now gdb can search just for the canonical
form.
gdb.dwarf2/enum-type.exp needed a small tweak, because the
canonicalizer turns "unsigned integer" into "unsigned int integer".
It seems better here to use the correct C type name.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29105
Tested-by: Simon Marchi <simark@simark.ca>
Reviewed-by: Andrew Burgess <aburgess@redhat.com>
Diffstat (limited to 'gdb/dwarf2')
-rw-r--r-- | gdb/dwarf2/cooked-index.c | 8 | ||||
-rw-r--r-- | gdb/dwarf2/read.c | 18 |
2 files changed, 23 insertions, 3 deletions
diff --git a/gdb/dwarf2/cooked-index.c b/gdb/dwarf2/cooked-index.c index a580d549d0d..0aa026c7779 100644 --- a/gdb/dwarf2/cooked-index.c +++ b/gdb/dwarf2/cooked-index.c @@ -21,6 +21,7 @@ #include "dwarf2/cooked-index.h" #include "dwarf2/read.h" #include "cp-support.h" +#include "c-lang.h" #include "ada-lang.h" #include "split-name.h" #include <algorithm> @@ -210,14 +211,17 @@ cooked_index::do_finalize () m_names.push_back (std::move (canon_name)); } } - else if (entry->per_cu->lang () == language_cplus) + else if (entry->per_cu->lang () == language_cplus + || entry->per_cu->lang () == language_c) { void **slot = htab_find_slot (seen_names.get (), entry, INSERT); if (*slot == nullptr) { gdb::unique_xmalloc_ptr<char> canon_name - = cp_canonicalize_string (entry->name); + = (entry->per_cu->lang () == language_cplus + ? cp_canonicalize_string (entry->name) + : c_canonicalize_name (entry->name)); if (canon_name == nullptr) entry->canonical = entry->name; else diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index aa13d42ad77..032e20af93a 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -22014,7 +22014,10 @@ static const char * dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu, struct objfile *objfile) { - if (name && cu->lang () == language_cplus) + if (name == nullptr) + return name; + + if (cu->lang () == language_cplus) { gdb::unique_xmalloc_ptr<char> canon_name = cp_canonicalize_string (name); @@ -22022,6 +22025,14 @@ dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu, if (canon_name != nullptr) name = objfile->intern (canon_name.get ()); } + else if (cu->lang () == language_c) + { + gdb::unique_xmalloc_ptr<char> canon_name + = c_canonicalize_name (name); + + if (canon_name != nullptr) + name = objfile->intern (canon_name.get ()); + } return name; } @@ -22050,6 +22061,11 @@ dwarf2_name (struct die_info *die, struct dwarf2_cu *cu) switch (die->tag) { + /* A member's name should not be canonicalized. This is a bit + of a hack, in that normally it should not be possible to run + into this situation; however, the dw2-unusual-field-names.exp + test creates custom DWARF that does. */ + case DW_TAG_member: case DW_TAG_compile_unit: case DW_TAG_partial_unit: /* Compilation units have a DW_AT_name that is a filename, not |