Skip to content

Commit 9c48aa6

Browse files
committed
[lldb] Refactor OptionValueProperties to return a std::optional (NFC)
Similar to fdbe7c7, refactor OptionValueProperties 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 3b8bc83 commit 9c48aa6

File tree

17 files changed

+318
-293
lines changed

17 files changed

+318
-293
lines changed

lldb/include/lldb/Interpreter/OptionValueProperties.h

+15-12
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ class OptionValueProperties
125125
bool SetPropertyAtIndexFromArgs(const ExecutionContext *exe_ctx, uint32_t idx,
126126
const Args &args);
127127

128-
bool GetPropertyAtIndexAsBoolean(const ExecutionContext *exe_ctx,
129-
uint32_t idx, bool fail_value) const;
128+
std::optional<bool>
129+
GetPropertyAtIndexAsBoolean(const ExecutionContext *exe_ctx,
130+
uint32_t idx) const;
130131

131132
bool SetPropertyAtIndexAsBoolean(const ExecutionContext *exe_ctx,
132133
uint32_t idx, bool new_value);
@@ -135,9 +136,9 @@ class OptionValueProperties
135136
GetPropertyAtIndexAsOptionValueDictionary(const ExecutionContext *exe_ctx,
136137
uint32_t idx) const;
137138

138-
int64_t GetPropertyAtIndexAsEnumeration(const ExecutionContext *exe_ctx,
139-
uint32_t idx,
140-
int64_t fail_value) const;
139+
std::optional<int64_t>
140+
GetPropertyAtIndexAsEnumeration(const ExecutionContext *exe_ctx,
141+
uint32_t idx) const;
141142

142143
bool SetPropertyAtIndexAsEnumeration(const ExecutionContext *exe_ctx,
143144
uint32_t idx, int64_t new_value);
@@ -158,21 +159,23 @@ class OptionValueProperties
158159
GetPropertyAtIndexAsOptionValueUInt64(const ExecutionContext *exe_ctx,
159160
uint32_t idx) const;
160161

161-
int64_t GetPropertyAtIndexAsSInt64(const ExecutionContext *exe_ctx,
162-
uint32_t idx, int64_t fail_value) const;
162+
std::optional<int64_t>
163+
GetPropertyAtIndexAsSInt64(const ExecutionContext *exe_ctx,
164+
uint32_t idx) const;
163165

164166
bool SetPropertyAtIndexAsSInt64(const ExecutionContext *exe_ctx, uint32_t idx,
165167
int64_t new_value);
166168

167-
uint64_t GetPropertyAtIndexAsUInt64(const ExecutionContext *exe_ctx,
168-
uint32_t idx, uint64_t fail_value) const;
169+
std::optional<uint64_t>
170+
GetPropertyAtIndexAsUInt64(const ExecutionContext *exe_ctx,
171+
uint32_t idx) const;
169172

170173
bool SetPropertyAtIndexAsUInt64(const ExecutionContext *exe_ctx, uint32_t idx,
171174
uint64_t new_value);
172175

173-
llvm::StringRef GetPropertyAtIndexAsString(const ExecutionContext *exe_ctx,
174-
uint32_t idx,
175-
llvm::StringRef fail_value) const;
176+
std::optional<llvm::StringRef>
177+
GetPropertyAtIndexAsString(const ExecutionContext *exe_ctx,
178+
uint32_t idx) const;
176179

177180
bool SetPropertyAtIndexAsString(const ExecutionContext *exe_ctx, uint32_t idx,
178181
llvm::StringRef new_value);

lldb/source/Core/Debugger.cpp

+66-51
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ Status Debugger::SetPropertyValue(const ExecutionContext *exe_ctx,
263263

264264
bool Debugger::GetAutoConfirm() const {
265265
const uint32_t idx = ePropertyAutoConfirm;
266-
return m_collection_sp->GetPropertyAtIndexAsBoolean(
267-
nullptr, idx, g_debugger_properties[idx].default_uint_value != 0);
266+
return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx)
267+
.value_or(g_debugger_properties[idx].default_uint_value != 0);
268268
}
269269

