Author: Ebuka Ezike Date: 2025-03-14T10:17:18-07:00 New Revision: 217fc6579bd0ddac96c283c4d996e97c9eab181e
URL: https://github.com/llvm/llvm-project/commit/217fc6579bd0ddac96c283c4d996e97c9eab181e DIFF: https://github.com/llvm/llvm-project/commit/217fc6579bd0ddac96c283c4d996e97c9eab181e.diff LOG: [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 `""` Added: Modified: lldb/tools/lldb-dap/BreakpointBase.cpp lldb/tools/lldb-dap/DAP.cpp lldb/tools/lldb-dap/FunctionBreakpoint.cpp lldb/tools/lldb-dap/Handler/AttachRequestHandler.cpp lldb/tools/lldb-dap/Handler/BreakpointLocationsHandler.cpp lldb/tools/lldb-dap/Handler/CompileUnitsRequestHandler.cpp lldb/tools/lldb-dap/Handler/CompletionsHandler.cpp lldb/tools/lldb-dap/Handler/DataBreakpointInfoRequestHandler.cpp lldb/tools/lldb-dap/Handler/DisassembleRequestHandler.cpp lldb/tools/lldb-dap/Handler/EvaluateRequestHandler.cpp lldb/tools/lldb-dap/Handler/LaunchRequestHandler.cpp lldb/tools/lldb-dap/Handler/ReadMemoryRequestHandler.cpp lldb/tools/lldb-dap/Handler/RequestHandler.cpp lldb/tools/lldb-dap/Handler/SetBreakpointsRequestHandler.cpp lldb/tools/lldb-dap/Handler/SetVariableRequestHandler.cpp lldb/tools/lldb-dap/InstructionBreakpoint.cpp lldb/tools/lldb-dap/JSONUtils.cpp lldb/tools/lldb-dap/JSONUtils.h lldb/tools/lldb-dap/SourceBreakpoint.cpp lldb/tools/lldb-dap/Watchpoint.cpp Removed: ################################################################################ diff --git a/lldb/tools/lldb-dap/BreakpointBase.cpp b/lldb/tools/lldb-dap/BreakpointBase.cpp index 1e28c29082a9f..7979bac098766 100644 --- a/lldb/tools/lldb-dap/BreakpointBase.cpp +++ b/lldb/tools/lldb-dap/BreakpointBase.cpp @@ -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(""))), + hitCondition(std::string(GetString(obj, "hitCondition").value_or(""))) {} void BreakpointBase::UpdateBreakpoint(const BreakpointBase &request_bp) { if (condition != request_bp.condition) { diff --git a/lldb/tools/lldb-dap/DAP.cpp b/lldb/tools/lldb-dap/DAP.cpp index 4080e2c211035..2a8fe8e24c242 100644 --- a/lldb/tools/lldb-dap/DAP.cpp +++ b/lldb/tools/lldb-dap/DAP.cpp @@ -630,9 +630,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. @@ -668,7 +670,7 @@ bool DAP::HandleObject(const protocol::Message &M) { llvm::json::Object object = *v.getAsObject(); const auto packet_type = GetString(object, "type"); 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()) { @@ -704,7 +706,7 @@ bool DAP::HandleObject(const protocol::Message &M) { Result = std::move(*B); (*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"; } diff --git a/lldb/tools/lldb-dap/FunctionBreakpoint.cpp b/lldb/tools/lldb-dap/FunctionBreakpoint.cpp index f266d751833c7..cafae32b662f2 100644 --- a/lldb/tools/lldb-dap/FunctionBreakpoint.cpp +++ b/lldb/tools/lldb-dap/FunctionBreakpoint.cpp @@ -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()) diff --git a/lldb/tools/lldb-dap/Handler/AttachRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/AttachRequestHandler.cpp index 8f42f5b96b937..20f7c80a1ed90 100644 --- a/lldb/tools/lldb-dap/Handler/AttachRequestHandler.cpp +++ b/lldb/tools/lldb-dap/Handler/AttachRequestHandler.cpp @@ -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); @@ -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(); diff --git a/lldb/tools/lldb-dap/Handler/BreakpointLocationsHandler.cpp b/lldb/tools/lldb-dap/Handler/BreakpointLocationsHandler.cpp index d6efd659ae8e0..022baf77458d0 100644 --- a/lldb/tools/lldb-dap/Handler/BreakpointLocationsHandler.cpp +++ b/lldb/tools/lldb-dap/Handler/BreakpointLocationsHandler.cpp @@ -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") diff --git a/lldb/tools/lldb-dap/Handler/CompileUnitsRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/CompileUnitsRequestHandler.cpp index 51ae44a0def9d..cd937116f7380 100644 --- a/lldb/tools/lldb-dap/Handler/CompileUnitsRequestHandler.cpp +++ b/lldb/tools/lldb-dap/Handler/CompileUnitsRequestHandler.cpp @@ -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); diff --git a/lldb/tools/lldb-dap/Handler/CompletionsHandler.cpp b/lldb/tools/lldb-dap/Handler/CompletionsHandler.cpp index bfe2e734ecff6..5414aaeb2c317 100644 --- a/lldb/tools/lldb-dap/Handler/CompletionsHandler.cpp +++ b/lldb/tools/lldb-dap/Handler/CompletionsHandler.cpp @@ -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); diff --git a/lldb/tools/lldb-dap/Handler/DataBreakpointInfoRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/DataBreakpointInfoRequestHandler.cpp index 7443db18331c5..4d920f8556254 100644 --- a/lldb/tools/lldb-dap/Handler/DataBreakpointInfoRequestHandler.cpp +++ b/lldb/tools/lldb-dap/Handler/DataBreakpointInfoRequestHandler.cpp @@ -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; diff --git a/lldb/tools/lldb-dap/Handler/DisassembleRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/DisassembleRequestHandler.cpp index 56502baf6d283..f0cb7be70210d 100644 --- a/lldb/tools/lldb-dap/Handler/DisassembleRequestHandler.cpp +++ b/lldb/tools/lldb-dap/Handler/DisassembleRequestHandler.cpp @@ -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; diff --git a/lldb/tools/lldb-dap/Handler/EvaluateRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/EvaluateRequestHandler.cpp index 36a9d9bff5db9..e9f08a1017abc 100644 --- a/lldb/tools/lldb-dap/Handler/EvaluateRequestHandler.cpp +++ b/lldb/tools/lldb-dap/Handler/EvaluateRequestHandler.cpp @@ -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(); diff --git a/lldb/tools/lldb-dap/Handler/LaunchRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/LaunchRequestHandler.cpp index 9fa1ddd4e848c..f64c186376a36 100644 --- a/lldb/tools/lldb-dap/Handler/LaunchRequestHandler.cpp +++ b/lldb/tools/lldb-dap/Handler/LaunchRequestHandler.cpp @@ -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(); diff --git a/lldb/tools/lldb-dap/Handler/ReadMemoryRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/ReadMemoryRequestHandler.cpp index 919aecd9c68dd..891c2af4f2f28 100644 --- a/lldb/tools/lldb-dap/Handler/ReadMemoryRequestHandler.cpp +++ b/lldb/tools/lldb-dap/Handler/ReadMemoryRequestHandler.cpp @@ -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; diff --git a/lldb/tools/lldb-dap/Handler/RequestHandler.cpp b/lldb/tools/lldb-dap/Handler/RequestHandler.cpp index e43fa36d25e3f..2e661329edea1 100644 --- a/lldb/tools/lldb-dap/Handler/RequestHandler.cpp +++ b/lldb/tools/lldb-dap/Handler/RequestHandler.cpp @@ -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"; @@ -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()); diff --git a/lldb/tools/lldb-dap/Handler/SetBreakpointsRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/SetBreakpointsRequestHandler.cpp index 413db38733ffa..5ca2c9c01965e 100644 --- a/lldb/tools/lldb-dap/Handler/SetBreakpointsRequestHandler.cpp +++ b/lldb/tools/lldb-dap/Handler/SetBreakpointsRequestHandler.cpp @@ -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; diff --git a/lldb/tools/lldb-dap/Handler/SetVariableRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/SetVariableRequestHandler.cpp index 7d2f13f0a327e..3ab1bb32fd18e 100644 --- a/lldb/tools/lldb-dap/Handler/SetVariableRequestHandler.cpp +++ b/lldb/tools/lldb-dap/Handler/SetVariableRequestHandler.cpp @@ -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); diff --git a/lldb/tools/lldb-dap/InstructionBreakpoint.cpp b/lldb/tools/lldb-dap/InstructionBreakpoint.cpp index a008b633c165b..710787625ec58 100644 --- a/lldb/tools/lldb-dap/InstructionBreakpoint.cpp +++ b/lldb/tools/lldb-dap/InstructionBreakpoint.cpp @@ -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; } diff --git a/lldb/tools/lldb-dap/JSONUtils.cpp b/lldb/tools/lldb-dap/JSONUtils.cpp index 932145b1799bd..795c73805eef3 100644 --- a/lldb/tools/lldb-dap/JSONUtils.cpp +++ b/lldb/tools/lldb-dap/JSONUtils.cpp @@ -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, @@ -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(); } @@ -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); } @@ -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); diff --git a/lldb/tools/lldb-dap/JSONUtils.h b/lldb/tools/lldb-dap/JSONUtils.h index 17d4b850eb78a..a8858020f7d85 100644 --- a/lldb/tools/lldb-dap/JSONUtils.h +++ b/lldb/tools/lldb-dap/JSONUtils.h @@ -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. diff --git a/lldb/tools/lldb-dap/SourceBreakpoint.cpp b/lldb/tools/lldb-dap/SourceBreakpoint.cpp index 7742dce2928b5..4c6b36119c84f 100644 --- a/lldb/tools/lldb-dap/SourceBreakpoint.cpp +++ b/lldb/tools/lldb-dap/SourceBreakpoint.cpp @@ -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") diff --git a/lldb/tools/lldb-dap/Watchpoint.cpp b/lldb/tools/lldb-dap/Watchpoint.cpp index 0e68a35877c66..8681057c8d3f2 100644 --- a/lldb/tools/lldb-dap/Watchpoint.cpp +++ b/lldb/tools/lldb-dap/Watchpoint.cpp @@ -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); _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits