clang-tools  14.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 "llvm/ADT/StringSet.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, typename RelationRange>
25  MemIndex(SymbolRange &&Symbols, RefRange &&Refs, RelationRange &&Relations) {
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  for (const Relation &R : Relations)
31  this->Relations[std::make_pair(R.Subject,
32  static_cast<uint8_t>(R.Predicate))]
33  .push_back(R.Object);
34  }
35  // Symbols are owned by BackingData, Index takes ownership.
36  template <typename SymbolRange, typename RefRange, typename RelationRange,
37  typename Payload>
38  MemIndex(SymbolRange &&Symbols, RefRange &&Refs, RelationRange &&Relations,
39  Payload &&BackingData, size_t BackingDataSize)
40  : MemIndex(std::forward<SymbolRange>(Symbols),
41  std::forward<RefRange>(Refs),
42  std::forward<RelationRange>(Relations)) {
43  KeepAlive = std::shared_ptr<void>(
44  std::make_shared<Payload>(std::move(BackingData)), nullptr);
45  this->BackingDataSize = BackingDataSize;
46  }
47 
48  template <typename SymbolRange, typename RefRange, typename RelationRange,
49  typename FileRange, typename Payload>
50  MemIndex(SymbolRange &&Symbols, RefRange &&Refs, RelationRange &&Relations,
51  FileRange &&Files, IndexContents IdxContents, Payload &&BackingData,
52  size_t BackingDataSize)
53  : MemIndex(std::forward<SymbolRange>(Symbols),
54  std::forward<RefRange>(Refs),
55  std::forward<RelationRange>(Relations),
56  std::forward<Payload>(BackingData), BackingDataSize) {
57  this->Files = std::forward<FileRange>(Files);
58  this->IdxContents = IdxContents;
59  }
60 
61  /// Builds an index from slabs. The index takes ownership of the data.
62  static std::unique_ptr<SymbolIndex> build(SymbolSlab Symbols, RefSlab Refs,
63  RelationSlab Relations);
64 
65  bool
66  fuzzyFind(const FuzzyFindRequest &Req,
67  llvm::function_ref<void(const Symbol &)> Callback) const override;
68 
69  void lookup(const LookupRequest &Req,
70  llvm::function_ref<void(const Symbol &)> Callback) const override;
71 
72  bool refs(const RefsRequest &Req,
73  llvm::function_ref<void(const Ref &)> Callback) const override;
74 
75  void relations(const RelationsRequest &Req,
76  llvm::function_ref<void(const SymbolID &, const Symbol &)>
77  Callback) const override;
78 
79  llvm::unique_function<IndexContents(llvm::StringRef) const>
80  indexedFiles() const override;
81 
82  size_t estimateMemoryUsage() const override;
83 
84 private:
85  // Index is a set of symbols that are deduplicated by symbol IDs.
86  llvm::DenseMap<SymbolID, const Symbol *> Index;
87  // A map from symbol ID to symbol refs, support query by IDs.
88  llvm::DenseMap<SymbolID, llvm::ArrayRef<Ref>> Refs;
89  // A map from (subject, predicate) pair to objects.
90  static_assert(sizeof(RelationKind) == sizeof(uint8_t),
91  "RelationKind should be of same size as a uint8_t");
92  llvm::DenseMap<std::pair<SymbolID, uint8_t>, std::vector<SymbolID>> Relations;
93  // Set of files which were used during this index build.
94  llvm::StringSet<> Files;
95  // Contents of the index (symbols, references, etc.)
96  IndexContents IdxContents;
97  std::shared_ptr<void> KeepAlive; // poor man's move-only std::any
98  // Size of memory retained by KeepAlive.
99  size_t BackingDataSize = 0;
100 };
101 
102 } // namespace clangd
103 } // namespace clang
104 
105 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_MEMINDEX_H
clang::clangd::MemIndex::MemIndex
MemIndex(SymbolRange &&Symbols, RefRange &&Refs, RelationRange &&Relations, FileRange &&Files, IndexContents IdxContents, Payload &&BackingData, size_t BackingDataSize)
Definition: MemIndex.h:50
clang::clangd::MemIndex::indexedFiles
llvm::unique_function< IndexContents(llvm::StringRef) const > indexedFiles() const override
Definition: MemIndex.cpp:113
Index.h
clang::clangd::MemIndex::fuzzyFind
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
clang::clangd::Relation
Represents a relation between two symbols.
Definition: Relation.h:34
clang::clangd::MemIndex::relations
void relations(const RelationsRequest &Req, llvm::function_ref< void(const SymbolID &, const Symbol &)> Callback) const override
Definition: MemIndex.cpp:91
clang::clangd::MemIndex::MemIndex
MemIndex(SymbolRange &&Symbols, RefRange &&Refs, RelationRange &&Relations, Payload &&BackingData, size_t BackingDataSize)
Definition: MemIndex.h:38
clang::clangd::RefSlab
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:110
clang::clangd::MemIndex::MemIndex
MemIndex()=default
clang::clangd::FuzzyFindRequest
Definition: Index.h:27
clang::clangd::RelationSlab
Definition: Relation.h:52
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
clang::clangd::IndexContents
IndexContents
Describes what data is covered by an index.
Definition: Index.h:91
Payload
std::string Payload
Definition: SourceCode.cpp:649
clang::clangd::MemIndex::build
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
clang::clangd::MemIndex
MemIndex is a naive in-memory index suitable for a small set of symbols.
Definition: MemIndex.h:20
clang::clangd::SymbolIndex
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:111
clang::clangd::LookupRequest
Definition: Index.h:65
clang::clangd::MemIndex::estimateMemoryUsage
size_t estimateMemoryUsage() const override
Returns estimated size of index (in bytes).
Definition: MemIndex.cpp:119
clang::clangd::MemIndex::lookup
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
clang::clangd::Ref
Represents a symbol occurrence in the source file.
Definition: Ref.h:87
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::MemIndex::refs
bool refs(const RefsRequest &Req, llvm::function_ref< void(const Ref &)> Callback) const override
Finds all occurrences (e.g.
Definition: MemIndex.cpp:70
clang::clangd::Callback
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Definition: Function.h:28
clang::clangd::RelationsRequest
Definition: Index.h:78
clang::clangd::SymbolSlab
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
clang::clangd::SymbolID
Definition: SymbolID.h:32
clang::clangd::RefsRequest
Definition: Index.h:69
clang::clangd::RelationKind
RelationKind
Definition: Relation.h:22
clang::clangd::MemIndex::MemIndex
MemIndex(SymbolRange &&Symbols, RefRange &&Refs, RelationRange &&Relations)
Definition: MemIndex.h:25