[clang] [llvm] Add support for template as type parameter (PR #127654)

via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 19 11:26:48 PST 2025


ykhatav wrote:

> Couple high-level of questions/comments:
> 
> 1. How is GDB going to make use of this additional info?
Currently with this implementation, GDB does not support printing template as a type and will display it as an unknown type. So, the implementation is currently behind a switch until support is added to GDB. 

> 2. Does GCC already do this? Or is this a Clang-specific extension?
No GCC's behavior is the same as current Clang's behavior

> 3. Unless other reviewers think otherwise, I think it might be worth a post on the LLVM Discourse. LLDB would love to have generic structure/function template definitions in DWARF (particularly for expression evaluation), but that's a much larger feature. But if/when we eventually get to exploring generic template definitions in DWARF, it would be nice if it builds on top of/complements this work.
Do you suggest creating a RFC first to get broader feedback on the design? 
> 4. Some more tests would be useful to understand the intentions of this feature. E.g., what does your DWARF output look like for cases like:
> 
> ```
> template <typename T>
> structure Bar {
>   T mem;
> };
> 
> template <typename T>
> structure Foo {
>   Bar<T> mem;
> };
> 
> Foo<Bar<int>> f;
> ```

Sure, I'll work on adding more test cases. As for the dwarf output, it looks like this:
```
0x0000000c: DW_TAG_compile_unit
              DW_AT_producer    ("clang")
              DW_AT_language    (DW_LANG_C_plus_plus_14)
              DW_AT_name        ("test1.cpp")
              DW_AT_str_offsets_base    (0x00000008)
              DW_AT_stmt_list   (0x00000000)
              DW_AT_comp_dir    ("")
              DW_AT_low_pc      (0x0000000000001130)
              DW_AT_high_pc     (0x000000000000113f)
              DW_AT_addr_base   (0x00000008)

0x00000023:   DW_TAG_subprogram
                DW_AT_low_pc    (0x0000000000001130)
                DW_AT_high_pc   (0x000000000000113f)
                DW_AT_frame_base        (DW_OP_reg6 RBP)
                DW_AT_name      ("main")
                DW_AT_decl_file ("")
                DW_AT_decl_line (10)
                DW_AT_type      (0x0000003e "int")
                DW_AT_external  (true)

0x00000032:     DW_TAG_variable
                  DW_AT_location        (DW_OP_fbreg -8)
                  DW_AT_name    ("f")
                  DW_AT_decl_file       ("")
                  DW_AT_decl_line       (11)
                  DW_AT_type    (0x00000042 "Foo<Bar<int> >")

0x0000003d:     NULL
0x0000003e:   DW_TAG_base_type
                DW_AT_name      ("int")
                DW_AT_encoding  (DW_ATE_signed)
                DW_AT_byte_size (0x04)

0x00000042:   DW_TAG_structure_type
                DW_AT_calling_convention        (DW_CC_pass_by_value)
                DW_AT_name      ("Foo<Bar<int> >")
                DW_AT_byte_size (0x04)
                DW_AT_decl_file ("")
                DW_AT_decl_line (7)

0x00000048:     DW_TAG_template_type_parameter
                  DW_AT_type    (0x00000058 "Bar<int>")
                  DW_AT_name    ("T")

0x0000004e:     DW_TAG_member
                  DW_AT_name    ("mem")
                  DW_AT_type    (0x00000074 "Bar<Bar<int> >")
                  DW_AT_decl_file       ("")
                  DW_AT_decl_line       (8)
                  DW_AT_data_member_location    (0x00)

0x00000057:     NULL
0x00000058:   DW_TAG_structure_type
                DW_AT_calling_convention        (DW_CC_pass_by_value)
                DW_AT_name      ("Bar<int>")
                DW_AT_byte_size (0x04)
                DW_AT_decl_file ("")
                DW_AT_decl_line (2)

0x0000005e:     DW_TAG_template_type_parameter
                  DW_AT_type    (0x0000003e "int")
                  DW_AT_name    ("T")

0x00000064:     DW_TAG_member
                  DW_AT_name    ("mem")
                  DW_AT_type    (0x0000006e "T")
                  DW_AT_decl_file       ("")
                  DW_AT_decl_line       (3)
                  DW_AT_data_member_location    (0x00)

0x0000006d:     NULL

0x0000006e:   DW_TAG_template_type_parameter
                DW_AT_type      (0x0000003e "int")
                DW_AT_name      ("T")

0x00000074:   DW_TAG_structure_type
                DW_AT_calling_convention        (DW_CC_pass_by_value)
                DW_AT_name      ("Bar<Bar<int> >")
                DW_AT_byte_size (0x04)
                DW_AT_decl_file ("")
                DW_AT_decl_line (2)

0x0000007a:     DW_TAG_template_type_parameter
                  DW_AT_type    (0x00000058 "Bar<int>")
                  DW_AT_name    ("T")

0x00000080:     DW_TAG_member
                  DW_AT_name    ("mem")
                  DW_AT_type    (0x0000008a "T")
                  DW_AT_decl_file       ("")
                  DW_AT_decl_line       (3)
                  DW_AT_data_member_location    (0x00)

0x00000089:     NULL

0x0000008a:   DW_TAG_template_type_parameter
                DW_AT_type      (0x00000058 "Bar<int>")
                DW_AT_name      ("T")

0x00000090:   NULL

```

https://github.com/llvm/llvm-project/pull/127654


More information about the cfe-commits mailing list