[cfe-dev] Using clang-tool to Exact type names in template specification arguments

David Rector via cfe-dev cfe-dev at lists.llvm.org
Thu Aug 13 07:13:57 PDT 2020

Ha, deja vu - Pratyush (cc’d) was just dealing with this exact issue in another context the other day, and Oliver we were kind of dealing with this same general issue the other week re: attributes.  

You definitely *should* be able to get the full type sugar without fuss.  The obstacle to that is in  

Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, QualType ParamType, …)

linked to here: https://github.com/llvm/llvm-project/blob/master/clang/lib/Sema/SemaTemplate.cpp#L6688 <https://github.com/llvm/llvm-project/blob/master/clang/lib/Sema/SemaTemplate.cpp#L6688>.

For some reason within that function the Converted template argument is always constructed with Context.getCanonicalType(ParamType), instead of ParamType, which erases the type sugar needlessly.

Pratyush has solved this for the C++17 branch of that function in https://reviews.llvm.org/D77598 <https://reviews.llvm.org/D77598>, but Pratyush it is probably a good idea to replace all other instances of Context.getCanonicalType(XYZ) in that function with XYZ, e.g. this line (among others):
https://github.com/llvm/llvm-project/blob/master/clang/lib/Sema/SemaTemplate.cpp#L6955 <https://github.com/llvm/llvm-project/blob/master/clang/lib/Sema/SemaTemplate.cpp#L6955>

I suspect no tests will be broken with such a change, and the result will be that template arguments will always have the proper sugar available.

- Dave

> On Aug 13, 2020, at 12:30 AM, David Blaikie via cfe-dev <cfe-dev at lists.llvm.org> wrote:
> Once you have the FunctionDecl you've gone too far and any type sugar
> is lost, I believe. (because func<vectors32_t> is the same function as
> func<unsigned int[32]> or any other typedef/alias/etc of that type
> parameter, etc)
> I think you'd have to be able to see the call to the function, and go
> from there to find the type as written.
> On Wed, Aug 12, 2020 at 8:14 PM Oliver Zhang via cfe-dev
> <cfe-dev at lists.llvm.org> wrote:
>> Hi,
>> I'm using clang-tool to extract type names in template arguments. For example,
>>> typedef unsigned int vectors32_t[32];
>>> template <typename T>
>>> func(T *const vec) {
>>>    ...
>>> }
>>> int main() {
>>>    ...
>>>    func<vector32_t>(...);
>>>    ...
>>> }
>> I'd like to get the type name "vector32_t" from func's template argument. So I use clang::FunctionDecl::getTemplateSpecializationArgs() to get func's template argument list, extract the argument from the list, and then try to get the type name for the argument by using clang::TemplateArgument::getAsType().getAsString(). However, "unsigned int [32]" is returned rather than "vector32_t".
>> Any thoughts on how I can extract the type alias name "vector32_t" in my above example?
>> Thanks,
>> Oliver
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at lists.llvm.org
>> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20200813/203ee286/attachment.html>

More information about the cfe-dev mailing list