[Lldb-commits] [lldb] r253125 - The existing logic to loop over formatters is very pre-C++11, using void* batons, and function pointers, and raw memory allocations instead of safer more modern constructs

Enrico Granata via lldb-commits lldb-commits at lists.llvm.org
Fri Nov 13 21:44:24 PST 2015


Author: enrico
Date: Fri Nov 13 23:44:23 2015
New Revision: 253125

URL: http://llvm.org/viewvc/llvm-project?rev=253125&view=rev
Log:
The existing logic to loop over formatters is very pre-C++11, using void* batons, and function pointers, and raw memory allocations instead of safer more modern constructs

This is a first pass at a cleanup of that code, modernizing the "type X clear" commands, and providing the basic infrastructure I plan to use all over
More cleanup will come over the next few days


Modified:
    lldb/trunk/include/lldb/DataFormatters/DataVisualization.h
    lldb/trunk/include/lldb/DataFormatters/FormatManager.h
    lldb/trunk/include/lldb/DataFormatters/FormattersContainer.h
    lldb/trunk/include/lldb/DataFormatters/TypeCategory.h
    lldb/trunk/include/lldb/DataFormatters/TypeCategoryMap.h
    lldb/trunk/source/Commands/CommandObjectType.cpp
    lldb/trunk/source/DataFormatters/DataVisualization.cpp
    lldb/trunk/source/DataFormatters/FormatManager.cpp
    lldb/trunk/source/DataFormatters/TypeCategoryMap.cpp

Modified: lldb/trunk/include/lldb/DataFormatters/DataVisualization.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/DataFormatters/DataVisualization.h?rev=253125&r1=253124&r2=253125&view=diff
==============================================================================
--- lldb/trunk/include/lldb/DataFormatters/DataVisualization.h (original)
+++ lldb/trunk/include/lldb/DataFormatters/DataVisualization.h Fri Nov 13 23:44:23 2015
@@ -160,6 +160,9 @@ public:
         static void
         LoopThrough (FormatManager::CategoryCallback callback, void* callback_baton);
         
+        static void
+        ForEach (TypeCategoryMap::ForEachCallback callback);
+        
         static uint32_t
         GetCount ();
         

Modified: lldb/trunk/include/lldb/DataFormatters/FormatManager.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/DataFormatters/FormatManager.h?rev=253125&r1=253124&r2=253125&view=diff
==============================================================================
--- lldb/trunk/include/lldb/DataFormatters/FormatManager.h (original)
+++ lldb/trunk/include/lldb/DataFormatters/FormatManager.h Fri Nov 13 23:44:23 2015
@@ -142,6 +142,9 @@ public:
     void
     LoopThroughCategories (CategoryCallback callback, void* param);
 
+    void
+    ForEachCategory (TypeCategoryMap::ForEachCallback callback);
+    
     lldb::TypeCategoryImplSP
     GetCategory(const char* category_name = nullptr,
                 bool can_create = true)

Modified: lldb/trunk/include/lldb/DataFormatters/FormattersContainer.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/DataFormatters/FormattersContainer.h?rev=253125&r1=253124&r2=253125&view=diff
==============================================================================
--- lldb/trunk/include/lldb/DataFormatters/FormattersContainer.h (original)
+++ lldb/trunk/include/lldb/DataFormatters/FormattersContainer.h Fri Nov 13 23:44:23 2015
@@ -82,6 +82,8 @@ public:
     typedef typename MapType::iterator MapIterator;
     typedef std::function<bool(void*, KeyType, const ValueSP&)> CallbackType;
     
+    typedef std::function<bool(KeyType, const ValueSP&)> ForEachCallback;
+    
     FormatMap(IFormatChangeListener* lst) :
     m_map(),
     m_map_mutex(Mutex::eMutexTypeRecursive),
@@ -154,6 +156,22 @@ public:
         }
     }
     
+    void
+    ForEach (ForEachCallback callback)
+    {
+        if (callback)
+        {
+            Mutex::Locker locker(m_map_mutex);
+            MapIterator pos, end = m_map.end();
+            for (pos = m_map.begin(); pos != end; pos++)
+            {
+                KeyType type = pos->first;
+                if (!callback(type, pos->second))
+                    break;
+            }
+        }
+    }
+    
     uint32_t
     GetCount ()
     {
@@ -225,6 +243,7 @@ public:
     typedef typename MapType::key_type MapKeyType;
     typedef typename MapType::mapped_type MapValueType;
     typedef typename BackEndType::CallbackType CallbackType;
+    typedef typename BackEndType::ForEachCallback ForEachCallback;
     typedef typename std::shared_ptr<FormattersContainer<KeyType, ValueType> > SharedPointer;
     
     friend class TypeCategoryImpl;
@@ -302,6 +321,12 @@ public:
         m_format_map.LoopThrough(callback,param);
     }
     
