Skip to content

Commit fdbe7c7

Browse files
committed
[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.
1 parent 65365cf commit fdbe7c7

File tree

13 files changed

+64
-98
lines changed

13 files changed

+64
-98
lines changed

lldb/include/lldb/Interpreter/OptionValue.h

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,6 @@ class OptionValue {
182182
CreateValueFromCStringForTypeMask(const char *value_cstr, uint32_t type_mask,
183183
Status &error);
184184

185-
// Get this value as a uint64_t value if it is encoded as a boolean, uint64_t
186-
// or int64_t. Other types will cause "fail_value" to be returned
187-
uint64_t GetUInt64Value(uint64_t fail_value, bool *success_ptr);
188-
189185
OptionValueArch *GetAsArch();
190186

191187
const OptionValueArch *GetAsArch() const;
@@ -262,15 +258,15 @@ class OptionValue {
262258

263259
const OptionValueFormatEntity *GetAsFormatEntity() const;
264260

265-
bool GetBooleanValue(bool fail_value = false) const;
261+
std::optional<bool> GetBooleanValue() const;
266262

267263
bool SetBooleanValue(bool new_value);
268264

269-
char GetCharValue(char fail_value) const;
265+
std::optional<char> GetCharValue() const;
270266

271267
char SetCharValue(char new_value);
272268

273-
int64_t GetEnumerationValue(int64_t fail_value = -1) const;
269+
std::optional<int64_t> GetEnumerationValue() const;
274270

275271
bool SetEnumerationValue(int64_t value);
276272

@@ -280,30 +276,27 @@ class OptionValue {
280276

281277
FileSpecList GetFileSpecListValue() const;
282278

283-
lldb::Format
284-
GetFormatValue(lldb::Format fail_value = lldb::eFormatDefault) const;
279+
std::optional<lldb::Format> GetFormatValue() const;
285280

286281
bool SetFormatValue(lldb::Format new_value);
287282

288-
lldb::LanguageType GetLanguageValue(
289-
lldb::LanguageType fail_value = lldb::eLanguageTypeUnknown) const;
283+
std::optional<lldb::LanguageType> GetLanguageValue() const;
290284

291285
bool SetLanguageValue(lldb::LanguageType new_language);
292286

293287
const FormatEntity::Entry *GetFormatEntity() const;
294288

295289
const RegularExpression *GetRegexValue() const;
296290

297-
int64_t GetSInt64Value(int64_t fail_value = 0) const;
291+
std::optional<int64_t> GetSInt64Value() const;
298292

299293
bool SetSInt64Value(int64_t new_value);
300294

301-
llvm::StringRef GetStringValue(llvm::StringRef fail_value) const;
302-
llvm::StringRef GetStringValue() const { return GetStringValue(llvm::StringRef()); }
295+
std::optional<llvm::StringRef> GetStringValue() const;
303296

304297
bool SetStringValue(llvm::StringRef new_value);
305298

306-
uint64_t GetUInt64Value(uint64_t fail_value = 0) const;
299+
std::optional<uint64_t> GetUInt64Value() const;
307300

308301
bool SetUInt64Value(uint64_t new_value);
309302

lldb/source/Commands/CommandObjectBreakpoint.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,7 +1739,8 @@ class CommandObjectBreakpointNameConfigure : public CommandObjectParsed {
17391739
// check the error:
17401740
BreakpointSP bp_sp;
17411741
if (m_bp_id.m_breakpoint.OptionWasSet()) {
1742-
lldb::break_id_t bp_id = m_bp_id.m_breakpoint.GetUInt64Value();
1742+
lldb::break_id_t bp_id =
1743+
m_bp_id.m_breakpoint.GetUInt64Value().value_or(0);
17431744
bp_sp = target.GetBreakpointByID(bp_id);
17441745
if (!bp_sp) {
17451746
result.AppendErrorWithFormatv("Could not find specified breakpoint {0}",
@@ -1755,7 +1756,8 @@ class CommandObjectBreakpointNameConfigure : public CommandObjectParsed {
17551756
if (!bp_name)
17561757
continue;
17571758
if (m_bp_id.m_help_string.OptionWasSet())
1758-
bp_name->SetHelp(m_bp_id.m_help_string.GetStringValue().str().c_str());
1759+
bp_name->SetHelp(
1760+
m_bp_id.m_help_string.GetStringValue().value_or("").str().c_str());
17591761

17601762
if (bp_sp)
17611763
target.ConfigureBreakpointName(*bp_name, bp_sp->GetOptions(),

lldb/source/Commands/CommandObjectMemory.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,18 +1047,20 @@ class CommandObjectMemoryFind : public CommandObjectParsed {
10471047
DataBufferHeap buffer;
10481048

10491049
if (m_memory_options.m_string.OptionWasSet()) {
1050-
llvm::StringRef str = m_memory_options.m_string.GetStringValue();
1051-
if (str.empty()) {
1050+
std::optional<llvm::StringRef> str =
1051+
m_memory_options.m_string.GetStringValue();
1052+
if (!str) {
10521053
result.AppendError("search string must have non-zero length.");
10531054
return false;
10541055
}
1055-
buffer.CopyData(str);
1056+
buffer.CopyData(*str);
10561057
} else if (m_memory_options.m_expr.OptionWasSet()) {
10571058
StackFrame *frame = m_exe_ctx.GetFramePtr();
10581059
ValueObjectSP result_sp;
10591060
if ((eExpressionCompleted ==
10601061
process->GetTarget().EvaluateExpression(
1061-
m_memory_options.m_expr.GetStringValue(), frame, result_sp)) &&
1062+
m_memory_options.m_expr.GetStringValue().value_or(""), frame,
1063+
result_sp)) &&
10621064
result_sp) {
10631065
uint64_t value = result_sp->GetValueAsUnsigned(0);
10641066
std::optional<uint64_t> size =

lldb/source/Commands/CommandObjectRegister.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ class CommandObjectRegisterRead : public CommandObjectParsed {
171171
const size_t set_array_size = m_command_options.set_indexes.GetSize();
172172
if (set_array_size > 0) {
173173
for (size_t i = 0; i < set_array_size; ++i) {
174-
set_idx = m_command_options.set_indexes[i]->GetUInt64Value(UINT32_MAX,
175-
nullptr);
174+
set_idx = m_command_options.set_indexes[i]->GetUInt64Value().value_or(
175+
UINT32_MAX);
176176
if (set_idx < reg_ctx->GetRegisterSetCount()) {
177177
if (!DumpRegisterSet(m_exe_ctx, strm, reg_ctx, set_idx)) {
178178
if (errno)

lldb/source/Core/Disassembler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ bool Instruction::TestEmulation(Stream *out_stream, const char *file_name) {
908908
return false;
909909
}
910910

911-
SetDescription(value_sp->GetStringValue());
911+
SetDescription(value_sp->GetStringValue().value_or(""));
912912

913913
value_sp = data_dictionary->GetValueForKey(triple_key);
914914
if (!value_sp) {
@@ -918,7 +918,7 @@ bool Instruction::TestEmulation(Stream *out_stream, const char *file_name) {
918918
}
919919

920920
ArchSpec arch;
921-
arch.SetTriple(llvm::Triple(value_sp->GetStringValue()));
921+
arch.SetTriple(llvm::Triple(value_sp->GetStringValue().value_or("")));
922922

923923
bool success = false;
924924
std::unique_ptr<EmulateInstruction> insn_emulator_up(

lldb/source/Interpreter/OptionValue.cpp

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,6 @@
1515
using namespace lldb;
1616
using namespace lldb_private;
1717

18-
// Get this value as a uint64_t value if it is encoded as a boolean, uint64_t
19-
// or int64_t. Other types will cause "fail_value" to be returned
20-
uint64_t OptionValue::GetUInt64Value(uint64_t fail_value, bool *success_ptr) {
21-
if (success_ptr)
22-
*success_ptr = true;
23-
switch (GetType()) {
24-
case OptionValue::eTypeBoolean:
25-
return static_cast<OptionValueBoolean *>(this)->GetCurrentValue();
26-
case OptionValue::eTypeSInt64:
27-
return static_cast<OptionValueSInt64 *>(this)->GetCurrentValue();
28-
case OptionValue::eTypeUInt64:
29-
return static_cast<OptionValueUInt64 *>(this)->GetCurrentValue();
30-
default:
31-
break;
32-
}
33-
if (success_ptr)
34-
*success_ptr = false;
35-
return fail_value;
36-
}
37-
3818
Status OptionValue::SetSubValue(const ExecutionContext *exe_ctx,
3919
VarSetOperationType op, llvm::StringRef name,
4020
llvm::StringRef value) {
@@ -271,11 +251,10 @@ const OptionValueUUID *OptionValue::GetAsUUID() const {
271251
return nullptr;
272252
}
273253

274-
bool OptionValue::GetBooleanValue(bool fail_value) const {
275-
const OptionValueBoolean *option_value = GetAsBoolean();
276-
if (option_value)
254+
std::optional<bool> OptionValue::GetBooleanValue() const {
255+
if (const OptionValueBoolean *option_value = GetAsBoolean())
277256
return option_value->GetCurrentValue();
278-
return fail_value;
257+
return {};
279258
}
280259

281260
bool OptionValue::SetBooleanValue(bool new_value) {
@@ -287,11 +266,10 @@ bool OptionValue::SetBooleanValue(bool new_value) {
287266
return false;
288267
}
289268

290-
char OptionValue::GetCharValue(char fail_value) const {
291-
const OptionValueChar *option_value = GetAsChar();
292-
if (option_value)
269+
std::optional<char> OptionValue::GetCharValue() const {
270+
if (const OptionValueChar *option_value = GetAsChar())
293271
return option_value->GetCurrentValue();
294-
return fail_value;
272+
return {};
295273
}
296274

297275
char OptionValue::SetCharValue(char new_value) {
@@ -303,11 +281,10 @@ char OptionValue::SetCharValue(char new_value) {
303281
return false;
304282
}
305283

306-
int64_t OptionValue::GetEnumerationValue(int64_t fail_value) const {
307-
const OptionValueEnumeration *option_value = GetAsEnumeration();
308-
if (option_value)
284+
std::optional<int64_t> OptionValue::GetEnumerationValue() const {
285+
if (const OptionValueEnumeration *option_value = GetAsEnumeration())
309286
return option_value->GetCurrentValue();
310-
return fail_value;
287+
return {};
311288
}
312289

313290
bool OptionValue::SetEnumerationValue(int64_t value) {
@@ -342,11 +319,10 @@ FileSpecList OptionValue::GetFileSpecListValue() const {
342319
return FileSpecList();
343320
}
344321

345-
lldb::Format OptionValue::GetFormatValue(lldb::Format fail_value) const {
346-
const OptionValueFormat *option_value = GetAsFormat();
347-
if (option_value)
322+
std::optional<lldb::Format> OptionValue::GetFormatValue() const {
323+
if (const OptionValueFormat *option_value = GetAsFormat())
348324
return option_value->GetCurrentValue();
349-
return fail_value;
325+
return {};
350326
}
351327

352328
bool OptionValue::SetFormatValue(lldb::Format new_value) {
@@ -358,12 +334,10 @@ bool OptionValue::SetFormatValue(lldb::Format new_value) {
358334
return false;
359335
}
360336

361-
lldb::LanguageType
362-
OptionValue::GetLanguageValue(lldb::LanguageType fail_value) const {
363-
const OptionValueLanguage *option_value = GetAsLanguage();
364-
if (option_value)
337+
std::optional<lldb::LanguageType> OptionValue::GetLanguageValue() const {
338+
if (const OptionValueLanguage *option_value = GetAsLanguage())
365339
return option_value->GetCurrentValue();
366-
return fail_value;
340+
return {};
367341
}
368342

369343
bool OptionValue::SetLanguageValue(lldb::LanguageType new_language) {
@@ -389,11 +363,10 @@ const RegularExpression *OptionValue::GetRegexValue() const {
389363
return nullptr;
390364
}
391365

392-
int64_t OptionValue::GetSInt64Value(int64_t fail_value) const {
393-
const OptionValueSInt64 *option_value = GetAsSInt64();
394-
if (option_value)
366+
std::optional<int64_t> OptionValue::GetSInt64Value() const {
367+
if (const OptionValueSInt64 *option_value = GetAsSInt64())
395368
return option_value->GetCurrentValue();
396-
return fail_value;
369+
return {};
397370
}
398371

399372
bool OptionValue::SetSInt64Value(int64_t new_value) {
@@ -405,11 +378,10 @@ bool OptionValue::SetSInt64Value(int64_t new_value) {
405378
return false;
406379
}
407380

408-
llvm::StringRef OptionValue::GetStringValue(llvm::StringRef fail_value) const {
409-
const OptionValueString *option_value = GetAsString();
410-
if (option_value)
381+
std::optional<llvm::StringRef> OptionValue::GetStringValue() const {
382+
if (const OptionValueString *option_value = GetAsString())
411383
return option_value->GetCurrentValueAsRef();
412-
return fail_value;
384+
return {};
413385
}
414386

415387
bool OptionValue::SetStringValue(llvm::StringRef new_value) {
@@ -421,11 +393,10 @@ bool OptionValue::SetStringValue(llvm::StringRef new_value) {
421393
return false;
422394
}
423395

424-
uint64_t OptionValue::GetUInt64Value(uint64_t fail_value) const {
425-
const OptionValueUInt64 *option_value = GetAsUInt64();
426-
if (option_value)
396+
std::optional<uint64_t> OptionValue::GetUInt64Value() const {
397+
if (const OptionValueUInt64 *option_value = GetAsUInt64())
427398
return option_value->GetCurrentValue();
428-
return fail_value;
399+
return {};
429400
}
430401

431402
bool OptionValue::SetUInt64Value(uint64_t new_value) {

lldb/source/Interpreter/OptionValueArgs.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ using namespace lldb_private;
1515

1616
size_t OptionValueArgs::GetArgs(Args &args) const {
1717
args.Clear();
18-
for (const auto &value : m_values) {
19-
llvm::StringRef string_value = value->GetStringValue();
20-
args.AppendArgument(string_value);
21-
}
22-
18+
for (const auto &value : m_values)
19+
args.AppendArgument(value->GetStringValue().value_or(""));
2320
return args.GetArgumentCount();
2421
}

lldb/source/Interpreter/OptionValueArray.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ size_t OptionValueArray::GetArgs(Args &args) const {
155155
args.Clear();
156156
const uint32_t size = m_values.size();
157157
for (uint32_t i = 0; i < size; ++i) {
158-
llvm::StringRef string_value = m_values[i]->GetStringValue();
159-
if (!string_value.empty())
160-
args.AppendArgument(string_value);
158+
std::optional<llvm::StringRef> string_value = m_values[i]->GetStringValue();
159+
if (string_value)
160+
args.AppendArgument(*string_value);
161161
}
162162

163163
return args.GetArgumentCount();

lldb/source/Interpreter/OptionValueProperties.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ bool OptionValueProperties::GetPropertyAtIndexAsBoolean(
297297
if (property) {
298298
OptionValue *value = property->GetValue().get();
299299
if (value)
300-
return value->GetBooleanValue(fail_value);
300+
return value->GetBooleanValue().value_or(fail_value);
301301
}
302302
return fail_value;
303303
}
@@ -330,7 +330,7 @@ int64_t OptionValueProperties::GetPropertyAtIndexAsEnumeration(
330330
if (property) {
331331
OptionValue *value = property->GetValue().get();
332332
if (value)
333-
return value->GetEnumerationValue(fail_value);
333+
return value->GetEnumerationValue().value_or(fail_value);
334334
}
335335
return fail_value;
336336
}
@@ -433,7 +433,7 @@ int64_t OptionValueProperties::GetPropertyAtIndexAsSInt64(
433433
if (property) {
434434
OptionValue *value = property->GetValue().get();
435435
if (value)
436-
return value->GetSInt64Value(fail_value);
436+
return value->GetSInt64Value().value_or(fail_value);
437437
}
438438
return fail_value;
439439
}
@@ -456,7 +456,7 @@ llvm::StringRef OptionValueProperties::GetPropertyAtIndexAsString(
456456
if (property) {
457457
OptionValue *value = property->GetValue().get();
458458
if (value)
459-
return value->GetStringValue(fail_value);
459+
return value->GetStringValue().value_or(fail_value);
460460
}
461461
return fail_value;
462462
}
@@ -486,7 +486,7 @@ uint64_t OptionValueProperties::GetPropertyAtIndexAsUInt64(
486486
if (property) {
487487
OptionValue *value = property->GetValue().get();
488488
if (value)
489-
return value->GetUInt64Value(fail_value);
489+
return value->GetUInt64Value().value_or(fail_value);
490490
}
491491
return fail_value;
492492
}

lldb/source/Plugins/Instruction/ARM/EmulateInstructionARM.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14364,7 +14364,7 @@ bool EmulateInstructionARM::TestEmulation(Stream *out_stream, ArchSpec &arch,
1436414364
out_stream->Printf("TestEmulation: Error reading opcode from test file.\n");
1436514365
return false;
1436614366
}
14367-
test_opcode = value_sp->GetUInt64Value();
14367+
test_opcode = value_sp->GetUInt64Value().value_or(0);
1436814368

1436914369
if (arch.GetTriple().getArch() == llvm::Triple::thumb ||
1437014370
arch.IsAlwaysThumbInstructions()) {

0 commit comments

Comments
 (0)