19 #include "clang/Index/IndexSymbol.h"
20 #include "llvm/ADT/None.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Support/Error.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/StringSaver.h"
36 clangd::SymbolLocation::Position Result;
37 Result.setColumn(static_cast<uint32_t>(
Message.column()));
38 Result.setLine(static_cast<uint32_t>(
Message.line()));
42 Position
toProtobuf(
const clangd::SymbolLocation::Position &Position) {
43 remote::Position Result;
44 Result.set_column(Position.column());
45 Result.set_line(Position.line());
51 Result.Kind = static_cast<clang::index::SymbolKind>(
Message.kind());
52 Result.SubKind = static_cast<clang::index::SymbolSubKind>(
Message.subkind());
53 Result.Lang = static_cast<clang::index::SymbolLanguage>(
Message.language());
55 static_cast<clang::index::SymbolPropertySet>(
Message.properties());
61 Result.set_kind(static_cast<uint32_t>(
Info.Kind));
62 Result.set_subkind(static_cast<uint32_t>(
Info.SubKind));
63 Result.set_language(static_cast<uint32_t>(
Info.Lang));
64 Result.set_properties(static_cast<uint32_t>(
Info.Properties));
68 llvm::Optional<clangd::SymbolLocation>
70 llvm::StringRef IndexRoot) {
81 llvm::Optional<SymbolLocation>
83 remote::SymbolLocation Result;
87 *Result.mutable_file_path() = *RelativePath;
93 llvm::Optional<HeaderWithReferences>
94 toProtobuf(
const clangd::Symbol::IncludeHeaderWithReferences &IncludeHeader,
95 llvm::StringRef IndexRoot) {
96 HeaderWithReferences Result;
97 Result.set_references(IncludeHeader.References);
98 const std::string Header = IncludeHeader.IncludeHeader.str();
100 Result.set_header(Header);
106 Result.set_header(*RelativePath);
110 llvm::Optional<clangd::Symbol::IncludeHeaderWithReferences>
112 llvm::UniqueStringSaver *
Strings, llvm::StringRef IndexRoot) {
113 std::string Header =
Message.header();
114 if (Header.front() !=
'<' && Header.front() !=
'"') {
120 return clangd::Symbol::IncludeHeaderWithReferences{
Strings->save(Header),
127 llvm::StringRef IndexRoot) {
129 Result.Query = Request->query();
130 for (
const auto &Scope : Request->scopes())
131 Result.Scopes.push_back(Scope);
132 Result.AnyScope = Request->any_scope();
133 if (Request->limit())
134 Result.Limit = Request->limit();
135 Result.RestrictForCodeCompletion = Request->restricted_for_code_completion();
136 for (
const auto &
Path : Request->proximity_paths()) {
137 llvm::SmallString<256> LocalPath = llvm::StringRef(IndexRoot);
138 llvm::sys::path::append(LocalPath,
Path);
139 Result.ProximityPaths.push_back(std::string(LocalPath));
141 for (
const auto &
Type : Request->preferred_types())
142 Result.ProximityPaths.push_back(
Type);
147 llvm::UniqueStringSaver *
Strings,
148 llvm::StringRef IndexRoot) {
150 !
Message.has_canonical_declaration()) {
151 elog(
"Cannot convert Symbol from Protobuf: {0}",
158 elog(
"Cannot parse SymbolID {0} given Protobuf: {1}", ID.takeError(),
165 Result.Scope =
Message.scope();
175 Result.References =
Message.references();
176 Result.Origin = static_cast<clangd::SymbolOrigin>(
Message.origin());
177 Result.Signature =
Message.signature();
178 Result.TemplateSpecializationArgs =
Message.template_specialization_args();
179 Result.CompletionSnippetSuffix =
Message.completion_snippet_suffix();
180 Result.Documentation =
Message.documentation();
181 Result.ReturnType =
Message.return_type();
183 for (
const auto &Header :
Message.headers()) {
185 if (SerializedHeader)
186 Result.IncludeHeaders.push_back(*SerializedHeader);
188 Result.Flags = static_cast<clangd::Symbol::SymbolFlag>(
Message.flags());
193 llvm::UniqueStringSaver *
Strings,
194 llvm::StringRef IndexRoot) {
196 elog(
"Cannot convert Ref from Protobuf: {}",
Message.ShortDebugString());
204 Result.Kind = static_cast<clangd::RefKind>(
Message.kind());
216 llvm::StringRef IndexRoot) {
218 RPCRequest.set_query(From.
Query);
219 for (
const auto &Scope : From.
Scopes)
220 RPCRequest.add_scopes(Scope);
221 RPCRequest.set_any_scope(From.
AnyScope);
223 RPCRequest.set_limit(*From.
Limit);
226 llvm::SmallString<256> RelativePath = llvm::StringRef(
Path);
227 if (llvm::sys::path::replace_path_prefix(RelativePath, IndexRoot,
""))
228 RPCRequest.add_proximity_paths(llvm::sys::path::convert_to_slash(
229 RelativePath, llvm::sys::path::Style::posix));
232 RPCRequest.add_preferred_types(
Type);
238 for (
const auto &ID : From.
IDs)
239 RPCRequest.add_ids(ID.str());
240 RPCRequest.set_filter(static_cast<uint32_t>(From.
Filter));
242 RPCRequest.set_limit(*From.
Limit);
248 Result.set_id(From.
ID.
str());
250 Result.set_name(From.
Name.str());
254 Result.set_scope(From.
Scope.str());
257 *Result.mutable_canonical_declaration() = *
Declaration;
259 Result.set_origin(static_cast<uint32_t>(From.
Origin));
260 Result.set_signature(From.
Signature.str());
261 Result.set_template_specialization_args(
265 Result.set_return_type(From.
ReturnType.str());
266 Result.set_type(From.
Type.str());
268 auto Serialized =
toProtobuf(Header, IndexRoot);
271 auto *NextHeader = Result.add_headers();
272 *NextHeader = *Serialized;
274 Result.set_flags(static_cast<uint32_t>(From.
Flags));
282 Result.set_kind(static_cast<uint32_t>(From.
Kind));
285 *Result.mutable_location() = *
Location;
290 llvm::StringRef IndexRoot) {
291 assert(RelativePath == llvm::sys::path::convert_to_slash(
292 RelativePath, llvm::sys::path::Style::posix));
293 assert(IndexRoot == llvm::sys::path::convert_to_slash(IndexRoot));
294 assert(IndexRoot.endswith(llvm::sys::path::get_separator()));
295 if (RelativePath.empty())
296 return std::string();
297 if (llvm::sys::path::is_absolute(RelativePath)) {
298 elog(
"Remote index client got absolute path from server: {0}",
302 if (llvm::sys::path::is_relative(IndexRoot)) {
303 elog(
"Remote index client got a relative path as index root: {0}",
307 llvm::SmallString<256> FullPath = IndexRoot;
308 llvm::sys::path::append(FullPath, RelativePath);
310 return Result.toString();
314 llvm::StringRef IndexRoot) {
315 assert(IndexRoot.endswith(llvm::sys::path::get_separator()));
316 assert(IndexRoot == llvm::sys::path::convert_to_slash(IndexRoot));
317 assert(!IndexRoot.empty());
318 if (llvm::sys::path::is_relative(IndexRoot)) {
319 elog(
"Index root {0} is not absolute path", IndexRoot);
324 elog(
"Remote index got bad URI from client {0}: {1}",
URI,
325 ParsedURI.takeError());
328 if (ParsedURI->scheme() !=
"file") {
329 elog(
"Remote index got URI with scheme other than \"file\" {0}: {1}",
URI,
333 llvm::SmallString<256> Result = ParsedURI->body();
334 if (!llvm::sys::path::replace_path_prefix(Result, IndexRoot,
"")) {
335 elog(
"Can not get relative path from the URI {0} given the index root {1}",
340 return llvm::sys::path::convert_to_slash(Result,
341 llvm::sys::path::Style::posix);