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(Prop: "Query", Out&: Request.Query) && O.map(Prop: "Scopes", Out&: Request.Scopes) &&
36 O.map(Prop: "AnyScope", Out&: Request.AnyScope) && O.map(Prop: "Limit", Out&: Limit) &&
37 O.map(Prop: "RestrictForCodeCompletion", Out&: Request.RestrictForCodeCompletion) &&
38 O.map(Prop: "ProximityPaths", Out&: Request.ProximityPaths) &&
39 O.map(Prop: "PreferredTypes", Out&: 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 {.K: "Query", .V: Request.Query},
48 {.K: "Scopes", .V: Request.Scopes},
49 {.K: "AnyScope", .V: Request.AnyScope},
50 {.K: "Limit", .V: Request.Limit},
51 {.K: "RestrictForCodeCompletion", .V: Request.RestrictForCodeCompletion},
52 {.K: "ProximityPaths", .V: Request.ProximityPaths},
53 {.K: "PreferredTypes", .V: Request.PreferredTypes},
54 };
55}
56
57bool SwapIndex::fuzzyFind(const FuzzyFindRequest &R,
58 llvm::function_ref<void(const Symbol &)> CB) const {
59 return snapshot()->fuzzyFind(Req: R, Callback: CB);
60}
61void SwapIndex::lookup(const LookupRequest &R,
62 llvm::function_ref<void(const Symbol &)> CB) const {
63 return snapshot()->lookup(Req: R, Callback: CB);
64}
65bool SwapIndex::refs(const RefsRequest &R,
66 llvm::function_ref<void(const Ref &)> CB) const {
67 return snapshot()->refs(Req: R, Callback: CB);
68}
69void SwapIndex::relations(
70 const RelationsRequest &R,
71 llvm::function_ref<void(const SymbolID &, const Symbol &)> CB) const {
72 return snapshot()->relations(Req: R, Callback: CB);
73}
74
75llvm::unique_function<IndexContents(llvm::StringRef) const>
76SwapIndex::indexedFiles() 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
86size_t SwapIndex::estimateMemoryUsage() const {
87 return snapshot()->estimateMemoryUsage();
88}
89
90} // namespace clangd
91} // namespace clang
92

source code of clang-tools-extra/clangd/index/Index.cpp