2017-12-15 20:25:02 +08:00
|
|
|
//===--- FileIndex.cpp - Indexes for files. ------------------------ C++-*-===//
|
2017-12-14 22:50:58 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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"
|
2018-09-18 18:30:44 +08:00
|
|
|
#include "index/Index.h"
|
|
|
|
#include "index/Merge.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-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
|
|
|
|
2018-09-18 18:30:44 +08:00
|
|
|
static std::pair<SymbolSlab, RefSlab>
|
|
|
|
indexSymbols(ASTContext &AST, std::shared_ptr<Preprocessor> PP,
|
|
|
|
llvm::ArrayRef<Decl *> DeclsToIndex, bool IsIndexMainAST,
|
|
|
|
llvm::ArrayRef<std::string> URISchemes) {
|
2018-01-10 22:57:58 +08:00
|
|
|
SymbolCollector::Options CollectorOpts;
|
[clangd] Not collect include headers for dynamic index for now.
Summary:
The new behaviors introduced by this patch:
o When include collection is enabled, we always set IncludeHeader field in Symbol
even if it's the same as FileURI in decl.
o Disable include collection in FileIndex which is currently only used to build
dynamic index. We should revisit when we actually want to use FileIndex to global
index.
o Code-completion only uses IncludeHeader to insert headers but not FileURI in
CanonicalDeclaration. This ensures that inserted headers are always canonicalized.
Note that include insertion can still be triggered for symbols that are already
included if they are merged from dynamic index and static index, but we would
only use includes that are already canonicalized (e.g. from static index).
Reason for change:
Collecting header includes in dynamic index enables inserting includes for headers
that are not indexed but opened in the editor. Comparing to inserting includes for
symbols in global/static index, this is nice-to-have but would probably require
non-trivial amount of work to get right. For example:
o Currently it's not easy to fully support CanonicalIncludes in dynamic index, given the way
we run dynamic index.
o It's also harder to reason about the correctness of include canonicalization for dynamic index
(i.e. symbols in the current file/TU) than static index where symbols are collected
offline and sanity check is possible before shipping to production.
o We have less control/flexibility over symbol info in the dynamic index
(e.g. URIs, path normalization), which could be used to help make decision when inserting includes.
As header collection (especially canonicalization) is relatively new, and enabling
it for dynamic index would immediately affect current users with only dynamic
index support, I propose we disable it for dynamic index for now to avoid
compromising other hot features like code completion and only support it for
static index where include insertion would likely to bring more value.
Reviewers: ilya-biryukov, sammccall, hokein
Subscribers: klimek, jkorous-apple, cfe-commits
Differential Revision: https://reviews.llvm.org/D43550
llvm-svn: 325764
2018-02-22 18:14:05 +08:00
|
|
|
// FIXME(ioeric): we might also want to collect include headers. We would need
|
|
|
|
// to make sure all includes are canonicalized (with CanonicalIncludes), which
|
|
|
|
// is not trivial given the current way of collecting symbols: we only have
|
|
|
|
// AST at this point, but we also need preprocessor callbacks (e.g.
|
|
|
|
// CommentHandler for IWYU pragma) to canonicalize includes.
|
|
|
|
CollectorOpts.CollectIncludePath = false;
|
2018-03-12 22:49:09 +08:00
|
|
|
CollectorOpts.CountReferences = false;
|
2018-09-18 18:30:44 +08:00
|
|
|
CollectorOpts.Origin = SymbolOrigin::Dynamic;
|
2018-06-15 16:55:00 +08:00
|
|
|
if (!URISchemes.empty())
|
|
|
|
CollectorOpts.URISchemes = URISchemes;
|
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;
|
2018-09-19 17:35:04 +08:00
|
|
|
}else {
|
|
|
|
IndexOpts.IndexMacrosInPreprocessor = true;
|
|
|
|
CollectorOpts.CollectMacro = true;
|
|
|
|
}
|
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();
|
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"
|
2018-10-18 23:33:20 +08:00
|
|
|
" ref slab: {4} symbols, {5} refs, {6} bytes",
|
2018-09-18 18:30:44 +08:00
|
|
|
FileName, IsIndexMainAST, Syms.size(), Syms.bytes(), Refs.size(),
|
2018-10-18 23:33:20 +08:00
|
|
|
Refs.numRefs(), Refs.bytes());
|
[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
|
|
|
return {std::move(Syms), std::move(Refs)};
|
2017-12-15 20:25:02 +08:00
|
|
|
}
|
|
|
|
|
2018-09-18 18:30:44 +08:00
|
|
|
std::pair<SymbolSlab, RefSlab>
|
2018-09-18 21:35:16 +08:00
|
|
|
indexMainDecls(ParsedAST &AST, llvm::ArrayRef<std::string> URISchemes) {
|
2018-09-18 18:30:44 +08:00
|
|
|
return indexSymbols(AST.getASTContext(), AST.getPreprocessorPtr(),
|
2018-09-18 21:35:16 +08:00
|
|
|
AST.getLocalTopLevelDecls(),
|
2018-09-18 18:30:44 +08:00
|
|
|
/*IsIndexMainAST=*/true, URISchemes);
|
|
|
|
}
|
|
|
|
|
|
|
|
SymbolSlab indexHeaderSymbols(ASTContext &AST, std::shared_ptr<Preprocessor> PP,
|
|
|
|
llvm::ArrayRef<std::string> URISchemes) {
|
|
|
|
std::vector<Decl *> DeclsToIndex(
|
|
|
|
AST.getTranslationUnitDecl()->decls().begin(),
|
|
|
|
AST.getTranslationUnitDecl()->decls().end());
|
|
|
|
return indexSymbols(AST, std::move(PP), DeclsToIndex,
|
|
|
|
/*IsIndexMainAST=*/false, URISchemes)
|
|
|
|
.first;
|
[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,
|
|
|
|
std::unique_ptr<RefSlab> Refs) {
|
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);
|
|
|
|
if (!Refs)
|
|
|
|
FileToRefs.erase(Path);
|
2018-09-01 03:53:37 +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
|
|
|
FileToRefs[Path] = std::move(Refs);
|
2017-12-14 22:50:58 +08:00
|
|
|
}
|
|
|
|
|
2018-10-16 16:53:52 +08:00
|
|
|
std::unique_ptr<SymbolIndex>
|
|
|
|
FileSymbols::buildIndex(IndexType Type, ArrayRef<std::string> URISchemes) {
|
[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;
|
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);
|
|
|
|
for (const auto &FileAndRefs : FileToRefs)
|
|
|
|
RefSlabs.push_back(FileAndRefs.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;
|
[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 &Slab : SymbolSlabs)
|
[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
|
|
|
for (const auto &Sym : *Slab)
|
|
|
|
AllSymbols.push_back(&Sym);
|
[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.
|
|
|
|
llvm::DenseMap<SymbolID, ArrayRef<Ref>> AllRefs;
|
|
|
|
{
|
|
|
|
llvm::DenseMap<SymbolID, SmallVector<Ref, 4>> MergedRefs;
|
|
|
|
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,
|
|
|
|
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
|
|
|
|
2018-09-10 19:46:07 +08:00
|
|
|
size_t StorageSize = RefsStorage.size() * sizeof(Ref);
|
|
|
|
for (const auto &Slab : SymbolSlabs)
|
|
|
|
StorageSize += Slab->bytes();
|
|
|
|
for (const auto &RefSlab : RefSlabs)
|
|
|
|
StorageSize += RefSlab->bytes();
|
|
|
|
|
[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>(
|
|
|
|
llvm::make_pointee_range(AllSymbols), std::move(AllRefs),
|
|
|
|
std::make_tuple(std::move(SymbolSlabs), std::move(RefSlabs),
|
|
|
|
std::move(RefsStorage)),
|
|
|
|
StorageSize);
|
|
|
|
case IndexType::Heavy:
|
|
|
|
return llvm::make_unique<dex::Dex>(
|
|
|
|
llvm::make_pointee_range(AllSymbols), std::move(AllRefs),
|
|
|
|
std::make_tuple(std::move(SymbolSlabs), std::move(RefSlabs),
|
|
|
|
std::move(RefsStorage)),
|
|
|
|
StorageSize, std::move(URISchemes));
|
|
|
|
}
|
2018-09-01 03:53:37 +08:00
|
|
|
}
|
|
|
|
|
2018-10-16 16:53:52 +08:00
|
|
|
FileIndex::FileIndex(std::vector<std::string> URISchemes, bool UseDex)
|
|
|
|
: MergedIndex(&MainFileIndex, &PreambleIndex), UseDex(UseDex),
|
2018-10-04 22:20:22 +08:00
|
|
|
URISchemes(std::move(URISchemes)),
|
2018-10-16 16:53:52 +08:00
|
|
|
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,
|
|
|
|
std::shared_ptr<Preprocessor> PP) {
|
|
|
|
auto Symbols = indexHeaderSymbols(AST, std::move(PP), URISchemes);
|
|
|
|
PreambleSymbols.update(Path,
|
|
|
|
llvm::make_unique<SymbolSlab>(std::move(Symbols)),
|
|
|
|
llvm::make_unique<RefSlab>());
|
2018-10-16 16:53:52 +08:00
|
|
|
PreambleIndex.reset(PreambleSymbols.buildIndex(
|
|
|
|
UseDex ? IndexType::Heavy : IndexType::Light, URISchemes));
|
2018-09-18 18:30:44 +08:00
|
|
|
}
|
|
|
|
|
2018-09-18 21:35:16 +08:00
|
|
|
void FileIndex::updateMain(PathRef Path, ParsedAST &AST) {
|
|
|
|
auto Contents = indexMainDecls(AST, URISchemes);
|
2018-09-18 18:30:44 +08:00
|
|
|
MainFileSymbols.update(
|
|
|
|
Path, llvm::make_unique<SymbolSlab>(std::move(Contents.first)),
|
|
|
|
llvm::make_unique<RefSlab>(std::move(Contents.second)));
|
2018-10-16 16:53:52 +08:00
|
|
|
MainFileIndex.reset(MainFileSymbols.buildIndex(IndexType::Light, URISchemes));
|
2018-08-24 17:12:54 +08:00
|
|
|
}
|
|
|
|
|
2017-12-14 22:50:58 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|