clang-tools  12.0.0git
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,
31  static_cast<uint8_t>(R.Predicate))]
32  .push_back(R.Object);
33  }
34  // Symbols are owned by BackingData, Index takes ownership.
35  template <typename SymbolRange, typename RefRange, typename RelationRange,
36  typename Payload>
37  MemIndex(SymbolRange &&Symbols, RefRange &&Refs, RelationRange &&Relations,
38  Payload &&BackingData, size_t BackingDataSize)
39  : MemIndex(std::forward<SymbolRange>(Symbols),
40  std::forward<RefRange>(Refs),
41  std::forward<RelationRange>(Relations)) {
42  KeepAlive = std::shared_ptr<void>(
43  std::make_shared<Payload>(std::move(BackingData)), nullptr);
44  this->BackingDataSize = BackingDataSize;
45  }
46 
47  /// Builds an index from slabs. The index takes ownership of the data.
48  static std::unique_ptr<SymbolIndex> build(SymbolSlab Symbols, RefSlab Refs,
49  RelationSlab Relations);
50 
51  bool
52  fuzzyFind(const FuzzyFindRequest &Req,
53  llvm::function_ref<void(const Symbol &)> Callback) const override;
54 
55  void lookup(const LookupRequest &Req,
56  llvm::function_ref<void(const Symbol &)> Callback) const override;
57 
58  bool refs(const RefsRequest &Req,
59  llvm::function_ref<void(const Ref &)> Callback) const override;
60 
61  void relations(const RelationsRequest &Req,
62  llvm::function_ref<void(const SymbolID &, const Symbol &)>
63  Callback) const override;
64 
65  size_t estimateMemoryUsage() const override;
66 
67 private:
68  // Index is a set of symbols that are deduplicated by symbol IDs.
69  llvm::DenseMap<SymbolID, const Symbol *> Index;
70  // A map from symbol ID to symbol refs, support query by IDs.
71  llvm::DenseMap<SymbolID, llvm::ArrayRef<Ref>> Refs;
72  // A map from (subject, predicate) pair to objects.
73  static_assert(sizeof(RelationKind) == sizeof(uint8_t),
74  "RelationKind should be of same size as a uint8_t");
75  llvm::DenseMap<std::pair<SymbolID, uint8_t>, std::vector<SymbolID>> Relations;
76  std::shared_ptr<void> KeepAlive; // poor man's move-only std::any
77  // Size of memory retained by KeepAlive.
78  size_t BackingDataSize = 0;
79 };
80 
81 } // namespace clangd
82 } // namespace clang
83 
84 #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:29
MemIndex(SymbolRange &&Symbols, RefRange &&Refs, RelationRange &&Relations, Payload &&BackingData, size_t BackingDataSize)
Definition: MemIndex.h:37
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:104
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:87
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:112
void relations(const RelationsRequest &Req, llvm::function_ref< void(const SymbolID &, const Symbol &)> Callback) const override
Definition: MemIndex.cpp:91
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
bool refs(const RefsRequest &Req, llvm::function_ref< void(const Ref &)> Callback) const override
Finds all occurrences (e.g.
Definition: MemIndex.cpp:70
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++ -*-===//
std::string Payload
Definition: SourceCode.cpp:648
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