Skip to content

Commit b62319d

Browse files
committed
Addressed comments
1 parent dcb202d commit b62319d

File tree

2 files changed

+23
-45
lines changed

2 files changed

+23
-45
lines changed

mlir/include/mlir/Tools/lsp-server-support/Transport.h

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ class JSONTransportInput {
5050
: style(style) {}
5151
virtual ~JSONTransportInput() = default;
5252

53-
virtual bool getError() const = 0;
53+
virtual bool hasError() const = 0;
5454
virtual bool isEndOfInput() const = 0;
5555

5656
/// Read in a message from the input stream.
57-
virtual LogicalResult readMessage(std::string &json) {
57+
LogicalResult readMessage(std::string &json) {
5858
return style == JSONStreamStyle::Delimited ? readDelimitedMessage(json)
5959
: readStandardMessage(json);
6060
}
@@ -66,23 +66,14 @@ class JSONTransportInput {
6666
JSONStreamStyle style;
6767
};
6868

69-
/// An abstract class used by the JSONTransport to write JSON messages.
70-
class JSONTransportOutput {
71-
public:
72-
explicit JSONTransportOutput() = default;
73-
virtual ~JSONTransportOutput() = default;
74-
75-
virtual void sendMessage(const llvm::json::Value &msg) = 0;
76-
};
77-
7869
/// Concrete implementation of the JSONTransportInput that reads from a file.
7970
class JSONTransportInputOverFile : public JSONTransportInput {
8071
public:
8172
explicit JSONTransportInputOverFile(
8273
std::FILE *in, JSONStreamStyle style = JSONStreamStyle::Standard)
8374
: JSONTransportInput(style), in(in) {}
8475

85-
bool getError() const final { return ferror(in); }
76+
bool hasError() const final { return ferror(in); }
8677
bool isEndOfInput() const final { return feof(in); }
8778

8879
LogicalResult readDelimitedMessage(std::string &json) final;
@@ -92,37 +83,19 @@ class JSONTransportInputOverFile : public JSONTransportInput {
9283
std::FILE *in;
9384
};
9485

95-
/// Concrete implementation of the JSONTransportOutput that writes to a stream.
96-
class JSONTransportOutputOverStream : public JSONTransportOutput {
97-
public:
98-
explicit JSONTransportOutputOverStream(raw_ostream &out,
99-
bool prettyOutput = false)
100-
: JSONTransportOutput(), out(out), prettyOutput(prettyOutput) {}
101-
102-
/// Writes the given message to the output stream.
103-
void sendMessage(const llvm::json::Value &msg) final;
104-
105-
private:
106-
SmallVector<char, 0> outputBuffer;
107-
raw_ostream &out;
108-
/// If the output JSON should be formatted for easier readability.
109-
bool prettyOutput;
110-
};
111-
11286
/// A transport class that performs the JSON-RPC communication with the LSP
11387
/// client.
11488
class JSONTransport {
11589
public:
116-
JSONTransport(std::unique_ptr<JSONTransportInput> in,
117-
std::unique_ptr<JSONTransportOutput> out)
118-
: in(std::move(in)), out(std::move(out)) {}
90+
JSONTransport(std::unique_ptr<JSONTransportInput> in, raw_ostream &out,
91+
bool prettyOutput = false)
92+
: in(std::move(in)), out(out), prettyOutput(prettyOutput) {}
11993

12094
JSONTransport(std::FILE *in, raw_ostream &out,
12195
JSONStreamStyle style = JSONStreamStyle::Standard,
12296
bool prettyOutput = false)
123-
: in(std::make_unique<JSONTransportInputOverFile>(in, style)),
124-
out(std::make_unique<JSONTransportOutputOverStream>(out,
125-
prettyOutput)) {}
97+
: in(std::make_unique<JSONTransportInputOverFile>(in, style)), out(out),
98+
prettyOutput(prettyOutput) {}
12699

127100
/// The following methods are used to send a message to the LSP client.
128101
void notify(StringRef method, llvm::json::Value params);
@@ -132,15 +105,20 @@ class JSONTransport {
132105
/// Start executing the JSON-RPC transport.
133106
llvm::Error run(MessageHandler &handler);
134107

108+
private:
135109
/// Dispatches the given incoming json message to the message handler.
136110
bool handleMessage(llvm::json::Value msg, MessageHandler &handler);
111+
/// Writes the given message to the output stream.
112+
void sendMessage(llvm::json::Value msg);
137113

138114
private:
139115
/// The input to read a message from.
140116
std::unique_ptr<JSONTransportInput> in;
141-
142-
/// The output to send a messages to.
143-
std::unique_ptr<JSONTransportOutput> out;
117+
SmallVector<char, 0> outputBuffer;
118+
/// The output file stream.
119+
raw_ostream &out;
120+
/// If the output JSON should be formatted for easier readability.
121+
bool prettyOutput;
144122
};
145123

146124
//===----------------------------------------------------------------------===//

mlir/lib/Tools/lsp-server-support/Transport.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,15 @@ llvm::Error decodeError(const llvm::json::Object &o) {
175175
}
176176

177177
void JSONTransport::notify(StringRef method, llvm::json::Value params) {
178-
out->sendMessage(llvm::json::Object{
178+
sendMessage(llvm::json::Object{
179179
{"jsonrpc", "2.0"},
180180
{"method", method},
181181
{"params", std::move(params)},
182182
});
183183
}
184184
void JSONTransport::call(StringRef method, llvm::json::Value params,
185185
llvm::json::Value id) {
186-
out->sendMessage(llvm::json::Object{
186+
sendMessage(llvm::json::Object{
187187
{"jsonrpc", "2.0"},
188188
{"id", std::move(id)},
189189
{"method", method},
@@ -193,14 +193,14 @@ void JSONTransport::call(StringRef method, llvm::json::Value params,
193193
void JSONTransport::reply(llvm::json::Value id,
194194
llvm::Expected<llvm::json::Value> result) {
195195
if (result) {
196-
return out->sendMessage(llvm::json::Object{
196+
return sendMessage(llvm::json::Object{
197197
{"jsonrpc", "2.0"},
198198
{"id", std::move(id)},
199199
{"result", std::move(*result)},
200200
});
201201
}
202202

203-
out->sendMessage(llvm::json::Object{
203+
sendMessage(llvm::json::Object{
204204
{"jsonrpc", "2.0"},
205205
{"id", std::move(id)},
206206
{"error", encodeError(result.takeError())},
@@ -210,7 +210,7 @@ void JSONTransport::reply(llvm::json::Value id,
210210
llvm::Error JSONTransport::run(MessageHandler &handler) {
211211
std::string json;
212212
while (!in->isEndOfInput()) {
213-
if (in->getError()) {
213+
if (in->hasError()) {
214214
return llvm::errorCodeToError(
215215
std::error_code(errno, std::system_category()));
216216
}
@@ -227,7 +227,7 @@ llvm::Error JSONTransport::run(MessageHandler &handler) {
227227
return llvm::errorCodeToError(std::make_error_code(std::errc::io_error));
228228
}
229229

230-
void JSONTransportOutputOverStream::sendMessage(const llvm::json::Value &msg) {
230+
void JSONTransport::sendMessage(llvm::json::Value msg) {
231231
outputBuffer.clear();
232232
llvm::raw_svector_ostream os(outputBuffer);
233233
os << llvm::formatv(prettyOutput ? "{0:2}\n" : "{0}", msg);
@@ -310,7 +310,7 @@ JSONTransportInputOverFile::readStandardMessage(std::string &json) {
310310
unsigned long long contentLength = 0;
311311
llvm::SmallString<128> line;
312312
while (true) {
313-
if (feof(in) || ferror(in) || failed(readLine(in, line)))
313+
if (feof(in) || hasError() || failed(readLine(in, line)))
314314
return failure();
315315

316316
// Content-Length is a mandatory header, and the only one we handle.

0 commit comments

Comments
 (0)