[Lldb-commits] [lldb] 7034794 - Replace redundant code in FormatManager and FormatCache with templates (NFC)

Adrian Prantl via lldb-commits lldb-commits at lists.llvm.org
Tue Dec 10 10:27:25 PST 2019


Author: Adrian Prantl
Date: 2019-12-10T10:27:16-08:00
New Revision: 7034794b314d9de808de004d22b47f18d134757d

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

LOG: Replace redundant code in FormatManager and FormatCache with templates (NFC)

This is a preparatory patch for an upcoming bugfix.

FormatManager and friends have four identical implementations of many
accessor functions to deal with the four types of shared pointers in
the FormatCache. This patch replaces these implementations with
templates. While this patch drastically reduces the amount of source
code and its maintainablity, it doesn't actually improve code
size. I'd argue, this is still an improvement.

rdar://problem/57756763

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

Added: 
    

Modified: 
    lldb/include/lldb/DataFormatters/FormatCache.h
    lldb/include/lldb/DataFormatters/FormatManager.h
    lldb/include/lldb/DataFormatters/TypeCategoryMap.h
    lldb/source/DataFormatters/FormatCache.cpp
    lldb/source/DataFormatters/FormatManager.cpp
    lldb/source/DataFormatters/LanguageCategory.cpp
    lldb/source/DataFormatters/TypeCategoryMap.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/DataFormatters/FormatCache.h b/lldb/include/lldb/DataFormatters/FormatCache.h
index 7a6774b7f837..e8be01e55d40 100644
--- a/lldb/include/lldb/DataFormatters/FormatCache.h
+++ b/lldb/include/lldb/DataFormatters/FormatCache.h
@@ -33,36 +33,22 @@ class FormatCache {
 
   public:
     Entry();
-    Entry(lldb::TypeFormatImplSP);
-    Entry(lldb::TypeSummaryImplSP);
-    Entry(lldb::SyntheticChildrenSP);
-    Entry(lldb::TypeValidatorImplSP);
-    Entry(lldb::TypeFormatImplSP, lldb::TypeSummaryImplSP,
-          lldb::SyntheticChildrenSP, lldb::TypeValidatorImplSP);
 
+    template<typename ImplSP> bool IsCached();
     bool IsFormatCached();
-
     bool IsSummaryCached();
-
     bool IsSyntheticCached();
-
     bool IsValidatorCached();
 
-    lldb::TypeFormatImplSP GetFormat();
-
-    lldb::TypeSummaryImplSP GetSummary();
-
-    lldb::SyntheticChildrenSP GetSynthetic();
-
-    lldb::TypeValidatorImplSP GetValidator();
-
-    void SetFormat(lldb::TypeFormatImplSP);
-
-    void SetSummary(lldb::TypeSummaryImplSP);
-
-    void SetSynthetic(lldb::SyntheticChildrenSP);
+    void Get(lldb::TypeFormatImplSP &);
+    void Get(lldb::TypeSummaryImplSP &);
+    void Get(lldb::SyntheticChildrenSP &);
+    void Get(lldb::TypeValidatorImplSP &);
 
-    void SetValidator(lldb::TypeValidatorImplSP);
+    void Set(lldb::TypeFormatImplSP);
+    void Set(lldb::TypeSummaryImplSP);
+    void Set(lldb::SyntheticChildrenSP);
+    void Set(lldb::TypeValidatorImplSP);
   };
   typedef std::map<ConstString, Entry> CacheMap;
   CacheMap m_map;
@@ -76,25 +62,11 @@ class FormatCache {
 public:
   FormatCache();
 
-  bool GetFormat(ConstString type, lldb::TypeFormatImplSP &format_sp);
-
-  bool GetSummary(ConstString type, lldb::TypeSummaryImplSP &summary_sp);
-
-  bool GetSynthetic(ConstString type,
-                    lldb::SyntheticChildrenSP &synthetic_sp);
-
-  bool GetValidator(ConstString type,
-                    lldb::TypeValidatorImplSP &summary_sp);
-
-  void SetFormat(ConstString type, lldb::TypeFormatImplSP &format_sp);
-
-  void SetSummary(ConstString type, lldb::TypeSummaryImplSP &summary_sp);
-
-  void SetSynthetic(ConstString type,
-                    lldb::SyntheticChildrenSP &synthetic_sp);
-
-  void SetValidator(ConstString type,
-                    lldb::TypeValidatorImplSP &synthetic_sp);
+  template <typename ImplSP> bool Get(ConstString type, ImplSP &format_impl_sp);
+  void Set(ConstString type, lldb::TypeFormatImplSP &format_sp);
+  void Set(ConstString type, lldb::TypeSummaryImplSP &summary_sp);
+  void Set(ConstString type, lldb::SyntheticChildrenSP &synthetic_sp);
+  void Set(ConstString type, lldb::TypeValidatorImplSP &synthetic_sp);
 
   void Clear();
 
@@ -102,6 +74,7 @@ class FormatCache {
 
   uint64_t GetCacheMisses() { return m_cache_misses; }
 };
+
 } // namespace lldb_private
 
 #endif // lldb_FormatCache_h_

