Skip to content

Commit 217fc65

Browse files
authored
[lldb][lldb-dap] Return optional from json utils (#129919)
Completion of #129818 Did not want to put `llvm::StringRef()` as the default value instead it was `""`
1 parent 86329ba commit 217fc65

20 files changed

+78
-71
lines changed

lldb/tools/lldb-dap/BreakpointBase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
using namespace lldb_dap;
1414

1515
BreakpointBase::BreakpointBase(DAP &d, const llvm::json::Object &obj)
16-
: dap(d), condition(std::string(GetString(obj, "condition"))),
17-
hitCondition(std::string(GetString(obj, "hitCondition"))) {}
16+
: dap(d), condition(std::string(GetString(obj, "condition").value_or(""))),
17+
hitCondition(std::string(GetString(obj, "hitCondition").value_or(""))) {}
1818

1919
void BreakpointBase::UpdateBreakpoint(const BreakpointBase &request_bp) {
2020
if (condition != request_bp.condition) {

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,11 @@ DAP::CreateTargetFromArguments(const llvm::json::Object &arguments,
630630
// enough information to determine correct arch and platform (or ELF can be
631631
// omitted at all), so it is good to leave the user an apportunity to specify
632632
// those. Any of those three can be left empty.
633-
llvm::StringRef target_triple = GetString(arguments, "targetTriple");
634-
llvm::StringRef platform_name = GetString(arguments, "platformName");
635-
llvm::StringRef program = GetString(arguments, "program");
633+
const llvm::StringRef target_triple =
634+
GetString(arguments, "targetTriple").value_or("");
635+
const llvm::StringRef platform_name =
636+
GetString(arguments, "platformName").value_or("");
637+
const llvm::StringRef program = GetString(arguments, "program").value_or("");
636638
auto target = this->debugger.CreateTarget(
637639
program.data(), target_triple.data(), platform_name.data(),
638640
true, // Add dependent modules.
@@ -668,7 +670,7 @@ bool DAP::HandleObject(const protocol::Message &M) {
668670
llvm::json::Object object = *v.getAsObject();
669671
const auto packet_type = GetString(object, "type");
670672
if (packet_type == "request") {
671-
const auto command = GetString(object, "command");
673+
const auto command = GetString(object, "command").value_or("");
672674

673675
auto new_handler_pos = request_handlers.find(command);
674676
if (new_handler_pos != request_handlers.end()) {
@@ -704,7 +706,7 @@ bool DAP::HandleObject(const protocol::Message &M) {
704706
Result = std::move(*B);
705707
(*response_handler)(Result);
706708
} else {
707-
llvm::StringRef message = GetString(object, "message");
709+
llvm::StringRef message = GetString(object, "message").value_or("");
708710
if (message.empty()) {
709711
message = "Unknown error, response failed";
710712
}

lldb/tools/lldb-dap/FunctionBreakpoint.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
namespace lldb_dap {
1414

1515
FunctionBreakpoint::FunctionBreakpoint(DAP &d, const llvm::json::Object &obj)
16-
: Breakpoint(d, obj), functionName(std::string(GetString(obj, "name"))) {}
16+
: Breakpoint(d, obj),
17+
functionName(std::string(GetString(obj, "name").value_or(""))) {}
1718

1819
void FunctionBreakpoint::SetBreakpoint() {
1920
if (functionName.empty())

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void AttachRequestHandler::operator()(const llvm::json::Object &request) const {
5858
const auto gdb_remote_port =
5959
GetInteger<uint64_t>(arguments, "gdb-remote-port").value_or(invalid_port);
6060
const auto gdb_remote_hostname =
61-
GetString(arguments, "gdb-remote-hostname", "localhost");
61+
GetString(arguments, "gdb-remote-hostname").value_or("localhost");
6262
if (pid != LLDB_INVALID_PROCESS_ID)
6363
attach_info.SetProcessID(pid);
6464
const auto wait_for = GetBoolean(arguments, "waitFor").value_or(false);
@@ -69,23 +69,25 @@ void AttachRequestHandler::operator()(const llvm::json::Object &request) const {
6969
dap.exit_commands = GetStrings(arguments, "exitCommands");
7070
dap.terminate_commands = GetStrings(arguments, "terminateCommands");
7171
auto attachCommands = GetStrings(arguments, "attachCommands");
72-
llvm::StringRef core_file = GetString(arguments, "coreFile");
73-
const auto timeout_seconds =
72+
llvm::StringRef core_file = GetString(arguments, "coreFile").value_or("");
73+
const uint64_t timeout_seconds =
7474
GetInteger<uint64_t>(arguments, "timeout").value_or(30);
7575
dap.stop_at_entry = core_file.empty()
7676
? GetBoolean(arguments, "stopOnEntry").value_or(false)
7777
: true;
7878
dap.post_run_commands = GetStrings(arguments, "postRunCommands");
79-
const llvm::StringRef debuggerRoot = GetString(arguments, "debuggerRoot");
79+
const llvm::StringRef debuggerRoot =
80+
GetString(arguments, "debuggerRoot").value_or("");
8081
dap.enable_auto_variable_summaries =
8182
GetBoolean(arguments, "enableAutoVariableSummaries").value_or(false);
8283
dap.enable_synthetic_child_debugging =
8384
GetBoolean(arguments, "enableSyntheticChildDebugging").value_or(false);
8485
dap.display_extended_backtrace =
8586
GetBoolean(arguments, "displayExtendedBacktrace").value_or(false);
86-
dap.command_escape_prefix = GetString(arguments, "commandEscapePrefix", "`");
87-
dap.SetFrameFormat(GetString(arguments, "customFrameFormat"));
88-
dap.SetThreadFormat(GetString(arguments, "customThreadFormat"));
87+
dap.command_escape_prefix =
88+
GetString(arguments, "commandEscapePrefix").value_or("`");
89+
dap.SetFrameFormat(GetString(arguments, "customFrameFormat").value_or(""));
90+
dap.SetThreadFormat(GetString(arguments, "customThreadFormat").value_or(""));
8991

9092
PrintWelcomeMessage();
9193

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void BreakpointLocationsRequestHandler::operator()(
130130
FillResponse(request, response);
131131
auto *arguments = request.getObject("arguments");
132132
auto *source = arguments->getObject("source");
133-
std::string path = GetString(source, "path").str();
133+
std::string path = GetString(source, "path").value_or("").str();
134134
const auto start_line = GetInteger<uint64_t>(arguments, "line")
135135
.value_or(LLDB_INVALID_LINE_NUMBER);
136136
const auto start_column = GetInteger<uint64_t>(arguments, "column")

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ void CompileUnitsRequestHandler::operator()(
6060
llvm::json::Object body;
6161
llvm::json::Array units;
6262
const auto *arguments = request.getObject("arguments");
63-
std::string module_id = std::string(GetString(arguments, "moduleId"));
63+
const std::string module_id =
64+
GetString(arguments, "moduleId").value_or("").str();
6465
int num_modules = dap.target.GetNumModules();
6566
for (int i = 0; i < num_modules; i++) {
6667
auto curr_module = dap.target.GetModuleAtIndex(i);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void CompletionsRequestHandler::operator()(
142142
frame.GetThread().SetSelectedFrame(frame.GetFrameID());
143143
}
144144

145-
std::string text = GetString(arguments, "text").str();
145+
std::string text = GetString(arguments, "text").value_or("").str();
146146
auto original_column =
147147
GetInteger<int64_t>(arguments, "column").value_or(text.size());
148148
auto original_line = GetInteger<int64_t>(arguments, "line").value_or(1);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void DataBreakpointInfoRequestHandler::operator()(
117117
const auto *arguments = request.getObject("arguments");
118118
const auto variablesReference =
119119
GetInteger<uint64_t>(arguments, "variablesReference").value_or(0);
120-
llvm::StringRef name = GetString(arguments, "name");
120+
llvm::StringRef name = GetString(arguments, "name").value_or("");
121121
lldb::SBFrame frame = dap.GetLLDBFrame(*arguments);
122122
lldb::SBValue variable = dap.variables.FindVariable(variablesReference, name);
123123
std::string addr, size;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ void DisassembleRequestHandler::operator()(
9393
FillResponse(request, response);
9494
auto *arguments = request.getObject("arguments");
9595

96-
llvm::StringRef memoryReference = GetString(arguments, "memoryReference");
96+
llvm::StringRef memoryReference =
97+
GetString(arguments, "memoryReference").value_or("");
9798
auto addr_opt = DecodeMemoryReference(memoryReference);
9899
if (!addr_opt.has_value()) {
99100
response["success"] = false;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ void EvaluateRequestHandler::operator()(
145145
llvm::json::Object body;
146146
const auto *arguments = request.getObject("arguments");
147147
lldb::SBFrame frame = dap.GetLLDBFrame(*arguments);
148-
std::string expression = GetString(arguments, "expression").str();
149-
llvm::StringRef context = GetString(arguments, "context");
148+
std::string expression =
149+
GetString(arguments, "expression").value_or("").str();
150+
const llvm::StringRef context = GetString(arguments, "context").value_or("");
150151
bool repeat_last_command =
151152
expression.empty() && dap.last_nonempty_var_expression.empty();
152153

0 commit comments

Comments
 (0)