Skip to content

[lldb-dap] Return a std::optional<bool> from GetBoolean (NFC) #129818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2025

Conversation

JDevlieghere
Copy link
Member

@JDevlieghere JDevlieghere commented Mar 5, 2025

Return a std::optional from GetBoolean so you can distinguish between the value not being present and it being explicitly set to true or false. All existing uses are replaced by calling value_or(fail_value).

Motivated by #129753

Return a std::optional<bool> from GetBoolean so you can distinguish
between the value not being present and it being explicitly set to true
or false. All existing uses are replaced by calling
`value_or(fail_value`).
@llvmbot
Copy link
Member

llvmbot commented Mar 5, 2025

@llvm/pr-subscribers-lldb

Author: Jonas Devlieghere (JDevlieghere)

Changes

Return a std::optional<bool> from GetBoolean so you can distinguish between the value not being present and it being explicitly set to true or false. All existing uses are replaced by calling value_or(fail_value).


Full diff: https://github.com/llvm/llvm-project/pull/129818.diff

11 Files Affected:

  • (modified) lldb/tools/lldb-dap/DAP.cpp (+1-1)
  • (modified) lldb/tools/lldb-dap/Handler/AttachRequestHandler.cpp (+7-6)
  • (modified) lldb/tools/lldb-dap/Handler/DisassembleRequestHandler.cpp (+2-1)
  • (modified) lldb/tools/lldb-dap/Handler/DisconnectRequestHandler.cpp (+2-2)
  • (modified) lldb/tools/lldb-dap/Handler/InitializeRequestHandler.cpp (+3-2)
  • (modified) lldb/tools/lldb-dap/Handler/LaunchRequestHandler.cpp (+4-4)
  • (modified) lldb/tools/lldb-dap/Handler/RequestHandler.cpp (+6-5)
  • (modified) lldb/tools/lldb-dap/Handler/StepInRequestHandler.cpp (+2-1)
  • (modified) lldb/tools/lldb-dap/Handler/VariablesRequestHandler.cpp (+1-1)
  • (modified) lldb/tools/lldb-dap/JSONUtils.cpp (+8-8)
  • (modified) lldb/tools/lldb-dap/JSONUtils.h (+7-5)
