clang-tools 8.0.0

MemIndex.h
Go to the documentation of this file.
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 MemIndex() = default;
23 // All symbols and refs must outlive this index.
24 template <typename SymbolRange, typename RefRange>
25 MemIndex(SymbolRange &&Symbols, RefRange &&Refs) {
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 }
31 // Symbols are owned by BackingData, Index takes ownership.
32 template <typename SymbolRange, typename RefRange, typename Payload>
33 MemIndex(SymbolRange &&Symbols, RefRange &&Refs, Payload &&BackingData,
34 size_t BackingDataSize)
35 : MemIndex(std::forward<SymbolRange>(Symbols),
36 std::forward<RefRange>(Refs)) {
37 KeepAlive = std::shared_ptr<void>(
38 std::make_shared<Payload>(std::move(BackingData)), nullptr);
39 this->BackingDataSize = BackingDataSize;
40 }
41
42 /// 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 bool
46 fuzzyFind(const FuzzyFindRequest &Req,
47 llvm::function_ref<void(const Symbol &)> Callback) const override;
48
49 void lookup(const LookupRequest &Req,
50 llvm::function_ref<void(const Symbol &)> Callback) const override;
51
52 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
clang::clangd::SymbolSlab
Definition: Index.h:292
clang::clangd::RefSlab
Definition: Index.h:388
Index.h
clang::clangd::MemIndex::refs
void refs(const RefsRequest &Req, llvm::function_ref< void(const Ref &)> Callback) const override
Finds all occurrences (e.g.
Definition: MemIndex.cpp:69
clang::clangd::SymbolIndex
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:487
clang::clangd::Ref
Definition: Index.h:373
clang::clangd::RefsRequest
Definition: Index.h:476
clang::clangd::MemIndex::build
static std::unique_ptr< SymbolIndex > build(SymbolSlab Symbols, RefSlab Refs)
Builds an index from slabs. The index takes ownership of the data.
Definition: MemIndex.cpp:19
clang::clangd::MemIndex::MemIndex
MemIndex(SymbolRange &&Symbols, RefRange &&Refs, Payload &&BackingData, size_t BackingDataSize)
Definition: MemIndex.h:33
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:29
clang::clangd::FuzzyFindRequest
Definition: Index.h:436
clang::clangd::MemIndex::MemIndex
MemIndex(SymbolRange &&Symbols, RefRange &&Refs)
Definition: MemIndex.h:25
clang::clangd::MemIndex::estimateMemoryUsage
size_t estimateMemoryUsage() const override
Returns estimated size of index (in bytes).
Definition: MemIndex.cpp:87
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:20
clang::clangd::Symbol
Definition: Index.h:160
clang::clangd::LookupRequest
Definition: Index.h:472
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
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:59
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:27
clang::clangd::SymbolID
Definition: SymbolID.h:31