clang-tools 10.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,
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
clang::clangd::SymbolSlab
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
clang::clangd::Relation
Represents a relation between two symbols.
Definition: Relation.h:29
clang::clangd::MemIndex::MemIndex
MemIndex(SymbolRange &&Symbols, RefRange &&Refs, RelationRange &&Relations, Payload &&BackingData, size_t BackingDataSize)
Definition: MemIndex.h:37
clang::clangd::RefSlab
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:69
Index.h
clang::clangd::RelationKind
RelationKind
Definition: Relation.h:22
clang::clangd::RelationSlab
Definition: Relation.h:45
clang::clangd::SymbolIndex
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:85
clang::clangd::Ref
Represents a symbol occurrence in the source file.
Definition: Ref.h:52
clang::clangd::RefsRequest
Definition: Index.h:67
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::FuzzyFindRequest
Definition: Index.h:26
clang::clangd::MemIndex::estimateMemoryUsage
size_t estimateMemoryUsage() const override
Returns estimated size of index (in bytes).
Definition: MemIndex.cpp:112
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::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
MemIndex()=default
clang::clangd::MemIndex
MemIndex is a naive in-memory index suitable for a small set of symbols.
Definition: MemIndex.h:19
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
Symbols
SymbolSlab Symbols
Definition: SymbolCollectorTests.cpp:295
clang::clangd::MemIndex::MemIndex
MemIndex(SymbolRange &&Symbols, RefRange &&Refs, RelationRange &&Relations)
Definition: MemIndex.h:24
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
clang::clangd::LookupRequest
Definition: Index.h:63
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::RelationsRequest
Definition: Index.h:76
Payload
std::string Payload
Definition: SourceCode.cpp:771
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::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::SymbolID
Definition: SymbolID.h:31