clang-tools  7.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 <queue>
14 
15 namespace clang {
16 namespace clangd {
17 
18 void MemIndex::build(std::shared_ptr<std::vector<const Symbol *>> Syms) {
19  llvm::DenseMap<SymbolID, const Symbol *> TempIndex;
20  for (const Symbol *Sym : *Syms)
21  TempIndex[Sym->ID] = Sym;
22 
23  // Swap out the old symbols and index.
24  {
25  std::lock_guard<std::mutex> Lock(Mutex);
26  Index = std::move(TempIndex);
27  Symbols = std::move(Syms); // Relase old symbols.
28  }
29 }
30 
32  const FuzzyFindRequest &Req,
33  llvm::function_ref<void(const Symbol &)> Callback) const {
34  assert(!StringRef(Req.Query).contains("::") &&
35  "There must be no :: in query.");
36 
37  std::priority_queue<std::pair<float, const Symbol *>> Top;
38  FuzzyMatcher Filter(Req.Query);
39  bool More = false;
40  {
41  std::lock_guard<std::mutex> Lock(Mutex);
42  for (const auto Pair : Index) {
43  const Symbol *Sym = Pair.second;
44 
45  // Exact match against all possible scopes.
46  if (!Req.Scopes.empty() && !llvm::is_contained(Req.Scopes, Sym->Scope))
47  continue;
49  continue;
50 
51  if (auto Score = Filter.match(Sym->Name)) {
52  Top.emplace(-*Score * quality(*Sym), Sym);
53  if (Top.size() > Req.MaxCandidateCount) {
54  More = true;
55  Top.pop();
56  }
57  }
58  }
59  for (; !Top.empty(); Top.pop())
60  Callback(*Top.top().second);
61  }
62  return More;
63 }
64 
66  llvm::function_ref<void(const Symbol &)> Callback) const {
67  for (const auto &ID : Req.IDs) {
68  auto I = Index.find(ID);
69  if (I != Index.end())
70  Callback(*I->second);
71  }
72 }
73 
74 std::unique_ptr<SymbolIndex> MemIndex::build(SymbolSlab Slab) {
75  struct Snapshot {
76  SymbolSlab Slab;
77  std::vector<const Symbol *> Pointers;
78  };
79  auto Snap = std::make_shared<Snapshot>();
80  Snap->Slab = std::move(Slab);
81  for (auto &Sym : Snap->Slab)
82  Snap->Pointers.push_back(&Sym);
83  auto S = std::shared_ptr<std::vector<const Symbol *>>(std::move(Snap),
84  &Snap->Pointers);
85  auto MemIdx = llvm::make_unique<MemIndex>();
86  MemIdx->build(std::move(S));
87  return std::move(MemIdx);
88 }
89 
90 } // namespace clangd
91 } // namespace clang
void build(std::shared_ptr< std::vector< const Symbol *>> Symbols)
(Re-)Build index for Symbols.
Definition: MemIndex.cpp:18
bool IsIndexedForCodeCompletion
Whether or not this symbol is meant to be used for the code completion.
Definition: Index.h:190
size_t MaxCandidateCount
The number of top candidates to return.
Definition: Index.h:303
bool RestrictForCodeCompletion
If set to true, only symbols for completion support will be considered.
Definition: Index.h:305
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:312
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Definition: Function.h:28
llvm::StringRef Scope
Definition: Index.h:172
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:300
std::string Query
A query string for the fuzzy find.
Definition: Index.h:293
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::StringRef Name
Definition: Index.h:170
llvm::Optional< float > match(llvm::StringRef Word)
Definition: FuzzyMatch.cpp:94
double quality(const Symbol &S)
Definition: Index.cpp:61
virtual 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:65
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:31