File: Marshalling.h

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-16
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,496,368 kB
  • sloc: cpp: 5,593,980; ansic: 986,873; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,547; xml: 953; cs: 573; fortran: 567
file content (108 lines) | stat: -rw-r--r-- 5,082 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//===--- 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REMOTE_MARSHALLING_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REMOTE_MARSHALLING_H

#include "Index.pb.h"
#include "index/Index.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/StringSaver.h"

namespace clang {
namespace clangd {
namespace remote {

// Marshalling provides an interface for translattion between native clangd
// types into the Protobuf-generated classes. Most translations are 1-to-1 and
// wrap variables into appropriate Protobuf types.
//
/// A notable exception is URI translation. Because paths to files are different
/// on indexing machine and client machine
/// ("/remote/machine/projects/llvm-project/llvm/include/HelloWorld.h" versus
/// "/usr/local/username/llvm-project/llvm/include/HelloWorld.h"), they need to
/// be converted appropriately. Remote machine strips the prefix
/// (RemoteIndexRoot) from the absolute path and passes paths relative to the
/// project root over the wire ("include/HelloWorld.h" in this example). The
/// indexed project root is passed to the remote server. Client receives this
/// relative path and constructs a URI that points to the relevant file in the
/// filesystem. The relative path is appended to LocalIndexRoot to construct the
/// full path and build the final URI.
class Marshaller {
public:
  Marshaller() = delete;
  Marshaller(llvm::StringRef RemoteIndexRoot, llvm::StringRef LocalIndexRoot);

  llvm::Expected<clangd::Symbol> fromProtobuf(const Symbol &Message);
  llvm::Expected<clangd::Ref> fromProtobuf(const Ref &Message);
  llvm::Expected<std::pair<clangd::SymbolID, clangd::Symbol>>
  fromProtobuf(const Relation &Message);

  llvm::Expected<clangd::LookupRequest>
  fromProtobuf(const LookupRequest *Message);
  llvm::Expected<clangd::FuzzyFindRequest>
  fromProtobuf(const FuzzyFindRequest *Message);
  llvm::Expected<clangd::RefsRequest> fromProtobuf(const RefsRequest *Message);
  llvm::Expected<clangd::RelationsRequest>
  fromProtobuf(const 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);

  llvm::Expected<Symbol> toProtobuf(const clangd::Symbol &From);
  llvm::Expected<Ref> toProtobuf(const clangd::Ref &From);
  llvm::Expected<Relation> 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
  /// \p RelativePath received from remote index server and \p IndexRoot is
  /// provided by the client.
  ///
  /// The relative path passed over the wire has unix slashes.
  llvm::Expected<std::string> relativePathToURI(llvm::StringRef RelativePath);
  /// Translates a URI from the server's backing index to a relative path
  /// suitable to send over the wire to the client.
  llvm::Expected<std::string> 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);
  llvm::Expected<clangd::SymbolLocation>
  fromProtobuf(const SymbolLocation &Message);
  llvm::Expected<SymbolLocation>
  toProtobuf(const clangd::SymbolLocation &Location);
  llvm::Expected<HeaderWithReferences>
  toProtobuf(const clangd::Symbol::IncludeHeaderWithReferences &IncludeHeader);
  llvm::Expected<clangd::Symbol::IncludeHeaderWithReferences>
  fromProtobuf(const HeaderWithReferences &Message);

  /// RemoteIndexRoot and LocalIndexRoot are absolute paths to the project (on
  /// remote and local machine respectively) and include a trailing slash. One
  /// of them can be missing (if the machines are different they don't know each
  /// other's specifics and will only do one-way translation), but both can not
  /// be missing at the same time.
  std::string RemoteIndexRoot;
  std::string LocalIndexRoot;
  llvm::BumpPtrAllocator Arena;
  llvm::UniqueStringSaver Strings;
};

} // namespace remote
} // namespace clangd
} // namespace clang

#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REMOTE_MARSHALLING_H