LLVM 22.0.0git
Transport.h
Go to the documentation of this file.
1//===--- Transport.h - Sending and Receiving LSP messages -------*- 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// The language server protocol is usually implemented by writing messages as
10// JSON-RPC over the stdin/stdout of a subprocess. This file contains a JSON
11// transport interface that handles this communication.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_SUPPORT_LSP_TRANSPORT_H
16#define LLVM_SUPPORT_LSP_TRANSPORT_H
17
19#include "llvm/ADT/StringMap.h"
20#include "llvm/ADT/StringRef.h"
23#include "llvm/Support/JSON.h"
27#include <memory>
28
29namespace llvm {
30// Simple helper function that returns a string as printed from a op.
31template <typename T> static std::string debugString(T &&Op) {
32 std::string InstrStr;
33 llvm::raw_string_ostream Os(InstrStr);
34 Os << Op;
35 return Os.str();
36}
37namespace lsp {
38class MessageHandler;
39
40//===----------------------------------------------------------------------===//
41// JSONTransport
42//===----------------------------------------------------------------------===//
43
44/// The encoding style of the JSON-RPC messages (both input and output).
46 /// Encoding per the LSP specification, with mandatory Content-Length header.
48 /// Messages are delimited by a '// -----' line. Comment lines start with //.
50};
51
52/// An abstract class used by the JSONTransport to read JSON message.
54public:
56 : Style(Style) {}
57 virtual ~JSONTransportInput() = default;
58
59 virtual bool hasError() const = 0;
60 virtual bool isEndOfInput() const = 0;
61
62 /// Read in a message from the input stream.
63 LogicalResult readMessage(std::string &Json) {
65 : readStandardMessage(Json);
66 }
67 virtual LogicalResult readDelimitedMessage(std::string &Json) = 0;
68 virtual LogicalResult readStandardMessage(std::string &Json) = 0;
69
70private:
71 /// The JSON stream style to use.
72 JSONStreamStyle Style;
73};
74
75/// Concrete implementation of the JSONTransportInput that reads from a file.
77public:
79 std::FILE *In, JSONStreamStyle Style = JSONStreamStyle::Standard)
80 : JSONTransportInput(Style), In(In) {}
81
82 bool hasError() const final { return ferror(In); }
83 bool isEndOfInput() const final { return feof(In); }
84
87
88private:
89 std::FILE *In;
90};
91
92/// A transport class that performs the JSON-RPC communication with the LSP
93/// client.
95public:
96 JSONTransport(std::unique_ptr<JSONTransportInput> In, raw_ostream &Out,
97 bool PrettyOutput = false)
98 : In(std::move(In)), Out(Out), PrettyOutput(PrettyOutput) {}
99
100 JSONTransport(std::FILE *In, raw_ostream &Out,
102 bool PrettyOutput = false)
103 : In(std::make_unique<JSONTransportInputOverFile>(In, Style)), Out(Out),
104 PrettyOutput(PrettyOutput) {}
105
106 /// The following methods are used to send a message to the LSP client.
112
113 /// Start executing the JSON-RPC transport.
115
116private:
117 /// Dispatches the given incoming json message to the message handler.
118 bool handleMessage(llvm::json::Value Msg, MessageHandler &Handler);
119 /// Writes the given message to the output stream.
120 void sendMessage(llvm::json::Value Msg);
121
122private:
123 /// The input to read a message from.
124 std::unique_ptr<JSONTransportInput> In;
126 /// The output file stream.
127 raw_ostream &Out;
128 /// If the output JSON should be formatted for easier readability.
129 bool PrettyOutput;
130};
131
132//===----------------------------------------------------------------------===//
133// MessageHandler
134//===----------------------------------------------------------------------===//
135
136/// A Callback<T> is a void function that accepts Expected<T>. This is
137/// accepted by functions that logically return T.
138template <typename T>
140
141/// An OutgoingNotification<T> is a function used for outgoing notifications
142/// send to the client.
143template <typename T>
145
146/// An OutgoingRequest<T> is a function used for outgoing requests to send to
147/// the client.
148template <typename T>
150 llvm::unique_function<void(const T &, llvm::json::Value Id)>;
151
152/// An `OutgoingRequestCallback` is invoked when an outgoing request to the
153/// client receives a response in turn. It is passed the original request's ID,
154/// as well as the response result.
155template <typename T>
157 std::function<void(llvm::json::Value, llvm::Expected<T>)>;
158
159/// A handler used to process the incoming transport messages.
161public:
162 MessageHandler(JSONTransport &Transport) : Transport(Transport) {}
163
167
168 template <typename T>
170 StringRef PayloadName, StringRef PayloadKind) {
171 T Result;
173 if (fromJSON(Raw, Result, Root))
174 return std::move(Result);
175
176 // Dump the relevant parts of the broken message.
177 std::string Context;
178 llvm::raw_string_ostream Os(Context);
179 Root.printErrorContext(Raw, Os);
180
181 // Report the error (e.g. to the client).
183 llvm::formatv("failed to decode {0} {1}: {2}", PayloadName, PayloadKind,
184 fmt_consume(Root.getError())),
186 }
187
188 template <typename Param, typename Result, typename ThisT>
189 void method(llvm::StringLiteral Method, ThisT *ThisPtr,
190 void (ThisT::*Handler)(const Param &, Callback<Result>)) {
191 MethodHandlers[Method] = [Method, Handler,
192 ThisPtr](llvm::json::Value RawParams,
195 parse<Param>(RawParams, Method, "request");
196 if (!Parameter)
197 return Reply(Parameter.takeError());
198 (ThisPtr->*Handler)(*Parameter, std::move(Reply));
199 };
200 }
201
202 template <typename Param, typename ThisT>
204 void (ThisT::*Handler)(const Param &)) {
205 NotificationHandlers[Method] = [Method, Handler,
206 ThisPtr](llvm::json::Value RawParams) {
208 parse<Param>(RawParams, Method, "notification");
209 if (!Parameter) {
211 Parameter.takeError(), [](const LSPError &LspError) {
212 Logger::error("JSON parsing error: {0}",
213 LspError.message.c_str());
214 }));
215 }
216 (ThisPtr->*Handler)(*Parameter);
217 };
218 }
219
220 /// Create an OutgoingNotification object used for the given method.
221 template <typename T>
223 return [&, Method](const T &Params) {
224 std::lock_guard<std::mutex> TransportLock(TransportOutputMutex);
225 Logger::info("--> {0}", Method);
226 Transport.notify(Method, llvm::json::Value(Params));
227 };
228 }
229
230 /// Create an OutgoingRequest function that, when called, sends a request with
231 /// the given method via the transport. Should the outgoing request be
232 /// met with a response, the result JSON is parsed and the response callback
233 /// is invoked.
234 template <typename Param, typename Result>
238 return [&, Method, Callback](const Param &Parameter, llvm::json::Value Id) {
239 auto CallbackWrapper = [Method, Callback = std::move(Callback)](
242 if (!Value)
243 return Callback(std::move(Id), Value.takeError());
244
245 std::string ResponseName = llvm::formatv("reply:{0}({1})", Method, Id);
247 parse<Result>(*Value, ResponseName, "response");
248 if (!ParseResult)
249 return Callback(std::move(Id), ParseResult.takeError());
250
251 return Callback(std::move(Id), *ParseResult);
252 };
253
254 {
255 std::lock_guard<std::mutex> Lock(ResponseHandlersMutex);
256 ResponseHandlers.insert(
257 {debugString(Id), std::make_pair(Method.str(), CallbackWrapper)});
258 }
259
260 std::lock_guard<std::mutex> TransportLock(TransportOutputMutex);
261 Logger::info("--> {0}({1})", Method, Id);
262 Transport.call(Method, llvm::json::Value(Parameter), Id);
263 };
264 }
265
266private:
267 template <typename HandlerT>
269
270 HandlerMap<void(llvm::json::Value)> NotificationHandlers;
272 MethodHandlers;
273
274 /// A pair of (1) the original request's method name, and (2) the callback
275 /// function to be invoked for responses.
276 using ResponseHandlerTy =
277 std::pair<std::string, OutgoingRequestCallback<llvm::json::Value>>;
278 /// A mapping from request/response ID to response handler.
279 llvm::StringMap<ResponseHandlerTy> ResponseHandlers;
280 /// Mutex to guard insertion into the response handler map.
281 std::mutex ResponseHandlersMutex;
282
283 JSONTransport &Transport;
284
285 /// Mutex to guard sending output messages to the transport.
286 std::mutex TransportOutputMutex;
287};
288
289} // namespace lsp
290} // namespace llvm
291
292#endif
aarch64 promote const
This file defines the StringMap class.
#define LLVM_ABI_FOR_TEST
Definition Compiler.h:218
This file provides a collection of function (or more generally, callable) type erasure utilities supp...
This file supports working with JSON data.
#define T
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
This class represents success/failure for parsing-like operations that find it important to chain tog...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition StringRef.h:854
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
The root is the trivial Path to the root value.
Definition JSON.h:713
LLVM_ABI void printErrorContext(const Value &, llvm::raw_ostream &) const
Print the root value with the error shown inline as a comment.
Definition JSON.cpp:300
LLVM_ABI Error getError() const
Returns the last error reported, or else a generic error.
Definition JSON.cpp:219
A Value is an JSON value of unknown type.
Definition JSON.h:290
Concrete implementation of the JSONTransportInput that reads from a file.
Definition Transport.h:76
LLVM_ABI_FOR_TEST LogicalResult readStandardMessage(std::string &Json) final
JSONTransportInputOverFile(std::FILE *In, JSONStreamStyle Style=JSONStreamStyle::Standard)
Definition Transport.h:78
LLVM_ABI_FOR_TEST LogicalResult readDelimitedMessage(std::string &Json) final
For lit tests we support a simplified syntax:
virtual bool hasError() const =0
virtual bool isEndOfInput() const =0
virtual LogicalResult readDelimitedMessage(std::string &Json)=0
LogicalResult readMessage(std::string &Json)
Read in a message from the input stream.
Definition Transport.h:63
virtual ~JSONTransportInput()=default
virtual LogicalResult readStandardMessage(std::string &Json)=0
JSONTransportInput(JSONStreamStyle Style=JSONStreamStyle::Standard)
Definition Transport.h:55
A transport class that performs the JSON-RPC communication with the LSP client.
Definition Transport.h:94
LLVM_ABI_FOR_TEST void reply(llvm::json::Value Id, llvm::Expected< llvm::json::Value > Result)
LLVM_ABI_FOR_TEST llvm::Error run(MessageHandler &Handler)
Start executing the JSON-RPC transport.
LLVM_ABI_FOR_TEST void notify(StringRef Method, llvm::json::Value Params)
The following methods are used to send a message to the LSP client.
JSONTransport(std::unique_ptr< JSONTransportInput > In, raw_ostream &Out, bool PrettyOutput=false)
Definition Transport.h:96
LLVM_ABI_FOR_TEST void call(StringRef Method, llvm::json::Value Params, llvm::json::Value Id)
JSONTransport(std::FILE *In, raw_ostream &Out, JSONStreamStyle Style=JSONStreamStyle::Standard, bool PrettyOutput=false)
Definition Transport.h:100
This class models an LSP error as an llvm::Error.
Definition Protocol.h:80
static void info(const char *Fmt, Ts &&...Vals)
Definition Logging.h:34
A handler used to process the incoming transport messages.
Definition Transport.h:160
OutgoingRequest< Param > outgoingRequest(llvm::StringLiteral Method, OutgoingRequestCallback< Result > Callback)
Create an OutgoingRequest function that, when called, sends a request with the given method via the t...
Definition Transport.h:236
void notification(llvm::StringLiteral Method, ThisT *ThisPtr, void(ThisT::*Handler)(const Param &))
Definition Transport.h:203
OutgoingNotification< T > outgoingNotification(llvm::StringLiteral Method)
Create an OutgoingNotification object used for the given method.
Definition Transport.h:222
bool onNotify(StringRef Method, llvm::json::Value Value)
Definition Transport.cpp:86
void method(llvm::StringLiteral Method, ThisT *ThisPtr, void(ThisT::*Handler)(const Param &, Callback< Result >))
Definition Transport.h:189
MessageHandler(JSONTransport &Transport)
Definition Transport.h:162
static llvm::Expected< T > parse(const llvm::json::Value &Raw, StringRef PayloadName, StringRef PayloadKind)
Definition Transport.h:169
bool onCall(StringRef Method, llvm::json::Value Params, llvm::json::Value Id)
bool onReply(llvm::json::Value Id, llvm::Expected< llvm::json::Value > Result)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
std::string & str()
Returns the string's reference.
unique_function is a type-erasing functor similar to std::function.
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Definition Transport.h:139
llvm::unique_function< void(const T &, llvm::json::Value Id)> OutgoingRequest
An OutgoingRequest<T> is a function used for outgoing requests to send to the client.
Definition Transport.h:149
@ Parameter
An inlay hint that is for a parameter.
Definition Protocol.h:1128
std::function< void(llvm::json::Value, llvm::Expected< T >)> OutgoingRequestCallback
An OutgoingRequestCallback is invoked when an outgoing request to the client receives a response in t...
Definition Transport.h:156
LLVM_ABI_FOR_TEST bool fromJSON(const llvm::json::Value &value, URIForFile &result, llvm::json::Path path)
Definition Protocol.cpp:238
JSONStreamStyle
The encoding style of the JSON-RPC messages (both input and output).
Definition Transport.h:45
@ Standard
Encoding per the LSP specification, with mandatory Content-Length header.
Definition Transport.h:47
@ Delimited
Messages are delimited by a '// --—' line. Comment lines start with //.
Definition Transport.h:49
llvm::unique_function< void(const T &)> OutgoingNotification
An OutgoingNotification<T> is a function used for outgoing notifications send to the client.
Definition Transport.h:144
This is an optimization pass for GlobalISel generic memory operations.
support::detail::ErrorAdapter fmt_consume(Error &&Item)
Error handleErrors(Error E, HandlerTs &&... Hs)
Pass the ErrorInfo(s) contained in E to their respective handlers.
Definition Error.h:967
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
static std::string debugString(T &&Op)
Definition Transport.h:31
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
DWARFExpression::Operation Op
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1867
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1083
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:867
This class represents an efficient way to signal success or failure.