Skip to content

Commit d380984

Browse files
Implement remote index support for the new containedRefs request
1 parent 247b790 commit d380984

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed

clang-tools-extra/clangd/index/remote/Client.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ class IndexClient : public clangd::SymbolIndex {
146146
return streamRPC(Request, &remote::v1::SymbolIndex::Stub::Refs, Callback);
147147
}
148148

149+
bool containedRefs(const clangd::ContainedRefsRequest &Request,
150+
llvm::function_ref<void(const ContainedRefsResult &)>
151+
Callback) const override {
152+
return streamRPC(Request, &remote::v1::SymbolIndex::Stub::ContainedRefs,
153+
Callback);
154+
}
155+
149156
void
150157
relations(const clangd::RelationsRequest &Request,
151158
llvm::function_ref<void(const SymbolID &, const clangd::Symbol &)>

clang-tools-extra/clangd/index/remote/Index.proto

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,21 @@ message Relation {
131131
optional string subject_id = 1;
132132
optional Symbol object = 2;
133133
}
134+
135+
message ContainedRefsRequest {
136+
required string id = 1;
137+
optional uint32 limit = 2;
138+
}
139+
140+
message ContainedRefsReply {
141+
oneof kind {
142+
ContainedRef stream_result = 1;
143+
FinalResult final_result = 2;
144+
}
145+
}
146+
147+
message ContainedRef {
148+
required SymbolLocation location = 1;
149+
required uint32 kind = 2;
150+
required string symbol = 3;
151+
}

clang-tools-extra/clangd/index/remote/Service.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ service SymbolIndex {
2121

2222
rpc Refs(RefsRequest) returns (stream RefsReply) {}
2323

24+
rpc ContainedRefs(ContainedRefsRequest) returns (stream ContainedRefsReply) {}
25+
2426
rpc Relations(RelationsRequest) returns (stream RelationsReply) {}
2527
}

clang-tools-extra/clangd/index/remote/marshalling/Marshalling.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ Marshaller::fromProtobuf(const RefsRequest *Message) {
126126
return Req;
127127
}
128128

129+
llvm::Expected<clangd::ContainedRefsRequest>
130+
Marshaller::fromProtobuf(const ContainedRefsRequest *Message) {
131+
clangd::ContainedRefsRequest Req;
132+
auto ID = SymbolID::fromStr(Message->id());
133+
if (!ID)
134+
return ID.takeError();
135+
Req.ID = *ID;
136+
if (Message->has_limit())
137+
Req.Limit = Message->limit();
138+
return Req;
139+
}
140+
129141
llvm::Expected<clangd::RelationsRequest>
130142
Marshaller::fromProtobuf(const RelationsRequest *Message) {
131143
clangd::RelationsRequest Req;
@@ -192,6 +204,21 @@ llvm::Expected<clangd::Ref> Marshaller::fromProtobuf(const Ref &Message) {
192204
return Result;
193205
}
194206

207+
llvm::Expected<clangd::ContainedRefsResult>
208+
Marshaller::fromProtobuf(const ContainedRef &Message) {
209+
clangd::ContainedRefsResult Result;
210+
auto Location = fromProtobuf(Message.location());
211+
if (!Location)
212+
return Location.takeError();
213+
Result.Location = *Location;
214+
Result.Kind = static_cast<RefKind>(Message.kind());
215+
auto Symbol = SymbolID::fromStr(Message.symbol());
216+
if (!Symbol)
217+
return Symbol.takeError();
218+
Result.Symbol = *Symbol;
219+
return Result;
220+
}
221+
195222
llvm::Expected<std::pair<clangd::SymbolID, clangd::Symbol>>
196223
Marshaller::fromProtobuf(const Relation &Message) {
197224
auto SubjectID = SymbolID::fromStr(Message.subject_id());
@@ -244,6 +271,15 @@ RefsRequest Marshaller::toProtobuf(const clangd::RefsRequest &From) {
244271
return RPCRequest;
245272
}
246273

274+
ContainedRefsRequest
275+
Marshaller::toProtobuf(const clangd::ContainedRefsRequest &From) {
276+
ContainedRefsRequest RPCRequest;
277+
RPCRequest.set_id(From.ID.str());
278+
if (From.Limit)
279+
RPCRequest.set_limit(*From.Limit);
280+
return RPCRequest;
281+
}
282+
247283
RelationsRequest Marshaller::toProtobuf(const clangd::RelationsRequest &From) {
248284
RelationsRequest RPCRequest;
249285
for (const auto &ID : From.Subjects)
@@ -299,6 +335,18 @@ llvm::Expected<Ref> Marshaller::toProtobuf(const clangd::Ref &From) {
299335
return Result;
300336
}
301337

338+
llvm::Expected<ContainedRef>
339+
Marshaller::toProtobuf(const clangd::ContainedRefsResult &From) {
340+
ContainedRef Result;
341+
auto Location = toProtobuf(From.Location);
342+
if (!Location)
343+
return Location.takeError();
344+
*Result.mutable_location() = *Location;
345+
Result.set_kind(static_cast<uint32_t>(From.Kind));
346+
*Result.mutable_symbol() = From.Symbol.str();
347+
return Result;
348+
}
349+
302350
llvm::Expected<Relation> Marshaller::toProtobuf(const clangd::SymbolID &Subject,
303351
const clangd::Symbol &Object) {
304352
Relation Result;

clang-tools-extra/clangd/index/remote/marshalling/Marshalling.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class Marshaller {
4040

4141
llvm::Expected<clangd::Symbol> fromProtobuf(const Symbol &Message);
4242
llvm::Expected<clangd::Ref> fromProtobuf(const Ref &Message);
43+
llvm::Expected<clangd::ContainedRefsResult>
44+
fromProtobuf(const ContainedRef &Message);
4345
llvm::Expected<std::pair<clangd::SymbolID, clangd::Symbol>>
4446
fromProtobuf(const Relation &Message);
4547

@@ -48,6 +50,8 @@ class Marshaller {
4850
llvm::Expected<clangd::FuzzyFindRequest>
4951
fromProtobuf(const FuzzyFindRequest *Message);
5052
llvm::Expected<clangd::RefsRequest> fromProtobuf(const RefsRequest *Message);
53+
llvm::Expected<clangd::ContainedRefsRequest>
54+
fromProtobuf(const ContainedRefsRequest *Message);
5155
llvm::Expected<clangd::RelationsRequest>
5256
fromProtobuf(const RelationsRequest *Message);
5357

@@ -58,10 +62,13 @@ class Marshaller {
5862
LookupRequest toProtobuf(const clangd::LookupRequest &From);
5963
FuzzyFindRequest toProtobuf(const clangd::FuzzyFindRequest &From);
6064
RefsRequest toProtobuf(const clangd::RefsRequest &From);
65+
ContainedRefsRequest toProtobuf(const clangd::ContainedRefsRequest &From);
6166
RelationsRequest toProtobuf(const clangd::RelationsRequest &From);
6267

6368
llvm::Expected<Symbol> toProtobuf(const clangd::Symbol &From);
6469
llvm::Expected<Ref> toProtobuf(const clangd::Ref &From);
70+
llvm::Expected<ContainedRef>
71+
toProtobuf(const clangd::ContainedRefsResult &From);
6572
llvm::Expected<Relation> toProtobuf(const clangd::SymbolID &Subject,
6673
const clangd::Symbol &Object);
6774

clang-tools-extra/clangd/index/remote/server/Server.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,53 @@ class RemoteIndexServer final : public v1::SymbolIndex::Service {
258258
return grpc::Status::OK;
259259
}
260260

261+
grpc::Status
262+
ContainedRefs(grpc::ServerContext *Context,
263+
const ContainedRefsRequest *Request,
264+
grpc::ServerWriter<ContainedRefsReply> *Reply) override {
265+
auto StartTime = stopwatch::now();
266+
WithContextValue WithRequestContext(CurrentRequest, Context);
267+
logRequest(*Request);
268+
trace::Span Tracer("ContainedRefsRequest");
269+
auto Req = ProtobufMarshaller->fromProtobuf(Request);
270+
if (!Req) {
271+
elog("Can not parse ContainedRefsRequest from protobuf: {0}",
272+
Req.takeError());
273+
return grpc::Status::CANCELLED;
274+
}
275+
if (!Req->Limit || *Req->Limit > LimitResults) {
276+
log("[public] Limiting result size for ContainedRefs request from {0} to "
277+
"{1}.",
278+
Req->Limit, LimitResults);
279+
Req->Limit = LimitResults;
280+
}
281+
unsigned Sent = 0;
282+
unsigned FailedToSend = 0;
283+
bool HasMore =
284+
Index.containedRefs(*Req, [&](const clangd::ContainedRefsResult &Item) {
285+
auto SerializedItem = ProtobufMarshaller->toProtobuf(Item);
286+
if (!SerializedItem) {
287+
elog("Unable to convert ContainedRefsResult to protobuf: {0}",
288+
SerializedItem.takeError());
289+
++FailedToSend;
290+
return;
291+
}
292+
ContainedRefsReply NextMessage;
293+
*NextMessage.mutable_stream_result() = *SerializedItem;
294+
logResponse(NextMessage);
295+
Reply->Write(NextMessage);
296+
++Sent;
297+
});
298+
ContainedRefsReply LastMessage;
299+
LastMessage.mutable_final_result()->set_has_more(HasMore);
300+
logResponse(LastMessage);
301+
Reply->Write(LastMessage);
302+
SPAN_ATTACH(Tracer, "Sent", Sent);
303+
SPAN_ATTACH(Tracer, "Failed to send", FailedToSend);
304+
logRequestSummary("v1/ContainedRefs", Sent, StartTime);
305+
return grpc::Status::OK;
306+
}
307+
261308
grpc::Status Relations(grpc::ServerContext *Context,
262309
const RelationsRequest *Request,
263310
grpc::ServerWriter<RelationsReply> *Reply) override {

0 commit comments

Comments
 (0)