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 
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 
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 &&
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 
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 
88  return Index.getMemorySize() + Refs.getMemorySize() + BackingDataSize;
89 }
90 
91 } // namespace clangd
92 } // namespace clang
size_t bytes() const
Definition: Index.h:307
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:477
bool AnyScope
If set to true, allow symbols from any scope.
Definition: Index.h:449
bool RestrictForCodeCompletion
If set to true, only symbols for completion support will be considered.
Definition: Index.h:454
void refs(const RefsRequest &Req, llvm::function_ref< void(const Ref &)> Callback) const override
Finds all occurrences (e.g.
Definition: MemIndex.cpp:69
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:473
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
std::vector< CodeCompletionResult > Results
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Definition: Function.h:29
llvm::StringRef Scope
Definition: Index.h:168
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
size_t estimateMemoryUsage() const override
Returns estimated size of index (in bytes).
Definition: MemIndex.cpp:87
Whether or not this symbol is meant to be used for the code completion.
Definition: Index.h:239
SymbolFlag Flags
Definition: Index.h:248
std::string Query
A query string for the fuzzy find.
Definition: Index.h:439
llvm::Optional< float > match(llvm::StringRef Word)
Definition: FuzzyMatch.cpp:93
bool push(value_type &&V)
Definition: Quality.h:156
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::StringRef Name
Definition: Index.h:166
llvm::Optional< uint32_t > Limit
If set, limit the number of refers returned from the index.
Definition: Index.h:482
llvm::Optional< uint32_t > Limit
The number of top candidates to return.
Definition: Index.h:452
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
size_t bytes() const
Definition: Index.h:405
Records an event whose duration is the lifetime of the Span object.
Definition: Trace.h:83
#define SPAN_ATTACH(S, Name, Expr)
Attach a key-value pair to a Span event.
Definition: Trace.h:99
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
TopN<T> is a lossy container that preserves only the "best" N elements.
Definition: Quality.h:148