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 {

0 commit comments

Comments
 (0)