[lldb] Refactor OptionValue to return a std::optional (NFC)

Refactor OptionValue to return a std::optional instead of taking a fail
value. This allows the caller to handle situations where there's no
value, instead of being unable to distinguish between the absence of a
value and the value happening the match the fail value. When a fail
value is required, std::optional::value_or() provides the same
functionality.
This commit is contained in:
Jonas Devlieghere
2023-05-01 20:34:51 -07:00
parent 65365cff3b
commit fdbe7c7faa
13 changed files with 64 additions and 98 deletions

View File

@@ -182,10 +182,6 @@ public:
CreateValueFromCStringForTypeMask(const char *value_cstr, uint32_t type_mask,
Status &error);
// Get this value as a uint64_t value if it is encoded as a boolean, uint64_t
// or int64_t. Other types will cause "fail_value" to be returned
uint64_t GetUInt64Value(uint64_t fail_value, bool *success_ptr);
OptionValueArch *GetAsArch();
const OptionValueArch *GetAsArch() const;
@@ -262,15 +258,15 @@ public:
const OptionValueFormatEntity *GetAsFormatEntity() const;
bool GetBooleanValue(bool fail_value = false) const;
std::optional<bool> GetBooleanValue() const;
bool SetBooleanValue(bool new_value);
char GetCharValue(char fail_value) const;
std::optional<char> GetCharValue() const;
char SetCharValue(char new_value);
int64_t GetEnumerationValue(int64_t fail_value = -1) const;
std::optional<int64_t> GetEnumerationValue() const;
bool SetEnumerationValue(int64_t value);
@@ -280,13 +276,11 @@ public:
FileSpecList GetFileSpecListValue() const;
lldb::Format
GetFormatValue(lldb::Format fail_value = lldb::eFormatDefault) const;
std::optional<lldb::Format> GetFormatValue() const;
bool SetFormatValue(lldb::Format new_value);
lldb::LanguageType GetLanguageValue(
lldb::LanguageType fail_value = lldb::eLanguageTypeUnknown) const;
std::optional<lldb::LanguageType> GetLanguageValue() const;
bool SetLanguageValue(lldb::LanguageType new_language);
@@ -294,16 +288,15 @@ public:
const RegularExpression *GetRegexValue() const;
int64_t GetSInt64Value(int64_t fail_value = 0) const;
std::optional<int64_t> GetSInt64Value() const;
bool SetSInt64Value(int64_t new_value);
llvm::StringRef GetStringValue(llvm::StringRef fail_value) const;
llvm::StringRef GetStringValue() const { return GetStringValue(llvm::StringRef()); }
std::optional<llvm::StringRef> GetStringValue() const;
bool SetStringValue(llvm::StringRef new_value);
uint64_t GetUInt64Value(uint64_t fail_value = 0) const;
std::optional<uint64_t> GetUInt64Value() const;
bool SetUInt64Value(uint64_t new_value);

View File

