Replace redundant code in LanguageCategory with templates (NFC)

Differential Revision: https://reviews.llvm.org/D71296
This commit is contained in:
Adrian Prantl 2019-12-10 13:22:03 -08:00
parent 636c93ed11
commit b3801de7c5
2 changed files with 63 additions and 153 deletions

View File

@ -25,27 +25,11 @@ public:
LanguageCategory(lldb::LanguageType lang_type); LanguageCategory(lldb::LanguageType lang_type);
bool Get(FormattersMatchData &match_data, lldb::TypeFormatImplSP &format_sp); template <typename ImplSP>
bool Get(FormattersMatchData &match_data, ImplSP &format_sp);
bool Get(FormattersMatchData &match_data, lldb::TypeSummaryImplSP &format_sp); template <typename ImplSP>
bool Get(FormattersMatchData &match_data,
lldb::SyntheticChildrenSP &format_sp);
bool Get(FormattersMatchData &match_data,
lldb::TypeValidatorImplSP &format_sp);
bool GetHardcoded(FormatManager &fmt_mgr, FormattersMatchData &match_data, bool GetHardcoded(FormatManager &fmt_mgr, FormattersMatchData &match_data,
lldb::TypeFormatImplSP &format_sp); ImplSP &format_sp);
bool GetHardcoded(FormatManager &fmt_mgr, FormattersMatchData &match_data,
lldb::TypeSummaryImplSP &format_sp);
bool GetHardcoded(FormatManager &fmt_mgr, FormattersMatchData &match_data,
lldb::SyntheticChildrenSP &format_sp);
bool GetHardcoded(FormatManager &fmt_mgr, FormattersMatchData &match_data,
lldb::TypeValidatorImplSP &format_sp);
lldb::TypeCategoryImplSP GetCategory() const; lldb::TypeCategoryImplSP GetCategory() const;
@ -65,6 +49,9 @@ private:
HardcodedFormatters::HardcodedSyntheticFinder m_hardcoded_synthetics; HardcodedFormatters::HardcodedSyntheticFinder m_hardcoded_synthetics;
HardcodedFormatters::HardcodedValidatorFinder m_hardcoded_validators; HardcodedFormatters::HardcodedValidatorFinder m_hardcoded_validators;
template <typename ImplSP>
auto &GetHardcodedFinder();
lldb_private::FormatCache m_format_cache; lldb_private::FormatCache m_format_cache;
bool m_enabled; bool m_enabled;

View File

