[Dwarf-Discuss] How to generate DWARF info for a template alias to a raw pointer

David Blaikie dblaikie at gmail.com
Mon May 9 19:24:42 PDT 2022


On Fri, May 6, 2022 at 10:08 AM Robinson, Paul via Dwarf-Discuss
<dwarf-discuss at lists.dwarfstd.org> wrote:
>
> > Could someone help to point out what kind of DWARF info should
> > be generated for below c++ source? Thanks
> >
> > ```
> > template<class T>
> > using ptr = T*;
> >
> > ptr<int>  abc;
> > ```
> >
> > We declare a template alias here, so we may generate
> > `DW_TAG_template_type_parameter` like:
> >
> > ```
> > 0x00000057:   DW_TAG_base_type
> >                 DW_AT_name  ("int")
> >                 DW_AT_byte_size          (0x04)
> >                 DW_AT_encoding           (DW_ATE_signed)
> >
> > 0x0000005e:   DW_TAG_pointer_type
> >                 DW_AT_type    (0x00000057 "int")
> >
> > 0x00000064:   DW_TAG_template_alias
> >                 DW_AT_name  ("ptr")
> >                 DW_AT_type    (0x0000005e "int *")
> >
> > 0x00000076:     DW_TAG_template_type_parameter
> >                   DW_AT_name               ("T")
> >                   DW_AT_type  (0x00000057 "int")
> >
> > 0x0000007e:   DW_TAG_variable
> >                 DW_AT_name  ("abc")
> >                 DW_AT_type    (0x00000064 "ptr<int>")
> > ```
>
> This all looks okay to me, with DW_TAG_template_type_parameter
> being a child of DW_TAG_template_alias.  There's an alias
> named `ptr`, its formal parameter is `T`, its actual parameter
> is `int`, and so the alias is a typedef of `int *`.

One quirk here is that this encoding is using something like
"simplified template names" - clang and GCC currently straddle both
unsimplified names (class and function templates would have DW_AT_name
with template parameters, eg: "base_name<parameter_name>", but
variable templates currently get simplified names (just "base_name")
along with DW_TAG_template_*_parameter DIEs)

I'm working on changes to Clang to allow opting into the simplified,
basename-only, form for everything, including function and class
templates, but that's not fully supported/tested at the moment.

I suspect for now, lldb probably won't have a perfect time with a
simplified name for a type like above - and it's probably good to at
least be consistent with the other type templates and include the
parameters in the name. But wiring it up (if you're working with
clang) to the simplified template names support to allow this to be
simplified where possible/when that option is enabled. (there are some
cases where names can't be simplified, such as pointer non-type
template parameters (because the name can't be rebuilt readily from
the debug info - some other cases are truly lossy/not possible to
rebuild)

>
> > ` DW_TAG_template_type_parameter ` should be for a notation to
> > create a template base on another template, but as you can see
> > the referred type 0x0000005e is not a template. What kind of
> > DWARF info should we generate here? We should use
> > ` DW_TAG_typedef` instead of ` DW_TAG_template_type_parameter`
> > for this special case?
>
> DW_TAG_template_type_parameter is correctly describing the
> parameter to the template.
>
> There's no need for a DW_TAG_typedef, because DW_TAG_template_alias
> is implicitly a typedef.

Yeah, that looks/sounds good to me.


More information about the Dwarf-Discuss mailing list