diff  --git a/lldb/include/lldb/DataFormatters/FormatManager.h b/lldb/include/lldb/DataFormatters/FormatManager.h
index 54877284c57b..dffba1f93987 100644
--- a/lldb/include/lldb/DataFormatters/FormatManager.h
+++ b/lldb/include/lldb/DataFormatters/FormatManager.h
@@ -208,14 +208,9 @@ class FormatManager : public IFormatChangeListener {
   ConstString m_system_category_name;
   ConstString m_vectortypes_category_name;
 
-  lldb::TypeFormatImplSP GetHardcodedFormat(FormattersMatchData &);
-
-  lldb::TypeSummaryImplSP GetHardcodedSummaryFormat(FormattersMatchData &);
-
-  lldb::SyntheticChildrenSP
-  GetHardcodedSyntheticChildren(FormattersMatchData &);
-
-  lldb::TypeValidatorImplSP GetHardcodedValidator(FormattersMatchData &);
+  template <typename ImplSP>
+  ImplSP GetCached(ValueObject &valobj, lldb::DynamicValueType use_dynamic);
+  template <typename ImplSP> ImplSP GetHardcoded(FormattersMatchData &);
 
   TypeCategoryMap &GetCategories() { return m_categories_map; }
 

diff  --git a/lldb/include/lldb/DataFormatters/TypeCategoryMap.h b/lldb/include/lldb/DataFormatters/TypeCategoryMap.h
index 753b58cb465e..ae577a334014 100644
--- a/lldb/include/lldb/DataFormatters/TypeCategoryMap.h
+++ b/lldb/include/lldb/DataFormatters/TypeCategoryMap.h
@@ -77,14 +77,7 @@ class TypeCategoryMap {
 
   uint32_t GetCount() { return m_map.size(); }
 
-  lldb::TypeFormatImplSP GetFormat(FormattersMatchData &match_data);
-
-  lldb::TypeSummaryImplSP GetSummaryFormat(FormattersMatchData &match_data);
-
-  lldb::SyntheticChildrenSP
-  GetSyntheticChildren(FormattersMatchData &match_data);
-
-  lldb::TypeValidatorImplSP GetValidator(FormattersMatchData &match_data);
+  template <typename ImplSP> void Get(FormattersMatchData &, ImplSP &);
 
 private:
   class delete_matching_categories {

diff  --git a/lldb/source/DataFormatters/FormatCache.cpp b/lldb/source/DataFormatters/FormatCache.cpp
index 7e328cb0dac8..0876673cfa8e 100644
--- a/lldb/source/DataFormatters/FormatCache.cpp
+++ b/lldb/source/DataFormatters/FormatCache.cpp
@@ -17,46 +17,7 @@ using namespace lldb_private;
 
 FormatCache::Entry::Entry()
     : m_format_cached(false), m_summary_cached(false),
-      m_synthetic_cached(false), m_validator_cached(false), m_format_sp(),
-      m_summary_sp(), m_synthetic_sp(), m_validator_sp() {}
-
-FormatCache::Entry::Entry(lldb::TypeFormatImplSP format_sp)
-    : m_summary_cached(false), m_synthetic_cached(false),
-      m_validator_cached(false), m_summary_sp(), m_synthetic_sp(),
-      m_validator_sp() {
-  SetFormat(format_sp);
-}
-
-FormatCache::Entry::Entry(lldb::TypeSummaryImplSP summary_sp)
-    : m_format_cached(false), m_synthetic_cached(false),
-      m_validator_cached(false), m_format_sp(), m_synthetic_sp(),
-      m_validator_sp() {
-  SetSummary(summary_sp);
-}
-
-FormatCache::Entry::Entry(lldb::SyntheticChildrenSP synthetic_sp)
-    : m_format_cached(false), m_summary_cached(false),
-      m_validator_cached(false), m_format_sp(), m_summary_sp(),
-      m_validator_sp() {
-  SetSynthetic(synthetic_sp);
-}
-
-FormatCache::Entry::Entry(lldb::TypeValidatorImplSP validator_sp)
-    : m_format_cached(false), m_summary_cached(false),
-      m_synthetic_cached(false), m_format_sp(), m_summary_sp(),
-      m_synthetic_sp() {
-  SetValidator(validator_sp);
-}
-
-FormatCache::Entry::Entry(lldb::TypeFormatImplSP format_sp,
-                          lldb::TypeSummaryImplSP summary_sp,
-                          lldb::SyntheticChildrenSP synthetic_sp,
-                          lldb::TypeValidatorImplSP validator_sp) {
-  SetFormat(format_sp);
-  SetSummary(summary_sp);
-  SetSynthetic(synthetic_sp);
-  SetValidator(validator_sp);
-}
+      m_synthetic_cached(false), m_validator_cached(false) {}
 
 bool FormatCache::Entry::IsFormatCached() { return m_format_cached; }
 
@@ -66,36 +27,38 @@ bool FormatCache::Entry::IsSyntheticCached() { return m_synthetic_cached; }
 
 bool FormatCache::Entry::IsValidatorCached() { return m_validator_cached; }
 
-lldb::TypeFormatImplSP FormatCache::Entry::GetFormat() { return m_format_sp; }
+void FormatCache::Entry::Get(lldb::TypeFormatImplSP &retval) {
+  retval = m_format_sp;
+}
 
-lldb::TypeSummaryImplSP FormatCache::Entry::GetSummary() {
-  return m_summary_sp;
+void FormatCache::Entry::Get(lldb::TypeSummaryImplSP &retval) {
+  retval = m_summary_sp;
 }
 
-lldb::SyntheticChildrenSP FormatCache::Entry::GetSynthetic() {
-  return m_synthetic_sp;
+void FormatCache::Entry::Get(lldb::SyntheticChildrenSP &retval) {
+  retval = m_synthetic_sp;
 }
 
-lldb::TypeValidatorImplSP FormatCache::Entry::GetValidator() {
-  return m_validator_sp;
+void FormatCache::Entry::Get(lldb::TypeValidatorImplSP &retval) {
+  retval = m_validator_sp;
 }
 
-void FormatCache::Entry::SetFormat(lldb::TypeFormatImplSP format_sp) {
+void FormatCache::Entry::Set(lldb::TypeFormatImplSP format_sp) {
   m_format_cached = true;
   m_format_sp = format_sp;
 }
 
-void FormatCache::Entry::SetSummary(lldb::TypeSummaryImplSP summary_sp) {
+void FormatCache::Entry::Set(lldb::TypeSummaryImplSP summary_sp) {
   m_summary_cached = true;
   m_summary_sp = summary_sp;
 }
 
-void FormatCache::Entry::SetSynthetic(lldb::SyntheticChildrenSP synthetic_sp) {
+void FormatCache::Entry::Set(lldb::SyntheticChildrenSP synthetic_sp) {
   m_synthetic_cached = true;
   m_synthetic_sp = synthetic_sp;
 }
 
-void FormatCache::Entry::SetValidator(lldb::TypeValidatorImplSP validator_sp) {
+void FormatCache::Entry::Set(lldb::TypeValidatorImplSP validator_sp) {
   m_validator_cached = true;
   m_validator_sp = validator_sp;
 }
@@ -117,100 +80,72 @@ FormatCache::Entry &FormatCache::GetEntry(ConstString type) {
   return m_map[type];
 }
 
-bool FormatCache::GetFormat(ConstString type,
-                            lldb::TypeFormatImplSP &format_sp) {
-  std::lock_guard<std::recursive_mutex> guard(m_mutex);
-  auto entry = GetEntry(type);
-  if (entry.IsFormatCached()) {
-#ifdef LLDB_CONFIGURATION_DEBUG
-    m_cache_hits++;
-#endif
-    format_sp = entry.GetFormat();
-    return true;
-  }
-#ifdef LLDB_CONFIGURATION_DEBUG
-  m_cache_misses++;
-#endif
-  format_sp.reset();
-  return false;
+template<> bool FormatCache::Entry::IsCached<lldb::TypeFormatImplSP>() {
+  return IsFormatCached();
 }
-
-bool FormatCache::GetSummary(ConstString type,
-                             lldb::TypeSummaryImplSP &summary_sp) {
-  std::lock_guard<std::recursive_mutex> guard(m_mutex);
-  auto entry = GetEntry(type);
-  if (entry.IsSummaryCached()) {
-#ifdef LLDB_CONFIGURATION_DEBUG
-    m_cache_hits++;
-#endif
-    summary_sp = entry.GetSummary();
-    return true;
-  }
-#ifdef LLDB_CONFIGURATION_DEBUG
-  m_cache_misses++;
-#endif
-  summary_sp.reset();
-  return false;
+template<> bool FormatCache::Entry::IsCached<lldb::TypeSummaryImplSP> () {
+  return IsSummaryCached();
 }
-
-bool FormatCache::GetSynthetic(ConstString type,
-                               lldb::SyntheticChildrenSP &synthetic_sp) {
-  std::lock_guard<std::recursive_mutex> guard(m_mutex);
-  auto entry = GetEntry(type);
-  if (entry.IsSyntheticCached()) {
-#ifdef LLDB_CONFIGURATION_DEBUG
-    m_cache_hits++;
-#endif
-    synthetic_sp = entry.GetSynthetic();
-    return true;
-  }
-#ifdef LLDB_CONFIGURATION_DEBUG
-  m_cache_misses++;
-#endif
-  synthetic_sp.reset();
-  return false;
+template<> bool FormatCache::Entry::IsCached<lldb::SyntheticChildrenSP>() {
+  return IsSyntheticCached();
+}
+template<> bool FormatCache::Entry::IsCached<lldb::TypeValidatorImplSP>() {
+  return IsValidatorCached();
 }
 
-bool FormatCache::GetValidator(ConstString type,
-                               lldb::TypeValidatorImplSP &validator_sp) {
+template <typename ImplSP>
+bool FormatCache::Get(ConstString type, ImplSP &format_impl_sp) {
   std::lock_guard<std::recursive_mutex> guard(m_mutex);
   auto entry = GetEntry(type);
-  if (entry.IsValidatorCached()) {
+  if (entry.IsCached<ImplSP>()) {
 #ifdef LLDB_CONFIGURATION_DEBUG
     m_cache_hits++;
 #endif
-    validator_sp = entry.GetValidator();
+    entry.Get(format_impl_sp);
     return true;
   }
 #ifdef LLDB_CONFIGURATION_DEBUG
   m_cache_misses++;
 #endif
-  validator_sp.reset();
+  format_impl_sp.reset();
   return false;
 }
 
-void FormatCache::SetFormat(ConstString type,
-                            lldb::TypeFormatImplSP &format_sp) {
+/// Explicit instantiations for the four types.
+/// \{
+template bool
+FormatCache::Get<lldb::TypeValidatorImplSP>(ConstString,
+                                            lldb::TypeValidatorImplSP &);
+template bool
+FormatCache::Get<lldb::TypeFormatImplSP>(ConstString, lldb::TypeFormatImplSP &);
+template bool
+FormatCache::Get<lldb::TypeSummaryImplSP>(ConstString,
+                                          lldb::TypeSummaryImplSP &);
+template bool
+FormatCache::Get<lldb::SyntheticChildrenSP>(ConstString,
+                                            lldb::SyntheticChildrenSP &);
+/// \}
+
+void FormatCache::Set(ConstString type, lldb::TypeFormatImplSP &format_sp) {
   std::lock_guard<std::recursive_mutex> guard(m_mutex);
-  GetEntry(type).SetFormat(format_sp);
+  GetEntry(type).Set(format_sp);
 }
 
-void FormatCache::SetSummary(ConstString type,
-                             lldb::TypeSummaryImplSP &summary_sp) {
+void FormatCache::Set(ConstString type, lldb::TypeSummaryImplSP &summary_sp) {
   std::lock_guard<std::recursive_mutex> guard(m_mutex);
-  GetEntry(type).SetSummary(summary_sp);
+  GetEntry(type).Set(summary_sp);
 }
 
-void FormatCache::SetSynthetic(ConstString type,
-                               lldb::SyntheticChildrenSP &synthetic_sp) {
+void FormatCache::Set(ConstString type,
+                      lldb::SyntheticChildrenSP &synthetic_sp) {
   std::lock_guard<std::recursive_mutex> guard(m_mutex);
-  GetEntry(type).SetSynthetic(synthetic_sp);
+  GetEntry(type).Set(synthetic_sp);
 }
 
-void FormatCache::SetValidator(ConstString type,
-                               lldb::TypeValidatorImplSP &validator_sp) {
+void FormatCache::Set(ConstString type,
+                      lldb::TypeValidatorImplSP &validator_sp) {
   std::lock_guard<std::recursive_mutex> guard(m_mutex);
-  GetEntry(type).SetValidator(validator_sp);
+  GetEntry(type).Set(validator_sp);
 }
 
 void FormatCache::Clear() {

diff  --git a/lldb/source/DataFormatters/FormatManager.cpp b/lldb/source/DataFormatters/FormatManager.cpp
index 8ae9bcc1c1cd..c8ddbd455943 100644
--- a/lldb/source/DataFormatters/FormatManager.cpp
+++ b/lldb/source/DataFormatters/FormatManager.cpp
@@ -610,302 +610,93 @@ FormatManager::GetCategoryForLanguage(lldb::LanguageType lang_type) {
   return lang_category;
 }
 
-lldb::TypeFormatImplSP
-FormatManager::GetHardcodedFormat(FormattersMatchData &match_data) {
-  TypeFormatImplSP retval_sp;
-
+template <typename ImplSP>
+ImplSP FormatManager::GetHardcoded(FormattersMatchData &match_data) {
+  ImplSP retval_sp;
   for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) {
     if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) {
       if (lang_category->GetHardcoded(*this, match_data, retval_sp))
-        break;
+        return retval_sp;
     }
   }
-
   return retval_sp;
 }
 
-lldb::TypeFormatImplSP
-FormatManager::GetFormat(ValueObject &valobj,
+template <typename ImplSP> ImplSP
+FormatManager::GetCached(ValueObject &valobj,
                          lldb::DynamicValueType use_dynamic) {
+  ImplSP retval_sp;
   FormattersMatchData match_data(valobj, use_dynamic);
-
-  TypeFormatImplSP retval;
   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS));
   if (match_data.GetTypeForCache()) {
-    LLDB_LOGF(log,
-              "\n\n[FormatManager::GetFormat] Looking into cache for type %s",
+    LLDB_LOGF(log, "\n\n[%s] Looking into cache for type %s", __FUNCTION__,
               match_data.GetTypeForCache().AsCString("<invalid>"));
-    if (m_format_cache.GetFormat(match_data.GetTypeForCache(), retval)) {
+    if (m_format_cache.Get(match_data.GetTypeForCache(), retval_sp)) {
       if (log) {
-        LLDB_LOGF(
-            log, "[FormatManager::GetFormat] Cache search success. Returning.");
+        LLDB_LOGF(log, "[%s] Cache search success. Returning.", __FUNCTION__);
         LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}",
                   m_format_cache.GetCacheHits(),
                   m_format_cache.GetCacheMisses());
       }
-      return retval;
+      return retval_sp;
     }
-    LLDB_LOGF(
-        log,
-        "[FormatManager::GetFormat] Cache search failed. Going normal route");
+    LLDB_LOGF(log, "[%s] Cache search failed. Going normal route",
+              __FUNCTION__);
   }
 
-  retval = m_categories_map.GetFormat(match_data);
-  if (!retval) {
-    LLDB_LOGF(log,
-              "[FormatManager::GetFormat] Search failed. Giving language a "
-              "chance.");
+  m_categories_map.Get(match_data, retval_sp);
+  if (!retval_sp) {
+    LLDB_LOGF(log, "[%s] Search failed. Giving language a chance.",
+              __FUNCTION__);
     for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) {
       if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) {
-        if (lang_category->Get(match_data, retval))
+        if (lang_category->Get(match_data, retval_sp))
           break;
       }
     }
-    if (retval) {
-      LLDB_LOGF(
-          log,
-          "[FormatManager::GetFormat] Language search success. Returning.");
-      return retval;
+    if (retval_sp) {
+      LLDB_LOGF(log, "[%s] Language search success. Returning.", __FUNCTION__);
+      return retval_sp;
     }
   }
-  if (!retval) {
-    LLDB_LOGF(log, "[FormatManager::GetFormat] Search failed. Giving hardcoded "
-                   "a chance.");
-    retval = GetHardcodedFormat(match_data);
+  if (!retval_sp) {
+    LLDB_LOGF(log, "[%s] Search failed. Giving hardcoded a chance.", __FUNCTION__);
+    retval_sp = GetHardcoded<ImplSP>(match_data);
   }
 
-  if (match_data.GetTypeForCache() && (!retval || !retval->NonCacheable())) {
-    LLDB_LOGF(log, "[FormatManager::GetFormat] Caching %p for type %s",
-              static_cast<void *>(retval.get()),
+  if (match_data.GetTypeForCache() && (!retval_sp || !retval_sp->NonCacheable())) {
+    LLDB_LOGF(log, "[%s] Caching %p for type %s", __FUNCTION__,
+              static_cast<void *>(retval_sp.get()),
               match_data.GetTypeForCache().AsCString("<invalid>"));
-    m_format_cache.SetFormat(match_data.GetTypeForCache(), retval);
+    m_format_cache.Set(match_data.GetTypeForCache(), retval_sp);
   }
   LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}",
             m_format_cache.GetCacheHits(), m_format_cache.GetCacheMisses());
-  return retval;
+  return retval_sp;
 }
 
-lldb::TypeSummaryImplSP
-FormatManager::GetHardcodedSummaryFormat(FormattersMatchData &match_data) {
-  TypeSummaryImplSP retval_sp;
-
-  for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) {
-    if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) {
-      if (lang_category->GetHardcoded(*this, match_data, retval_sp))
-        break;
-    }
-  }
-
-  return retval_sp;
+lldb::TypeFormatImplSP
+FormatManager::GetFormat(ValueObject &valobj,
+                         lldb::DynamicValueType use_dynamic) {
+  return GetCached<lldb::TypeFormatImplSP>(valobj, use_dynamic);
 }
 
 lldb::TypeSummaryImplSP
 FormatManager::GetSummaryFormat(ValueObject &valobj,
                                 lldb::DynamicValueType use_dynamic) {
-  FormattersMatchData match_data(valobj, use_dynamic);
-
-  TypeSummaryImplSP retval;
-  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS));
-  if (match_data.GetTypeForCache()) {
-    LLDB_LOGF(log,
-              "\n\n[FormatManager::GetSummaryFormat] Looking into cache "
-              "for type %s",
-              match_data.GetTypeForCache().AsCString("<invalid>"));
-    if (m_format_cache.GetSummary(match_data.GetTypeForCache(), retval)) {
-      if (log) {
-        LLDB_LOGF(log,
-                  "[FormatManager::GetSummaryFormat] Cache search success. "
-                  "Returning.");
-        LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}",
-                  m_format_cache.GetCacheHits(),
-                  m_format_cache.GetCacheMisses());
-      }
-      return retval;
-    }
-    LLDB_LOGF(log, "[FormatManager::GetSummaryFormat] Cache search failed. "
-                   "Going normal route");
-  }
-
-  retval = m_categories_map.GetSummaryFormat(match_data);
-  if (!retval) {
-    LLDB_LOGF(log, "[FormatManager::GetSummaryFormat] Search failed. Giving "
-                   "language a chance.");
-    for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) {
-      if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) {
-        if (lang_category->Get(match_data, retval))
-          break;
-      }
-    }
-    if (retval) {
-      LLDB_LOGF(log, "[FormatManager::GetSummaryFormat] Language search "
-                     "success. Returning.");
-      return retval;
-    }
-  }
-  if (!retval) {
-    LLDB_LOGF(log, "[FormatManager::GetSummaryFormat] Search failed. Giving "
-                   "hardcoded a chance.");
-    retval = GetHardcodedSummaryFormat(match_data);
-  }
-
-  if (match_data.GetTypeForCache() && (!retval || !retval->NonCacheable())) {
-    LLDB_LOGF(log, "[FormatManager::GetSummaryFormat] Caching %p for type %s",
-              static_cast<void *>(retval.get()),
-              match_data.GetTypeForCache().AsCString("<invalid>"));
-    m_format_cache.SetSummary(match_data.GetTypeForCache(), retval);
-  }
-  LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}",
-            m_format_cache.GetCacheHits(), m_format_cache.GetCacheMisses());
-  return retval;
-}
-
-lldb::SyntheticChildrenSP
-FormatManager::GetHardcodedSyntheticChildren(FormattersMatchData &match_data) {
-  SyntheticChildrenSP retval_sp;
-
-  for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) {
-    if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) {
-      if (lang_category->GetHardcoded(*this, match_data, retval_sp))
-        break;
-    }
-  }
-
-  return retval_sp;
+  return GetCached<lldb::TypeSummaryImplSP>(valobj, use_dynamic);
 }
 
 lldb::SyntheticChildrenSP
 FormatManager::GetSyntheticChildren(ValueObject &valobj,
                                     lldb::DynamicValueType use_dynamic) {
-  FormattersMatchData match_data(valobj, use_dynamic);
-
-  SyntheticChildrenSP retval;
-  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS));
-  if (match_data.GetTypeForCache()) {
-    LLDB_LOGF(log,
-              "\n\n[FormatManager::GetSyntheticChildren] Looking into "
-              "cache for type %s",
-              match_data.GetTypeForCache().AsCString("<invalid>"));
-    if (m_format_cache.GetSynthetic(match_data.GetTypeForCache(), retval)) {
-      if (log) {
-        LLDB_LOGF(log, "[FormatManager::GetSyntheticChildren] Cache search "
-                       "success. Returning.");
-        LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}",
-                  m_format_cache.GetCacheHits(),
-                  m_format_cache.GetCacheMisses());
-      }
-      return retval;
-    }
-    LLDB_LOGF(log, "[FormatManager::GetSyntheticChildren] Cache search failed. "
-                   "Going normal route");
-  }
-
-  retval = m_categories_map.GetSyntheticChildren(match_data);
-  if (!retval) {
-    LLDB_LOGF(log,
-              "[FormatManager::GetSyntheticChildren] Search failed. Giving "
-              "language a chance.");
-    for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) {
-      if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) {
-        if (lang_category->Get(match_data, retval))
-          break;
-      }
-    }
-    if (retval) {
-      LLDB_LOGF(log, "[FormatManager::GetSyntheticChildren] Language search "
-                     "success. Returning.");
-      return retval;
-    }
-  }
-  if (!retval) {
-    LLDB_LOGF(log,
-              "[FormatManager::GetSyntheticChildren] Search failed. Giving "
-              "hardcoded a chance.");
-    retval = GetHardcodedSyntheticChildren(match_data);
-  }
-
-  if (match_data.GetTypeForCache() && (!retval || !retval->NonCacheable())) {
-    LLDB_LOGF(log,
-              "[FormatManager::GetSyntheticChildren] Caching %p for type %s",
-              static_cast<void *>(retval.get()),
-              match_data.GetTypeForCache().AsCString("<invalid>"));
-    m_format_cache.SetSynthetic(match_data.GetTypeForCache(), retval);
-  }
-  LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}",
-            m_format_cache.GetCacheHits(), m_format_cache.GetCacheMisses());
-  return retval;
+  return GetCached<lldb::SyntheticChildrenSP>(valobj, use_dynamic);
 }
 
 lldb::TypeValidatorImplSP
 FormatManager::GetValidator(ValueObject &valobj,
                             lldb::DynamicValueType use_dynamic) {
-  FormattersMatchData match_data(valobj, use_dynamic);
-
-  TypeValidatorImplSP retval;
-  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS));
-  if (match_data.GetTypeForCache()) {
-    LLDB_LOGF(
-        log, "\n\n[FormatManager::GetValidator] Looking into cache for type %s",
-        match_data.GetTypeForCache().AsCString("<invalid>"));
-    if (m_format_cache.GetValidator(match_data.GetTypeForCache(), retval)) {
-      if (log) {
-        LLDB_LOGF(
-            log,
-            "[FormatManager::GetValidator] Cache search success. Returning.");
-        LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}",
-                  m_format_cache.GetCacheHits(),
-                  m_format_cache.GetCacheMisses());
-      }
-      return retval;
-    }
-    LLDB_LOGF(log, "[FormatManager::GetValidator] Cache search failed. Going "
-                   "normal route");
-  }
-
-  retval = m_categories_map.GetValidator(match_data);
-  if (!retval) {
-    LLDB_LOGF(log, "[FormatManager::GetValidator] Search failed. Giving "
-                   "language a chance.");
-    for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) {
-      if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) {
-        if (lang_category->Get(match_data, retval))
-          break;
-      }
-    }
-    if (retval) {
-      LLDB_LOGF(log, "[FormatManager::GetValidator] Language search success. "
-                     "Returning.");
-      return retval;
-    }
-  }
-  if (!retval) {
-    LLDB_LOGF(log, "[FormatManager::GetValidator] Search failed. Giving "
-                   "hardcoded a chance.");
-    retval = GetHardcodedValidator(match_data);
-  }
-
-  if (match_data.GetTypeForCache() && (!retval || !retval->NonCacheable())) {
-    LLDB_LOGF(log, "[FormatManager::GetValidator] Caching %p for type %s",
-              static_cast<void *>(retval.get()),
-              match_data.GetTypeForCache().AsCString("<invalid>"));
-    m_format_cache.SetValidator(match_data.GetTypeForCache(), retval);
-  }
-  LLDB_LOGV(log, "Cache hits: {0} - Cache Misses: {1}",
-            m_format_cache.GetCacheHits(), m_format_cache.GetCacheMisses());
-  return retval;
-}
-
-lldb::TypeValidatorImplSP
-FormatManager::GetHardcodedValidator(FormattersMatchData &match_data) {
-  TypeValidatorImplSP retval_sp;
-
-  for (lldb::LanguageType lang_type : match_data.GetCandidateLanguages()) {
-    if (LanguageCategory *lang_category = GetCategoryForLanguage(lang_type)) {
-      if (lang_category->GetHardcoded(*this, match_data, retval_sp))
-        break;
-    }
-  }
-
-  return retval_sp;
+  return GetCached<lldb::TypeValidatorImplSP>(valobj, use_dynamic);
 }
 
 FormatManager::FormatManager()

