1 //===--- Client.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 <grpcpp/grpcpp.h> 10 11 #include "Client.h" 12 #include "Index.grpc.pb.h" 13 #include "index/Serialization.h" 14 #include "marshalling/Marshalling.h" 15 #include "support/Logger.h" 16 #include "support/Trace.h" 17 #include "llvm/Support/YAMLTraits.h" 18 19 namespace clang { 20 namespace clangd { 21 namespace remote { 22 namespace { 23 24 class IndexClient : public clangd::SymbolIndex { 25 template <typename RequestT, typename ReplyT> 26 using StreamingCall = std::unique_ptr<grpc::ClientReader<ReplyT>> ( 27 remote::SymbolIndex::Stub::*)(grpc::ClientContext *, const RequestT &); 28 29 // FIXME(kirillbobyrev): Set deadlines for requests. 30 template <typename RequestT, typename ReplyT, typename ClangdRequestT, 31 typename CallbackT> 32 bool streamRPC(ClangdRequestT Request, 33 StreamingCall<RequestT, ReplyT> RPCCall, 34 CallbackT Callback) const { 35 bool FinalResult = false; 36 trace::Span Tracer(RequestT::descriptor()->name()); 37 const auto RPCRequest = toProtobuf(Request); 38 grpc::ClientContext Context; 39 auto Reader = (Stub.get()->*RPCCall)(&Context, RPCRequest); 40 llvm::BumpPtrAllocator Arena; 41 llvm::UniqueStringSaver Strings(Arena); 42 ReplyT Reply; 43 while (Reader->Read(&Reply)) { 44 if (!Reply.has_stream_result()) { 45 FinalResult = Reply.final_result(); 46 continue; 47 } 48 auto Sym = fromProtobuf(Reply.stream_result(), &Strings); 49 if (!Sym) 50 elog("Received invalid {0}: {1}", ReplyT::descriptor()->name(), 51 Reply.stream_result().yaml_serialization()); 52 Callback(*Sym); 53 } 54 SPAN_ATTACH(Tracer, "status", Reader->Finish().ok()); 55 return FinalResult; 56 } 57 58 public: 59 IndexClient(std::shared_ptr<grpc::Channel> Channel) 60 : Stub(remote::SymbolIndex::NewStub(Channel)) {} 61 62 void lookup(const clangd::LookupRequest &Request, 63 llvm::function_ref<void(const clangd::Symbol &)> Callback) const { 64 streamRPC(Request, &remote::SymbolIndex::Stub::Lookup, Callback); 65 } 66 67 bool 68 fuzzyFind(const clangd::FuzzyFindRequest &Request, 69 llvm::function_ref<void(const clangd::Symbol &)> Callback) const { 70 return streamRPC(Request, &remote::SymbolIndex::Stub::FuzzyFind, Callback); 71 } 72 73 bool refs(const clangd::RefsRequest &Request, 74 llvm::function_ref<void(const clangd::Ref &)> Callback) const { 75 return streamRPC(Request, &remote::SymbolIndex::Stub::Refs, Callback); 76 } 77 78 // FIXME(kirillbobyrev): Implement this. 79 void 80 relations(const clangd::RelationsRequest &, 81 llvm::function_ref<void(const SymbolID &, const clangd::Symbol &)>) 82 const {} 83 84 // IndexClient does not take any space since the data is stored on the server. 85 size_t estimateMemoryUsage() const { return 0; } 86 87 private: 88 std::unique_ptr<remote::SymbolIndex::Stub> Stub; 89 }; 90 91 } // namespace 92 93 std::unique_ptr<clangd::SymbolIndex> getClient(llvm::StringRef Address) { 94 const auto Channel = 95 grpc::CreateChannel(Address.str(), grpc::InsecureChannelCredentials()); 96 Channel->GetState(true); 97 return std::unique_ptr<clangd::SymbolIndex>(new IndexClient(Channel)); 98 } 99 100 } // namespace remote 101 } // namespace clangd 102 } // namespace clang 103