2017-12-14 19:25:49 +08:00
|
|
|
//===--- MemIndex.cpp - Dynamic in-memory symbol index. ----------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===-------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MemIndex.h"
|
2018-01-18 16:35:04 +08:00
|
|
|
#include "../FuzzyMatch.h"
|
2017-12-20 17:17:31 +08:00
|
|
|
#include "../Logger.h"
|
2018-01-18 16:35:04 +08:00
|
|
|
#include <queue>
|
2017-12-14 19:25:49 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
|
|
|
void MemIndex::build(std::shared_ptr<std::vector<const Symbol *>> Syms) {
|
|
|
|
llvm::DenseMap<SymbolID, const Symbol *> TempIndex;
|
|
|
|
for (const Symbol *Sym : *Syms)
|
|
|
|
TempIndex[Sym->ID] = Sym;
|
|
|
|
|
|
|
|
// Swap out the old symbols and index.
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
Index = std::move(TempIndex);
|
|
|
|
Symbols = std::move(Syms); // Relase old symbols.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-28 22:47:01 +08:00
|
|
|
bool MemIndex::fuzzyFind(
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
const FuzzyFindRequest &Req,
|
2017-12-28 22:47:01 +08:00
|
|
|
llvm::function_ref<void(const Symbol &)> Callback) const {
|
2017-12-19 19:37:40 +08:00
|
|
|
assert(!StringRef(Req.Query).contains("::") &&
|
|
|
|
"There must be no :: in query.");
|
|
|
|
|
2018-01-18 16:35:04 +08:00
|
|
|
std::priority_queue<std::pair<float, const Symbol *>> Top;
|
|
|
|
FuzzyMatcher Filter(Req.Query);
|
|
|
|
bool More = false;
|
2017-12-14 19:25:49 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
for (const auto Pair : Index) {
|
|
|
|
const Symbol *Sym = Pair.second;
|
2017-12-19 19:37:40 +08:00
|
|
|
|
|
|
|
// Exact match against all possible scopes.
|
2017-12-28 22:47:01 +08:00
|
|
|
if (!Req.Scopes.empty() && !llvm::is_contained(Req.Scopes, Sym->Scope))
|
2017-12-19 19:37:40 +08:00
|
|
|
continue;
|
[clangd] Add "member" symbols to the index
Summary:
This adds more symbols to the index:
- member variables and functions
- enum constants in scoped enums
The code completion behavior should remain intact but workspace symbols should
now provide much more useful symbols.
Other symbols should be considered such as the ones in "main files" (files not
being included) but this can be done separately as this introduces its fair
share of problems.
Signed-off-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Reviewers: ioeric, sammccall
Reviewed By: ioeric, sammccall
Subscribers: hokein, sammccall, jkorous, klimek, ilya-biryukov, jkorous-apple, ioeric, MaskRay, cfe-commits
Differential Revision: https://reviews.llvm.org/D44954
llvm-svn: 334017
2018-06-05 22:01:40 +08:00
|
|
|
if (Req.RestrictForCodeCompletion && !Sym->IsIndexedForCodeCompletion)
|
|
|
|
continue;
|
2017-12-19 19:37:40 +08:00
|
|
|
|
2018-01-18 16:35:04 +08:00
|
|
|
if (auto Score = Filter.match(Sym->Name)) {
|
2018-05-03 22:53:02 +08:00
|
|
|
Top.emplace(-*Score * quality(*Sym), Sym);
|
2018-01-18 16:35:04 +08:00
|
|
|
if (Top.size() > Req.MaxCandidateCount) {
|
|
|
|
More = true;
|
|
|
|
Top.pop();
|
|
|
|
}
|
2017-12-14 19:25:49 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-18 16:35:04 +08:00
|
|
|
for (; !Top.empty(); Top.pop())
|
|
|
|
Callback(*Top.top().second);
|
2017-12-14 19:25:49 +08:00
|
|
|
}
|
2018-01-18 16:35:04 +08:00
|
|
|
return More;
|
2017-12-14 19:25:49 +08:00
|
|
|
}
|
|
|
|
|
2018-03-14 17:48:05 +08:00
|
|
|
void MemIndex::lookup(const LookupRequest &Req,
|
|
|
|
llvm::function_ref<void(const Symbol &)> Callback) const {
|
2018-08-20 17:07:59 +08:00
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
2018-03-14 17:48:05 +08:00
|
|
|
for (const auto &ID : Req.IDs) {
|
|
|
|
auto I = Index.find(ID);
|
|
|
|
if (I != Index.end())
|
|
|
|
Callback(*I->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 22:44:34 +08:00
|
|
|
std::unique_ptr<SymbolIndex> MemIndex::build(SymbolSlab Slab) {
|
|
|
|
struct Snapshot {
|
|
|
|
SymbolSlab Slab;
|
|
|
|
std::vector<const Symbol *> Pointers;
|
|
|
|
};
|
|
|
|
auto Snap = std::make_shared<Snapshot>();
|
|
|
|
Snap->Slab = std::move(Slab);
|
|
|
|
for (auto &Sym : Snap->Slab)
|
|
|
|
Snap->Pointers.push_back(&Sym);
|
|
|
|
auto S = std::shared_ptr<std::vector<const Symbol *>>(std::move(Snap),
|
|
|
|
&Snap->Pointers);
|
|
|
|
auto MemIdx = llvm::make_unique<MemIndex>();
|
|
|
|
MemIdx->build(std::move(S));
|
|
|
|
return std::move(MemIdx);
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:14:32 +08:00
|
|
|
void MemIndex::findOccurrences(
|
|
|
|
const OccurrencesRequest &Req,
|
|
|
|
llvm::function_ref<void(const SymbolOccurrence &)> Callback) const {
|
|
|
|
log("findOccurrences is not implemented.");
|
|
|
|
}
|
|
|
|
|
2017-12-14 19:25:49 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|