diff --git a/lldb/tools/lldb-dap/DAP.cpp b/lldb/tools/lldb-dap/DAP.cpp
index 3dc9d6f5ca0a4..c4790414f64f9 100644
--- a/lldb/tools/lldb-dap/DAP.cpp
+++ b/lldb/tools/lldb-dap/DAP.cpp
@@ -787,7 +787,7 @@ bool DAP::HandleObject(const llvm::json::Object &object) {
       response_handler = std::make_unique<UnknownResponseHandler>("", id);
 
     // Result should be given, use null if not.
-    if (GetBoolean(object, "success", false)) {
+    if (GetBoolean(object, "success").value_or(false)) {
       llvm::json::Value Result = nullptr;
       if (auto *B = object.get("body")) {
         Result = std::move(*B);
diff --git a/lldb/tools/lldb-dap/Handler/AttachRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/AttachRequestHandler.cpp
index 8b203e0392066..2733f58b74683 100644
--- a/lldb/tools/lldb-dap/Handler/AttachRequestHandler.cpp
+++ b/lldb/tools/lldb-dap/Handler/AttachRequestHandler.cpp
@@ -61,7 +61,7 @@ void AttachRequestHandler::operator()(const llvm::json::Object &request) const {
       GetString(arguments, "gdb-remote-hostname", "localhost");
   if (pid != LLDB_INVALID_PROCESS_ID)
     attach_info.SetProcessID(pid);
-  const auto wait_for = GetBoolean(arguments, "waitFor", false);
+  const auto wait_for = GetBoolean(arguments, "waitFor").value_or(false);
   attach_info.SetWaitForLaunch(wait_for, false /*async*/);
   dap.init_commands = GetStrings(arguments, "initCommands");
   dap.pre_run_commands = GetStrings(arguments, "preRunCommands");
@@ -71,16 +71,17 @@ void AttachRequestHandler::operator()(const llvm::json::Object &request) const {
   auto attachCommands = GetStrings(arguments, "attachCommands");
   llvm::StringRef core_file = GetString(arguments, "coreFile");
   const uint64_t timeout_seconds = GetUnsigned(arguments, "timeout", 30);
-  dap.stop_at_entry =
-      core_file.empty() ? GetBoolean(arguments, "stopOnEntry", false) : true;
+  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");
   dap.enable_auto_variable_summaries =
-      GetBoolean(arguments, "enableAutoVariableSummaries", false);
+      GetBoolean(arguments, "enableAutoVariableSummaries").value_or(false);
   dap.enable_synthetic_child_debugging =
-      GetBoolean(arguments, "enableSyntheticChildDebugging", false);
+      GetBoolean(arguments, "enableSyntheticChildDebugging").value_or(false);
   dap.display_extended_backtrace =
-      GetBoolean(arguments, "displayExtendedBacktrace", false);
+      GetBoolean(arguments, "displayExtendedBacktrace").value_or(false);
   dap.command_escape_prefix = GetString(arguments, "commandEscapePrefix", "`");
   dap.SetFrameFormat(GetString(arguments, "customFrameFormat"));
   dap.SetThreadFormat(GetString(arguments, "customThreadFormat"));
diff --git a/lldb/tools/lldb-dap/Handler/DisassembleRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/DisassembleRequestHandler.cpp
index 4c2690d32d3b2..d6ff2a7fd8880 100644
--- a/lldb/tools/lldb-dap/Handler/DisassembleRequestHandler.cpp
+++ b/lldb/tools/lldb-dap/Handler/DisassembleRequestHandler.cpp
@@ -123,7 +123,8 @@ void DisassembleRequestHandler::operator()(
     return;
   }
 
-  const bool resolveSymbols = GetBoolean(arguments, "resolveSymbols", false);
+  const bool resolveSymbols =
+      GetBoolean(arguments, "resolveSymbols").value_or(false);
   llvm::json::Array instructions;
   const auto num_insts = insts.GetSize();
   for (size_t i = 0; i < num_insts; ++i) {
diff --git a/lldb/tools/lldb-dap/Handler/DisconnectRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/DisconnectRequestHandler.cpp
index 1d6ff0d039405..b8f3404874e91 100644
--- a/lldb/tools/lldb-dap/Handler/DisconnectRequestHandler.cpp
+++ b/lldb/tools/lldb-dap/Handler/DisconnectRequestHandler.cpp
@@ -64,8 +64,8 @@ void DisconnectRequestHandler::operator()(
   const auto *arguments = request.getObject("arguments");
 
   bool defaultTerminateDebuggee = dap.is_attach ? false : true;
-  bool terminateDebuggee =
-      GetBoolean(arguments, "terminateDebuggee", defaultTerminateDebuggee);
+  bool terminateDebuggee = GetBoolean(arguments, "terminateDebuggee")
+                               .value_or(defaultTerminateDebuggee);
 
   lldb::SBError error = dap.Disconnect(terminateDebuggee);
   if (error.Fail())
diff --git a/lldb/tools/lldb-dap/Handler/InitializeRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/InitializeRequestHandler.cpp
index d0196088a59b7..3ae66ea779e93 100644
--- a/lldb/tools/lldb-dap/Handler/InitializeRequestHandler.cpp
+++ b/lldb/tools/lldb-dap/Handler/InitializeRequestHandler.cpp
@@ -258,7 +258,8 @@ void InitializeRequestHandler::operator()(
   // sourceInitFile option is not from formal DAP specification. It is only
   // used by unit tests to prevent sourcing .lldbinit files from environment
   // which may affect the outcome of tests.
-  bool source_init_file = GetBoolean(arguments, "sourceInitFile", true);
+  bool source_init_file =
+      GetBoolean(arguments, "sourceInitFile").value_or(true);
 
   // Do not source init files until in/out/err are configured.
   dap.debugger = lldb::SBDebugger::Create(false);
@@ -300,7 +301,7 @@ void InitializeRequestHandler::operator()(
   dap.PopulateExceptionBreakpoints();
   auto cmd = dap.debugger.GetCommandInterpreter().AddMultiwordCommand(
       "lldb-dap", "Commands for managing lldb-dap.");
-  if (GetBoolean(arguments, "supportsStartDebuggingRequest", false)) {
+  if (GetBoolean(arguments, "supportsStartDebuggingRequest").value_or(false)) {
     cmd.AddCommand(
         "start-debugging", new StartDebuggingRequestHandler(dap),
         "Sends a startDebugging request from the debug adapter to the client "
diff --git a/lldb/tools/lldb-dap/Handler/LaunchRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/LaunchRequestHandler.cpp
index a41bdd36e5fef..9fa1ddd4e848c 100644
--- a/lldb/tools/lldb-dap/Handler/LaunchRequestHandler.cpp
+++ b/lldb/tools/lldb-dap/Handler/LaunchRequestHandler.cpp
@@ -60,14 +60,14 @@ void LaunchRequestHandler::operator()(const llvm::json::Object &request) const {
   dap.exit_commands = GetStrings(arguments, "exitCommands");
   dap.terminate_commands = GetStrings(arguments, "terminateCommands");
   dap.post_run_commands = GetStrings(arguments, "postRunCommands");
-  dap.stop_at_entry = GetBoolean(arguments, "stopOnEntry", false);
+  dap.stop_at_entry = GetBoolean(arguments, "stopOnEntry").value_or(false);
   const llvm::StringRef debuggerRoot = GetString(arguments, "debuggerRoot");
   dap.enable_auto_variable_summaries =
-      GetBoolean(arguments, "enableAutoVariableSummaries", false);
+      GetBoolean(arguments, "enableAutoVariableSummaries").value_or(false);
   dap.enable_synthetic_child_debugging =
-      GetBoolean(arguments, "enableSyntheticChildDebugging", false);
+      GetBoolean(arguments, "enableSyntheticChildDebugging").value_or(false);
   dap.display_extended_backtrace =
-      GetBoolean(arguments, "displayExtendedBacktrace", false);
+      GetBoolean(arguments, "displayExtendedBacktrace").value_or(false);
   dap.command_escape_prefix = GetString(arguments, "commandEscapePrefix", "`");
   dap.SetFrameFormat(GetString(arguments, "customFrameFormat"));
   dap.SetThreadFormat(GetString(arguments, "customThreadFormat"));
diff --git a/lldb/tools/lldb-dap/Handler/RequestHandler.cpp b/lldb/tools/lldb-dap/Handler/RequestHandler.cpp
index 606ada90ce2e5..d4030965869a1 100644
--- a/lldb/tools/lldb-dap/Handler/RequestHandler.cpp
+++ b/lldb/tools/lldb-dap/Handler/RequestHandler.cpp
@@ -173,19 +173,20 @@ RequestHandler::LaunchProcess(const llvm::json::Object &request) const {
 
   auto flags = launch_info.GetLaunchFlags();
 
-  if (GetBoolean(arguments, "disableASLR", true))
+  if (GetBoolean(arguments, "disableASLR").value_or(true))
     flags |= lldb::eLaunchFlagDisableASLR;
-  if (GetBoolean(arguments, "disableSTDIO", false))
+  if (GetBoolean(arguments, "disableSTDIO").value_or(false))
     flags |= lldb::eLaunchFlagDisableSTDIO;
-  if (GetBoolean(arguments, "shellExpandArguments", false))
+  if (GetBoolean(arguments, "shellExpandArguments").value_or(false))
     flags |= lldb::eLaunchFlagShellExpandArguments;
-  const bool detachOnError = GetBoolean(arguments, "detachOnError", false);
+  const bool detachOnError =
+      GetBoolean(arguments, "detachOnError").value_or(false);
   launch_info.SetDetachOnError(detachOnError);
   launch_info.SetLaunchFlags(flags | lldb::eLaunchFlagDebug |
                              lldb::eLaunchFlagStopAtEntry);
   const uint64_t timeout_seconds = GetUnsigned(arguments, "timeout", 30);
 
-  if (GetBoolean(arguments, "runInTerminal", false)) {
+  if (GetBoolean(arguments, "runInTerminal").value_or(false)) {
     if (llvm::Error err = RunInTerminal(dap, request, timeout_seconds))
       error.SetErrorString(llvm::toString(std::move(err)).c_str());
   } else if (launchCommands.empty()) {
diff --git a/lldb/tools/lldb-dap/Handler/StepInRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/StepInRequestHandler.cpp
index 98fa74a3044d9..c2653322b8898 100644
--- a/lldb/tools/lldb-dap/Handler/StepInRequestHandler.cpp
+++ b/lldb/tools/lldb-dap/Handler/StepInRequestHandler.cpp
@@ -74,7 +74,8 @@ void StepInRequestHandler::operator()(const llvm::json::Object &request) const {
   if (it != dap.step_in_targets.end())
     step_in_target = it->second;
 
-  const bool single_thread = GetBoolean(arguments, "singleThread", false);
+  const bool single_thread =
+      GetBoolean(arguments, "singleThread").value_or(false);
   lldb::RunMode run_mode =
       single_thread ? lldb::eOnlyThisThread : lldb::eOnlyDuringStepping;
   lldb::SBThread thread = dap.GetLLDBThread(*arguments);
diff --git a/lldb/tools/lldb-dap/Handler/VariablesRequestHandler.cpp b/lldb/tools/lldb-dap/Handler/VariablesRequestHandler.cpp
index d096c4220914a..05131c0dae276 100644
--- a/lldb/tools/lldb-dap/Handler/VariablesRequestHandler.cpp
+++ b/lldb/tools/lldb-dap/Handler/VariablesRequestHandler.cpp
@@ -99,7 +99,7 @@ void VariablesRequestHandler::operator()(
   bool hex = false;
   const auto *format = arguments->getObject("format");
   if (format)
-    hex = GetBoolean(format, "hex", false);
+    hex = GetBoolean(format, "hex").value_or(false);
 
   if (lldb::SBValueList *top_scope =
           dap.variables.GetTopLevelScope(variablesReference)) {
diff --git a/lldb/tools/lldb-dap/JSONUtils.cpp b/lldb/tools/lldb-dap/JSONUtils.cpp
index 9dec4ca1df49a..dac2346020454 100644
--- a/lldb/tools/lldb-dap/JSONUtils.cpp
+++ b/lldb/tools/lldb-dap/JSONUtils.cpp
@@ -100,20 +100,20 @@ uint64_t GetUnsigned(const llvm::json::Object *obj, llvm::StringRef key,
   return GetUnsigned(*obj, key, fail_value);
 }
 
-bool GetBoolean(const llvm::json::Object &obj, llvm::StringRef key,
-                bool fail_value) {
+std::optional<bool> GetBoolean(const llvm::json::Object &obj,
+                               llvm::StringRef key) {
   if (auto value = obj.getBoolean(key))
     return *value;
   if (auto value = obj.getInteger(key))
     return *value != 0;
-  return fail_value;
+  return std::nullopt;
 }
 
-bool GetBoolean(const llvm::json::Object *obj, llvm::StringRef key,
-                bool fail_value) {
-  if (obj == nullptr)
-    return fail_value;
-  return GetBoolean(*obj, key, fail_value);
+std::optional<bool> GetBoolean(const llvm::json::Object *obj,
+                               llvm::StringRef key) {
+  if (obj != nullptr)
+    return GetBoolean(*obj, key);
+  return std::nullopt;
 }
 
 int64_t GetSigned(const llvm::json::Object &obj, llvm::StringRef key,
diff --git a/lldb/tools/lldb-dap/JSONUtils.h b/lldb/tools/lldb-dap/JSONUtils.h
index 55d2360e0a224..d212108ce6958 100644
--- a/lldb/tools/lldb-dap/JSONUtils.h
+++ b/lldb/tools/lldb-dap/JSONUtils.h
@@ -102,13 +102,15 @@ uint64_t GetUnsigned(const llvm::json::Object *obj, llvm::StringRef key,
 ///     The key to use when extracting the value
 ///
 /// \return
-///     The boolean value for the specified \a key, or \a fail_value
+///     The boolean value for the specified \a key, or std::nullopt
 ///     if there is no key that matches or if the value is not a
 ///     boolean value of an integer.
-bool GetBoolean(const llvm::json::Object &obj, llvm::StringRef key,
-                bool fail_value);
-bool GetBoolean(const llvm::json::Object *obj, llvm::StringRef key,
-                bool fail_value);
+/// @{
+std::optional<bool> GetBoolean(const llvm::json::Object &obj,
+                               llvm::StringRef key);
+std::optional<bool> GetBoolean(const llvm::json::Object *obj,
+                               llvm::StringRef key);
+/// @}
 
 /// Extract the signed integer for the specified key from the
 /// specified object.

@JDevlieghere
Copy link
Member Author

CC @da-viper (I can't add you as a reviewer because you're not part of the LLVM organization)

JDevlieghere added a commit to JDevlieghere/llvm-project that referenced this pull request Mar 5, 2025
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 llvm#129818
Copy link
Contributor

@ashgti ashgti left a comment

Choose a reason for hiding this comment

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

LGTM

@JDevlieghere JDevlieghere merged commit fc7482e into llvm:main Mar 5, 2025
13 checks passed
@JDevlieghere JDevlieghere deleted the GetBoolean branch March 5, 2025 07:17
JDevlieghere added a commit to JDevlieghere/llvm-project that referenced this pull request Mar 5, 2025
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 llvm#129818
JDevlieghere added a commit that referenced this pull request Mar 5, 2025
…9823)

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
JDevlieghere pushed a commit that referenced this pull request Mar 14, 2025
Completion of #129818 

Did not want to put `llvm::StringRef()` as the default value instead it
was `""`
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
…29818)

Return a std::optional<bool> from GetBoolean so you can distinguish
between the value not being present and it being explicitly set to true
or false. All existing uses are replaced by calling
`value_or(fail_value`).

Motivated by llvm#129753
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
…m#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 llvm#129818
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants