-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[lldb-dap] Creating well defined structures for DAP messages. #129155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5ec8706
[lldb-dap] Creating a new set of types for handling DAP messages.
ashgti b171e5e
Addressing style comments and reviewer feedback.
ashgti a76b3eb
Applying reviewer suggestions.
ashgti 8512820
Adding 'ErrorMessage' for handling error messages that can be user fa…
ashgti dd6340b
Adjusting Response.message field to be an enum of supported values.
ashgti 95c6201
Update lldb/tools/lldb-dap/Protocol.h
ashgti 68531e4
Removing optional<bool> since they can just be skipped if not set.
ashgti cbf5c4e
Adjusting the Response.message into a std::variant<string, Message> t…
ashgti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| //===-- Protocol.cpp ------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "Protocol.h" | ||
| #include "llvm/ADT/StringRef.h" | ||
| #include "llvm/ADT/StringSwitch.h" | ||
| #include "llvm/Support/ErrorHandling.h" | ||
| #include "llvm/Support/JSON.h" | ||
| #include <optional> | ||
| #include <utility> | ||
|
|
||
| using namespace llvm; | ||
|
|
||
| static bool mapRaw(const json::Value &Params, StringLiteral Prop, | ||
| std::optional<json::Value> &V, json::Path P) { | ||
| const auto *O = Params.getAsObject(); | ||
| if (!O) { | ||
| P.report("expected object"); | ||
| return false; | ||
| } | ||
| if (const json::Value *E = O->get(Prop)) | ||
| V = std::move(Params); | ||
| return true; | ||
| } | ||
|
|
||
| namespace lldb_dap { | ||
| namespace protocol { | ||
|
|
||
| enum class MessageType { request, response, event }; | ||
|
|
||
| bool fromJSON(const json::Value &Params, MessageType &M, json::Path P) { | ||
| auto rawType = Params.getAsString(); | ||
| if (!rawType) { | ||
| P.report("expected a string"); | ||
| return false; | ||
| } | ||
| std::optional<MessageType> type = | ||
| StringSwitch<std::optional<MessageType>>(*rawType) | ||
| .Case("request", MessageType::request) | ||
| .Case("response", MessageType::response) | ||
| .Case("event", MessageType::event) | ||
| .Default(std::nullopt); | ||
| if (!type) { | ||
| P.report("unexpected value, expected 'request', 'response' or 'event'"); | ||
| return false; | ||
| } | ||
| M = *type; | ||
| return true; | ||
| } | ||
|
|
||
| json::Value toJSON(const Request &R) { | ||
| json::Object Result{ | ||
| {"type", "request"}, | ||
| {"seq", R.seq}, | ||
| {"command", R.command}, | ||
| }; | ||
|
|
||
| if (R.rawArguments) | ||
| Result.insert({"arguments", R.rawArguments}); | ||
|
|
||
| return std::move(Result); | ||
| } | ||
|
|
||
| bool fromJSON(json::Value const &Params, Request &R, json::Path P) { | ||
| json::ObjectMapper O(Params, P); | ||
| if (!O) | ||
| return false; | ||
|
|
||
| MessageType type; | ||
| if (!O.map("type", type) || !O.map("command", R.command) || | ||
| !O.map("seq", R.seq)) | ||
| return false; | ||
|
|
||
| if (type != MessageType::request) { | ||
| P.field("type").report("expected to be 'request'"); | ||
| return false; | ||
| } | ||
|
|
||
| if (R.command.empty()) { | ||
| P.field("command").report("expected to not be ''"); | ||
| return false; | ||
| } | ||
|
|
||
| if (!R.seq) { | ||
| P.field("seq").report("expected to not be '0'"); | ||
| return false; | ||
| } | ||
|
|
||
| return mapRaw(Params, "arguments", R.rawArguments, P); | ||
| } | ||
|
|
||
| json::Value toJSON(const Response &R) { | ||
| json::Object Result{{"type", "response"}, | ||
| {"req", 0}, | ||
| {"command", R.command}, | ||
| {"request_seq", R.request_seq}, | ||
| {"success", R.success}}; | ||
|
|
||
| if (R.message) | ||
| Result.insert({"message", R.message}); | ||
ashgti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (R.rawBody) | ||
| Result.insert({"body", R.rawBody}); | ||
|
|
||
| return std::move(Result); | ||
| } | ||
|
|
||
| bool fromJSON(json::Value const &Params, Response &R, json::Path P) { | ||
| json::ObjectMapper O(Params, P); | ||
| if (!O) | ||
| return false; | ||
|
|
||
| MessageType type; | ||
| int64_t seq; | ||
| if (!O.map("type", type) || !O.map("seq", seq) || | ||
| !O.map("command", R.command) || !O.map("request_seq", R.request_seq)) | ||
| return false; | ||
|
|
||
| if (type != MessageType::response) { | ||
| P.field("type").report("expected to be 'response'"); | ||
| return false; | ||
| } | ||
|
|
||
| if (seq != 0) { | ||
| P.field("seq").report("expected to be '0'"); | ||
| return false; | ||
| } | ||
|
|
||
| if (R.command.empty()) { | ||
| P.field("command").report("expected to not be ''"); | ||
| return false; | ||
| } | ||
|
|
||
| if (R.request_seq == 0) { | ||
| P.field("request_seq").report("expected to not be '0'"); | ||
| return false; | ||
| } | ||
|
|
||
| return O.map("success", R.success) && O.mapOptional("message", R.message) && | ||
| mapRaw(Params, "body", R.rawBody, P); | ||
| } | ||
|
|
||
| json::Value toJSON(const Event &E) { | ||
| json::Object Result{ | ||
| {"type", "event"}, | ||
| {"seq", 0}, | ||
| {"event", E.event}, | ||
| }; | ||
|
|
||
| if (E.rawBody) | ||
| Result.insert({"body", E.rawBody}); | ||
|
|
||
| if (E.statistics) | ||
| Result.insert({"statistics", E.statistics}); | ||
ashgti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return std::move(Result); | ||
| } | ||
|
|
||
| bool fromJSON(json::Value const &Params, Event &E, json::Path P) { | ||
| json::ObjectMapper O(Params, P); | ||
| if (!O) | ||
| return false; | ||
|
|
||
| MessageType type; | ||
| int64_t seq; | ||
| if (!O.map("type", type) || !O.map("seq", seq) || O.map("event", E.event)) | ||
| return false; | ||
|
|
||
| if (type != MessageType::event) { | ||
| P.field("type").report("expected to be 'event'"); | ||
| return false; | ||
| } | ||
|
|
||
| if (seq != 0) { | ||
| P.field("seq").report("expected to be '0'"); | ||
| return false; | ||
| } | ||
|
|
||
| if (E.event.empty()) { | ||
| P.field("event").report("expected to not be ''"); | ||
| return false; | ||
| } | ||
|
|
||
| return mapRaw(Params, "body", E.rawBody, P) && | ||
| mapRaw(Params, "statistics", E.statistics, P); | ||
| } | ||
|
|
||
| bool fromJSON(const json::Value &Params, Message &PM, json::Path P) { | ||
| json::ObjectMapper O(Params, P); | ||
| if (!O) | ||
| return false; | ||
|
|
||
| MessageType type; | ||
| if (!O.map("type", type)) | ||
| return false; | ||
|
|
||
| switch (type) { | ||
| case MessageType::request: { | ||
| Request req; | ||
| if (!fromJSON(Params, req, P)) | ||
| return false; | ||
| PM = std::move(req); | ||
| return true; | ||
| } | ||
| case MessageType::response: { | ||
| Response resp; | ||
| if (!fromJSON(Params, resp, P)) | ||
| return false; | ||
| PM = std::move(resp); | ||
| return true; | ||
| } | ||
| case MessageType::event: | ||
| Event evt; | ||
| if (!fromJSON(Params, evt, P)) | ||
| return false; | ||
| PM = std::move(evt); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| json::Value toJSON(const Message &M) { | ||
| return std::visit([](auto &M) { return toJSON(M); }, M); | ||
| } | ||
|
|
||
| } // namespace protocol | ||
| } // namespace lldb_dap | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.