1 //===--- Marshalling.h -------------------------------------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Marshalling provides translation between native clangd types into the
10 // Protobuf-generated classes. Most translations are 1-to-1 and wrap variables
11 // into appropriate Protobuf types.
12 //
13 // A notable exception is URI translation. Because paths to files are different
14 // on indexing machine and client machine
15 // ("/remote/machine/projects/llvm-project/llvm/include/HelloWorld.h" versus
16 // "/usr/local/username/llvm-project/llvm/include/HelloWorld.h"), they need to
17 // be converted appropriately. Remote machine strips the prefix from the
18 // absolute path and passes paths relative to the project root over the wire
19 // ("include/HelloWorld.h" in this example). The indexed project root is passed
20 // to the remote server. Client receives this relative path and constructs a URI
21 // that points to the relevant file in the filesystem. The relative path is
22 // appended to IndexRoot to construct the full path and build the final URI.
23 //
24 // Index root is the absolute path to the project and includes a trailing slash.
25 // The relative path passed over the wire has unix slashes.
26 //
27 // toProtobuf() functions serialize native clangd types and strip IndexRoot from
28 // the file paths specific to indexing machine. fromProtobuf() functions
29 // deserialize clangd types and translate relative paths into machine-native
30 // URIs.
31 //
32 //===----------------------------------------------------------------------===//
33 
34 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REMOTE_MARSHALLING_H
35 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REMOTE_MARSHALLING_H
36 
37 #include "Index.pb.h"
38 #include "index/Index.h"
39 #include "llvm/ADT/StringRef.h"
40 #include "llvm/Support/StringSaver.h"
41 
42 namespace clang {
43 namespace clangd {
44 namespace remote {
45 
46 clangd::FuzzyFindRequest fromProtobuf(const FuzzyFindRequest *Request,
47                                       llvm::StringRef IndexRoot);
48 llvm::Optional<clangd::Symbol> fromProtobuf(const Symbol &Message,
49                                             llvm::UniqueStringSaver *Strings,
50                                             llvm::StringRef IndexRoot);
51 llvm::Optional<clangd::Ref> fromProtobuf(const Ref &Message,
52                                          llvm::UniqueStringSaver *Strings,
53                                          llvm::StringRef IndexRoot);
54 
55 LookupRequest toProtobuf(const clangd::LookupRequest &From);
56 FuzzyFindRequest toProtobuf(const clangd::FuzzyFindRequest &From,
57                             llvm::StringRef IndexRoot);
58 RefsRequest toProtobuf(const clangd::RefsRequest &From);
59 
60 Ref toProtobuf(const clangd::Ref &From, llvm::StringRef IndexRoot);
61 Symbol toProtobuf(const clangd::Symbol &From, llvm::StringRef IndexRoot);
62 
63 /// Translates \p RelativePath into the absolute path and builds URI for the
64 /// user machine. This translation happens on the client side with the
65 /// \p RelativePath received from remote index server and \p IndexRoot is
66 /// provided by the client.
67 llvm::Optional<std::string> relativePathToURI(llvm::StringRef RelativePath,
68                                               llvm::StringRef IndexRoot);
69 /// Translates a URI from the server's backing index to a relative path suitable
70 /// to send over the wire to the client.
71 llvm::Optional<std::string> uriToRelativePath(llvm::StringRef URI,
72                                               llvm::StringRef IndexRoot);
73 
74 } // namespace remote
75 } // namespace clangd
76 } // namespace clang
77 
78 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REMOTE_MARSHALLING_H
79