Skip to content

Commit

Permalink
[Clang][Index] Add support for dependent class scope explicit special…
Browse files Browse the repository at this point in the history
…izations of function templates to USRGenerator (llvm#98027)

Given the following:
```
template<typename T>
struct A
{
    void f(int); // #1
    
    template<typename U>
    void f(U); // #2
    
    template<>
    void f<int>(int); // #3
};
```
Clang will generate the same USR for `#1` and `#2`. This patch fixes the
issue by including the template arguments of dependent class scope
explicit specializations in their USRs.
  • Loading branch information
sdkrystian authored Jul 8, 2024
1 parent 359c64f commit d528537
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
18 changes: 13 additions & 5 deletions clang/lib/Index/USRGeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,20 @@ void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
!D->hasAttr<OverloadableAttr>())
return;

if (const TemplateArgumentList *
SpecArgs = D->getTemplateSpecializationArgs()) {
if (D->isFunctionTemplateSpecialization()) {
Out << '<';
for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) {
Out << '#';
VisitTemplateArgument(SpecArgs->get(I));
if (const TemplateArgumentList *SpecArgs =
D->getTemplateSpecializationArgs()) {
for (const auto &Arg : SpecArgs->asArray()) {
Out << '#';
VisitTemplateArgument(Arg);
}
} else if (const ASTTemplateArgumentListInfo *SpecArgsWritten =
D->getTemplateSpecializationArgsAsWritten()) {
for (const auto &ArgLoc : SpecArgsWritten->arguments()) {
Out << '#';
VisitTemplateArgument(ArgLoc.getArgument());
}
}
Out << '>';
}
Expand Down
15 changes: 15 additions & 0 deletions clang/test/Index/USR/func-template.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: c-index-test core -print-source-symbols -- %s | FileCheck %s

template<typename T>
struct A {
void f(int);
// CHECK: {{[0-9]+}}:8 | instance-method/C++ | f | c:@ST>1#T@A@F@f#I# |

template<typename U>
void f(U);
// CHECK: {{[0-9]+}}:8 | instance-method/C++ | f | c:@ST>1#T@A@FT@>1#Tf#t1.0#v# |

template<>
void f<int>(int);
// CHECK: {{[0-9]+}}:8 | instance-method/C++ | f | c:@ST>1#T@A@F@f<#I>#I# |
};

0 comments on commit d528537

Please sign in to comment.