Skip to content

Commit 6f8b33f

Browse files
committed
[lldb] Use templates to simplify {Get,Set}PropertyAtIndex (NFC)
Use templates to simplify {Get,Set}PropertyAtIndex. It has always bothered me how cumbersome those calls are when adding new properties. After this patch, SetPropertyAtIndex infers the type from its arguments and GetPropertyAtIndex required a single template argument for the return value. As an added benefit, this enables us to remove a bunch of wrappers from UserSettingsController and OptionValueProperties. Differential revision: https://reviews.llvm.org/D149774
1 parent 04aa943 commit 6f8b33f

File tree

22 files changed

+446
-525
lines changed

22 files changed

+446
-525
lines changed

lldb/include/lldb/Core/Debugger.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
269269

270270
const FormatEntity::Entry *GetFrameFormatUnique() const;
271271

272-
uint32_t GetStopDisassemblyMaxSize() const;
272+
uint64_t GetStopDisassemblyMaxSize() const;
273273

274274
const FormatEntity::Entry *GetThreadFormat() const;
275275

@@ -283,7 +283,7 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
283283

284284
bool SetREPLLanguage(lldb::LanguageType repl_lang);
285285

286-
uint32_t GetTerminalWidth() const;
286+
uint64_t GetTerminalWidth() const;
287287

288288
bool SetTerminalWidth(uint32_t term_width);
289289

@@ -329,11 +329,11 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
329329

330330
llvm::StringRef GetStopShowColumnAnsiSuffix() const;
331331

332-
uint32_t GetStopSourceLineCount(bool before) const;
332+
uint64_t GetStopSourceLineCount(bool before) const;
333333

334334
StopDisassemblyType GetStopDisassemblyDisplay() const;
335335

336-
uint32_t GetDisassemblyLineCount() const;
336+
uint64_t GetDisassemblyLineCount() const;
337337

338338
llvm::StringRef GetStopShowLineMarkerAnsiPrefix() const;
339339

@@ -349,7 +349,7 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
349349

350350
bool SetPrintDecls(bool b);
351351

352-
uint32_t GetTabSize() const;
352+
uint64_t GetTabSize() const;
353353

354354
bool SetTabSize(uint32_t tab_size);
355355

lldb/include/lldb/Core/UserSettingsController.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLDB_CORE_USERSETTINGSCONTROLLER_H
1010
#define LLDB_CORE_USERSETTINGSCONTROLLER_H
1111

12+
#include "lldb/Interpreter/OptionValueProperties.h"
1213
#include "lldb/Utility/Status.h"
1314
#include "lldb/lldb-forward.h"
1415
#include "lldb/lldb-private-enumerations.h"
@@ -82,6 +83,27 @@ class Properties {
8283

8384
static bool IsSettingExperimental(llvm::StringRef setting);
8485

86+
template <typename T>
87+
T GetPropertyAtIndexAs(uint32_t idx, T default_value,
88+
const ExecutionContext *exe_ctx = nullptr) const {
89+
return m_collection_sp->GetPropertyAtIndexAs<T>(idx, exe_ctx)
90+
.value_or(default_value);
91+
}
92+
93+
template <typename T, typename U = typename std::remove_pointer<T>::type,
94+
std::enable_if_t<std::is_pointer_v<T>, bool> = true>
95+
const U *
96+
GetPropertyAtIndexAs(uint32_t idx,
97+
const ExecutionContext *exe_ctx = nullptr) const {
98+
return m_collection_sp->GetPropertyAtIndexAs<T>(idx, exe_ctx);
99+
}
100+
101+
template <typename T>
102+
bool SetPropertyAtIndex(uint32_t idx, T t,
103+
const ExecutionContext *exe_ctx = nullptr) const {
104+
return m_collection_sp->SetPropertyAtIndex<T>(idx, t, exe_ctx);
105+
}
106+
85107
protected:
86108
lldb::OptionValuePropertiesSP m_collection_sp;
87109
};

lldb/include/lldb/Interpreter/OptionValue.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,51 @@ class OptionValue {
322322
m_callback();
323323
}
324324

