12 #include "llvm/Support/Errno.h" 13 #include "llvm/Support/Error.h" 23 std::move(E), [&](
const LSPError &L) ->
llvm::Error {
26 return llvm::Error::success();
30 return llvm::json::Object{
31 {
"message", std::move(Message)},
32 {
"code", int64_t(Code)},
36 llvm::Error decodeError(
const llvm::json::Object &O) {
37 std::string Msg = O.getString(
"message").getValueOr(
"Unspecified error");
38 if (
auto Code = O.getInteger(
"code"))
39 return llvm::make_error<LSPError>(std::move(Msg),
ErrorCode(*Code));
40 return llvm::make_error<llvm::StringError>(std::move(Msg),
41 llvm::inconvertibleErrorCode());
44 class JSONTransport :
public Transport {
46 JSONTransport(std::FILE *In, llvm::raw_ostream &Out,
48 : In(In), Out(Out), InMirror(InMirror ? *InMirror :
llvm::nulls()),
49 Pretty(Pretty), Style(Style) {}
51 void notify(llvm::StringRef
Method, llvm::json::Value Params)
override {
52 sendMessage(llvm::json::Object{
55 {
"params", std::move(Params)},
58 void call(llvm::StringRef Method, llvm::json::Value Params,
59 llvm::json::Value ID)
override {
60 sendMessage(llvm::json::Object{
62 {
"id", std::move(ID)},
64 {
"params", std::move(Params)},
67 void reply(llvm::json::Value ID,
68 llvm::Expected<llvm::json::Value> Result)
override {
70 sendMessage(llvm::json::Object{
72 {
"id", std::move(ID)},
73 {
"result", std::move(*Result)},
76 sendMessage(llvm::json::Object{
78 {
"id", std::move(ID)},
79 {
"error", encodeError(Result.takeError())},
84 llvm::Error loop(MessageHandler &Handler)
override {
87 return llvm::createStringError(
88 std::make_error_code(std::errc::operation_canceled),
89 "Got signal, shutting down");
91 return llvm::errorCodeToError(
92 std::error_code(errno, std::system_category()));
93 if (
auto JSON = readRawMessage()) {
94 if (
auto Doc = llvm::json::parse(*JSON)) {
95 vlog(Pretty ?
"<<< {0:2}\n" :
"<<< {0}\n", *Doc);
96 if (!handleMessage(std::move(*Doc), Handler))
97 return llvm::Error::success();
100 vlog(
"<<< {0}\n", *JSON);
105 return llvm::errorCodeToError(std::make_error_code(std::errc::io_error));
110 bool handleMessage(llvm::json::Value Message, MessageHandler &Handler);
112 void sendMessage(llvm::json::Value Message) {
114 llvm::raw_string_ostream OS(S);
115 OS << llvm::formatv(Pretty ?
"{0:2}" :
"{0}", Message);
117 Out <<
"Content-Length: " << S.size() <<
"\r\n\r\n" << S;
119 vlog(
">>> {0}\n", S);
123 llvm::Optional<std::string> readRawMessage() {
125 : readStandardMessage();
127 llvm::Optional<std::string> readDelimitedMessage();
128 llvm::Optional<std::string> readStandardMessage();
131 llvm::raw_ostream &Out;
132 llvm::raw_ostream &InMirror;
137 bool JSONTransport::handleMessage(llvm::json::Value Message,
138 MessageHandler &Handler) {
140 auto *
Object = Message.getAsObject();
142 Object->getString(
"jsonrpc") != llvm::Optional<llvm::StringRef>(
"2.0")) {
143 elog(
"Not a JSON-RPC 2.0 message: {0:2}", Message);
147 llvm::Optional<llvm::json::Value> ID;
148 if (
auto *I =
Object->get(
"id"))
150 auto Method =
Object->getString(
"method");
153 elog(
"No method and no response ID: {0:2}", Message);
156 if (
auto *Err =
Object->getObject(
"error"))
157 return Handler.onReply(std::move(*ID), decodeError(*Err));
159 llvm::json::Value Result =
nullptr;
160 if (
auto *R =
Object->get(
"result"))
161 Result = std::move(*R);
162 return Handler.onReply(std::move(*ID), std::move(Result));
165 llvm::json::Value Params =
nullptr;
166 if (
auto *P =
Object->get(
"params"))
167 Params = std::move(*P);
170 return Handler.onCall(*Method, std::move(Params), std::move(*ID));
172 return Handler.onNotify(*Method, std::move(Params));
177 bool readLine(std::FILE *In, std::string &Out) {
178 static constexpr
int BufSize = 1024;
182 Out.resize(Size + BufSize);
185 nullptr, [&] {
return std::fgets(&Out[Size], BufSize, In); }))
190 size_t Read = std::strlen(&Out[Size]);
191 if (Read > 0 && Out[Size + Read - 1] ==
'\n') {
192 Out.resize(Size + Read);
202 llvm::Optional<std::string> JSONTransport::readStandardMessage() {
205 unsigned long long ContentLength = 0;
208 if (feof(In) || ferror(In) || !readLine(In, Line))
212 llvm::StringRef LineRef(Line);
217 if (LineRef.startswith(
"#"))
221 if (LineRef.consume_front(
"Content-Length: ")) {
222 if (ContentLength != 0) {
223 elog(
"Warning: Duplicate Content-Length header received. " 224 "The previous value for this message ({0}) was ignored.",
227 llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength);
229 }
else if (!LineRef.trim().empty()) {
240 if (ContentLength > 1 << 30) {
241 elog(
"Refusing to read message with long Content-Length: {0}. " 242 "Expect protocol errors",
246 if (ContentLength == 0) {
247 log(
"Warning: Missing Content-Length header, or zero-length message.");
251 std::string JSON(ContentLength,
'\0');
252 for (
size_t Pos = 0, Read;
Pos < ContentLength;
Pos +=
Read) {
255 return std::fread(&JSON[
Pos], 1, ContentLength - Pos, In);
258 elog(
"Input was aborted. Read only {0} bytes of expected {1}.",
Pos,
262 InMirror << llvm::StringRef(&JSON[
Pos], Read);
267 return std::move(JSON);
275 llvm::Optional<std::string> JSONTransport::readDelimitedMessage() {
278 while (readLine(In, Line)) {
280 auto LineRef = llvm::StringRef(Line).trim();
281 if (LineRef.startswith(
"#"))
285 if (LineRef.rtrim() ==
"---")
294 elog(
"Input error while reading message!");
297 return std::move(JSON);
303 llvm::raw_ostream &Out,
304 llvm::raw_ostream *InMirror,
307 return std::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)
bool shutdownRequested()
Checks whether requestShutdown() was called.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Ret retryAfterSignalUnlessShutdown(const typename std::enable_if< true, Ret >::type &Fail, const Fun &F)
Retry an operation if it gets interrupted by a signal.