diff --git a/lldb/include/lldb/DataFormatters/FormattersContainer.h b/lldb/include/lldb/DataFormatters/FormattersContainer.h index 079102810025d7..de2edb103151a4 100644 --- a/lldb/include/lldb/DataFormatters/FormattersContainer.h +++ b/lldb/include/lldb/DataFormatters/FormattersContainer.h @@ -65,7 +65,7 @@ template class FormattersContainer; template class FormatMap { public: typedef typename ValueType::SharedPointer ValueSP; - typedef std::map MapType; + typedef std::vector> MapType; typedef typename MapType::iterator MapIterator; typedef std::function ForEachCallback; @@ -79,20 +79,22 @@ template class FormatMap { entry->GetRevision() = 0; std::lock_guard guard(m_map_mutex); - m_map[std::move(name)] = entry; + Delete(name); + m_map.emplace_back(std::move(name), std::move(entry)); if (listener) listener->Changed(); } bool Delete(const KeyType &name) { std::lock_guard guard(m_map_mutex); - MapIterator iter = m_map.find(name); - if (iter == m_map.end()) - return false; - m_map.erase(iter); - if (listener) - listener->Changed(); - return true; + for (MapIterator iter = m_map.begin(); iter != m_map.end(); ++iter) + if (iter->first == name) { + m_map.erase(iter); + if (listener) + listener->Changed(); + return true; + } + return false; } void Clear() { @@ -104,11 +106,12 @@ template class FormatMap { bool Get(const KeyType &name, ValueSP &entry) { std::lock_guard guard(m_map_mutex); - MapIterator iter = m_map.find(name); - if (iter == m_map.end()) - return false; - entry = iter->second; - return true; + for (const auto &pos : m_map) + if (pos.first == name) { + entry = pos.second; + return true; + } + return false; } void ForEach(ForEachCallback callback) { @@ -126,29 +129,17 @@ template class FormatMap { ValueSP GetValueAtIndex(size_t index) { std::lock_guard guard(m_map_mutex); - MapIterator iter = m_map.begin(); - MapIterator end = m_map.end(); - while (index > 0) { - iter++; - index--; - if (end == iter) - return ValueSP(); - } - return iter->second; + 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. KeyType GetKeyAtIndex(size_t index) { std::lock_guard guard(m_map_mutex); - MapIterator iter = m_map.begin(); - MapIterator end = m_map.end(); - while (index > 0) { - iter++; - index--; - if (end == iter) - return KeyType(); - } - return iter->first; + if (index >= m_map.size()) + return {}; + return m_map[index].first; } protected: @@ -171,8 +162,8 @@ template class FormattersContainer { public: typedef typename BackEndType::MapType MapType; typedef typename MapType::iterator MapIterator; - typedef typename MapType::key_type MapKeyType; - typedef typename MapType::mapped_type MapValueType; + typedef KeyType MapKeyType; + typedef std::shared_ptr MapValueType; typedef typename BackEndType::ForEachCallback ForEachCallback; typedef typename std::shared_ptr> SharedPointer; @@ -294,7 +285,8 @@ template class FormattersContainer { RegularExpression *dummy) { llvm::StringRef key_str = key.GetStringRef(); std::lock_guard guard(m_format_map.mutex()); - for (const auto &pos : m_format_map.map()) { + // Patterns are matched in reverse-chronological order. + for (const auto &pos : llvm::reverse(m_format_map.map())) { const RegularExpression ®ex = pos.first; if (regex.Execute(key_str)) { value = pos.second; diff --git a/lldb/include/lldb/Utility/RegularExpression.h b/lldb/include/lldb/Utility/RegularExpression.h index ee2e2f5dbc3981..6acc203d8e7c08 100644 --- a/lldb/include/lldb/Utility/RegularExpression.h +++ b/lldb/include/lldb/Utility/RegularExpression.h @@ -78,10 +78,6 @@ class RegularExpression { /// otherwise. llvm::Error GetError() const; - bool operator<(const RegularExpression &rhs) const { - return GetText() < rhs.GetText(); - } - bool operator==(const RegularExpression &rhs) const { return GetText() == rhs.GetText(); } diff --git a/lldb/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-advanced/TestDataFormatterAdv.py b/lldb/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-advanced/TestDataFormatterAdv.py index 8615fe65dd5774..3390da4bf3fd9a 100644 --- a/lldb/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-advanced/TestDataFormatterAdv.py +++ b/lldb/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-advanced/TestDataFormatterAdv.py @@ -136,6 +136,13 @@ def cleanup(): self.expect("frame variable int_array", substrs=['1,2']) + # Test the patterns are matched in reverse-chronological order. + self.runCmd( + 'type summary add --summary-string \"${var[2-3]}\" "int []"') + + self.expect("frame variable int_array", + substrs=['3,4']) + self.runCmd("type summary clear") self.runCmd("type summary add -c -x \"i_am_cool \[[0-9]\]\"")