325+
template <typename T, std::enable_if_t<!std::is_pointer_v<T>, bool> = true>
326+
std::optional<T> GetValueAs() const {
327+
if constexpr (std::is_same_v<T, uint64_t>)
328+
return GetUInt64Value();
329+
if constexpr (std::is_same_v<T, int64_t>)
330+
return GetSInt64Value();
331+
if constexpr (std::is_same_v<T, bool>)
332+
return GetBooleanValue();
333+
if constexpr (std::is_same_v<T, char>)
334+
return GetCharValue();
335+
if constexpr (std::is_same_v<T, lldb::Format>)
336+
return GetFormatValue();
337+
if constexpr (std::is_same_v<T, lldb::LanguageType>)
338+
return GetLanguageValue();
339+
if constexpr (std::is_same_v<T, llvm::StringRef>)
340+
return GetStringValue();
341+
if constexpr (std::is_enum_v<T>)
342+
if (std::optional<int64_t> value = GetEnumerationValue())
343+
return static_cast<T>(*value);
344+
return {};
345+
}
346+
347+
template <typename T,
348+
typename U = typename std::remove_const<
349+
typename std::remove_pointer<T>::type>::type,
350+
std::enable_if_t<std::is_pointer_v<T>, bool> = true>
351+
T GetValueAs() const {
352+
if constexpr (std::is_same_v<U, FormatEntity::Entry>)
353+
return GetFormatEntity();
354+
if constexpr (std::is_same_v<U, RegularExpression>)
355+
return GetRegexValue();
356+
return {};
357+
}
358+
359+
bool SetValueAs(bool v) { return SetBooleanValue(v); }
360+
361+
bool SetValueAs(llvm::StringRef v) { return SetStringValue(v); }
362+
363+
bool SetValueAs(lldb::LanguageType v) { return SetLanguageValue(v); }
364+
365+
template <typename T, std::enable_if_t<std::is_enum_v<T>, bool> = true>
366+
bool SetValueAs(T t) {
367+
return SetEnumerationValue(t);
368+
}
369+
325370
protected:
326371
using TopmostBase = OptionValue;
327372

lldb/include/lldb/Interpreter/OptionValueProperties.h

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -122,57 +122,15 @@ class OptionValueProperties
122122
bool SetPropertyAtIndexFromArgs(uint32_t idx, const Args &args,
123123
const ExecutionContext *exe_ctx = nullptr);
124124

125-
std::optional<bool>
126-
GetPropertyAtIndexAsBoolean(uint32_t idx,
127-
const ExecutionContext *exe_ctx = nullptr) const;
128-
129-
bool SetPropertyAtIndexAsBoolean(uint32_t idx, bool new_value,
130-
const ExecutionContext *exe_ctx = nullptr);
131-
132125
OptionValueDictionary *GetPropertyAtIndexAsOptionValueDictionary(
133126
uint32_t idx, const ExecutionContext *exe_ctx = nullptr) const;
134127

135-
std::optional<int64_t> GetPropertyAtIndexAsEnumeration(
136-
uint32_t idx, const ExecutionContext *exe_ctx = nullptr) const;
137-
138-
bool
139-
SetPropertyAtIndexAsEnumeration(uint32_t idx, int64_t new_value,
140-
const ExecutionContext *exe_ctx = nullptr);
141-
142-
const FormatEntity::Entry *
143-
GetPropertyAtIndexAsFormatEntity(uint32_t idx,
144-
const ExecutionContext *exe_ctx = nullptr);
145-
146-
const RegularExpression *GetPropertyAtIndexAsOptionValueRegex(
147-
uint32_t idx, const ExecutionContext *exe_ctx = nullptr) const;
148-
149128
OptionValueSInt64 *GetPropertyAtIndexAsOptionValueSInt64(
150129
uint32_t idx, const ExecutionContext *exe_ctx = nullptr) const;
151130

152131
OptionValueUInt64 *GetPropertyAtIndexAsOptionValueUInt64(
153132
uint32_t idx, const ExecutionContext *exe_ctx = nullptr) const;
154133

