diff --git a/clang-tools-extra/clangd/index/remote/Client.cpp b/clang-tools-extra/clangd/index/remote/Client.cpp --- a/clang-tools-extra/clangd/index/remote/Client.cpp +++ b/clang-tools-extra/clangd/index/remote/Client.cpp @@ -15,6 +15,7 @@ #include "marshalling/Marshalling.h" #include "support/Logger.h" #include "support/Trace.h" +#include "clang/Basic/Version.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Error.h" @@ -28,7 +29,8 @@ class IndexClient : public clangd::SymbolIndex { template using StreamingCall = std::unique_ptr> ( - remote::SymbolIndex::Stub::*)(grpc::ClientContext *, const RequestT &); + remote::v1::SymbolIndex::Stub::*)(grpc::ClientContext *, + const RequestT &); template @@ -40,6 +42,7 @@ const auto RPCRequest = ProtobufMarshaller->toProtobuf(Request); SPAN_ATTACH(Tracer, "Request", RPCRequest.DebugString()); grpc::ClientContext Context; + Context.AddMetadata("version", clang::getClangToolFullVersion("clangd")); std::chrono::system_clock::time_point Deadline = std::chrono::system_clock::now() + DeadlineWaitingTime; Context.set_deadline(Deadline); @@ -73,7 +76,7 @@ IndexClient( std::shared_ptr Channel, llvm::StringRef ProjectRoot, std::chrono::milliseconds DeadlineTime = std::chrono::milliseconds(1000)) - : Stub(remote::SymbolIndex::NewStub(Channel)), + : Stub(remote::v1::SymbolIndex::NewStub(Channel)), ProtobufMarshaller(new Marshaller(/*RemoteIndexRoot=*/"", /*LocalIndexRoot=*/ProjectRoot)), DeadlineWaitingTime(DeadlineTime) { @@ -82,25 +85,26 @@ void lookup(const clangd::LookupRequest &Request, llvm::function_ref Callback) const { - streamRPC(Request, &remote::SymbolIndex::Stub::Lookup, Callback); + streamRPC(Request, &remote::v1::SymbolIndex::Stub::Lookup, Callback); } bool fuzzyFind(const clangd::FuzzyFindRequest &Request, llvm::function_ref Callback) const { - return streamRPC(Request, &remote::SymbolIndex::Stub::FuzzyFind, Callback); + return streamRPC(Request, &remote::v1::SymbolIndex::Stub::FuzzyFind, + Callback); } bool refs(const clangd::RefsRequest &Request, llvm::function_ref Callback) const { - return streamRPC(Request, &remote::SymbolIndex::Stub::Refs, Callback); + return streamRPC(Request, &remote::v1::SymbolIndex::Stub::Refs, Callback); } void relations(const clangd::RelationsRequest &Request, llvm::function_ref Callback) const { - streamRPC(Request, &remote::SymbolIndex::Stub::Relations, + streamRPC(Request, &remote::v1::SymbolIndex::Stub::Relations, // Unpack protobuf Relation. [&](std::pair SubjectAndObject) { Callback(SubjectAndObject.first, SubjectAndObject.second); @@ -112,7 +116,7 @@ size_t estimateMemoryUsage() const { return 0; } private: - std::unique_ptr Stub; + std::unique_ptr Stub; std::unique_ptr ProtobufMarshaller; // Each request will be terminated if it takes too long. std::chrono::milliseconds DeadlineWaitingTime; diff --git a/clang-tools-extra/clangd/index/remote/Index.proto b/clang-tools-extra/clangd/index/remote/Index.proto --- a/clang-tools-extra/clangd/index/remote/Index.proto +++ b/clang-tools-extra/clangd/index/remote/Index.proto @@ -8,7 +8,7 @@ syntax = "proto3"; -package clang.clangd.remote; +package clang.clangd.remote.v1; // Semantics of SymbolIndex match clangd::SymbolIndex with all required // structures corresponding to their clangd::* counterparts. diff --git a/clang-tools-extra/clangd/index/remote/marshalling/Marshalling.h b/clang-tools-extra/clangd/index/remote/marshalling/Marshalling.h --- a/clang-tools-extra/clangd/index/remote/marshalling/Marshalling.h +++ b/clang-tools-extra/clangd/index/remote/marshalling/Marshalling.h @@ -38,32 +38,33 @@ Marshaller() = delete; Marshaller(llvm::StringRef RemoteIndexRoot, llvm::StringRef LocalIndexRoot); - llvm::Expected fromProtobuf(const Symbol &Message); - llvm::Expected fromProtobuf(const Ref &Message); + llvm::Expected fromProtobuf(const v1::Symbol &Message); + llvm::Expected fromProtobuf(const v1::Ref &Message); llvm::Expected> - fromProtobuf(const Relation &Message); + fromProtobuf(const v1::Relation &Message); llvm::Expected - fromProtobuf(const LookupRequest *Message); + fromProtobuf(const v1::LookupRequest *Message); llvm::Expected - fromProtobuf(const FuzzyFindRequest *Message); - llvm::Expected fromProtobuf(const RefsRequest *Message); + fromProtobuf(const v1::FuzzyFindRequest *Message); + llvm::Expected + fromProtobuf(const v1::RefsRequest *Message); llvm::Expected - fromProtobuf(const RelationsRequest *Message); + fromProtobuf(const v1::RelationsRequest *Message); /// toProtobuf() functions serialize native clangd types and strip IndexRoot /// from the file paths specific to indexing machine. fromProtobuf() functions /// deserialize clangd types and translate relative paths into machine-native /// URIs. - LookupRequest toProtobuf(const clangd::LookupRequest &From); - FuzzyFindRequest toProtobuf(const clangd::FuzzyFindRequest &From); - RefsRequest toProtobuf(const clangd::RefsRequest &From); - RelationsRequest toProtobuf(const clangd::RelationsRequest &From); + v1::LookupRequest toProtobuf(const clangd::LookupRequest &From); + v1::FuzzyFindRequest toProtobuf(const clangd::FuzzyFindRequest &From); + v1::RefsRequest toProtobuf(const clangd::RefsRequest &From); + v1::RelationsRequest toProtobuf(const clangd::RelationsRequest &From); - llvm::Expected toProtobuf(const clangd::Symbol &From); - llvm::Expected toProtobuf(const clangd::Ref &From); - llvm::Expected toProtobuf(const clangd::SymbolID &Subject, - const clangd::Symbol &Object); + llvm::Expected toProtobuf(const clangd::Symbol &From); + llvm::Expected toProtobuf(const clangd::Ref &From); + llvm::Expected toProtobuf(const clangd::SymbolID &Subject, + const clangd::Symbol &Object); /// Translates \p RelativePath into the absolute path and builds URI for the /// user machine. This translation happens on the client side with the @@ -77,18 +78,18 @@ llvm::Expected uriToRelativePath(llvm::StringRef URI); private: - clangd::SymbolLocation::Position fromProtobuf(const Position &Message); - Position toProtobuf(const clangd::SymbolLocation::Position &Position); - clang::index::SymbolInfo fromProtobuf(const SymbolInfo &Message); - SymbolInfo toProtobuf(const clang::index::SymbolInfo &Info); + clangd::SymbolLocation::Position fromProtobuf(const v1::Position &Message); + v1::Position toProtobuf(const clangd::SymbolLocation::Position &Position); + clang::index::SymbolInfo fromProtobuf(const v1::SymbolInfo &Message); + v1::SymbolInfo toProtobuf(const clang::index::SymbolInfo &Info); llvm::Expected - fromProtobuf(const SymbolLocation &Message); - llvm::Expected + fromProtobuf(const v1::SymbolLocation &Message); + llvm::Expected toProtobuf(const clangd::SymbolLocation &Location); - llvm::Expected + llvm::Expected toProtobuf(const clangd::Symbol::IncludeHeaderWithReferences &IncludeHeader); llvm::Expected - fromProtobuf(const HeaderWithReferences &Message); + fromProtobuf(const v1::HeaderWithReferences &Message); /// RemoteIndexRoot and LocalIndexRoot are absolute paths to the project (on /// remote and local machine respectively) and include a trailing slash. One diff --git a/clang-tools-extra/clangd/index/remote/marshalling/Marshalling.cpp b/clang-tools-extra/clangd/index/remote/marshalling/Marshalling.cpp --- a/clang-tools-extra/clangd/index/remote/marshalling/Marshalling.cpp +++ b/clang-tools-extra/clangd/index/remote/marshalling/Marshalling.cpp @@ -72,7 +72,7 @@ } llvm::Expected -Marshaller::fromProtobuf(const LookupRequest *Message) { +Marshaller::fromProtobuf(const v1::LookupRequest *Message) { clangd::LookupRequest Req; auto IDs = getIDs(Message->ids()); if (!IDs) @@ -82,7 +82,7 @@ } llvm::Expected -Marshaller::fromProtobuf(const FuzzyFindRequest *Message) { +Marshaller::fromProtobuf(const v1::FuzzyFindRequest *Message) { assert(RemoteIndexRoot); clangd::FuzzyFindRequest Result; Result.Query = Message->query(); @@ -106,7 +106,7 @@ } llvm::Expected -Marshaller::fromProtobuf(const RefsRequest *Message) { +Marshaller::fromProtobuf(const v1::RefsRequest *Message) { clangd::RefsRequest Req; auto IDs = getIDs(Message->ids()); if (!IDs) @@ -119,7 +119,7 @@ } llvm::Expected -Marshaller::fromProtobuf(const RelationsRequest *Message) { +Marshaller::fromProtobuf(const v1::RelationsRequest *Message) { clangd::RelationsRequest Req; auto IDs = getIDs(Message->subjects()); if (!IDs) @@ -131,7 +131,8 @@ return Req; } -llvm::Expected Marshaller::fromProtobuf(const Symbol &Message) { +llvm::Expected +Marshaller::fromProtobuf(const v1::Symbol &Message) { if (!Message.has_info() || !Message.has_canonical_declaration()) return error("Missing info or declaration."); clangd::Symbol Result; @@ -169,7 +170,7 @@ return Result; } -llvm::Expected Marshaller::fromProtobuf(const Ref &Message) { +llvm::Expected Marshaller::fromProtobuf(const v1::Ref &Message) { if (!Message.has_location()) return error("Missing location."); clangd::Ref Result; @@ -182,7 +183,7 @@ } llvm::Expected> -Marshaller::fromProtobuf(const Relation &Message) { +Marshaller::fromProtobuf(const v1::Relation &Message) { auto SubjectID = SymbolID::fromStr(Message.subject_id()); if (!SubjectID) return SubjectID.takeError(); @@ -194,16 +195,17 @@ return std::make_pair(*SubjectID, *Object); } -LookupRequest Marshaller::toProtobuf(const clangd::LookupRequest &From) { - LookupRequest RPCRequest; +v1::LookupRequest Marshaller::toProtobuf(const clangd::LookupRequest &From) { + v1::LookupRequest RPCRequest; for (const auto &SymbolID : From.IDs) RPCRequest.add_ids(SymbolID.str()); return RPCRequest; } -FuzzyFindRequest Marshaller::toProtobuf(const clangd::FuzzyFindRequest &From) { +v1::FuzzyFindRequest +Marshaller::toProtobuf(const clangd::FuzzyFindRequest &From) { assert(LocalIndexRoot); - FuzzyFindRequest RPCRequest; + v1::FuzzyFindRequest RPCRequest; RPCRequest.set_query(From.Query); for (const auto &Scope : From.Scopes) RPCRequest.add_scopes(Scope); @@ -222,8 +224,8 @@ return RPCRequest; } -RefsRequest Marshaller::toProtobuf(const clangd::RefsRequest &From) { - RefsRequest RPCRequest; +v1::RefsRequest Marshaller::toProtobuf(const clangd::RefsRequest &From) { + v1::RefsRequest RPCRequest; for (const auto &ID : From.IDs) RPCRequest.add_ids(ID.str()); RPCRequest.set_filter(static_cast(From.Filter)); @@ -232,8 +234,9 @@ return RPCRequest; } -RelationsRequest Marshaller::toProtobuf(const clangd::RelationsRequest &From) { - RelationsRequest RPCRequest; +v1::RelationsRequest +Marshaller::toProtobuf(const clangd::RelationsRequest &From) { + v1::RelationsRequest RPCRequest; for (const auto &ID : From.Subjects) RPCRequest.add_subjects(ID.str()); RPCRequest.set_predicate(static_cast(From.Predicate)); @@ -242,8 +245,8 @@ return RPCRequest; } -llvm::Expected Marshaller::toProtobuf(const clangd::Symbol &From) { - Symbol Result; +llvm::Expected Marshaller::toProtobuf(const clangd::Symbol &From) { + v1::Symbol Result; Result.set_id(From.ID.str()); *Result.mutable_info() = toProtobuf(From.SymInfo); Result.set_name(From.Name.str()); @@ -278,8 +281,8 @@ return Result; } -llvm::Expected Marshaller::toProtobuf(const clangd::Ref &From) { - Ref Result; +llvm::Expected Marshaller::toProtobuf(const clangd::Ref &From) { + v1::Ref Result; Result.set_kind(static_cast(From.Kind)); auto Location = toProtobuf(From.Location); if (!Location) @@ -288,9 +291,10 @@ return Result; } -llvm::Expected Marshaller::toProtobuf(const clangd::SymbolID &Subject, - const clangd::Symbol &Object) { - Relation Result; +llvm::Expected +Marshaller::toProtobuf(const clangd::SymbolID &Subject, + const clangd::Symbol &Object) { + v1::Relation Result; *Result.mutable_subject_id() = Subject.str(); auto SerializedObject = toProtobuf(Object); if (!SerializedObject) @@ -335,22 +339,23 @@ } clangd::SymbolLocation::Position -Marshaller::fromProtobuf(const Position &Message) { +Marshaller::fromProtobuf(const v1::Position &Message) { clangd::SymbolLocation::Position Result; Result.setColumn(static_cast(Message.column())); Result.setLine(static_cast(Message.line())); return Result; } -Position +v1::Position Marshaller::toProtobuf(const clangd::SymbolLocation::Position &Position) { - remote::Position Result; + remote::v1::Position Result; Result.set_column(Position.column()); Result.set_line(Position.line()); return Result; } -clang::index::SymbolInfo Marshaller::fromProtobuf(const SymbolInfo &Message) { +clang::index::SymbolInfo +Marshaller::fromProtobuf(const v1::SymbolInfo &Message) { clang::index::SymbolInfo Result; Result.Kind = static_cast(Message.kind()); Result.SubKind = static_cast(Message.subkind()); @@ -360,8 +365,8 @@ return Result; } -SymbolInfo Marshaller::toProtobuf(const clang::index::SymbolInfo &Info) { - SymbolInfo Result; +v1::SymbolInfo Marshaller::toProtobuf(const clang::index::SymbolInfo &Info) { + v1::SymbolInfo Result; Result.set_kind(static_cast(Info.Kind)); Result.set_subkind(static_cast(Info.SubKind)); Result.set_language(static_cast(Info.Lang)); @@ -370,7 +375,7 @@ } llvm::Expected -Marshaller::fromProtobuf(const SymbolLocation &Message) { +Marshaller::fromProtobuf(const v1::SymbolLocation &Message) { clangd::SymbolLocation Location; auto URIString = relativePathToURI(Message.file_path()); if (!URIString) @@ -381,9 +386,9 @@ return Location; } -llvm::Expected +llvm::Expected Marshaller::toProtobuf(const clangd::SymbolLocation &Location) { - remote::SymbolLocation Result; + remote::v1::SymbolLocation Result; auto RelativePath = uriToRelativePath(Location.FileURI); if (!RelativePath) return RelativePath.takeError(); @@ -393,9 +398,9 @@ return Result; } -llvm::Expected Marshaller::toProtobuf( +llvm::Expected Marshaller::toProtobuf( const clangd::Symbol::IncludeHeaderWithReferences &IncludeHeader) { - HeaderWithReferences Result; + v1::HeaderWithReferences Result; Result.set_references(IncludeHeader.References); const std::string Header = IncludeHeader.IncludeHeader.str(); if (isLiteralInclude(Header)) { @@ -410,7 +415,7 @@ } llvm::Expected -Marshaller::fromProtobuf(const HeaderWithReferences &Message) { +Marshaller::fromProtobuf(const v1::HeaderWithReferences &Message) { std::string Header = Message.header(); if (!isLiteralInclude(Header)) { auto URIString = relativePathToURI(Header); diff --git a/clang-tools-extra/clangd/unittests/remote/MarshallingTests.cpp b/clang-tools-extra/clangd/unittests/remote/MarshallingTests.cpp --- a/clang-tools-extra/clangd/unittests/remote/MarshallingTests.cpp +++ b/clang-tools-extra/clangd/unittests/remote/MarshallingTests.cpp @@ -133,7 +133,7 @@ // Paths transmitted over the wire can not be absolute, they have to be // relative. - Ref WithAbsolutePath; + v1::Ref WithAbsolutePath; *WithAbsolutePath.mutable_location()->mutable_file_path() = "/usr/local/user/home/HelloWorld.cpp"; Deserialized = ProtobufMarshaller.fromProtobuf(WithAbsolutePath); @@ -282,7 +282,7 @@ Sym.IncludeHeaders.pop_back(); Serialized = ProtobufMarshaller.toProtobuf(Sym); ASSERT_TRUE(bool(Serialized)); - HeaderWithReferences InvalidHeader; + v1::HeaderWithReferences InvalidHeader; InvalidHeader.set_header(convert_to_slash("/absolute/path/Header.h")); InvalidHeader.set_references(9000); *Serialized->add_headers() = InvalidHeader; @@ -388,7 +388,7 @@ } TEST(RemoteMarshallingTest, RelationsRequestFailingSerialization) { - RelationsRequest Serialized; + v1::RelationsRequest Serialized; Serialized.add_subjects("ZZZZZZZZZZZZZZZZ"); Marshaller ProtobufMarshaller(testPath("remote/"), testPath("local/")); auto Deserialized = ProtobufMarshaller.fromProtobuf(&Serialized);