kbobyrev updated this revision to Diff 259257.
kbobyrev added a comment.
Stream responses in refs() and fuzzyFind()
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D78521/new/
https://reviews.llvm.org/D78521
Files:
clang-tools-extra/clangd/CMakeLists.txt
clang-tools-extra/clangd/index/Serialization.h
clang-tools-extra/clangd/index/YAMLSerialization.cpp
clang-tools-extra/clangd/index/dex/dexp/CMakeLists.txt
clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp
clang-tools-extra/clangd/index/remote/CMakeLists.txt
clang-tools-extra/clangd/index/remote/Index.cpp
clang-tools-extra/clangd/index/remote/Index.h
clang-tools-extra/clangd/index/remote/Index.proto
clang-tools-extra/clangd/index/remote/Marshalling.cpp
clang-tools-extra/clangd/index/remote/Marshalling.h
clang-tools-extra/clangd/index/remote/client/CMakeLists.txt
clang-tools-extra/clangd/index/remote/client/Client.cpp
clang-tools-extra/clangd/index/remote/server/CMakeLists.txt
clang-tools-extra/clangd/index/remote/server/Server.cpp
llvm/cmake/modules/FindGRPC.cmake
Index: llvm/cmake/modules/FindGRPC.cmake
===================================================================
--- llvm/cmake/modules/FindGRPC.cmake
+++ llvm/cmake/modules/FindGRPC.cmake
@@ -23,11 +23,11 @@
find_program(PROTOC protoc)
endif()
-# Proto headers are generated in ${CMAKE_CURRENT_BINARY_DIR}.
+# Proto headers are generated in ${GeneratedFilesLocation}.
# Libraries that use these headers should adjust the include path.
# FIXME(kirillbobyrev): Allow optional generation of gRPC code and give callers
# control over it via additional parameters.
-function(generate_grpc_protos LibraryName ProtoFile)
+function(generate_grpc_protos LibraryName ProtoFile GeneratedFilesLocation)
get_filename_component(ProtoSourceAbsolutePath "${CMAKE_CURRENT_SOURCE_DIR}/${ProtoFile}" ABSOLUTE)
get_filename_component(ProtoSourcePath ${ProtoSourceAbsolutePath} PATH)
@@ -35,6 +35,7 @@
set(GeneratedProtoHeader "${CMAKE_CURRENT_BINARY_DIR}/Index.pb.h")
set(GeneratedGRPCSource "${CMAKE_CURRENT_BINARY_DIR}/Index.grpc.pb.cc")
set(GeneratedGRPCHeader "${CMAKE_CURRENT_BINARY_DIR}/Index.grpc.pb.h")
+ set(${GeneratedFilesLocation} ${CMAKE_CURRENT_BINARY_DIR} PARENT_SCOPE)
add_custom_command(
OUTPUT "${GeneratedProtoSource}" "${GeneratedProtoHeader}" "${GeneratedGRPCSource}" "${GeneratedGRPCHeader}"
COMMAND ${PROTOC}
Index: clang-tools-extra/clangd/index/remote/server/Server.cpp
===================================================================
--- clang-tools-extra/clangd/index/remote/server/Server.cpp
+++ clang-tools-extra/clangd/index/remote/server/Server.cpp
@@ -8,6 +8,7 @@
#include "index/Index.h"
#include "index/Serialization.h"
+#include "index/remote/Marshalling.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/LineEditor/LineEditor.h"
@@ -23,6 +24,7 @@
namespace clang {
namespace clangd {
+namespace remote {
namespace {
static const std::string Overview = R"(
@@ -33,8 +35,9 @@
llvm::cl::opt<std::string> IndexPath(llvm::cl::desc("<INDEX FILE>"),
llvm::cl::Positional, llvm::cl::Required);
-llvm::cl::opt<std::string> ServerAddress("server-address",
- llvm::cl::init("0.0.0.0:50051"));
+llvm::cl::opt<std::string> ServerAddress(
+ "server-address", llvm::cl::init("0.0.0.0:50051"),
+ llvm::cl::desc("Address of the invoked server. Defaults to 0.0.0.0:50051"));
std::unique_ptr<SymbolIndex> openIndex(llvm::StringRef Index) {
return loadIndex(Index, /*UseIndex=*/true);
@@ -47,24 +50,60 @@
private:
grpc::Status Lookup(grpc::ServerContext *Context,
- const remote::LookupRequest *Request,
- grpc::ServerWriter<remote::LookupReply> *Reply) override {
- llvm::outs() << "Lookup of symbol with ID " << Request->id() << '\n';
- LookupRequest Req;
- auto SID = SymbolID::fromStr(Request->id());
- if (!SID) {
- llvm::outs() << llvm::toString(SID.takeError()) << "\n";
- return grpc::Status::CANCELLED;
+ const LookupRequest *Request,
+ grpc::ServerWriter<Symbol> *Reply) override {
+ clangd::LookupRequest Req;
+ for (const auto &ID : Request->ids()) {
+ auto SID = SymbolID::fromStr(StringRef(ID));
+ if (!SID)
+ return grpc::Status::CANCELLED;
+ Req.IDs.insert(*SID);
}
- Req.IDs.insert(*SID);
- Index->lookup(Req, [&](const Symbol &Sym) {
- remote::LookupReply NextSymbol;
- NextSymbol.set_symbol_yaml(toYAML(Sym));
+ Index->lookup(Req, [&](const clangd::Symbol &Sym) {
+ remote::Symbol NextSymbol;
+ NextSymbol.set_yaml_serializatiton(toYAML(Sym));
Reply->Write(NextSymbol);
});
return grpc::Status::OK;
}
+ grpc::Status FuzzyFind(grpc::ServerContext *Context,
+ const FuzzyFindRequest *Request,
+ grpc::ServerWriter<FuzzyFindReply> *Reply) override {
+ const auto Req = fromProtobuf(Request);
+ bool HasMore = Index->fuzzyFind(Req, [&](const clangd::Symbol &Sym) {
+ FuzzyFindReply NextMessage;
+ Symbol *NextSymbol = new Symbol;
+ NextSymbol->set_yaml_serializatiton(toYAML(Sym));
+ NextMessage.set_allocated_symbol(NextSymbol);
+ Reply->Write(NextMessage);
+ });
+ FuzzyFindReply LastMessage;
+ LastMessage.set_has_more(HasMore);
+ Reply->Write(LastMessage);
+ return grpc::Status::OK;
+ }
+
+ grpc::Status Refs(grpc::ServerContext *Context, const RefsRequest *Request,
+ grpc::ServerWriter<RefsReply> *Reply) override {
+ clangd::RefsRequest Req;
+ for (const auto &ID : Request->ids()) {
+ auto SID = SymbolID::fromStr(StringRef(ID));
+ if (!SID)
+ return grpc::Status::CANCELLED;
+ Req.IDs.insert(*SID);
+ }
+ bool HasMore = Index->refs(Req, [&](const clangd::Ref &Reference) {
+ RefsReply NextMessage;
+ NextMessage.set_ref(toYAML(Reference));
+ Reply->Write(NextMessage);
+ });
+ RefsReply LastMessage;
+ LastMessage.set_has_more(HasMore);
+ Reply->Write(LastMessage);
+ return grpc::Status::OK;
+ }
+
std::unique_ptr<SymbolIndex> Index;
};
@@ -83,15 +122,16 @@
}
} // namespace
+} // namespace remote
} // namespace clangd
} // namespace clang
int main(int argc, char *argv[]) {
- using namespace clang::clangd;
- llvm::cl::ParseCommandLineOptions(argc, argv, clang::clangd::Overview);
+ using namespace clang::clangd::remote;
+ llvm::cl::ParseCommandLineOptions(argc, argv, Overview);
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
- std::unique_ptr<SymbolIndex> Index = openIndex(IndexPath);
+ std::unique_ptr<clang::clangd::SymbolIndex> Index = openIndex(IndexPath);
if (!Index) {
llvm::outs() << "Failed to open the index.\n";
Index: clang-tools-extra/clangd/index/remote/server/CMakeLists.txt
===================================================================
--- clang-tools-extra/clangd/index/remote/server/CMakeLists.txt
+++ clang-tools-extra/clangd/index/remote/server/CMakeLists.txt
@@ -5,7 +5,7 @@
add_clang_executable(clangd-index-server
Server.cpp
)
-target_compile_definitions(clangd-index-server PRIVATE -DGOOGLE_PROTOBUF_NO_RTTI=1)
+target_compile_definitions(clangd-index-server PRIVATE -D GOOGLE_PROTOBUF_NO_RTTI=1)
clang_target_link_libraries(clangd-index-server
PRIVATE
clangDaemon
@@ -16,5 +16,5 @@
protobuf
grpc++
- clangDaemon
+ clangRemoteIndex
)
Index: clang-tools-extra/clangd/index/remote/client/Client.cpp
===================================================================
--- clang-tools-extra/clangd/index/remote/client/Client.cpp
+++ /dev/null
@@ -1,91 +0,0 @@
-//===--- Client.cpp - Remote Index Client -----------------------*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-//
-// This file implements a simple interactive tool which can be used to manually
-// evaluate symbol search quality of Clangd index.
-//
-//===----------------------------------------------------------------------===//
-
-#include "SourceCode.h"
-#include "index/Serialization.h"
-#include "index/dex/Dex.h"
-#include "llvm/ADT/ScopeExit.h"
-#include "llvm/ADT/SmallVector.h"
-#include "llvm/ADT/StringRef.h"
-#include "llvm/ADT/StringSwitch.h"
-#include "llvm/LineEditor/LineEditor.h"
-#include "llvm/Support/CommandLine.h"
-#include "llvm/Support/Signals.h"
-
-#include "grpcpp/grpcpp.h"
-
-#include "Index.grpc.pb.h"
-
-namespace clang {
-namespace clangd {
-namespace {
-
-llvm::cl::opt<std::string>
- ServerAddress("server-address",
- llvm::cl::desc("Address of remote index server to use."),
- llvm::cl::init("0.0.0.0:50051"));
-
-static const std::string Overview = R"(
-This is an **experimental** interactive tool to process user-provided search
-queries over given symbol collection obtained via clangd-indexer with the help
-of remote index server. The client will connect to remote index server and pass
-it lookup queries.
-)";
-
-class RemoteIndexClient {
-public:
- RemoteIndexClient(std::shared_ptr<grpc::Channel> Channel)
- : Stub(remote::Index::NewStub(Channel)) {}
-
- void lookup(llvm::StringRef ID) {
- llvm::outs() << "Lookup of symbol with ID " << ID << '\n';
- remote::LookupRequest Proto;
- Proto.set_id(ID.str());
-
- grpc::ClientContext Context;
- remote::LookupReply Reply;
- std::unique_ptr<grpc::ClientReader<remote::LookupReply>> Reader(
- Stub->Lookup(&Context, Proto));
- while (Reader->Read(&Reply)) {
- llvm::outs() << Reply.symbol_yaml();
- }
- grpc::Status Status = Reader->Finish();
- if (Status.ok()) {
- llvm::outs() << "lookupRequest rpc succeeded.\n";
- } else {
- llvm::outs() << "lookupRequest rpc failed.\n";
- }
- }
-
-private:
- std::unique_ptr<remote::Index::Stub> Stub;
-};
-
-} // namespace
-} // namespace clangd
-} // namespace clang
-
-int main(int argc, const char *argv[]) {
- using namespace clang::clangd;
-
- llvm::cl::ParseCommandLineOptions(argc, argv, Overview);
- llvm::cl::ResetCommandLineParser(); // We reuse it for REPL commands.
- llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
-
- RemoteIndexClient IndexClient(
- grpc::CreateChannel(ServerAddress, grpc::InsecureChannelCredentials()));
-
- llvm::LineEditor LE("remote-index-client");
- while (llvm::Optional<std::string> Request = LE.readLine())
- IndexClient.lookup(std::move(*Request));
-}
Index: clang-tools-extra/clangd/index/remote/client/CMakeLists.txt
===================================================================
--- clang-tools-extra/clangd/index/remote/client/CMakeLists.txt
+++ /dev/null
@@ -1,19 +0,0 @@
-set(LLVM_LINK_COMPONENTS
- LineEditor
- Support
- )
-add_clang_executable(clangd-index-client
- Client.cpp
- )
-target_compile_definitions(clangd-index-client PRIVATE -DGOOGLE_PROTOBUF_NO_RTTI=1)
-clang_target_link_libraries(clangd-index-client
- PRIVATE
- clangDaemon
- )
-target_link_libraries(clangd-index-client
- PRIVATE
- RemoteIndexProtos
-
- protobuf
- grpc++
- )
Index: clang-tools-extra/clangd/index/remote/Marshalling.h
===================================================================
--- /dev/null
+++ clang-tools-extra/clangd/index/remote/Marshalling.h
@@ -0,0 +1,33 @@
+//===--- Marshalling.h -------------------------------------------*- C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Transformations between native Clangd types and Protobuf-generated classes.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REMOTE_MARSHALLING_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REMOTE_MARSHALLING_H
+
+#include "Index.grpc.pb.h"
+#include "index/Index.h"
+
+namespace clang {
+namespace clangd {
+namespace remote {
+
+clangd::FuzzyFindRequest fromProtobuf(const FuzzyFindRequest *Request);
+
+LookupRequest toProtobuf(const clangd::LookupRequest &From);
+FuzzyFindRequest toProtobuf(const clangd::FuzzyFindRequest &From);
+RefsRequest toProtobuf(const clangd::RefsRequest &From);
+
+} // namespace remote
+} // namespace clangd
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REMOTE_MARSHALLING_H
Index: clang-tools-extra/clangd/index/remote/Marshalling.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/clangd/index/remote/Marshalling.cpp
@@ -0,0 +1,66 @@
+//===--- Marshalling.cpp -----------------------------------------*- C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "Marshalling.h"
+
+namespace clang {
+namespace clangd {
+namespace remote {
+
+clangd::FuzzyFindRequest fromProtobuf(const FuzzyFindRequest *Request) {
+ clangd::FuzzyFindRequest Result;
+ Result.Query = Request->query();
+ for (const auto &Scope : Request->scopes())
+ Result.Scopes.push_back(Scope);
+ Result.AnyScope = Request->any_scope();
+ if (Request->limit())
+ Result.Limit = Request->limit();
+ Result.RestrictForCodeCompletion = Request->resricted_for_code_completion();
+ for (const auto &Path : Request->proximity_paths())
+ Result.ProximityPaths.push_back(Path);
+ for (const auto &Type : Request->preferred_types())
+ Result.ProximityPaths.push_back(Type);
+ return Result;
+}
+
+LookupRequest toProtobuf(const clangd::LookupRequest &From) {
+ LookupRequest RPCRequest;
+ for (const auto &SymbolID : From.IDs)
+ RPCRequest.add_ids(SymbolID.str());
+ return RPCRequest;
+}
+
+FuzzyFindRequest toProtobuf(const clangd::FuzzyFindRequest &From) {
+ FuzzyFindRequest RPCRequest;
+ RPCRequest.set_query(From.Query);
+ for (const auto &Scope : From.Scopes)
+ RPCRequest.add_scopes(Scope);
+ RPCRequest.set_any_scope(From.AnyScope);
+ if (From.Limit)
+ RPCRequest.set_limit(*From.Limit);
+ RPCRequest.set_resricted_for_code_completion(From.RestrictForCodeCompletion);
+ for (const auto &Path : From.ProximityPaths)
+ RPCRequest.add_proximity_paths(Path);
+ for (const auto &Type : From.PreferredTypes)
+ RPCRequest.add_preferred_types(Type);
+ return RPCRequest;
+}
+
+RefsRequest toProtobuf(const clangd::RefsRequest &From) {
+ RefsRequest RPCRequest;
+ for (const auto &ID : From.IDs)
+ RPCRequest.add_ids(ID.str());
+ RPCRequest.set_filter(static_cast<uint32_t>(From.Filter));
+ if (From.Limit)
+ RPCRequest.set_limit(*From.Limit);
+ return RPCRequest;
+}
+
+} // namespace remote
+} // namespace clangd
+} // namespace clang
Index: clang-tools-extra/clangd/index/remote/Index.proto
===================================================================
--- clang-tools-extra/clangd/index/remote/Index.proto
+++ clang-tools-extra/clangd/index/remote/Index.proto
@@ -11,9 +11,48 @@
package clang.clangd.remote;
service Index {
- rpc Lookup(LookupRequest) returns (stream LookupReply) {}
+ rpc Lookup(LookupRequest) returns (stream Symbol) {}
+
+ rpc FuzzyFind(FuzzyFindRequest) returns (stream FuzzyFindReply) {}
+
+ rpc Refs(RefsRequest) returns (stream RefsReply) {}
+}
+
+message LookupRequest { repeated string ids = 1; }
+
+// FIXME(kirillbobyrev): Properly serialize symbol instead of passing YAML.
+message Symbol { string yaml_serializatiton = 1; }
+
+message FuzzyFindRequest {
+ string query = 1;
+ repeated string scopes = 2;
+ bool any_scope = 3;
+ uint32 limit = 4;
+ bool resricted_for_code_completion = 5;
+ repeated string proximity_paths = 6;
+ repeated string preferred_types = 7;
}
-message LookupRequest { string id = 1; }
+// Actual messages contain only symbols and they will be followed by a
+// terminating message with has_more field.
+message FuzzyFindReply {
+ oneof symbol_or_has_more {
+ Symbol symbol = 1;
+ bool has_more = 2;
+ }
+}
-message LookupReply { string symbol_yaml = 1; }
+message RefsRequest {
+ repeated string ids = 1;
+ uint32 filter = 2;
+ uint32 limit = 3;
+}
+
+// Actual messages contain only refs and they will be followed by a terminating
+// message with has_more field.
+message RefsReply {
+ oneof ref_or_has_more {
+ string ref = 1;
+ bool has_more = 2;
+ }
+}
Index: clang-tools-extra/clangd/index/remote/Index.h
===================================================================
--- /dev/null
+++ clang-tools-extra/clangd/index/remote/Index.h
@@ -0,0 +1,27 @@
+//===--- Index.h -------------------------------------------------*- C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REMOTE_INDEX_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REMOTE_INDEX_H
+
+#include "grpcpp/grpcpp.h"
+
+#include "Index.grpc.pb.h"
+#include "index/Index.h"
+
+namespace clang {
+namespace clangd {
+namespace remote {
+
+std::unique_ptr<SymbolIndex> connect(llvm::StringRef Address);
+
+} // namespace remote
+} // namespace clangd
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REMOTE_INDEX_H
Index: clang-tools-extra/clangd/index/remote/Index.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/clangd/index/remote/Index.cpp
@@ -0,0 +1,108 @@
+//===--- Index.cpp -----------------------------------------------*- C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "Index.h"
+#include "Index.grpc.pb.h"
+#include "Marshalling.h"
+#include "index/Serialization.h"
+#include "llvm/Support/YAMLTraits.h"
+
+namespace clang {
+namespace clangd {
+namespace remote {
+namespace {
+class IndexClient : public SymbolIndex {
+public:
+ IndexClient(llvm::StringRef Address)
+ : Stub(remote::Index::NewStub(grpc::CreateChannel(
+ Address.str(), grpc::InsecureChannelCredentials()))) {}
+
+ void lookup(const clangd::LookupRequest &Request,
+ llvm::function_ref<void(const clangd::Symbol &)> Callback) const {
+ const LookupRequest RPCRequest = toProtobuf(Request);
+
+ grpc::ClientContext Context;
+ std::unique_ptr<grpc::ClientReader<remote::Symbol>> Reader(
+ Stub->Lookup(&Context, RPCRequest));
+ llvm::BumpPtrAllocator Arena;
+ llvm::UniqueStringSaver Strings(Arena);
+ remote::Symbol Reply;
+ while (Reader->Read(&Reply))
+ Callback(symbolFromYAML(Reply.yaml_serializatiton(), &Strings));
+ grpc::Status Status = Reader->Finish();
+ llvm::outs() << "lookup rpc " << (Status.ok() ? "succeeded" : "failed")
+ << '\n';
+ }
+
+ bool
+ fuzzyFind(const clangd::FuzzyFindRequest &Request,
+ llvm::function_ref<void(const clangd::Symbol &)> Callback) const {
+ const FuzzyFindRequest RPCRequest = toProtobuf(Request);
+
+ grpc::ClientContext Context;
+ std::unique_ptr<grpc::ClientReader<remote::FuzzyFindReply>> Reader(
+ Stub->FuzzyFind(&Context, RPCRequest));
+ llvm::BumpPtrAllocator Arena;
+ llvm::UniqueStringSaver Strings(Arena);
+ FuzzyFindReply Reply;
+ while (Reader->Read(&Reply))
+ if (Reply.has_symbol())
+ Callback(
+ symbolFromYAML(Reply.symbol().yaml_serializatiton(), &Strings));
+ grpc::Status Status = Reader->Finish();
+ llvm::outs() << "FuzzyFind rpc " << (Status.ok() ? "succeeded" : "failed")
+ << '\n';
+ return Reply.has_more();
+ }
+
+ bool refs(const clangd::RefsRequest &Request,
+ llvm::function_ref<void(const Ref &)> Callback) const {
+ RefsRequest RPCRequest = toProtobuf(Request);
+
+ grpc::ClientContext Context;
+ std::unique_ptr<grpc::ClientReader<remote::RefsReply>> Reader(
+ Stub->Refs(&Context, RPCRequest));
+ llvm::BumpPtrAllocator Arena;
+ llvm::UniqueStringSaver Strings(Arena);
+ RefsReply Reply;
+ while (Reader->Read(&Reply))
+ if (Reply.ref_or_has_more_case() == RefsReply::kRef)
+ Callback(refFromYAML(Reply.ref(), &Strings));
+ grpc::Status Status = Reader->Finish();
+ llvm::outs() << "Refs rpc " << (Status.ok() ? "succeeded" : "failed")
+ << '\n';
+ return Reply.has_more();
+ }
+
+ // FIXME(kirillbobyrev): Implement this.
+ void
+ relations(const clangd::RelationsRequest &,
+ llvm::function_ref<void(const SymbolID &, const clangd::Symbol &)>)
+ const {}
+
+ // IndexClient does not take any space since the data is stored on the server.
+ size_t estimateMemoryUsage() const { return 0; }
+
+private:
+ std::unique_ptr<remote::Index::Stub> Stub;
+};
+} // namespace
+
+std::unique_ptr<SymbolIndex> connect(llvm::StringRef Address) {
+#ifdef CLANGD_REMOTE
+ return std::unique_ptr<SymbolIndex>(new IndexClient(Address));
+#else
+ llvm_unreachable(
+ "Can't create SymbolIndex client instance without remote index support.");
+ return nullptr;
+#endif
+}
+
+} // namespace remote
+} // namespace clangd
+} // namespace clang
Index: clang-tools-extra/clangd/index/remote/CMakeLists.txt
===================================================================
--- clang-tools-extra/clangd/index/remote/CMakeLists.txt
+++ clang-tools-extra/clangd/index/remote/CMakeLists.txt
@@ -1,7 +1,20 @@
-generate_grpc_protos(RemoteIndexProtos "Index.proto")
-
-include_directories("${CMAKE_CURRENT_BINARY_DIR}")
+include_directories(${RemoteProtosLocation})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../)
-add_subdirectory(client)
+# FIXME(kirillbobyrev): target_compile_definitions is not working with
+# add_clang_library for some reason. Is there any way to make this
+# target-local?
+add_definitions(-DGOOGLE_PROTOBUF_NO_RTTI=1)
+add_clang_library(clangRemoteIndex
+ Index.cpp
+ Marshalling.cpp
+
+ LINK_LIBS
+ RemoteIndexProtos
+
+ protobuf
+ grpc++
+ clangDaemon
+ )
+
add_subdirectory(server)
Index: clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp
===================================================================
--- clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp
+++ clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp
@@ -14,6 +14,7 @@
#include "SourceCode.h"
#include "index/Serialization.h"
#include "index/dex/Dex.h"
+#include "index/remote/Index.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
@@ -22,16 +23,27 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Signals.h"
+#ifdef CLANGD_ENABLE_REMOTE
+#include "Index.grpc.pb.h"
+#endif
+
namespace clang {
namespace clangd {
namespace {
-llvm::cl::opt<std::string> IndexPath(llvm::cl::desc("<INDEX FILE>"),
- llvm::cl::Positional, llvm::cl::Required);
+llvm::cl::opt<std::string> IndexLocation(llvm::cl::desc("<INDEX LOCATION>"),
+ llvm::cl::Positional);
llvm::cl::opt<std::string>
ExecCommand("c", llvm::cl::desc("Command to execute and then exit"));
+#ifdef CLANGD_REMOTE
+// FIXME(kirillbobyrev): This is not working properly for some reason.
+llvm::cl::opt<bool>
+ RemoteMode("remote",
+ llvm::cl::desc("Connect to <INDEX LOCATION> remote index"));
+#endif
+
static const std::string Overview = R"(
This is an **experimental** interactive tool to process user-provided search
queries over given symbol collection obtained via clangd-indexer. The
@@ -150,7 +162,7 @@
}
Request.AnyScope = Request.Scopes.empty();
// FIXME(kbobyrev): Print symbol final scores to see the distribution.
- static const auto OutputFormat = "{0,-4} | {1,-40} | {2,-25}\n";
+ static const auto *OutputFormat = "{0,-4} | {1,-40} | {2,-25}\n";
llvm::outs() << llvm::formatv(OutputFormat, "Rank", "Symbol ID",
"Symbol Name");
size_t Rank = 0;
@@ -266,12 +278,17 @@
{"find", "Search for symbols with fuzzyFind", std::make_unique<FuzzyFind>},
{"lookup", "Dump symbol details by ID or qualified name",
std::make_unique<Lookup>},
- {"refs", "Find references by ID or qualified name",
- std::make_unique<Refs>},
+ {"refs", "Find references by ID or qualified name", std::make_unique<Refs>},
};
-std::unique_ptr<SymbolIndex> openIndex(llvm::StringRef Index) {
+std::unique_ptr<SymbolIndex> openIndex(llvm::StringRef Index,
+ bool ConnectToServer) {
+#ifdef CLANGD_REMOTE
+ return ConnectToServer ? remote::connect(Index)
+ : loadIndex(Index, /*UseDex=*/true);
+#else
return loadIndex(Index, /*UseDex=*/true);
+#endif
}
bool runCommand(std::string Request, const SymbolIndex &Index) {
@@ -310,13 +327,15 @@
using namespace clang::clangd;
llvm::cl::ParseCommandLineOptions(argc, argv, Overview);
+ // Need to save RemoteMode because it will be reset to defaul by
+ // llvm::cl::ResettCommandLineParser().
+ const bool ConnectToServer = RemoteMode;
llvm::cl::ResetCommandLineParser(); // We reuse it for REPL commands.
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
std::unique_ptr<SymbolIndex> Index;
- reportTime("Dex build", [&]() {
- Index = openIndex(IndexPath);
- });
+ reportTime("Dex build",
+ [&]() { Index = openIndex(IndexLocation, ConnectToServer); });
if (!Index) {
llvm::outs() << "Failed to open the index.\n";
Index: clang-tools-extra/clangd/index/dex/dexp/CMakeLists.txt
===================================================================
--- clang-tools-extra/clangd/index/dex/dexp/CMakeLists.txt
+++ clang-tools-extra/clangd/index/dex/dexp/CMakeLists.txt
@@ -1,4 +1,5 @@
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../)
+include_directories(${RemoteProtosLocation})
set(LLVM_LINK_COMPONENTS
LineEditor
@@ -8,6 +9,7 @@
add_clang_executable(dexp
Dexp.cpp
)
+target_compile_definitions(dexp PUBLIC -D GOOGLE_PROTOBUF_NO_RTTI=1)
clang_target_link_libraries(dexp
PRIVATE
@@ -16,4 +18,8 @@
target_link_libraries(dexp
PRIVATE
clangDaemon
+ clangRemoteIndex
+
+ RemoteIndexProtos
+ grpc++
)
Index: clang-tools-extra/clangd/index/YAMLSerialization.cpp
===================================================================
--- clang-tools-extra/clangd/index/YAMLSerialization.cpp
+++ clang-tools-extra/clangd/index/YAMLSerialization.cpp
@@ -418,5 +418,31 @@
return Buf;
}
+std::string toYAML(const Ref &R) {
+ std::string Buf;
+ {
+ llvm::raw_string_ostream OS(Buf);
+ llvm::yaml::Output Yout(OS);
+ Ref Reference = R; // copy: Yout<< requires mutability.
+ Yout << Reference;
+ }
+ return Buf;
+}
+
+clangd::Symbol symbolFromYAML(StringRef YAML,
+ llvm::UniqueStringSaver *Strings) {
+ clangd::Symbol Deserialized;
+ llvm::yaml::Input YAMLInput(YAML, Strings);
+ YAMLInput >> Deserialized;
+ return Deserialized;
+}
+
+clangd::Ref refFromYAML(StringRef YAML, llvm::UniqueStringSaver *Strings) {
+ clangd::Ref Deserialized;
+ llvm::yaml::Input YAMLInput(YAML, Strings);
+ YAMLInput >> Deserialized;
+ return Deserialized;
+}
+
} // namespace clangd
} // namespace clang
Index: clang-tools-extra/clangd/index/Serialization.h
===================================================================
--- clang-tools-extra/clangd/index/Serialization.h
+++ clang-tools-extra/clangd/index/Serialization.h
@@ -77,6 +77,11 @@
std::string toYAML(const Symbol &);
std::string toYAML(const std::pair<SymbolID, ArrayRef<Ref>> &);
std::string toYAML(const Relation &);
+std::string toYAML(const Ref &);
+
+// Deserialize a single symbol from YAML.
+clangd::Symbol symbolFromYAML(StringRef YAML, llvm::UniqueStringSaver *Strings);
+clangd::Ref refFromYAML(StringRef YAML, llvm::UniqueStringSaver *Strings);
// Build an in-memory static index from an index file.
// The size should be relatively small, so data can be managed in memory.
Index: clang-tools-extra/clangd/CMakeLists.txt
===================================================================
--- clang-tools-extra/clangd/CMakeLists.txt
+++ clang-tools-extra/clangd/CMakeLists.txt
@@ -140,7 +140,6 @@
endif()
add_subdirectory(tool)
add_subdirectory(indexer)
-add_subdirectory(index/dex/dexp)
if (LLVM_INCLUDE_BENCHMARKS)
add_subdirectory(benchmarks)
@@ -159,6 +158,9 @@
set(GRPC_INSTALL_PATH "" CACHE PATH "Path to gRPC library manual installation.")
if (CLANGD_ENABLE_REMOTE)
+ add_definitions(-D CLANGD_REMOTE)
include(FindGRPC)
+ generate_grpc_protos(RemoteIndexProtos "index/remote/Index.proto" RemoteProtosLocation)
add_subdirectory(index/remote)
endif()
+add_subdirectory(index/dex/dexp)
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits