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 bool SwapIndex::containedRefs( 70 const ContainedRefsRequest &R, 71 llvm::function_ref<void(const ContainedRefsResult &)> CB) const { 72 return snapshot()->containedRefs(R, CB); 73 } 74 void SwapIndex::relations( 75 const RelationsRequest &R, 76 llvm::function_ref<void(const SymbolID &, const Symbol &)> CB) const { 77 return snapshot()->relations(R, CB); 78 } 79 80 llvm::unique_function<IndexContents(llvm::StringRef) const> 81 SwapIndex::indexedFiles() const { 82 // The index snapshot should outlive this method return value. 83 auto SnapShot = snapshot(); 84 auto IndexedFiles = SnapShot->indexedFiles(); 85 return [KeepAlive{std::move(SnapShot)}, 86 IndexContainsFile{std::move(IndexedFiles)}](llvm::StringRef File) { 87 return IndexContainsFile(File); 88 }; 89 } 90 91 size_t SwapIndex::estimateMemoryUsage() const { 92 return snapshot()->estimateMemoryUsage(); 93 } 94 95 } // namespace clangd 96 } // namespace clang 97