Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lldb/tools/lldb-dap/BreakpointBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
using namespace lldb_dap;

BreakpointBase::BreakpointBase(DAP &d, const llvm::json::Object &obj)
: dap(d), condition(std::string(GetString(obj, "condition"))),
hitCondition(std::string(GetString(obj, "hitCondition"))) {}
: dap(d), condition(std::string(GetString(obj, "condition").value_or(""))),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that instead of "" you should use {}, which would create an empty StringRef without an actual string being backed by it. That is closer to the original source code.
I don't think why "" would break with your patch, but if there's a place that does GetString().GetData() == nullptr, then there would be some divergence. So I'd rather play safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did try that before, but I doesn't work because at the position the compiler sees it as an initializer list. and llvm::StringRef does not have an initializer-list constructor.

in the previous fuction
GetString(..... llvm::StringRef default_value = { } ) here it is treated as llvm::StringRef{} and can infer it.

hitCondition(std::string(GetString(obj, "hitCondition").value_or(""))) {}

void BreakpointBase::UpdateBreakpoint(const BreakpointBase &request_bp) {
if (condition != request_bp.condition) {
Expand Down
14 changes: 8 additions & 6 deletions lldb/tools/lldb-dap/DAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,9 +672,11 @@ DAP::CreateTargetFromArguments(const llvm::json::Object &arguments,
// enough information to determine correct arch and platform (or ELF can be
// omitted at all), so it is good to leave the user an apportunity to specify
// those. Any of those three can be left empty.
llvm::StringRef target_triple = GetString(arguments, "targetTriple");
llvm::StringRef platform_name = GetString(arguments, "platformName");
llvm::StringRef program = GetString(arguments, "program");
const llvm::StringRef target_triple =
GetString(arguments, "targetTriple").value_or("");
const llvm::StringRef platform_name =
GetString(arguments, "platformName").value_or("");
const llvm::StringRef program = GetString(arguments, "program").value_or("");
auto target = this->debugger.CreateTarget(
program.data(), target_triple.data(), platform_name.data(),
true, // Add dependent modules.
Expand Down Expand Up @@ -734,9 +736,9 @@ PacketStatus DAP::GetNextObject(llvm::json::Object &object) {
}

bool DAP::HandleObject(const llvm::json::Object &object) {
const auto packet_type = GetString(object, "type");
const auto packet_type = GetString(object, "type").value_or("");
if (packet_type == "request") {
const auto command = GetString(object, "command");
const auto command = GetString(object, "command").value_or("");

auto new_handler_pos = request_handlers.find(command);
if (new_handler_pos != request_handlers.end()) {
Expand Down Expand Up @@ -774,7 +776,7 @@ bool DAP::HandleObject(const llvm::json::Object &object) {
}
(*response_handler)(Result);
} else {
llvm::StringRef message = GetString(object, "message");
llvm::StringRef message = GetString(object, "message").value_or("");
if (message.empty()) {
message = "Unknown error, response failed";
}
Expand Down
3 changes: 2 additions & 1 deletion lldb/tools/lldb-dap/FunctionBreakpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
namespace lldb_dap {

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

void FunctionBreakpoint::SetBreakpoint() {
if (functionName.empty())
Expand Down
16 changes: 9 additions & 7 deletions lldb/tools/lldb-dap/Handler/AttachRequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void AttachRequestHandler::operator()(const llvm::json::Object &request) const {
const auto gdb_remote_port =
GetInteger<uint64_t>(arguments, "gdb-remote-port").value_or(invalid_port);
const auto gdb_remote_hostname =
GetString(arguments, "gdb-remote-hostname", "localhost");
GetString(arguments, "gdb-remote-hostname").value_or("localhost");
if (pid != LLDB_INVALID_PROCESS_ID)
attach_info.SetProcessID(pid);
const auto wait_for = GetBoolean(arguments, "waitFor").value_or(false);
Expand All @@ -69,23 +69,25 @@ void AttachRequestHandler::operator()(const llvm::json::Object &request) const {
dap.exit_commands = GetStrings(arguments, "exitCommands");
dap.terminate_commands = GetStrings(arguments, "terminateCommands");
auto attachCommands = GetStrings(arguments, "attachCommands");
llvm::StringRef core_file = GetString(arguments, "coreFile");
const auto timeout_seconds =
llvm::StringRef core_file = GetString(arguments, "coreFile").value_or("");
const uint64_t timeout_seconds =
GetInteger<uint64_t>(arguments, "timeout").value_or(30);
dap.stop_at_entry = core_file.empty()
? GetBoolean(arguments, "stopOnEntry").value_or(false)
: true;
dap.post_run_commands = GetStrings(arguments, "postRunCommands");
const llvm::StringRef debuggerRoot = GetString(arguments, "debuggerRoot");
const llvm::StringRef debuggerRoot =
GetString(arguments, "debuggerRoot").value_or("");
dap.enable_auto_variable_summaries =
GetBoolean(arguments, "enableAutoVariableSummaries").value_or(false);
dap.enable_synthetic_child_debugging =
GetBoolean(arguments, "enableSyntheticChildDebugging").value_or(false);
dap.display_extended_backtrace =
GetBoolean(arguments, "displayExtendedBacktrace").value_or(false);
dap.command_escape_prefix = GetString(arguments, "commandEscapePrefix", "`");
dap.SetFrameFormat(GetString(arguments, "customFrameFormat"));
dap.SetThreadFormat(GetString(arguments, "customThreadFormat"));
dap.command_escape_prefix =
GetString(arguments, "commandEscapePrefix").value_or("`");
dap.SetFrameFormat(GetString(arguments, "customFrameFormat").value_or(""));
dap.SetThreadFormat(GetString(arguments, "customThreadFormat").value_or(""));

PrintWelcomeMessage();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void BreakpointLocationsRequestHandler::operator()(
FillResponse(request, response);
auto *arguments = request.getObject("arguments");
auto *source = arguments->getObject("source");
std::string path = GetString(source, "path").str();
std::string path = GetString(source, "path").value_or("").str();
const auto start_line = GetInteger<uint64_t>(arguments, "line")
.value_or(LLDB_INVALID_LINE_NUMBER);
const auto start_column = GetInteger<uint64_t>(arguments, "column")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ void CompileUnitsRequestHandler::operator()(
llvm::json::Object body;
llvm::json::Array units;
const auto *arguments = request.getObject("arguments");
std::string module_id = std::string(GetString(arguments, "moduleId"));
const std::string module_id =
GetString(arguments, "moduleId").value_or("").str();
int num_modules = dap.target.GetNumModules();
for (int i = 0; i < num_modules; i++) {
auto curr_module = dap.target.GetModuleAtIndex(i);
Expand Down
2 changes: 1 addition & 1 deletion lldb/tools/lldb-dap/Handler/CompletionsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void CompletionsRequestHandler::operator()(
frame.GetThread().SetSelectedFrame(frame.GetFrameID());
}

std::string text = GetString(arguments, "text").str();
std::string text = GetString(arguments, "text").value_or("").str();
auto original_column =
GetInteger<int64_t>(arguments, "column").value_or(text.size());
auto original_line = GetInteger<int64_t>(arguments, "line").value_or(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void DataBreakpointInfoRequestHandler::operator()(
const auto *arguments = request.getObject("arguments");
const auto variablesReference =
GetInteger<uint64_t>(arguments, "variablesReference").value_or(0);
llvm::StringRef name = GetString(arguments, "name");
llvm::StringRef name = GetString(arguments, "name").value_or("");
lldb::SBFrame frame = dap.GetLLDBFrame(*arguments);
lldb::SBValue variable = dap.variables.FindVariable(variablesReference, name);
std::string addr, size;
Expand Down
3 changes: 2 additions & 1 deletion lldb/tools/lldb-dap/Handler/DisassembleRequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ void DisassembleRequestHandler::operator()(
FillResponse(request, response);
auto *arguments = request.getObject("arguments");

llvm::StringRef memoryReference = GetString(arguments, "memoryReference");
llvm::StringRef memoryReference =
GetString(arguments, "memoryReference").value_or("");
auto addr_opt = DecodeMemoryReference(memoryReference);
if (!addr_opt.has_value()) {
response["success"] = false;
Expand Down
5 changes: 3 additions & 2 deletions lldb/tools/lldb-dap/Handler/EvaluateRequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ void EvaluateRequestHandler::operator()(
llvm::json::Object body;
const auto *arguments = request.getObject("arguments");
lldb::SBFrame frame = dap.GetLLDBFrame(*arguments);
std::string expression = GetString(arguments, "expression").str();
llvm::StringRef context = GetString(arguments, "context");
std::string expression =
GetString(arguments, "expression").value_or("").str();
const llvm::StringRef context = GetString(arguments, "context").value_or("");
bool repeat_last_command =
expression.empty() && dap.last_nonempty_var_expression.empty();

Expand Down
10 changes: 6 additions & 4 deletions lldb/tools/lldb-dap/Handler/LaunchRequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,18 @@ void LaunchRequestHandler::operator()(const llvm::json::Object &request) const {
dap.terminate_commands = GetStrings(arguments, "terminateCommands");
dap.post_run_commands = GetStrings(arguments, "postRunCommands");
dap.stop_at_entry = GetBoolean(arguments, "stopOnEntry").value_or(false);
const llvm::StringRef debuggerRoot = GetString(arguments, "debuggerRoot");
const llvm::StringRef debuggerRoot =
GetString(arguments, "debuggerRoot").value_or("");
dap.enable_auto_variable_summaries =
GetBoolean(arguments, "enableAutoVariableSummaries").value_or(false);
dap.enable_synthetic_child_debugging =
GetBoolean(arguments, "enableSyntheticChildDebugging").value_or(false);
dap.display_extended_backtrace =
GetBoolean(arguments, "displayExtendedBacktrace").value_or(false);
dap.command_escape_prefix = GetString(arguments, "commandEscapePrefix", "`");
dap.SetFrameFormat(GetString(arguments, "customFrameFormat"));
dap.SetThreadFormat(GetString(arguments, "customThreadFormat"));
dap.command_escape_prefix =
GetString(arguments, "commandEscapePrefix").value_or("`");
dap.SetFrameFormat(GetString(arguments, "customFrameFormat").value_or(""));
dap.SetThreadFormat(GetString(arguments, "customThreadFormat").value_or(""));

PrintWelcomeMessage();

Expand Down
3 changes: 2 additions & 1 deletion lldb/tools/lldb-dap/Handler/ReadMemoryRequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ void ReadMemoryRequestHandler::operator()(
FillResponse(request, response);
auto *arguments = request.getObject("arguments");

llvm::StringRef memoryReference = GetString(arguments, "memoryReference");
llvm::StringRef memoryReference =
GetString(arguments, "memoryReference").value_or("");
auto addr_opt = DecodeMemoryReference(memoryReference);
if (!addr_opt.has_value()) {
response["success"] = false;
Expand Down
4 changes: 2 additions & 2 deletions lldb/tools/lldb-dap/Handler/RequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void RequestHandler::SetSourceMapFromArguments(
std::string sourceMapCommand;
llvm::raw_string_ostream strm(sourceMapCommand);
strm << "settings set target.source-map ";
const auto sourcePath = GetString(arguments, "sourcePath");
const auto sourcePath = GetString(arguments, "sourcePath").value_or("");

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void SetBreakpointsRequestHandler::operator()(
FillResponse(request, response);
const auto *arguments = request.getObject("arguments");
const auto *source = arguments->getObject("source");
const auto path = GetString(source, "path");
const auto path = GetString(source, "path").value_or("");
const auto *breakpoints = arguments->getArray("breakpoints");
llvm::json::Array response_breakpoints;

Expand Down
4 changes: 2 additions & 2 deletions lldb/tools/lldb-dap/Handler/SetVariableRequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ void SetVariableRequestHandler::operator()(
// This is a reference to the containing variable/scope
const auto variablesReference =
GetInteger<uint64_t>(arguments, "variablesReference").value_or(0);
llvm::StringRef name = GetString(arguments, "name");
llvm::StringRef name = GetString(arguments, "name").value_or("");

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

Expand Down
1 change: 1 addition & 0 deletions lldb/tools/lldb-dap/InstructionBreakpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ InstructionBreakpoint::InstructionBreakpoint(DAP &d,
: Breakpoint(d, obj), instructionAddressReference(LLDB_INVALID_ADDRESS),
offset(GetInteger<int64_t>(obj, "offset").value_or(0)) {
GetString(obj, "instructionReference")
.value_or("")
.getAsInteger(0, instructionAddressReference);
instructionAddressReference += offset;
}
Expand Down
54 changes: 26 additions & 28 deletions lldb/tools/lldb-dap/JSONUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,17 @@ llvm::StringRef GetAsString(const llvm::json::Value &value) {
}

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

llvm::StringRef GetString(const llvm::json::Object *obj, llvm::StringRef key,
llvm::StringRef defaultValue) {
std::optional<llvm::StringRef> GetString(const llvm::json::Object *obj,
llvm::StringRef key) {
if (obj == nullptr)
return defaultValue;
return GetString(*obj, key, defaultValue);
}

// Gets an unsigned integer from a JSON object using the key, or returns the
// specified fail value.
uint64_t GetUnsigned(const llvm::json::Object &obj, llvm::StringRef key,
uint64_t fail_value) {
if (auto value = obj.getInteger(key))
return (uint64_t)*value;
return fail_value;
}
return std::nullopt;

uint64_t GetUnsigned(const llvm::json::Object *obj, llvm::StringRef key,
uint64_t fail_value) {
if (obj == nullptr)
return fail_value;
return GetUnsigned(*obj, key, fail_value);
return GetString(*obj, key);
}

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

std::optional<int64_t> GetSigned(const llvm::json::Object &obj,
llvm::StringRef key) {
return obj.getInteger(key);
}

std::optional<int64_t> GetSigned(const llvm::json::Object *obj,
llvm::StringRef key) {
if (obj == nullptr)
return std::nullopt;

return GetSigned(*obj, key);
}

bool ObjectContainsKey(const llvm::json::Object &obj, llvm::StringRef key) {
return obj.find(key) != obj.end();
}
Expand Down Expand Up @@ -265,8 +261,9 @@ void FillResponse(const llvm::json::Object &request,
// to true by default.
response.try_emplace("type", "response");
response.try_emplace("seq", (int64_t)0);
EmplaceSafeString(response, "command", GetString(request, "command"));
const auto seq = GetInteger<int64_t>(request, "seq").value_or(0);
EmplaceSafeString(response, "command",
GetString(request, "command").value_or(""));
const int64_t seq = GetSigned(request, "seq").value_or(0);
response.try_emplace("request_seq", seq);
response.try_emplace("success", true);
}
Expand Down Expand Up @@ -1439,13 +1436,14 @@ CreateRunInTerminalReverseRequest(const llvm::json::Object &launch_request,
args.push_back(std::to_string(debugger_pid));
}
args.push_back("--launch-target");
args.push_back(GetString(launch_request_arguments, "program").str());
args.push_back(
GetString(launch_request_arguments, "program").value_or("").str());
std::vector<std::string> target_args =
GetStrings(launch_request_arguments, "args");
args.insert(args.end(), target_args.begin(), target_args.end());
run_in_terminal_args.try_emplace("args", args);

const auto cwd = GetString(launch_request_arguments, "cwd");
const auto cwd = GetString(launch_request_arguments, "cwd").value_or("");
if (!cwd.empty())
run_in_terminal_args.try_emplace("cwd", cwd);

Expand Down
13 changes: 5 additions & 8 deletions lldb/tools/lldb-dap/JSONUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,14 @@ llvm::StringRef GetAsString(const llvm::json::Value &value);
/// \param[in] key
/// The key to use when extracting the value
///
/// \param[in] defaultValue
/// The default value to return if the key is not present
///
/// \return
/// A llvm::StringRef that contains the string value for the
/// specified \a key, or the default value if there is no key that
/// specified \a key, or \a std::nullopt if there is no key that
/// matches or if the value is not a string.
llvm::StringRef GetString(const llvm::json::Object &obj, llvm::StringRef key,
llvm::StringRef defaultValue = {});
llvm::StringRef GetString(const llvm::json::Object *obj, llvm::StringRef key,
llvm::StringRef defaultValue = {});
std::optional<llvm::StringRef> GetString(const llvm::json::Object &obj,
llvm::StringRef key);
std::optional<llvm::StringRef> GetString(const llvm::json::Object *obj,
llvm::StringRef key);

/// Extract the integer value for the specified key from the specified object
/// and return it as the specified integer type T.
Expand Down
2 changes: 1 addition & 1 deletion lldb/tools/lldb-dap/SourceBreakpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace lldb_dap {

SourceBreakpoint::SourceBreakpoint(DAP &dap, const llvm::json::Object &obj)
: Breakpoint(dap, obj),
logMessage(std::string(GetString(obj, "logMessage"))),
logMessage(GetString(obj, "logMessage").value_or("").str()),
line(
GetInteger<uint64_t>(obj, "line").value_or(LLDB_INVALID_LINE_NUMBER)),
column(GetInteger<uint64_t>(obj, "column")
Expand Down
4 changes: 2 additions & 2 deletions lldb/tools/lldb-dap/Watchpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
namespace lldb_dap {
Watchpoint::Watchpoint(DAP &d, const llvm::json::Object &obj)
: BreakpointBase(d, obj) {
llvm::StringRef dataId = GetString(obj, "dataId");
std::string accessType = GetString(obj, "accessType").str();
llvm::StringRef dataId = GetString(obj, "dataId").value_or("");
std::string accessType = GetString(obj, "accessType").value_or("").str();
auto [addr_str, size_str] = dataId.split('/');
llvm::to_integer(addr_str, addr, 16);
llvm::to_integer(size_str, size);
Expand Down