Skip to content

Commit

Permalink
[lldb] Use templates to simplify {Get,Set}PropertyAtIndex (NFC)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JDevlieghere committed May 4, 2023
1 parent 04aa943 commit 6f8b33f
Show file tree
Hide file tree
Showing 22 changed files with 446 additions and 525 deletions.
10 changes: 5 additions & 5 deletions lldb/include/lldb/Core/Debugger.h
Expand Up @@ -269,7 +269,7 @@ class Debugger : public std::enable_shared_from_this<Debugger>,

const FormatEntity::Entry *GetFrameFormatUnique() const;

uint32_t GetStopDisassemblyMaxSize() const;
uint64_t GetStopDisassemblyMaxSize() const;

const FormatEntity::Entry *GetThreadFormat() const;

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

bool SetREPLLanguage(lldb::LanguageType repl_lang);

uint32_t GetTerminalWidth() const;
uint64_t GetTerminalWidth() const;

bool SetTerminalWidth(uint32_t term_width);

Expand Down Expand Up @@ -329,11 +329,11 @@ class Debugger : public std::enable_shared_from_this<Debugger>,

llvm::StringRef GetStopShowColumnAnsiSuffix() const;

uint32_t GetStopSourceLineCount(bool before) const;
uint64_t GetStopSourceLineCount(bool before) const;

StopDisassemblyType GetStopDisassemblyDisplay() const;

uint32_t GetDisassemblyLineCount() const;
uint64_t GetDisassemblyLineCount() const;

llvm::StringRef GetStopShowLineMarkerAnsiPrefix() const;

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

bool SetPrintDecls(bool b);

uint32_t GetTabSize() const;
uint64_t GetTabSize() const;

bool SetTabSize(uint32_t tab_size);

Expand Down
22 changes: 22 additions & 0 deletions lldb/include/lldb/Core/UserSettingsController.h
Expand Up @@ -9,6 +9,7 @@
#ifndef LLDB_CORE_USERSETTINGSCONTROLLER_H
#define LLDB_CORE_USERSETTINGSCONTROLLER_H

#include "lldb/Interpreter/OptionValueProperties.h"
#include "lldb/Utility/Status.h"
#include "lldb/lldb-forward.h"
#include "lldb/lldb-private-enumerations.h"
Expand Down Expand Up @@ -82,6 +83,27 @@ class Properties {

static bool IsSettingExperimental(llvm::StringRef setting);

template <typename T>
T GetPropertyAtIndexAs(uint32_t idx, T default_value,
const ExecutionContext *exe_ctx = nullptr) const {
return m_collection_sp->GetPropertyAtIndexAs<T>(idx, exe_ctx)
.value_or(default_value);
}

template <typename T, typename U = typename std::remove_pointer<T>::type,
std::enable_if_t<std::is_pointer_v<T>, bool> = true>
const U *
GetPropertyAtIndexAs(uint32_t idx,
const ExecutionContext *exe_ctx = nullptr) const {
return m_collection_sp->GetPropertyAtIndexAs<T>(idx, exe_ctx);
}

template <typename T>
bool SetPropertyAtIndex(uint32_t idx, T t,
const ExecutionContext *exe_ctx = nullptr) const {
return m_collection_sp->SetPropertyAtIndex<T>(idx, t, exe_ctx);
}

protected:
lldb::OptionValuePropertiesSP m_collection_sp;
};
Expand Down
45 changes: 45 additions & 0 deletions lldb/include/lldb/Interpreter/OptionValue.h
Expand Up @@ -322,6 +322,51 @@ class OptionValue {
m_callback();
}

template <typename T, std::enable_if_t<!std::is_pointer_v<T>, bool> = true>
std::optional<T> GetValueAs() const {
if constexpr (std::is_same_v<T, uint64_t>)
return GetUInt64Value();
if constexpr (std::is_same_v<T, int64_t>)
return GetSInt64Value();
if constexpr (std::is_same_v<T, bool>)
return GetBooleanValue();
if constexpr (std::is_same_v<T, char>)
return GetCharValue();
if constexpr (std::is_same_v<T, lldb::Format>)
return GetFormatValue();
if constexpr (std::is_same_v<T, lldb::LanguageType>)
return GetLanguageValue();
if constexpr (std::is_same_v<T, llvm::StringRef>)
return GetStringValue();
if constexpr (std::is_enum_v<T>)
if (std::optional<int64_t> value = GetEnumerationValue())
return static_cast<T>(*value);
return {};
}

template <typename T,
typename U = typename std::remove_const<
typename std::remove_pointer<T>::type>::type,
std::enable_if_t<std::is_pointer_v<T>, bool> = true>
T GetValueAs() const {
if constexpr (std::is_same_v<U, FormatEntity::Entry>)
return GetFormatEntity();
if constexpr (std::is_same_v<U, RegularExpression>)
return GetRegexValue();
return {};
}

bool SetValueAs(bool v) { return SetBooleanValue(v); }

bool SetValueAs(llvm::StringRef v) { return SetStringValue(v); }

bool SetValueAs(lldb::LanguageType v) { return SetLanguageValue(v); }

template <typename T, std::enable_if_t<std::is_enum_v<T>, bool> = true>
bool SetValueAs(T t) {
return SetEnumerationValue(t);
}