270270
const FormatEntity::Entry *Debugger::GetDisassemblyFormat() const {
@@ -284,20 +284,20 @@ const FormatEntity::Entry *Debugger::GetFrameFormatUnique() const {
284284

285285
uint32_t Debugger::GetStopDisassemblyMaxSize() const {
286286
const uint32_t idx = ePropertyStopDisassemblyMaxSize;
287-
return m_collection_sp->GetPropertyAtIndexAsUInt64(
288-
nullptr, idx, g_debugger_properties[idx].default_uint_value);
287+
return m_collection_sp->GetPropertyAtIndexAsUInt64(nullptr, idx)
288+
.value_or(g_debugger_properties[idx].default_uint_value);
289289
}
290290

291291
bool Debugger::GetNotifyVoid() const {
292292
const uint32_t idx = ePropertyNotiftVoid;
293-
return m_collection_sp->GetPropertyAtIndexAsBoolean(
294-
nullptr, idx, g_debugger_properties[idx].default_uint_value != 0);
293+
return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx)
294+
.value_or(g_debugger_properties[idx].default_uint_value != 0);
295295
}
296296

297297
llvm::StringRef Debugger::GetPrompt() const {
298298
const uint32_t idx = ePropertyPrompt;
299-
return m_collection_sp->GetPropertyAtIndexAsString(
300-
nullptr, idx, g_debugger_properties[idx].default_cstr_value);
299+
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx)
300+
.value_or(g_debugger_properties[idx].default_cstr_value);
301301
}
302302

303303
void Debugger::SetPrompt(llvm::StringRef p) {
@@ -323,8 +323,9 @@ const FormatEntity::Entry *Debugger::GetThreadStopFormat() const {
323323

324324
lldb::ScriptLanguage Debugger::GetScriptLanguage() const {
325325
const uint32_t idx = ePropertyScriptLanguage;
326-
return (lldb::ScriptLanguage)m_collection_sp->GetPropertyAtIndexAsEnumeration(
327-
nullptr, idx, g_debugger_properties[idx].default_uint_value);
326+
return (lldb::ScriptLanguage)m_collection_sp
327+
->GetPropertyAtIndexAsEnumeration(nullptr, idx)
328+
.value_or(g_debugger_properties[idx].default_uint_value);
328329
}
329330

330331
bool Debugger::SetScriptLanguage(lldb::ScriptLanguage script_lang) {
@@ -349,8 +350,8 @@ bool Debugger::SetREPLLanguage(lldb::LanguageType repl_lang) {
349350

350351
uint32_t Debugger::GetTerminalWidth() const {
351352
const uint32_t idx = ePropertyTerminalWidth;
352-
return m_collection_sp->GetPropertyAtIndexAsSInt64(
353-
nullptr, idx, g_debugger_properties[idx].default_uint_value);
353+
return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx)
354+
.value_or(g_debugger_properties[idx].default_uint_value);
354355
}
355356

356357
bool Debugger::SetTerminalWidth(uint32_t term_width) {
@@ -363,8 +364,8 @@ bool Debugger::SetTerminalWidth(uint32_t term_width) {
363364

364365
bool Debugger::GetUseExternalEditor() const {
365366
const uint32_t idx = ePropertyUseExternalEditor;
366-
return m_collection_sp->GetPropertyAtIndexAsBoolean(
367-
nullptr, idx, g_debugger_properties[idx].default_uint_value != 0);
367+
return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx)
368+
.value_or(g_debugger_properties[idx].default_uint_value != 0);
368369
}
369370

370371
bool Debugger::SetUseExternalEditor(bool b) {
@@ -374,7 +375,8 @@ bool Debugger::SetUseExternalEditor(bool b) {
374375

375376
llvm::StringRef Debugger::GetExternalEditor() const {
376377
const uint32_t idx = ePropertyExternalEditor;
377-
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, "");
378+
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx)
379+
.value_or(g_debugger_properties[idx].default_cstr_value);
378380
}
379381

380382
bool Debugger::SetExternalEditor(llvm::StringRef editor) {
@@ -384,8 +386,8 @@ bool Debugger::SetExternalEditor(llvm::StringRef editor) {
384386

385387
bool Debugger::GetUseColor() const {
386388
const uint32_t idx = ePropertyUseColor;
387-
return m_collection_sp->GetPropertyAtIndexAsBoolean(
388-
nullptr, idx, g_debugger_properties[idx].default_uint_value != 0);
389+
return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx)
390+
.value_or(g_debugger_properties[idx].default_uint_value != 0);
389391
}
390392

391393
bool Debugger::SetUseColor(bool b) {
@@ -397,8 +399,8 @@ bool Debugger::SetUseColor(bool b) {
397399

398400
bool Debugger::GetShowProgress() const {
399401
const uint32_t idx = ePropertyShowProgress;
400-
return m_collection_sp->GetPropertyAtIndexAsBoolean(
401-
nullptr, idx, g_debugger_properties[idx].default_uint_value != 0);
402+
return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx)
403+
.value_or(g_debugger_properties[idx].default_uint_value != 0);
402404
}
403405

404406
bool Debugger::SetShowProgress(bool show_progress) {
@@ -409,34 +411,38 @@ bool Debugger::SetShowProgress(bool show_progress) {
409411

410412
llvm::StringRef Debugger::GetShowProgressAnsiPrefix() const {
411413
const uint32_t idx = ePropertyShowProgressAnsiPrefix;
412-
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, "");
414+
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx)
415+
.value_or(g_debugger_properties[idx].default_cstr_value);
413416
}
414417

415418
llvm::StringRef Debugger::GetShowProgressAnsiSuffix() const {
416419
const uint32_t idx = ePropertyShowProgressAnsiSuffix;
417-
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, "");
420+
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx)
421+
.value_or(g_debugger_properties[idx].default_cstr_value);
418422
}
419423

420424
bool Debugger::GetUseAutosuggestion() const {
421425
const uint32_t idx = ePropertyShowAutosuggestion;
422-
return m_collection_sp->GetPropertyAtIndexAsBoolean(
423-
nullptr, idx, g_debugger_properties[idx].default_uint_value != 0);
426+
return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx)
427+
.value_or(g_debugger_properties[idx].default_uint_value != 0);
424428
}
425429

426430
llvm::StringRef Debugger::GetAutosuggestionAnsiPrefix() const {
427431
const uint32_t idx = ePropertyShowAutosuggestionAnsiPrefix;
428-
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, "");
432+
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx)
433+
.value_or(g_debugger_properties[idx].default_cstr_value);
429434
}
430435

431436
llvm::StringRef Debugger::GetAutosuggestionAnsiSuffix() const {
432437
const uint32_t idx = ePropertyShowAutosuggestionAnsiSuffix;
433-
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, "");
438+
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx)
439+
.value_or(g_debugger_properties[idx].default_cstr_value);
434440
}
435441

