1 //===--- Server.cpp - gRPC-based Remote Index Server  ---------------------===//
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/Index.h"
10 #include "index/Serialization.h"
11 #include "index/remote/marshalling/Marshalling.h"
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/LineEditor/LineEditor.h"
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/Path.h"
18 #include "llvm/Support/Signals.h"
19 
20 #include <grpcpp/grpcpp.h>
21 #include <grpcpp/health_check_service_interface.h>
22 
23 #include "Index.grpc.pb.h"
24 
25 namespace clang {
26 namespace clangd {
27 namespace remote {
28 namespace {
29 
30 static const std::string Overview = R"(
31 This is an experimental remote index implementation. The server opens Dex and
32 awaits gRPC lookup requests from the client.
33 )";
34 
35 llvm::cl::opt<std::string> IndexPath(llvm::cl::desc("<INDEX FILE>"),
36                                      llvm::cl::Positional, llvm::cl::Required);
37 
38 llvm::cl::opt<std::string> ServerAddress(
39     "server-address", llvm::cl::init("0.0.0.0:50051"),
40     llvm::cl::desc("Address of the invoked server. Defaults to 0.0.0.0:50051"));
41 
42 std::unique_ptr<clangd::SymbolIndex> openIndex(llvm::StringRef Index) {
43   return loadIndex(Index, /*UseIndex=*/true);
44 }
45 
46 class RemoteIndexServer final : public SymbolIndex::Service {
47 public:
48   RemoteIndexServer(std::unique_ptr<clangd::SymbolIndex> Index)
49       : Index(std::move(Index)) {}
50 
51 private:
52   grpc::Status Lookup(grpc::ServerContext *Context,
53                       const LookupRequest *Request,
54                       grpc::ServerWriter<LookupReply> *Reply) override {
55     clangd::LookupRequest Req;
56     for (const auto &ID : Request->ids()) {
57       auto SID = SymbolID::fromStr(StringRef(ID));
58       if (!SID)
59         return grpc::Status::CANCELLED;
60       Req.IDs.insert(*SID);
61     }
62     Index->lookup(Req, [&](const clangd::Symbol &Sym) {
63       LookupReply NextMessage;
64       *NextMessage.mutable_stream_result() = toProtobuf(Sym);
65       Reply->Write(NextMessage);
66     });
67     LookupReply LastMessage;
68     LastMessage.set_final_result(true);
69     Reply->Write(LastMessage);
70     return grpc::Status::OK;
71   }
72 
73   grpc::Status FuzzyFind(grpc::ServerContext *Context,
74                          const FuzzyFindRequest *Request,
75                          grpc::ServerWriter<FuzzyFindReply> *Reply) override {
76     const auto Req = fromProtobuf(Request);
77     bool HasMore = Index->fuzzyFind(Req, [&](const clangd::Symbol &Sym) {
78       FuzzyFindReply NextMessage;
79       *NextMessage.mutable_stream_result() = toProtobuf(Sym);
80       Reply->Write(NextMessage);
81     });
82     FuzzyFindReply LastMessage;
83     LastMessage.set_final_result(HasMore);
84     Reply->Write(LastMessage);
85     return grpc::Status::OK;
86   }
87 
88   grpc::Status Refs(grpc::ServerContext *Context, const RefsRequest *Request,
89                     grpc::ServerWriter<RefsReply> *Reply) override {
90     clangd::RefsRequest Req;
91     for (const auto &ID : Request->ids()) {
92       auto SID = SymbolID::fromStr(StringRef(ID));
93       if (!SID)
94         return grpc::Status::CANCELLED;
95       Req.IDs.insert(*SID);
96     }
97     bool HasMore = Index->refs(Req, [&](const clangd::Ref &Reference) {
98       RefsReply NextMessage;
99       *NextMessage.mutable_stream_result() = toProtobuf(Reference);
100       Reply->Write(NextMessage);
101     });
102     RefsReply LastMessage;
103     LastMessage.set_final_result(HasMore);
104     Reply->Write(LastMessage);
105     return grpc::Status::OK;
106   }
107 
108   std::unique_ptr<clangd::SymbolIndex> Index;
109 };
110 
111 void runServer(std::unique_ptr<clangd::SymbolIndex> Index,
112                const std::string &ServerAddress) {
113   RemoteIndexServer Service(std::move(Index));
114 
115   grpc::EnableDefaultHealthCheckService(true);
116   grpc::ServerBuilder Builder;
117   Builder.AddListeningPort(ServerAddress, grpc::InsecureServerCredentials());
118   Builder.RegisterService(&Service);
119   std::unique_ptr<grpc::Server> Server(Builder.BuildAndStart());
120   llvm::outs() << "Server listening on " << ServerAddress << '\n';
121 
122   Server->Wait();
123 }
124 
125 } // namespace
126 } // namespace remote
127 } // namespace clangd
128 } // namespace clang
129 
130 int main(int argc, char *argv[]) {
131   using namespace clang::clangd::remote;
132   llvm::cl::ParseCommandLineOptions(argc, argv, Overview);
133   llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
134 
135   std::unique_ptr<clang::clangd::SymbolIndex> Index = openIndex(IndexPath);
136 
137   if (!Index) {
138     llvm::outs() << "Failed to open the index.\n";
139     return -1;
140   }
141 
142   runServer(std::move(Index), ServerAddress);
143 }
144