Line data Source code
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 0 : MemIndex() = default;
23 : // All symbols and refs must outlive this index.
24 : template <typename SymbolRange, typename RefRange>
25 0 : MemIndex(SymbolRange &&Symbols, RefRange &&Refs) {
26 0 : for (const Symbol &S : Symbols)
27 0 : Index[S.ID] = &S;
28 0 : for (const std::pair<SymbolID, llvm::ArrayRef<Ref>> &R : Refs)
29 0 : this->Refs.try_emplace(R.first, R.second.begin(), R.second.end());
30 0 : }
31 0 : // Symbols are owned by BackingData, Index takes ownership.
32 0 : template <typename SymbolRange, typename RefRange, typename Payload>
33 0 : MemIndex(SymbolRange &&Symbols, RefRange &&Refs, Payload &&BackingData,
34 0 : size_t BackingDataSize)
35 0 : : MemIndex(std::forward<SymbolRange>(Symbols),
36 0 : std::forward<RefRange>(Refs)) {
37 0 : KeepAlive = std::shared_ptr<void>(
38 0 : std::make_shared<Payload>(std::move(BackingData)), nullptr);
39 0 : this->BackingDataSize = BackingDataSize;
40 0 : }
41 0 :
42 0 : /// 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 0 : bool
46 : fuzzyFind(const FuzzyFindRequest &Req,
47 : llvm::function_ref<void(const Symbol &)> Callback) const override;
48 0 :
49 0 : void lookup(const LookupRequest &Req,
50 : llvm::function_ref<void(const Symbol &)> Callback) const override;
51 0 :
52 0 : 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
|