436442
bool Debugger::GetUseSourceCache() const {
437443
const uint32_t idx = ePropertyUseSourceCache;
438-
return m_collection_sp->GetPropertyAtIndexAsBoolean(
439-
nullptr, idx, g_debugger_properties[idx].default_uint_value != 0);
444+
return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx)
445+
.value_or(g_debugger_properties[idx].default_uint_value != 0);
440446
}
441447

442448
bool Debugger::SetUseSourceCache(bool b) {
@@ -449,69 +455,77 @@ bool Debugger::SetUseSourceCache(bool b) {
449455
}
450456
bool Debugger::GetHighlightSource() const {
451457
const uint32_t idx = ePropertyHighlightSource;
452-
return m_collection_sp->GetPropertyAtIndexAsBoolean(
453-
nullptr, idx, g_debugger_properties[idx].default_uint_value);
458+
return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx)
459+
.value_or(g_debugger_properties[idx].default_uint_value);
454460
}
455461

456462
StopShowColumn Debugger::GetStopShowColumn() const {
457463
const uint32_t idx = ePropertyStopShowColumn;
458-
return (lldb::StopShowColumn)m_collection_sp->GetPropertyAtIndexAsEnumeration(
459-
nullptr, idx, g_debugger_properties[idx].default_uint_value);
464+
return (lldb::StopShowColumn)m_collection_sp
465+
->GetPropertyAtIndexAsEnumeration(nullptr, idx)
466+
.value_or(g_debugger_properties[idx].default_uint_value);
460467
}
461468

462469
llvm::StringRef Debugger::GetStopShowColumnAnsiPrefix() const {
463470
const uint32_t idx = ePropertyStopShowColumnAnsiPrefix;
464-
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, "");
471+
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx)
472+
.value_or(g_debugger_properties[idx].default_cstr_value);
465473
}
466474

467475
llvm::StringRef Debugger::GetStopShowColumnAnsiSuffix() const {
468476
const uint32_t idx = ePropertyStopShowColumnAnsiSuffix;
469-
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, "");
477+
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx)
478+
.value_or(g_debugger_properties[idx].default_cstr_value);
470479
}
471480

472481
llvm::StringRef Debugger::GetStopShowLineMarkerAnsiPrefix() const {
473482
const uint32_t idx = ePropertyStopShowLineMarkerAnsiPrefix;
474-
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, "");
483+
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx)
484+
.value_or(g_debugger_properties[idx].default_cstr_value);
475485
}
476486