155-
std::optional<int64_t>
156-
GetPropertyAtIndexAsSInt64(uint32_t idx,
157-
const ExecutionContext *exe_ctx = nullptr) const;
158-
159-
bool SetPropertyAtIndexAsSInt64(uint32_t idx, int64_t new_value,
160-
const ExecutionContext *exe_ctx = nullptr);
161-
162-
std::optional<uint64_t>
163-
GetPropertyAtIndexAsUInt64(uint32_t idx,
164-
const ExecutionContext *exe_ctx = nullptr) const;
165-
166-
bool SetPropertyAtIndexAsUInt64(uint32_t idx, uint64_t new_value,
167-
const ExecutionContext *exe_ctx = nullptr);
168-
169-
std::optional<llvm::StringRef>
170-
GetPropertyAtIndexAsString(uint32_t idx,
171-
const ExecutionContext *exe_ctx = nullptr) const;
172-
173-
bool SetPropertyAtIndexAsString(uint32_t idx, llvm::StringRef new_value,
174-
const ExecutionContext *exe_ctx = nullptr);
175-
176134
OptionValueString *GetPropertyAtIndexAsOptionValueString(
177135
uint32_t idx, const ExecutionContext *exe_ctx = nullptr) const;
178136

@@ -201,6 +159,31 @@ class OptionValueProperties
201159
void SetValueChangedCallback(uint32_t property_idx,
202160
std::function<void()> callback);
203161

162+
template <typename T>
163+
auto GetPropertyAtIndexAs(uint32_t idx,
164+
const ExecutionContext *exe_ctx = nullptr) const {
165+
if (const Property *property = GetPropertyAtIndex(idx, exe_ctx)) {
166+
if (OptionValue *value = property->GetValue().get())
167+
return value->GetValueAs<T>();
168+
}
169+
if constexpr (std::is_pointer_v<T>)
170+
return T{nullptr};
171+
else
172+
return std::optional<T>{std::nullopt};
173+
}
174+
175+
template <typename T>
176+
bool SetPropertyAtIndex(uint32_t idx, T t,
177+
const ExecutionContext *exe_ctx = nullptr) const {
178+
if (const Property *property = GetPropertyAtIndex(idx, exe_ctx)) {
179+
if (OptionValue *value = property->GetValue().get()) {
180+
value->SetValueAs(t);
181+
return true;
182+
}
183+
}
184+
return false;
185+
}
186+
204187
protected:
205188
Property *ProtectedGetPropertyAtIndex(uint32_t idx) {
206189
assert(idx < m_properties.size() && "invalid property index");

lldb/source/Core/CoreProperties.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ let Definition = "debugger" in {
7474
Global,
7575
DefaultEnumValue<"eLanguageTypeUnknown">,
7676
Desc<"The language to use for the REPL.">;
77-
def StopDisassemblyCount: Property<"stop-disassembly-count", "SInt64">,
77+
def StopDisassemblyCount: Property<"stop-disassembly-count", "UInt64">,
7878
Global,
7979
DefaultUnsignedValue<4>,
8080
Desc<"The number of disassembly lines to show when displaying a stopped context.">;
@@ -87,11 +87,11 @@ let Definition = "debugger" in {
8787
Global,
8888
DefaultUnsignedValue<32000>,
8989
Desc<"The size limit to use when disassembling large functions (default: 32KB).">;
90-
def StopLineCountAfter: Property<"stop-line-count-after", "SInt64">,
90+
def StopLineCountAfter: Property<"stop-line-count-after", "UInt64">,
9191
Global,
9292
DefaultUnsignedValue<3>,
9393
Desc<"The number of sources lines to display that come after the current source line when displaying a stopped context.">;
94-
def StopLineCountBefore: Property<"stop-line-count-before", "SInt64">,
94+
def StopLineCountBefore: Property<"stop-line-count-before", "UInt64">,
9595
Global,
9696
DefaultUnsignedValue<3>,
9797
Desc<"The number of sources lines to display that come before the current source line when displaying a stopped context.">;

0 commit comments

Comments
 (0)