11 #include "llvm/Support/Errno.h" 21 std::move(E), [&](
const LSPError &L) ->
llvm::Error {
24 return llvm::Error::success();
28 return llvm::json::Object{
29 {
"message", std::move(Message)},
30 {
"code", int64_t(Code)},
34 llvm::Error decodeError(
const llvm::json::Object &O) {
35 std::string Msg = O.getString(
"message").getValueOr(
"Unspecified error");
36 if (
auto Code = O.getInteger(
"code"))
37 return llvm::make_error<LSPError>(std::move(Msg),
ErrorCode(*Code));
38 return llvm::make_error<llvm::StringError>(std::move(Msg),
39 llvm::inconvertibleErrorCode());
42 class JSONTransport :
public Transport {
44 JSONTransport(std::FILE *In, llvm::raw_ostream &Out,
46 : In(In), Out(Out), InMirror(InMirror ? *InMirror :
llvm::nulls()),
47 Pretty(Pretty), Style(Style) {}
49 void notify(llvm::StringRef
Method, llvm::json::Value Params)
override {
50 sendMessage(llvm::json::Object{
53 {
"params", std::move(Params)},
56 void call(llvm::StringRef Method, llvm::json::Value Params,
57 llvm::json::Value ID)
override {
58 sendMessage(llvm::json::Object{
60 {
"id", std::move(ID)},
62 {
"params", std::move(Params)},
65 void reply(llvm::json::Value ID,
66 llvm::Expected<llvm::json::Value>
Result)
override {
68 sendMessage(llvm::json::Object{
70 {
"id", std::move(ID)},
71 {
"result", std::move(*Result)},
74 sendMessage(llvm::json::Object{
76 {
"id", std::move(ID)},
77 {
"error", encodeError(Result.takeError())},
82 llvm::Error loop(MessageHandler &Handler)
override {
85 return llvm::errorCodeToError(
86 std::error_code(errno, std::system_category()));
87 if (
auto JSON = readRawMessage()) {
88 if (
auto Doc = llvm::json::parse(*JSON)) {
89 vlog(Pretty ?
"<<< {0:2}\n" :
"<<< {0}\n", *Doc);
90 if (!handleMessage(std::move(*Doc), Handler))
91 return llvm::Error::success();
94 vlog(
"<<< {0}\n", *JSON);
99 return llvm::errorCodeToError(std::make_error_code(std::errc::io_error));
104 bool handleMessage(llvm::json::Value Message, MessageHandler &Handler);
106 void sendMessage(llvm::json::Value Message) {
108 llvm::raw_string_ostream OS(S);
109 OS << llvm::formatv(Pretty ?
"{0:2}" :
"{0}", Message);
111 Out <<
"Content-Length: " << S.size() <<
"\r\n\r\n" << S;
113 vlog(
">>> {0}\n", S);
117 llvm::Optional<std::string> readRawMessage() {
119 : readStandardMessage();
121 llvm::Optional<std::string> readDelimitedMessage();
122 llvm::Optional<std::string> readStandardMessage();
125 llvm::raw_ostream &Out;
126 llvm::raw_ostream &InMirror;
131 bool JSONTransport::handleMessage(llvm::json::Value Message,
132 MessageHandler &Handler) {
134 auto *
Object = Message.getAsObject();
136 Object->getString(
"jsonrpc") != llvm::Optional<llvm::StringRef>(
"2.0")) {
137 elog(
"Not a JSON-RPC 2.0 message: {0:2}", Message);
141 llvm::Optional<llvm::json::Value> ID;
142 if (
auto *I =
Object->get(
"id"))
144 auto Method =
Object->getString(
"method");
147 elog(
"No method and no response ID: {0:2}", Message);
150 if (
auto *Err =
Object->getObject(
"error"))
151 return Handler.onReply(std::move(*ID), decodeError(*Err));
153 llvm::json::Value Result =
nullptr;
154 if (
auto *R =
Object->get(
"result"))
155 Result = std::move(*R);
156 return Handler.onReply(std::move(*ID), std::move(Result));
159 llvm::json::Value Params =
nullptr;
160 if (
auto *P =
Object->get(
"params"))
161 Params = std::move(*P);
164 return Handler.onCall(*Method, std::move(Params), std::move(*ID));
166 return Handler.onNotify(*Method, std::move(Params));
171 bool readLine(std::FILE *In, std::string &Out) {
172 static constexpr
int BufSize = 1024;
176 Out.resize(Size + BufSize);
178 if (!llvm::sys::RetryAfterSignal(
nullptr, ::fgets, &Out[Size], BufSize, In))
183 size_t Read = std::strlen(&Out[Size]);
184 if (Read > 0 && Out[Size + Read - 1] ==
'\n') {
185 Out.resize(Size + Read);
195 llvm::Optional<std::string> JSONTransport::readStandardMessage() {
198 unsigned long long ContentLength = 0;
201 if (feof(In) || ferror(In) || !readLine(In, Line))
205 llvm::StringRef LineRef(Line);
210 if (LineRef.startswith(
"#"))
214 if (LineRef.consume_front(
"Content-Length: ")) {
215 if (ContentLength != 0) {
216 elog(
"Warning: Duplicate Content-Length header received. " 217 "The previous value for this message ({0}) was ignored.",
220 llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength);
222 }
else if (!LineRef.trim().empty()) {
233 if (ContentLength > 1 << 30) {
234 elog(
"Refusing to read message with long Content-Length: {0}. " 235 "Expect protocol errors",
239 if (ContentLength == 0) {
240 log(
"Warning: Missing Content-Length header, or zero-length message.");
244 std::string JSON(ContentLength,
'\0');
245 for (
size_t Pos = 0, Read; Pos < ContentLength; Pos +=
Read) {
247 Read = llvm::sys::RetryAfterSignal(0u, ::fread, &JSON[Pos], 1,
248 ContentLength - Pos, In);
250 elog(
"Input was aborted. Read only {0} bytes of expected {1}.", Pos,
254 InMirror << llvm::StringRef(&JSON[Pos], Read);
259 return std::move(JSON);
267 llvm::Optional<std::string> JSONTransport::readDelimitedMessage() {
270 while (readLine(In, Line)) {
272 auto LineRef = llvm::StringRef(Line).trim();
273 if (LineRef.startswith(
"#"))
277 if (LineRef.rtrim() ==
"---")
284 elog(
"Input error while reading message!");
287 return std::move(JSON);
293 llvm::raw_ostream &Out,
294 llvm::raw_ostream *InMirror,
297 return llvm::make_unique<JSONTransport>(In, Out, InMirror, Pretty, Style);
Some operations such as code completion produce a set of candidates.
constexpr llvm::StringLiteral Message
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
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)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result