103 lines · cpp
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-exception6//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("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 45llvm::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 57bool SwapIndex::fuzzyFind(const FuzzyFindRequest &R,58 llvm::function_ref<void(const Symbol &)> CB) const {59 return snapshot()->fuzzyFind(R, CB);60}61void SwapIndex::lookup(const LookupRequest &R,62 llvm::function_ref<void(const Symbol &)> CB) const {63 return snapshot()->lookup(R, CB);64}65bool SwapIndex::refs(const RefsRequest &R,66 llvm::function_ref<void(const Ref &)> CB) const {67 return snapshot()->refs(R, CB);68}69bool SwapIndex::containedRefs(70 const ContainedRefsRequest &R,71 llvm::function_ref<void(const ContainedRefsResult &)> CB) const {72 return snapshot()->containedRefs(R, CB);73}74void 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 80void SwapIndex::reverseRelations(81 const RelationsRequest &R,82 llvm::function_ref<void(const SymbolID &, const Symbol &)> CB) const {83 return snapshot()->reverseRelations(R, CB);84}85 86llvm::unique_function<IndexContents(llvm::StringRef) const>87SwapIndex::indexedFiles() const {88 // The index snapshot should outlive this method return value.89 auto SnapShot = snapshot();90 auto IndexedFiles = SnapShot->indexedFiles();91 return [KeepAlive{std::move(SnapShot)},92 IndexContainsFile{std::move(IndexedFiles)}](llvm::StringRef File) {93 return IndexContainsFile(File);94 };95}96 97size_t SwapIndex::estimateMemoryUsage() const {98 return snapshot()->estimateMemoryUsage();99}100 101} // namespace clangd102} // namespace clang103