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_TypeSummary_h_
10 #define lldb_TypeSummary_h_
11 
12 #include <stdint.h>
13 
14 #include <functional>
15 #include <memory>
16 #include <string>
17 
18 #include "lldb/lldb-enumerations.h"
19 #include "lldb/lldb-public.h"
20 
21 #include "lldb/Core/FormatEntity.h"
22 #include "lldb/Utility/Status.h"
24 
25 namespace lldb_private {
27 public:
30 
31  ~TypeSummaryOptions() = default;
32 
34 
36 
38 
40 
42 
43 private:
44  lldb::LanguageType m_lang;
45  lldb::TypeSummaryCapping m_capping;
46 };
47 
49 public:
50  enum class Kind { eSummaryString, eScript, eCallback, eInternal };
51 
52  virtual ~TypeSummaryImpl() = default;
53 
54  Kind GetKind() const { return m_kind; }
55 
56  class Flags {
57  public:
58  Flags() : m_flags(lldb::eTypeOptionCascade) {}
59 
60  Flags(const Flags &other) : m_flags(other.m_flags) {}
61 
62  Flags(uint32_t value) : m_flags(value) {}
63 
64  Flags &operator=(const Flags &rhs) {
65  if (&rhs != this)
66  m_flags = rhs.m_flags;
67 
68  return *this;
69  }
70 
71  Flags &operator=(const uint32_t &rhs) {
72  m_flags = rhs;
73  return *this;
74  }
75 
76  Flags &Clear() {
77  m_flags = 0;
78  return *this;
79  }
80 
81  bool GetCascades() const {
82  return (m_flags & lldb::eTypeOptionCascade) == lldb::eTypeOptionCascade;
83  }
84 
85  Flags &SetCascades(bool value = true) {
86  if (value)
87  m_flags |= lldb::eTypeOptionCascade;
88  else
89  m_flags &= ~lldb::eTypeOptionCascade;
90  return *this;
91  }
92 
93  bool GetSkipPointers() const {
94  return (m_flags & lldb::eTypeOptionSkipPointers) ==
95  lldb::eTypeOptionSkipPointers;
96  }
97 
98  Flags &SetSkipPointers(bool value = true) {
99  if (value)
100  m_flags |= lldb::eTypeOptionSkipPointers;
101  else
102  m_flags &= ~lldb::eTypeOptionSkipPointers;
103  return *this;
104  }
105 
106  bool GetSkipReferences() const {
107  return (m_flags & lldb::eTypeOptionSkipReferences) ==
108  lldb::eTypeOptionSkipReferences;
109  }
110 
111  Flags &SetSkipReferences(bool value = true) {
112  if (value)
113  m_flags |= lldb::eTypeOptionSkipReferences;
114  else
115  m_flags &= ~lldb::eTypeOptionSkipReferences;
116  return *this;
117  }
118 
119  bool GetDontShowChildren() const {
120  return (m_flags & lldb::eTypeOptionHideChildren) ==
121  lldb::eTypeOptionHideChildren;
122  }
123 
124  Flags &SetDontShowChildren(bool value = true) {
125  if (value)
126  m_flags |= lldb::eTypeOptionHideChildren;
127  else
128  m_flags &= ~lldb::eTypeOptionHideChildren;
129  return *this;
130  }
131 
132  bool GetHideEmptyAggregates() const {
133  return (m_flags & lldb::eTypeOptionHideEmptyAggregates) ==
134  lldb::eTypeOptionHideEmptyAggregates;
135  }
136 
137  Flags &SetHideEmptyAggregates(bool value = true) {
138  if (value)
139  m_flags |= lldb::eTypeOptionHideEmptyAggregates;
140  else
141  m_flags &= ~lldb::eTypeOptionHideEmptyAggregates;
142  return *this;
143  }
144 
145  bool GetDontShowValue() const {
146  return (m_flags & lldb::eTypeOptionHideValue) ==
147  lldb::eTypeOptionHideValue;
148  }
149 
150  Flags &SetDontShowValue(bool value = true) {
151  if (value)
152  m_flags |= lldb::eTypeOptionHideValue;
153  else
154  m_flags &= ~lldb::eTypeOptionHideValue;
155  return *this;
156  }
157 
158  bool GetShowMembersOneLiner() const {
159  return (m_flags & lldb::eTypeOptionShowOneLiner) ==
160  lldb::eTypeOptionShowOneLiner;
161  }
162 
163  Flags &SetShowMembersOneLiner(bool value = true) {
164  if (value)
165  m_flags |= lldb::eTypeOptionShowOneLiner;
166  else
167  m_flags &= ~lldb::eTypeOptionShowOneLiner;
168  return *this;
169  }
170 
171  bool GetHideItemNames() const {
172  return (m_flags & lldb::eTypeOptionHideNames) ==
173  lldb::eTypeOptionHideNames;
174  }
175 
176  Flags &SetHideItemNames(bool value = true) {
177  if (value)
178  m_flags |= lldb::eTypeOptionHideNames;
179  else
180  m_flags &= ~lldb::eTypeOptionHideNames;
181  return *this;
182  }
183 
184  bool GetNonCacheable() const {
185  return (m_flags & lldb::eTypeOptionNonCacheable) ==
186  lldb::eTypeOptionNonCacheable;
187  }
188 
189  Flags &SetNonCacheable(bool value = true) {
190  if (value)
191  m_flags |= lldb::eTypeOptionNonCacheable;
192  else
193  m_flags &= ~lldb::eTypeOptionNonCacheable;
194  return *this;
195  }
196 
197  uint32_t GetValue() { return m_flags; }
198 
199  void SetValue(uint32_t value) { m_flags = value; }
200 
201  private:
202  uint32_t m_flags;
203  };
204 
205  bool Cascades() const { return m_flags.GetCascades(); }
206 
207  bool SkipsPointers() const { return m_flags.GetSkipPointers(); }
208 
209  bool SkipsReferences() const { return m_flags.GetSkipReferences(); }
210 
211  bool NonCacheable() const { return m_flags.GetNonCacheable(); }
212 
213  virtual bool DoesPrintChildren(ValueObject *valobj) const {
214  return !m_flags.GetDontShowChildren();
215  }
216 
217  virtual bool DoesPrintEmptyAggregates() const {
218  return !m_flags.GetHideEmptyAggregates();
219  }
220 
221  virtual bool DoesPrintValue(ValueObject *valobj) const {
222  return !m_flags.GetDontShowValue();
223  }
224 
225  bool IsOneLiner() const { return m_flags.GetShowMembersOneLiner(); }
226 
227  virtual bool HideNames(ValueObject *valobj) const {
228  return m_flags.GetHideItemNames();
229  }
230 
231  void SetCascades(bool value) { m_flags.SetCascades(value); }
232 
233  void SetSkipsPointers(bool value) { m_flags.SetSkipPointers(value); }
234 
235  void SetSkipsReferences(bool value) { m_flags.SetSkipReferences(value); }
236 
237  virtual void SetDoesPrintChildren(bool value) {
238  m_flags.SetDontShowChildren(!value);
239  }
240 
241  virtual void SetDoesPrintValue(bool value) {
242  m_flags.SetDontShowValue(!value);
243  }
244 
245  void SetIsOneLiner(bool value) { m_flags.SetShowMembersOneLiner(value); }
246 
247  virtual void SetHideNames(bool value) { m_flags.SetHideItemNames(value); }
248 
249  virtual void SetNonCacheable(bool value) { m_flags.SetNonCacheable(value); }
250 
251  uint32_t GetOptions() { return m_flags.GetValue(); }
252 
253  void SetOptions(uint32_t value) { m_flags.SetValue(value); }
254 
255  // we are using a ValueObject* instead of a ValueObjectSP because we do not
256  // need to hold on to this for extended periods of time and we trust the
257  // ValueObject to stay around for as long as it is required for us to
258  // generate its summary
259  virtual bool FormatObject(ValueObject *valobj, std::string &dest,
260  const TypeSummaryOptions &options) = 0;
261 
262  virtual std::string GetDescription() = 0;
263 
264  uint32_t &GetRevision() { return m_my_revision; }
265 
266  typedef std::shared_ptr<TypeSummaryImpl> SharedPointer;
267 
268 protected:
271 
272  TypeSummaryImpl(Kind kind, const TypeSummaryImpl::Flags &flags);
273 
274 private:
275  Kind m_kind;
276  DISALLOW_COPY_AND_ASSIGN(TypeSummaryImpl);
277 };
278 
279 // simple string-based summaries, using ${var to show data
281  std::string m_format_str;
284 
285  StringSummaryFormat(const TypeSummaryImpl::Flags &flags, const char *f);
286 
287  ~StringSummaryFormat() override = default;
288 
289  const char *GetSummaryString() const { return m_format_str.c_str(); }
290 
291  void SetSummaryString(const char *f);
292 
293  bool FormatObject(ValueObject *valobj, std::string &dest,
294  const TypeSummaryOptions &options) override;
295 
296  std::string GetDescription() override;
297 
298  static bool classof(const TypeSummaryImpl *S) {
299  return S->GetKind() == Kind::eSummaryString;
300  }
301 
302 private:
303  DISALLOW_COPY_AND_ASSIGN(StringSummaryFormat);
304 };
305 
306 // summaries implemented via a C++ function
308  // we should convert these to SBValue and SBStream if we ever cross the
309  // boundary towards the external world
310  typedef std::function<bool(ValueObject &, Stream &,
311  const TypeSummaryOptions &)>
313 
315  std::string m_description;
316 
318  const char *description);
319 
320  ~CXXFunctionSummaryFormat() override = default;
321 
322  Callback GetBackendFunction() const { return m_impl; }
323 
324  const char *GetTextualInfo() const { return m_description.c_str(); }
325 
326  void SetBackendFunction(Callback cb_func) { m_impl = cb_func; }
327 
328  void SetTextualInfo(const char *descr) {
329  if (descr)
330  m_description.assign(descr);
331  else
332  m_description.clear();
333  }
334 
335  bool FormatObject(ValueObject *valobj, std::string &dest,
336  const TypeSummaryOptions &options) override;
337 
338  std::string GetDescription() override;
339 
340  static bool classof(const TypeSummaryImpl *S) {
341  return S->GetKind() == Kind::eCallback;
342  }
343 
344  typedef std::shared_ptr<CXXFunctionSummaryFormat> SharedPointer;
345 
346 private:
347  DISALLOW_COPY_AND_ASSIGN(CXXFunctionSummaryFormat);
348 };
349 
350 // Python-based summaries, running script code to show data
352  std::string m_function_name;
353  std::string m_python_script;
355 
357  const char *function_name,
358  const char *python_script = nullptr);
359 
360  ~ScriptSummaryFormat() override = default;
361 
362  const char *GetFunctionName() const { return m_function_name.c_str(); }
363 
364  const char *GetPythonScript() const { return m_python_script.c_str(); }
365 
366  void SetFunctionName(const char *function_name) {
367  if (function_name)
368  m_function_name.assign(function_name);
369  else
370  m_function_name.clear();
371  m_python_script.clear();
372  }
373 
374  void SetPythonScript(const char *script) {
375  if (script)
376  m_python_script.assign(script);
377  else
378  m_python_script.clear();
379  }
380 
381  bool FormatObject(ValueObject *valobj, std::string &dest,
382  const TypeSummaryOptions &options) override;
383 
384  std::string GetDescription() override;
385 
386  static bool classof(const TypeSummaryImpl *S) {
387  return S->GetKind() == Kind::eScript;
388  }
389 
390  typedef std::shared_ptr<ScriptSummaryFormat> SharedPointer;
391 
392 private:
393  DISALLOW_COPY_AND_ASSIGN(ScriptSummaryFormat);
394 };
395 } // namespace lldb_private
396 
397 #endif // lldb_TypeSummary_h_
virtual bool DoesPrintEmptyAggregates() const
Definition: TypeSummary.h:217
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
Flags & SetCascades(bool value=true)
Definition: TypeSummary.h:85
static bool classof(const TypeSummaryImpl *S)
Definition: TypeSummary.h:298
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
Flags & SetHideItemNames(bool value=true)
Definition: TypeSummary.h:176
void SetSkipsReferences(bool value)
Definition: TypeSummary.h:235
static bool classof(const TypeSummaryImpl *S)
Definition: TypeSummary.h:340
virtual bool DoesPrintValue(ValueObject *valobj) const
Definition: TypeSummary.h:221
const char * GetPythonScript() const
Definition: TypeSummary.h:364
std::function< bool(ValueObject &, Stream &, const TypeSummaryOptions &)> Callback
Definition: TypeSummary.h:312
void SetBackendFunction(Callback cb_func)
Definition: TypeSummary.h:326
Flags & SetNonCacheable(bool value=true)
Definition: TypeSummary.h:189
void SetSkipsPointers(bool value)
Definition: TypeSummary.h:233
std::shared_ptr< TypeSummaryImpl > SharedPointer
Definition: TypeSummary.h:266
StructuredData::ObjectSP m_script_function_sp
Definition: TypeSummary.h:354
lldb::TypeSummaryCapping GetCapping() const
Definition: TypeSummary.cpp:44
void SetFunctionName(const char *function_name)
Definition: TypeSummary.h:366
lldb::LanguageType GetLanguage() const
Definition: TypeSummary.cpp:42
Flags & operator=(const uint32_t &rhs)
Definition: TypeSummary.h:71
virtual void SetHideNames(bool value)
Definition: TypeSummary.h:247
void SetPythonScript(const char *script)
Definition: TypeSummary.h:374
FormatEntity::Entry m_format
Definition: TypeSummary.h:282
TypeSummaryOptions & operator=(const TypeSummaryOptions &rhs)
Definition: TypeSummary.cpp:36
LanguageType
Programming language type.
Flags & SetDontShowValue(bool value=true)
Definition: TypeSummary.h:150
Flags & SetSkipPointers(bool value=true)
Definition: TypeSummary.h:98
virtual void SetDoesPrintChildren(bool value)
Definition: TypeSummary.h:237
const char * GetSummaryString() const
Definition: TypeSummary.h:289
TypeSummaryOptions & SetLanguage(lldb::LanguageType)
Definition: TypeSummary.cpp:48
const char * GetFunctionName() const
Definition: TypeSummary.h:362
void SetIsOneLiner(bool value)
Definition: TypeSummary.h:245
virtual bool HideNames(ValueObject *valobj) const
Definition: TypeSummary.h:227
virtual void SetDoesPrintValue(bool value)
Definition: TypeSummary.h:241
static bool classof(const TypeSummaryImpl *S)
Definition: TypeSummary.h:386
void SetOptions(uint32_t value)
Definition: TypeSummary.h:253
std::shared_ptr< ScriptSummaryFormat > SharedPointer
Definition: TypeSummary.h:390
TypeSummaryOptions & SetCapping(lldb::TypeSummaryCapping)
Definition: TypeSummary.cpp:54
std::shared_ptr< CXXFunctionSummaryFormat > SharedPointer
Definition: TypeSummary.h:344
Flags & SetDontShowChildren(bool value=true)
Definition: TypeSummary.h:124
void SetCascades(bool value)
Definition: TypeSummary.h:231
Definition: SBAddress.h:15
std::shared_ptr< Object > ObjectSP
Flags & SetHideEmptyAggregates(bool value=true)
Definition: TypeSummary.h:137
virtual void SetNonCacheable(bool value)
Definition: TypeSummary.h:249
Flags & SetSkipReferences(bool value=true)
Definition: TypeSummary.h:111
Flags & SetShowMembersOneLiner(bool value=true)
Definition: TypeSummary.h:163
virtual bool DoesPrintChildren(ValueObject *valobj) const
Definition: TypeSummary.h:213
void SetTextualInfo(const char *descr)
Definition: TypeSummary.h:328
Flags & operator=(const Flags &rhs)
Definition: TypeSummary.h:64
An error handling class.
Definition: Status.h:44