LLDB mainline
TypeSummary.h
Go to the documentation of this file.
1//===-- TypeSummary.h -------------------------------------------*- C++ -*-===//
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
9#ifndef LLDB_DATAFORMATTERS_TYPESUMMARY_H
10#define LLDB_DATAFORMATTERS_TYPESUMMARY_H
11
12#include <cstdint>
13
14#include <functional>
15#include <memory>
16#include <string>
17
19#include "lldb/lldb-public.h"
20
22#include "lldb/Utility/Status.h"
24
25namespace llvm {
26class MemoryBuffer;
27}
28
29namespace lldb_private {
48
50public:
52
53 virtual ~TypeSummaryImpl() = default;
54
55 Kind GetKind() const { return m_kind; }
56
57 class Flags {
58 public:
59 Flags() = default;
60
61 Flags(const Flags &other) : m_flags(other.m_flags) {}
62
63 Flags(uint32_t value) : m_flags(value) {}
64
65 Flags &operator=(const Flags &rhs) {
66 if (&rhs != this)
67 m_flags = rhs.m_flags;
68
69 return *this;
70 }
71
72 Flags &operator=(const uint32_t &rhs) {
73 m_flags = rhs;
74 return *this;
75 }
76
78 m_flags = 0;
79 return *this;
80 }
81
82 bool GetCascades() const {
83 return (m_flags & lldb::eTypeOptionCascade) == lldb::eTypeOptionCascade;
84 }
85
86 Flags &SetCascades(bool value = true) {
87 if (value)
88 m_flags |= lldb::eTypeOptionCascade;
89 else
90 m_flags &= ~lldb::eTypeOptionCascade;
91 return *this;
92 }
93
94 bool GetSkipPointers() const {
95 return (m_flags & lldb::eTypeOptionSkipPointers) ==
96 lldb::eTypeOptionSkipPointers;
97 }
98
99 Flags &SetSkipPointers(bool value = true) {
100 if (value)
101 m_flags |= lldb::eTypeOptionSkipPointers;
102 else
103 m_flags &= ~lldb::eTypeOptionSkipPointers;
104 return *this;
105 }
106
107 bool GetSkipReferences() const {
108 return (m_flags & lldb::eTypeOptionSkipReferences) ==
109 lldb::eTypeOptionSkipReferences;
110 }
111
112 Flags &SetSkipReferences(bool value = true) {
113 if (value)
114 m_flags |= lldb::eTypeOptionSkipReferences;
115 else
116 m_flags &= ~lldb::eTypeOptionSkipReferences;
117 return *this;
118 }
119
120 bool GetDontShowChildren() const {
121 return (m_flags & lldb::eTypeOptionHideChildren) ==
122 lldb::eTypeOptionHideChildren;
123 }
124
125 Flags &SetDontShowChildren(bool value = true) {
126 if (value)
127 m_flags |= lldb::eTypeOptionHideChildren;
128 else
129 m_flags &= ~lldb::eTypeOptionHideChildren;
130 return *this;
131 }
132
134 return (m_flags & lldb::eTypeOptionHideEmptyAggregates) ==
135 lldb::eTypeOptionHideEmptyAggregates;
136 }
137
138 Flags &SetHideEmptyAggregates(bool value = true) {
139 if (value)
140 m_flags |= lldb::eTypeOptionHideEmptyAggregates;
141 else
142 m_flags &= ~lldb::eTypeOptionHideEmptyAggregates;
143 return *this;
144 }
145
146 bool GetDontShowValue() const {
147 return (m_flags & lldb::eTypeOptionHideValue) ==
148 lldb::eTypeOptionHideValue;
149 }
150
151 Flags &SetDontShowValue(bool value = true) {
152 if (value)
153 m_flags |= lldb::eTypeOptionHideValue;
154 else
155 m_flags &= ~lldb::eTypeOptionHideValue;
156 return *this;
157 }
158
160 return (m_flags & lldb::eTypeOptionShowOneLiner) ==
161 lldb::eTypeOptionShowOneLiner;
162 }
163
164 Flags &SetShowMembersOneLiner(bool value = true) {
165 if (value)
166 m_flags |= lldb::eTypeOptionShowOneLiner;
167 else
168 m_flags &= ~lldb::eTypeOptionShowOneLiner;
169 return *this;
170 }
171
172 bool GetHideItemNames() const {
173 return (m_flags & lldb::eTypeOptionHideNames) ==
174 lldb::eTypeOptionHideNames;
175 }
176
177 Flags &SetHideItemNames(bool value = true) {
178 if (value)
179 m_flags |= lldb::eTypeOptionHideNames;
180 else
181 m_flags &= ~lldb::eTypeOptionHideNames;
182 return *this;
183 }
184
185 bool GetNonCacheable() const {
186 return (m_flags & lldb::eTypeOptionNonCacheable) ==
187 lldb::eTypeOptionNonCacheable;
188 }
189
190 Flags &SetNonCacheable(bool value = true) {
191 if (value)
192 m_flags |= lldb::eTypeOptionNonCacheable;
193 else
194 m_flags &= ~lldb::eTypeOptionNonCacheable;
195 return *this;
196 }
197
198 uint32_t GetValue() { return m_flags; }
199
200 void SetValue(uint32_t value) { m_flags = value; }
201
202 private:
203 uint32_t m_flags = lldb::eTypeOptionCascade;
204 };
205
206 bool Cascades() const { return m_flags.GetCascades(); }
207
208 bool SkipsPointers() const { return m_flags.GetSkipPointers(); }
209
210 bool SkipsReferences() const { return m_flags.GetSkipReferences(); }
211
212 bool NonCacheable() const { return m_flags.GetNonCacheable(); }
213
214 virtual bool DoesPrintChildren(ValueObject *valobj) const {
215 return !m_flags.GetDontShowChildren();
216 }
217
218 virtual bool DoesPrintEmptyAggregates() const {
219 return !m_flags.GetHideEmptyAggregates();
220 }
221
222 virtual bool DoesPrintValue(ValueObject *valobj) const {
223 return !m_flags.GetDontShowValue();
224 }
225
226 bool IsOneLiner() const { return m_flags.GetShowMembersOneLiner(); }
227
228 virtual bool HideNames(ValueObject *valobj) const {
229 return m_flags.GetHideItemNames();
230 }
231
232 void SetCascades(bool value) { m_flags.SetCascades(value); }
233
234 void SetSkipsPointers(bool value) { m_flags.SetSkipPointers(value); }
235
236 void SetSkipsReferences(bool value) { m_flags.SetSkipReferences(value); }
237
238 virtual void SetDoesPrintChildren(bool value) {
239 m_flags.SetDontShowChildren(!value);
240 }
241
242 virtual void SetDoesPrintValue(bool value) {
243 m_flags.SetDontShowValue(!value);
244 }
245
246 void SetIsOneLiner(bool value) { m_flags.SetShowMembersOneLiner(value); }
247
248 virtual void SetHideNames(bool value) { m_flags.SetHideItemNames(value); }
249
250 virtual void SetNonCacheable(bool value) { m_flags.SetNonCacheable(value); }
251
252 uint32_t GetOptions() { return m_flags.GetValue(); }
253
254 void SetOptions(uint32_t value) { m_flags.SetValue(value); }
255
256 uint32_t GetPtrMatchDepth() { return m_ptr_match_depth; }
257
258 void SetPtrMatchDepth(uint32_t value) { m_ptr_match_depth = value; }
259
260 // we are using a ValueObject* instead of a ValueObjectSP because we do not
261 // need to hold on to this for extended periods of time and we trust the
262 // ValueObject to stay around for as long as it is required for us to
263 // generate its summary
264 virtual bool FormatObject(ValueObject *valobj, std::string &dest,
265 const TypeSummaryOptions &options) = 0;
266
267 virtual std::string GetDescription() = 0;
268
269 /// Get the name of the Type Summary Provider, either a C++ class, a summary
270 /// string, or a script function name.
271 virtual std::string GetName() = 0;
272
273 /// Get the name of the kind of Summary Provider, either c++, summary string,
274 /// script or python.
275 virtual std::string GetSummaryKindName();
276
277 uint32_t &GetRevision() { return m_my_revision; }
278
279 typedef std::shared_ptr<TypeSummaryImpl> SharedPointer;
280
281protected:
282 uint32_t m_my_revision = 0;
284
286 uint32_t ptr_match_depth = 1);
287
288private:
290 uint32_t m_ptr_match_depth = 1;
292 const TypeSummaryImpl &operator=(const TypeSummaryImpl &) = delete;
293};
294
295// simple string-based summaries, using ${var to show data
297 std::string m_format_str;
300
301 StringSummaryFormat(const TypeSummaryImpl::Flags &flags, const char *f,
302 uint32_t ptr_match_depth = 1);
303
304 ~StringSummaryFormat() override = default;
305
306 const char *GetSummaryString() const { return m_format_str.c_str(); }
307
308 void SetSummaryString(const char *f);
309
310 bool FormatObject(ValueObject *valobj, std::string &dest,
311 const TypeSummaryOptions &options) override;
312
313 std::string GetDescription() override;
314
315 std::string GetName() override;
316
317 static bool classof(const TypeSummaryImpl *S) {
318 return S->GetKind() == Kind::eSummaryString;
319 }
320
321private:
324};
325
326// summaries implemented via a C++ function
328 // we should convert these to SBValue and SBStream if we ever cross the
329 // boundary towards the external world
330 typedef std::function<bool(ValueObject &, Stream &,
331 const TypeSummaryOptions &)>
333
335 std::string m_description;
336
338 const char *description,
339 uint32_t ptr_match_depth = 1);
340
341 ~CXXFunctionSummaryFormat() override = default;
342
344
345 const char *GetTextualInfo() const { return m_description.c_str(); }
346
347 void SetBackendFunction(Callback cb_func) { m_impl = std::move(cb_func); }
348
349 void SetTextualInfo(const char *descr) {
350 if (descr)
351 m_description.assign(descr);
352 else
353 m_description.clear();
354 }
355
356 bool FormatObject(ValueObject *valobj, std::string &dest,
357 const TypeSummaryOptions &options) override;
358
359 std::string GetDescription() override;
360
361 static bool classof(const TypeSummaryImpl *S) {
362 return S->GetKind() == Kind::eCallback;
363 }
364
365 std::string GetName() override;
366
367 typedef std::shared_ptr<CXXFunctionSummaryFormat> SharedPointer;
368
369private:
373};
374
375// Python-based summaries, running script code to show data
377 std::string m_function_name;
378 std::string m_python_script;
381
383 const char *function_name,
384 const char *python_script = nullptr,
385 uint32_t ptr_match_depth = 1);
386
387 ~ScriptSummaryFormat() override = default;
388
389 const char *GetFunctionName() const { return m_function_name.c_str(); }
390
391 const char *GetPythonScript() const { return m_python_script.c_str(); }
392
393 void SetFunctionName(const char *function_name) {
394 if (function_name)
395 m_function_name.assign(function_name);
396 else
397 m_function_name.clear();
398 m_python_script.clear();
399 }
400
401 void SetPythonScript(const char *script) {
402 if (script)
403 m_python_script.assign(script);
404 else
405 m_python_script.clear();
406 }
407
408 bool FormatObject(ValueObject *valobj, std::string &dest,
409 const TypeSummaryOptions &options) override;
410
411 std::string GetDescription() override;
412
413 std::string GetName() override;
414
415 static bool classof(const TypeSummaryImpl *S) {
416 return S->GetKind() == Kind::eScript;
417 }
418
419 typedef std::shared_ptr<ScriptSummaryFormat> SharedPointer;
420
421private:
424};
425
426/// A summary formatter that is defined in LLDB formmater bytecode.
428 std::unique_ptr<llvm::MemoryBuffer> m_bytecode;
429
430public:
432 std::unique_ptr<llvm::MemoryBuffer> bytecode);
433 bool FormatObject(ValueObject *valobj, std::string &dest,
434 const TypeSummaryOptions &options) override;
435 std::string GetDescription() override;
436 std::string GetName() override;
437 static bool classof(const TypeSummaryImpl *S) {
438 return S->GetKind() == Kind::eBytecode;
439 }
440};
441
442} // namespace lldb_private
443
444#endif // LLDB_DATAFORMATTERS_TYPESUMMARY_H
static bool classof(const TypeSummaryImpl *S)
std::string GetDescription() override
BytecodeSummaryFormat(const TypeSummaryImpl::Flags &flags, std::unique_ptr< llvm::MemoryBuffer > bytecode)
std::unique_ptr< llvm::MemoryBuffer > m_bytecode
std::string GetName() override
Get the name of the Type Summary Provider, either a C++ class, a summary string, or a script function...
bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options) override
An error handling class.
Definition Status.h:118
A stream class that can stream formatted output to a file.
Definition Stream.h:28
std::shared_ptr< Object > ObjectSP
Flags & SetNonCacheable(bool value=true)
Flags & operator=(const uint32_t &rhs)
Definition TypeSummary.h:72
Flags & SetCascades(bool value=true)
Definition TypeSummary.h:86
Flags & SetHideEmptyAggregates(bool value=true)
Flags & SetSkipPointers(bool value=true)
Definition TypeSummary.h:99
Flags & SetHideItemNames(bool value=true)
Flags & operator=(const Flags &rhs)
Definition TypeSummary.h:65
Flags & SetDontShowChildren(bool value=true)
Flags & SetSkipReferences(bool value=true)
Flags & SetShowMembersOneLiner(bool value=true)
Flags & SetDontShowValue(bool value=true)
void SetIsOneLiner(bool value)
virtual bool HideNames(ValueObject *valobj) const
TypeSummaryImpl(const TypeSummaryImpl &)=delete
virtual ~TypeSummaryImpl()=default
virtual std::string GetName()=0
Get the name of the Type Summary Provider, either a C++ class, a summary string, or a script function...
virtual std::string GetSummaryKindName()
Get the name of the kind of Summary Provider, either c++, summary string, script or python.
virtual void SetDoesPrintValue(bool value)
void SetSkipsReferences(bool value)
virtual bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options)=0
void SetSkipsPointers(bool value)
virtual bool DoesPrintEmptyAggregates() const
virtual std::string GetDescription()=0
void SetOptions(uint32_t value)
void SetPtrMatchDepth(uint32_t value)
virtual void SetDoesPrintChildren(bool value)
const TypeSummaryImpl & operator=(const TypeSummaryImpl &)=delete
virtual void SetNonCacheable(bool value)
virtual void SetHideNames(bool value)
TypeSummaryImpl(Kind kind, const TypeSummaryImpl::Flags &flags, uint32_t ptr_match_depth=1)
virtual bool DoesPrintValue(ValueObject *valobj) const
std::shared_ptr< TypeSummaryImpl > SharedPointer
virtual bool DoesPrintChildren(ValueObject *valobj) const
lldb::TypeSummaryCapping m_capping
Definition TypeSummary.h:46
lldb::LanguageType GetLanguage() const
TypeSummaryOptions & SetCapping(lldb::TypeSummaryCapping)
TypeSummaryOptions & SetLanguage(lldb::LanguageType)
lldb::TypeSummaryCapping GetCapping() const
A class that represents a running process on the host machine.
TypeSummaryCapping
Whether a summary should cap how much data it returns to users or not.
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
std::shared_ptr< CXXFunctionSummaryFormat > SharedPointer
CXXFunctionSummaryFormat(const CXXFunctionSummaryFormat &)=delete
std::function< bool(ValueObject &, Stream &, const TypeSummaryOptions &)> Callback
void SetTextualInfo(const char *descr)
~CXXFunctionSummaryFormat() override=default
const CXXFunctionSummaryFormat & operator=(const CXXFunctionSummaryFormat &)=delete
bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options) override
void SetBackendFunction(Callback cb_func)
CXXFunctionSummaryFormat(const TypeSummaryImpl::Flags &flags, Callback impl, const char *description, uint32_t ptr_match_depth=1)
static bool classof(const TypeSummaryImpl *S)
std::string GetName() override
Get the name of the Type Summary Provider, either a C++ class, a summary string, or a script function...
static bool classof(const TypeSummaryImpl *S)
~ScriptSummaryFormat() override=default
const char * GetPythonScript() const
std::string GetDescription() override
bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options) override
const char * GetFunctionName() const
ScriptSummaryFormat(const ScriptSummaryFormat &)=delete
void SetPythonScript(const char *script)
const ScriptSummaryFormat & operator=(const ScriptSummaryFormat &)=delete
std::shared_ptr< ScriptSummaryFormat > SharedPointer
StructuredData::ObjectSP m_script_function_sp
void SetFunctionName(const char *function_name)
std::string GetName() override
Get the name of the Type Summary Provider, either a C++ class, a summary string, or a script function...
ScriptSummaryFormat(const TypeSummaryImpl::Flags &flags, const char *function_name, const char *python_script=nullptr, uint32_t ptr_match_depth=1)
const StringSummaryFormat & operator=(const StringSummaryFormat &)=delete
~StringSummaryFormat() override=default
bool FormatObject(ValueObject *valobj, std::string &dest, const TypeSummaryOptions &options) override
StringSummaryFormat(const TypeSummaryImpl::Flags &flags, const char *f, uint32_t ptr_match_depth=1)
std::string GetName() override
Get the name of the Type Summary Provider, either a C++ class, a summary string, or a script function...
static bool classof(const TypeSummaryImpl *S)
StringSummaryFormat(const StringSummaryFormat &)=delete
const char * GetSummaryString() const
void SetSummaryString(const char *f)
std::string GetDescription() override