clang-tools  9.0.0
MemIndex.h
Go to the documentation of this file.
1 //===--- MemIndex.h - Dynamic in-memory symbol index. -------------- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_MEMINDEX_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_MEMINDEX_H
11 
12 #include "Index.h"
13 #include <mutex>
14 
15 namespace clang {
16 namespace clangd {
17 
18 /// MemIndex is a naive in-memory index suitable for a small set of symbols.
19 class MemIndex : public SymbolIndex {
20 public:
21  MemIndex() = default;
22  // All symbols and refs must outlive this index.
23  template <typename SymbolRange, typename RefRange, typename RelationRange>
24  MemIndex(SymbolRange &&Symbols, RefRange &&Refs, RelationRange &&Relations) {
25  for (const Symbol &S : Symbols)
26  Index[S.ID] = &S;
27  for (const std::pair<SymbolID, llvm::ArrayRef<Ref>> &R : Refs)
28  this->Refs.try_emplace(R.first, R.second.begin(), R.second.end());
29  for (const Relation &R : Relations)
30  this->Relations[std::make_pair(R.Subject, R.Predicate)].push_back(
31  R.Object);
32  }
33  // Symbols are owned by BackingData, Index takes ownership.
34  template <typename SymbolRange, typename RefRange, typename RelationRange,
35  typename Payload>
36  MemIndex(SymbolRange &&Symbols, RefRange &&Refs, RelationRange &&Relations,
37  Payload &&BackingData, size_t BackingDataSize)
38  : MemIndex(std::forward<SymbolRange>(Symbols),
39  std::forward<RefRange>(Refs),
40  std::forward<RelationRange>(Relations)) {
41  KeepAlive = std::shared_ptr<void>(
42  std::make_shared<Payload>(std::move(BackingData)), nullptr);
43  this->BackingDataSize = BackingDataSize;
44  }
45 
46  /// Builds an index from slabs. The index takes ownership of the data.
47  static std::unique_ptr<SymbolIndex> build(SymbolSlab Symbols, RefSlab Refs,
48  RelationSlab Relations);
49 
50  bool
51  fuzzyFind(const FuzzyFindRequest &Req,
52  llvm::function_ref<void(const Symbol &)> Callback) const override;
53 
54  void lookup(const LookupRequest &Req,
55  llvm::function_ref<void(const Symbol &)> Callback) const override;
56 
57  void refs(const RefsRequest &Req,
58  llvm::function_ref<void(const Ref &)> Callback) const override;
59 
60  void relations(const RelationsRequest &Req,
61  llvm::function_ref<void(const SymbolID &, const Symbol &)>
62  Callback) const override;
63 
64  size_t estimateMemoryUsage() const override;
65 
66 private:
67  // Index is a set of symbols that are deduplicated by symbol IDs.
68  llvm::DenseMap<SymbolID, const Symbol *> Index;
69  // A map from symbol ID to symbol refs, support query by IDs.
70  llvm::DenseMap<SymbolID, llvm::ArrayRef<Ref>> Refs;
71  // A map from (subject, predicate) pair to objects.
72  llvm::DenseMap<std::pair<SymbolID, index::SymbolRole>, std::vector<SymbolID>>
73  Relations;
74  std::shared_ptr<void> KeepAlive; // poor man's move-only std::any
75  // Size of memory retained by KeepAlive.
76  size_t BackingDataSize = 0;
77 };
78 
79 } // namespace clangd
80 } // namespace clang
81 
82 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_MEMINDEX_H
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
Represents a relation between two symbols.
Definition: Relation.h:25
MemIndex(SymbolRange &&Symbols, RefRange &&Refs, RelationRange &&Relations, Payload &&BackingData, size_t BackingDataSize)
Definition: MemIndex.h:36
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:69
void refs(const RefsRequest &Req, llvm::function_ref< void(const Ref &)> Callback) const override
Finds all occurrences (e.g.
Definition: MemIndex.cpp:70
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:85
Represents a symbol occurrence in the source file.
Definition: Ref.h:52
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Definition: Function.h:28
size_t estimateMemoryUsage() const override
Returns estimated size of index (in bytes).
Definition: MemIndex.cpp:108
void relations(const RelationsRequest &Req, llvm::function_ref< void(const SymbolID &, const Symbol &)> Callback) const override
Definition: MemIndex.cpp:88
static std::unique_ptr< SymbolIndex > build(SymbolSlab Symbols, RefSlab Refs, RelationSlab Relations)
Builds an index from slabs. The index takes ownership of the data.
Definition: MemIndex.cpp:19
MemIndex is a naive in-memory index suitable for a small set of symbols.
Definition: MemIndex.h:19
SymbolSlab Symbols
MemIndex(SymbolRange &&Symbols, RefRange &&Refs, RelationRange &&Relations)
Definition: MemIndex.h:24
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
===– 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:60
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:28