1 //===-- TypeSummary.h -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef lldb_TypeSummary_h_
11 #define lldb_TypeSummary_h_
21 // Other libraries and framework includes
23 #include "lldb/lldb-enumerations.h"
24 #include "lldb/lldb-public.h"
26 #include "lldb/Core/FormatEntity.h"
27 #include "lldb/Utility/Status.h"
28 #include "lldb/Utility/StructuredData.h"
30 namespace lldb_private {
31 class TypeSummaryOptions {
34 TypeSummaryOptions(const TypeSummaryOptions &rhs);
36 ~TypeSummaryOptions() = default;
38 TypeSummaryOptions &operator=(const TypeSummaryOptions &rhs);
40 lldb::LanguageType GetLanguage() const;
42 lldb::TypeSummaryCapping GetCapping() const;
44 TypeSummaryOptions &SetLanguage(lldb::LanguageType);
46 TypeSummaryOptions &SetCapping(lldb::TypeSummaryCapping);
49 lldb::LanguageType m_lang;
50 lldb::TypeSummaryCapping m_capping;
53 class TypeSummaryImpl {
55 enum class Kind { eSummaryString, eScript, eCallback, eInternal };
57 virtual ~TypeSummaryImpl() = default;
59 Kind GetKind() const { return m_kind; }
63 Flags() : m_flags(lldb::eTypeOptionCascade) {}
65 Flags(const Flags &other) : m_flags(other.m_flags) {}
67 Flags(uint32_t value) : m_flags(value) {}
69 Flags &operator=(const Flags &rhs) {
71 m_flags = rhs.m_flags;
76 Flags &operator=(const uint32_t &rhs) {
86 bool GetCascades() const {
87 return (m_flags & lldb::eTypeOptionCascade) == lldb::eTypeOptionCascade;
90 Flags &SetCascades(bool value = true) {
92 m_flags |= lldb::eTypeOptionCascade;
94 m_flags &= ~lldb::eTypeOptionCascade;
98 bool GetSkipPointers() const {
99 return (m_flags & lldb::eTypeOptionSkipPointers) ==
100 lldb::eTypeOptionSkipPointers;
103 Flags &SetSkipPointers(bool value = true) {
105 m_flags |= lldb::eTypeOptionSkipPointers;
107 m_flags &= ~lldb::eTypeOptionSkipPointers;
111 bool GetSkipReferences() const {
112 return (m_flags & lldb::eTypeOptionSkipReferences) ==
113 lldb::eTypeOptionSkipReferences;
116 Flags &SetSkipReferences(bool value = true) {
118 m_flags |= lldb::eTypeOptionSkipReferences;
120 m_flags &= ~lldb::eTypeOptionSkipReferences;
124 bool GetDontShowChildren() const {
125 return (m_flags & lldb::eTypeOptionHideChildren) ==
126 lldb::eTypeOptionHideChildren;
129 Flags &SetDontShowChildren(bool value = true) {
131 m_flags |= lldb::eTypeOptionHideChildren;
133 m_flags &= ~lldb::eTypeOptionHideChildren;
137 bool GetHideEmptyAggregates() const {
138 return (m_flags & lldb::eTypeOptionHideEmptyAggregates) ==
139 lldb::eTypeOptionHideEmptyAggregates;
142 Flags &SetHideEmptyAggregates(bool value = true) {
144 m_flags |= lldb::eTypeOptionHideEmptyAggregates;
146 m_flags &= ~lldb::eTypeOptionHideEmptyAggregates;
150 bool GetDontShowValue() const {
151 return (m_flags & lldb::eTypeOptionHideValue) ==
152 lldb::eTypeOptionHideValue;
155 Flags &SetDontShowValue(bool value = true) {
157 m_flags |= lldb::eTypeOptionHideValue;
159 m_flags &= ~lldb::eTypeOptionHideValue;
163 bool GetShowMembersOneLiner() const {
164 return (m_flags & lldb::eTypeOptionShowOneLiner) ==
165 lldb::eTypeOptionShowOneLiner;
168 Flags &SetShowMembersOneLiner(bool value = true) {
170 m_flags |= lldb::eTypeOptionShowOneLiner;
172 m_flags &= ~lldb::eTypeOptionShowOneLiner;
176 bool GetHideItemNames() const {
177 return (m_flags & lldb::eTypeOptionHideNames) ==
178 lldb::eTypeOptionHideNames;
181 Flags &SetHideItemNames(bool value = true) {
183 m_flags |= lldb::eTypeOptionHideNames;
185 m_flags &= ~lldb::eTypeOptionHideNames;
189 bool GetNonCacheable() const {
190 return (m_flags & lldb::eTypeOptionNonCacheable) ==
191 lldb::eTypeOptionNonCacheable;
194 Flags &SetNonCacheable(bool value = true) {
196 m_flags |= lldb::eTypeOptionNonCacheable;
198 m_flags &= ~lldb::eTypeOptionNonCacheable;
202 uint32_t GetValue() { return m_flags; }
204 void SetValue(uint32_t value) { m_flags = value; }
210 bool Cascades() const { return m_flags.GetCascades(); }
212 bool SkipsPointers() const { return m_flags.GetSkipPointers(); }
214 bool SkipsReferences() const { return m_flags.GetSkipReferences(); }
216 bool NonCacheable() const { return m_flags.GetNonCacheable(); }
218 virtual bool DoesPrintChildren(ValueObject *valobj) const {
219 return !m_flags.GetDontShowChildren();
222 virtual bool DoesPrintEmptyAggregates() const {
223 return !m_flags.GetHideEmptyAggregates();
226 virtual bool DoesPrintValue(ValueObject *valobj) const {
227 return !m_flags.GetDontShowValue();
230 bool IsOneLiner() const { return m_flags.GetShowMembersOneLiner(); }
232 virtual bool HideNames(ValueObject *valobj) const {
233 return m_flags.GetHideItemNames();
236 void SetCascades(bool value) { m_flags.SetCascades(value); }
238 void SetSkipsPointers(bool value) { m_flags.SetSkipPointers(value); }
240 void SetSkipsReferences(bool value) { m_flags.SetSkipReferences(value); }
242 virtual void SetDoesPrintChildren(bool value) {
243 m_flags.SetDontShowChildren(!value);
246 virtual void SetDoesPrintValue(bool value) {
247 m_flags.SetDontShowValue(!value);
250 void SetIsOneLiner(bool value) { m_flags.SetShowMembersOneLiner(value); }
252 virtual void SetHideNames(bool value) { m_flags.SetHideItemNames(value); }
254 virtual void SetNonCacheable(bool value) { m_flags.SetNonCacheable(value); }
256 uint32_t GetOptions() { return m_flags.GetValue(); }
258 void SetOptions(uint32_t value) { m_flags.SetValue(value); }
260 // we are using a ValueObject* instead of a ValueObjectSP because we do not
261 // need to hold on to this for
262 // extended periods of time and we trust the ValueObject to stay around for as
263 // long as it is required
264 // for us to generate its summary
265 virtual bool FormatObject(ValueObject *valobj, std::string &dest,
266 const TypeSummaryOptions &options) = 0;
268 virtual std::string GetDescription() = 0;
270 uint32_t &GetRevision() { return m_my_revision; }
272 typedef std::shared_ptr<TypeSummaryImpl> SharedPointer;
275 uint32_t m_my_revision;
278 TypeSummaryImpl(Kind kind, const TypeSummaryImpl::Flags &flags);
282 DISALLOW_COPY_AND_ASSIGN(TypeSummaryImpl);
285 // simple string-based summaries, using ${var to show data
286 struct StringSummaryFormat : public TypeSummaryImpl {
287 std::string m_format_str;
288 FormatEntity::Entry m_format;
291 StringSummaryFormat(const TypeSummaryImpl::Flags &flags, const char *f);
293 ~StringSummaryFormat() override = default;
295 const char *GetSummaryString() const { return m_format_str.c_str(); }
297 void SetSummaryString(const char *f);
299 bool FormatObject(ValueObject *valobj, std::string &dest,
300 const TypeSummaryOptions &options) override;
302 std::string GetDescription() override;
304 static bool classof(const TypeSummaryImpl *S) {
305 return S->GetKind() == Kind::eSummaryString;
309 DISALLOW_COPY_AND_ASSIGN(StringSummaryFormat);
312 // summaries implemented via a C++ function
313 struct CXXFunctionSummaryFormat : public TypeSummaryImpl {
314 // we should convert these to SBValue and SBStream if we ever cross
315 // the boundary towards the external world
316 typedef std::function<bool(ValueObject &, Stream &,
317 const TypeSummaryOptions &)>
321 std::string m_description;
323 CXXFunctionSummaryFormat(const TypeSummaryImpl::Flags &flags, Callback impl,
324 const char *description);
326 ~CXXFunctionSummaryFormat() override = default;
328 Callback GetBackendFunction() const { return m_impl; }
330 const char *GetTextualInfo() const { return m_description.c_str(); }
332 void SetBackendFunction(Callback cb_func) { m_impl = cb_func; }
334 void SetTextualInfo(const char *descr) {
336 m_description.assign(descr);
338 m_description.clear();
341 bool FormatObject(ValueObject *valobj, std::string &dest,
342 const TypeSummaryOptions &options) override;
344 std::string GetDescription() override;
346 static bool classof(const TypeSummaryImpl *S) {
347 return S->GetKind() == Kind::eCallback;
350 typedef std::shared_ptr<CXXFunctionSummaryFormat> SharedPointer;
353 DISALLOW_COPY_AND_ASSIGN(CXXFunctionSummaryFormat);
356 // Python-based summaries, running script code to show data
357 struct ScriptSummaryFormat : public TypeSummaryImpl {
358 std::string m_function_name;
359 std::string m_python_script;
360 StructuredData::ObjectSP m_script_function_sp;
362 ScriptSummaryFormat(const TypeSummaryImpl::Flags &flags,
363 const char *function_name,
364 const char *python_script = nullptr);
366 ~ScriptSummaryFormat() override = default;
368 const char *GetFunctionName() const { return m_function_name.c_str(); }
370 const char *GetPythonScript() const { return m_python_script.c_str(); }
372 void SetFunctionName(const char *function_name) {
374 m_function_name.assign(function_name);
376 m_function_name.clear();
377 m_python_script.clear();
380 void SetPythonScript(const char *script) {
382 m_python_script.assign(script);
384 m_python_script.clear();
387 bool FormatObject(ValueObject *valobj, std::string &dest,
388 const TypeSummaryOptions &options) override;
390 std::string GetDescription() override;
392 static bool classof(const TypeSummaryImpl *S) {
393 return S->GetKind() == Kind::eScript;
396 typedef std::shared_ptr<ScriptSummaryFormat> SharedPointer;
399 DISALLOW_COPY_AND_ASSIGN(ScriptSummaryFormat);
401 } // namespace lldb_private
403 #endif // lldb_TypeSummary_h_