diff options
author | Alex Langford <alangford@apple.com> | 2024-02-14 10:20:58 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-14 10:20:58 -0800 |
commit | 307cd883546348cd658d74699915fd48ae01e9a0 (patch) | |
tree | deebed4fa377a200eba0c65e77262b3aa9903ca4 | |
parent | 275eeda32f4f32d2385043f8d1d8af3d4f65bb2c (diff) | |
download | llvm-307cd883546348cd658d74699915fd48ae01e9a0.zip llvm-307cd883546348cd658d74699915fd48ae01e9a0.tar.gz llvm-307cd883546348cd658d74699915fd48ae01e9a0.tar.bz2 |
[lldb][NFCI] Remove CommandObjectProcessHandle::VerifyCommandOptionValue (#79901)
I was refactoring something else but ran into this function. It was
somewhat confusing to read through and understand, but it boils down to
two steps:
- First we try `OptionArgParser::ToBoolean`. If that works, then we're
good to go.
- Second, we try `llvm::to_integer` to see if it's an integer. If it
parses to 0 or 1, we're good.
- Failing either of the steps above means we cannot parse it into a
bool.
Instead of having an integer out param and a bool return value, the
interface is better served with an optional<bool> -- Either it parses
into true or false, or you get back nothing (nullopt).
-rw-r--r-- | lldb/source/Commands/CommandObjectProcess.cpp | 126 |
1 files changed, 59 insertions, 67 deletions
diff --git a/lldb/source/Commands/CommandObjectProcess.cpp b/lldb/source/Commands/CommandObjectProcess.cpp index c7b874d..722b0e0 100644 --- a/lldb/source/Commands/CommandObjectProcess.cpp +++ b/lldb/source/Commands/CommandObjectProcess.cpp @@ -1591,26 +1591,6 @@ public: Options *GetOptions() override { return &m_options; } - bool VerifyCommandOptionValue(const std::string &option, int &real_value) { - bool okay = true; - bool success = false; - bool tmp_value = OptionArgParser::ToBoolean(option, false, &success); - - if (success && tmp_value) - real_value = 1; - else if (success && !tmp_value) - real_value = 0; - else { - // If the value isn't 'true' or 'false', it had better be 0 or 1. - if (!llvm::to_integer(option, real_value)) - real_value = 3; - if (real_value != 0 && real_value != 1) - okay = false; - } - - return okay; - } - void PrintSignalHeader(Stream &str) { str.Printf("NAME PASS STOP NOTIFY\n"); str.Printf("=========== ===== ===== ======\n"); @@ -1666,33 +1646,52 @@ protected: // the user's options. ProcessSP process_sp = target.GetProcessSP(); - int stop_action = -1; // -1 means leave the current setting alone - int pass_action = -1; // -1 means leave the current setting alone - int notify_action = -1; // -1 means leave the current setting alone + std::optional<bool> stop_action = {}; + std::optional<bool> pass_action = {}; + std::optional<bool> notify_action = {}; - if (!m_options.stop.empty() && - !VerifyCommandOptionValue(m_options.stop, stop_action)) { - result.AppendError("Invalid argument for command option --stop; must be " - "true or false.\n"); - return; + if (!m_options.stop.empty()) { + bool success = false; + bool value = OptionArgParser::ToBoolean(m_options.stop, false, &success); + if (!success) { + result.AppendError( + "Invalid argument for command option --stop; must be " + "true or false.\n"); + return; + } + + stop_action = value; } - if (!m_options.notify.empty() && - !VerifyCommandOptionValue(m_options.notify, notify_action)) { - result.AppendError("Invalid argument for command option --notify; must " - "be true or false.\n"); - return; + if (!m_options.pass.empty()) { + bool success = false; + bool value = OptionArgParser::ToBoolean(m_options.pass, false, &success); + if (!success) { + result.AppendError( + "Invalid argument for command option --pass; must be " + "true or false.\n"); + return; + } + pass_action = value; } - if (!m_options.pass.empty() && - !VerifyCommandOptionValue(m_options.pass, pass_action)) { - result.AppendError("Invalid argument for command option --pass; must be " - "true or false.\n"); - return; + if (!m_options.notify.empty()) { + bool success = false; + bool value = + OptionArgParser::ToBoolean(m_options.notify, false, &success); + if (!success) { + result.AppendError("Invalid argument for command option --notify; must " + "be true or false.\n"); + return; + } + notify_action = value; + } + + if (!m_options.notify.empty() && !notify_action.has_value()) { } - bool no_actions = (stop_action == -1 && pass_action == -1 - && notify_action == -1); + bool no_actions = (!stop_action.has_value() && !pass_action.has_value() && + !notify_action.has_value()); if (m_options.only_target_values && !no_actions) { result.AppendError("-t is for reporting, not setting, target values."); return; @@ -1730,16 +1729,14 @@ protected: if (signals_sp) { int32_t signo = signals_sp->GetSignalNumberFromName(arg.c_str()); if (signo != LLDB_INVALID_SIGNAL_NUMBER) { - // Casting the actions as bools here should be okay, because - // VerifyCommandOptionValue guarantees the value is either 0 or 1. - if (stop_action != -1) - signals_sp->SetShouldStop(signo, stop_action); - if (pass_action != -1) { - bool suppress = !pass_action; + if (stop_action.has_value()) + signals_sp->SetShouldStop(signo, *stop_action); + if (pass_action.has_value()) { + bool suppress = !*pass_action; signals_sp->SetShouldSuppress(signo, suppress); } - if (notify_action != -1) - signals_sp->SetShouldNotify(signo, notify_action); + if (notify_action.has_value()) + signals_sp->SetShouldNotify(signo, *notify_action); ++num_signals_set; } else { result.AppendErrorWithFormat("Invalid signal name '%s'\n", @@ -1759,21 +1756,15 @@ protected: } num_signals_set = num_args; } - auto set_lazy_bool = [] (int action) -> LazyBool { - LazyBool lazy; - if (action == -1) - lazy = eLazyBoolCalculate; - else if (action) - lazy = eLazyBoolYes; - else - lazy = eLazyBoolNo; - return lazy; + auto set_lazy_bool = [](std::optional<bool> action) -> LazyBool { + if (!action.has_value()) + return eLazyBoolCalculate; + return (*action) ? eLazyBoolYes : eLazyBoolNo; }; // If there were no actions, we're just listing, don't add the dummy: if (!no_actions) - target.AddDummySignal(arg.ref(), - set_lazy_bool(pass_action), + target.AddDummySignal(arg.ref(), set_lazy_bool(pass_action), set_lazy_bool(notify_action), set_lazy_bool(stop_action)); } @@ -1781,18 +1772,19 @@ protected: // No signal specified, if any command options were specified, update ALL // signals. But we can't do this without a process since we don't know // all the possible signals that might be valid for this target. - if (((notify_action != -1) || (stop_action != -1) || (pass_action != -1)) - && process_sp) { + if ((notify_action.has_value() || stop_action.has_value() || + pass_action.has_value()) && + process_sp) { if (m_interpreter.Confirm( "Do you really want to update all the signals?", false)) { int32_t signo = signals_sp->GetFirstSignalNumber(); while (signo != LLDB_INVALID_SIGNAL_NUMBER) { - if (notify_action != -1) - signals_sp->SetShouldNotify(signo, notify_action); - if (stop_action != -1) - signals_sp->SetShouldStop(signo, stop_action); - if (pass_action != -1) { - bool suppress = !pass_action; + if (notify_action.has_value()) + signals_sp->SetShouldNotify(signo, *notify_action); + if (stop_action.has_value()) + signals_sp->SetShouldStop(signo, *stop_action); + if (pass_action.has_value()) { + bool suppress = !*pass_action; signals_sp->SetShouldSuppress(signo, suppress); } signo = signals_sp->GetNextSignalNumber(signo); |