@@ -1739,7 +1739,8 @@ protected:
// check the error:
BreakpointSP bp_sp;
if (m_bp_id.m_breakpoint.OptionWasSet()) {
lldb::break_id_t bp_id = m_bp_id.m_breakpoint.GetUInt64Value();
lldb::break_id_t bp_id =
m_bp_id.m_breakpoint.GetUInt64Value().value_or(0);
bp_sp = target.GetBreakpointByID(bp_id);
if (!bp_sp) {
result.AppendErrorWithFormatv("Could not find specified breakpoint {0}",
@@ -1755,7 +1756,8 @@ protected:
if (!bp_name)
continue;
if (m_bp_id.m_help_string.OptionWasSet())
bp_name->SetHelp(m_bp_id.m_help_string.GetStringValue().str().c_str());
bp_name->SetHelp(
m_bp_id.m_help_string.GetStringValue().value_or("").str().c_str());
if (bp_sp)
target.ConfigureBreakpointName(*bp_name, bp_sp->GetOptions(),

View File

@@ -1047,18 +1047,20 @@ protected:
DataBufferHeap buffer;
if (m_memory_options.m_string.OptionWasSet()) {
llvm::StringRef str = m_memory_options.m_string.GetStringValue();
if (str.empty()) {
std::optional<llvm::StringRef> str =
m_memory_options.m_string.GetStringValue();
if (!str) {
result.AppendError("search string must have non-zero length.");
return false;
}
buffer.CopyData(str);
buffer.CopyData(*str);
} else if (m_memory_options.m_expr.OptionWasSet()) {
StackFrame *frame = m_exe_ctx.GetFramePtr();
ValueObjectSP result_sp;
if ((eExpressionCompleted ==
process->GetTarget().EvaluateExpression(
m_memory_options.m_expr.GetStringValue(), frame, result_sp)) &&
m_memory_options.m_expr.GetStringValue().value_or(""), frame,
result_sp)) &&
result_sp) {
uint64_t value = result_sp->GetValueAsUnsigned(0);
std::optional<uint64_t> size =

View File

@@ -171,8 +171,8 @@ protected:
const size_t set_array_size = m_command_options.set_indexes.GetSize();
if (set_array_size > 0) {
for (size_t i = 0; i < set_array_size; ++i) {
set_idx = m_command_options.set_indexes[i]->GetUInt64Value(UINT32_MAX,
nullptr);
set_idx = m_command_options.set_indexes[i]->GetUInt64Value().value_or(
UINT32_MAX);
if (set_idx < reg_ctx->GetRegisterSetCount()) {
if (!DumpRegisterSet(m_exe_ctx, strm, reg_ctx, set_idx)) {
if (errno)

View File

@@ -908,7 +908,7 @@ bool Instruction::TestEmulation(Stream *out_stream, const char *file_name) {
return false;
}
SetDescription(value_sp->GetStringValue());
SetDescription(value_sp->GetStringValue().value_or(""));
value_sp = data_dictionary->GetValueForKey(triple_key);
if (!value_sp) {
@@ -918,7 +918,7 @@ bool Instruction::TestEmulation(Stream *out_stream, const char *file_name) {
}
ArchSpec arch;
arch.SetTriple(llvm::Triple(value_sp->GetStringValue()));
arch.SetTriple(llvm::Triple(value_sp->GetStringValue().value_or("")));
bool success = false;
std::unique_ptr<EmulateInstruction> insn_emulator_up(

View File

@@ -15,26 +15,6 @@
using namespace lldb;
using namespace lldb_private;
// Get this value as a uint64_t value if it is encoded as a boolean, uint64_t
// or int64_t. Other types will cause "fail_value" to be returned
uint64_t OptionValue::GetUInt64Value(uint64_t fail_value, bool *success_ptr) {
if (success_ptr)
*success_ptr = true;
switch (GetType()) {
case OptionValue::eTypeBoolean:
return static_cast<OptionValueBoolean *>(this)->GetCurrentValue();
case OptionValue::eTypeSInt64:
return static_cast<OptionValueSInt64 *>(this)->GetCurrentValue();
case OptionValue::eTypeUInt64:
return static_cast<OptionValueUInt64 *>(this)->GetCurrentValue();
default:
break;
}
if (success_ptr)
*success_ptr = false;
return fail_value;
}
Status OptionValue::SetSubValue(const ExecutionContext *exe_ctx,
VarSetOperationType op, llvm::StringRef name,
llvm::StringRef value) {
@@ -271,11 +251,10 @@ const OptionValueUUID *OptionValue::GetAsUUID() const {
return nullptr;
}
bool OptionValue::GetBooleanValue(bool fail_value) const {
const OptionValueBoolean *option_value = GetAsBoolean();
if (option_value)
std::optional<bool> OptionValue::GetBooleanValue() const {
if (const OptionValueBoolean *option_value = GetAsBoolean())
return option_value->GetCurrentValue();
return fail_value;
return {};
}
bool OptionValue::SetBooleanValue(bool new_value) {
@@ -287,11 +266,10 @@ bool OptionValue::SetBooleanValue(bool new_value) {
return false;
}
char OptionValue::GetCharValue(char fail_value) const {
const OptionValueChar *option_value = GetAsChar();
if (option_value)
std::optional<char> OptionValue::GetCharValue() const {
if (const OptionValueChar *option_value = GetAsChar())
return option_value->GetCurrentValue();
return fail_value;
return {};
}
char OptionValue::SetCharValue(char new_value) {
@@ -303,11 +281,10 @@ char OptionValue::SetCharValue(char new_value) {
return false;
}
int64_t OptionValue::GetEnumerationValue(int64_t fail_value) const {
const OptionValueEnumeration *option_value = GetAsEnumeration();
if (option_value)
std::optional<int64_t> OptionValue::GetEnumerationValue() const {
if (const OptionValueEnumeration *option_value = GetAsEnumeration())
return option_value->GetCurrentValue();
return fail_value;
return {};
}
bool OptionValue::SetEnumerationValue(int64_t value) {
@@ -342,11 +319,10 @@ FileSpecList OptionValue::GetFileSpecListValue() const {
return FileSpecList();
}
lldb::Format OptionValue::GetFormatValue(lldb::Format fail_value) const {
const OptionValueFormat *option_value = GetAsFormat();
if (option_value)
std::optional<lldb::Format> OptionValue::GetFormatValue() const {
if (const OptionValueFormat *option_value = GetAsFormat())
return option_value->GetCurrentValue();
return fail_value;
return {};
}
bool OptionValue::SetFormatValue(lldb::Format new_value) {
@@ -358,12 +334,10 @@ bool OptionValue::SetFormatValue(lldb::Format new_value) {
return false;
}
lldb::LanguageType
OptionValue::GetLanguageValue(lldb::LanguageType fail_value) const {
const OptionValueLanguage *option_value = GetAsLanguage();
if (option_value)
std::optional<lldb::LanguageType> OptionValue::GetLanguageValue() const {
if (const OptionValueLanguage *option_value = GetAsLanguage())
return option_value->GetCurrentValue();
return fail_value;
return {};
}
bool OptionValue::SetLanguageValue(lldb::LanguageType new_language) {
@@ -389,11 +363,10 @@ const RegularExpression *OptionValue::GetRegexValue() const {
return nullptr;
}
int64_t OptionValue::GetSInt64Value(int64_t fail_value) const {
const OptionValueSInt64 *option_value = GetAsSInt64();
if (option_value)
std::optional<int64_t> OptionValue::GetSInt64Value() const {
if (const OptionValueSInt64 *option_value = GetAsSInt64())
return option_value->GetCurrentValue();
return fail_value;
return {};
}
bool OptionValue::SetSInt64Value(int64_t new_value) {
@@ -405,11 +378,10 @@ bool OptionValue::SetSInt64Value(int64_t new_value) {
return false;
}
llvm::StringRef OptionValue::GetStringValue(llvm::StringRef fail_value) const {
const OptionValueString *option_value = GetAsString();
if (option_value)
std::optional<llvm::StringRef> OptionValue::GetStringValue() const {
if (const OptionValueString *option_value = GetAsString())
return option_value->GetCurrentValueAsRef();
return fail_value;
return {};
}
bool OptionValue::SetStringValue(llvm::StringRef new_value) {
@@ -421,11 +393,10 @@ bool OptionValue::SetStringValue(llvm::StringRef new_value) {
return false;
}
uint64_t OptionValue::GetUInt64Value(uint64_t fail_value) const {
const OptionValueUInt64 *option_value = GetAsUInt64();
if (option_value)
std::optional<uint64_t> OptionValue::GetUInt64Value() const {
if (const OptionValueUInt64 *option_value = GetAsUInt64())
return option_value->GetCurrentValue();
return fail_value;
return {};
}
bool OptionValue::SetUInt64Value(uint64_t new_value) {

View File

@@ -15,10 +15,7 @@ using namespace lldb_private;
size_t OptionValueArgs::GetArgs(Args &args) const {
args.Clear();
for (const auto &value : m_values) {
llvm::StringRef string_value = value->GetStringValue();
args.AppendArgument(string_value);
}
for (const auto &value : m_values)
args.AppendArgument(value->GetStringValue().value_or(""));
return args.GetArgumentCount();
}

View File

@@ -155,9 +155,9 @@ size_t OptionValueArray::GetArgs(Args &args) const {
args.Clear();
const uint32_t size = m_values.size();
for (uint32_t i = 0; i < size; ++i) {
llvm::StringRef string_value = m_values[i]->GetStringValue();
if (!string_value.empty())
args.AppendArgument(string_value);
std::optional<llvm::StringRef> string_value = m_values[i]->GetStringValue();
if (string_value)
args.AppendArgument(*string_value);
}
return args.GetArgumentCount();

View File

@@ -297,7 +297,7 @@ bool OptionValueProperties::GetPropertyAtIndexAsBoolean(
if (property) {
OptionValue *value = property->GetValue().get();
if (value)
return value->GetBooleanValue(fail_value);
return value->GetBooleanValue().value_or(fail_value);
}
return fail_value;
}
@@ -330,7 +330,7 @@ int64_t OptionValueProperties::GetPropertyAtIndexAsEnumeration(
if (property) {
OptionValue *value = property->GetValue().get();
if (value)
return value->GetEnumerationValue(fail_value);
return value->GetEnumerationValue().value_or(fail_value);
}
return fail_value;
}
@@ -433,7 +433,7 @@ int64_t OptionValueProperties::GetPropertyAtIndexAsSInt64(
if (property) {
OptionValue *value = property->GetValue().get();
if (value)
return value->GetSInt64Value(fail_value);
return value->GetSInt64Value().value_or(fail_value);
}
return fail_value;
}
@@ -456,7 +456,7 @@ llvm::StringRef OptionValueProperties::GetPropertyAtIndexAsString(
if (property) {
OptionValue *value = property->GetValue().get();
if (value)
return value->GetStringValue(fail_value);
return value->GetStringValue().value_or(fail_value);
}
return fail_value;
}
@@ -486,7 +486,7 @@ uint64_t OptionValueProperties::GetPropertyAtIndexAsUInt64(
if (property) {
OptionValue *value = property->GetValue().get();
if (value)
return value->GetUInt64Value(fail_value);
return value->GetUInt64Value().value_or(fail_value);
}
return fail_value;
}

View File

@@ -14364,7 +14364,7 @@ bool EmulateInstructionARM::TestEmulation(Stream *out_stream, ArchSpec &arch,
out_stream->Printf("TestEmulation: Error reading opcode from test file.\n");
return false;
}
test_opcode = value_sp->GetUInt64Value();
test_opcode = value_sp->GetUInt64Value().value_or(0);
if (arch.GetTriple().getArch() == llvm::Triple::thumb ||
arch.IsAlwaysThumbInstructions()) {

View File

@@ -267,7 +267,7 @@ bool EmulationStateARM::LoadRegistersStateFromDictionary(
OptionValueSP value_sp = reg_dict->GetValueForKey(sstr.GetString());
if (value_sp.get() == nullptr)
return false;
uint64_t reg_value = value_sp->GetUInt64Value();
uint64_t reg_value = value_sp->GetUInt64Value().value_or(0);
StorePseudoRegisterValue(first_reg + i, reg_value);
}
@@ -296,7 +296,7 @@ bool EmulationStateARM::LoadStateFromDictionary(
if (value_sp.get() == nullptr)
return false;
else
start_address = value_sp->GetUInt64Value();
start_address = value_sp->GetUInt64Value().value_or(0);
value_sp = mem_dict->GetValueForKey(data_key);
OptionValueArray *mem_array = value_sp->GetAsArray();
@@ -310,7 +310,7 @@ bool EmulationStateARM::LoadStateFromDictionary(
value_sp = mem_array->GetValueAtIndex(i);
if (value_sp.get() == nullptr)
return false;
uint64_t value = value_sp->GetUInt64Value();
uint64_t value = value_sp->GetUInt64Value().value_or(0);
StoreToPseudoAddress(address, value);
address = address + 4;
}
@@ -330,7 +330,7 @@ bool EmulationStateARM::LoadStateFromDictionary(
value_sp = reg_dict->GetValueForKey(cpsr_name);
if (value_sp.get() == nullptr)
return false;
StorePseudoRegisterValue(dwarf_cpsr, value_sp->GetUInt64Value());
StorePseudoRegisterValue(dwarf_cpsr, value_sp->GetUInt64Value().value_or(0));
// Load s/d Registers
// To prevent you giving both types in a state and overwriting

View File

@@ -319,7 +319,8 @@ size_t ObjectFilePECOFF::GetModuleSpecifications(
llvm::Triple::EnvironmentType env;
if (module_env_option)
env =
(llvm::Triple::EnvironmentType)module_env_option->GetEnumerationValue();
(llvm::Triple::EnvironmentType)module_env_option->GetEnumerationValue()
.value_or(0);
else
env = GetGlobalPluginProperties().ABI();

View File

@@ -887,7 +887,7 @@ public:
"the --command option must be set to a valid command byte");
} else {
const uint64_t command_byte =
m_command_byte.GetOptionValue().GetUInt64Value(0);
m_command_byte.GetOptionValue().GetUInt64Value().value_or(0);
if (command_byte > 0 && command_byte <= UINT8_MAX) {
ProcessKDP *process =
(ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr();