2017-12-15 20:25:02 +08:00
|
|
|
//===--- FileIndex.cpp - Indexes for files. ------------------------ C++-*-===//
|
2017-12-14 22:50:58 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-12-14 22:50:58 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-12-15 20:25:02 +08:00
|
|
|
#include "FileIndex.h"
|
2018-09-18 18:30:44 +08:00
|
|
|
#include "ClangdUnit.h"
|
2018-09-07 17:40:36 +08:00
|
|
|
#include "Logger.h"
|
2018-08-22 20:43:17 +08:00
|
|
|
#include "SymbolCollector.h"
|
2019-02-05 00:19:57 +08:00
|
|
|
#include "index/CanonicalIncludes.h"
|
2018-09-18 18:30:44 +08:00
|
|
|
#include "index/Index.h"
|
2018-11-06 18:55:21 +08:00
|
|
|
#include "index/MemIndex.h"
|
2018-09-18 18:30:44 +08:00
|
|
|
#include "index/Merge.h"
|
2019-02-28 20:31:49 +08:00
|
|
|
#include "index/SymbolOrigin.h"
|
2018-10-16 16:53:52 +08:00
|
|
|
#include "index/dex/Dex.h"
|
2017-12-14 22:50:58 +08:00
|
|
|
#include "clang/Index/IndexingAction.h"
|
2018-09-19 17:35:04 +08:00
|
|
|
#include "clang/Lex/MacroInfo.h"
|
2018-05-24 23:50:15 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2018-11-06 18:55:21 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2018-09-18 18:30:44 +08:00
|
|
|
#include <memory>
|
2017-12-14 22:50:58 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2017-12-15 20:25:02 +08:00
|
|
|
|
2019-06-15 10:26:47 +08:00
|
|
|
static SlabTuple indexSymbols(ASTContext &AST, std::shared_ptr<Preprocessor> PP,
|
|
|
|
llvm::ArrayRef<Decl *> DeclsToIndex,
|
|
|
|
const CanonicalIncludes &Includes,
|
|
|
|
bool IsIndexMainAST) {
|
2018-01-10 22:57:58 +08:00
|
|
|
SymbolCollector::Options CollectorOpts;
|
2019-02-05 00:19:57 +08:00
|
|
|
CollectorOpts.CollectIncludePath = true;
|
|
|
|
CollectorOpts.Includes = &Includes;
|
2018-03-12 22:49:09 +08:00
|
|
|
CollectorOpts.CountReferences = false;
|
2018-09-18 18:30:44 +08:00
|
|
|
CollectorOpts.Origin = SymbolOrigin::Dynamic;
|
2018-02-20 02:48:44 +08:00
|
|
|
|
2017-12-15 20:25:02 +08:00
|
|
|
index::IndexingOptions IndexOpts;
|
2018-03-12 22:49:09 +08:00
|
|
|
// We only need declarations, because we don't count references.
|
2017-12-15 20:25:02 +08:00
|
|
|
IndexOpts.SystemSymbolFilter =
|
2018-03-12 22:49:09 +08:00
|
|
|
index::IndexingOptions::SystemSymbolFilterKind::DeclarationsOnly;
|
2017-12-15 20:25:02 +08:00
|
|
|
IndexOpts.IndexFunctionLocals = false;
|
2018-09-19 17:35:04 +08:00
|
|
|
if (IsIndexMainAST) {
|
2018-09-18 18:30:44 +08:00
|
|
|
// We only collect refs when indexing main AST.
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
CollectorOpts.RefFilter = RefKind::All;
|
2019-02-26 00:00:00 +08:00
|
|
|
// Comments for main file can always be obtained from sema, do not store
|
|
|
|
// them in the index.
|
|
|
|
CollectorOpts.StoreAllDocumentation = false;
|
2019-02-05 00:19:57 +08:00
|
|
|
} else {
|
2018-09-19 17:35:04 +08:00
|
|
|
IndexOpts.IndexMacrosInPreprocessor = true;
|
|
|
|
CollectorOpts.CollectMacro = true;
|
2019-02-26 00:00:00 +08:00
|
|
|
CollectorOpts.StoreAllDocumentation = true;
|
2018-09-19 17:35:04 +08:00
|
|
|
}
|
2018-09-01 03:53:37 +08:00
|
|
|
|
|
|
|
SymbolCollector Collector(std::move(CollectorOpts));
|
|
|
|
Collector.setPreprocessor(PP);
|
2018-09-18 16:52:14 +08:00
|
|
|
index::indexTopLevelDecls(AST, *PP, DeclsToIndex, Collector, IndexOpts);
|
2018-05-24 23:50:15 +08:00
|
|
|
|
2018-09-01 03:53:37 +08:00
|
|
|
const auto &SM = AST.getSourceManager();
|
|
|
|
const auto *MainFileEntry = SM.getFileEntryForID(SM.getMainFileID());
|
|
|
|
std::string FileName = MainFileEntry ? MainFileEntry->getName() : "";
|
|
|
|
|
|
|
|
auto Syms = Collector.takeSymbols();
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
auto Refs = Collector.takeRefs();
|
2019-06-15 10:26:47 +08:00
|
|
|
auto Relations = Collector.takeRelations();
|
2018-09-18 18:30:44 +08:00
|
|
|
vlog("index AST for {0} (main={1}): \n"
|
2018-09-01 03:53:37 +08:00
|
|
|
" symbol slab: {2} symbols, {3} bytes\n"
|
2019-06-15 10:26:47 +08:00
|
|
|
" ref slab: {4} symbols, {5} refs, {6} bytes\n"
|
|
|
|
" relations slab: {7} relations, {8} bytes",
|
2018-09-18 18:30:44 +08:00
|
|
|
FileName, IsIndexMainAST, Syms.size(), Syms.bytes(), Refs.size(),
|
2019-06-15 10:26:47 +08:00
|
|
|
Refs.numRefs(), Refs.bytes(), Relations.size(), Relations.bytes());
|
2019-06-16 09:09:41 +08:00
|
|
|
return std::make_tuple(std::move(Syms), std::move(Refs),
|
|
|
|
std::move(Relations));
|
2017-12-15 20:25:02 +08:00
|
|
|
}
|
|
|
|
|
2019-06-15 10:26:47 +08:00
|
|
|
SlabTuple indexMainDecls(ParsedAST &AST) {
|
2018-09-18 18:30:44 +08:00
|
|
|
return indexSymbols(AST.getASTContext(), AST.getPreprocessorPtr(),
|
2019-02-05 00:19:57 +08:00
|
|
|
AST.getLocalTopLevelDecls(), AST.getCanonicalIncludes(),
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
/*IsIndexMainAST=*/true);
|
2018-09-18 18:30:44 +08:00
|
|
|
}
|
|
|
|
|
2019-06-15 10:26:47 +08:00
|
|
|
SlabTuple indexHeaderSymbols(ASTContext &AST, std::shared_ptr<Preprocessor> PP,
|
|
|
|
const CanonicalIncludes &Includes) {
|
2018-09-18 18:30:44 +08:00
|
|
|
std::vector<Decl *> DeclsToIndex(
|
|
|
|
AST.getTranslationUnitDecl()->decls().begin(),
|
|
|
|
AST.getTranslationUnitDecl()->decls().end());
|
2019-02-05 00:19:57 +08:00
|
|
|
return indexSymbols(AST, std::move(PP), DeclsToIndex, Includes,
|
2019-06-15 10:26:47 +08:00
|
|
|
/*IsIndexMainAST=*/false);
|
[clangd] Factor out the data-swapping functionality from MemIndex/DexIndex.
Summary:
This is now handled by a wrapper class SwapIndex, so MemIndex/DexIndex can be
immutable and focus on their job.
Old and busted:
I have a MemIndex, which holds a shared_ptr<vector<Symbol*>>, which keeps the
symbol slab alive. I update by calling build(shared_ptr<vector<Symbol*>>).
New hotness: I have a SwapIndex, which holds a unique_ptr<SymbolIndex>, which
holds a MemIndex, which holds a shared_ptr<void>, which keeps backing
data alive.
I update by building a new MemIndex and calling SwapIndex::reset().
Reviewers: kbobyrev, ioeric
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51422
llvm-svn: 341318
2018-09-03 22:37:43 +08:00
|
|
|
}
|
2018-06-15 16:55:00 +08:00
|
|
|
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
void FileSymbols::update(PathRef Path, std::unique_ptr<SymbolSlab> Symbols,
|
2019-06-15 10:26:47 +08:00
|
|
|
std::unique_ptr<RefSlab> Refs,
|
|
|
|
std::unique_ptr<RelationSlab> Relations,
|
|
|
|
bool CountReferences) {
|
2017-12-14 22:50:58 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
if (!Symbols)
|
|
|
|
FileToSymbols.erase(Path);
|
2017-12-14 22:50:58 +08:00
|
|
|
else
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
FileToSymbols[Path] = std::move(Symbols);
|
2019-05-09 22:22:07 +08:00
|
|
|
if (!Refs) {
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
FileToRefs.erase(Path);
|
2019-06-15 10:26:47 +08:00
|
|
|
} else {
|
|
|
|
RefSlabAndCountReferences Item;
|
|
|
|
Item.CountReferences = CountReferences;
|
|
|
|
Item.Slab = std::move(Refs);
|
|
|
|
FileToRefs[Path] = std::move(Item);
|
2019-05-09 22:22:07 +08:00
|
|
|
}
|
2019-06-15 10:26:47 +08:00
|
|
|
if (!Relations)
|
|
|
|
FileToRelations.erase(Path);
|
|
|
|
else
|
|
|
|
FileToRelations[Path] = std::move(Relations);
|
2017-12-14 22:50:58 +08:00
|
|
|
}
|
|
|
|
|
2018-10-16 16:53:52 +08:00
|
|
|
std::unique_ptr<SymbolIndex>
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
FileSymbols::buildIndex(IndexType Type, DuplicateHandling DuplicateHandle) {
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
std::vector<std::shared_ptr<SymbolSlab>> SymbolSlabs;
|
|
|
|
std::vector<std::shared_ptr<RefSlab>> RefSlabs;
|
2019-06-15 10:26:47 +08:00
|
|
|
std::vector<std::shared_ptr<RelationSlab>> RelationSlabs;
|
2019-05-09 22:22:07 +08:00
|
|
|
std::vector<RefSlab *> MainFileRefs;
|
2017-12-14 22:50:58 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
for (const auto &FileAndSymbols : FileToSymbols)
|
|
|
|
SymbolSlabs.push_back(FileAndSymbols.second);
|
2019-05-09 22:22:07 +08:00
|
|
|
for (const auto &FileAndRefs : FileToRefs) {
|
|
|
|
RefSlabs.push_back(FileAndRefs.second.Slab);
|
|
|
|
if (FileAndRefs.second.CountReferences)
|
|
|
|
MainFileRefs.push_back(RefSlabs.back().get());
|
|
|
|
}
|
2019-06-15 10:26:47 +08:00
|
|
|
for (const auto &FileAndRelations : FileToRelations)
|
|
|
|
RelationSlabs.push_back(FileAndRelations.second);
|
2017-12-14 22:50:58 +08:00
|
|
|
}
|
[clangd] Factor out the data-swapping functionality from MemIndex/DexIndex.
Summary:
This is now handled by a wrapper class SwapIndex, so MemIndex/DexIndex can be
immutable and focus on their job.
Old and busted:
I have a MemIndex, which holds a shared_ptr<vector<Symbol*>>, which keeps the
symbol slab alive. I update by calling build(shared_ptr<vector<Symbol*>>).
New hotness: I have a SwapIndex, which holds a unique_ptr<SymbolIndex>, which
holds a MemIndex, which holds a shared_ptr<void>, which keeps backing
data alive.
I update by building a new MemIndex and calling SwapIndex::reset().
Reviewers: kbobyrev, ioeric
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51422
llvm-svn: 341318
2018-09-03 22:37:43 +08:00
|
|
|
std::vector<const Symbol *> AllSymbols;
|
2018-11-06 18:55:21 +08:00
|
|
|
std::vector<Symbol> SymsStorage;
|
|
|
|
switch (DuplicateHandle) {
|
|
|
|
case DuplicateHandling::Merge: {
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::DenseMap<SymbolID, Symbol> Merged;
|
2018-11-06 18:55:21 +08:00
|
|
|
for (const auto &Slab : SymbolSlabs) {
|
|
|
|
for (const auto &Sym : *Slab) {
|
2019-05-09 22:22:07 +08:00
|
|
|
assert(Sym.References == 0 &&
|
|
|
|
"Symbol with non-zero references sent to FileSymbols");
|
2018-11-06 18:55:21 +08:00
|
|
|
auto I = Merged.try_emplace(Sym.ID, Sym);
|
|
|
|
if (!I.second)
|
2018-11-07 20:25:27 +08:00
|
|
|
I.first->second = mergeSymbol(I.first->second, Sym);
|
2018-11-06 18:55:21 +08:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 22:22:07 +08:00
|
|
|
for (const RefSlab *Refs : MainFileRefs)
|
|
|
|
for (const auto &Sym : *Refs) {
|
|
|
|
auto It = Merged.find(Sym.first);
|
2019-05-09 23:07:53 +08:00
|
|
|
// This might happen while background-index is still running.
|
|
|
|
if (It == Merged.end())
|
|
|
|
continue;
|
2019-05-09 22:22:07 +08:00
|
|
|
It->getSecond().References += Sym.second.size();
|
|
|
|
}
|
2018-11-06 18:55:21 +08:00
|
|
|
SymsStorage.reserve(Merged.size());
|
|
|
|
for (auto &Sym : Merged) {
|
|
|
|
SymsStorage.push_back(std::move(Sym.second));
|
|
|
|
AllSymbols.push_back(&SymsStorage.back());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DuplicateHandling::PickOne: {
|
|
|
|
llvm::DenseSet<SymbolID> AddedSymbols;
|
|
|
|
for (const auto &Slab : SymbolSlabs)
|
2019-05-09 22:22:07 +08:00
|
|
|
for (const auto &Sym : *Slab) {
|
|
|
|
assert(Sym.References == 0 &&
|
|
|
|
"Symbol with non-zero references sent to FileSymbols");
|
2018-11-06 18:55:21 +08:00
|
|
|
if (AddedSymbols.insert(Sym.ID).second)
|
|
|
|
AllSymbols.push_back(&Sym);
|
2019-05-09 22:22:07 +08:00
|
|
|
}
|
2018-11-06 18:55:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
|
|
|
|
std::vector<Ref> RefsStorage; // Contiguous ranges for each SymbolID.
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::DenseMap<SymbolID, llvm::ArrayRef<Ref>> AllRefs;
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
{
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::DenseMap<SymbolID, llvm::SmallVector<Ref, 4>> MergedRefs;
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
size_t Count = 0;
|
|
|
|
for (const auto &RefSlab : RefSlabs)
|
|
|
|
for (const auto &Sym : *RefSlab) {
|
|
|
|
MergedRefs[Sym.first].append(Sym.second.begin(), Sym.second.end());
|
|
|
|
Count += Sym.second.size();
|
|
|
|
}
|
|
|
|
RefsStorage.reserve(Count);
|
|
|
|
AllRefs.reserve(MergedRefs.size());
|
|
|
|
for (auto &Sym : MergedRefs) {
|
|
|
|
auto &SymRefs = Sym.second;
|
|
|
|
// Sorting isn't required, but yields more stable results over rebuilds.
|
2018-10-07 22:49:41 +08:00
|
|
|
llvm::sort(SymRefs);
|
|
|
|
llvm::copy(SymRefs, back_inserter(RefsStorage));
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
AllRefs.try_emplace(
|
|
|
|
Sym.first,
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::ArrayRef<Ref>(&RefsStorage[RefsStorage.size() - SymRefs.size()],
|
|
|
|
SymRefs.size()));
|
2018-09-01 03:53:37 +08:00
|
|
|
}
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
}
|
2018-09-01 03:53:37 +08:00
|
|
|
|
2019-06-15 10:26:47 +08:00
|
|
|
std::vector<Relation> AllRelations;
|
|
|
|
for (const auto &RelationSlab : RelationSlabs) {
|
|
|
|
for (const auto &R : *RelationSlab)
|
|
|
|
AllRelations.push_back(R);
|
|
|
|
}
|
|
|
|
|
2018-11-06 18:55:21 +08:00
|
|
|
size_t StorageSize =
|
|
|
|
RefsStorage.size() * sizeof(Ref) + SymsStorage.size() * sizeof(Symbol);
|
2018-09-10 19:46:07 +08:00
|
|
|
for (const auto &Slab : SymbolSlabs)
|
|
|
|
StorageSize += Slab->bytes();
|
|
|
|
for (const auto &RefSlab : RefSlabs)
|
|
|
|
StorageSize += RefSlab->bytes();
|
2019-06-15 10:26:47 +08:00
|
|
|
for (const auto &RelationSlab : RelationSlabs)
|
|
|
|
StorageSize += RelationSlab->bytes();
|
2018-09-10 19:46:07 +08:00
|
|
|
|
[clangd] SymbolOccurrences -> Refs and cleanup
Summary:
A few things that I noticed while merging the SwapIndex patch:
- SymbolOccurrences and particularly SymbolOccurrenceSlab are unwieldy names,
and these names appear *a lot*. Ref, RefSlab, etc seem clear enough
and read/format much better.
- The asymmetry between SymbolSlab and RefSlab (build() vs freeze()) is
confusing and irritating, and doesn't even save much code.
Avoiding RefSlab::Builder was my idea, but it was a bad one; add it.
- DenseMap<SymbolID, ArrayRef<Ref>> seems like a reasonable compromise for
constructing MemIndex - and means many less wasted allocations than the
current DenseMap<SymbolID, vector<Ref*>> for FileIndex, and none for
slabs.
- RefSlab::find() is not actually used for anything, so we can throw
away the DenseMap and keep the representation much more compact.
- A few naming/consistency fixes: e.g. Slabs,Refs -> Symbols,Refs.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51605
llvm-svn: 341368
2018-09-04 22:39:56 +08:00
|
|
|
// Index must keep the slabs and contiguous ranges alive.
|
2018-10-16 16:53:52 +08:00
|
|
|
switch (Type) {
|
|
|
|
case IndexType::Light:
|
|
|
|
return llvm::make_unique<MemIndex>(
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::make_pointee_range(AllSymbols), std::move(AllRefs),
|
2019-06-15 10:26:47 +08:00
|
|
|
std::move(AllRelations),
|
2018-10-16 16:53:52 +08:00
|
|
|
std::make_tuple(std::move(SymbolSlabs), std::move(RefSlabs),
|
2018-11-06 18:55:21 +08:00
|
|
|
std::move(RefsStorage), std::move(SymsStorage)),
|
2018-10-16 16:53:52 +08:00
|
|
|
StorageSize);
|
|
|
|
case IndexType::Heavy:
|
|
|
|
return llvm::make_unique<dex::Dex>(
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::make_pointee_range(AllSymbols), std::move(AllRefs),
|
2019-06-15 10:26:47 +08:00
|
|
|
std::move(AllRelations),
|
2018-10-16 16:53:52 +08:00
|
|
|
std::make_tuple(std::move(SymbolSlabs), std::move(RefSlabs),
|
2018-11-06 18:55:21 +08:00
|
|
|
std::move(RefsStorage), std::move(SymsStorage)),
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
StorageSize);
|
2018-10-16 16:53:52 +08:00
|
|
|
}
|
2018-10-20 21:18:49 +08:00
|
|
|
llvm_unreachable("Unknown clangd::IndexType");
|
2018-09-01 03:53:37 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
FileIndex::FileIndex(bool UseDex)
|
2018-10-16 16:53:52 +08:00
|
|
|
: MergedIndex(&MainFileIndex, &PreambleIndex), UseDex(UseDex),
|
|
|
|
PreambleIndex(llvm::make_unique<MemIndex>()),
|
|
|
|
MainFileIndex(llvm::make_unique<MemIndex>()) {}
|
2018-09-18 18:30:44 +08:00
|
|
|
|
|
|
|
void FileIndex::updatePreamble(PathRef Path, ASTContext &AST,
|
2019-02-05 00:19:57 +08:00
|
|
|
std::shared_ptr<Preprocessor> PP,
|
|
|
|
const CanonicalIncludes &Includes) {
|
2019-06-15 10:26:47 +08:00
|
|
|
auto Slabs = indexHeaderSymbols(AST, std::move(PP), Includes);
|
2019-05-09 22:22:07 +08:00
|
|
|
PreambleSymbols.update(
|
2019-06-15 10:26:47 +08:00
|
|
|
Path, llvm::make_unique<SymbolSlab>(std::move(std::get<0>(Slabs))),
|
|
|
|
llvm::make_unique<RefSlab>(),
|
|
|
|
llvm::make_unique<RelationSlab>(std::move(std::get<2>(Slabs))),
|
|
|
|
/*CountReferences=*/false);
|
2018-11-06 18:55:21 +08:00
|
|
|
PreambleIndex.reset(
|
|
|
|
PreambleSymbols.buildIndex(UseDex ? IndexType::Heavy : IndexType::Light,
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
DuplicateHandling::PickOne));
|
2018-09-18 18:30:44 +08:00
|
|
|
}
|
|
|
|
|
2018-09-18 21:35:16 +08:00
|
|
|
void FileIndex::updateMain(PathRef Path, ParsedAST &AST) {
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
auto Contents = indexMainDecls(AST);
|
2018-09-18 18:30:44 +08:00
|
|
|
MainFileSymbols.update(
|
2019-06-15 10:26:47 +08:00
|
|
|
Path, llvm::make_unique<SymbolSlab>(std::move(std::get<0>(Contents))),
|
|
|
|
llvm::make_unique<RefSlab>(std::move(std::get<1>(Contents))),
|
|
|
|
llvm::make_unique<RelationSlab>(std::move(std::get<2>(Contents))),
|
2019-05-09 22:22:07 +08:00
|
|
|
/*CountReferences=*/true);
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
MainFileIndex.reset(
|
|
|
|
MainFileSymbols.buildIndex(IndexType::Light, DuplicateHandling::PickOne));
|
2018-08-24 17:12:54 +08:00
|
|
|
}
|
|
|
|
|
2017-12-14 22:50:58 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|