[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang-tools-extra / clangd / index / Index.cpp
blob7a0c23287db225d1a1a68a2ef33f0c508c4a0232
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 //===----------------------------------------------------------------------===//
9 #include "Index.h"
10 #include "llvm/ADT/StringRef.h"
11 #include <limits>
13 namespace clang {
14 namespace clangd {
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;
20 std::lock_guard<std::mutex> Lock(Mutex);
21 Pin = std::move(this->Index);
22 this->Index = std::move(Index);
25 std::shared_ptr<SymbolIndex> SwapIndex::snapshot() const {
26 std::lock_guard<std::mutex> Lock(Mutex);
27 return Index;
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;
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},
57 bool SwapIndex::fuzzyFind(const FuzzyFindRequest &R,
58 llvm::function_ref<void(const Symbol &)> CB) const {
59 return snapshot()->fuzzyFind(R, CB);
61 void SwapIndex::lookup(const LookupRequest &R,
62 llvm::function_ref<void(const Symbol &)> CB) const {
63 return snapshot()->lookup(R, CB);
65 bool SwapIndex::refs(const RefsRequest &R,
66 llvm::function_ref<void(const Ref &)> CB) const {
67 return snapshot()->refs(R, CB);
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);
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);
86 size_t SwapIndex::estimateMemoryUsage() const {
87 return snapshot()->estimateMemoryUsage();
90 } // namespace clangd
91 } // namespace clang