Skip to content

[lldb-dap] Addressing ubsan enum usage. #133542

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 2 commits into from
Mar 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lldb/tools/lldb-dap/DAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,12 +711,12 @@ bool DAP::HandleObject(const protocol::Message &M) {
[](const std::string &message) -> llvm::StringRef {
return message;
},
[](const protocol::Response::Message &message)
[](const protocol::ResponseMessage &message)
-> llvm::StringRef {
switch (message) {
case protocol::Response::Message::cancelled:
case protocol::eResponseMessageCancelled:
return "cancelled";
case protocol::Response::Message::notStopped:
case protocol::eResponseMessageNotStopped:
return "notStopped";
}
}),
Expand Down
24 changes: 11 additions & 13 deletions lldb/tools/lldb-dap/Protocol/ProtocolBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "Protocol/ProtocolBase.h"
#include "lldb/lldb-enumerations.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/ErrorHandling.h"
Expand All @@ -31,11 +32,8 @@ static bool mapRaw(const json::Value &Params, StringLiteral Prop,

namespace lldb_dap::protocol {

enum MessageType {
eMessageTypeRequest,
eMessageTypeResponse,
eMessageTypeEvent
};
FLAGS_ENUM(MessageType){eMessageTypeRequest, eMessageTypeResponse,
eMessageTypeEvent};

bool fromJSON(const json::Value &Params, MessageType &M, json::Path P) {
auto rawType = Params.getAsString();
Expand Down Expand Up @@ -107,12 +105,12 @@ json::Value toJSON(const Response &R) {

if (R.message) {
assert(!R.success && "message can only be used if success is false");
if (const auto *messageEnum = std::get_if<Response::Message>(&*R.message)) {
if (const auto *messageEnum = std::get_if<ResponseMessage>(&*R.message)) {
switch (*messageEnum) {
case Response::Message::cancelled:
case eResponseMessageCancelled:
Result.insert({"message", "cancelled"});
break;
case Response::Message::notStopped:
case eResponseMessageNotStopped:
Result.insert({"message", "notStopped"});
break;
}
Expand All @@ -129,16 +127,16 @@ json::Value toJSON(const Response &R) {
}

bool fromJSON(json::Value const &Params,
std::variant<Response::Message, std::string> &M, json::Path P) {
std::variant<ResponseMessage, std::string> &M, json::Path P) {
auto rawMessage = Params.getAsString();
if (!rawMessage) {
P.report("expected a string");
return false;
}
std::optional<Response::Message> message =
StringSwitch<std::optional<Response::Message>>(*rawMessage)
.Case("cancelled", Response::Message::cancelled)
.Case("notStopped", Response::Message::notStopped)
std::optional<ResponseMessage> message =
StringSwitch<std::optional<ResponseMessage>>(*rawMessage)
.Case("cancelled", eResponseMessageCancelled)
.Case("notStopped", eResponseMessageNotStopped)
.Default(std::nullopt);
if (message)
M = *message;
Expand Down
15 changes: 8 additions & 7 deletions lldb/tools/lldb-dap/Protocol/ProtocolBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef LLDB_TOOLS_LLDB_DAP_PROTOCOL_H
#define LLDB_TOOLS_LLDB_DAP_PROTOCOL_H

#include "lldb/lldb-enumerations.h"
#include "llvm/Support/JSON.h"
#include <cstdint>
#include <optional>
Expand Down Expand Up @@ -64,15 +65,15 @@ struct Event {
llvm::json::Value toJSON(const Event &);
bool fromJSON(const llvm::json::Value &, Event &, llvm::json::Path);

/// Response for a request.
struct Response {
enum class Message {
FLAGS_ENUM(ResponseMessage){
/// The request was cancelled
cancelled,
eResponseMessageCancelled,
/// The request may be retried once the adapter is in a 'stopped' state
notStopped,
};
eResponseMessageNotStopped,
};

/// Response for a request.
struct Response {
/// Sequence number of the corresponding request.
int64_t request_seq;

Expand All @@ -90,7 +91,7 @@ struct Response {
/// Contains the raw error in short form if `success` is false. This raw error
/// might be interpreted by the client and is not shown in the UI. Some
/// predefined values exist.
std::optional<std::variant<Message, std::string>> message;
std::optional<std::variant<ResponseMessage, std::string>> message;

/// Contains request result if success is true and error details if success is
/// false.
Expand Down
36 changes: 19 additions & 17 deletions lldb/tools/lldb-dap/Protocol/ProtocolRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include "Protocol/ProtocolBase.h"
#include "Protocol/ProtocolTypes.h"
#include "lldb/lldb-enumerations.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/Support/JSON.h"
#include <cstdint>
#include <optional>
Expand Down Expand Up @@ -55,26 +57,26 @@ bool fromJSON(const llvm::json::Value &, DisconnectArguments &,
using DisconnectResponse = VoidResponse;

/// Features supported by DAP clients.
enum ClientFeature {
eClientFeatureVariableType,
eClientFeatureVariablePaging,
eClientFeatureRunInTerminalRequest,
eClientFeatureMemoryReferences,
eClientFeatureProgressReporting,
eClientFeatureInvalidatedEvent,
eClientFeatureMemoryEvent,
/// Client supports the `argsCanBeInterpretedByShell` attribute on the
/// `runInTerminal` request.
eClientFeatureArgsCanBeInterpretedByShell,
eClientFeatureStartDebuggingRequest,
/// The client will interpret ANSI escape sequences in the display of
/// `OutputEvent.output` and `Variable.value` fields when
/// `Capabilities.supportsANSIStyling` is also enabled.
eClientFeatureANSIStyling,
FLAGS_ENUM(ClientFeature){
eClientFeatureVariableType,
eClientFeatureVariablePaging,
eClientFeatureRunInTerminalRequest,
eClientFeatureMemoryReferences,
eClientFeatureProgressReporting,
eClientFeatureInvalidatedEvent,
eClientFeatureMemoryEvent,
/// Client supports the `argsCanBeInterpretedByShell` attribute on the
/// `runInTerminal` request.
eClientFeatureArgsCanBeInterpretedByShell,
eClientFeatureStartDebuggingRequest,
/// The client will interpret ANSI escape sequences in the display of
/// `OutputEvent.output` and `Variable.value` fields when
/// `Capabilities.supportsANSIStyling` is also enabled.
eClientFeatureANSIStyling,
};

/// Format of paths reported by the debug adapter.
enum PathFormat { ePatFormatPath, ePathFormatURI };
FLAGS_ENUM(PathFormat){ePatFormatPath, ePathFormatURI};

/// Arguments for `initialize` request.
struct InitializeRequestArguments {
Expand Down
Loading