12 #include "llvm/Support/Errno.h" 18 llvm::json::Object encodeError(llvm::Error E) {
22 std::move(E), [&](
const LSPError &L) -> llvm::Error {
25 return llvm::Error::success();
29 return llvm::json::Object{
30 {
"message", std::move(Message)},
31 {
"code", int64_t(Code)},
35 llvm::Error decodeError(
const llvm::json::Object &O) {
36 std::string Msg = O.getString(
"message").getValueOr(
"Unspecified error");
37 if (
auto Code = O.getInteger(
"code"))
38 return llvm::make_error<LSPError>(std::move(Msg),
ErrorCode(*Code));
39 return llvm::make_error<llvm::StringError>(std::move(Msg),
40 llvm::inconvertibleErrorCode());
43 class JSONTransport :
public Transport {
45 JSONTransport(std::FILE *In, llvm::raw_ostream &Out,
47 : In(In), Out(Out), InMirror(InMirror ? *InMirror :
llvm::nulls()),
48 Pretty(Pretty), Style(Style) {}
50 void notify(llvm::StringRef
Method, llvm::json::Value Params)
override {
51 sendMessage(llvm::json::Object{
54 {
"params", std::move(Params)},
57 void call(llvm::StringRef Method, llvm::json::Value Params,
58 llvm::json::Value ID)
override {
59 sendMessage(llvm::json::Object{
61 {
"id", std::move(ID)},
63 {
"params", std::move(Params)},
66 void reply(llvm::json::Value ID,
67 llvm::Expected<llvm::json::Value>
Result)
override {
69 sendMessage(llvm::json::Object{
71 {
"id", std::move(ID)},
72 {
"result", std::move(*Result)},
75 sendMessage(llvm::json::Object{
77 {
"id", std::move(ID)},
78 {
"error", encodeError(Result.takeError())},
83 llvm::Error loop(MessageHandler &Handler)
override {
86 return llvm::errorCodeToError(
87 std::error_code(errno, std::system_category()));
88 if (
auto JSON = readRawMessage()) {
89 if (
auto Doc = llvm::json::parse(*JSON)) {
90 vlog(Pretty ?
"<<< {0:2}\n" :
"<<< {0}\n", *Doc);
91 if (!handleMessage(std::move(*Doc), Handler))
92 return llvm::Error::success();
95 vlog(
"<<< {0}\n", *JSON);
100 return llvm::errorCodeToError(std::make_error_code(std::errc::io_error));
105 bool handleMessage(llvm::json::Value Message, MessageHandler &Handler);
107 void sendMessage(llvm::json::Value Message) {
109 llvm::raw_string_ostream OS(S);
110 OS << llvm::formatv(Pretty ?
"{0:2}" :
"{0}", Message);
112 Out <<
"Content-Length: " << S.size() <<
"\r\n\r\n" << S;
114 vlog(
">>> {0}\n", S);
118 llvm::Optional<std::string> readRawMessage() {
120 : readStandardMessage();
122 llvm::Optional<std::string> readDelimitedMessage();
123 llvm::Optional<std::string> readStandardMessage();
126 llvm::raw_ostream &Out;
127 llvm::raw_ostream &InMirror;
132 bool JSONTransport::handleMessage(llvm::json::Value Message,
133 MessageHandler &Handler) {
135 auto *
Object = Message.getAsObject();
137 Object->getString(
"jsonrpc") != llvm::Optional<llvm::StringRef>(
"2.0")) {
138 elog(
"Not a JSON-RPC 2.0 message: {0:2}", Message);
142 llvm::Optional<llvm::json::Value> ID;
143 if (
auto *I =
Object->get(
"id"))
145 auto Method =
Object->getString(
"method");
148 elog(
"No method and no response ID: {0:2}", Message);
151 if (
auto *Err =
Object->getObject(
"error"))
152 return Handler.onReply(std::move(*ID), decodeError(*Err));
154 llvm::json::Value Result =
nullptr;
155 if (
auto *R =
Object->get(
"result"))
156 Result = std::move(*R);
157 return Handler.onReply(std::move(*ID), std::move(Result));
160 llvm::json::Value Params =
nullptr;
161 if (
auto *P =
Object->get(
"params"))
162 Params = std::move(*P);
165 return Handler.onCall(*Method, std::move(Params), std::move(*ID));
167 return Handler.onNotify(*Method, std::move(Params));
172 bool readLine(std::FILE *In, std::string &Out) {
173 static constexpr
int BufSize = 1024;
177 Out.resize(Size + BufSize);
179 if (!llvm::sys::RetryAfterSignal(
nullptr, ::fgets, &Out[Size], BufSize, In))
184 size_t Read = std::strlen(&Out[Size]);
185 if (Read > 0 && Out[Size + Read - 1] ==
'\n') {
186 Out.resize(Size + Read);
196 llvm::Optional<std::string> JSONTransport::readStandardMessage() {
199 unsigned long long ContentLength = 0;
202 if (feof(In) || ferror(In) || !readLine(In, Line))
206 llvm::StringRef LineRef(Line);
211 if (LineRef.startswith(
"#"))
215 if (LineRef.consume_front(
"Content-Length: ")) {
216 if (ContentLength != 0) {
217 elog(
"Warning: Duplicate Content-Length header received. " 218 "The previous value for this message ({0}) was ignored.",
221 llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength);
223 }
else if (!LineRef.trim().empty()) {
234 if (ContentLength > 1 << 30) {
235 elog(
"Refusing to read message with long Content-Length: {0}. " 236 "Expect protocol errors",
240 if (ContentLength == 0) {
241 log(
"Warning: Missing Content-Length header, or zero-length message.");
245 std::string JSON(ContentLength,
'\0');
246 for (
size_t Pos = 0, Read;
Pos < ContentLength;
Pos +=
Read) {
248 Read = llvm::sys::RetryAfterSignal(0u, ::fread, &JSON[
Pos], 1,
249 ContentLength - Pos, In);
251 elog(
"Input was aborted. Read only {0} bytes of expected {1}.",
Pos,
255 InMirror << llvm::StringRef(&JSON[
Pos], Read);
260 return std::move(JSON);
268 llvm::Optional<std::string> JSONTransport::readDelimitedMessage() {
271 while (readLine(In, Line)) {
273 auto LineRef = llvm::StringRef(Line).trim();
274 if (LineRef.startswith(
"#"))
278 if (LineRef.rtrim() ==
"---")
285 elog(
"Input error while reading message!");
288 return std::move(JSON);
294 llvm::raw_ostream &Out,
295 llvm::raw_ostream *InMirror,
298 return llvm::make_unique<JSONTransport>(In, Out, InMirror, Pretty, Style);
Some operations such as code completion produce a set of candidates.
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
static const StringRef Message
Documents should not be synced at all.
void vlog(const char *Fmt, Ts &&... Vals)
void elog(const char *Fmt, Ts &&... Vals)
void handleErrors(llvm::ArrayRef< ClangTidyError > Errors, ClangTidyContext &Context, bool Fix, unsigned &WarningsAsErrorsCount, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS)
Displays the found Errors to the users.
void log(const char *Fmt, Ts &&... Vals)
std::unique_ptr< Transport > newJSONTransport(std::FILE *In, llvm::raw_ostream &Out, llvm::raw_ostream *InMirror, bool Pretty, JSONStreamStyle Style)
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//