Skip to content

Commit 81e5f29

Browse files
committed
[clangd] Give the server information about client's remote index protocol version
And also introduce Protobuf package versioning, it will help to deal with breaking changes. Inroducing package version itself is a breaking change, clients and servers need to be updated. Reviewed By: sammccall Differential Revision: https://reviews.llvm.org/D89862
1 parent da27beb commit 81e5f29

File tree

5 files changed

+78
-68
lines changed

5 files changed

+78
-68
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "marshalling/Marshalling.h"
1616
#include "support/Logger.h"
1717
#include "support/Trace.h"
18+
#include "clang/Basic/Version.h"
1819
#include "llvm/ADT/StringRef.h"
1920
#include "llvm/Support/Error.h"
2021

@@ -28,7 +29,8 @@ namespace {
2829
class IndexClient : public clangd::SymbolIndex {
2930
template <typename RequestT, typename ReplyT>
3031
using StreamingCall = std::unique_ptr<grpc::ClientReader<ReplyT>> (
31-
remote::SymbolIndex::Stub::*)(grpc::ClientContext *, const RequestT &);
32+
remote::v1::SymbolIndex::Stub::*)(grpc::ClientContext *,
33+
const RequestT &);
3234

3335
template <typename RequestT, typename ReplyT, typename ClangdRequestT,
3436
typename CallbackT>
@@ -40,6 +42,7 @@ class IndexClient : public clangd::SymbolIndex {
4042
const auto RPCRequest = ProtobufMarshaller->toProtobuf(Request);
4143
SPAN_ATTACH(Tracer, "Request", RPCRequest.DebugString());
4244
grpc::ClientContext Context;
45+
Context.AddMetadata("version", clang::getClangToolFullVersion("clangd"));
4346
std::chrono::system_clock::time_point Deadline =
4447
std::chrono::system_clock::now() + DeadlineWaitingTime;
4548
Context.set_deadline(Deadline);
@@ -73,7 +76,7 @@ class IndexClient : public clangd::SymbolIndex {
7376
IndexClient(
7477
std::shared_ptr<grpc::Channel> Channel, llvm::StringRef ProjectRoot,
7578
std::chrono::milliseconds DeadlineTime = std::chrono::milliseconds(1000))
76-
: Stub(remote::SymbolIndex::NewStub(Channel)),
79+
: Stub(remote::v1::SymbolIndex::NewStub(Channel)),
7780
ProtobufMarshaller(new Marshaller(/*RemoteIndexRoot=*/"",
7881
/*LocalIndexRoot=*/ProjectRoot)),
7982
DeadlineWaitingTime(DeadlineTime) {
@@ -82,25 +85,26 @@ class IndexClient : public clangd::SymbolIndex {
8285

8386
void lookup(const clangd::LookupRequest &Request,
8487
llvm::function_ref<void(const clangd::Symbol &)> Callback) const {
85-
streamRPC(Request, &remote::SymbolIndex::Stub::Lookup, Callback);
88+
streamRPC(Request, &remote::v1::SymbolIndex::Stub::Lookup, Callback);
8689
}
8790

8891
bool
8992
fuzzyFind(const clangd::FuzzyFindRequest &Request,
9093
llvm::function_ref<void(const clangd::Symbol &)> Callback) const {
91-
return streamRPC(Request, &remote::SymbolIndex::Stub::FuzzyFind, Callback);
94+
return streamRPC(Request, &remote::v1::SymbolIndex::Stub::FuzzyFind,
95+
Callback);
9296
}
9397

9498
bool refs(const clangd::RefsRequest &Request,
9599
llvm::function_ref<void(const clangd::Ref &)> Callback) const {
96-
return streamRPC(Request, &remote::SymbolIndex::Stub::Refs, Callback);
100+
return streamRPC(Request, &remote::v1::SymbolIndex::Stub::Refs, Callback);
97101
}
98102

99103
void
100104
relations(const clangd::RelationsRequest &Request,
101105
llvm::function_ref<void(const SymbolID &, const clangd::Symbol &)>
102106
Callback) const {
103-
streamRPC(Request, &remote::SymbolIndex::Stub::Relations,
107+
streamRPC(Request, &remote::v1::SymbolIndex::Stub::Relations,
104108
// Unpack protobuf Relation.
105109
[&](std::pair<SymbolID, clangd::Symbol> SubjectAndObject) {
106110
Callback(SubjectAndObject.first, SubjectAndObject.second);
@@ -112,7 +116,7 @@ class IndexClient : public clangd::SymbolIndex {
112116
size_t estimateMemoryUsage() const { return 0; }
113117

114118
private:
115-
std::unique_ptr<remote::SymbolIndex::Stub> Stub;
119+
std::unique_ptr<remote::v1::SymbolIndex::Stub> Stub;
116120
std::unique_ptr<Marshaller> ProtobufMarshaller;
117121
// Each request will be terminated if it takes too long.
118122
std::chrono::milliseconds DeadlineWaitingTime;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
syntax = "proto3";
1010

11-
package clang.clangd.remote;
11+
package clang.clangd.remote.v1;
1212

1313
// Semantics of SymbolIndex match clangd::SymbolIndex with all required
1414
// structures corresponding to their clangd::* counterparts.

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

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Marshaller::Marshaller(llvm::StringRef RemoteIndexRoot,
7272
}
7373

7474
llvm::Expected<clangd::LookupRequest>
75-
Marshaller::fromProtobuf(const LookupRequest *Message) {
75+
Marshaller::fromProtobuf(const v1::LookupRequest *Message) {
7676
clangd::LookupRequest Req;
7777
auto IDs = getIDs(Message->ids());
7878
if (!IDs)
@@ -82,7 +82,7 @@ Marshaller::fromProtobuf(const LookupRequest *Message) {
8282
}
8383

8484
llvm::Expected<clangd::FuzzyFindRequest>
85-
Marshaller::fromProtobuf(const FuzzyFindRequest *Message) {
85+
Marshaller::fromProtobuf(const v1::FuzzyFindRequest *Message) {
8686
assert(RemoteIndexRoot);
8787
clangd::FuzzyFindRequest Result;
8888
Result.Query = Message->query();
@@ -106,7 +106,7 @@ Marshaller::fromProtobuf(const FuzzyFindRequest *Message) {
106106
}
107107

108108
llvm::Expected<clangd::RefsRequest>
109-
Marshaller::fromProtobuf(const RefsRequest *Message) {
109+
Marshaller::fromProtobuf(const v1::RefsRequest *Message) {
110110
clangd::RefsRequest Req;
111111
auto IDs = getIDs(Message->ids());
112112
if (!IDs)
@@ -119,7 +119,7 @@ Marshaller::fromProtobuf(const RefsRequest *Message) {
119119
}
120120

121121
llvm::Expected<clangd::RelationsRequest>
122-
Marshaller::fromProtobuf(const RelationsRequest *Message) {
122+
Marshaller::fromProtobuf(const v1::RelationsRequest *Message) {
123123
clangd::RelationsRequest Req;
124124
auto IDs = getIDs(Message->subjects());
125125
if (!IDs)
@@ -131,7 +131,8 @@ Marshaller::fromProtobuf(const RelationsRequest *Message) {
131131
return Req;
132132
}
133133

134-
llvm::Expected<clangd::Symbol> Marshaller::fromProtobuf(const Symbol &Message) {
134+
llvm::Expected<clangd::Symbol>
135+
Marshaller::fromProtobuf(const v1::Symbol &Message) {
135136
if (!Message.has_info() || !Message.has_canonical_declaration())
136137
return error("Missing info or declaration.");
137138
clangd::Symbol Result;
@@ -169,7 +170,7 @@ llvm::Expected<clangd::Symbol> Marshaller::fromProtobuf(const Symbol &Message) {
169170
return Result;
170171
}
171172

172-
llvm::Expected<clangd::Ref> Marshaller::fromProtobuf(const Ref &Message) {
173+
llvm::Expected<clangd::Ref> Marshaller::fromProtobuf(const v1::Ref &Message) {
173174
if (!Message.has_location())
174175
return error("Missing location.");
175176
clangd::Ref Result;
@@ -182,7 +183,7 @@ llvm::Expected<clangd::Ref> Marshaller::fromProtobuf(const Ref &Message) {
182183
}
183184

184185
llvm::Expected<std::pair<clangd::SymbolID, clangd::Symbol>>
185-
Marshaller::fromProtobuf(const Relation &Message) {
186+
Marshaller::fromProtobuf(const v1::Relation &Message) {
186187
auto SubjectID = SymbolID::fromStr(Message.subject_id());
187188
if (!SubjectID)
188189
return SubjectID.takeError();
@@ -194,16 +195,17 @@ Marshaller::fromProtobuf(const Relation &Message) {
194195
return std::make_pair(*SubjectID, *Object);
195196
}
196197

197-
LookupRequest Marshaller::toProtobuf(const clangd::LookupRequest &From) {
198-
LookupRequest RPCRequest;
198+
v1::LookupRequest Marshaller::toProtobuf(const clangd::LookupRequest &From) {
199+
v1::LookupRequest RPCRequest;
199200
for (const auto &SymbolID : From.IDs)
200201
RPCRequest.add_ids(SymbolID.str());
201202
return RPCRequest;
202203
}
203204

204-
FuzzyFindRequest Marshaller::toProtobuf(const clangd::FuzzyFindRequest &From) {
205+
v1::FuzzyFindRequest
206+
Marshaller::toProtobuf(const clangd::FuzzyFindRequest &From) {
205207
assert(LocalIndexRoot);
206-
FuzzyFindRequest RPCRequest;
208+
v1::FuzzyFindRequest RPCRequest;
207209
RPCRequest.set_query(From.Query);
208210
for (const auto &Scope : From.Scopes)
209211
RPCRequest.add_scopes(Scope);
@@ -222,8 +224,8 @@ FuzzyFindRequest Marshaller::toProtobuf(const clangd::FuzzyFindRequest &From) {
222224
return RPCRequest;
223225
}
224226

225-
RefsRequest Marshaller::toProtobuf(const clangd::RefsRequest &From) {
226-
RefsRequest RPCRequest;
227+
v1::RefsRequest Marshaller::toProtobuf(const clangd::RefsRequest &From) {
228+
v1::RefsRequest RPCRequest;
227229
for (const auto &ID : From.IDs)
228230
RPCRequest.add_ids(ID.str());
229231
RPCRequest.set_filter(static_cast<uint32_t>(From.Filter));
@@ -232,8 +234,9 @@ RefsRequest Marshaller::toProtobuf(const clangd::RefsRequest &From) {
232234
return RPCRequest;
233235
}
234236

235-
RelationsRequest Marshaller::toProtobuf(const clangd::RelationsRequest &From) {
236-
RelationsRequest RPCRequest;
237+
v1::RelationsRequest
238+
Marshaller::toProtobuf(const clangd::RelationsRequest &From) {
239+
v1::RelationsRequest RPCRequest;
237240
for (const auto &ID : From.Subjects)
238241
RPCRequest.add_subjects(ID.str());
239242
RPCRequest.set_predicate(static_cast<uint32_t>(From.Predicate));
@@ -242,8 +245,8 @@ RelationsRequest Marshaller::toProtobuf(const clangd::RelationsRequest &From) {
242245
return RPCRequest;
243246
}
244247

245-
llvm::Expected<Symbol> Marshaller::toProtobuf(const clangd::Symbol &From) {
246-
Symbol Result;
248+
llvm::Expected<v1::Symbol> Marshaller::toProtobuf(const clangd::Symbol &From) {
249+
v1::Symbol Result;
247250
Result.set_id(From.ID.str());
248251
*Result.mutable_info() = toProtobuf(From.SymInfo);
249252
Result.set_name(From.Name.str());
@@ -278,8 +281,8 @@ llvm::Expected<Symbol> Marshaller::toProtobuf(const clangd::Symbol &From) {
278281
return Result;
279282
}
280283

281-
llvm::Expected<Ref> Marshaller::toProtobuf(const clangd::Ref &From) {
282-
Ref Result;
284+
llvm::Expected<v1::Ref> Marshaller::toProtobuf(const clangd::Ref &From) {
285+
v1::Ref Result;
283286
Result.set_kind(static_cast<uint32_t>(From.Kind));
284287
auto Location = toProtobuf(From.Location);
285288
if (!Location)
@@ -288,9 +291,10 @@ llvm::Expected<Ref> Marshaller::toProtobuf(const clangd::Ref &From) {
288291
return Result;
289292
}
290293

291-
llvm::Expected<Relation> Marshaller::toProtobuf(const clangd::SymbolID &Subject,
292-
const clangd::Symbol &Object) {
293-
Relation Result;
294+
llvm::Expected<v1::Relation>
295+
Marshaller::toProtobuf(const clangd::SymbolID &Subject,
296+
const clangd::Symbol &Object) {
297+
v1::Relation Result;
294298
*Result.mutable_subject_id() = Subject.str();
295299
auto SerializedObject = toProtobuf(Object);
296300
if (!SerializedObject)
@@ -335,22 +339,23 @@ llvm::Expected<std::string> Marshaller::uriToRelativePath(llvm::StringRef URI) {
335339
}
336340

337341
clangd::SymbolLocation::Position
338-
Marshaller::fromProtobuf(const Position &Message) {
342+
Marshaller::fromProtobuf(const v1::Position &Message) {
339343
clangd::SymbolLocation::Position Result;
340344
Result.setColumn(static_cast<uint32_t>(Message.column()));
341345
Result.setLine(static_cast<uint32_t>(Message.line()));
342346
return Result;
343347
}
344348

345-
Position
349+
v1::Position
346350
Marshaller::toProtobuf(const clangd::SymbolLocation::Position &Position) {
347-
remote::Position Result;
351+
remote::v1::Position Result;
348352
Result.set_column(Position.column());
349353
Result.set_line(Position.line());
350354
return Result;
351355
}
352356

353-
clang::index::SymbolInfo Marshaller::fromProtobuf(const SymbolInfo &Message) {
357+
clang::index::SymbolInfo
358+
Marshaller::fromProtobuf(const v1::SymbolInfo &Message) {
354359
clang::index::SymbolInfo Result;
355360
Result.Kind = static_cast<clang::index::SymbolKind>(Message.kind());
356361
Result.SubKind = static_cast<clang::index::SymbolSubKind>(Message.subkind());
@@ -360,8 +365,8 @@ clang::index::SymbolInfo Marshaller::fromProtobuf(const SymbolInfo &Message) {
360365
return Result;
361366
}
362367

363-
SymbolInfo Marshaller::toProtobuf(const clang::index::SymbolInfo &Info) {
364-
SymbolInfo Result;
368+
v1::SymbolInfo Marshaller::toProtobuf(const clang::index::SymbolInfo &Info) {
369+
v1::SymbolInfo Result;
365370
Result.set_kind(static_cast<uint32_t>(Info.Kind));
366371
Result.set_subkind(static_cast<uint32_t>(Info.SubKind));
367372
Result.set_language(static_cast<uint32_t>(Info.Lang));
@@ -370,7 +375,7 @@ SymbolInfo Marshaller::toProtobuf(const clang::index::SymbolInfo &Info) {
370375
}
371376

372377
llvm::Expected<clangd::SymbolLocation>
373-
Marshaller::fromProtobuf(const SymbolLocation &Message) {
378+
Marshaller::fromProtobuf(const v1::SymbolLocation &Message) {
374379
clangd::SymbolLocation Location;
375380
auto URIString = relativePathToURI(Message.file_path());
376381
if (!URIString)
@@ -381,9 +386,9 @@ Marshaller::fromProtobuf(const SymbolLocation &Message) {
381386
return Location;
382387
}
383388

384-
llvm::Expected<SymbolLocation>
389+
llvm::Expected<v1::SymbolLocation>
385390
Marshaller::toProtobuf(const clangd::SymbolLocation &Location) {
386-
remote::SymbolLocation Result;
391+
remote::v1::SymbolLocation Result;
387392
auto RelativePath = uriToRelativePath(Location.FileURI);
388393
if (!RelativePath)
389394
return RelativePath.takeError();
@@ -393,9 +398,9 @@ Marshaller::toProtobuf(const clangd::SymbolLocation &Location) {
393398
return Result;
394399
}
395400

396-
llvm::Expected<HeaderWithReferences> Marshaller::toProtobuf(
401+
llvm::Expected<v1::HeaderWithReferences> Marshaller::toProtobuf(
397402
const clangd::Symbol::IncludeHeaderWithReferences &IncludeHeader) {
398-
HeaderWithReferences Result;
403+
v1::HeaderWithReferences Result;
399404
Result.set_references(IncludeHeader.References);
400405
const std::string Header = IncludeHeader.IncludeHeader.str();
401406
if (isLiteralInclude(Header)) {
@@ -410,7 +415,7 @@ llvm::Expected<HeaderWithReferences> Marshaller::toProtobuf(
410415
}
411416

412417
llvm::Expected<clangd::Symbol::IncludeHeaderWithReferences>
413-
Marshaller::fromProtobuf(const HeaderWithReferences &Message) {
418+
Marshaller::fromProtobuf(const v1::HeaderWithReferences &Message) {
414419
std::string Header = Message.header();
415420
if (!isLiteralInclude(Header)) {
416421
auto URIString = relativePathToURI(Header);

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

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,33 @@ class Marshaller {
3838
Marshaller() = delete;
3939
Marshaller(llvm::StringRef RemoteIndexRoot, llvm::StringRef LocalIndexRoot);
4040

41-
llvm::Expected<clangd::Symbol> fromProtobuf(const Symbol &Message);
42-
llvm::Expected<clangd::Ref> fromProtobuf(const Ref &Message);
41+
llvm::Expected<clangd::Symbol> fromProtobuf(const v1::Symbol &Message);
42+
llvm::Expected<clangd::Ref> fromProtobuf(const v1::Ref &Message);
4343
llvm::Expected<std::pair<clangd::SymbolID, clangd::Symbol>>
44-
fromProtobuf(const Relation &Message);
44+
fromProtobuf(const v1::Relation &Message);
4545

4646
llvm::Expected<clangd::LookupRequest>
47-
fromProtobuf(const LookupRequest *Message);
47+
fromProtobuf(const v1::LookupRequest *Message);
4848
llvm::Expected<clangd::FuzzyFindRequest>
49-
fromProtobuf(const FuzzyFindRequest *Message);
50-
llvm::Expected<clangd::RefsRequest> fromProtobuf(const RefsRequest *Message);
49+
fromProtobuf(const v1::FuzzyFindRequest *Message);
50+
llvm::Expected<clangd::RefsRequest>
51+
fromProtobuf(const v1::RefsRequest *Message);
5152
llvm::Expected<clangd::RelationsRequest>
52-
fromProtobuf(const RelationsRequest *Message);
53+
fromProtobuf(const v1::RelationsRequest *Message);
5354

5455
/// toProtobuf() functions serialize native clangd types and strip IndexRoot
5556
/// from the file paths specific to indexing machine. fromProtobuf() functions
5657
/// deserialize clangd types and translate relative paths into machine-native
5758
/// URIs.
58-
LookupRequest toProtobuf(const clangd::LookupRequest &From);
59-
FuzzyFindRequest toProtobuf(const clangd::FuzzyFindRequest &From);
60-
RefsRequest toProtobuf(const clangd::RefsRequest &From);
61-
RelationsRequest toProtobuf(const clangd::RelationsRequest &From);
59+
v1::LookupRequest toProtobuf(const clangd::LookupRequest &From);
60+
v1::FuzzyFindRequest toProtobuf(const clangd::FuzzyFindRequest &From);
61+
v1::RefsRequest toProtobuf(const clangd::RefsRequest &From);
62+
v1::RelationsRequest toProtobuf(const clangd::RelationsRequest &From);
6263

63-
llvm::Expected<Symbol> toProtobuf(const clangd::Symbol &From);
64-
llvm::Expected<Ref> toProtobuf(const clangd::Ref &From);
65-
llvm::Expected<Relation> toProtobuf(const clangd::SymbolID &Subject,
66-
const clangd::Symbol &Object);
64+
llvm::Expected<v1::Symbol> toProtobuf(const clangd::Symbol &From);
65+
llvm::Expected<v1::Ref> toProtobuf(const clangd::Ref &From);
66+
llvm::Expected<v1::Relation> toProtobuf(const clangd::SymbolID &Subject,
67+
const clangd::Symbol &Object);
6768

6869
/// Translates \p RelativePath into the absolute path and builds URI for the
6970
/// user machine. This translation happens on the client side with the
@@ -77,18 +78,18 @@ class Marshaller {
7778
llvm::Expected<std::string> uriToRelativePath(llvm::StringRef URI);
7879

7980
private:
80-
clangd::SymbolLocation::Position fromProtobuf(const Position &Message);
81-
Position toProtobuf(const clangd::SymbolLocation::Position &Position);
82-
clang::index::SymbolInfo fromProtobuf(const SymbolInfo &Message);
83-
SymbolInfo toProtobuf(const clang::index::SymbolInfo &Info);
81+
clangd::SymbolLocation::Position fromProtobuf(const v1::Position &Message);
82+
v1::Position toProtobuf(const clangd::SymbolLocation::Position &Position);
83+
clang::index::SymbolInfo fromProtobuf(const v1::SymbolInfo &Message);
84+
v1::SymbolInfo toProtobuf(const clang::index::SymbolInfo &Info);
8485
llvm::Expected<clangd::SymbolLocation>
85-
fromProtobuf(const SymbolLocation &Message);
86-
llvm::Expected<SymbolLocation>
86+
fromProtobuf(const v1::SymbolLocation &Message);
87+
llvm::Expected<v1::SymbolLocation>
8788
toProtobuf(const clangd::SymbolLocation &Location);
88-
llvm::Expected<HeaderWithReferences>
89+
llvm::Expected<v1::HeaderWithReferences>
8990
toProtobuf(const clangd::Symbol::IncludeHeaderWithReferences &IncludeHeader);
9091
llvm::Expected<clangd::Symbol::IncludeHeaderWithReferences>
91-
fromProtobuf(const HeaderWithReferences &Message);
92+
fromProtobuf(const v1::HeaderWithReferences &Message);
9293

9394
/// RemoteIndexRoot and LocalIndexRoot are absolute paths to the project (on
9495
/// remote and local machine respectively) and include a trailing slash. One

0 commit comments

Comments
 (0)