Index: cfe/trunk/lib/Index/IndexTypeSourceInfo.cpp =================================================================== --- cfe/trunk/lib/Index/IndexTypeSourceInfo.cpp +++ cfe/trunk/lib/Index/IndexTypeSourceInfo.cpp @@ -129,10 +129,10 @@ template bool HandleTemplateSpecializationTypeLoc(TypeLocType TL) { if (const auto *T = TL.getTypePtr()) { - if (IndexCtx.shouldIndexImplicitInstantiation()) { - if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) { - IndexCtx.handleReference(RD, TL.getTemplateNameLoc(), - Parent, ParentDC, SymbolRoleSet(), Relations); + if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) { + if (!RD->isImplicit() || IndexCtx.shouldIndexImplicitInstantiation()) { + IndexCtx.handleReference(RD, TL.getTemplateNameLoc(), Parent, + ParentDC, SymbolRoleSet(), Relations); return true; } } Index: cfe/trunk/test/Index/Core/index-source.cpp =================================================================== --- cfe/trunk/test/Index/Core/index-source.cpp +++ cfe/trunk/test/Index/Core/index-source.cpp @@ -212,7 +212,7 @@ }; // CHECK: [[@LINE-2]]:54 | class(Gen)/C++ | InnerClass | c:@S@PseudoOverridesInSpecializations>#d#I@ST>1#T@InnerClass | | Def,RelChild | rel: 1 // CHECK-NEXT: RelChild -// CHECK: [[@LINE-4]]:7 | class(Gen)/C++ | PseudoOverridesInSpecializations | c:@ST>2#T#T@PseudoOverridesInSpecializations | | Ref,RelCont | rel: 1 +// CHECK: [[@LINE-4]]:7 | class(Gen,TS)/C++ | PseudoOverridesInSpecializations | c:@S@PseudoOverridesInSpecializations>#d#I | | Ref,RelCont | rel: 1 // CHECK-NEXT: RelCont template @@ -285,13 +285,13 @@ class SpecializationDecl; // CHECK: [[@LINE-1]]:7 | class(Gen,TS)/C++ | SpecializationDecl | c:@S@SpecializationDecl>#I | | Decl,RelSpecialization | rel: 1 // CHECK-NEXT: RelSpecialization | SpecializationDecl | c:@ST>1#T@SpecializationDecl -// CHECK: [[@LINE-3]]:7 | class(Gen)/C++ | SpecializationDecl | c:@ST>1#T@SpecializationDecl | | Ref | rel: 0 +// CHECK: [[@LINE-3]]:7 | class(Gen,TS)/C++ | SpecializationDecl | c:@S@SpecializationDecl>#I | | Ref | rel: 0 template<> class SpecializationDecl { }; // CHECK: [[@LINE-1]]:7 | class(Gen,TS)/C++ | SpecializationDecl | c:@S@SpecializationDecl>#I | | Def,RelSpecialization | rel: 1 // CHECK-NEXT: RelSpecialization | SpecializationDecl | c:@ST>1#T@SpecializationDecl -// CHECK-NEXT: [[@LINE-3]]:7 | class(Gen)/C++ | SpecializationDecl | c:@ST>1#T@SpecializationDecl | | Ref | rel: 0 +// CHECK-NEXT: [[@LINE-3]]:7 | class(Gen,TS)/C++ | SpecializationDecl | c:@S@SpecializationDecl>#I | | Ref | rel: 0 template class PartialSpecilizationClass; @@ -306,7 +306,7 @@ // CHECK-NEXT: RelSpecialization | PartialSpecilizationClass | c:@ST>2#T#T@PartialSpecilizationClass // CHECK-NEXT: [[@LINE-3]]:45 | class/C++ | Cls | c:@S@Cls | | Ref,RelBase,RelCont | rel: 1 // CHECK-NEXT: RelBase,RelCont | PartialSpecilizationClass | c:@S@PartialSpecilizationClass>#$@S@Cls#S0_ -// CHECK-NEXT: [[@LINE-5]]:7 | class(Gen)/C++ | PartialSpecilizationClass | c:@ST>2#T#T@PartialSpecilizationClass | | Ref | rel: 0 +// CHECK-NEXT: [[@LINE-5]]:7 | class(Gen,TS)/C++ | PartialSpecilizationClass | c:@S@PartialSpecilizationClass>#$@S@Cls#S0_ | | Ref | rel: 0 // CHECK-NEXT: [[@LINE-6]]:33 | class/C++ | Cls | c:@S@Cls | | Ref | rel: 0 // CHECK-NEXT: [[@LINE-7]]:38 | class/C++ | Cls | c:@S@Cls | | Ref | rel: 0 @@ -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)/C++ | SpecializationDecl | c:@ST>1#T@SpecializationDecl | | Ref,RelCont | rel: 1 +// CHECK: [[@LINE-3]]:17 | class(Gen,TS)/C++ | SpecializationDecl | c:@S@SpecializationDecl>#$@S@Cls | | 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)/C++ | SpecializationDecl | c:@ST>1#T@SpecializationDecl | | Ref | rel: 0 +// CHECK: [[@LINE-1]]:38 | class(Gen,TS)/C++ | SpecializationDecl | c:@S@SpecializationDecl>#$@S@Cls | | 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 @@ -494,7 +494,7 @@ // CHECK: [[@LINE-1]]:69 | variable/C++ | structuredBinding2 | c:@N@cpp17structuredBinding@structuredBinding2 | | Ref,Read,RelCont | rel: 1 // CHECK-NEXT: RelCont | localStructuredBindingAndRef | c:@N@cpp17structuredBinding@F@localStructuredBindingAndRef# // CHECK-NOT: localBinding -// LOCAL: [[@LINE-4]]:9 | variable(local)/C++ | localBinding1 | c:index-source.cpp@25382@N@cpp17structuredBinding@F@localStructuredBindingAndRef#@localBinding1 +// LOCAL: [[@LINE-4]]:9 | variable(local)/C++ | localBinding1 | c:index-source.cpp@25408@N@cpp17structuredBinding@F@localStructuredBindingAndRef#@localBinding1 } } @@ -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)/C++ | Guided | c:@ST>1#T@Guided | | Ref | rel: 0 +// CHECK: [[@LINE-1]]:19 | struct(Gen,TS)/C++ | Guided | c:@S@Guided>#f | | 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 Index: cfe/trunk/test/Index/index-refs.cpp =================================================================== --- cfe/trunk/test/Index/index-refs.cpp +++ cfe/trunk/test/Index/index-refs.cpp @@ -117,7 +117,7 @@ /* when indexing implicit instantiations [indexEntityReference]: kind: struct-template-spec | name: TS | USR: c:@S@TS>#I | {{.*}} | loc: 55:3 */ -// CHECK-NEXT: [indexEntityReference]: kind: c++-class-template | name: TS | USR: c:@ST>2#T#T@TS | {{.*}} | loc: 55:3 +// CHECK-NEXT: [indexEntityReference]: kind: struct-template-partial-spec | name: TS | USR: c:@SP>1#T@TS>#t0.0#I | {{.*}} | loc: 55:3 // CHECK: [indexEntityReference]: kind: variable | name: array_size | {{.*}} | loc: 59:22 // CHECK: [indexEntityReference]: kind: variable | name: default_param | {{.*}} | loc: 62:19 | {{.*}} | role: ref read Index: cfe/trunk/unittests/Index/IndexTests.cpp =================================================================== --- cfe/trunk/unittests/Index/IndexTests.cpp +++ cfe/trunk/unittests/Index/IndexTests.cpp @@ -7,7 +7,10 @@ //===----------------------------------------------------------------------===// #include "clang/AST/ASTConsumer.h" +#include "clang/AST/ASTContext.h" #include "clang/AST/Decl.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/FrontendAction.h" #include "clang/Index/IndexDataConsumer.h" @@ -23,27 +26,62 @@ namespace clang { namespace index { +namespace { +struct Position { + size_t Line = 0; + size_t Column = 0; + + Position(size_t Line = 0, size_t Column = 0) : Line(Line), Column(Column) {} + + static Position fromSourceLocation(SourceLocation Loc, + const SourceManager &SM) { + FileID FID; + unsigned Offset; + std::tie(FID, Offset) = SM.getDecomposedSpellingLoc(Loc); + Position P; + P.Line = SM.getLineNumber(FID, Offset); + P.Column = SM.getColumnNumber(FID, Offset); + return P; + } +}; + +bool operator==(const Position &LHS, const Position &RHS) { + return std::tie(LHS.Line, LHS.Column) == std::tie(RHS.Line, RHS.Column); +} + +llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Position &Pos) { + return OS << Pos.Line << ':' << Pos.Column; +} struct TestSymbol { std::string QName; + Position WrittenPos; + Position DeclPos; // FIXME: add more information. }; llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const TestSymbol &S) { - return OS << S.QName; + return OS << S.QName << '[' << S.WrittenPos << ']' << '@' << S.DeclPos; } -namespace { class Indexer : public IndexDataConsumer { public: + void initialize(ASTContext &Ctx) override { + AST = &Ctx; + IndexDataConsumer::initialize(Ctx); + } + bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles, - ArrayRef, SourceLocation, + ArrayRef, SourceLocation Loc, ASTNodeInfo) override { const auto *ND = llvm::dyn_cast(D); if (!ND) return true; TestSymbol S; S.QName = ND->getQualifiedNameAsString(); + S.WrittenPos = Position::fromSourceLocation(Loc, AST->getSourceManager()); + S.DeclPos = + Position::fromSourceLocation(D->getLocation(), AST->getSourceManager()); Symbols.push_back(std::move(S)); return true; } @@ -57,6 +95,7 @@ } std::vector Symbols; + const ASTContext *AST = nullptr; }; class IndexAction : public ASTFrontendAction { @@ -93,11 +132,14 @@ IndexingOptions Opts; }; +using testing::AllOf; using testing::Contains; using testing::Not; using testing::UnorderedElementsAre; MATCHER_P(QName, Name, "") { return arg.QName == Name; } +MATCHER_P(WrittenAt, Pos, "") { return arg.WrittenPos == Pos; } +MATCHER_P(DeclAt, Pos, "") { return arg.DeclPos == Pos; } TEST(IndexTest, Simple) { auto Index = std::make_shared(); @@ -134,6 +176,46 @@ EXPECT_THAT(Index->Symbols, Not(Contains(QName("bar")))); } +TEST(IndexTest, IndexExplicitTemplateInstantiation) { + std::string Code = R"cpp( + template + struct Foo { void bar() {} }; + template <> + struct Foo { void bar() {} }; + void foo() { + Foo abc; + Foo b; + } + )cpp"; + auto Index = std::make_shared(); + IndexingOptions Opts; + tooling::runToolOnCode(new IndexAction(Index, Opts), Code); + EXPECT_THAT(Index->Symbols, + AllOf(Contains(AllOf(QName("Foo"), WrittenAt(Position(8, 7)), + DeclAt(Position(5, 12)))), + Contains(AllOf(QName("Foo"), WrittenAt(Position(7, 7)), + DeclAt(Position(3, 12)))))); +} + +TEST(IndexTest, IndexTemplateInstantiationPartial) { + std::string Code = R"cpp( + template + struct Foo { void bar() {} }; + template + struct Foo { void bar() {} }; + void foo() { + Foo abc; + Foo b; + } + )cpp"; + auto Index = std::make_shared(); + IndexingOptions Opts; + tooling::runToolOnCode(new IndexAction(Index, Opts), Code); + EXPECT_THAT(Index->Symbols, + Contains(AllOf(QName("Foo"), WrittenAt(Position(8, 7)), + DeclAt(Position(5, 12))))); +} + } // namespace } // namespace index } // namespace clang