[Lldb-commits] [lldb] b3801de - Replace redundant code in LanguageCategory with templates (NFC)

Adrian Prantl via lldb-commits lldb-commits at lists.llvm.org
Tue Dec 10 16:11:18 PST 2019


Author: Adrian Prantl
Date: 2019-12-10T16:11:10-08:00
New Revision: b3801de7c50faa2b730e62e1cfabeec8c5cebae2

URL: https://github.com/llvm/llvm-project/commit/b3801de7c50faa2b730e62e1cfabeec8c5cebae2
DIFF: https://github.com/llvm/llvm-project/commit/b3801de7c50faa2b730e62e1cfabeec8c5cebae2.diff

LOG: Replace redundant code in LanguageCategory with templates (NFC)

Differential Revision: https://reviews.llvm.org/D71296

Added: 
    

Modified: 
    lldb/include/lldb/DataFormatters/LanguageCategory.h
    lldb/source/DataFormatters/LanguageCategory.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/DataFormatters/LanguageCategory.h b/lldb/include/lldb/DataFormatters/LanguageCategory.h
index f109d46194ad..c6604ec80fad 100644
--- a/lldb/include/lldb/DataFormatters/LanguageCategory.h
+++ b/lldb/include/lldb/DataFormatters/LanguageCategory.h
@@ -25,27 +25,11 @@ class LanguageCategory {
 
   LanguageCategory(lldb::LanguageType lang_type);
 
-  bool Get(FormattersMatchData &match_data, lldb::TypeFormatImplSP &format_sp);
-
-  bool Get(FormattersMatchData &match_data, lldb::TypeSummaryImplSP &format_sp);
-
-  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,
-                    lldb::TypeFormatImplSP &format_sp);
-
+  template <typename ImplSP>
+  bool Get(FormattersMatchData &match_data, ImplSP &format_sp);
+  template <typename ImplSP>
   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);
+                    ImplSP &format_sp);
 
   lldb::TypeCategoryImplSP GetCategory() const;
 
@@ -65,6 +49,9 @@ class LanguageCategory {
   HardcodedFormatters::HardcodedSyntheticFinder m_hardcoded_synthetics;
   HardcodedFormatters::HardcodedValidatorFinder m_hardcoded_validators;
 
+  template <typename ImplSP>
+  auto &GetHardcodedFinder();
+
   lldb_private::FormatCache m_format_cache;
 
   bool m_enabled;

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


        


More information about the lldb-commits mailing list