clang-tools 20.0.0git
MemIndex.cpp
Go to the documentation of this file.
1//===--- MemIndex.cpp - 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#include "MemIndex.h"
10#include "FuzzyMatch.h"
11#include "Quality.h"
12#include "index/Index.h"
13#include "support/Trace.h"
14
15namespace clang {
16namespace clangd {
17
18std::unique_ptr<SymbolIndex> MemIndex::build(SymbolSlab Slab, RefSlab Refs,
19 RelationSlab Relations) {
20 // Store Slab size before it is moved.
21 const auto BackingDataSize = Slab.bytes() + Refs.bytes();
22 auto Data = std::make_pair(std::move(Slab), std::move(Refs));
23 return std::make_unique<MemIndex>(Data.first, Data.second, Relations,
24 std::move(Data), BackingDataSize);
25}
26
28 const FuzzyFindRequest &Req,
29 llvm::function_ref<void(const Symbol &)> Callback) const {
30 assert(!StringRef(Req.Query).contains("::") &&
31 "There must be no :: in query.");
32 trace::Span Tracer("MemIndex fuzzyFind");
33
35 Req.Limit.value_or(std::numeric_limits<size_t>::max()));
36 FuzzyMatcher Filter(Req.Query);
37 bool More = false;
38 for (const auto &Pair : Index) {
39 const Symbol *Sym = Pair.second;
40
41 // Exact match against all possible scopes.
42 if (!Req.AnyScope && !llvm::is_contained(Req.Scopes, Sym->Scope))
43 continue;
46 continue;
47
48 if (auto Score = Filter.match(Sym->Name))
49 if (Top.push({*Score * quality(*Sym), Sym}))
50 More = true; // An element with smallest score was discarded.
51 }
52 auto Results = std::move(Top).items();
53 SPAN_ATTACH(Tracer, "results", static_cast<int>(Results.size()));
54 for (const auto &Item : Results)
55 Callback(*Item.second);
56 return More;
57}
58
60 llvm::function_ref<void(const Symbol &)> Callback) const {
61 trace::Span Tracer("MemIndex lookup");
62 for (const auto &ID : Req.IDs) {
63 auto I = Index.find(ID);
64 if (I != Index.end())
65 Callback(*I->second);
66 }
67}
68
70 llvm::function_ref<void(const Ref &)> Callback) const {
71 trace::Span Tracer("MemIndex refs");
72 uint32_t Remaining = Req.Limit.value_or(std::numeric_limits<uint32_t>::max());
73 for (const auto &ReqID : Req.IDs) {
74 auto SymRefs = Refs.find(ReqID);
75 if (SymRefs == Refs.end())
76 continue;
77 for (const auto &O : SymRefs->second) {
78 if (!static_cast<int>(Req.Filter & O.Kind))
79 continue;
80 if (Remaining == 0)
81 return true; // More refs were available.
82 --Remaining;
83 Callback(O);
84 }
85 }
86 return false; // We reported all refs.
87}
88
90 const ContainedRefsRequest &Req,
91 llvm::function_ref<void(const ContainedRefsResult &)> Callback) const {
92 trace::Span Tracer("MemIndex refersTo");
93 uint32_t Remaining = Req.Limit.value_or(std::numeric_limits<uint32_t>::max());
94 for (const auto &Pair : Refs) {
95 for (const auto &R : Pair.second) {
96 if (!static_cast<int>(ContainedRefsRequest::SupportedRefKinds & R.Kind) ||
97 Req.ID != R.Container)
98 continue;
99 if (Remaining == 0)
100 return true; // More refs were available.
101 --Remaining;
102 Callback({R.Location, R.Kind, Pair.first});
103 }
104 }
105 return false; // We reported all refs.
106}
107
109 const RelationsRequest &Req,
110 llvm::function_ref<void(const SymbolID &, const Symbol &)> Callback) const {
111 uint32_t Remaining = Req.Limit.value_or(std::numeric_limits<uint32_t>::max());
112 for (const SymbolID &Subject : Req.Subjects) {
113 LookupRequest LookupReq;
114 auto It = Relations.find(
115 std::make_pair(Subject, static_cast<uint8_t>(Req.Predicate)));
116 if (It != Relations.end()) {
117 for (const auto &Obj : It->second) {
118 if (Remaining > 0) {
119 --Remaining;
120 LookupReq.IDs.insert(Obj);
121 }
122 }
123 }
124 lookup(LookupReq, [&](const Symbol &Object) { Callback(Subject, Object); });
125 }
126}
127
128llvm::unique_function<IndexContents(llvm::StringRef) const>
130 return [this](llvm::StringRef FileURI) {
131 return Files.contains(FileURI) ? IdxContents : IndexContents::None;
132 };
133}
134
136 return Index.getMemorySize() + Refs.getMemorySize() +
137 Relations.getMemorySize() + BackingDataSize;
138}
139
140} // namespace clangd
141} // namespace clang
Bracket::Index Pair
Definition: Bracket.cpp:80
std::vector< CodeCompletionResult > Results
std::optional< float > Score
#define SPAN_ATTACH(S, Name, Expr)
Attach a key-value pair to a Span event.
Definition: Trace.h:164
size_t estimateMemoryUsage() const override
Returns estimated size of index (in bytes).
Definition: MemIndex.cpp:135
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
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
void relations(const RelationsRequest &Req, llvm::function_ref< void(const SymbolID &, const Symbol &)> Callback) const override
Definition: MemIndex.cpp:108
llvm::unique_function< IndexContents(llvm::StringRef) const > indexedFiles() const override
Definition: MemIndex.cpp:129
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:18
bool containedRefs(const ContainedRefsRequest &Req, llvm::function_ref< void(const ContainedRefsResult &)> Callback) const override
Find all symbols that are referenced by a symbol and apply Callback on each result.
Definition: MemIndex.cpp:89
bool refs(const RefsRequest &Req, llvm::function_ref< void(const Ref &)> Callback) const override
Finds all occurrences (e.g.
Definition: MemIndex.cpp:69
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:111
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:201
size_t bytes() const
Definition: Symbol.h:217
TopN<T> is a lossy container that preserves only the "best" N elements.
Definition: Quality.h:189
bool push(value_type &&V)
Definition: Quality.h:197
Records an event whose duration is the lifetime of the Span object.
Definition: Trace.h:143
IndexContents
Describes what data is covered by an index.
Definition: Index.h:114
llvm::json::Object Obj
Definition: LSPClient.cpp:217
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Definition: Function.h:28
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::optional< uint32_t > Limit
If set, limit the number of refers returned from the index.
Definition: Index.h:90
static const RefKind SupportedRefKinds
Note that RefKind::Call just restricts the matched SymbolKind to functions, not the form of the refer...
Definition: Index.h:84
std::vector< std::string > Scopes
If this is non-empty, symbols must be in at least one of the scopes (e.g.
Definition: Index.h:36
bool RestrictForCodeCompletion
If set to true, only symbols for completion support will be considered.
Definition: Index.h:44
std::string Query
A query string for the fuzzy find.
Definition: Index.h:29
bool AnyScope
If set to true, allow symbols from any scope.
Definition: Index.h:39
std::optional< uint32_t > Limit
The number of top candidates to return.
Definition: Index.h:42
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:65
Represents a symbol occurrence in the source file.
Definition: Ref.h:88
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:69
std::optional< uint32_t > Limit
If set, limit the number of refers returned from the index.
Definition: Index.h:74
std::optional< uint32_t > Limit
If set, limit the number of relations returned from the index.
Definition: Index.h:97
llvm::DenseSet< SymbolID > Subjects
Definition: Index.h:94
The class presents a C++ symbol, e.g.
Definition: Symbol.h:39
SymbolFlag Flags
Definition: Symbol.h:151
@ IndexedForCodeCompletion
Whether or not this symbol is meant to be used for the code completion.
Definition: Symbol.h:141
llvm::StringRef Name
The unqualified name of the symbol, e.g. "bar" (for ns::bar).
Definition: Symbol.h:45
llvm::StringRef Scope
The containing namespace. e.g. "" (global), "ns::" (top-level namespace).
Definition: Symbol.h:47