diff  --git a/lldb/source/DataFormatters/LanguageCategory.cpp b/lldb/source/DataFormatters/LanguageCategory.cpp
index 969b02556548..64a891804c57 100644
--- a/lldb/source/DataFormatters/LanguageCategory.cpp
+++ b/lldb/source/DataFormatters/LanguageCategory.cpp
@@ -43,7 +43,7 @@ bool LanguageCategory::Get(FormattersMatchData &match_data,
     return false;
 
   if (match_data.GetTypeForCache()) {
-    if (m_format_cache.GetFormat(match_data.GetTypeForCache(), format_sp))
+    if (m_format_cache.Get(match_data.GetTypeForCache(), format_sp))
       return format_sp.get() != nullptr;
   }
 
@@ -52,7 +52,7 @@ bool LanguageCategory::Get(FormattersMatchData &match_data,
       m_category_sp->Get(valobj, match_data.GetMatchesVector(), format_sp);
   if (match_data.GetTypeForCache() &&
       (!format_sp || !format_sp->NonCacheable())) {
-    m_format_cache.SetFormat(match_data.GetTypeForCache(), format_sp);
+    m_format_cache.Set(match_data.GetTypeForCache(), format_sp);
   }
   return result;
 }
@@ -66,7 +66,7 @@ bool LanguageCategory::Get(FormattersMatchData &match_data,
     return false;
 
   if (match_data.GetTypeForCache()) {
-    if (m_format_cache.GetSummary(match_data.GetTypeForCache(), format_sp))
+    if (m_format_cache.Get(match_data.GetTypeForCache(), format_sp))
       return format_sp.get() != nullptr;
   }
 
@@ -75,7 +75,7 @@ bool LanguageCategory::Get(FormattersMatchData &match_data,
       m_category_sp->Get(valobj, match_data.GetMatchesVector(), format_sp);
   if (match_data.GetTypeForCache() &&
       (!format_sp || !format_sp->NonCacheable())) {
-    m_format_cache.SetSummary(match_data.GetTypeForCache(), format_sp);
+    m_format_cache.Set(match_data.GetTypeForCache(), format_sp);
   }
   return result;
 }
@@ -89,7 +89,7 @@ bool LanguageCategory::Get(FormattersMatchData &match_data,
     return false;
 
   if (match_data.GetTypeForCache()) {
-    if (m_format_cache.GetSynthetic(match_data.GetTypeForCache(), format_sp))
+    if (m_format_cache.Get(match_data.GetTypeForCache(), format_sp))
       return format_sp.get() != nullptr;
   }
 
@@ -98,7 +98,7 @@ bool LanguageCategory::Get(FormattersMatchData &match_data,
       m_category_sp->Get(valobj, match_data.GetMatchesVector(), format_sp);
   if (match_data.GetTypeForCache() &&
       (!format_sp || !format_sp->NonCacheable())) {
-    m_format_cache.SetSynthetic(match_data.GetTypeForCache(), format_sp);
+    m_format_cache.Set(match_data.GetTypeForCache(), format_sp);
   }
   return result;
 }
@@ -112,7 +112,7 @@ bool LanguageCategory::Get(FormattersMatchData &match_data,
     return false;
 
   if (match_data.GetTypeForCache()) {
-    if (m_format_cache.GetValidator(match_data.GetTypeForCache(), format_sp))
+    if (m_format_cache.Get(match_data.GetTypeForCache(), format_sp))
       return format_sp.get() != nullptr;
   }
 
@@ -121,7 +121,7 @@ bool LanguageCategory::Get(FormattersMatchData &match_data,
       m_category_sp->Get(valobj, match_data.GetMatchesVector(), format_sp);
   if (match_data.GetTypeForCache() &&
       (!format_sp || !format_sp->NonCacheable())) {
-    m_format_cache.SetValidator(match_data.GetTypeForCache(), format_sp);
+    m_format_cache.Set(match_data.GetTypeForCache(), format_sp);
   }
   return result;
 }
@@ -141,7 +141,7 @@ bool LanguageCategory::GetHardcoded(FormatManager &fmt_mgr,
   }
   if (match_data.GetTypeForCache() &&
       (!format_sp || !format_sp->NonCacheable())) {
-    m_format_cache.SetFormat(match_data.GetTypeForCache(), format_sp);
+    m_format_cache.Set(match_data.GetTypeForCache(), format_sp);
   }
   return format_sp.get() != nullptr;
 }
@@ -161,7 +161,7 @@ bool LanguageCategory::GetHardcoded(FormatManager &fmt_mgr,
   }
   if (match_data.GetTypeForCache() &&
       (!format_sp || !format_sp->NonCacheable())) {
-    m_format_cache.SetSummary(match_data.GetTypeForCache(), format_sp);
+    m_format_cache.Set(match_data.GetTypeForCache(), format_sp);
   }
   return format_sp.get() != nullptr;
 }
@@ -181,7 +181,7 @@ bool LanguageCategory::GetHardcoded(FormatManager &fmt_mgr,
   }
   if (match_data.GetTypeForCache() &&
       (!format_sp || !format_sp->NonCacheable())) {
-    m_format_cache.SetSynthetic(match_data.GetTypeForCache(), format_sp);
+    m_format_cache.Set(match_data.GetTypeForCache(), format_sp);
   }
   return format_sp.get() != nullptr;
 }
@@ -201,7 +201,7 @@ bool LanguageCategory::GetHardcoded(FormatManager &fmt_mgr,
   }
   if (match_data.GetTypeForCache() &&
       (!format_sp || !format_sp->NonCacheable())) {
-    m_format_cache.SetValidator(match_data.GetTypeForCache(), format_sp);
+    m_format_cache.Set(match_data.GetTypeForCache(), format_sp);
   }
   return format_sp.get() != nullptr;
 }

