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_
19 #include "lldb/lldb-enumerations.h"
20 #include "lldb/lldb-public.h"
22 #include "lldb/Core/FormatEntity.h"
23 #include "lldb/Utility/Status.h"
24 #include "lldb/Utility/StructuredData.h"
26 namespace lldb_private {
27 class TypeSummaryOptions {
30 TypeSummaryOptions(const TypeSummaryOptions &rhs);
32 ~TypeSummaryOptions() = default;
34 TypeSummaryOptions &operator=(const TypeSummaryOptions &rhs);
36 lldb::LanguageType GetLanguage() const;
38 lldb::TypeSummaryCapping GetCapping() const;
40 TypeSummaryOptions &SetLanguage(lldb::LanguageType);
42 TypeSummaryOptions &SetCapping(lldb::TypeSummaryCapping);
45 lldb::LanguageType m_lang;
46 lldb::TypeSummaryCapping m_capping;
49 class TypeSummaryImpl {
51 enum class Kind { eSummaryString, eScript, eCallback, eInternal };
53 virtual ~TypeSummaryImpl() = default;
55 Kind GetKind() const { return m_kind; }
59 Flags() : m_flags(lldb::eTypeOptionCascade) {}
61 Flags(const Flags &other) : m_flags(other.m_flags) {}
63 Flags(uint32_t value) : m_flags(value) {}
65 Flags &operator=(const Flags &rhs) {
67 m_flags = rhs.m_flags;
72 Flags &operator=(const uint32_t &rhs) {
82 bool GetCascades() const {
83 return (m_flags & lldb::eTypeOptionCascade) == lldb::eTypeOptionCascade;
86 Flags &SetCascades(bool value = true) {
88 m_flags |= lldb::eTypeOptionCascade;
90 m_flags &= ~lldb::eTypeOptionCascade;
94 bool GetSkipPointers() const {
95 return (m_flags & lldb::eTypeOptionSkipPointers) ==
96 lldb::eTypeOptionSkipPointers;
99 Flags &SetSkipPointers(bool value = true) {
101 m_flags |= lldb::eTypeOptionSkipPointers;
103 m_flags &= ~lldb::eTypeOptionSkipPointers;
107 bool GetSkipReferences() const {
108 return (m_flags & lldb::eTypeOptionSkipReferences) ==
109 lldb::eTypeOptionSkipReferences;
112 Flags &SetSkipReferences(bool value = true) {
114 m_flags |= lldb::eTypeOptionSkipReferences;
116 m_flags &= ~lldb::eTypeOptionSkipReferences;
120 bool GetDontShowChildren() const {
121 return (m_flags & lldb::eTypeOptionHideChildren) ==
122 lldb::eTypeOptionHideChildren;
125 Flags &SetDontShowChildren(bool value = true) {
127 m_flags |= lldb::eTypeOptionHideChildren;
129 m_flags &= ~lldb::eTypeOptionHideChildren;
133 bool GetHideEmptyAggregates() const {
134 return (m_flags & lldb::eTypeOptionHideEmptyAggregates) ==
135 lldb::eTypeOptionHideEmptyAggregates;
138 Flags &SetHideEmptyAggregates(bool value = true) {
140 m_flags |= lldb::eTypeOptionHideEmptyAggregates;
142 m_flags &= ~lldb::eTypeOptionHideEmptyAggregates;
146 bool GetDontShowValue() const {
147 return (m_flags & lldb::eTypeOptionHideValue) ==
148 lldb::eTypeOptionHideValue;
151 Flags &SetDontShowValue(bool value = true) {
153 m_flags |= lldb::eTypeOptionHideValue;
155 m_flags &= ~lldb::eTypeOptionHideValue;
159 bool GetShowMembersOneLiner() const {
160 return (m_flags & lldb::eTypeOptionShowOneLiner) ==
161 lldb::eTypeOptionShowOneLiner;
164 Flags &SetShowMembersOneLiner(bool value = true) {
166 m_flags |= lldb::eTypeOptionShowOneLiner;
168 m_flags &= ~lldb::eTypeOptionShowOneLiner;
172 bool GetHideItemNames() const {
173 return (m_flags & lldb::eTypeOptionHideNames) ==
174 lldb::eTypeOptionHideNames;
177 Flags &SetHideItemNames(bool value = true) {
179 m_flags |= lldb::eTypeOptionHideNames;
181 m_flags &= ~lldb::eTypeOptionHideNames;
185 bool GetNonCacheable() const {
186 return (m_flags & lldb::eTypeOptionNonCacheable) ==
187 lldb::eTypeOptionNonCacheable;
190 Flags &SetNonCacheable(bool value = true) {
192 m_flags |= lldb::eTypeOptionNonCacheable;
194 m_flags &= ~lldb::eTypeOptionNonCacheable;
198 uint32_t GetValue() { return m_flags; }
200 void SetValue(uint32_t value) { m_flags = value; }
206 bool Cascades() const { return m_flags.GetCascades(); }
208 bool SkipsPointers() const { return m_flags.GetSkipPointers(); }
210 bool SkipsReferences() const { return m_flags.GetSkipReferences(); }
212 bool NonCacheable() const { return m_flags.GetNonCacheable(); }
214 virtual bool DoesPrintChildren(ValueObject *valobj) const {
215 return !m_flags.GetDontShowChildren();
218 virtual bool DoesPrintEmptyAggregates() const {
219 return !m_flags.GetHideEmptyAggregates();
222 virtual bool DoesPrintValue(ValueObject *valobj) const {
223 return !m_flags.GetDontShowValue();
226 bool IsOneLiner() const { return m_flags.GetShowMembersOneLiner(); }
228 virtual bool HideNames(ValueObject *valobj) const {
229 return m_flags.GetHideItemNames();
232 void SetCascades(bool value) { m_flags.SetCascades(value); }
234 void SetSkipsPointers(bool value) { m_flags.SetSkipPointers(value); }
236 void SetSkipsReferences(bool value) { m_flags.SetSkipReferences(value); }
238 virtual void SetDoesPrintChildren(bool value) {
239 m_flags.SetDontShowChildren(!value);
242 virtual void SetDoesPrintValue(bool value) {
243 m_flags.SetDontShowValue(!value);
246 void SetIsOneLiner(bool value) { m_flags.SetShowMembersOneLiner(value); }
248 virtual void SetHideNames(bool value) { m_flags.SetHideItemNames(value); }
250 virtual void SetNonCacheable(bool value) { m_flags.SetNonCacheable(value); }
252 uint32_t GetOptions() { return m_flags.GetValue(); }
254 void SetOptions(uint32_t value) { m_flags.SetValue(value); }
256 // we are using a ValueObject* instead of a ValueObjectSP because we do not
257 // need to hold on to this for extended periods of time and we trust the
258 // ValueObject to stay around for as long as it is required for us to
259 // generate its summary
260 virtual bool FormatObject(ValueObject *valobj, std::string &dest,
261 const TypeSummaryOptions &options) = 0;
263 virtual std::string GetDescription() = 0;
265 uint32_t &GetRevision() { return m_my_revision; }
267 typedef std::shared_ptr<TypeSummaryImpl> SharedPointer;
270 uint32_t m_my_revision;
273 TypeSummaryImpl(Kind kind, const TypeSummaryImpl::Flags &flags);
277 DISALLOW_COPY_AND_ASSIGN(TypeSummaryImpl);
280 // simple string-based summaries, using ${var to show data
281 struct StringSummaryFormat : public TypeSummaryImpl {
282 std::string m_format_str;
283 FormatEntity::Entry m_format;
286 StringSummaryFormat(const TypeSummaryImpl::Flags &flags, const char *f);
288 ~StringSummaryFormat() override = default;
290 const char *GetSummaryString() const { return m_format_str.c_str(); }
292 void SetSummaryString(const char *f);
294 bool FormatObject(ValueObject *valobj, std::string &dest,
295 const TypeSummaryOptions &options) override;
297 std::string GetDescription() override;
299 static bool classof(const TypeSummaryImpl *S) {
300 return S->GetKind() == Kind::eSummaryString;
304 DISALLOW_COPY_AND_ASSIGN(StringSummaryFormat);
307 // summaries implemented via a C++ function
308 struct CXXFunctionSummaryFormat : public TypeSummaryImpl {
309 // we should convert these to SBValue and SBStream if we ever cross the
310 // boundary towards the external world
311 typedef std::function<bool(ValueObject &, Stream &,
312 const TypeSummaryOptions &)>
316 std::string m_description;
318 CXXFunctionSummaryFormat(const TypeSummaryImpl::Flags &flags, Callback impl,
319 const char *description);
321 ~CXXFunctionSummaryFormat() override = default;
323 Callback GetBackendFunction() const { return m_impl; }
325 const char *GetTextualInfo() const { return m_description.c_str(); }
327 void SetBackendFunction(Callback cb_func) { m_impl = cb_func; }
329 void SetTextualInfo(const char *descr) {
331 m_description.assign(descr);
333 m_description.clear();
336 bool FormatObject(ValueObject *valobj, std::string &dest,
337 const TypeSummaryOptions &options) override;
339 std::string GetDescription() override;
341 static bool classof(const TypeSummaryImpl *S) {
342 return S->GetKind() == Kind::eCallback;
345 typedef std::shared_ptr<CXXFunctionSummaryFormat> SharedPointer;
348 DISALLOW_COPY_AND_ASSIGN(CXXFunctionSummaryFormat);
351 // Python-based summaries, running script code to show data
352 struct ScriptSummaryFormat : public TypeSummaryImpl {
353 std::string m_function_name;
354 std::string m_python_script;
355 StructuredData::ObjectSP m_script_function_sp;
357 ScriptSummaryFormat(const TypeSummaryImpl::Flags &flags,
358 const char *function_name,
359 const char *python_script = nullptr);
361 ~ScriptSummaryFormat() override = default;
363 const char *GetFunctionName() const { return m_function_name.c_str(); }
365 const char *GetPythonScript() const { return m_python_script.c_str(); }
367 void SetFunctionName(const char *function_name) {
369 m_function_name.assign(function_name);
371 m_function_name.clear();
372 m_python_script.clear();
375 void SetPythonScript(const char *script) {
377 m_python_script.assign(script);
379 m_python_script.clear();
382 bool FormatObject(ValueObject *valobj, std::string &dest,
383 const TypeSummaryOptions &options) override;
385 std::string GetDescription() override;
387 static bool classof(const TypeSummaryImpl *S) {
388 return S->GetKind() == Kind::eScript;
391 typedef std::shared_ptr<ScriptSummaryFormat> SharedPointer;
394 DISALLOW_COPY_AND_ASSIGN(ScriptSummaryFormat);
396 } // namespace lldb_private
398 #endif // lldb_TypeSummary_h_