binutils-gdb/gdb/dwarf2
Nils-Christian Kempke 945e66a74d gdb, dwarf: create symbols for template tags without names
The following GDB behavior was also reported as a GDB bug in

  https://sourceware.org/bugzilla/show_bug.cgi?id=28396

I will reiterate the problem a bit and give some more information here.
This patch closes the above mentioned bug.

The DWARF 5 standard 2.23 'Template Parameters' reads:

   A template type parameter is represented by a debugging information
   entry with the tag DW_TAG_template_type_parameter.  A template value
   parameter is represented by a debugging information entry with the tag
   DW_TAG_template_value_parameter.  The actual template parameter entries
   appear in the same order as the corresponding template formal
   parameter declarations in the source progam.

   A type or value parameter entry may have a DW_AT_name attribute, whose
   value is a null-terminated string containing the name of the
   corresponding formal parameter.

So the DW_AT_name attribute for DW_TAG_template_type_parameter and
DW_TAG_template_value_parameter is optional.

Within GDB, creating a new symbol from some read DIE usually requires the
presence of a DW_AT_name for the DIE (an exception here is the case of
unnamed namespaces or the existence of a linkage name).

This patch makes the presence of the DW_AT_name for template value/type
tags optional, similar to the unnamed namespaces.

For unnamed namespaces dwarf2_name simply returns the constant string
CP_ANONYMOUS_NAMESPACE_STR '(anonymous namespace)'.  For template tags a
case was added to the switch statement calling the
unnamed_template_tag_name helper.  Within the scope of parent which
the template parameter is a child of, the helper counts the position
of the template tag within the unnamed template tags and returns
'<unnamedNUMBER>' where NUMBER is its position.  This way we end up with
unique names within the respective scope of the function/class/struct
(these are the only currenltly supported template kinds within GDB and
usually the compilers) where we discovered the template tags in.

While I do not know of a way to bring GCC to emit template tags without
names there is one for clang/icpx.  Consider the following example

  template<typename A, typename B, typename C>
  class Foo {};

  template<typename, typename B, typename>
  class Foo;

  int main () {
    Foo<double, int, float> f;
    return 0;
  }

The forward declaration for 'Foo' with the missing template type names
'A' and 'C' makes clang emit a bunch of template tags without names:

 ...
  <2><43>: Abbrev Number: 3 (DW_TAG_variable)
    <44>   DW_AT_location    : 2 byte block: 91 78      (DW_OP_fbreg: -8)
    <47>   DW_AT_name        : (indirect string, offset: 0x63): f
    <4b>   DW_AT_decl_file   : 1
    <4c>   DW_AT_decl_line   : 8
    <4d>   DW_AT_type        : <0x59>
 ...
 <1><59>: Abbrev Number: 5 (DW_TAG_class_type)
    <5a>   DW_AT_calling_convention: 5  (pass by value)
    <5b>   DW_AT_name        : (indirect string, offset: 0x74): Foo<double, int, float>
    <5f>   DW_AT_byte_size   : 1
    <60>   DW_AT_decl_file   : 1
    <61>   DW_AT_decl_line   : 2
 <2><62>: Abbrev Number: 6 (DW_TAG_template_type_param)
    <63>   DW_AT_type        : <0x76>
 <2><67>: Abbrev Number: 7 (DW_TAG_template_type_param)
    <68>   DW_AT_type        : <0x52>
    <6c>   DW_AT_name        : (indirect string, offset: 0x6c): B
 <2><70>: Abbrev Number: 6 (DW_TAG_template_type_param)
    <71>   DW_AT_type        : <0x7d>
 ...

Befor this patch, GDB would not create any symbols for the read template
tag DIEs and thus lose knowledge about them.  Breaking at the return
statement and printing f's type would read

  (gdb) ptype f
  type = class Foo<double, int, float> [with B = int] {
      <no data fields>
  }

