clang-tools 8.0.0

MemIndex.cpp
Go to the documentation of this file.
1 //===--- MemIndex.cpp - Dynamic in-memory symbol index. ----------*- C++-*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===-------------------------------------------------------------------===//
9
10 #include "MemIndex.h"
11 #include "FuzzyMatch.h"
12 #include "Logger.h"
13 #include "Quality.h"
14 #include "Trace.h"
15
16 namespace clang {
17 namespace clangd {
18
19 std::unique_ptr<SymbolIndex> MemIndex::build(SymbolSlab Slab, RefSlab Refs) {
20 // Store Slab size before it is moved.
21 const auto BackingDataSize = Slab.bytes() + Refs.bytes();
22 auto Data = std::make_pair(std::move(Slab), std::move(Refs));
23 return llvm::make_unique<MemIndex>(Data.first, Data.second, std::move(Data),
24 BackingDataSize);
25 }
26
27 bool MemIndex::fuzzyFind(
28 const FuzzyFindRequest &Req,
29 llvm::function_ref<void(const Symbol &)> Callback) const {
30 assert(!StringRef(Req.Query).contains("::") &&
31 "There must be no :: in query.");
32 trace::Span Tracer("MemIndex fuzzyFind");
33
34 TopN<std::pair<float, const Symbol *>> Top(
35 Req.Limit ? *Req.Limit : std::numeric_limits<size_t>::max());
36 FuzzyMatcher Filter(Req.Query);
37 bool More = false;
38 for (const auto Pair : Index) {
39 const Symbol *Sym = Pair.second;
40
41 // Exact match against all possible scopes.
42 if (!Req.AnyScope && !llvm::is_contained(Req.Scopes, Sym->Scope))
43 continue;
44 if (Req.RestrictForCodeCompletion &&
45 !(Sym->Flags & Symbol::IndexedForCodeCompletion))
46 continue;
47
48 if (auto Score = Filter.match(Sym->Name))
49 if (Top.push({*Score * quality(*Sym), Sym}))
50 More = true; // An element with smallest score was discarded.
51 }
52 auto Results = std::move(Top).items();
53 SPAN_ATTACH(Tracer, "results", static_cast<int>(Results.size()));
54 for (const auto &Item : Results)
55 Callback(*Item.second);
56 return More;
57 }
58
59 void MemIndex::lookup(const LookupRequest &Req,
60 llvm::function_ref<void(const Symbol &)> Callback) const {
61 trace::Span Tracer("MemIndex lookup");
62 for (const auto &ID : Req.IDs) {
63 auto I = Index.find(ID);
64 if (I != Index.end())
65 Callback(*I->second);
66 }
67 }
68
69 void MemIndex::refs(const RefsRequest &Req,
70 llvm::function_ref<void(const Ref &)> Callback) const {
71 trace::Span Tracer("MemIndex refs");
72 uint32_t Remaining =
73 Req.Limit.getValueOr(std::numeric_limits<uint32_t>::max());
74 for (const auto &ReqID : Req.IDs) {
75 auto SymRefs = Refs.find(ReqID);
76 if (SymRefs == Refs.end())
77 continue;
78 for (const auto &O : SymRefs->second) {
79 if (Remaining > 0 && static_cast<int>(Req.Filter & O.Kind)) {
80 --Remaining;
81 Callback(O);
82 }
83 }
84 }
85 }
86
87 size_t MemIndex::estimateMemoryUsage() const {
88 return Index.getMemorySize() + Refs.getMemorySize() + BackingDataSize;
89 }
90
91 } // namespace clangd
92 } // namespace clang
clang::clangd::SymbolSlab::bytes
size_t bytes() const
Definition: Index.h:307
clang::clangd::SymbolSlab
Definition: Index.h:292
clang::clangd::RefsRequest::IDs
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:477
clang::clangd::FuzzyFindRequest::AnyScope
bool AnyScope
If set to true, allow symbols from any scope.
Definition: Index.h:449
Trace.h
clang::clangd::FuzzyFindRequest::RestrictForCodeCompletion
bool RestrictForCodeCompletion
If set to true, only symbols for completion support will be considered.
Definition: Index.h:454
clang::clangd::RefSlab
Definition: Index.h:388
clang::clangd::MemIndex::refs
void refs(const RefsRequest &Req, llvm::function_ref< void(const Ref &)> Callback) const override
Finds all occurrences (e.g.
Definition: MemIndex.cpp:69
clang::clangd::FuzzyMatcher
Definition: FuzzyMatch.h:72
clang::clangd::LookupRequest::IDs
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:473
clang::clangd::Ref
Definition: Index.h:373
clang::clangd::RefsRequest
Definition: Index.h:476
clang::clangd::MemIndex::build
static std::unique_ptr< SymbolIndex > build(SymbolSlab Symbols, RefSlab Refs)
Builds an index from slabs. The index takes ownership of the data.
Definition: MemIndex.cpp:19
MemIndex.h
Results
std::vector< CodeCompletionResult > Results
Definition: CodeComplete.cpp:702
clang::clangd::Callback
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Definition: Function.h:29
clang::clangd::Symbol::Scope
llvm::StringRef Scope
Definition: Index.h:168
clang::clangd::FuzzyFindRequest
Definition: Index.h:436
clang::clangd::FuzzyFindRequest::Scopes
std::vector< std::string > Scopes
If this is non-empty, symbols must be in at least one of the scopes (e.g.
Definition: Index.h:446
clang::clangd::MemIndex::estimateMemoryUsage
size_t estimateMemoryUsage() const override
Returns estimated size of index (in bytes).
Definition: MemIndex.cpp:87
clang::clangd::RefsRequest::Filter
RefKind Filter
Definition: Index.h:478
clang::clangd::Symbol::IndexedForCodeCompletion
Whether or not this symbol is meant to be used for the code completion.
Definition: Index.h:239
clang::clangd::Symbol::Flags
SymbolFlag Flags
Definition: Index.h:248
clang::clangd::FuzzyFindRequest::Query
std::string Query
A query string for the fuzzy find.
Definition: Index.h:439
clang::clangd::FuzzyMatcher::match
llvm::Optional< float > match(llvm::StringRef Word)
Definition: FuzzyMatch.cpp:93
clang::clangd::TopN::push
bool push(value_type &&V)
Definition: Quality.h:156
Quality.h
Logger.h
clang::clangd::Symbol
Definition: Index.h:160
clang::clangd::LookupRequest
Definition: Index.h:472
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::clangd::Symbol::Name
llvm::StringRef Name
Definition: Index.h:166
clang::clangd::RefsRequest::Limit
llvm::Optional< uint32_t > Limit
If set, limit the number of refers returned from the index.
Definition: Index.h:482
clang::clangd::FuzzyFindRequest::Limit
llvm::Optional< uint32_t > Limit
The number of top candidates to return.
Definition: Index.h:452
clang::clangd::MemIndex::lookup
void lookup(const LookupRequest &Req, llvm::function_ref< void(const Symbol &)> Callback) const override
Looks up symbols with any of the given symbol IDs and applies Callback on each matched symbol...
Definition: MemIndex.cpp:59
clang::clangd::RefSlab::bytes
size_t bytes() const
Definition: Index.h:405
clang::clangd::trace::Span
Records an event whose duration is the lifetime of the Span object.
Definition: Trace.h:83
SPAN_ATTACH
#define SPAN_ATTACH(S, Name, Expr)
Attach a key-value pair to a Span event.
Definition: Trace.h:99
clang::clangd::MemIndex::fuzzyFind
bool fuzzyFind(const FuzzyFindRequest &Req, llvm::function_ref< void(const Symbol &)> Callback) const override
Matches symbols in the index fuzzily and applies Callback on each matched symbol before returning...
Definition: MemIndex.cpp:27
FuzzyMatch.h
clang::clangd::TopN
TopN<T> is a lossy container that preserves only the "best" N elements.
Definition: Quality.h:148