protected:
using TopmostBase = OptionValue;

Expand Down
67 changes: 25 additions & 42 deletions lldb/include/lldb/Interpreter/OptionValueProperties.h
Expand Up @@ -122,57 +122,15 @@ class OptionValueProperties
bool SetPropertyAtIndexFromArgs(uint32_t idx, const Args &args,
const ExecutionContext *exe_ctx = nullptr);

std::optional<bool>
GetPropertyAtIndexAsBoolean(uint32_t idx,
const ExecutionContext *exe_ctx = nullptr) const;

bool SetPropertyAtIndexAsBoolean(uint32_t idx, bool new_value,
const ExecutionContext *exe_ctx = nullptr);

OptionValueDictionary *GetPropertyAtIndexAsOptionValueDictionary(
uint32_t idx, const ExecutionContext *exe_ctx = nullptr) const;

std::optional<int64_t> GetPropertyAtIndexAsEnumeration(
uint32_t idx, const ExecutionContext *exe_ctx = nullptr) const;

bool
SetPropertyAtIndexAsEnumeration(uint32_t idx, int64_t new_value,
const ExecutionContext *exe_ctx = nullptr);

const FormatEntity::Entry *
GetPropertyAtIndexAsFormatEntity(uint32_t idx,
const ExecutionContext *exe_ctx = nullptr);

const RegularExpression *GetPropertyAtIndexAsOptionValueRegex(
uint32_t idx, const ExecutionContext *exe_ctx = nullptr) const;

OptionValueSInt64 *GetPropertyAtIndexAsOptionValueSInt64(
uint32_t idx, const ExecutionContext *exe_ctx = nullptr) const;

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

std::optional<int64_t>
GetPropertyAtIndexAsSInt64(uint32_t idx,
const ExecutionContext *exe_ctx = nullptr) const;

bool SetPropertyAtIndexAsSInt64(uint32_t idx, int64_t new_value,
const ExecutionContext *exe_ctx = nullptr);

std::optional<uint64_t>
GetPropertyAtIndexAsUInt64(uint32_t idx,
const ExecutionContext *exe_ctx = nullptr) const;

bool SetPropertyAtIndexAsUInt64(uint32_t idx, uint64_t new_value,
const ExecutionContext *exe_ctx = nullptr);

std::optional<llvm::StringRef>
GetPropertyAtIndexAsString(uint32_t idx,
const ExecutionContext *exe_ctx = nullptr) const;

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

OptionValueString *GetPropertyAtIndexAsOptionValueString(
uint32_t idx, const ExecutionContext *exe_ctx = nullptr) const;

Expand Down Expand Up @@ -201,6 +159,31 @@ class OptionValueProperties
void SetValueChangedCallback(uint32_t property_idx,
std::function<void()> callback);

template <typename T>
auto GetPropertyAtIndexAs(uint32_t idx,
const ExecutionContext *exe_ctx = nullptr) const {
if (const Property *property = GetPropertyAtIndex(idx, exe_ctx)) {
if (OptionValue *value = property->GetValue().get())
return value->GetValueAs<T>();
}
if constexpr (std::is_pointer_v<T>)
return T{nullptr};
else
return std::optional<T>{std::nullopt};
}

template <typename T>
bool SetPropertyAtIndex(uint32_t idx, T t,
const ExecutionContext *exe_ctx = nullptr) const {
if (const Property *property = GetPropertyAtIndex(idx, exe_ctx)) {
if (OptionValue *value = property->GetValue().get()) {
value->SetValueAs(t);
return true;
}
}
return false;
}

protected:
Property *ProtectedGetPropertyAtIndex(uint32_t idx) {
assert(idx < m_properties.size() && "invalid property index");
Expand Down
6 changes: 3 additions & 3 deletions lldb/source/Core/CoreProperties.td
Expand Up @@ -74,7 +74,7 @@ let Definition = "debugger" in {
Global,
DefaultEnumValue<"eLanguageTypeUnknown">,
Desc<"The language to use for the REPL.">;
def StopDisassemblyCount: Property<"stop-disassembly-count", "SInt64">,
def StopDisassemblyCount: Property<"stop-disassembly-count", "UInt64">,
Global,
DefaultUnsignedValue<4>,
Desc<"The number of disassembly lines to show when displaying a stopped context.">;
Expand All @@ -87,11 +87,11 @@ let Definition = "debugger" in {
Global,
DefaultUnsignedValue<32000>,
Desc<"The size limit to use when disassembling large functions (default: 32KB).">;
def StopLineCountAfter: Property<"stop-line-count-after", "SInt64">,
def StopLineCountAfter: Property<"stop-line-count-after", "UInt64">,
Global,
DefaultUnsignedValue<3>,
Desc<"The number of sources lines to display that come after the current source line when displaying a stopped context.">;
def StopLineCountBefore: Property<"stop-line-count-before", "SInt64">,
def StopLineCountBefore: Property<"stop-line-count-before", "UInt64">,
Global,
DefaultUnsignedValue<3>,
Desc<"The number of sources lines to display that come before the current source line when displaying a stopped context.">;
Expand Down

0 comments on commit 6f8b33f

Please sign in to comment.