diff --git a/lldb/include/lldb/DataFormatters/FormatManager.h b/lldb/include/lldb/DataFormatters/FormatManager.h index 98c5b132c203..978ad148d6c4 100644 --- a/lldb/include/lldb/DataFormatters/FormatManager.h +++ b/lldb/include/lldb/DataFormatters/FormatManager.h @@ -34,7 +34,7 @@ namespace lldb_private { // this file's objects directly class FormatManager : public IFormatChangeListener { - typedef FormatMap NamedSummariesMap; + typedef FormattersContainer NamedSummariesMap; typedef TypeCategoryMap::MapType::iterator CategoryMapIterator; public: diff --git a/lldb/include/lldb/DataFormatters/FormattersContainer.h b/lldb/include/lldb/DataFormatters/FormattersContainer.h index d7c531e9618a..a30ad920ef45 100644 --- a/lldb/include/lldb/DataFormatters/FormattersContainer.h +++ b/lldb/include/lldb/DataFormatters/FormattersContainer.h @@ -104,18 +104,18 @@ public: } }; -template class FormattersContainer; - -template class FormatMap { +template class FormattersContainer { public: - typedef typename ValueType::SharedPointer ValueSP; + typedef typename std::shared_ptr ValueSP; typedef std::vector> MapType; - typedef typename MapType::iterator MapIterator; typedef std::function ForEachCallback; + typedef typename std::shared_ptr> + SharedPointer; - FormatMap(IFormatChangeListener *lst) - : m_map(), m_map_mutex(), listener(lst) {} + friend class TypeCategoryImpl; + + FormattersContainer(IFormatChangeListener *lst) : listener(lst) {} void Add(TypeMatcher matcher, const ValueSP &entry) { if (listener) @@ -130,9 +130,9 @@ public: listener->Changed(); } - bool Delete(const TypeMatcher &matcher) { + bool Delete(TypeMatcher matcher) { std::lock_guard guard(m_map_mutex); - for (MapIterator iter = m_map.begin(); iter != m_map.end(); ++iter) + for (auto iter = m_map.begin(); iter != m_map.end(); ++iter) if (iter->first.CreatedBySameMatchString(matcher)) { m_map.erase(iter); if (listener) @@ -142,14 +142,18 @@ public: return false; } - void Clear() { + bool Get(ConstString type, ValueSP &entry) { std::lock_guard guard(m_map_mutex); - m_map.clear(); - if (listener) - listener->Changed(); + for (auto &formatter : llvm::reverse(m_map)) { + if (formatter.first.Matches(type)) { + entry = formatter.second; + return true; + } + } + return false; } - bool Get(const TypeMatcher &matcher, ValueSP &entry) { + bool GetExact(TypeMatcher matcher, ValueSP &entry) { std::lock_guard guard(m_map_mutex); for (const auto &pos : m_map) if (pos.first.CreatedBySameMatchString(matcher)) { @@ -159,6 +163,29 @@ public: return false; } + ValueSP GetAtIndex(size_t index) { + std::lock_guard guard(m_map_mutex); + if (index >= m_map.size()) + return ValueSP(); + return m_map[index].second; + } + + lldb::TypeNameSpecifierImplSP GetTypeNameSpecifierAtIndex(size_t index) { + std::lock_guard guard(m_map_mutex); + if (index >= m_map.size()) + return lldb::TypeNameSpecifierImplSP(); + TypeMatcher type_matcher = m_map[index].first; + return std::make_shared( + type_matcher.GetMatchString().GetStringRef(), true); + } + + void Clear() { + std::lock_guard guard(m_map_mutex); + m_map.clear(); + if (listener) + listener->Changed(); + } + void ForEach(ForEachCallback callback) { if (callback) { std::lock_guard guard(m_map_mutex); @@ -170,97 +197,16 @@ public: } } - uint32_t GetCount() { return m_map.size(); } - - ValueSP GetValueAtIndex(size_t index) { + uint32_t GetCount() { std::lock_guard guard(m_map_mutex); - if (index >= m_map.size()) - return ValueSP(); - return m_map[index].second; - } - - // If caller holds the mutex we could return a reference without copy ctor. - llvm::Optional GetKeyAtIndex(size_t index) { - std::lock_guard guard(m_map_mutex); - if (index >= m_map.size()) - return llvm::None; - return m_map[index].first; + return m_map.size(); } protected: - MapType m_map; - std::recursive_mutex m_map_mutex; - IFormatChangeListener *listener; - - MapType &map() { return m_map; } - - std::recursive_mutex &mutex() { return m_map_mutex; } - - friend class FormattersContainer; - friend class FormatManager; -}; - -template class FormattersContainer { -protected: - typedef FormatMap BackEndType; - -public: - typedef std::shared_ptr MapValueType; - typedef typename BackEndType::ForEachCallback ForEachCallback; - typedef typename std::shared_ptr> - SharedPointer; - - friend class TypeCategoryImpl; - - FormattersContainer(IFormatChangeListener *lst) : m_format_map(lst) {} - - void Add(TypeMatcher type, const MapValueType &entry) { - m_format_map.Add(std::move(type), entry); - } - - bool Delete(TypeMatcher type) { return m_format_map.Delete(type); } - - bool Get(ConstString type, MapValueType &entry) { - std::lock_guard guard(m_format_map.mutex()); - for (auto &formatter : llvm::reverse(m_format_map.map())) { - if (formatter.first.Matches(type)) { - entry = formatter.second; - return true; - } - } - return false; - } - - bool GetExact(ConstString type, MapValueType &entry) { - return m_format_map.Get(type, entry); - } - - MapValueType GetAtIndex(size_t index) { - return m_format_map.GetValueAtIndex(index); - } - - lldb::TypeNameSpecifierImplSP GetTypeNameSpecifierAtIndex(size_t index) { - llvm::Optional type_matcher = - m_format_map.GetKeyAtIndex(index); - if (!type_matcher) - return lldb::TypeNameSpecifierImplSP(); - return lldb::TypeNameSpecifierImplSP(new TypeNameSpecifierImpl( - type_matcher->GetMatchString().GetStringRef(), true)); - } - - void Clear() { m_format_map.Clear(); } - - void ForEach(ForEachCallback callback) { m_format_map.ForEach(callback); } - - uint32_t GetCount() { return m_format_map.GetCount(); } - -protected: - BackEndType m_format_map; - FormattersContainer(const FormattersContainer &) = delete; const FormattersContainer &operator=(const FormattersContainer &) = delete; - bool Get(const FormattersMatchVector &candidates, MapValueType &entry) { + bool Get(const FormattersMatchVector &candidates, ValueSP &entry) { for (const FormattersMatchCandidate &candidate : candidates) { if (Get(candidate.GetTypeName(), entry)) { if (candidate.IsMatch(entry) == false) { @@ -273,6 +219,10 @@ protected: } return false; } + + MapType m_map; + std::recursive_mutex m_map_mutex; + IFormatChangeListener *listener; }; } // namespace lldb_private diff --git a/lldb/include/lldb/DataFormatters/TypeCategory.h b/lldb/include/lldb/DataFormatters/TypeCategory.h index 4c8a7e14be12..2662ffc5aeac 100644 --- a/lldb/include/lldb/DataFormatters/TypeCategory.h +++ b/lldb/include/lldb/DataFormatters/TypeCategory.h @@ -31,7 +31,7 @@ public: typedef TypeMatcher ExactMatchMap; typedef TypeMatcher RegexMatchMap; - typedef typename ExactMatchContainer::MapValueType MapValueType; + typedef typename ExactMatchContainer::ValueSP MapValueType; typedef typename ExactMatchContainer::SharedPointer ExactMatchContainerSP; typedef typename RegexMatchContainer::SharedPointer RegexMatchContainerSP; diff --git a/lldb/source/DataFormatters/DataVisualization.cpp b/lldb/source/DataFormatters/DataVisualization.cpp index 82248bb64285..ded8bbd90391 100644 --- a/lldb/source/DataFormatters/DataVisualization.cpp +++ b/lldb/source/DataFormatters/DataVisualization.cpp @@ -169,7 +169,7 @@ DataVisualization::Categories::GetCategoryAtIndex(size_t index) { bool DataVisualization::NamedSummaryFormats::GetSummaryFormat( ConstString type, lldb::TypeSummaryImplSP &entry) { - return GetFormatManager().GetNamedSummaryContainer().Get(type, entry); + return GetFormatManager().GetNamedSummaryContainer().GetExact(type, entry); } void DataVisualization::NamedSummaryFormats::Add(