clang-tools  8.0.0
MemIndex.h
Go to the documentation of this file.
1 //===--- MemIndex.h - 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 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_MEMINDEX_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_MEMINDEX_H
12 
13 #include "Index.h"
14 #include <mutex>
15 
16 namespace clang {
17 namespace clangd {
18 
19 /// MemIndex is a naive in-memory index suitable for a small set of symbols.
20 class MemIndex : public SymbolIndex {
21 public:
22  MemIndex() = default;
23  // All symbols and refs must outlive this index.
24  template <typename SymbolRange, typename RefRange>
25  MemIndex(SymbolRange &&Symbols, RefRange &&Refs) {
26  for (const Symbol &S : Symbols)
27  Index[S.ID] = &S;
28  for (const std::pair<SymbolID, llvm::ArrayRef<Ref>> &R : Refs)
29  this->Refs.try_emplace(R.first, R.second.begin(), R.second.end());
30  }
31  // Symbols are owned by BackingData, Index takes ownership.
32  template <typename SymbolRange, typename RefRange, typename Payload>
33  MemIndex(SymbolRange &&Symbols, RefRange &&Refs, Payload &&BackingData,
34  size_t BackingDataSize)
35  : MemIndex(std::forward<SymbolRange>(Symbols),
36  std::forward<RefRange>(Refs)) {
37  KeepAlive = std::shared_ptr<void>(
38  std::make_shared<Payload>(std::move(BackingData)), nullptr);
39  this->BackingDataSize = BackingDataSize;
40  }
41 
42  /// Builds an index from slabs. The index takes ownership of the data.
43  static std::unique_ptr<SymbolIndex> build(SymbolSlab Symbols, RefSlab Refs);
44 
45  bool
46  fuzzyFind(const FuzzyFindRequest &Req,
47  llvm::function_ref<void(const Symbol &)> Callback) const override;
48 
49  void lookup(const LookupRequest &Req,
50  llvm::function_ref<void(const Symbol &)> Callback) const override;
51 
52  void refs(const RefsRequest &Req,
53  llvm::function_ref<void(const Ref &)> Callback) const override;
54 
55  size_t estimateMemoryUsage() const override;
56 
57 private:
58  // Index is a set of symbols that are deduplicated by symbol IDs.
59  llvm::DenseMap<SymbolID, const Symbol *> Index;
60  // A map from symbol ID to symbol refs, support query by IDs.
61  llvm::DenseMap<SymbolID, llvm::ArrayRef<Ref>> Refs;
62  std::shared_ptr<void> KeepAlive; // poor man's move-only std::any
63  // Size of memory retained by KeepAlive.
64  size_t BackingDataSize = 0;
65 };
66 
67 } // namespace clangd
68 } // namespace clang
69 
70 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_MEMINDEX_H
void refs(const RefsRequest &Req, llvm::function_ref< void(const Ref &)> Callback) const override
Finds all occurrences (e.g.
Definition: MemIndex.cpp:69
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:487
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(SymbolRange &&Symbols, RefRange &&Refs, Payload &&BackingData, size_t BackingDataSize)
Definition: MemIndex.h:33
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Definition: Function.h:29
MemIndex(SymbolRange &&Symbols, RefRange &&Refs)
Definition: MemIndex.h:25
size_t estimateMemoryUsage() const override
Returns estimated size of index (in bytes).
Definition: MemIndex.cpp:87
MemIndex is a naive in-memory index suitable for a small set of symbols.
Definition: MemIndex.h:20
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
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
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