From b780517ca5a1102187acbcedca8381542c809063 Mon Sep 17 00:00:00 2001 From: Kadir Cetinkaya Date: Thu, 21 Feb 2019 09:52:33 +0000 Subject: [PATCH] [clang][Index] Enable indexing of Template Type Parameters behind a flag Summary: clangd uses indexing api to provide references and it was not possible to perform symbol information for template parameters. This patch enables visiting of TemplateTypeParmTypeLocs. Reviewers: ilya-biryukov, akyrtzi Subscribers: javed.absar, kristof.beyls, ioeric, arphaman, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D58293 llvm-svn: 354560 --- clang/include/clang/Index/IndexingAction.h | 1 + clang/lib/Index/IndexDecl.cpp | 2 ++ clang/lib/Index/IndexSymbol.cpp | 3 --- clang/lib/Index/IndexTypeSourceInfo.cpp | 7 +++++++ clang/lib/Index/IndexingContext.cpp | 9 +++++++- clang/lib/Index/IndexingContext.h | 2 ++ clang/unittests/Index/IndexTests.cpp | 24 ++++++++++++++++++++++ 7 files changed, 44 insertions(+), 4 deletions(-) diff --git a/clang/include/clang/Index/IndexingAction.h b/clang/include/clang/Index/IndexingAction.h index 8b3d5415c063..9756f3c539e6 100644 --- a/clang/include/clang/Index/IndexingAction.h +++ b/clang/include/clang/Index/IndexingAction.h @@ -46,6 +46,7 @@ struct IndexingOptions { bool IndexMacrosInPreprocessor = false; // Has no effect if IndexFunctionLocals are false. bool IndexParametersInDeclarations = false; + bool IndexTemplateParameters = false; }; /// Creates a frontend action that indexes all symbols (macros and AST decls). diff --git a/clang/lib/Index/IndexDecl.cpp b/clang/lib/Index/IndexDecl.cpp index eb0b3d4a70e3..0f6d6212a243 100644 --- a/clang/lib/Index/IndexDecl.cpp +++ b/clang/lib/Index/IndexDecl.cpp @@ -672,6 +672,8 @@ public: shouldIndexTemplateParameterDefaultValue(Parent)) { const TemplateParameterList *Params = D->getTemplateParameters(); for (const NamedDecl *TP : *Params) { + if (IndexCtx.shouldIndexTemplateParameters()) + IndexCtx.handleDecl(TP); if (const auto *TTP = dyn_cast(TP)) { if (TTP->hasDefaultArgument()) IndexCtx.indexTypeSourceInfo(TTP->getDefaultArgumentInfo(), Parent); diff --git a/clang/lib/Index/IndexSymbol.cpp b/clang/lib/Index/IndexSymbol.cpp index 27e62af19f2d..d21c673dee4e 100644 --- a/clang/lib/Index/IndexSymbol.cpp +++ b/clang/lib/Index/IndexSymbol.cpp @@ -55,9 +55,6 @@ bool index::isFunctionLocalSymbol(const Decl *D) { if (isa(D)) return true; - if (isa(D)) - return true; - if (isa(D)) return true; diff --git a/clang/lib/Index/IndexTypeSourceInfo.cpp b/clang/lib/Index/IndexTypeSourceInfo.cpp index e791a9d4a3c9..9f9740b60797 100644 --- a/clang/lib/Index/IndexTypeSourceInfo.cpp +++ b/clang/lib/Index/IndexTypeSourceInfo.cpp @@ -45,6 +45,13 @@ public: return false; \ } while (0) + bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TTPL) { + SourceLocation Loc = TTPL.getNameLoc(); + TemplateTypeParmDecl *TTPD = TTPL.getDecl(); + return IndexCtx.handleReference(TTPD, Loc, Parent, ParentDC, + SymbolRoleSet()); + } + bool VisitTypedefTypeLoc(TypedefTypeLoc TL) { SourceLocation Loc = TL.getNameLoc(); TypedefNameDecl *ND = TL.getTypedefNameDecl(); diff --git a/clang/lib/Index/IndexingContext.cpp b/clang/lib/Index/IndexingContext.cpp index 3d2d7d4ff02a..d01dd50f3b5d 100644 --- a/clang/lib/Index/IndexingContext.cpp +++ b/clang/lib/Index/IndexingContext.cpp @@ -44,6 +44,10 @@ bool IndexingContext::shouldIndexParametersInDeclarations() const { return IndexOpts.IndexParametersInDeclarations; } +bool IndexingContext::shouldIndexTemplateParameters() const { + return IndexOpts.IndexTemplateParameters; +} + bool IndexingContext::handleDecl(const Decl *D, SymbolRoleSet Roles, ArrayRef Relations) { @@ -76,8 +80,11 @@ bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc, if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalSymbol(D)) return true; - if (isa(D) || isa(D)) + if (!shouldIndexTemplateParameters() && + (isa(D) || isa(D) || + isa(D))) { return true; + } return handleDeclOccurrence(D, Loc, /*IsRef=*/true, Parent, Roles, Relations, RefE, RefD, DC); diff --git a/clang/lib/Index/IndexingContext.h b/clang/lib/Index/IndexingContext.h index 7765fa73f82c..3136878c080c 100644 --- a/clang/lib/Index/IndexingContext.h +++ b/clang/lib/Index/IndexingContext.h @@ -63,6 +63,8 @@ public: bool shouldIndexParametersInDeclarations() const; + bool shouldIndexTemplateParameters() const; + static bool isTemplateImplicitInstantiation(const Decl *D); bool handleDecl(const Decl *D, SymbolRoleSet Roles = SymbolRoleSet(), diff --git a/clang/unittests/Index/IndexTests.cpp b/clang/unittests/Index/IndexTests.cpp index 61c969f978c6..66cef881f636 100644 --- a/clang/unittests/Index/IndexTests.cpp +++ b/clang/unittests/Index/IndexTests.cpp @@ -216,6 +216,30 @@ TEST(IndexTest, IndexTemplateInstantiationPartial) { DeclAt(Position(5, 12))))); } +TEST(IndexTest, IndexTypeParmDecls) { + std::string Code = R"cpp( + template class C, typename NoRef> + struct Foo { + T t = I; + C x; + }; + )cpp"; + auto Index = std::make_shared(); + IndexingOptions Opts; + tooling::runToolOnCode(new IndexAction(Index, Opts), Code); + EXPECT_THAT(Index->Symbols, AllOf(Not(Contains(QName("Foo::T"))), + Not(Contains(QName("Foo::I"))), + Not(Contains(QName("Foo::C"))), + Not(Contains(QName("Foo::NoRef"))))); + + Opts.IndexTemplateParameters = true; + Index->Symbols.clear(); + tooling::runToolOnCode(new IndexAction(Index, Opts), Code); + EXPECT_THAT(Index->Symbols, + AllOf(Contains(QName("Foo::T")), Contains(QName("Foo::I")), + Contains(QName("Foo::C")), Contains(QName("Foo::NoRef")))); +} + } // namespace } // namespace index } // namespace clang