1 //===-- TypeSummary.h -------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef lldb_TypeSummary_h_
10 #define lldb_TypeSummary_h_
18 #include "lldb/lldb-enumerations.h"
19 #include "lldb/lldb-public.h"
21 #include "lldb/Core/FormatEntity.h"
22 #include "lldb/Utility/Status.h"
23 #include "lldb/Utility/StructuredData.h"
25 namespace lldb_private {
26 class TypeSummaryOptions {
30 ~TypeSummaryOptions() = default;
32 lldb::LanguageType GetLanguage() const;
34 lldb::TypeSummaryCapping GetCapping() const;
36 TypeSummaryOptions &SetLanguage(lldb::LanguageType);
38 TypeSummaryOptions &SetCapping(lldb::TypeSummaryCapping);
41 lldb::LanguageType m_lang;
42 lldb::TypeSummaryCapping m_capping;
45 class TypeSummaryImpl {
47 enum class Kind { eSummaryString, eScript, eCallback, eInternal };
49 virtual ~TypeSummaryImpl() = default;
51 Kind GetKind() const { return m_kind; }
55 Flags() : m_flags(lldb::eTypeOptionCascade) {}
57 Flags(const Flags &other) : m_flags(other.m_flags) {}
59 Flags(uint32_t value) : m_flags(value) {}
61 Flags &operator=(const Flags &rhs) {
63 m_flags = rhs.m_flags;
68 Flags &operator=(const uint32_t &rhs) {
78 bool GetCascades() const {
79 return (m_flags & lldb::eTypeOptionCascade) == lldb::eTypeOptionCascade;
82 Flags &SetCascades(bool value = true) {
84 m_flags |= lldb::eTypeOptionCascade;
86 m_flags &= ~lldb::eTypeOptionCascade;
90 bool GetSkipPointers() const {
91 return (m_flags & lldb::eTypeOptionSkipPointers) ==
92 lldb::eTypeOptionSkipPointers;
95 Flags &SetSkipPointers(bool value = true) {
97 m_flags |= lldb::eTypeOptionSkipPointers;
99 m_flags &= ~lldb::eTypeOptionSkipPointers;
103 bool GetSkipReferences() const {
104 return (m_flags & lldb::eTypeOptionSkipReferences) ==
105 lldb::eTypeOptionSkipReferences;
108 Flags &SetSkipReferences(bool value = true) {
110 m_flags |= lldb::eTypeOptionSkipReferences;
112 m_flags &= ~lldb::eTypeOptionSkipReferences;
116 bool GetDontShowChildren() const {
117 return (m_flags & lldb::eTypeOptionHideChildren) ==
118 lldb::eTypeOptionHideChildren;
121 Flags &SetDontShowChildren(bool value = true) {
123 m_flags |= lldb::eTypeOptionHideChildren;
125 m_flags &= ~lldb::eTypeOptionHideChildren;
129 bool GetHideEmptyAggregates() const {
130 return (m_flags & lldb::eTypeOptionHideEmptyAggregates) ==
131 lldb::eTypeOptionHideEmptyAggregates;
134 Flags &SetHideEmptyAggregates(bool value = true) {
136 m_flags |= lldb::eTypeOptionHideEmptyAggregates;
138 m_flags &= ~lldb::eTypeOptionHideEmptyAggregates;
142 bool GetDontShowValue() const {
143 return (m_flags & lldb::eTypeOptionHideValue) ==
144 lldb::eTypeOptionHideValue;
147 Flags &SetDontShowValue(bool value = true) {
149 m_flags |= lldb::eTypeOptionHideValue;
151 m_flags &= ~lldb::eTypeOptionHideValue;
155 bool GetShowMembersOneLiner() const {
156 return (m_flags & lldb::eTypeOptionShowOneLiner) ==
157 lldb::eTypeOptionShowOneLiner;
160 Flags &SetShowMembersOneLiner(bool value = true) {
162 m_flags |= lldb::eTypeOptionShowOneLiner;
164 m_flags &= ~lldb::eTypeOptionShowOneLiner;
168 bool GetHideItemNames() const {
169 return (m_flags & lldb::eTypeOptionHideNames) ==
170 lldb::eTypeOptionHideNames;
173 Flags &SetHideItemNames(bool value = true) {
175 m_flags |= lldb::eTypeOptionHideNames;
177 m_flags &= ~lldb::eTypeOptionHideNames;
181 bool GetNonCacheable() const {
182 return (m_flags & lldb::eTypeOptionNonCacheable) ==
183 lldb::eTypeOptionNonCacheable;
186 Flags &SetNonCacheable(bool value = true) {
188 m_flags |= lldb::eTypeOptionNonCacheable;
190 m_flags &= ~lldb::eTypeOptionNonCacheable;
194 uint32_t GetValue() { return m_flags; }
196 void SetValue(uint32_t value) { m_flags = value; }
202 bool Cascades() const { return m_flags.GetCascades(); }
204 bool SkipsPointers() const { return m_flags.GetSkipPointers(); }
206 bool SkipsReferences() const { return m_flags.GetSkipReferences(); }
208 bool NonCacheable() const { return m_flags.GetNonCacheable(); }
210 virtual bool DoesPrintChildren(ValueObject *valobj) const {
211 return !m_flags.GetDontShowChildren();
214 virtual bool DoesPrintEmptyAggregates() const {
215 return !m_flags.GetHideEmptyAggregates();
218 virtual bool DoesPrintValue(ValueObject *valobj) const {
219 return !m_flags.GetDontShowValue();
222 bool IsOneLiner() const { return m_flags.GetShowMembersOneLiner(); }
224 virtual bool HideNames(ValueObject *valobj) const {
225 return m_flags.GetHideItemNames();
228 void SetCascades(bool value) { m_flags.SetCascades(value); }
230 void SetSkipsPointers(bool value) { m_flags.SetSkipPointers(value); }
232 void SetSkipsReferences(bool value) { m_flags.SetSkipReferences(value); }
234 virtual void SetDoesPrintChildren(bool value) {
235 m_flags.SetDontShowChildren(!value);
238 virtual void SetDoesPrintValue(bool value) {
239 m_flags.SetDontShowValue(!value);
242 void SetIsOneLiner(bool value) { m_flags.SetShowMembersOneLiner(value); }
244 virtual void SetHideNames(bool value) { m_flags.SetHideItemNames(value); }
246 virtual void SetNonCacheable(bool value) { m_flags.SetNonCacheable(value); }
248 uint32_t GetOptions() { return m_flags.GetValue(); }
250 void SetOptions(uint32_t value) { m_flags.SetValue(value); }
252 // we are using a ValueObject* instead of a ValueObjectSP because we do not
253 // need to hold on to this for extended periods of time and we trust the
254 // ValueObject to stay around for as long as it is required for us to
255 // generate its summary
256 virtual bool FormatObject(ValueObject *valobj, std::string &dest,
257 const TypeSummaryOptions &options) = 0;
259 virtual std::string GetDescription() = 0;
261 uint32_t &GetRevision() { return m_my_revision; }
263 typedef std::shared_ptr<TypeSummaryImpl> SharedPointer;
266 uint32_t m_my_revision;
269 TypeSummaryImpl(Kind kind, const TypeSummaryImpl::Flags &flags);
273 DISALLOW_COPY_AND_ASSIGN(TypeSummaryImpl);
276 // simple string-based summaries, using ${var to show data
277 struct StringSummaryFormat : public TypeSummaryImpl {
278 std::string m_format_str;
279 FormatEntity::Entry m_format;
282 StringSummaryFormat(const TypeSummaryImpl::Flags &flags, const char *f);
284 ~StringSummaryFormat() override = default;
286 const char *GetSummaryString() const { return m_format_str.c_str(); }
288 void SetSummaryString(const char *f);
290 bool FormatObject(ValueObject *valobj, std::string &dest,
291 const TypeSummaryOptions &options) override;
293 std::string GetDescription() override;
295 static bool classof(const TypeSummaryImpl *S) {
296 return S->GetKind() == Kind::eSummaryString;
300 DISALLOW_COPY_AND_ASSIGN(StringSummaryFormat);
303 // summaries implemented via a C++ function
304 struct CXXFunctionSummaryFormat : public TypeSummaryImpl {
305 // we should convert these to SBValue and SBStream if we ever cross the
306 // boundary towards the external world
307 typedef std::function<bool(ValueObject &, Stream &,
308 const TypeSummaryOptions &)>
312 std::string m_description;
314 CXXFunctionSummaryFormat(const TypeSummaryImpl::Flags &flags, Callback impl,
315 const char *description);
317 ~CXXFunctionSummaryFormat() override = default;
319 Callback GetBackendFunction() const { return m_impl; }
321 const char *GetTextualInfo() const { return m_description.c_str(); }
323 void SetBackendFunction(Callback cb_func) { m_impl = cb_func; }
325 void SetTextualInfo(const char *descr) {
327 m_description.assign(descr);
329 m_description.clear();
332 bool FormatObject(ValueObject *valobj, std::string &dest,
333 const TypeSummaryOptions &options) override;
335 std::string GetDescription() override;
337 static bool classof(const TypeSummaryImpl *S) {
338 return S->GetKind() == Kind::eCallback;
341 typedef std::shared_ptr<CXXFunctionSummaryFormat> SharedPointer;
344 DISALLOW_COPY_AND_ASSIGN(CXXFunctionSummaryFormat);
347 // Python-based summaries, running script code to show data
348 struct ScriptSummaryFormat : public TypeSummaryImpl {
349 std::string m_function_name;
350 std::string m_python_script;
351 StructuredData::ObjectSP m_script_function_sp;
353 ScriptSummaryFormat(const TypeSummaryImpl::Flags &flags,
354 const char *function_name,
355 const char *python_script = nullptr);
357 ~ScriptSummaryFormat() override = default;
359 const char *GetFunctionName() const { return m_function_name.c_str(); }
361 const char *GetPythonScript() const { return m_python_script.c_str(); }
363 void SetFunctionName(const char *function_name) {
365 m_function_name.assign(function_name);
367 m_function_name.clear();
368 m_python_script.clear();
371 void SetPythonScript(const char *script) {
373 m_python_script.assign(script);
375 m_python_script.clear();
378 bool FormatObject(ValueObject *valobj, std::string &dest,
379 const TypeSummaryOptions &options) override;
381 std::string GetDescription() override;
383 static bool classof(const TypeSummaryImpl *S) {
384 return S->GetKind() == Kind::eScript;
387 typedef std::shared_ptr<ScriptSummaryFormat> SharedPointer;
390 DISALLOW_COPY_AND_ASSIGN(ScriptSummaryFormat);
392 } // namespace lldb_private
394 #endif // lldb_TypeSummary_h_