aboutsummaryrefslogtreecommitdiff
path: root/include/lldb/DataFormatters/FormatManager.h
blob: 24ec877ee515689cec7c6e8c8046a219c948a39a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//===-- FormatManager.h -------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef lldb_FormatManager_h_
#define lldb_FormatManager_h_

// C Includes
// C++ Includes

// Other libraries and framework includes
// Project includes
#include "lldb/lldb-public.h"
#include "lldb/lldb-enumerations.h"

#include "lldb/DataFormatters/FormatCache.h"
#include "lldb/DataFormatters/FormatClasses.h"
#include "lldb/DataFormatters/FormattersContainer.h"
#include "lldb/DataFormatters/TypeCategory.h"
#include "lldb/DataFormatters/TypeCategoryMap.h"

#include <atomic>

namespace lldb_private {
    
// this file (and its. cpp) contain the low-level implementation of LLDB Data Visualization
// class DataVisualization is the high-level front-end of this feature
// clients should refer to that class as the entry-point into the data formatters
// unless they have a good reason to bypass it and prefer to use this file's objects directly

class FormatManager : public IFormatChangeListener
{
    typedef FormatMap<ConstString, TypeSummaryImpl> NamedSummariesMap;
    typedef TypeCategoryMap::MapType::iterator CategoryMapIterator;
public:
    
    typedef TypeCategoryMap::CallbackType CategoryCallback;
    
    FormatManager ();
    
    NamedSummariesMap&
    GetNamedSummaryContainer ()
    {
        return m_named_summaries_map;
    }
    
    void
    EnableCategory (const ConstString& category_name,
                    TypeCategoryMap::Position pos = TypeCategoryMap::Default)
    {
        m_categories_map.Enable(category_name,
                                pos);
    }
    
    void
    DisableCategory (const ConstString& category_name)
    {
        m_categories_map.Disable(category_name);
    }
    
    void
    EnableCategory (const lldb::TypeCategoryImplSP& category,
                    TypeCategoryMap::Position pos = TypeCategoryMap::Default)
    {
        m_categories_map.Enable(category,
                                pos);
    }
    
    void
    DisableCategory (const lldb::TypeCategoryImplSP& category)
    {
        m_categories_map.Disable(category);
    }
    
    bool
    DeleteCategory (const ConstString& category_name)
    {
        return m_categories_map.Delete(category_name);
    }
    
    void
    ClearCategories ()
    {
        return m_categories_map.Clear();
    }
    
    uint32_t
    GetCategoriesCount ()
    {
        return m_categories_map.GetCount();
    }
    
    lldb::TypeCategoryImplSP
    GetCategoryAtIndex (size_t index)
    {
        return m_categories_map.GetAtIndex(index);
    }
    
    void
    LoopThroughCategories (CategoryCallback callback, void* param)
    {
        m_categories_map.LoopThrough(callback, param);
    }
    
    lldb::TypeCategoryImplSP
    GetCategory (const char* category_name = NULL,
                 bool can_create = true)
    {
        if (!category_name)
            return GetCategory(m_default_category_name);
        return GetCategory(ConstString(category_name));
    }
    
    lldb::TypeCategoryImplSP
    GetCategory (const ConstString& category_name,
                 bool can_create = true);

    lldb::TypeFormatImplSP
    GetFormatForType (lldb::TypeNameSpecifierImplSP type_sp);
    
    lldb::TypeSummaryImplSP
    GetSummaryForType (lldb::TypeNameSpecifierImplSP type_sp);

    lldb::TypeFilterImplSP
    GetFilterForType (lldb::TypeNameSpecifierImplSP type_sp);

#ifndef LLDB_DISABLE_PYTHON
    lldb::ScriptedSyntheticChildrenSP
    GetSyntheticForType (lldb::TypeNameSpecifierImplSP type_sp);
#endif
    
#ifndef LLDB_DISABLE_PYTHON
    lldb::SyntheticChildrenSP
    GetSyntheticChildrenForType (lldb::TypeNameSpecifierImplSP type_sp);
#endif
    
    lldb::TypeFormatImplSP
    GetFormat (ValueObject& valobj,
               lldb::DynamicValueType use_dynamic);
    
