From 73e6f47da249dcd869320b539da7625b6864f110 Mon Sep 17 00:00:00 2001 From: Nathan Ridge Date: Tue, 4 Jun 2019 04:25:44 +0000 Subject: [PATCH] [clangd] SymbolCollector support for relations Summary: The only relation currently collected is RelationBaseOf, because this is all we need for type hierarchy subtypes. Additional relations can be collected in the future as the need arises. This patch builds on D59407 and D62459. Reviewers: kadircet Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D62471 llvm-svn: 362467 --- clang-tools-extra/clangd/index/SymbolCollector.cpp | 51 ++++++++++++++++++++-- clang-tools-extra/clangd/index/SymbolCollector.h | 5 +++ .../clangd/unittests/SymbolCollectorTests.cpp | 31 +++++++++---- 3 files changed, 75 insertions(+), 12 deletions(-) diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp index 507d0ea..f7b027c 100644 --- a/clang-tools-extra/clangd/index/SymbolCollector.cpp +++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp @@ -193,6 +193,11 @@ RefKind toRefKind(index::SymbolRoleSet Roles) { return static_cast(static_cast(RefKind::All) & Roles); } +bool shouldIndexRelation(const index::SymbolRelation &R) { + // We currently only index BaseOf relations, for type hierarchy subtypes. + return R.Roles & static_cast(index::SymbolRole::RelationBaseOf); +} + } // namespace SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {} @@ -291,6 +296,16 @@ bool SymbolCollector::handleDeclOccurence( SM.getFileID(SpellingLoc) == SM.getMainFileID()) ReferencedDecls.insert(ND); + auto ID = getSymbolID(ND); + if (!ID) + return true; + + // Note: we need to process relations for all decl occurrences, including + // refs, because the indexing code only populates relations for specific + // occurrences. For example, RelationBaseOf is only populated for the + // occurrence inside the base-specifier. + processRelations(*ND, *ID, Relations); + bool CollectRef = static_cast(Opts.RefFilter) & Roles; bool IsOnlyRef = !(Roles & (static_cast(index::SymbolRole::Declaration) | @@ -315,10 +330,6 @@ bool SymbolCollector::handleDeclOccurence( if (IsOnlyRef) return true; - auto ID = getSymbolID(ND); - if (!ID) - return true; - // FIXME: ObjCPropertyDecl are not properly indexed here: // - ObjCPropertyDecl may have an OrigD of ObjCPropertyImplDecl, which is // not a NamedDecl. @@ -338,6 +349,7 @@ bool SymbolCollector::handleDeclOccurence( if (Roles & static_cast(index::SymbolRole::Definition)) addDefinition(*OriginalDecl, *BasicSymbol); + return true; } @@ -416,6 +428,37 @@ bool SymbolCollector::handleMacroOccurence(const IdentifierInfo *Name, return true; } +void SymbolCollector::processRelations( + const NamedDecl &ND, const SymbolID &ID, + ArrayRef Relations) { + // Store subtype relations. + if (!dyn_cast(&ND)) + return; + + for (const auto &R : Relations) { + if (!shouldIndexRelation(R)) + continue; + + const Decl *Object = R.RelatedSymbol; + + auto ObjectID = getSymbolID(Object); + if (!ObjectID) + continue; + + // Record the relation. + // TODO: There may be cases where the object decl is not indexed for some + // reason. Those cases should probably be removed in due course, but for + // now there are two possible ways to handle it: + // (A) Avoid storing the relation in such cases. + // (B) Store it anyways. Clients will likely lookup() the SymbolID + // in the index and find nothing, but that's a situation they + // probably need to handle for other reasons anyways. + // We currently do (B) because it's simpler. + this->Relations.insert( + Relation{ID, index::SymbolRole::RelationBaseOf, *ObjectID}); + } +} + void SymbolCollector::setIncludeLocation(const Symbol &S, SourceLocation Loc) { if (Opts.CollectIncludePath) if (shouldCollectIncludePath(S.SymInfo.Kind)) diff --git a/clang-tools-extra/clangd/index/SymbolCollector.h b/clang-tools-extra/clangd/index/SymbolCollector.h index f746002..3c28a45 100644 --- a/clang-tools-extra/clangd/index/SymbolCollector.h +++ b/clang-tools-extra/clangd/index/SymbolCollector.h @@ -110,6 +110,7 @@ public: SymbolSlab takeSymbols() { return std::move(Symbols).build(); } RefSlab takeRefs() { return std::move(Refs).build(); } + RelationSlab takeRelations() { return std::move(Relations).build(); } void finish() override; @@ -117,6 +118,8 @@ private: const Symbol *addDeclaration(const NamedDecl &, SymbolID, bool IsMainFileSymbol); void addDefinition(const NamedDecl &, const Symbol &DeclSymbol); + void processRelations(const NamedDecl &ND, const SymbolID &ID, + ArrayRef Relations); llvm::Optional getIncludeHeader(llvm::StringRef QName, FileID); bool isSelfContainedHeader(FileID); @@ -135,6 +138,8 @@ private: // Only symbols declared in preamble (from #include) and referenced from the // main file will be included. RefSlab::Builder Refs; + // All relations collected from the AST. + RelationSlab::Builder Relations; ASTContext *ASTCtx; std::shared_ptr PP; std::shared_ptr CompletionAllocator; diff --git a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp index e422f5c..4a71438 100644 --- a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp +++ b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp @@ -123,8 +123,9 @@ public: assert(AST.hasValue()); const NamedDecl &ND = Qualified ? findDecl(*AST, Name) : findUnqualifiedDecl(*AST, Name); - const SourceManager& SM = AST->getSourceManager(); - bool MainFile = SM.isWrittenInMainFile(SM.getExpansionLoc(ND.getBeginLoc())); + const SourceManager &SM = AST->getSourceManager(); + bool MainFile = + SM.isWrittenInMainFile(SM.getExpansionLoc(ND.getBeginLoc())); return SymbolCollector::shouldCollectSymbol( ND, AST->getASTContext(), SymbolCollector::Options(), MainFile); } @@ -272,13 +273,14 @@ public: Args, Factory->create(), Files.get(), std::make_shared()); - InMemoryFileSystem->addFile( - TestHeaderName, 0, llvm::MemoryBuffer::getMemBuffer(HeaderCode)); + InMemoryFileSystem->addFile(TestHeaderName, 0, + llvm::MemoryBuffer::getMemBuffer(HeaderCode)); InMemoryFileSystem->addFile(TestFileName, 0, llvm::MemoryBuffer::getMemBuffer(MainCode)); Invocation.run(); Symbols = Factory->Collector->takeSymbols(); Refs = Factory->Collector->takeRefs(); + Relations = Factory->Collector->takeRelations(); return true; } @@ -290,6 +292,7 @@ protected: std::string TestFileURI; SymbolSlab Symbols; RefSlab Refs; + RelationSlab Relations; SymbolCollector::Options CollectorOpts; std::unique_ptr PragmaHandler; }; @@ -634,6 +637,19 @@ TEST_F(SymbolCollectorTest, RefsInHeaders) { HaveRanges(Header.ranges())))); } +TEST_F(SymbolCollectorTest, Relations) { + std::string Header = R"( + class Base {}; + class Derived : public Base {}; + )"; + runSymbolCollector(Header, /*Main=*/""); + const Symbol &Base = findSymbol(Symbols, "Base"); + const Symbol &Derived = findSymbol(Symbols, "Derived"); + EXPECT_THAT(Relations, + Contains(Relation{Base.ID, index::SymbolRole::RelationBaseOf, + Derived.ID})); +} + TEST_F(SymbolCollectorTest, References) { const std::string Header = R"( class W; @@ -783,10 +799,9 @@ TEST_F(SymbolCollectorTest, SymbolsInMainFile) { void f1() {} )"; runSymbolCollector(/*Header=*/"", Main); - EXPECT_THAT(Symbols, - UnorderedElementsAre(QName("Foo"), QName("f1"), QName("f2"), - QName("ff"), QName("foo"), QName("foo::Bar"), - QName("main_f"))); + EXPECT_THAT(Symbols, UnorderedElementsAre( + QName("Foo"), QName("f1"), QName("f2"), QName("ff"), + QName("foo"), QName("foo::Bar"), QName("main_f"))); } TEST_F(SymbolCollectorTest, Documentation) { -- 2.7.4