[AMDGPU][AsmParser][NFC] Translate parsed MIMG instructions to MCInsts automatically.
[llvm-project.git] / clang-tools-extra / clangd / xpc / XPCTransport.cpp
blob8f5b787f8f71039a00857c22041aac7920320a23
1 //===--- XPCTransport.cpp - sending and receiving LSP messages over XPC ---===//
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 #include "Conversion.h"
9 #include "Protocol.h" // For LSPError
10 #include "Transport.h"
11 #include "support/Logger.h"
12 #include "llvm/Support/Errno.h"
14 #include <optional>
15 #include <xpc/xpc.h>
17 using namespace llvm;
18 using namespace clang;
19 using namespace clangd;
21 namespace {
23 json::Object encodeError(Error E) {
24 std::string Message;
25 ErrorCode Code = ErrorCode::UnknownErrorCode;
26 if (Error Unhandled =
27 handleErrors(std::move(E), [&](const LSPError &L) -> Error {
28 Message = L.Message;
29 Code = L.Code;
30 return Error::success();
31 }))
32 Message = toString(std::move(Unhandled));
34 return json::Object{
35 {"message", std::move(Message)},
36 {"code", int64_t(Code)},
40 Error decodeError(const json::Object &O) {
41 std::string Msg =
42 std::string(O.getString("message").value_or("Unspecified error"));
43 if (auto Code = O.getInteger("code"))
44 return make_error<LSPError>(std::move(Msg), ErrorCode(*Code));
45 return error("{0}", Msg);
48 // C "closure" for XPCTransport::loop() method
49 namespace xpcClosure {
50 void connection_handler(xpc_connection_t clientConnection);
51 } // namespace xpcClosure
53 class XPCTransport : public Transport {
54 public:
55 XPCTransport() {}
57 void notify(StringRef Method, json::Value Params) override {
58 sendMessage(json::Object{
59 {"jsonrpc", "2.0"},
60 {"method", Method},
61 {"params", std::move(Params)},
62 });
64 void call(StringRef Method, json::Value Params, json::Value ID) override {
65 sendMessage(json::Object{
66 {"jsonrpc", "2.0"},
67 {"id", std::move(ID)},
68 {"method", Method},
69 {"params", std::move(Params)},
70 });
72 void reply(json::Value ID, Expected<json::Value> Result) override {
73 if (Result) {
74 sendMessage(json::Object{
75 {"jsonrpc", "2.0"},
76 {"id", std::move(ID)},
77 {"result", std::move(*Result)},
78 });
79 } else {
80 sendMessage(json::Object{
81 {"jsonrpc", "2.0"},
82 {"id", std::move(ID)},
83 {"error", encodeError(Result.takeError())},
84 });
88 Error loop(MessageHandler &Handler) override;
90 private:
91 // Needs access to handleMessage() and resetClientConnection()
92 friend void xpcClosure::connection_handler(xpc_connection_t clientConnection);
94 // Dispatches incoming message to Handler onNotify/onCall/onReply.
95 bool handleMessage(json::Value Message, MessageHandler &Handler);
96 void sendMessage(json::Value Message) {
97 xpc_object_t response = jsonToXpc(Message);
98 xpc_connection_send_message(clientConnection, response);
99 xpc_release(response);
101 void resetClientConnection(xpc_connection_t newClientConnection) {
102 clientConnection = newClientConnection;
104 xpc_connection_t clientConnection;
107 bool XPCTransport::handleMessage(json::Value Message, MessageHandler &Handler) {
108 // Message must be an object with "jsonrpc":"2.0".
109 auto *Object = Message.getAsObject();
110 if (!Object ||
111 Object->getString("jsonrpc") != std::optional<StringRef>("2.0")) {
112 elog("Not a JSON-RPC 2.0 message: {0:2}", Message);
113 return false;
115 // ID may be any JSON value. If absent, this is a notification.
116 std::optional<json::Value> ID;
117 if (auto *I = Object->get("id"))
118 ID = std::move(*I);
119 auto Method = Object->getString("method");
120 if (!Method) { // This is a response.
121 if (!ID) {
122 elog("No method and no response ID: {0:2}", Message);
123 return false;
125 if (auto *Err = Object->getObject("error"))
126 return Handler.onReply(std::move(*ID), decodeError(*Err));
127 // Result should be given, use null if not.
128 json::Value Result = nullptr;
129 if (auto *R = Object->get("result"))
130 Result = std::move(*R);
131 return Handler.onReply(std::move(*ID), std::move(Result));
133 // Params should be given, use null if not.
134 json::Value Params = nullptr;
135 if (auto *P = Object->get("params"))
136 Params = std::move(*P);
138 if (ID)
139 return Handler.onCall(*Method, std::move(Params), std::move(*ID));
140 else
141 return Handler.onNotify(*Method, std::move(Params));
144 namespace xpcClosure {
145 // "owner" of this "closure object" - necessary for propagating connection to
146 // XPCTransport so it can send messages to the client.
147 XPCTransport *TransportObject = nullptr;
148 Transport::MessageHandler *HandlerPtr = nullptr;
150 void connection_handler(xpc_connection_t clientConnection) {
151 xpc_connection_set_target_queue(clientConnection, dispatch_get_main_queue());
153 xpc_transaction_begin();
155 TransportObject->resetClientConnection(clientConnection);
157 xpc_connection_set_event_handler(clientConnection, ^(xpc_object_t message) {
158 if (message == XPC_ERROR_CONNECTION_INVALID) {
159 // connection is being terminated
160 log("Received XPC_ERROR_CONNECTION_INVALID message - returning from the "
161 "event_handler.");
162 return;
165 if (xpc_get_type(message) != XPC_TYPE_DICTIONARY) {
166 log("Received XPC message of unknown type - returning from the "
167 "event_handler.");
168 return;
171 const json::Value Doc = xpcToJson(message);
172 if (Doc == json::Value(nullptr)) {
173 log("XPC message was converted to Null JSON message - returning from the "
174 "event_handler.");
175 return;
178 vlog("<<< {0}\n", Doc);
180 if (!TransportObject->handleMessage(std::move(Doc), *HandlerPtr)) {
181 log("Received exit notification - cancelling connection.");
182 xpc_connection_cancel(xpc_dictionary_get_remote_connection(message));
183 xpc_transaction_end();
187 xpc_connection_resume(clientConnection);
189 } // namespace xpcClosure
191 Error XPCTransport::loop(MessageHandler &Handler) {
192 assert(xpcClosure::TransportObject == nullptr &&
193 "TransportObject has already been set.");
194 // This looks scary since lifetime of this (or any) XPCTransport object has
195 // to fully contain lifetime of any XPC connection. In practise any Transport
196 // object is destroyed only at the end of main() which is always after
197 // exit of xpc_main().
198 xpcClosure::TransportObject = this;
200 assert(xpcClosure::HandlerPtr == nullptr &&
201 "HandlerPtr has already been set.");
202 xpcClosure::HandlerPtr = &Handler;
204 xpc_main(xpcClosure::connection_handler);
205 // xpc_main doesn't ever return
206 return errorCodeToError(std::make_error_code(std::errc::io_error));
209 } // namespace
211 namespace clang {
212 namespace clangd {
214 std::unique_ptr<Transport> newXPCTransport() {
215 return std::make_unique<XPCTransport>();
218 } // namespace clangd
219 } // namespace clang