diff  --git a/lldb/source/DataFormatters/TypeCategoryMap.cpp b/lldb/source/DataFormatters/TypeCategoryMap.cpp
index b1075e9878d8..08f9bc3f1a4d 100644
--- a/lldb/source/DataFormatters/TypeCategoryMap.cpp
+++ b/lldb/source/DataFormatters/TypeCategoryMap.cpp
@@ -169,8 +169,8 @@ bool TypeCategoryMap::AnyMatches(
   return false;
 }
 
-lldb::TypeFormatImplSP
-TypeCategoryMap::GetFormat(FormattersMatchData &match_data) {
+template <typename ImplSP>
+void TypeCategoryMap::Get(FormattersMatchData &match_data, ImplSP &retval) {
   std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
 
   uint32_t reason_why;
@@ -182,8 +182,8 @@ TypeCategoryMap::GetFormat(FormattersMatchData &match_data) {
     for (auto match : match_data.GetMatchesVector()) {
       LLDB_LOGF(
           log,
-          "[CategoryMap::GetFormat] candidate match = %s %s %s %s reason = "
-          "%" PRIu32,
+          "[%s] candidate match = %s %s %s %s reason = %" PRIu32,
+          __FUNCTION__,
           match.GetTypeName().GetCString(),
           match.DidStripPointer() ? "strip-pointers" : "no-strip-pointers",
           match.DidStripReference() ? "strip-reference" : "no-strip-reference",
@@ -194,140 +194,34 @@ TypeCategoryMap::GetFormat(FormattersMatchData &match_data) {
 
   for (begin = m_active_categories.begin(); begin != end; begin++) {
     lldb::TypeCategoryImplSP category_sp = *begin;
-    lldb::TypeFormatImplSP current_format;
-    LLDB_LOGF(log, "[TypeCategoryMap::GetFormat] Trying to use category %s",
+    ImplSP current_format;
+    LLDB_LOGF(log, "[%s] Trying to use category %s", __FUNCTION__,
               category_sp->GetName());
     if (!category_sp->Get(match_data.GetValueObject(),
                           match_data.GetMatchesVector(), current_format,
                           &reason_why))
       continue;
-    return current_format;
-  }
-  LLDB_LOGF(log,
-            "[TypeCategoryMap::GetFormat] nothing found - returning empty SP");
-  return lldb::TypeFormatImplSP();
-}
-
-lldb::TypeSummaryImplSP
-TypeCategoryMap::GetSummaryFormat(FormattersMatchData &match_data) {
-  std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
-
-  uint32_t reason_why;
-  ActiveCategoriesIterator begin, end = m_active_categories.end();
-
-  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS));
-
-  if (log) {
-    for (auto match : match_data.GetMatchesVector()) {
-      LLDB_LOGF(
-          log,
-          "[CategoryMap::GetSummaryFormat] candidate match = %s %s %s %s "
-          "reason = %" PRIu32,
-          match.GetTypeName().GetCString(),
-          match.DidStripPointer() ? "strip-pointers" : "no-strip-pointers",
-          match.DidStripReference() ? "strip-reference" : "no-strip-reference",
-          match.DidStripTypedef() ? "strip-typedef" : "no-strip-typedef",
-          match.GetReason());
-    }
-  }
-
-  for (begin = m_active_categories.begin(); begin != end; begin++) {
-    lldb::TypeCategoryImplSP category_sp = *begin;
-    lldb::TypeSummaryImplSP current_format;
-    LLDB_LOGF(log, "[CategoryMap::GetSummaryFormat] Trying to use category %s",
-              category_sp->GetName());
-    if (!category_sp->Get(match_data.GetValueObject(),
-                          match_data.GetMatchesVector(), current_format,
-                          &reason_why))
-      continue;
-    return current_format;
-  }
-  LLDB_LOGF(
-      log,
-      "[CategoryMap::GetSummaryFormat] nothing found - returning empty SP");
-  return lldb::TypeSummaryImplSP();
-}
-
-lldb::SyntheticChildrenSP
-TypeCategoryMap::GetSyntheticChildren(FormattersMatchData &match_data) {
-  std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
-
-  uint32_t reason_why;
-
-  ActiveCategoriesIterator begin, end = m_active_categories.end();
-
-  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS));
-
-  if (log) {
-    for (auto match : match_data.GetMatchesVector()) {
-      LLDB_LOGF(
-          log,
-          "[CategoryMap::GetSyntheticChildren] candidate match = %s %s %s %s "
-          "reason = %" PRIu32,
-          match.GetTypeName().GetCString(),
-          match.DidStripPointer() ? "strip-pointers" : "no-strip-pointers",
-          match.DidStripReference() ? "strip-reference" : "no-strip-reference",
-          match.DidStripTypedef() ? "strip-typedef" : "no-strip-typedef",
-          match.GetReason());
-    }
-  }
 
-  for (begin = m_active_categories.begin(); begin != end; begin++) {
-    lldb::TypeCategoryImplSP category_sp = *begin;
-    lldb::SyntheticChildrenSP current_format;
-    LLDB_LOGF(log,
-              "[CategoryMap::GetSyntheticChildren] Trying to use category %s",
-              category_sp->GetName());
-    if (!category_sp->Get(match_data.GetValueObject(),
-                          match_data.GetMatchesVector(), current_format,
-                          &reason_why))
-      continue;
-    return current_format;
+    retval = std::move(current_format);
+    return;
   }
-  LLDB_LOGF(log,
-            "[CategoryMap::GetSyntheticChildren] nothing found - returning "
-            "empty SP");
-  return lldb::SyntheticChildrenSP();
+  LLDB_LOGF(log, "[%s] nothing found - returning empty SP", __FUNCTION__);
 }
 
-lldb::TypeValidatorImplSP
-TypeCategoryMap::GetValidator(FormattersMatchData &match_data) {
-  std::lock_guard<std::recursive_mutex> guard(m_map_mutex);
-
-  uint32_t reason_why;
-  ActiveCategoriesIterator begin, end = m_active_categories.end();
-
-  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS));
-
-  if (log) {
-    for (auto match : match_data.GetMatchesVector()) {
-      LLDB_LOGF(
-          log,
-          "[CategoryMap::GetValidator] candidate match = %s %s %s %s reason = "
-          "%" PRIu32,
-          match.GetTypeName().GetCString(),
-          match.DidStripPointer() ? "strip-pointers" : "no-strip-pointers",
-          match.DidStripReference() ? "strip-reference" : "no-strip-reference",
-          match.DidStripTypedef() ? "strip-typedef" : "no-strip-typedef",
-          match.GetReason());
-    }
-  }
-
-  for (begin = m_active_categories.begin(); begin != end; begin++) {
-    lldb::TypeCategoryImplSP category_sp = *begin;
-    lldb::TypeValidatorImplSP current_format;
-    LLDB_LOGF(log, "[CategoryMap::GetValidator] Trying to use category %s",
-              category_sp->GetName());
-    if (!category_sp->Get(match_data.GetValueObject(),
-                          match_data.GetMatchesVector(), current_format,
-                          &reason_why))
-      continue;
-    return current_format;
-  }
-  LLDB_LOGF(log,
-            "[CategoryMap::GetValidator] nothing found - returning empty SP");
-  return lldb::TypeValidatorImplSP();
-}
+/// Explicit instantiations for the four types.
+/// \{
+template void TypeCategoryMap::Get<lldb::TypeValidatorImplSP>(
+    FormattersMatchData &match_data, lldb::TypeValidatorImplSP &retval);
+
+template void
+TypeCategoryMap::Get<lldb::TypeFormatImplSP>(FormattersMatchData &match_data,
+                                             lldb::TypeFormatImplSP &retval);
+template void
+TypeCategoryMap::Get<lldb::TypeSummaryImplSP>(FormattersMatchData &match_data,
+                                              lldb::TypeSummaryImplSP &retval);
+template void TypeCategoryMap::Get<lldb::SyntheticChildrenSP>(
+    FormattersMatchData &match_data, lldb::SyntheticChildrenSP &retval);
+/// \}
 
 void TypeCategoryMap::ForEach(ForEachCallback callback) {
   if (callback) {


        


More information about the lldb-commits mailing list