Home | History | Annotate | Download | only in DataFormatters
      1 //===-- FormatManager.h -------------------------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #ifndef lldb_FormatManager_h_
     11 #define lldb_FormatManager_h_
     12 
     13 // C Includes
     14 // C++ Includes
     15 
     16 // Other libraries and framework includes
     17 // Project includes
     18 #include "lldb/lldb-public.h"
     19 #include "lldb/lldb-enumerations.h"
     20 
     21 #include "lldb/DataFormatters/FormatCache.h"
     22 #include "lldb/DataFormatters/FormatNavigator.h"
     23 #include "lldb/DataFormatters/TypeCategory.h"
     24 #include "lldb/DataFormatters/TypeCategoryMap.h"
     25 
     26 namespace lldb_private {
     27 
     28 // this file (and its. cpp) contain the low-level implementation of LLDB Data Visualization
     29 // class DataVisualization is the high-level front-end of this feature
     30 // clients should refer to that class as the entry-point into the data formatters
     31 // unless they have a good reason to bypass it and prefer to use this file's objects directly
     32 
     33 class FormatManager : public IFormatChangeListener
     34 {
     35     typedef FormatNavigator<ConstString, TypeFormatImpl> ValueNavigator;
     36     typedef ValueNavigator::MapType ValueMap;
     37     typedef FormatMap<ConstString, TypeSummaryImpl> NamedSummariesMap;
     38     typedef TypeCategoryMap::MapType::iterator CategoryMapIterator;
     39 public:
     40 
     41     typedef TypeCategoryMap::CallbackType CategoryCallback;
     42 
     43     FormatManager ();
     44 
     45     ValueNavigator&
     46     GetValueNavigator ()
     47     {
     48         return m_value_nav;
     49     }
     50 
     51     NamedSummariesMap&
     52     GetNamedSummaryNavigator ()
     53     {
     54         return m_named_summaries_map;
     55     }
     56 
     57     void
     58     EnableCategory (const ConstString& category_name,
     59                     TypeCategoryMap::Position pos = TypeCategoryMap::Default)
     60     {
     61         m_categories_map.Enable(category_name,
     62                                 pos);
     63     }
     64 
     65     void
     66     DisableCategory (const ConstString& category_name)
     67     {
     68         m_categories_map.Disable(category_name);
     69     }
     70 
     71     void
     72     EnableCategory (const lldb::TypeCategoryImplSP& category,
     73                     TypeCategoryMap::Position pos = TypeCategoryMap::Default)
     74     {
     75         m_categories_map.Enable(category,
     76                                 pos);
     77     }
     78 
     79     void
     80     DisableCategory (const lldb::TypeCategoryImplSP& category)
     81     {
     82         m_categories_map.Disable(category);
     83     }
     84 
     85     bool
     86     DeleteCategory (const ConstString& category_name)
     87     {
     88         return m_categories_map.Delete(category_name);
     89     }
     90 
     91     void
     92     ClearCategories ()
     93     {
     94         return m_categories_map.Clear();
     95     }
     96 
     97     uint32_t
     98     GetCategoriesCount ()
     99     {
    100         return m_categories_map.GetCount();
    101     }
    102 
    103     lldb::TypeCategoryImplSP
    104     GetCategoryAtIndex (size_t index)
    105     {
    106         return m_categories_map.GetAtIndex(index);
    107     }
    108 
    109     void
    110     LoopThroughCategories (CategoryCallback callback, void* param)
    111     {
    112         m_categories_map.LoopThrough(callback, param);
    113     }
    114 
    115     lldb::TypeCategoryImplSP
    116     GetCategory (const char* category_name = NULL,
    117                  bool can_create = true)
    118     {
    119         if (!category_name)
    120             return GetCategory(m_default_category_name);
    121         return GetCategory(ConstString(category_name));
    122     }
    123 
    124     lldb::TypeCategoryImplSP
    125     GetCategory (const ConstString& category_name,
    126                  bool can_create = true);
    127 
    128     lldb::TypeSummaryImplSP
    129     GetSummaryForType (lldb::TypeNameSpecifierImplSP type_sp);
    130 
    131     lldb::TypeFilterImplSP
    132     GetFilterForType (lldb::TypeNameSpecifierImplSP type_sp);
    133 
    134 #ifndef LLDB_DISABLE_PYTHON
    135     lldb::ScriptedSyntheticChildrenSP
    136     GetSyntheticForType (lldb::TypeNameSpecifierImplSP type_sp);
    137 #endif
    138 
    139 #ifndef LLDB_DISABLE_PYTHON
    140     lldb::SyntheticChildrenSP
    141     GetSyntheticChildrenForType (lldb::TypeNameSpecifierImplSP type_sp);
    142 #endif
    143 
    144     lldb::TypeSummaryImplSP
    145     GetSummaryFormat (ValueObject& valobj,
    146                       lldb::DynamicValueType use_dynamic);
    147 
    148 #ifndef LLDB_DISABLE_PYTHON
    149     lldb::SyntheticChildrenSP
    150     GetSyntheticChildren (ValueObject& valobj,
    151                           lldb::DynamicValueType use_dynamic);
    152 #endif
    153 
    154     bool
    155     AnyMatches (ConstString type_name,
    156                 TypeCategoryImpl::FormatCategoryItems items = TypeCategoryImpl::ALL_ITEM_TYPES,
    157                 bool only_enabled = true,
    158                 const char** matching_category = NULL,
    159                 TypeCategoryImpl::FormatCategoryItems* matching_type = NULL)
    160     {
    161         return m_categories_map.AnyMatches(type_name,
    162                                            items,
    163                                            only_enabled,
    164                                            matching_category,
    165                                            matching_type);
    166     }
    167 
    168     static bool
    169     GetFormatFromCString (const char *format_cstr,
    170                           bool partial_match_ok,
    171                           lldb::Format &format);
    172 
    173     static char
    174     GetFormatAsFormatChar (lldb::Format format);
    175 
    176     static const char *
    177     GetFormatAsCString (lldb::Format format);
    178 
    179     // if the user tries to add formatters for, say, "struct Foo"
    180     // those will not match any type because of the way we strip qualifiers from typenames
    181     // this method looks for the case where the user is adding a "class","struct","enum" or "union" Foo
    182     // and strips the unnecessary qualifier
    183     static ConstString
    184     GetValidTypeName (const ConstString& type);
    185 
    186     // when DataExtractor dumps a vectorOfT, it uses a predefined format for each item
    187     // this method returns it, or eFormatInvalid if vector_format is not a vectorOf
    188     static lldb::Format
    189     GetSingleItemFormat (lldb::Format vector_format);
    190 
    191     void
    192     Changed ()
    193     {
    194         __sync_add_and_fetch(&m_last_revision, +1);
    195         m_format_cache.Clear ();
    196     }
    197 
    198     uint32_t
    199     GetCurrentRevision ()
    200     {
    201         return m_last_revision;
    202     }
    203 
    204     ~FormatManager ()
    205     {
    206     }
    207 
    208 private:
    209     FormatCache m_format_cache;
    210     ValueNavigator m_value_nav;
    211     NamedSummariesMap m_named_summaries_map;
    212     uint32_t m_last_revision;
    213     TypeCategoryMap m_categories_map;
    214 
    215     ConstString m_default_category_name;
    216     ConstString m_system_category_name;
    217     ConstString m_gnu_cpp_category_name;
    218     ConstString m_libcxx_category_name;
    219     ConstString m_objc_category_name;
    220     ConstString m_corefoundation_category_name;
    221     ConstString m_coregraphics_category_name;
    222     ConstString m_coreservices_category_name;
    223     ConstString m_vectortypes_category_name;
    224     ConstString m_appkit_category_name;
    225 
    226     TypeCategoryMap&
    227     GetCategories ()
    228     {
    229         return m_categories_map;
    230     }
    231 
    232     // WARNING: these are temporary functions that setup formatters
    233     // while a few of these actually should be globally available and setup by LLDB itself
    234     // most would actually belong to the users' lldbinit file or to some other form of configurable
    235     // storage
    236     void
    237     LoadLibStdcppFormatters ();
    238 
    239     void
    240     LoadLibcxxFormatters ();
    241 
    242     void
    243     LoadSystemFormatters ();
    244 
    245     void
    246     LoadObjCFormatters ();
    247 };
    248 
    249 } // namespace lldb_private
    250 
    251 #endif	// lldb_FormatManager_h_
    252