Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 1 | //===--- JSONTransport.cpp - sending and receiving LSP messages over JSON -===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 8 | #include "Protocol.h" // For LSPError |
| 9 | #include "Transport.h" |
Sam McCall | ad97ccf | 2020-04-28 17:49:17 +0200 | [diff] [blame] | 10 | #include "support/Cancellation.h" |
| 11 | #include "support/Logger.h" |
| 12 | #include "support/Shutdown.h" |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Errno.h" |
Sam McCall | 19ac0eaf | 2019-11-25 19:51:07 +0100 | [diff] [blame] | 14 | #include "llvm/Support/Error.h" |
Sam McCall | 687e1d7 | 2020-09-14 11:33:12 +0200 | [diff] [blame] | 15 | #include <system_error> |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 16 | |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 17 | namespace clang { |
| 18 | namespace clangd { |
| 19 | namespace { |
| 20 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 21 | llvm::json::Object encodeError(llvm::Error E) { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 22 | std::string Message; |
| 23 | ErrorCode Code = ErrorCode::UnknownErrorCode; |
Sam McCall | c627b12 | 2020-03-04 14:04:17 +0100 | [diff] [blame] | 24 | // FIXME: encode cancellation errors using RequestCancelled or ContentModified |
| 25 | // as appropriate. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 26 | if (llvm::Error Unhandled = llvm::handleErrors( |
Sam McCall | 31db1e0 | 2020-04-11 18:19:50 +0200 | [diff] [blame] | 27 | std::move(E), |
| 28 | [&](const CancelledError &C) -> llvm::Error { |
| 29 | switch (C.Reason) { |
| 30 | case static_cast<int>(ErrorCode::ContentModified): |
| 31 | Code = ErrorCode::ContentModified; |
| 32 | Message = "Request cancelled because the document was modified"; |
| 33 | break; |
| 34 | default: |
| 35 | Code = ErrorCode::RequestCancelled; |
| 36 | Message = "Request cancelled"; |
| 37 | break; |
| 38 | } |
| 39 | return llvm::Error::success(); |
| 40 | }, |
| 41 | [&](const LSPError &L) -> llvm::Error { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 42 | Message = L.Message; |
| 43 | Code = L.Code; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 44 | return llvm::Error::success(); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 45 | })) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 46 | Message = llvm::toString(std::move(Unhandled)); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 47 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 48 | return llvm::json::Object{ |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 49 | {"message", std::move(Message)}, |
| 50 | {"code", int64_t(Code)}, |
| 51 | }; |
| 52 | } |
| 53 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 54 | llvm::Error decodeError(const llvm::json::Object &O) { |
Sam McCall | 30667c9 | 2020-07-08 21:49:38 +0200 | [diff] [blame] | 55 | llvm::StringRef Msg = O.getString("message").getValueOr("Unspecified error"); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 56 | if (auto Code = O.getInteger("code")) |
Sam McCall | 30667c9 | 2020-07-08 21:49:38 +0200 | [diff] [blame] | 57 | return llvm::make_error<LSPError>(Msg.str(), ErrorCode(*Code)); |
| 58 | return error(Msg.str()); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | class JSONTransport : public Transport { |
| 62 | public: |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 63 | JSONTransport(std::FILE *In, llvm::raw_ostream &Out, |
| 64 | llvm::raw_ostream *InMirror, bool Pretty, JSONStreamStyle Style) |
| 65 | : In(In), Out(Out), InMirror(InMirror ? *InMirror : llvm::nulls()), |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 66 | Pretty(Pretty), Style(Style) {} |
| 67 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 68 | void notify(llvm::StringRef Method, llvm::json::Value Params) override { |
| 69 | sendMessage(llvm::json::Object{ |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 70 | {"jsonrpc", "2.0"}, |
| 71 | {"method", Method}, |
| 72 | {"params", std::move(Params)}, |
| 73 | }); |
| 74 | } |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 75 | void call(llvm::StringRef Method, llvm::json::Value Params, |
| 76 | llvm::json::Value ID) override { |
| 77 | sendMessage(llvm::json::Object{ |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 78 | {"jsonrpc", "2.0"}, |
| 79 | {"id", std::move(ID)}, |
| 80 | {"method", Method}, |
| 81 | {"params", std::move(Params)}, |
| 82 | }); |
| 83 | } |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 84 | void reply(llvm::json::Value ID, |
| 85 | llvm::Expected<llvm::json::Value> Result) override { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 86 | if (Result) { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 87 | sendMessage(llvm::json::Object{ |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 88 | {"jsonrpc", "2.0"}, |
| 89 | {"id", std::move(ID)}, |
| 90 | {"result", std::move(*Result)}, |
| 91 | }); |
| 92 | } else { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 93 | sendMessage(llvm::json::Object{ |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 94 | {"jsonrpc", "2.0"}, |
| 95 | {"id", std::move(ID)}, |
| 96 | {"error", encodeError(Result.takeError())}, |
| 97 | }); |
| 98 | } |
| 99 | } |
| 100 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 101 | llvm::Error loop(MessageHandler &Handler) override { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 102 | while (!feof(In)) { |
Sam McCall | 19ac0eaf | 2019-11-25 19:51:07 +0100 | [diff] [blame] | 103 | if (shutdownRequested()) |
Sam McCall | 687e1d7 | 2020-09-14 11:33:12 +0200 | [diff] [blame] | 104 | return error(std::make_error_code(std::errc::operation_canceled), |
| 105 | "Got signal, shutting down"); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 106 | if (ferror(In)) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 107 | return llvm::errorCodeToError( |
| 108 | std::error_code(errno, std::system_category())); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 109 | if (auto JSON = readRawMessage()) { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 110 | if (auto Doc = llvm::json::parse(*JSON)) { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 111 | vlog(Pretty ? "<<< {0:2}\n" : "<<< {0}\n", *Doc); |
| 112 | if (!handleMessage(std::move(*Doc), Handler)) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 113 | return llvm::Error::success(); // we saw the "exit" notification. |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 114 | } else { |
| 115 | // Parse error. Log the raw message. |
| 116 | vlog("<<< {0}\n", *JSON); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 117 | elog("JSON parse error: {0}", llvm::toString(Doc.takeError())); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 121 | return llvm::errorCodeToError(std::make_error_code(std::errc::io_error)); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | private: |
| 125 | // Dispatches incoming message to Handler onNotify/onCall/onReply. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 126 | bool handleMessage(llvm::json::Value Message, MessageHandler &Handler); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 127 | // Writes outgoing message to Out stream. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 128 | void sendMessage(llvm::json::Value Message) { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 129 | std::string S; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 130 | llvm::raw_string_ostream OS(S); |
| 131 | OS << llvm::formatv(Pretty ? "{0:2}" : "{0}", Message); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 132 | OS.flush(); |
| 133 | Out << "Content-Length: " << S.size() << "\r\n\r\n" << S; |
| 134 | Out.flush(); |
| 135 | vlog(">>> {0}\n", S); |
| 136 | } |
| 137 | |
| 138 | // Read raw string messages from input stream. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 139 | llvm::Optional<std::string> readRawMessage() { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 140 | return Style == JSONStreamStyle::Delimited ? readDelimitedMessage() |
| 141 | : readStandardMessage(); |
| 142 | } |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 143 | llvm::Optional<std::string> readDelimitedMessage(); |
| 144 | llvm::Optional<std::string> readStandardMessage(); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 145 | |
| 146 | std::FILE *In; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 147 | llvm::raw_ostream &Out; |
| 148 | llvm::raw_ostream &InMirror; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 149 | bool Pretty; |
| 150 | JSONStreamStyle Style; |
| 151 | }; |
| 152 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 153 | bool JSONTransport::handleMessage(llvm::json::Value Message, |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 154 | MessageHandler &Handler) { |
| 155 | // Message must be an object with "jsonrpc":"2.0". |
| 156 | auto *Object = Message.getAsObject(); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 157 | if (!Object || |
| 158 | Object->getString("jsonrpc") != llvm::Optional<llvm::StringRef>("2.0")) { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 159 | elog("Not a JSON-RPC 2.0 message: {0:2}", Message); |
| 160 | return false; |
| 161 | } |
| 162 | // ID may be any JSON value. If absent, this is a notification. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 163 | llvm::Optional<llvm::json::Value> ID; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 164 | if (auto *I = Object->get("id")) |
| 165 | ID = std::move(*I); |
| 166 | auto Method = Object->getString("method"); |
| 167 | if (!Method) { // This is a response. |
| 168 | if (!ID) { |
| 169 | elog("No method and no response ID: {0:2}", Message); |
| 170 | return false; |
| 171 | } |
| 172 | if (auto *Err = Object->getObject("error")) |
| 173 | return Handler.onReply(std::move(*ID), decodeError(*Err)); |
| 174 | // Result should be given, use null if not. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 175 | llvm::json::Value Result = nullptr; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 176 | if (auto *R = Object->get("result")) |
| 177 | Result = std::move(*R); |
| 178 | return Handler.onReply(std::move(*ID), std::move(Result)); |
| 179 | } |
| 180 | // Params should be given, use null if not. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 181 | llvm::json::Value Params = nullptr; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 182 | if (auto *P = Object->get("params")) |
| 183 | Params = std::move(*P); |
| 184 | |
| 185 | if (ID) |
| 186 | return Handler.onCall(*Method, std::move(Params), std::move(*ID)); |
| 187 | else |
| 188 | return Handler.onNotify(*Method, std::move(Params)); |
| 189 | } |
| 190 | |
| 191 | // Tries to read a line up to and including \n. |
Sam McCall | 19ac0eaf | 2019-11-25 19:51:07 +0100 | [diff] [blame] | 192 | // If failing, feof(), ferror(), or shutdownRequested() will be set. |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 193 | bool readLine(std::FILE *In, std::string &Out) { |
| 194 | static constexpr int BufSize = 1024; |
| 195 | size_t Size = 0; |
| 196 | Out.clear(); |
| 197 | for (;;) { |
| 198 | Out.resize(Size + BufSize); |
| 199 | // Handle EINTR which is sent when a debugger attaches on some platforms. |
Sam McCall | 19ac0eaf | 2019-11-25 19:51:07 +0100 | [diff] [blame] | 200 | if (!retryAfterSignalUnlessShutdown( |
| 201 | nullptr, [&] { return std::fgets(&Out[Size], BufSize, In); })) |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 202 | return false; |
| 203 | clearerr(In); |
| 204 | // If the line contained null bytes, anything after it (including \n) will |
| 205 | // be ignored. Fortunately this is not a legal header or JSON. |
| 206 | size_t Read = std::strlen(&Out[Size]); |
| 207 | if (Read > 0 && Out[Size + Read - 1] == '\n') { |
| 208 | Out.resize(Size + Read); |
| 209 | return true; |
| 210 | } |
| 211 | Size += Read; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // Returns None when: |
Sam McCall | 19ac0eaf | 2019-11-25 19:51:07 +0100 | [diff] [blame] | 216 | // - ferror(), feof(), or shutdownRequested() are set. |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 217 | // - Content-Length is missing or empty (protocol error) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 218 | llvm::Optional<std::string> JSONTransport::readStandardMessage() { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 219 | // A Language Server Protocol message starts with a set of HTTP headers, |
| 220 | // delimited by \r\n, and terminated by an empty line (\r\n). |
| 221 | unsigned long long ContentLength = 0; |
| 222 | std::string Line; |
| 223 | while (true) { |
| 224 | if (feof(In) || ferror(In) || !readLine(In, Line)) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 225 | return llvm::None; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 226 | InMirror << Line; |
| 227 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 228 | llvm::StringRef LineRef(Line); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 229 | |
| 230 | // We allow comments in headers. Technically this isn't part |
| 231 | |
| 232 | // of the LSP specification, but makes writing tests easier. |
| 233 | if (LineRef.startswith("#")) |
| 234 | continue; |
| 235 | |
| 236 | // Content-Length is a mandatory header, and the only one we handle. |
| 237 | if (LineRef.consume_front("Content-Length: ")) { |
| 238 | if (ContentLength != 0) { |
| 239 | elog("Warning: Duplicate Content-Length header received. " |
| 240 | "The previous value for this message ({0}) was ignored.", |
| 241 | ContentLength); |
| 242 | } |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 243 | llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 244 | continue; |
| 245 | } else if (!LineRef.trim().empty()) { |
| 246 | // It's another header, ignore it. |
| 247 | continue; |
| 248 | } else { |
| 249 | // An empty line indicates the end of headers. |
| 250 | // Go ahead and read the JSON. |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // The fuzzer likes crashing us by sending "Content-Length: 9999999999999999" |
| 256 | if (ContentLength > 1 << 30) { // 1024M |
| 257 | elog("Refusing to read message with long Content-Length: {0}. " |
| 258 | "Expect protocol errors", |
| 259 | ContentLength); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 260 | return llvm::None; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 261 | } |
| 262 | if (ContentLength == 0) { |
| 263 | log("Warning: Missing Content-Length header, or zero-length message."); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 264 | return llvm::None; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | std::string JSON(ContentLength, '\0'); |
| 268 | for (size_t Pos = 0, Read; Pos < ContentLength; Pos += Read) { |
| 269 | // Handle EINTR which is sent when a debugger attaches on some platforms. |
Sam McCall | 19ac0eaf | 2019-11-25 19:51:07 +0100 | [diff] [blame] | 270 | Read = retryAfterSignalUnlessShutdown(0, [&]{ |
| 271 | return std::fread(&JSON[Pos], 1, ContentLength - Pos, In); |
| 272 | }); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 273 | if (Read == 0) { |
| 274 | elog("Input was aborted. Read only {0} bytes of expected {1}.", Pos, |
| 275 | ContentLength); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 276 | return llvm::None; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 277 | } |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 278 | InMirror << llvm::StringRef(&JSON[Pos], Read); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 279 | clearerr(In); // If we're done, the error was transient. If we're not done, |
| 280 | // either it was transient or we'll see it again on retry. |
| 281 | Pos += Read; |
| 282 | } |
| 283 | return std::move(JSON); |
| 284 | } |
| 285 | |
| 286 | // For lit tests we support a simplified syntax: |
| 287 | // - messages are delimited by '---' on a line by itself |
| 288 | // - lines starting with # are ignored. |
| 289 | // This is a testing path, so favor simplicity over performance here. |
Sam McCall | 19ac0eaf | 2019-11-25 19:51:07 +0100 | [diff] [blame] | 290 | // When returning None, feof(), ferror(), or shutdownRequested() will be set. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 291 | llvm::Optional<std::string> JSONTransport::readDelimitedMessage() { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 292 | std::string JSON; |
| 293 | std::string Line; |
| 294 | while (readLine(In, Line)) { |
| 295 | InMirror << Line; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 296 | auto LineRef = llvm::StringRef(Line).trim(); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 297 | if (LineRef.startswith("#")) // comment |
| 298 | continue; |
| 299 | |
| 300 | // found a delimiter |
| 301 | if (LineRef.rtrim() == "---") |
| 302 | break; |
| 303 | |
| 304 | JSON += Line; |
| 305 | } |
| 306 | |
Sam McCall | 19ac0eaf | 2019-11-25 19:51:07 +0100 | [diff] [blame] | 307 | if (shutdownRequested()) |
| 308 | return llvm::None; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 309 | if (ferror(In)) { |
| 310 | elog("Input error while reading message!"); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 311 | return llvm::None; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 312 | } |
| 313 | return std::move(JSON); // Including at EOF |
| 314 | } |
| 315 | |
| 316 | } // namespace |
| 317 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 318 | std::unique_ptr<Transport> newJSONTransport(std::FILE *In, |
| 319 | llvm::raw_ostream &Out, |
| 320 | llvm::raw_ostream *InMirror, |
| 321 | bool Pretty, |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 322 | JSONStreamStyle Style) { |
Jonas Devlieghere | 1c705d9 | 2019-08-14 23:52:23 +0000 | [diff] [blame] | 323 | return std::make_unique<JSONTransport>(In, Out, InMirror, Pretty, Style); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | } // namespace clangd |
| 327 | } // namespace clang |