clang-tools 19.0.0git
Index.cpp
Go to the documentation of this file.
1//===--- Index.cpp -----------------------------------------------*- 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 "Index.h"
10#include "llvm/ADT/StringRef.h"
11#include <limits>
12
13namespace clang {
14namespace clangd {
15
16void SwapIndex::reset(std::unique_ptr<SymbolIndex> Index) {
17 // Keep the old index alive, so we don't destroy it under lock (may be slow).
18 std::shared_ptr<SymbolIndex> Pin;
19 {
20 std::lock_guard<std::mutex> Lock(Mutex);
21 Pin = std::move(this->Index);
22 this->Index = std::move(Index);
23 }
24}
25std::shared_ptr<SymbolIndex> SwapIndex::snapshot() const {
26 std::lock_guard<std::mutex> Lock(Mutex);
27 return Index;
28}
29
30bool fromJSON(const llvm::json::Value &Parameters, FuzzyFindRequest &Request,
31 llvm::json::Path P) {
32 llvm::json::ObjectMapper O(Parameters, P);
33 int64_t Limit;
34 bool OK =
35 O && O.map("Query", Request.Query) && O.map("Scopes", Request.Scopes) &&
36 O.map("AnyScope", Request.AnyScope) && O.map("Limit", Limit) &&
37 O.map("RestrictForCodeCompletion", Request.RestrictForCodeCompletion) &&
38 O.map("ProximityPaths", Request.ProximityPaths) &&
39 O.map("PreferredTypes", Request.PreferredTypes);
40 if (OK && Limit <= std::numeric_limits<uint32_t>::max())
41 Request.Limit = Limit;
42 return OK;
43}
44
45llvm::json::Value toJSON(const FuzzyFindRequest &Request) {
46 return llvm::json::Object{
47 {"Query", Request.Query},
48 {"Scopes", Request.Scopes},
49 {"AnyScope", Request.AnyScope},
50 {"Limit", Request.Limit},
51 {"RestrictForCodeCompletion", Request.RestrictForCodeCompletion},
52 {"ProximityPaths", Request.ProximityPaths},
53 {"PreferredTypes", Request.PreferredTypes},
54 };
55}
56
58 llvm::function_ref<void(const Symbol &)> CB) const {
59 return snapshot()->fuzzyFind(R, CB);
60}
62 llvm::function_ref<void(const Symbol &)> CB) const {
63 return snapshot()->lookup(R, CB);
64}
66 llvm::function_ref<void(const Ref &)> CB) const {
67 return snapshot()->refs(R, CB);
68}
70 const RelationsRequest &R,
71 llvm::function_ref<void(const SymbolID &, const Symbol &)> CB) const {
72 return snapshot()->relations(R, CB);
73}
74
75llvm::unique_function<IndexContents(llvm::StringRef) const>
77 // The index snapshot should outlive this method return value.
78 auto SnapShot = snapshot();
79 auto IndexedFiles = SnapShot->indexedFiles();
80 return [KeepAlive{std::move(SnapShot)},
81 IndexContainsFile{std::move(IndexedFiles)}](llvm::StringRef File) {
82 return IndexContainsFile(File);
83 };
84}
85
87 return snapshot()->estimateMemoryUsage();
88}
89
90} // namespace clangd
91} // namespace clang
ArrayRef< const ParmVarDecl * > Parameters
Definition: AST.cpp:816
void relations(const RelationsRequest &, llvm::function_ref< void(const SymbolID &, const Symbol &)>) const override
Definition: Index.cpp:69
bool refs(const RefsRequest &, llvm::function_ref< void(const Ref &)>) const override
Finds all occurrences (e.g.
Definition: Index.cpp:65
void reset(std::unique_ptr< SymbolIndex >)
Definition: Index.cpp:16
llvm::unique_function< IndexContents(llvm::StringRef) const > indexedFiles() const override
Definition: Index.cpp:76
size_t estimateMemoryUsage() const override
Returns estimated size of index (in bytes).
Definition: Index.cpp:86
void lookup(const LookupRequest &, llvm::function_ref< void(const Symbol &)>) const override
Looks up symbols with any of the given symbol IDs and applies Callback on each matched symbol.
Definition: Index.cpp:61
bool fuzzyFind(const FuzzyFindRequest &, llvm::function_ref< void(const Symbol &)>) const override
Matches symbols in the index fuzzily and applies Callback on each matched symbol before returning.
Definition: Index.cpp:57
llvm::unique_function< IndexContents(llvm::StringRef) const > IndexedFiles
Returns function which checks if the specified file was used to build this index or not.
Definition: Index.h:155
IndexContents
Describes what data is covered by an index.
Definition: Index.h:93
llvm::json::Value toJSON(const FuzzyFindRequest &Request)
Definition: Index.cpp:45
bool fromJSON(const llvm::json::Value &Parameters, FuzzyFindRequest &Request, llvm::json::Path P)
Definition: Index.cpp:30
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
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
std::vector< std::string > ProximityPaths
Contextually relevant files (e.g.
Definition: Index.h:47
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
std::vector< std::string > PreferredTypes
Preferred types of symbols. These are raw representation of OpaqueType.
Definition: Index.h:49
Represents a symbol occurrence in the source file.
Definition: Ref.h:85
The class presents a C++ symbol, e.g.
Definition: Symbol.h:39