    lldb::TypeSummaryImplSP
    GetSummaryFormat (ValueObject& valobj,
                      lldb::DynamicValueType use_dynamic);

#ifndef LLDB_DISABLE_PYTHON
    lldb::SyntheticChildrenSP
    GetSyntheticChildren (ValueObject& valobj,
                          lldb::DynamicValueType use_dynamic);
#endif
    
    bool
    AnyMatches (ConstString type_name,
                TypeCategoryImpl::FormatCategoryItems items = TypeCategoryImpl::ALL_ITEM_TYPES,
                bool only_enabled = true,
                const char** matching_category = NULL,
                TypeCategoryImpl::FormatCategoryItems* matching_type = NULL)
    {
        return m_categories_map.AnyMatches(type_name,
                                           items,
                                           only_enabled,
                                           matching_category,
                                           matching_type);
    }

    static bool
    GetFormatFromCString (const char *format_cstr,
                          bool partial_match_ok,
                          lldb::Format &format);

    static char
    GetFormatAsFormatChar (lldb::Format format);

    static const char *
    GetFormatAsCString (lldb::Format format);
    
    // if the user tries to add formatters for, say, "struct Foo"
    // those will not match any type because of the way we strip qualifiers from typenames
    // this method looks for the case where the user is adding a "class","struct","enum" or "union" Foo
    // and strips the unnecessary qualifier
    static ConstString
    GetValidTypeName (const ConstString& type);
    
    // when DataExtractor dumps a vectorOfT, it uses a predefined format for each item
    // this method returns it, or eFormatInvalid if vector_format is not a vectorOf
    static lldb::Format
    GetSingleItemFormat (lldb::Format vector_format);
    
    // this returns true if the ValueObjectPrinter is *highly encouraged*
    // to actually represent this ValueObject in one-liner format
    // If this object has a summary formatter, however, we should not
    // try and do one-lining, just let the summary do the right thing
    bool
    ShouldPrintAsOneLiner (ValueObject& valobj);
    
    void
    Changed ()
    {
        ++m_last_revision;
        m_format_cache.Clear ();
    }
    
    uint32_t
    GetCurrentRevision ()
    {
        return m_last_revision;
    }
    
    ~FormatManager ()
    {
    }
    
    static FormattersMatchVector
    GetPossibleMatches (ValueObject& valobj,
                        lldb::DynamicValueType use_dynamic)
    {
        FormattersMatchVector matches;
        GetPossibleMatches (valobj,
                            valobj.GetClangType(),
                            lldb_private::eFormatterChoiceCriterionDirectChoice,
                            use_dynamic,
                            matches,
                            false,
                            false,
                            false,
                            true);
        return matches;
    }

private:
    
    static void
    GetPossibleMatches (ValueObject& valobj,
                        ClangASTType clang_type,
                        uint32_t reason,
                        lldb::DynamicValueType use_dynamic,
                        FormattersMatchVector& entries,
                        bool did_strip_ptr,
                        bool did_strip_ref,
                        bool did_strip_typedef,
                        bool root_level = false);
    
    FormatCache m_format_cache;
    NamedSummariesMap m_named_summaries_map;
    std::atomic<uint32_t> m_last_revision;
    TypeCategoryMap m_categories_map;
    
    ConstString m_default_category_name;
    ConstString m_system_category_name;
    ConstString m_gnu_cpp_category_name;
    ConstString m_libcxx_category_name;
    ConstString m_objc_category_name;
    ConstString m_corefoundation_category_name;
    ConstString m_coregraphics_category_name;
    ConstString m_coreservices_category_name;
    ConstString m_vectortypes_category_name;
    ConstString m_appkit_category_name;
    
    TypeCategoryMap&
    GetCategories ()
    {
        return m_categories_map;
    }
    
    // WARNING: these are temporary functions that setup formatters
    // while a few of these actually should be globally available and setup by LLDB itself
    // most would actually belong to the users' lldbinit file or to some other form of configurable
    // storage
    void
    LoadLibStdcppFormatters ();
    
    void
    LoadLibcxxFormatters ();
    
    void
    LoadSystemFormatters ();
    
    void
    LoadObjCFormatters ();
};
    
} // namespace lldb_private

#endif	// lldb_FormatManager_h_