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

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,18 @@ void LaunchRequestHandler::operator()(const llvm::json::Object &request) const {
6161
dap.terminate_commands = GetStrings(arguments, "terminateCommands");
6262
dap.post_run_commands = GetStrings(arguments, "postRunCommands");
6363
dap.stop_at_entry = GetBoolean(arguments, "stopOnEntry").value_or(false);
64-
const llvm::StringRef debuggerRoot = GetString(arguments, "debuggerRoot");
64+
const llvm::StringRef debuggerRoot =
65+
GetString(arguments, "debuggerRoot").value_or("");
6566
dap.enable_auto_variable_summaries =
6667
GetBoolean(arguments, "enableAutoVariableSummaries").value_or(false);
6768
dap.enable_synthetic_child_debugging =
6869
GetBoolean(arguments, "enableSyntheticChildDebugging").value_or(false);
6970
dap.display_extended_backtrace =
7071
GetBoolean(arguments, "displayExtendedBacktrace").value_or(false);
71-
dap.command_escape_prefix = GetString(arguments, "commandEscapePrefix", "`");
72-
dap.SetFrameFormat(GetString(arguments, "customFrameFormat"));
73-
dap.SetThreadFormat(GetString(arguments, "customThreadFormat"));
72+
dap.command_escape_prefix =
73+
GetString(arguments, "commandEscapePrefix").value_or("`");
74+
dap.SetFrameFormat(GetString(arguments, "customFrameFormat").value_or(""));
75+
dap.SetThreadFormat(GetString(arguments, "customThreadFormat").value_or(""));
7476

7577
PrintWelcomeMessage();
7678

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

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

101-
llvm::StringRef memoryReference = GetString(arguments, "memoryReference");
101+
llvm::StringRef memoryReference =
102+
GetString(arguments, "memoryReference").value_or("");
102103
auto addr_opt = DecodeMemoryReference(memoryReference);
103104
if (!addr_opt.has_value()) {
104105
response["success"] = false;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void RequestHandler::SetSourceMapFromArguments(
5454
std::string sourceMapCommand;
5555
llvm::raw_string_ostream strm(sourceMapCommand);
5656
strm << "settings set target.source-map ";
57-
const auto sourcePath = GetString(arguments, "sourcePath");
57+
const auto sourcePath = GetString(arguments, "sourcePath").value_or("");
5858

5959
// sourceMap is the new, more general form of sourcePath and overrides it.
6060
constexpr llvm::StringRef sourceMapKey = "sourceMap";
@@ -169,7 +169,7 @@ RequestHandler::LaunchProcess(const llvm::json::Object &request) const {
169169

170170
// Grab the current working directory if there is one and set it in the
171171
// launch info.
172-
const auto cwd = GetString(arguments, "cwd");
172+
const auto cwd = GetString(arguments, "cwd").value_or("");
173173
if (!cwd.empty())
174174
launch_info.SetWorkingDirectory(cwd.data());
175175

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void SetBreakpointsRequestHandler::operator()(
130130
FillResponse(request, response);
131131
const auto *arguments = request.getObject("arguments");
132132
const auto *source = arguments->getObject("source");
133-
const auto path = GetString(source, "path");
133+
const auto path = GetString(source, "path").value_or("");
134134
const auto *breakpoints = arguments->getArray("breakpoints");
135135
llvm::json::Array response_breakpoints;
136136

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ void SetVariableRequestHandler::operator()(
116116
// This is a reference to the containing variable/scope
117117
const auto variablesReference =
118118
GetInteger<uint64_t>(arguments, "variablesReference").value_or(0);
119-
llvm::StringRef name = GetString(arguments, "name");
119+
llvm::StringRef name = GetString(arguments, "name").value_or("");
120120

121-
const auto value = GetString(arguments, "value");
121+
const auto value = GetString(arguments, "value").value_or("");
122122
// Set success to false just in case we don't find the variable by name
123123
response.try_emplace("success", false);
124124

lldb/tools/lldb-dap/InstructionBreakpoint.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ InstructionBreakpoint::InstructionBreakpoint(DAP &d,
2222
: Breakpoint(d, obj), instructionAddressReference(LLDB_INVALID_ADDRESS),
2323
offset(GetInteger<int64_t>(obj, "offset").value_or(0)) {
2424
GetString(obj, "instructionReference")
25+
.value_or("")
2526
.getAsInteger(0, instructionAddressReference);
2627
instructionAddressReference += offset;
2728
}

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,34 +70,17 @@ llvm::StringRef GetAsString(const llvm::json::Value &value) {
7070
}
7171

7272
// Gets a string from a JSON object using the key, or returns an empty string.
73-
llvm::StringRef GetString(const llvm::json::Object &obj, llvm::StringRef key,
74-
llvm::StringRef defaultValue) {
75-
if (std::optional<llvm::StringRef> value = obj.getString(key))
76-
return *value;
77-
return defaultValue;
73+
std::optional<llvm::StringRef> GetString(const llvm::json::Object &obj,
74+
llvm::StringRef key) {
75+
return obj.getString(key);
7876
}
7977

80-
llvm::StringRef GetString(const llvm::json::Object *obj, llvm::StringRef key,
81-
llvm::StringRef defaultValue) {
78+
std::optional<llvm::StringRef> GetString(const llvm::json::Object *obj,
79+
llvm::StringRef key) {
8280
if (obj == nullptr)
83-
return defaultValue;
84-
return GetString(*obj, key, defaultValue);
85-
}
86-
87-
// Gets an unsigned integer from a JSON object using the key, or returns the
88-
// specified fail value.
89-
uint64_t GetUnsigned(const llvm::json::Object &obj, llvm::StringRef key,
90-
uint64_t fail_value) {
91-
if (auto value = obj.getInteger(key))
92-
return (uint64_t)*value;
93-
return fail_value;
94-
}
81+
return std::nullopt;
9582

96-
uint64_t GetUnsigned(const llvm::json::Object *obj, llvm::StringRef key,
97-
uint64_t fail_value) {
98-
if (obj == nullptr)
99-
return fail_value;
100-
return GetUnsigned(*obj, key, fail_value);
83+
return GetString(*obj, key);
10184
}
10285

10386
std::optional<bool> GetBoolean(const llvm::json::Object &obj,
@@ -116,6 +99,19 @@ std::optional<bool> GetBoolean(const llvm::json::Object *obj,
11699
return std::nullopt;
117100
}
118101

102+
std::optional<int64_t> GetSigned(const llvm::json::Object &obj,
103+
llvm::StringRef key) {
104+
return obj.getInteger(key);
105+
}
106+
107+
std::optional<int64_t> GetSigned(const llvm::json::Object *obj,
108+
llvm::StringRef key) {
109+
if (obj == nullptr)
110+
return std::nullopt;
111+
112+
return GetSigned(*obj, key);
113+
}
114+
119115
bool ObjectContainsKey(const llvm::json::Object &obj, llvm::StringRef key) {
120116
return obj.find(key) != obj.end();
121117
}
@@ -265,8 +261,9 @@ void FillResponse(const llvm::json::Object &request,
265261
// to true by default.
266262
response.try_emplace("type", "response");
267263
response.try_emplace("seq", (int64_t)0);
268-
EmplaceSafeString(response, "command", GetString(request, "command"));
269-
const auto seq = GetInteger<int64_t>(request, "seq").value_or(0);
264+
EmplaceSafeString(response, "command",
265+
GetString(request, "command").value_or(""));
266+
const int64_t seq = GetSigned(request, "seq").value_or(0);
270267
response.try_emplace("request_seq", seq);
271268
response.try_emplace("success", true);
272269
}
@@ -1439,13 +1436,14 @@ CreateRunInTerminalReverseRequest(const llvm::json::Object &launch_request,
14391436
args.push_back(std::to_string(debugger_pid));
14401437
}
14411438
args.push_back("--launch-target");
1442-
args.push_back(GetString(launch_request_arguments, "program").str());
1439+
args.push_back(
1440+
GetString(launch_request_arguments, "program").value_or("").str());
14431441
std::vector<std::string> target_args =
14441442
GetStrings(launch_request_arguments, "args");
14451443
args.insert(args.end(), target_args.begin(), target_args.end());
14461444
run_in_terminal_args.try_emplace("args", args);
14471445

1448-
const auto cwd = GetString(launch_request_arguments, "cwd");
1446+
const auto cwd = GetString(launch_request_arguments, "cwd").value_or("");
14491447
if (!cwd.empty())
14501448
run_in_terminal_args.try_emplace("cwd", cwd);
14511449

lldb/tools/lldb-dap/JSONUtils.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,14 @@ llvm::StringRef GetAsString(const llvm::json::Value &value);
6262
/// \param[in] key
6363
/// The key to use when extracting the value
6464
///
65-
/// \param[in] defaultValue
66-
/// The default value to return if the key is not present
67-
///
6865
/// \return
6966
/// A llvm::StringRef that contains the string value for the
70-
/// specified \a key, or the default value if there is no key that
67+
/// specified \a key, or \a std::nullopt if there is no key that
7168
/// matches or if the value is not a string.
72-
llvm::StringRef GetString(const llvm::json::Object &obj, llvm::StringRef key,
73-
llvm::StringRef defaultValue = {});
74-
llvm::StringRef GetString(const llvm::json::Object *obj, llvm::StringRef key,
75-
llvm::StringRef defaultValue = {});
69+
std::optional<llvm::StringRef> GetString(const llvm::json::Object &obj,
70+
llvm::StringRef key);
71+
std::optional<llvm::StringRef> GetString(const llvm::json::Object *obj,
72+
llvm::StringRef key);
7673

7774
/// Extract the integer value for the specified key from the specified object
7875
/// and return it as the specified integer type T.

lldb/tools/lldb-dap/SourceBreakpoint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace lldb_dap {
2626

2727
SourceBreakpoint::SourceBreakpoint(DAP &dap, const llvm::json::Object &obj)
2828
: Breakpoint(dap, obj),
29-
logMessage(std::string(GetString(obj, "logMessage"))),
29+
logMessage(GetString(obj, "logMessage").value_or("").str()),
3030
line(
3131
GetInteger<uint64_t>(obj, "line").value_or(LLDB_INVALID_LINE_NUMBER)),
3232
column(GetInteger<uint64_t>(obj, "column")

lldb/tools/lldb-dap/Watchpoint.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
namespace lldb_dap {
2121
Watchpoint::Watchpoint(DAP &d, const llvm::json::Object &obj)
2222
: BreakpointBase(d, obj) {
23-
llvm::StringRef dataId = GetString(obj, "dataId");
24-
std::string accessType = GetString(obj, "accessType").str();
23+
llvm::StringRef dataId = GetString(obj, "dataId").value_or("");
24+
std::string accessType = GetString(obj, "accessType").value_or("").str();
2525
auto [addr_str, size_str] = dataId.split('/');
2626
llvm::to_integer(addr_str, addr, 16);
2727
llvm::to_integer(size_str, size);

0 commit comments

Comments
 (0)