Skip to content

Commit a3fe301

Browse files
committed
[lldb] [lldb-dap] Return optional values for signed json values.
1 parent 14bd9a7 commit a3fe301

File tree

8 files changed

+26
-26
lines changed

8 files changed

+26
-26
lines changed

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ ExceptionBreakpoint *DAP::GetExceptionBPFromStopReason(lldb::SBThread &thread) {
526526
}
527527

528528
lldb::SBThread DAP::GetLLDBThread(const llvm::json::Object &arguments) {
529-
auto tid = GetSigned(arguments, "threadId", LLDB_INVALID_THREAD_ID);
529+
auto tid = GetSigned(arguments, "threadId").value_or(LLDB_INVALID_THREAD_ID);
530530
return target.GetProcess().GetThreadByID(tid);
531531
}
532532

@@ -772,7 +772,7 @@ bool DAP::HandleObject(const llvm::json::Object &object) {
772772
}
773773

774774
if (packet_type == "response") {
775-
auto id = GetSigned(object, "request_seq", 0);
775+
auto id = GetSigned(object, "request_seq").value_or(0);
776776

777777
std::unique_ptr<ResponseHandler> response_handler;
778778
{

lldb/tools/lldb-dap/Handler/CompletionsHandler.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,9 @@ void CompletionsRequestHandler::operator()(
143143
}
144144

145145
std::string text = GetString(arguments, "text").str();
146-
auto original_column = GetSigned(arguments, "column", text.size());
147-
auto original_line = GetSigned(arguments, "line", 1);
146+
const auto original_column =
147+
GetSigned(arguments, "column").value_or(text.size());
148+
const auto original_line = GetSigned(arguments, "line").value_or(1);
148149
auto offset = original_column - 1;
149150
if (original_line > 1) {
150151
llvm::SmallVector<::llvm::StringRef, 2> lines;

lldb/tools/lldb-dap/Handler/DisassembleRequestHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ void DisassembleRequestHandler::operator()(
104104
}
105105
lldb::addr_t addr_ptr = *addr_opt;
106106

107-
addr_ptr += GetSigned(arguments, "instructionOffset", 0);
107+
addr_ptr += GetSigned(arguments, "instructionOffset").value_or(0);
108108
lldb::SBAddress addr(addr_ptr, dap.target);
109109
if (!addr.IsValid()) {
110110
response["success"] = false;

lldb/tools/lldb-dap/Handler/ReadMemoryRequestHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void ReadMemoryRequestHandler::operator()(
108108
return;
109109
}
110110
lldb::addr_t addr_int = *addr_opt;
111-
addr_int += GetSigned(arguments, "offset", 0);
111+
addr_int += GetSigned(arguments, "offset").value_or(0);
112112
const uint64_t count_requested = GetUnsigned(arguments, "count").value_or(0);
113113

114114
// We also need support reading 0 bytes

lldb/tools/lldb-dap/Handler/VariablesRequestHandler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ void VariablesRequestHandler::operator()(
9494
const auto *arguments = request.getObject("arguments");
9595
const auto variablesReference =
9696
GetUnsigned(arguments, "variablesReference").value_or(0);
97-
const int64_t start = GetSigned(arguments, "start", 0);
98-
const int64_t count = GetSigned(arguments, "count", 0);
97+
const int64_t start = GetSigned(arguments, "start").value_or(0);
98+
const int64_t count = GetSigned(arguments, "count").value_or(0);
9999
bool hex = false;
100100
const auto *format = arguments->getObject("format");
101101
if (format)

lldb/tools/lldb-dap/InstructionBreakpoint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace lldb_dap {
2020
InstructionBreakpoint::InstructionBreakpoint(DAP &d,
2121
const llvm::json::Object &obj)
2222
: Breakpoint(d, obj), instructionAddressReference(LLDB_INVALID_ADDRESS),
23-
offset(GetSigned(obj, "offset", 0)) {
23+
offset(GetSigned(obj, "offset").value_or(0)) {
2424
GetString(obj, "instructionReference")
2525
.getAsInteger(0, instructionAddressReference);
2626
instructionAddressReference += offset;

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,17 @@ std::optional<bool> GetBoolean(const llvm::json::Object *obj,
115115
return std::nullopt;
116116
}
117117

118-
int64_t GetSigned(const llvm::json::Object &obj, llvm::StringRef key,
119-
int64_t fail_value) {
120-
if (auto value = obj.getInteger(key))
121-
return *value;
122-
return fail_value;
118+
std::optional<int64_t> GetSigned(const llvm::json::Object &obj,
119+
llvm::StringRef key) {
120+
return obj.getInteger(key);
123121
}
124122

125-
int64_t GetSigned(const llvm::json::Object *obj, llvm::StringRef key,
126-
int64_t fail_value) {
123+
std::optional<int64_t> GetSigned(const llvm::json::Object *obj,
124+
llvm::StringRef key) {
127125
if (obj == nullptr)
128-
return fail_value;
129-
return GetSigned(*obj, key, fail_value);
126+
return std::nullopt;
127+
128+
return GetSigned(*obj, key);
130129
}
131130

132131
bool ObjectContainsKey(const llvm::json::Object &obj, llvm::StringRef key) {
@@ -279,7 +278,7 @@ void FillResponse(const llvm::json::Object &request,
279278
response.try_emplace("type", "response");
280279
response.try_emplace("seq", (int64_t)0);
281280
EmplaceSafeString(response, "command", GetString(request, "command"));
282-
const int64_t seq = GetSigned(request, "seq", 0);
281+
const int64_t seq = GetSigned(request, "seq").value_or(0);
283282
response.try_emplace("request_seq", seq);
284283
response.try_emplace("success", true);
285284
}

lldb/tools/lldb-dap/JSONUtils.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ llvm::StringRef GetString(const llvm::json::Object *obj, llvm::StringRef key,
8585
///
8686
/// \return
8787
/// The unsigned integer value for the specified \a key, or
88-
/// std::nullopt if there is no key that matches or if the
88+
/// \a std::nullopt if there is no key that matches or if the
8989
/// value is not an integer.
9090
std::optional<uint64_t> GetUnsigned(const llvm::json::Object &obj,
9191
llvm::StringRef key);
@@ -102,7 +102,7 @@ std::optional<uint64_t> GetUnsigned(const llvm::json::Object *obj,
102102
/// The key to use when extracting the value
103103
///
104104
/// \return
105-
/// The boolean value for the specified \a key, or std::nullopt
105+
/// The boolean value for the specified \a key, or \a std::nullopt
106106
/// if there is no key that matches or if the value is not a
107107
/// boolean value of an integer.
108108
/// @{
@@ -123,12 +123,12 @@ std::optional<bool> GetBoolean(const llvm::json::Object *obj,
123123
///
124124
/// \return
125125
/// The signed integer value for the specified \a key, or
126-
/// \a fail_value if there is no key that matches or if the
126+
/// \a std::nullopt if there is no key that matches or if the
127127
/// value is not an integer.
128-
int64_t GetSigned(const llvm::json::Object &obj, llvm::StringRef key,
129-
int64_t fail_value);
130-
int64_t GetSigned(const llvm::json::Object *obj, llvm::StringRef key,
131-
int64_t fail_value);
128+
std::optional<int64_t> GetSigned(const llvm::json::Object &obj,
129+
llvm::StringRef key);
130+
std::optional<int64_t> GetSigned(const llvm::json::Object *obj,
131+
llvm::StringRef key);
132132

133133
/// Check if the specified key exists in the specified object.
134134
///

0 commit comments

Comments
 (0)