diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp --- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -948,6 +948,12 @@ int [[v^ar]] = 0; void foo(int s = [[var]]); )cpp", + + R"cpp( + template + class [[Fo^o]] {}; + void func([[Foo]]); + )cpp", }; for (const char *Test : Tests) { Annotations T(Test); diff --git a/clang/lib/Index/IndexingContext.cpp b/clang/lib/Index/IndexingContext.cpp --- a/clang/lib/Index/IndexingContext.cpp +++ b/clang/lib/Index/IndexingContext.cpp @@ -167,6 +167,13 @@ if (const auto *Parent = dyn_cast(D->getDeclContext())) return isTemplateImplicitInstantiation(Parent); } + + // Incomplete template specialization is not instantiated, we still treat it + // as an instantiation as we'd like to keep the canonicalized result + // consistent. + if (TKind == TSK_Undeclared) + return dyn_cast(D); + switch (TKind) { case TSK_Undeclared: case TSK_ExplicitSpecialization: @@ -206,6 +213,9 @@ static const Decl *adjustTemplateImplicitInstantiation(const Decl *D) { if (const ClassTemplateSpecializationDecl * SD = dyn_cast(D)) { + // Fallback to primary template if this is an incomplete specialization. + if (SD->getTemplateSpecializationKind() == TSK_Undeclared) + return SD->getSpecializedTemplate()->getTemplatedDecl(); return SD->getTemplateInstantiationPattern(); } else if (const FunctionDecl *FD = dyn_cast(D)) { return FD->getTemplateInstantiationPattern(); diff --git a/clang/test/Index/Core/index-instantiated-source.cpp b/clang/test/Index/Core/index-instantiated-source.cpp --- a/clang/test/Index/Core/index-instantiated-source.cpp +++ b/clang/test/Index/Core/index-instantiated-source.cpp @@ -86,3 +86,25 @@ (void)TT::NestedType::Enum::EnumCase; // CHECK: [[@LINE-1]]:31 | enumerator/C | EnumCase | c:@ST>2#T#T@TemplateClass@S@NestedType@E@Enum@EnumCase | } + +namespace index_specialization { +template +class Foo {}; + +// if there are no explicit template specializations provided, report the +// primary templates. +Foo* t1; // incomplete instantiation. +// CHECK: [[@LINE-1]]:1 | class(Gen)/C++ | Foo | c:@N@index_specialization@ST>1#T@Foo | | Ref,RelCont | rel: 1 + +Foo t2; +// CHECK: [[@LINE-1]]:1 | class(Gen)/C++ | Foo | c:@N@index_specialization@ST>1#T@Foo | | Ref,RelCont | rel: 1 + + +template +class Bar {}; +template<> +class Bar {}; +// report the explicit template specialization if it exists. +Bar* t3; +// CHECK: [[@LINE-1]]:1 | class(Gen,TS)/C++ | Bar | c:@N@index_specialization@S@Bar>#I | | Ref,RelCont | rel: 1 +} diff --git a/clang/test/Index/Core/index-source.cpp b/clang/test/Index/Core/index-source.cpp --- a/clang/test/Index/Core/index-source.cpp +++ b/clang/test/Index/Core/index-source.cpp @@ -321,7 +321,7 @@ void functionSp, Record::C>() { // CHECK: [[@LINE-1]]:6 | function(Gen,TS)/C++ | functionSp | c:@F@functionSp<#$@S@SpecializationDecl>#$@S@Cls#VI2># | __Z10functionSpI18SpecializationDeclI3ClsELi2EEvv | Def,RelSpecialization | rel: 1 // CHECK: RelSpecialization | functionSp | c:@FT@>2#T#NIfunctionSp#v# -// CHECK: [[@LINE-3]]:17 | class(Gen,TS)/C++ | SpecializationDecl | c:@S@SpecializationDecl>#$@S@Cls | | Ref,RelCont | rel: 1 +// CHECK: [[@LINE-3]]:17 | class(Gen)/C++ | SpecializationDecl | c:@ST>1#T@SpecializationDecl | | Ref,RelCont | rel: 1 // CHECK: [[@LINE-4]]:36 | class/C++ | Cls | c:@S@Cls | | Ref,RelCont | rel: 1 // CHECK: [[@LINE-5]]:50 | static-property/C++ | C | c:@S@Record@C | __ZN6Record1CE | Ref,RelCont | rel: 1 // CHECK: [[@LINE-6]]:42 | struct/C++ | Record | c:@S@Record | | Ref,RelCont | rel: 1 @@ -332,7 +332,7 @@ template<> class ClassWithCorrectSpecialization, Record::C> { }; -// CHECK: [[@LINE-1]]:38 | class(Gen,TS)/C++ | SpecializationDecl | c:@S@SpecializationDecl>#$@S@Cls | | Ref | rel: 0 +// CHECK: [[@LINE-1]]:38 | class(Gen)/C++ | SpecializationDecl | c:@ST>1#T@SpecializationDecl | | Ref | rel: 0 // CHECK: [[@LINE-2]]:57 | class/C++ | Cls | c:@S@Cls | | Ref | rel: 0 // CHECK: [[@LINE-3]]:71 | static-property/C++ | C | c:@S@Record@C | __ZN6Record1CE | Ref,Read | rel: 0 // CHECK: [[@LINE-4]]:63 | struct/C++ | Record | c:@S@Record | | Ref | rel: 0 @@ -505,7 +505,7 @@ // CHECK-NEXT: [[@LINE-2]]:19 | field/C++ | t | c:@ST>1#T@Guided@FI@t | | Def,RelChild | rel: 1 // CHECK-NEXT: RelChild | Guided | c:@ST>1#T@Guided Guided(double) -> Guided; -// CHECK: [[@LINE-1]]:19 | struct(Gen,TS)/C++ | Guided | c:@S@Guided>#f | | Ref | rel: 0 +// CHECK: [[@LINE-1]]:19 | struct(Gen)/C++ | Guided | c:@ST>1#T@Guided | | Ref | rel: 0 // CHECK-NEXT: [[@LINE-2]]:1 | struct(Gen)/C++ | Guided | c:@ST>1#T@Guided | | Ref | rel: 0 auto guided = Guided{1.0}; // CHECK: [[@LINE-1]]:6 | variable/C | guided | c:@guided | _guided | Def | rel: 0