Skip to content

Commit b53e757

Browse files
authored
[lldb-dap] Replace Get{Signed,Unsigned} with GetInteger<T> (NFC) (#129823)
Replace Get{Signed,Unsigned} with GetInteger<T> and return std::optional so you can distinguish between the value not being present and it being explicitly set to the previous fail_value. All existing uses are replaced by calling value_or(fail_value). Continuation of #129818
1 parent 7bd492f commit b53e757

18 files changed

+68
-75
lines changed

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,12 +526,14 @@ 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 = GetInteger<int64_t>(arguments, "threadId")
530+
.value_or(LLDB_INVALID_THREAD_ID);
530531
return target.GetProcess().GetThreadByID(tid);
531532
}
532533

533534
lldb::SBFrame DAP::GetLLDBFrame(const llvm::json::Object &arguments) {
534-
const uint64_t frame_id = GetUnsigned(arguments, "frameId", UINT64_MAX);
535+
const uint64_t frame_id =
536+
GetInteger<uint64_t>(arguments, "frameId").value_or(UINT64_MAX);
535537
lldb::SBProcess process = target.GetProcess();
536538
// Upper 32 bits is the thread index ID
537539
lldb::SBThread thread =
@@ -771,7 +773,7 @@ bool DAP::HandleObject(const llvm::json::Object &object) {
771773
}
772774

773775
if (packet_type == "response") {
774-
auto id = GetSigned(object, "request_seq", 0);
776+
auto id = GetInteger<int64_t>(object, "request_seq").value_or(0);
775777

776778
std::unique_ptr<ResponseHandler> response_handler;
777779
{

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ void AttachRequestHandler::operator()(const llvm::json::Object &request) const {
5454
const int invalid_port = 0;
5555
const auto *arguments = request.getObject("arguments");
5656
const lldb::pid_t pid =
57-
GetUnsigned(arguments, "pid", LLDB_INVALID_PROCESS_ID);
57+
GetInteger<uint64_t>(arguments, "pid").value_or(LLDB_INVALID_PROCESS_ID);
5858
const auto gdb_remote_port =
59-
GetUnsigned(arguments, "gdb-remote-port", invalid_port);
59+
GetInteger<uint64_t>(arguments, "gdb-remote-port").value_or(invalid_port);
6060
const auto gdb_remote_hostname =
6161
GetString(arguments, "gdb-remote-hostname", "localhost");
6262
if (pid != LLDB_INVALID_PROCESS_ID)
@@ -70,7 +70,8 @@ void AttachRequestHandler::operator()(const llvm::json::Object &request) const {
7070
dap.terminate_commands = GetStrings(arguments, "terminateCommands");
7171
auto attachCommands = GetStrings(arguments, "attachCommands");
7272
llvm::StringRef core_file = GetString(arguments, "coreFile");
73-
const uint64_t timeout_seconds = GetUnsigned(arguments, "timeout", 30);
73+
const auto timeout_seconds =
74+
GetInteger<uint64_t>(arguments, "timeout").value_or(30);
7475
dap.stop_at_entry = core_file.empty()
7576
? GetBoolean(arguments, "stopOnEntry").value_or(false)
7677
: true;

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,13 @@ void BreakpointLocationsRequestHandler::operator()(
131131
auto *arguments = request.getObject("arguments");
132132
auto *source = arguments->getObject("source");
133133
std::string path = GetString(source, "path").str();
134-
uint64_t start_line = GetUnsigned(arguments, "line", 0);
135-
uint64_t start_column = GetUnsigned(arguments, "column", 0);
136-
uint64_t end_line = GetUnsigned(arguments, "endLine", start_line);
137-
uint64_t end_column =
138-
GetUnsigned(arguments, "endColumn", std::numeric_limits<uint64_t>::max());
134+
const auto start_line = GetInteger<uint64_t>(arguments, "line").value_or(0);
135+
const auto start_column =
136+
GetInteger<uint64_t>(arguments, "column").value_or(0);
137+
const auto end_line =
138+
GetInteger<uint64_t>(arguments, "endLine").value_or(start_line);
139+
const auto end_column = GetInteger<uint64_t>(arguments, "endColumn")
140+
.value_or(std::numeric_limits<uint64_t>::max());
139141

140142
lldb::SBFileSpec file_spec(path.c_str(), true);
141143
lldb::SBSymbolContextList compile_units =

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+
auto original_column =
147+
GetInteger<int64_t>(arguments, "column").value_or(text.size());
148+
auto original_line = GetInteger<int64_t>(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/DataBreakpointInfoRequestHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void DataBreakpointInfoRequestHandler::operator()(
116116
llvm::json::Array accessTypes{"read", "write", "readWrite"};
117117
const auto *arguments = request.getObject("arguments");
118118
const auto variablesReference =
119-
GetUnsigned(arguments, "variablesReference", 0);
119+
GetInteger<uint64_t>(arguments, "variablesReference").value_or(0);
120120
llvm::StringRef name = GetString(arguments, "name");
121121
lldb::SBFrame frame = dap.GetLLDBFrame(*arguments);
122122
lldb::SBValue variable = dap.variables.FindVariable(variablesReference, name);

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

Lines changed: 3 additions & 2 deletions
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 += GetInteger<int64_t>(arguments, "instructionOffset").value_or(0);
108108
lldb::SBAddress addr(addr_ptr, dap.target);
109109
if (!addr.IsValid()) {
110110
response["success"] = false;
@@ -113,7 +113,8 @@ void DisassembleRequestHandler::operator()(
113113
return;
114114
}
115115

116-
const auto inst_count = GetUnsigned(arguments, "instructionCount", 0);
116+
const auto inst_count =
117+
GetInteger<int64_t>(arguments, "instructionCount").value_or(0);
117118
lldb::SBInstructionList insts = dap.target.ReadInstructions(addr, inst_count);
118119

119120
if (!insts.IsValid()) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ void LocationsRequestHandler::operator()(
9797
FillResponse(request, response);
9898
auto *arguments = request.getObject("arguments");
9999

100-
uint64_t location_id = GetUnsigned(arguments, "locationReference", 0);
100+
const auto location_id =
101+
GetInteger<uint64_t>(arguments, "locationReference").value_or(0);
101102
// We use the lowest bit to distinguish between value location and declaration
102103
// location
103104
auto [var_ref, is_value_location] = UnpackLocation(location_id);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ void ReadMemoryRequestHandler::operator()(
108108
return;
109109
}
110110
lldb::addr_t addr_int = *addr_opt;
111-
addr_int += GetSigned(arguments, "offset", 0);
112-
const uint64_t count_requested = GetUnsigned(arguments, "count", 0);
111+
addr_int += GetInteger<uint64_t>(arguments, "offset").value_or(0);
112+
const uint64_t count_requested =
113+
GetInteger<uint64_t>(arguments, "count").value_or(0);
113114

114115
// We also need support reading 0 bytes
115116
// VS Code sends those requests to check if a `memoryReference`

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ RequestHandler::LaunchProcess(const llvm::json::Object &request) const {
184184
launch_info.SetDetachOnError(detachOnError);
185185
launch_info.SetLaunchFlags(flags | lldb::eLaunchFlagDebug |
186186
lldb::eLaunchFlagStopAtEntry);
187-
const uint64_t timeout_seconds = GetUnsigned(arguments, "timeout", 30);
187+
const auto timeout_seconds =
188+
GetInteger<uint64_t>(arguments, "timeout").value_or(30);
188189

189190
if (GetBoolean(arguments, "runInTerminal").value_or(false)) {
190191
if (llvm::Error err = RunInTerminal(dap, request, timeout_seconds))

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void SetVariableRequestHandler::operator()(
115115
const auto *arguments = request.getObject("arguments");
116116
// This is a reference to the containing variable/scope
117117
const auto variablesReference =
118-
GetUnsigned(arguments, "variablesReference", 0);
118+
GetInteger<uint64_t>(arguments, "variablesReference").value_or(0);
119119
llvm::StringRef name = GetString(arguments, "name");
120120

121121
const auto value = GetString(arguments, "value");
@@ -133,7 +133,8 @@ void SetVariableRequestHandler::operator()(
133133
// the name of the variable. We could have two shadowed variables with the
134134
// same name in "Locals" or "Globals". In our case the "id" absolute index
135135
// of the variable within the dap.variables list.
136-
const auto id_value = GetUnsigned(arguments, "id", UINT64_MAX);
136+
const auto id_value =
137+
GetInteger<uint64_t>(arguments, "id").value_or(UINT64_MAX);
137138
if (id_value != UINT64_MAX) {
138139
variable = dap.variables.GetVariable(id_value);
139140
} else {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ void SourceRequestHandler::operator()(const llvm::json::Object &request) const {
8585
const auto *arguments = request.getObject("arguments");
8686
const auto *source = arguments->getObject("source");
8787
llvm::json::Object body;
88-
int64_t source_ref = GetUnsigned(
89-
source, "sourceReference", GetUnsigned(arguments, "sourceReference", 0));
88+
const auto source_ref =
89+
GetInteger<uint64_t>(source, "sourceReference")
90+
.value_or(
91+
GetInteger<uint64_t>(arguments, "sourceReference").value_or(0));
9092

9193
if (source_ref) {
9294
lldb::SBProcess process = dap.target.GetProcess();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,9 @@ void StackTraceRequestHandler::operator()(
179179
llvm::json::Object body;
180180

181181
if (thread.IsValid()) {
182-
const auto start_frame = GetUnsigned(arguments, "startFrame", 0);
183-
const auto levels = GetUnsigned(arguments, "levels", 0);
182+
const auto start_frame =
183+
GetInteger<uint64_t>(arguments, "startFrame").value_or(0);
184+
const auto levels = GetInteger<uint64_t>(arguments, "levels").value_or(0);
184185
int64_t offset = 0;
185186
bool reached_end_of_stack =
186187
FillStackFrames(dap, thread, stack_frames, offset, start_frame,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ void StepInRequestHandler::operator()(const llvm::json::Object &request) const {
6969
const auto *arguments = request.getObject("arguments");
7070

7171
std::string step_in_target;
72-
uint64_t target_id = GetUnsigned(arguments, "targetId", 0);
72+
const auto target_id =
73+
GetInteger<uint64_t>(arguments, "targetId").value_or(0);
7374
auto it = dap.step_in_targets.find(target_id);
7475
if (it != dap.step_in_targets.end())
7576
step_in_target = it->second;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ void VariablesRequestHandler::operator()(
9393
llvm::json::Array variables;
9494
const auto *arguments = request.getObject("arguments");
9595
const auto variablesReference =
96-
GetUnsigned(arguments, "variablesReference", 0);
97-
const int64_t start = GetSigned(arguments, "start", 0);
98-
const int64_t count = GetSigned(arguments, "count", 0);
96+
GetInteger<uint64_t>(arguments, "variablesReference").value_or(0);
97+
const auto start = GetInteger<int64_t>(arguments, "start").value_or(0);
98+
const auto count = GetInteger<int64_t>(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(GetInteger<int64_t>(obj, "offset").value_or(0)) {
2424
GetString(obj, "instructionReference")
2525
.getAsInteger(0, instructionAddressReference);
2626
instructionAddressReference += offset;

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,6 @@ std::optional<bool> GetBoolean(const llvm::json::Object *obj,
116116
return std::nullopt;
117117
}
118118

119-
int64_t GetSigned(const llvm::json::Object &obj, llvm::StringRef key,
120-
int64_t fail_value) {
121-
if (auto value = obj.getInteger(key))
122-
return *value;
123-
return fail_value;
124-
}
125-
126-
int64_t GetSigned(const llvm::json::Object *obj, llvm::StringRef key,
127-
int64_t fail_value) {
128-
if (obj == nullptr)
129-
return fail_value;
130-
return GetSigned(*obj, key, fail_value);
131-
}
132-
133119
bool ObjectContainsKey(const llvm::json::Object &obj, llvm::StringRef key) {
134120
return obj.find(key) != obj.end();
135121
}
@@ -280,7 +266,7 @@ void FillResponse(const llvm::json::Object &request,
280266
response.try_emplace("type", "response");
281267
response.try_emplace("seq", (int64_t)0);
282268
EmplaceSafeString(response, "command", GetString(request, "command"));
283-
const int64_t seq = GetSigned(request, "seq", 0);
269+
const auto seq = GetInteger<int64_t>(request, "seq").value_or(0);
284270
response.try_emplace("request_seq", seq);
285271
response.try_emplace("success", true);
286272
}

lldb/tools/lldb-dap/JSONUtils.h

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ llvm::StringRef GetString(const llvm::json::Object &obj, llvm::StringRef key,
7474
llvm::StringRef GetString(const llvm::json::Object *obj, llvm::StringRef key,
7575
llvm::StringRef defaultValue = {});
7676

77-
/// Extract the unsigned integer value for the specified key from
78-
/// the specified object.
77+
/// Extract the integer value for the specified key from the specified object
78+
/// and return it as the specified integer type T.
7979
///
8080
/// \param[in] obj
8181
/// A JSON object that we will attempt to extract the value from
@@ -84,13 +84,23 @@ llvm::StringRef GetString(const llvm::json::Object *obj, llvm::StringRef key,
8484
/// The key to use when extracting the value
8585
///
8686
/// \return
87-
/// The unsigned integer value for the specified \a key, or
88-
/// \a fail_value if there is no key that matches or if the
89-
/// value is not an integer.
90-
uint64_t GetUnsigned(const llvm::json::Object &obj, llvm::StringRef key,
91-
uint64_t fail_value);
92-
uint64_t GetUnsigned(const llvm::json::Object *obj, llvm::StringRef key,
93-
uint64_t fail_value);
87+
/// The integer value for the specified \a key, or std::nullopt if there is
88+
/// no key that matches or if the value is not an integer.
89+
/// @{
90+
template <typename T>
91+
std::optional<T> GetInteger(const llvm::json::Object &obj,
92+
llvm::StringRef key) {
93+
return obj.getInteger(key);
94+
}
95+
96+
template <typename T>
97+
std::optional<T> GetInteger(const llvm::json::Object *obj,
98+
llvm::StringRef key) {
99+
if (obj != nullptr)
100+
return GetInteger<T>(*obj, key);
101+
return std::nullopt;
102+
}
103+
/// @}
94104

95105
/// Extract the boolean value for the specified key from the
96106
/// specified object.
@@ -112,24 +122,6 @@ std::optional<bool> GetBoolean(const llvm::json::Object *obj,
112122
llvm::StringRef key);
113123
/// @}
114124

115-
/// Extract the signed integer for the specified key from the
116-
/// specified object.
117-
///
118-
/// \param[in] obj
119-
/// A JSON object that we will attempt to extract the value from
120-
///
121-
/// \param[in] key
122-
/// The key to use when extracting the value
123-
///
124-
/// \return
125-
/// The signed integer value for the specified \a key, or
126-
/// \a fail_value if there is no key that matches or if the
127-
/// 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);
132-
133125
/// Check if the specified key exists in the specified object.
134126
///
135127
/// \param[in] obj

lldb/tools/lldb-dap/SourceBreakpoint.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ namespace lldb_dap {
2727
SourceBreakpoint::SourceBreakpoint(DAP &dap, const llvm::json::Object &obj)
2828
: Breakpoint(dap, obj),
2929
logMessage(std::string(GetString(obj, "logMessage"))),
30-
line(GetUnsigned(obj, "line", 0)), column(GetUnsigned(obj, "column", 0)) {
31-
}
30+
line(GetInteger<uint64_t>(obj, "line").value_or(0)),
31+
column(GetInteger<uint64_t>(obj, "column").value_or(0)) {}
3232

3333
void SourceBreakpoint::SetBreakpoint(const llvm::StringRef source_path) {
3434
lldb::SBFileSpecList module_list;

0 commit comments

Comments
 (0)