Skip to content

Commit 93bb994

Browse files
committed
[clangd] Implement path and URI translation for remote index
Reviewers: sammccall Reviewed By: sammccall Subscribers: ormris, ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D82938
1 parent 47c4ce4 commit 93bb994

File tree

9 files changed

+530
-124
lines changed

9 files changed

+530
-124
lines changed

clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ llvm::cl::opt<std::string> IndexLocation(
3232
llvm::cl::opt<std::string>
3333
ExecCommand("c", llvm::cl::desc("Command to execute and then exit"));
3434

35+
llvm::cl::opt<std::string> ProjectRoot("project-root",
36+
llvm::cl::desc("Path to the project"));
37+
3538
static constexpr char Overview[] = R"(
3639
This is an **experimental** interactive tool to process user-provided search
3740
queries over given symbol collection obtained via clangd-indexer. The
@@ -326,7 +329,8 @@ struct {
326329

327330
std::unique_ptr<SymbolIndex> openIndex(llvm::StringRef Index) {
328331
return Index.startswith("remote:")
329-
? remote::getClient(Index.drop_front(strlen("remote:")))
332+
? remote::getClient(Index.drop_front(strlen("remote:")),
333+
ProjectRoot)
330334
: loadIndex(Index, /*UseDex=*/true);
331335
}
332336

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010

1111
#include "Client.h"
1212
#include "Index.grpc.pb.h"
13+
#include "index/Index.h"
1314
#include "index/Serialization.h"
1415
#include "marshalling/Marshalling.h"
1516
#include "support/Logger.h"
1617
#include "support/Trace.h"
18+
#include "llvm/ADT/StringRef.h"
1719

1820
#include <chrono>
1921

@@ -27,14 +29,24 @@ class IndexClient : public clangd::SymbolIndex {
2729
using StreamingCall = std::unique_ptr<grpc::ClientReader<ReplyT>> (
2830
remote::SymbolIndex::Stub::*)(grpc::ClientContext *, const RequestT &);
2931

32+
template <typename ClangdRequestT, typename RequestT>
33+
RequestT serializeRequest(ClangdRequestT Request) const {
34+
return toProtobuf(Request);
35+
}
36+
37+
template <>
38+
FuzzyFindRequest serializeRequest(clangd::FuzzyFindRequest Request) const {
39+
return toProtobuf(Request, ProjectRoot);
40+
}
41+
3042
template <typename RequestT, typename ReplyT, typename ClangdRequestT,
3143
typename CallbackT>
3244
bool streamRPC(ClangdRequestT Request,
3345
StreamingCall<RequestT, ReplyT> RPCCall,
3446
CallbackT Callback) const {
3547
bool FinalResult = false;
3648
trace::Span Tracer(RequestT::descriptor()->name());
37-
const auto RPCRequest = toProtobuf(Request);
49+
const auto RPCRequest = serializeRequest<ClangdRequestT, RequestT>(Request);
3850
grpc::ClientContext Context;
3951
std::chrono::system_clock::time_point Deadline =
4052
std::chrono::system_clock::now() + DeadlineWaitingTime;
@@ -48,20 +60,21 @@ class IndexClient : public clangd::SymbolIndex {
4860
FinalResult = Reply.final_result();
4961
continue;
5062
}
51-
auto Sym = fromProtobuf(Reply.stream_result(), &Strings);
52-
if (!Sym)
63+
auto Response =
64+
fromProtobuf(Reply.stream_result(), &Strings, ProjectRoot);
65+
if (!Response)
5366
elog("Received invalid {0}", ReplyT::descriptor()->name());
54-
Callback(*Sym);
67+
Callback(*Response);
5568
}
5669
SPAN_ATTACH(Tracer, "status", Reader->Finish().ok());
5770
return FinalResult;
5871
}
5972

6073
public:
6174
IndexClient(
62-
std::shared_ptr<grpc::Channel> Channel,
75+
std::shared_ptr<grpc::Channel> Channel, llvm::StringRef ProjectRoot,
6376
std::chrono::milliseconds DeadlineTime = std::chrono::milliseconds(1000))
64-
: Stub(remote::SymbolIndex::NewStub(Channel)),
77+
: Stub(remote::SymbolIndex::NewStub(Channel)), ProjectRoot(ProjectRoot),
6578
DeadlineWaitingTime(DeadlineTime) {}
6679

6780
void lookup(const clangd::LookupRequest &Request,
@@ -86,22 +99,26 @@ class IndexClient : public clangd::SymbolIndex {
8699
llvm::function_ref<void(const SymbolID &, const clangd::Symbol &)>)
87100
const {}
88101

89-
// IndexClient does not take any space since the data is stored on the server.
102+
// IndexClient does not take any space since the data is stored on the
103+
// server.
90104
size_t estimateMemoryUsage() const { return 0; }
91105

92106
private:
93107
std::unique_ptr<remote::SymbolIndex::Stub> Stub;
108+
std::string ProjectRoot;
94109
// Each request will be terminated if it takes too long.
95110
std::chrono::milliseconds DeadlineWaitingTime;
96111
};
97112

98113
} // namespace
99114

100-
std::unique_ptr<clangd::SymbolIndex> getClient(llvm::StringRef Address) {
115+
std::unique_ptr<clangd::SymbolIndex> getClient(llvm::StringRef Address,
116+
llvm::StringRef ProjectRoot) {
101117
const auto Channel =
102118
grpc::CreateChannel(Address.str(), grpc::InsecureChannelCredentials());
103119
Channel->GetState(true);
104-
return std::unique_ptr<clangd::SymbolIndex>(new IndexClient(Channel));
120+
return std::unique_ptr<clangd::SymbolIndex>(
121+
new IndexClient(Channel, ProjectRoot));
105122
}
106123

107124
} // namespace remote

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,24 @@
1010
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REMOTE_INDEX_H
1111

1212
#include "index/Index.h"
13+
#include "llvm/ADT/StringRef.h"
1314

1415
namespace clang {
1516
namespace clangd {
1617
namespace remote {
1718

1819
/// Returns an SymbolIndex client that passes requests to remote index located
1920
/// at \p Address. The client allows synchronous RPC calls.
21+
/// \p IndexRoot is an absolute path on the local machine to the source tree
22+
/// described by the remote index. Paths returned by the index will be treated
23+
/// as relative to this directory.
2024
///
2125
/// This method attempts to resolve the address and establish the connection.
2226
///
2327
/// \returns nullptr if the address is not resolved during the function call or
2428
/// if the project was compiled without Remote Index support.
25-
std::unique_ptr<clangd::SymbolIndex> getClient(llvm::StringRef Address);
29+
std::unique_ptr<clangd::SymbolIndex> getClient(llvm::StringRef Address,
30+
llvm::StringRef IndexRoot);
2631

2732
} // namespace remote
2833
} // namespace clangd

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ message Symbol {
7171
string name = 3;
7272
SymbolLocation definition = 4;
7373
string scope = 5;
74-
SymbolLocation canonical_declarattion = 6;
74+
SymbolLocation canonical_declaration = 6;
7575
int32 references = 7;
7676
uint32 origin = 8;
7777
string signature = 9;
@@ -99,7 +99,10 @@ message SymbolInfo {
9999
message SymbolLocation {
100100
Position start = 1;
101101
Position end = 2;
102-
string file_uri = 3;
102+
// clangd::SymbolLocation stores FileURI, but the protocol transmits a the
103+
// relative path. Because paths are different on the remote and local machines
104+
// they will be translated in the marshalling layer.
105+
string file_path = 3;
103106
}
104107

105108
message Position {

0 commit comments

Comments
 (0)