LLDB mainline
TypeCategory.cpp
Go to the documentation of this file.
1//===-- TypeCategory.cpp --------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
11
12
13using namespace lldb;
14using namespace lldb_private;
15
17 ConstString name)
18 : m_format_cont(clist), m_summary_cont(clist), m_filter_cont(clist),
19 m_synth_cont(clist), m_enabled(false), m_change_listener(clist),
20 m_mutex(), m_name(name), m_languages() {}
21
22static bool IsApplicable(lldb::LanguageType category_lang,
23 lldb::LanguageType valobj_lang) {
24 switch (category_lang) {
25 // Unless we know better, allow only exact equality.
26 default:
27 return category_lang == valobj_lang;
28
29 // the C family, we consider it as one
31 case eLanguageTypeC:
33 return valobj_lang == eLanguageTypeC89 || valobj_lang == eLanguageTypeC ||
34 valobj_lang == eLanguageTypeC99;
35
36 // ObjC knows about C and itself
38 return valobj_lang == eLanguageTypeC89 || valobj_lang == eLanguageTypeC ||
39 valobj_lang == eLanguageTypeC99 || valobj_lang == eLanguageTypeObjC;
40
41 // C++ knows about C and C++
43 return valobj_lang == eLanguageTypeC89 || valobj_lang == eLanguageTypeC ||
44 valobj_lang == eLanguageTypeC99 ||
45 valobj_lang == eLanguageTypeC_plus_plus;
46
47 // ObjC++ knows about C,C++,ObjC and ObjC++
49 return valobj_lang == eLanguageTypeC89 || valobj_lang == eLanguageTypeC ||
50 valobj_lang == eLanguageTypeC99 ||
51 valobj_lang == eLanguageTypeC_plus_plus ||
52 valobj_lang == eLanguageTypeObjC;
53
54 // Categories with unspecified language match everything.
56 return true;
57 }
58}
59
61 for (size_t idx = 0; idx < GetNumLanguages(); idx++) {
62 const lldb::LanguageType category_lang = GetLanguageAtIndex(idx);
63 if (::IsApplicable(category_lang, lang))
64 return true;
65 }
66 return false;
67}
68
70 if (m_languages.empty())
71 return 1;
72 return m_languages.size();
73}
74
76 if (m_languages.empty())
78 return m_languages[idx];
79}
80
82 m_languages.push_back(lang);
83}
84
86 const FormattersMatchVector &candidates,
88 if (!IsEnabled() || !IsApplicable(lang))
89 return false;
90 return m_format_cont.Get(candidates, entry);
91}
92
94 const FormattersMatchVector &candidates,
96 if (!IsEnabled() || !IsApplicable(lang))
97 return false;
98 return m_summary_cont.Get(candidates, entry);
99}
100
102 const FormattersMatchVector &candidates,
104 if (!IsEnabled() || !IsApplicable(lang))
105 return false;
106
107 // first find both Filter and Synth, and then check which is most recent
108 bool pick_synth = false;
109
111 m_filter_cont.Get(candidates, filter_sp);
112
114 m_synth_cont.Get(candidates, synth_sp);
115
116 if (!filter_sp.get() && !synth_sp.get())
117 return false;
118 else if (!filter_sp.get() && synth_sp.get())
119 pick_synth = true;
120 else if (filter_sp.get() && !synth_sp.get())
121 pick_synth = false;
122 else /*if (filter_sp.get() && synth_sp.get())*/
123 {
124 pick_synth = filter_sp->GetRevision() <= synth_sp->GetRevision();
125 }
126
127 if (pick_synth) {
128 entry = synth_sp;
129 return true;
130 } else {
131 entry = filter_sp;
132 return true;
133 }
134 return false;
135}
136
138 if (items & eFormatCategoryItemFormat)
140
141 if (items & eFormatCategoryItemSummary)
143
144 if (items & eFormatCategoryItemFilter)
146
147 if (items & eFormatCategoryItemSynth)
149}
150
152 bool success = false;
153
154 if (items & eFormatCategoryItemFormat)
155 success = m_format_cont.Delete(name) || success;
156
157 if (items & eFormatCategoryItemSummary)
158 success = m_summary_cont.Delete(name) || success;
159
160 if (items & eFormatCategoryItemFilter)
161 success = m_filter_cont.Delete(name) || success;
162
163 if (items & eFormatCategoryItemSynth)
164 success = m_synth_cont.Delete(name) || success;
165
166 return success;
167}
168
170 uint32_t count = 0;
171
172 if (items & eFormatCategoryItemFormat)
173 count += m_format_cont.GetCount();
174
175 if (items & eFormatCategoryItemSummary)
176 count += m_summary_cont.GetCount();
177
178 if (items & eFormatCategoryItemFilter)
179 count += m_filter_cont.GetCount();
180
181 if (items & eFormatCategoryItemSynth)
182 count += m_synth_cont.GetCount();
183
184 return count;
185}
186
188 const FormattersMatchCandidate &candidate_type, FormatCategoryItems items,
189 bool only_enabled, const char **matching_category,
190 FormatCategoryItems *matching_type) {
191 if (!IsEnabled() && only_enabled)
192 return false;
193
194 if (items & eFormatCategoryItemFormat) {
195 if (m_format_cont.AnyMatches(candidate_type)) {
196 if (matching_category)
197 *matching_category = m_name.GetCString();
198 if (matching_type)
199 *matching_type = eFormatCategoryItemFormat;
200 return true;
201 }
202 }
203
204 if (items & eFormatCategoryItemSummary) {
205 if (m_summary_cont.AnyMatches(candidate_type)) {
206 if (matching_category)
207 *matching_category = m_name.GetCString();
208 if (matching_type)
209 *matching_type = eFormatCategoryItemSummary;
210 return true;
211 }
212 }
213
214 if (items & eFormatCategoryItemFilter) {
215 if (m_filter_cont.AnyMatches(candidate_type)) {
216 if (matching_category)
217 *matching_category = m_name.GetCString();
218 if (matching_type)
219 *matching_type = eFormatCategoryItemFilter;
220 return true;
221 }
222 }
223
224 if (items & eFormatCategoryItemSynth) {
225 if (m_synth_cont.AnyMatches(candidate_type)) {
226 if (matching_category)
227 *matching_category = m_name.GetCString();
228 if (matching_type)
229 *matching_type = eFormatCategoryItemSynth;
230 return true;
231 }
232 }
233
234 return false;
235}
236
238 FormatCategoryItems items) {
239 if (items & eFormatCategoryItemFormat)
241 if (items & eFormatCategoryItemSummary)
243 if (items & eFormatCategoryItemFilter)
245 if (items & eFormatCategoryItemSynth)
246 m_synth_cont.AutoComplete(request);
247}
248
252}
253
257}
258
262}
263
267}
268
271 return m_format_cont.GetAtIndex(index);
272}
273
276 return m_summary_cont.GetAtIndex(index);
277}
278
281 return m_filter_cont.GetAtIndex(index);
282}
283
286 return m_synth_cont.GetAtIndex(index);
287}
288
292}
293
297}
298
302}
303
307}
308
309void TypeCategoryImpl::Enable(bool value, uint32_t position) {
310 std::lock_guard<std::recursive_mutex> guard(m_mutex);
311 if ((m_enabled = value))
312 m_enabled_position = position;
315}
316
318 StreamString stream;
319 stream.Printf("%s (%s", GetName(), (IsEnabled() ? "enabled" : "disabled"));
320 StreamString lang_stream;
321 lang_stream.Printf(", applicable for language(s): ");
322 bool print_lang = false;
323 for (size_t idx = 0; idx < GetNumLanguages(); idx++) {
324 const lldb::LanguageType lang = GetLanguageAtIndex(idx);
325 if (lang != lldb::eLanguageTypeUnknown)
326 print_lang = true;
327 lang_stream.Printf("%s%s", Language::GetNameForLanguageType(lang),
328 idx + 1 < GetNumLanguages() ? ", " : "");
329 }
330 if (print_lang)
331 stream.PutCString(lang_stream.GetString());
332 stream.PutChar(')');
333 return std::string(stream.GetString());
334}
static bool IsApplicable(lldb::LanguageType category_lang, lldb::LanguageType valobj_lang)
"lldb/Utility/ArgCompletionRequest.h"
A uniqued constant string class.
Definition: ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
static const char * GetNameForLanguageType(lldb::LanguageType language)
Definition: Language.cpp:266
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
size_t PutChar(char ch)
Definition: Stream.cpp:131
std::shared_ptr< SyntheticChildren > SharedPointer
void AutoComplete(CompletionRequest &request)
Definition: TypeCategory.h:157
MapValueType GetAtIndex(size_t index)
Returns the formatter at index, simulating a flattened view of all subcontainers in priority order.
Definition: TypeCategory.h:87
bool Get(const FormattersMatchVector &candidates, std::shared_ptr< FormatterImpl > &entry)
Looks for a matching candidate across all priority tiers, in priority order.
Definition: TypeCategory.h:99
void Clear()
Clears all subcontainers.
Definition: TypeCategory.h:49
lldb::TypeNameSpecifierImplSP GetTypeNameSpecifierAtIndex(size_t index)
Returns the type name specifier at index, simulating a flattened view of all subcontainers in priorit...
Definition: TypeCategory.h:138
bool Delete(lldb::TypeNameSpecifierImplSP type_sp)
Deletes the formatter specified by type_sp.
Definition: TypeCategory.h:63
MapValueType GetForTypeNameSpecifier(lldb::TypeNameSpecifierImplSP type_specifier_sp)
Returns a formatter that is an exact match for type_specifier_sp.
Definition: TypeCategory.h:127
uint32_t GetCount()
Returns the total count of elements across all subcontainers.
Definition: TypeCategory.h:78
bool AnyMatches(const FormattersMatchCandidate &candidate)
Definition: TypeCategory.h:108
uint32_t GetCount(FormatCategoryItems items=ALL_ITEM_TYPES)
SummaryContainer::MapValueType GetSummaryAtIndex(size_t index)
lldb::TypeNameSpecifierImplSP GetTypeNameSpecifierForSyntheticAtIndex(size_t index)
SummaryContainer::MapValueType GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp)
FilterContainer::MapValueType GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp)
lldb::TypeNameSpecifierImplSP GetTypeNameSpecifierForSummaryAtIndex(size_t index)
lldb::TypeNameSpecifierImplSP GetTypeNameSpecifierForFormatAtIndex(size_t index)
bool Get(lldb::LanguageType lang, const FormattersMatchVector &candidates, lldb::TypeFormatImplSP &entry)
lldb::TypeNameSpecifierImplSP GetTypeNameSpecifierForFilterAtIndex(size_t index)
FormatContainer::MapValueType GetFormatAtIndex(size_t index)
IFormatChangeListener * m_change_listener
Definition: TypeCategory.h:367
std::recursive_mutex m_mutex
Definition: TypeCategory.h:369
void AddLanguage(lldb::LanguageType lang)
lldb::LanguageType GetLanguageAtIndex(size_t idx)
TypeCategoryImpl(IFormatChangeListener *clist, ConstString name)
bool AnyMatches(const FormattersMatchCandidate &candidate_type, FormatCategoryItems items=ALL_ITEM_TYPES, bool only_enabled=true, const char **matching_category=nullptr, FormatCategoryItems *matching_type=nullptr)
bool Delete(ConstString name, FormatCategoryItems items=ALL_ITEM_TYPES)
SummaryContainer m_summary_cont
Definition: TypeCategory.h:361
void AutoComplete(CompletionRequest &request, FormatCategoryItems items)
FilterContainer::MapValueType GetFilterAtIndex(size_t index)
SynthContainer::MapValueType GetSyntheticAtIndex(size_t index)
bool IsApplicable(lldb::LanguageType lang)
FormatContainer::MapValueType GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp)
std::vector< lldb::LanguageType > m_languages
Definition: TypeCategory.h:373
void Clear(FormatCategoryItems items=ALL_ITEM_TYPES)
SynthContainer::MapValueType GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp)
void Enable(bool value, uint32_t position)
std::shared_ptr< TypeFilterImpl > SharedPointer
A class that represents a running process on the host machine.
std::vector< FormattersMatchCandidate > FormattersMatchVector
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::TypeSummaryImpl > TypeSummaryImplSP
Definition: lldb-forward.h:470
std::shared_ptr< lldb_private::TypeFormatImpl > TypeFormatImplSP
Definition: lldb-forward.h:467
std::shared_ptr< lldb_private::TypeNameSpecifierImpl > TypeNameSpecifierImplSP
Definition: lldb-forward.h:469
LanguageType
Programming language type.
@ eLanguageTypeC99
ISO C:1999.
@ eLanguageTypeUnknown
Unknown or invalid language value.
@ eLanguageTypeObjC_plus_plus
Objective-C++.
@ eLanguageTypeC89
ISO C:1989.
@ eLanguageTypeC
Non-standardized C, such as K&R.
@ eLanguageTypeObjC
Objective-C.
@ eLanguageTypeC_plus_plus
ISO C++:1998.
std::shared_ptr< lldb_private::SyntheticChildren > SyntheticChildrenSP
Definition: lldb-forward.h:440