After this patch GDB does generate symbols from the DWARF (with their
artificial names:

  (gdb) ptype f
  type = class Foo<double, int, float> [with <unnamed0> = double, B = int,
  <unnamed1> = float] {
      <no data fields>
  }

The same principle theoretically applies to template functions.  Also
here, GDB would not record unnamed template TAGs but I know of no visual
way to trigger and test this changed behavior.  Template functions do
not emit a '[with...]' list and their name generation also does not
suffer from template tags without names.  GDB does not check whether or
not a template tag has a name in 'dwarf2_compute_name' and thus, the
names of the template functions are created independently of whether or
not the template TAGs have a DW_TAT_name attribute.  A testcase has
been added in the gdb.dwarf2 for template classes and structs.

Bug:  https://sourceware.org/bugzilla/show_bug.cgi?id=28396
2022-08-31 10:28:40 +02:00
..
abbrev-cache.c Introduce DWARF abbrev cache 2022-04-12 09:31:16 -06:00
abbrev-cache.h Add comments to dwarf2/abbrev-cache.h 2022-04-16 13:58:34 -06:00
abbrev.c Fix method naming bug in new DWARF indexer 2022-04-22 06:40:49 -06:00
abbrev.h Statically examine abbrev properties 2022-04-12 09:31:16 -06:00
attribute.c Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
attribute.h Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
comp-unit-head.c Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
comp-unit-head.h Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
cooked-index.c [gdb/symtab] Add get/set functions for per_cu->lang/unit_type 2022-07-04 10:28:42 +02:00
cooked-index.h [gdb/symtab] Fix data race in cooked_index_functions::expand_symtabs_matching 2022-07-14 20:47:54 +02:00
cu.c gdb: add "id" fields to identify symtabs and subfiles 2022-07-29 20:54:49 -04:00
cu.h [gdb/symtab] Add dwarf2_cu::lang () 2022-07-12 17:12:17 +02:00
die.h Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
dwz.c Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
dwz.h Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
expr.c gdb: call check_typedef at beginning of dwarf_expr_context::fetch_result 2022-08-18 12:03:39 -04:00
expr.h Fix some stale header names from dwarf files 2022-03-22 09:33:49 +00:00
file-and-dir.h Add "fullname" handling to file_and_directory 2022-04-12 09:31:16 -06:00
frame-tailcall.c Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
frame-tailcall.h Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
frame.c Use registry in gdbarch 2022-08-04 13:28:04 -06:00
frame.h Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
index-cache.c Use gdb_bfd_ref_ptr in objfile 2022-08-03 13:26:58 -06:00
index-cache.h Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
index-common.c Add new overload of dwarf5_djb_hash 2022-04-12 09:31:16 -06:00
index-common.h Add new overload of dwarf5_djb_hash 2022-04-12 09:31:16 -06:00
index-write.c Use gdb_bfd_ref_ptr in objfile 2022-08-03 13:26:58 -06:00
index-write.h Rename write_psymtabs_to_index 2022-04-12 09:31:16 -06:00
leb.c Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
leb.h Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
line-header.c gdb: add "id" fields to identify symtabs and subfiles 2022-07-29 20:54:49 -04:00
line-header.h gdb: add "id" fields to identify symtabs and subfiles 2022-07-29 20:54:49 -04:00
loc.c Use gdb_bfd_ref_ptr in objfile 2022-08-03 13:26:58 -06:00
loc.h gdb/dwarf: pass an array of values to the dwarf evaluator 2022-04-03 17:18:20 +01:00
macro.c gdb/dwarf: pass a file_entry to line_header::file_file_name 2022-07-29 20:54:49 -04:00
macro.h Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
mapped-index.h Fix crash when creating index from index 2022-05-04 08:38:05 -06:00
public.h Always use dwarf2_initialize_objfile 2022-04-21 12:18:25 -06:00
read.c gdb, dwarf: create symbols for template tags without names 2022-08-31 10:28:40 +02:00
read.h [gdb/symtab] Make per_cu->m_lang atomic 2022-07-14 08:19:00 +02:00
sect-names.h Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
section.c Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
section.h Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
stringify.c Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
stringify.h Automatic Copyright Year update after running gdb/copyright.py 2022-01-01 19:13:23 +04:00
tag.h Fix .debug_names regression with new indexer 2022-04-29 13:16:44 -06:00