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 "Logger.h"
11 #include "llvm/ADT/StringExtras.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/Support/Error.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include <limits>
16
17 namespace clang {
18 namespace clangd {
19
reset(std::unique_ptr<SymbolIndex> Index)20 void SwapIndex::reset(std::unique_ptr<SymbolIndex> Index) {
21 // Keep the old index alive, so we don't destroy it under lock (may be slow).
22 std::shared_ptr<SymbolIndex> Pin;
23 {
24 std::lock_guard<std::mutex> Lock(Mutex);
25 Pin = std::move(this->Index);
26 this->Index = std::move(Index);
27 }
28 }
snapshot() const29 std::shared_ptr<SymbolIndex> SwapIndex::snapshot() const {
30 std::lock_guard<std::mutex> Lock(Mutex);
31 return Index;
32 }
33
fromJSON(const llvm::json::Value & Parameters,FuzzyFindRequest & Request)34 bool fromJSON(const llvm::json::Value &Parameters, FuzzyFindRequest &Request) {
35 llvm::json::ObjectMapper O(Parameters);
36 int64_t Limit;
37 bool OK =
38 O && O.map("Query", Request.Query) && O.map("Scopes", Request.Scopes) &&
39 O.map("AnyScope", Request.AnyScope) && O.map("Limit", Limit) &&
40 O.map("RestrictForCodeCompletion", Request.RestrictForCodeCompletion) &&
41 O.map("ProximityPaths", Request.ProximityPaths) &&
42 O.map("PreferredTypes", Request.PreferredTypes);
43 if (OK && Limit <= std::numeric_limits<uint32_t>::max())
44 Request.Limit = Limit;
45 return OK;
46 }
47
toJSON(const FuzzyFindRequest & Request)48 llvm::json::Value toJSON(const FuzzyFindRequest &Request) {
49 return llvm::json::Object{
50 {"Query", Request.Query},
51 {"Scopes", llvm::json::Array{Request.Scopes}},
52 {"AnyScope", Request.AnyScope},
53 {"Limit", Request.Limit},
54 {"RestrictForCodeCompletion", Request.RestrictForCodeCompletion},
55 {"ProximityPaths", llvm::json::Array{Request.ProximityPaths}},
56 {"PreferredTypes", llvm::json::Array{Request.PreferredTypes}},
57 };
58 }
59
fuzzyFind(const FuzzyFindRequest & R,llvm::function_ref<void (const Symbol &)> CB) const60 bool SwapIndex::fuzzyFind(const FuzzyFindRequest &R,
61 llvm::function_ref<void(const Symbol &)> CB) const {
62 return snapshot()->fuzzyFind(R, CB);
63 }
lookup(const LookupRequest & R,llvm::function_ref<void (const Symbol &)> CB) const64 void SwapIndex::lookup(const LookupRequest &R,
65 llvm::function_ref<void(const Symbol &)> CB) const {
66 return snapshot()->lookup(R, CB);
67 }
refs(const RefsRequest & R,llvm::function_ref<void (const Ref &)> CB) const68 bool SwapIndex::refs(const RefsRequest &R,
69 llvm::function_ref<void(const Ref &)> CB) const {
70 return snapshot()->refs(R, CB);
71 }
relations(const RelationsRequest & R,llvm::function_ref<void (const SymbolID &,const Symbol &)> CB) const72 void SwapIndex::relations(
73 const RelationsRequest &R,
74 llvm::function_ref<void(const SymbolID &, const Symbol &)> CB) const {
75 return snapshot()->relations(R, CB);
76 }
77
estimateMemoryUsage() const78 size_t SwapIndex::estimateMemoryUsage() const {
79 return snapshot()->estimateMemoryUsage();
80 }
81
82 } // namespace clangd
83 } // namespace clang
84