@ -34,8 +34,9 @@ LanguageCategory::LanguageCategory(lldb::LanguageType lang_type)
Enable(); Enable();
} }
template<typename ImplSP>
bool LanguageCategory::Get(FormattersMatchData &match_data, bool LanguageCategory::Get(FormattersMatchData &match_data,
lldb::TypeFormatImplSP &format_sp) { ImplSP &retval_sp) {
if (!m_category_sp) if (!m_category_sp)
return false; return false;
@ -43,168 +44,90 @@ bool LanguageCategory::Get(FormattersMatchData &match_data,
return false; return false;
if (match_data.GetTypeForCache()) { if (match_data.GetTypeForCache()) {
if (m_format_cache.Get(match_data.GetTypeForCache(), format_sp)) if (m_format_cache.Get(match_data.GetTypeForCache(), retval_sp))
return format_sp.get() != nullptr; return (bool)retval_sp;
} }
ValueObject &valobj(match_data.GetValueObject()); ValueObject &valobj(match_data.GetValueObject());
bool result = m_category_sp->Get(valobj.GetObjectRuntimeLanguage(), bool result = m_category_sp->Get(valobj.GetObjectRuntimeLanguage(),
match_data.GetMatchesVector(), format_sp); match_data.GetMatchesVector(), retval_sp);
if (match_data.GetTypeForCache() && if (match_data.GetTypeForCache() &&
(!format_sp || !format_sp->NonCacheable())) { (!retval_sp || !retval_sp->NonCacheable())) {
m_format_cache.Set(match_data.GetTypeForCache(), format_sp); m_format_cache.Set(match_data.GetTypeForCache(), retval_sp);
} }
return result; return result;
} }
bool LanguageCategory::Get(FormattersMatchData &match_data, /// Explicit instantiations for the four types.
lldb::TypeSummaryImplSP &format_sp) { /// \{
if (!m_category_sp) template bool
return false; LanguageCategory::Get<lldb::TypeValidatorImplSP>(FormattersMatchData &,
lldb::TypeValidatorImplSP &);
template bool
LanguageCategory::Get<lldb::TypeFormatImplSP>(FormattersMatchData &,
lldb::TypeFormatImplSP &);
template bool
LanguageCategory::Get<lldb::TypeSummaryImplSP>(FormattersMatchData &,
lldb::TypeSummaryImplSP &);
template bool
LanguageCategory::Get<lldb::SyntheticChildrenSP>(FormattersMatchData &,
lldb::SyntheticChildrenSP &);
/// \}
if (!IsEnabled()) template <>
return false; auto &LanguageCategory::GetHardcodedFinder<lldb::TypeFormatImplSP>() {
return m_hardcoded_formats;
if (match_data.GetTypeForCache()) {
if (m_format_cache.Get(match_data.GetTypeForCache(), format_sp))
return format_sp.get() != nullptr;
}
ValueObject &valobj(match_data.GetValueObject());
bool result = m_category_sp->Get(valobj.GetObjectRuntimeLanguage(),
match_data.GetMatchesVector(), format_sp);
if (match_data.GetTypeForCache() &&
(!format_sp || !format_sp->NonCacheable())) {
m_format_cache.Set(match_data.GetTypeForCache(), format_sp);
}
return result;
} }
bool LanguageCategory::Get(FormattersMatchData &match_data, template <>
lldb::SyntheticChildrenSP &format_sp) { auto &LanguageCategory::GetHardcodedFinder<lldb::TypeSummaryImplSP>() {
if (!m_category_sp) return m_hardcoded_summaries;
return false;
if (!IsEnabled())
return false;
if (match_data.GetTypeForCache()) {
if (m_format_cache.Get(match_data.GetTypeForCache(), format_sp))
return format_sp.get() != nullptr;
}
ValueObject &valobj(match_data.GetValueObject());
bool result = m_category_sp->Get(valobj.GetObjectRuntimeLanguage(),
match_data.GetMatchesVector(), format_sp);
if (match_data.GetTypeForCache() &&
(!format_sp || !format_sp->NonCacheable())) {
m_format_cache.Set(match_data.GetTypeForCache(), format_sp);
}
return result;
} }
bool LanguageCategory::Get(FormattersMatchData &match_data, template <>
lldb::TypeValidatorImplSP &format_sp) { auto &LanguageCategory::GetHardcodedFinder<lldb::SyntheticChildrenSP>() {
if (!m_category_sp) return m_hardcoded_synthetics;
return false;
if (!IsEnabled())
return false;
if (match_data.GetTypeForCache()) {
if (m_format_cache.Get(match_data.GetTypeForCache(), format_sp))
return format_sp.get() != nullptr;
}
ValueObject &valobj(match_data.GetValueObject());
bool result = m_category_sp->Get(valobj.GetObjectRuntimeLanguage(),
match_data.GetMatchesVector(), format_sp);
if (match_data.GetTypeForCache() &&
(!format_sp || !format_sp->NonCacheable())) {
m_format_cache.Set(match_data.GetTypeForCache(), format_sp);
}
return result;
} }
template <>
auto &LanguageCategory::GetHardcodedFinder<lldb::TypeValidatorImplSP>() {
return m_hardcoded_validators;
}
template <typename ImplSP>
bool LanguageCategory::GetHardcoded(FormatManager &fmt_mgr, bool LanguageCategory::GetHardcoded(FormatManager &fmt_mgr,
FormattersMatchData &match_data, FormattersMatchData &match_data,
lldb::TypeFormatImplSP &format_sp) { ImplSP &retval_sp) {
if (!IsEnabled()) if (!IsEnabled())
return false; return false;
ValueObject &valobj(match_data.GetValueObject()); ValueObject &valobj(match_data.GetValueObject());
lldb::DynamicValueType use_dynamic(match_data.GetDynamicValueType()); lldb::DynamicValueType use_dynamic(match_data.GetDynamicValueType());
for (auto &candidate : m_hardcoded_formats) { for (auto &candidate : GetHardcodedFinder<ImplSP>()) {
if ((format_sp = candidate(valobj, use_dynamic, fmt_mgr))) if (auto result = candidate(valobj, use_dynamic, fmt_mgr)) {
retval_sp = result;
break; break;
}
} }
if (match_data.GetTypeForCache() && if (match_data.GetTypeForCache() &&
(!format_sp || !format_sp->NonCacheable())) { (!retval_sp || !retval_sp->NonCacheable())) {
m_format_cache.Set(match_data.GetTypeForCache(), format_sp); m_format_cache.Set(match_data.GetTypeForCache(), retval_sp);
} }
return format_sp.get() != nullptr; return (bool)retval_sp;
} }
bool LanguageCategory::GetHardcoded(FormatManager &fmt_mgr, /// Explicit instantiations for the four types.
FormattersMatchData &match_data, /// \{
lldb::TypeSummaryImplSP &format_sp) { template bool LanguageCategory::GetHardcoded<lldb::TypeValidatorImplSP>(
if (!IsEnabled()) FormatManager &, FormattersMatchData &, lldb::TypeValidatorImplSP &);
return false; template bool LanguageCategory::GetHardcoded<lldb::TypeFormatImplSP>(
FormatManager &, FormattersMatchData &, lldb::TypeFormatImplSP &);
ValueObject &valobj(match_data.GetValueObject()); template bool LanguageCategory::GetHardcoded<lldb::TypeSummaryImplSP>(
lldb::DynamicValueType use_dynamic(match_data.GetDynamicValueType()); FormatManager &, FormattersMatchData &, lldb::TypeSummaryImplSP &);
template bool LanguageCategory::GetHardcoded<lldb::SyntheticChildrenSP>(
for (auto &candidate : m_hardcoded_summaries) { FormatManager &, FormattersMatchData &, lldb::SyntheticChildrenSP &);
if ((format_sp = candidate(valobj, use_dynamic, fmt_mgr))) /// \}
break;
}
if (match_data.GetTypeForCache() &&
(!format_sp || !format_sp->NonCacheable())) {
m_format_cache.Set(match_data.GetTypeForCache(), format_sp);
}
return format_sp.get() != nullptr;
}
bool LanguageCategory::GetHardcoded(FormatManager &fmt_mgr,
FormattersMatchData &match_data,
lldb::SyntheticChildrenSP &format_sp) {
if (!IsEnabled())
return false;
ValueObject &valobj(match_data.GetValueObject());
lldb::DynamicValueType use_dynamic(match_data.GetDynamicValueType());
for (auto &candidate : m_hardcoded_synthetics) {
if ((format_sp = candidate(valobj, use_dynamic, fmt_mgr)))
break;
}
if (match_data.GetTypeForCache() &&
(!format_sp || !format_sp->NonCacheable())) {
m_format_cache.Set(match_data.GetTypeForCache(), format_sp);
}
return format_sp.get() != nullptr;
}
bool LanguageCategory::GetHardcoded(FormatManager &fmt_mgr,
FormattersMatchData &match_data,
lldb::TypeValidatorImplSP &format_sp) {
if (!IsEnabled())
return false;
ValueObject &valobj(match_data.GetValueObject());
lldb::DynamicValueType use_dynamic(match_data.GetDynamicValueType());
for (auto &candidate : m_hardcoded_validators) {
if ((format_sp = candidate(valobj, use_dynamic, fmt_mgr)))
break;
}
if (match_data.GetTypeForCache() &&
(!format_sp || !format_sp->NonCacheable())) {
m_format_cache.Set(match_data.GetTypeForCache(), format_sp);
}
return format_sp.get() != nullptr;
}
lldb::TypeCategoryImplSP LanguageCategory::GetCategory() const { lldb::TypeCategoryImplSP LanguageCategory::GetCategory() const {
return m_category_sp; return m_category_sp;