+    void
+    ForEach (ForEachCallback callback)
+    {
+        m_format_map.ForEach(callback);
+    }
+    
     uint32_t
     GetCount ()
     {

Modified: lldb/trunk/include/lldb/DataFormatters/TypeCategory.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/DataFormatters/TypeCategory.h?rev=253125&r1=253124&r2=253125&view=diff
==============================================================================
--- lldb/trunk/include/lldb/DataFormatters/TypeCategory.h (original)
+++ lldb/trunk/include/lldb/DataFormatters/TypeCategory.h Fri Nov 13 23:44:23 2015
@@ -42,6 +42,9 @@ namespace lldb_private {
         typedef typename ExactMatchContainer::SharedPointer ExactMatchContainerSP;
         typedef typename RegexMatchContainer::SharedPointer RegexMatchContainerSP;
         
+        typedef typename ExactMatchContainer::ForEachCallback ExactMatchForEachCallback;
+        typedef typename RegexMatchContainer::ForEachCallback RegexMatchForEachCallback;
+        
         FormatterContainerPair (const char* exact_name,
                                 const char* regex_name,
                                 IFormatChangeListener* clist) :
@@ -115,10 +118,180 @@ namespace lldb_private {
         typedef ValidatorContainer::ExactMatchContainerSP ValidatorContainerSP;
         typedef ValidatorContainer::RegexMatchContainerSP RegexValidatorContainerSP;
         
+        class ForEach
+        {
+        public:
+            ForEach () = default;
+            ~ForEach () = default;
+            
+            ForEach&
+            SetFormatExactCallback (FormatContainer::ExactMatchForEachCallback callback)
+            {
+                m_format_exact = callback;
+                return *this;
+            }
+            ForEach&
+            SetFormatRegexCallback (FormatContainer::RegexMatchForEachCallback callback)
+            {
+                m_format_regex = callback;
+                return *this;
+            }
+
+            ForEach&
+            SetSummaryExactCallback (SummaryContainer::ExactMatchForEachCallback callback)
+            {
+                m_summary_exact = callback;
+                return *this;
+            }
+            ForEach&
+            SetSummaryRegexCallback (SummaryContainer::RegexMatchForEachCallback callback)
+            {
+                m_summary_regex = callback;
+                return *this;
+            }
+
+            ForEach&
+            SetFilterExactCallback (FilterContainer::ExactMatchForEachCallback callback)
+            {
+                m_filter_exact = callback;
+                return *this;
+            }
+            ForEach&
+            SetFilterRegexCallback (FilterContainer::RegexMatchForEachCallback callback)
+            {
+                m_filter_regex = callback;
+                return *this;
+            }
+
+#ifndef LLDB_DISABLE_PYTHON
+            ForEach&
+            SetSynthExactCallback (SynthContainer::ExactMatchForEachCallback callback)
+            {
+                m_synth_exact = callback;
+                return *this;
+            }
+            ForEach&
+            SetSynthRegexCallback (SynthContainer::RegexMatchForEachCallback callback)
+            {
+                m_synth_regex = callback;
+                return *this;
+            }
+#endif // LLDB_DISABLE_PYTHON
+
+            ForEach&
+            SetValidatorExactCallback (ValidatorContainer::ExactMatchForEachCallback callback)
+            {
+                m_validator_exact = callback;
+                return *this;
+            }
+            ForEach&
+            SetValidatorRegexCallback (ValidatorContainer::RegexMatchForEachCallback callback)
+            {
+                m_validator_regex = callback;
+                return *this;
+            }
+            
+            FormatContainer::ExactMatchForEachCallback
+            GetFormatExactCallback () const
+            {
+                return m_format_exact;
+            }
+            FormatContainer::RegexMatchForEachCallback
+            GetFormatRegexCallback () const
+            {
+                return m_format_regex;
+            }
+
+            SummaryContainer::ExactMatchForEachCallback
+            GetSummaryExactCallback () const
+            {
+                return m_summary_exact;
+            }
+            SummaryContainer::RegexMatchForEachCallback
+            GetSummaryRegexCallback () const
+            {
+                return m_summary_regex;
+            }
+
+            FilterContainer::ExactMatchForEachCallback
+            GetFilterExactCallback () const
+            {
+                return m_filter_exact;
+            }
+            FilterContainer::RegexMatchForEachCallback
+            GetFilterRegexCallback () const
+            {
+                return m_filter_regex;
+            }
+
+#ifndef LLDB_DISABLE_PYTHON
+            SynthContainer::ExactMatchForEachCallback
+            GetSynthExactCallback () const
+            {
+                return m_synth_exact;
+            }
+            SynthContainer::RegexMatchForEachCallback
+            GetSynthRegexCallback () const
+            {
+                return m_synth_regex;
+            }
+#endif // LLDB_DISABLE_PYTHON
+
+            ValidatorContainer::ExactMatchForEachCallback
+            GetValidatorExactCallback () const
+            {
+                return m_validator_exact;
+            }
+            ValidatorContainer::RegexMatchForEachCallback
+            GetValidatorRegexCallback () const
+            {
+                return m_validator_regex;
+            }
+            
+        private:
+            FormatContainer::ExactMatchForEachCallback m_format_exact;
+            FormatContainer::RegexMatchForEachCallback m_format_regex;
+
+            SummaryContainer::ExactMatchForEachCallback m_summary_exact;
+            SummaryContainer::RegexMatchForEachCallback m_summary_regex;
+            
+            FilterContainer::ExactMatchForEachCallback m_filter_exact;
+            FilterContainer::RegexMatchForEachCallback m_filter_regex;
+
+#ifndef LLDB_DISABLE_PYTHON
+            SynthContainer::ExactMatchForEachCallback m_synth_exact;
+            SynthContainer::RegexMatchForEachCallback m_synth_regex;
+#endif // LLDB_DISABLE_PYTHON
+
+            ValidatorContainer::ExactMatchForEachCallback m_validator_exact;
+            ValidatorContainer::RegexMatchForEachCallback m_validator_regex;
+        };
+        
         TypeCategoryImpl (IFormatChangeListener* clist,
                           ConstString name,
                           std::initializer_list<lldb::LanguageType> langs = {});
         
+        void
+        ForEach (const ForEach &foreach)
+        {
+            GetTypeFormatsContainer()->ForEach(foreach.GetFormatExactCallback());
+            GetRegexTypeFormatsContainer()->ForEach(foreach.GetFormatRegexCallback());
+            
+            GetTypeSummariesContainer()->ForEach(foreach.GetSummaryExactCallback());
+            GetRegexTypeSummariesContainer()->ForEach(foreach.GetSummaryRegexCallback());
+            
+            GetTypeFiltersContainer()->ForEach(foreach.GetFilterExactCallback());
+            GetRegexTypeFiltersContainer()->ForEach(foreach.GetFilterRegexCallback());
+            
+#ifndef LLDB_DISABLE_PYTHON
+            GetTypeSyntheticsContainer()->ForEach(foreach.GetSynthExactCallback());
+            GetRegexTypeSyntheticsContainer()->ForEach(foreach.GetSynthRegexCallback());
+#endif // LLDB_DISABLE_PYTHON
+            
+            GetTypeValidatorsContainer()->ForEach(foreach.GetValidatorExactCallback());
+            GetRegexTypeValidatorsContainer()->ForEach(foreach.GetValidatorRegexCallback());
+        }
+        
         FormatContainerSP
         GetTypeFormatsContainer ()
         {

Modified: lldb/trunk/include/lldb/DataFormatters/TypeCategoryMap.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/DataFormatters/TypeCategoryMap.h?rev=253125&r1=253124&r2=253125&view=diff
==============================================================================
--- lldb/trunk/include/lldb/DataFormatters/TypeCategoryMap.h (original)
+++ lldb/trunk/include/lldb/DataFormatters/TypeCategoryMap.h Fri Nov 13 23:44:23 2015
@@ -12,6 +12,7 @@
 
 // C Includes
 // C++ Includes
+#include <functional>
 #include <list>
 #include <map>
 
@@ -37,6 +38,9 @@ namespace lldb_private {
         typedef std::map<KeyType, ValueSP> MapType;
         typedef MapType::iterator MapIterator;
         typedef bool(*CallbackType)(void*, const ValueSP&);
+        
+        typedef std::function<bool(const ValueSP&)> ForEachCallback;
+        
         typedef uint32_t Position;
         
         static const Position First = 0;
@@ -86,6 +90,9 @@ namespace lldb_private {
         void
         LoopThrough (CallbackType callback, void* param);
         
+        void
+        ForEach (ForEachCallback callback);
+        
         lldb::TypeCategoryImplSP
         GetAtIndex (uint32_t);
         

Modified: lldb/trunk/source/Commands/CommandObjectType.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectType.cpp?rev=253125&r1=253124&r2=253125&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectType.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectType.cpp Fri Nov 13 23:44:23 2015
@@ -1156,14 +1156,6 @@ private:
         return &m_options;
     }
     
-    static bool
-    PerCategoryCallback(void* param,
-                        const lldb::TypeCategoryImplSP& cate)
-    {
-        cate->Clear(eFormatCategoryItemValue | eFormatCategoryItemRegexValue);
-        return true;
-    }
-    
 public:
     CommandObjectTypeFormatClear (CommandInterpreter &interpreter) :
         CommandObjectParsed (interpreter,
@@ -1183,8 +1175,12 @@ protected:
     DoExecute (Args& command, CommandReturnObject &result) override
     {
         if (m_options.m_delete_all)
-            DataVisualization::Categories::LoopThrough(PerCategoryCallback, NULL);
-        
+        {
+            DataVisualization::Categories::ForEach( [] (const TypeCategoryImplSP& category_sp) -> bool {
+                category_sp->Clear(eFormatCategoryItemValue | eFormatCategoryItemRegexValue);
+                return true;
+            });
+        }
         else
         {
             lldb::TypeCategoryImplSP category;
@@ -2188,16 +2184,6 @@ private:
         return &m_options;
     }
     
-    static bool
-    PerCategoryCallback(void* param,
-                        const lldb::TypeCategoryImplSP& cate)
-    {
-        cate->GetTypeSummariesContainer()->Clear();
-        cate->GetRegexTypeSummariesContainer()->Clear();
-        return true;
-        
-    }
-    
 public:
     CommandObjectTypeSummaryClear (CommandInterpreter &interpreter) :
         CommandObjectParsed (interpreter,
@@ -2218,8 +2204,12 @@ protected:
     {
         
         if (m_options.m_delete_all)
-            DataVisualization::Categories::LoopThrough(PerCategoryCallback, NULL);
-        
+        {
+            DataVisualization::Categories::ForEach( [] (const TypeCategoryImplSP& category_sp) -> bool {
+                category_sp->Clear(eFormatCategoryItemSummary | eFormatCategoryItemRegexSummary);
+                return true;
+            });
+        }
         else
         {        
             lldb::TypeCategoryImplSP category;
@@ -3931,15 +3921,6 @@ private:
         return &m_options;
     }
     
-    static bool
-    PerCategoryCallback(void* param,
-                        const lldb::TypeCategoryImplSP& cate)
-    {
-        cate->Clear(eFormatCategoryItemFilter | eFormatCategoryItemRegexFilter);
-        return true;
-        
-    }
-    
 public:
     CommandObjectTypeFilterClear (CommandInterpreter &interpreter) :
         CommandObjectParsed (interpreter,
@@ -3960,8 +3941,12 @@ protected:
     {
         
         if (m_options.m_delete_all)
-            DataVisualization::Categories::LoopThrough(PerCategoryCallback, NULL);
-        
+        {
+            DataVisualization::Categories::ForEach( [] (const TypeCategoryImplSP& category_sp) -> bool {
+                category_sp->Clear(eFormatCategoryItemFilter | eFormatCategoryItemRegexFilter);
+                return true;
+            });
+        }
         else
         {        
             lldb::TypeCategoryImplSP category;
@@ -3973,8 +3958,7 @@ protected:
             }
             else
                 DataVisualization::Categories::GetCategory(ConstString(NULL), category);
-            category->GetTypeFiltersContainer()->Clear();
-            category->GetRegexTypeFiltersContainer()->Clear();
+            category->Clear(eFormatCategoryItemFilter | eFormatCategoryItemRegexFilter);
         }
         
         result.SetStatus(eReturnStatusSuccessFinishResult);
@@ -4059,15 +4043,6 @@ private:
         return &m_options;
     }
     
-    static bool
-    PerCategoryCallback(void* param,
-                        const lldb::TypeCategoryImplSP& cate)
-    {
-        cate->Clear(eFormatCategoryItemSynth | eFormatCategoryItemRegexSynth);
-        return true;
-        
-    }
-    
 public:
     CommandObjectTypeSynthClear (CommandInterpreter &interpreter) :
         CommandObjectParsed (interpreter,
@@ -4088,8 +4063,12 @@ protected:
     {
         
         if (m_options.m_delete_all)
-            DataVisualization::Categories::LoopThrough(PerCategoryCallback, NULL);
-        
+        {
+            DataVisualization::Categories::ForEach( [] (const TypeCategoryImplSP& category_sp) -> bool {
+                category_sp->Clear(eFormatCategoryItemSynth | eFormatCategoryItemRegexSynth);
+                return true;
+            });
+        }
         else
         {        
             lldb::TypeCategoryImplSP category;
@@ -4101,8 +4080,7 @@ protected:
             }
             else
                 DataVisualization::Categories::GetCategory(ConstString(NULL), category);
-            category->GetTypeSyntheticsContainer()->Clear();
-            category->GetRegexTypeSyntheticsContainer()->Clear();
+            category->Clear(eFormatCategoryItemSynth | eFormatCategoryItemRegexSynth);
         }
         
         result.SetStatus(eReturnStatusSuccessFinishResult);

Modified: lldb/trunk/source/DataFormatters/DataVisualization.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/DataFormatters/DataVisualization.cpp?rev=253125&r1=253124&r2=253125&view=diff
==============================================================================
--- lldb/trunk/source/DataFormatters/DataVisualization.cpp (original)
+++ lldb/trunk/source/DataFormatters/DataVisualization.cpp Fri Nov 13 23:44:23 2015
@@ -231,6 +231,12 @@ DataVisualization::Categories::LoopThrou
     GetFormatManager().LoopThroughCategories(callback, callback_baton);
 }
 
+void
+DataVisualization::Categories::ForEach (TypeCategoryMap::ForEachCallback callback)
+{
+    GetFormatManager().ForEachCategory(callback);
+}
+
 uint32_t
 DataVisualization::Categories::GetCount ()
 {

Modified: lldb/trunk/source/DataFormatters/FormatManager.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/DataFormatters/FormatManager.cpp?rev=253125&r1=253124&r2=253125&view=diff
==============================================================================
--- lldb/trunk/source/DataFormatters/FormatManager.cpp (original)
+++ lldb/trunk/source/DataFormatters/FormatManager.cpp Fri Nov 13 23:44:23 2015
@@ -500,6 +500,21 @@ FormatManager::LoopThroughCategories (Ca
     }
 }
 
+void
+FormatManager::ForEachCategory(TypeCategoryMap::ForEachCallback callback)
+{
+    m_categories_map.ForEach(callback);
+    Mutex::Locker locker(m_language_categories_mutex);
+    for (const auto& entry : m_language_categories_map)
+    {
+        if (auto category_sp = entry.second->GetCategory())
+        {
+            if (!callback(category_sp))
+                break;
+        }
+    }
+}
+
 lldb::TypeCategoryImplSP
 FormatManager::GetCategory (const ConstString& category_name,
                             bool can_create)

Modified: lldb/trunk/source/DataFormatters/TypeCategoryMap.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/DataFormatters/TypeCategoryMap.cpp?rev=253125&r1=253124&r2=253125&view=diff
==============================================================================
--- lldb/trunk/source/DataFormatters/TypeCategoryMap.cpp (original)
+++ lldb/trunk/source/DataFormatters/TypeCategoryMap.cpp Fri Nov 13 23:44:23 2015
@@ -405,6 +405,39 @@ TypeCategoryMap::LoopThrough(CallbackTyp
     }
 }
 
+void
+TypeCategoryMap::ForEach(ForEachCallback callback)
+{
+    if (callback)
+    {
+        Mutex::Locker locker(m_map_mutex);
+        
+        // loop through enabled categories in respective order
+        {
+            ActiveCategoriesIterator begin, end = m_active_categories.end();
+            for (begin = m_active_categories.begin(); begin != end; begin++)
+            {
+                lldb::TypeCategoryImplSP category = *begin;
+                if (!callback(category))
+                    break;
+            }
+        }
+        
+        // loop through disabled categories in just any order
+        {
+            MapIterator pos, end = m_map.end();
+            for (pos = m_map.begin(); pos != end; pos++)
+            {
+                if (pos->second->IsEnabled())
+                    continue;
+                KeyType type = pos->first;
+                if (!callback(pos->second))
+                    break;
+            }
+        }
+    }
+}
+
 TypeCategoryImplSP
 TypeCategoryMap::GetAtIndex (uint32_t index)
 {




More information about the lldb-commits mailing list