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

via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 12 08:40:08 PDT 2025


ykhatav wrote:

> > Are you suggesting that for the implementation to be considered as complete, both v1 and v2 should have the same type information? I.e "v1" type should point to 0x48 instead of 0x6d? As per my understanding based on the DWARF output below, the type for "trait::type"(0x6d) DOES reference a DW_TAG_template_type_parameter(0x67) entry:
> 
> No, I don't think it should point to 0x67 instead of 0x6d - you could imagine a trait with a resulting type that has nothing to do with T (like `std::conditional_v<T, bool, char>`).
> 
> My contention is that DWARF doesn't have a way to express this - and I don't really have a good idea for novel solutions/additions to DWARF etc to handle this. And without that, only changing the raw `T` references is of limited value.
> 
> That's why I don't think this is a great direction to go.

I created a sample test case containing a conditional trait type:
`````template <typename T>
struct trait {
  using type = std::conditional_t<std::is_integral_v<T>, bool, double>;
};

template <typename T>
struct other {
  typename trait<T>::type v1;
  T v2;
};

int main() {
  other<int> obj1;
  other<float> obj2;
  return 0;
}
`````

If you run the above test case with -fdebug-template-parameter-as-type, we get the attached DWARF(conditional_trait_dwarf_w_change.txt). Note that this DWARF has the added benefit of indicating whether the type came from the true or false branch. While the type of trait remains the same with and without -fdebug-template-parameter-as-type , I believe the new DWARF adds more context about types in templates and conditionals.  Could you please take a look at the attached DWARF and let me know your concerns with the implementation? I have also attached the DWARF output without  -fdebug-template-parameter-as-type for your reference. 

[conditional_trait_dwarf_orig.txt](https://github.com/user-attachments/files/19212989/conditional_trait_dwarf_orig.txt)
[conditional_trait_dwarf_w_change.txt](https://github.com/user-attachments/files/19212995/conditional_trait_dwarf_w_change.txt)


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


More information about the cfe-commits mailing list