477487
llvm::StringRef Debugger::GetStopShowLineMarkerAnsiSuffix() const {
478488
const uint32_t idx = ePropertyStopShowLineMarkerAnsiSuffix;
479-
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, "");
489+
return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx)
490+
.value_or(g_debugger_properties[idx].default_cstr_value);
480491
}
481492

482493
uint32_t Debugger::GetStopSourceLineCount(bool before) const {
483494
const uint32_t idx =
484495
before ? ePropertyStopLineCountBefore : ePropertyStopLineCountAfter;
485-
return m_collection_sp->GetPropertyAtIndexAsSInt64(
486-
nullptr, idx, g_debugger_properties[idx].default_uint_value);
496+
return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx)
497+
.value_or(g_debugger_properties[idx].default_uint_value);
487498
}
488499

489500
Debugger::StopDisassemblyType Debugger::GetStopDisassemblyDisplay() const {
490501
const uint32_t idx = ePropertyStopDisassemblyDisplay;
491-
return (Debugger::StopDisassemblyType)
492-
m_collection_sp->GetPropertyAtIndexAsEnumeration(
493-
nullptr, idx, g_debugger_properties[idx].default_uint_value);
502+
return (Debugger::StopDisassemblyType)m_collection_sp
503+
->GetPropertyAtIndexAsEnumeration(nullptr, idx)
504+
.value_or(g_debugger_properties[idx].default_uint_value);
494505
}
495506

496507
uint32_t Debugger::GetDisassemblyLineCount() const {
497508
const uint32_t idx = ePropertyStopDisassemblyCount;
498-
return m_collection_sp->GetPropertyAtIndexAsSInt64(
499-
nullptr, idx, g_debugger_properties[idx].default_uint_value);
509+
return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx)
510+
.value_or(g_debugger_properties[idx].default_uint_value);
500511
}
501512

502513
bool Debugger::GetAutoOneLineSummaries() const {
503514
const uint32_t idx = ePropertyAutoOneLineSummaries;
504-
return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, true);
515+
return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx)
516+
.value_or(g_debugger_properties[idx].default_uint_value != 0);
505517
}
506518

507519
bool Debugger::GetEscapeNonPrintables() const {
508520
const uint32_t idx = ePropertyEscapeNonPrintables;
509-
return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, true);
521+
return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx)
522+
.value_or(g_debugger_properties[idx].default_uint_value != 0);
510523
}
511524

512525
bool Debugger::GetAutoIndent() const {
513526
const uint32_t idx = ePropertyAutoIndent;
514-
return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, true);
527+
return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx)
528+
.value_or(g_debugger_properties[idx].default_uint_value != 0);
515529
}
516530

517531
bool Debugger::SetAutoIndent(bool b) {
@@ -521,7 +535,8 @@ bool Debugger::SetAutoIndent(bool b) {
521535

522536
bool Debugger::GetPrintDecls() const {
523537
const uint32_t idx = ePropertyPrintDecls;
524-
return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, true);
538+
return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx)
539+
.value_or(g_debugger_properties[idx].default_uint_value != 0);
525540
}
526541

527542
bool Debugger::SetPrintDecls(bool b) {
@@ -531,8 +546,8 @@ bool Debugger::SetPrintDecls(bool b) {
531546

532547
uint32_t Debugger::GetTabSize() const {
533548
const uint32_t idx = ePropertyTabSize;
534-
return m_collection_sp->GetPropertyAtIndexAsUInt64(
535-
nullptr, idx, g_debugger_properties[idx].default_uint_value);
549+
return m_collection_sp->GetPropertyAtIndexAsUInt64(nullptr, idx)
550+
.value_or(g_debugger_properties[idx].default_uint_value);
536551
}
537552

538553
bool Debugger::SetTabSize(uint32_t tab_size) {
@@ -542,9 +557,9 @@ bool Debugger::SetTabSize(uint32_t tab_size) {
542557

543558
lldb::DWIMPrintVerbosity Debugger::GetDWIMPrintVerbosity() const {
544559
const uint32_t idx = ePropertyDWIMPrintVerbosity;
545-
return (lldb::DWIMPrintVerbosity)
546-
m_collection_sp->GetPropertyAtIndexAsEnumeration(
547-
nullptr, idx, g_debugger_properties[idx].default_uint_value);
560+
return (lldb::DWIMPrintVerbosity)m_collection_sp
561+
->GetPropertyAtIndexAsEnumeration(nullptr, idx)
562+
.value_or(g_debugger_properties[idx].default_uint_value);
548563
}
549564

550565
#pragma mark Debugger

0 commit comments

Comments
 (0)