| 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 |
|
| 13 |
namespace clang { |
| 14 |
namespace clangd { |
| 15 |
|
| 16 |
void 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 |
} |
| 25 |
std::shared_ptr<SymbolIndex> SwapIndex::snapshot() const { |
| 26 |
std::lock_guard<std::mutex> Lock(Mutex); |
| 27 |
return Index; |
| 28 |
} |
| 29 |
|
| 30 |
bool 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 |
|
| 45 |
llvm::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 |
|
| 57 |
bool SwapIndex::fuzzyFind(const FuzzyFindRequest &R, |
| 58 |
llvm::function_ref<void(const Symbol &)> CB) const { |
| 59 |
return snapshot()->fuzzyFind(R, CB); |
| 60 |
} |
| 61 |
void SwapIndex::lookup(const LookupRequest &R, |
| 62 |
llvm::function_ref<void(const Symbol &)> CB) const { |
| 63 |
return snapshot()->lookup(R, CB); |
| 64 |
} |
| 65 |
bool SwapIndex::refs(const RefsRequest &R, |
| 66 |
llvm::function_ref<void(const Ref &)> CB) const { |
| 67 |
return snapshot()->refs(R, CB); |
| 68 |
} |
| 69 |
void SwapIndex::relations( |
| 70 |
const RelationsRequest &R, |
| 71 |
llvm::function_ref<void(const SymbolID &, const Symbol &)> CB) const { |
| 72 |
return snapshot()->relations(R, CB); |
| 73 |
} |
| 74 |
|
| 75 |
llvm::unique_function<IndexContents(llvm::StringRef) const> |
| 76 |
SwapIndex::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 |
|
| 86 |
size_t SwapIndex::estimateMemoryUsage() const { |
| 87 |
return snapshot()->estimateMemoryUsage(); |
| 88 |
} |
| 89 |
|
| 90 |
} // namespace clangd |
| 91 |
} // namespace clang |