]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/lldb/DataFormatters/TypeSummary.h
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / include / lldb / DataFormatters / TypeSummary.h
1 //===-- TypeSummary.h -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef lldb_TypeSummary_h_
11 #define lldb_TypeSummary_h_
12
13 // C Includes
14 #include <stdint.h>
15
16 // C++ Includes
17 #include <functional>
18 #include <memory>
19 #include <string>
20
21 // Other libraries and framework includes
22 // Project includes
23 #include "lldb/lldb-enumerations.h"
24 #include "lldb/lldb-public.h"
25
26 #include "lldb/Core/Error.h"
27 #include "lldb/Core/FormatEntity.h"
28 #include "lldb/Core/StructuredData.h"
29
30 namespace lldb_private {
31 class TypeSummaryOptions {
32 public:
33   TypeSummaryOptions();
34   TypeSummaryOptions(const TypeSummaryOptions &rhs);
35
36   ~TypeSummaryOptions() = default;
37
38   TypeSummaryOptions &operator=(const TypeSummaryOptions &rhs);
39
40   lldb::LanguageType GetLanguage() const;
41
42   lldb::TypeSummaryCapping GetCapping() const;
43
44   TypeSummaryOptions &SetLanguage(lldb::LanguageType);
45
46   TypeSummaryOptions &SetCapping(lldb::TypeSummaryCapping);
47
48 private:
49   lldb::LanguageType m_lang;
50   lldb::TypeSummaryCapping m_capping;
51 };
52
53 class TypeSummaryImpl {
54 public:
55   enum class Kind { eSummaryString, eScript, eCallback, eInternal };
56
57   virtual ~TypeSummaryImpl() = default;
58
59   Kind GetKind() const { return m_kind; }
60
61   class Flags {
62   public:
63     Flags() : m_flags(lldb::eTypeOptionCascade) {}
64
65     Flags(const Flags &other) : m_flags(other.m_flags) {}
66
67     Flags(uint32_t value) : m_flags(value) {}
68
69     Flags &operator=(const Flags &rhs) {
70       if (&rhs != this)
71         m_flags = rhs.m_flags;
72
73       return *this;
74     }
75
76     Flags &operator=(const uint32_t &rhs) {
77       m_flags = rhs;
78       return *this;
79     }
80
81     Flags &Clear() {
82       m_flags = 0;
83       return *this;
84     }
85
86     bool GetCascades() const {
87       return (m_flags & lldb::eTypeOptionCascade) == lldb::eTypeOptionCascade;
88     }
89
90     Flags &SetCascades(bool value = true) {
91       if (value)
92         m_flags |= lldb::eTypeOptionCascade;
93       else
94         m_flags &= ~lldb::eTypeOptionCascade;
95       return *this;
96     }
97
98     bool GetSkipPointers() const {
99       return (m_flags & lldb::eTypeOptionSkipPointers) ==
100              lldb::eTypeOptionSkipPointers;
101     }
102
103     Flags &SetSkipPointers(bool value = true) {
104       if (value)
105         m_flags |= lldb::eTypeOptionSkipPointers;
106       else
107         m_flags &= ~lldb::eTypeOptionSkipPointers;
108       return *this;
109     }
110
111     bool GetSkipReferences() const {
112       return (m_flags & lldb::eTypeOptionSkipReferences) ==
113              lldb::eTypeOptionSkipReferences;
114     }
115
116     Flags &SetSkipReferences(bool value = true) {
117       if (value)
118         m_flags |= lldb::eTypeOptionSkipReferences;
119       else
120         m_flags &= ~lldb::eTypeOptionSkipReferences;
121       return *this;
122     }
123
124     bool GetDontShowChildren() const {
125       return (m_flags & lldb::eTypeOptionHideChildren) ==
126              lldb::eTypeOptionHideChildren;
127     }
128
129     Flags &SetDontShowChildren(bool value = true) {
130       if (value)
131         m_flags |= lldb::eTypeOptionHideChildren;
132       else
133         m_flags &= ~lldb::eTypeOptionHideChildren;
134       return *this;
135     }
136
137     bool GetHideEmptyAggregates() const {
138       return (m_flags & lldb::eTypeOptionHideEmptyAggregates) ==
139              lldb::eTypeOptionHideEmptyAggregates;
140     }
141
142     Flags &SetHideEmptyAggregates(bool value = true) {
143       if (value)
144         m_flags |= lldb::eTypeOptionHideEmptyAggregates;
145       else
146         m_flags &= ~lldb::eTypeOptionHideEmptyAggregates;
147       return *this;
148     }
149
150     bool GetDontShowValue() const {
151       return (m_flags & lldb::eTypeOptionHideValue) ==
152              lldb::eTypeOptionHideValue;
153     }
154
155     Flags &SetDontShowValue(bool value = true) {
156       if (value)
157         m_flags |= lldb::eTypeOptionHideValue;
158       else
159         m_flags &= ~lldb::eTypeOptionHideValue;
160       return *this;
161     }
162
163     bool GetShowMembersOneLiner() const {
164       return (m_flags & lldb::eTypeOptionShowOneLiner) ==
165              lldb::eTypeOptionShowOneLiner;
166     }
167
168     Flags &SetShowMembersOneLiner(bool value = true) {
169       if (value)
170         m_flags |= lldb::eTypeOptionShowOneLiner;
171       else
172         m_flags &= ~lldb::eTypeOptionShowOneLiner;
173       return *this;
174     }
175
176     bool GetHideItemNames() const {
177       return (m_flags & lldb::eTypeOptionHideNames) ==
178              lldb::eTypeOptionHideNames;
179     }
180
181     Flags &SetHideItemNames(bool value = true) {
182       if (value)
183         m_flags |= lldb::eTypeOptionHideNames;
184       else
185         m_flags &= ~lldb::eTypeOptionHideNames;
186       return *this;
187     }
188
189     bool GetNonCacheable() const {
190       return (m_flags & lldb::eTypeOptionNonCacheable) ==
191              lldb::eTypeOptionNonCacheable;
192     }
193
194     Flags &SetNonCacheable(bool value = true) {
195       if (value)
196         m_flags |= lldb::eTypeOptionNonCacheable;
197       else
198         m_flags &= ~lldb::eTypeOptionNonCacheable;
199       return *this;
200     }
201
202     uint32_t GetValue() { return m_flags; }
203
204     void SetValue(uint32_t value) { m_flags = value; }
205
206   private:
207     uint32_t m_flags;
208   };
209
210   bool Cascades() const { return m_flags.GetCascades(); }
211
212   bool SkipsPointers() const { return m_flags.GetSkipPointers(); }
213
214   bool SkipsReferences() const { return m_flags.GetSkipReferences(); }
215
216   bool NonCacheable() const { return m_flags.GetNonCacheable(); }
217
218   virtual bool DoesPrintChildren(ValueObject *valobj) const {
219     return !m_flags.GetDontShowChildren();
220   }
221
222   virtual bool DoesPrintEmptyAggregates() const {
223     return !m_flags.GetHideEmptyAggregates();
224   }
225
226   virtual bool DoesPrintValue(ValueObject *valobj) const {
227     return !m_flags.GetDontShowValue();
228   }
229
230   bool IsOneLiner() const { return m_flags.GetShowMembersOneLiner(); }
231
232   virtual bool HideNames(ValueObject *valobj) const {
233     return m_flags.GetHideItemNames();
234   }
235
236   void SetCascades(bool value) { m_flags.SetCascades(value); }
237
238   void SetSkipsPointers(bool value) { m_flags.SetSkipPointers(value); }
239
240   void SetSkipsReferences(bool value) { m_flags.SetSkipReferences(value); }
241
242   virtual void SetDoesPrintChildren(bool value) {
243     m_flags.SetDontShowChildren(!value);
244   }
245
246   virtual void SetDoesPrintValue(bool value) {
247     m_flags.SetDontShowValue(!value);
248   }
249
250   void SetIsOneLiner(bool value) { m_flags.SetShowMembersOneLiner(value); }
251
252   virtual void SetHideNames(bool value) { m_flags.SetHideItemNames(value); }
253
254   virtual void SetNonCacheable(bool value) { m_flags.SetNonCacheable(value); }
255
256   uint32_t GetOptions() { return m_flags.GetValue(); }
257
258   void SetOptions(uint32_t value) { m_flags.SetValue(value); }
259
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;
267
268   virtual std::string GetDescription() = 0;
269
270   uint32_t &GetRevision() { return m_my_revision; }
271
272   typedef std::shared_ptr<TypeSummaryImpl> SharedPointer;
273
274 protected:
275   uint32_t m_my_revision;
276   Flags m_flags;
277
278   TypeSummaryImpl(Kind kind, const TypeSummaryImpl::Flags &flags);
279
280 private:
281   Kind m_kind;
282   DISALLOW_COPY_AND_ASSIGN(TypeSummaryImpl);
283 };
284
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;
289   Error m_error;
290
291   StringSummaryFormat(const TypeSummaryImpl::Flags &flags, const char *f);
292
293   ~StringSummaryFormat() override = default;
294
295   const char *GetSummaryString() const { return m_format_str.c_str(); }
296
297   void SetSummaryString(const char *f);
298
299   bool FormatObject(ValueObject *valobj, std::string &dest,
300                     const TypeSummaryOptions &options) override;
301
302   std::string GetDescription() override;
303
304   static bool classof(const TypeSummaryImpl *S) {
305     return S->GetKind() == Kind::eSummaryString;
306   }
307
308 private:
309   DISALLOW_COPY_AND_ASSIGN(StringSummaryFormat);
310 };
311
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 &)>
318       Callback;
319
320   Callback m_impl;
321   std::string m_description;
322
323   CXXFunctionSummaryFormat(const TypeSummaryImpl::Flags &flags, Callback impl,
324                            const char *description);
325
326   ~CXXFunctionSummaryFormat() override = default;
327
328   Callback GetBackendFunction() const { return m_impl; }
329
330   const char *GetTextualInfo() const { return m_description.c_str(); }
331
332   void SetBackendFunction(Callback cb_func) { m_impl = cb_func; }
333
334   void SetTextualInfo(const char *descr) {
335     if (descr)
336       m_description.assign(descr);
337     else
338       m_description.clear();
339   }
340
341   bool FormatObject(ValueObject *valobj, std::string &dest,
342                     const TypeSummaryOptions &options) override;
343
344   std::string GetDescription() override;
345
346   static bool classof(const TypeSummaryImpl *S) {
347     return S->GetKind() == Kind::eCallback;
348   }
349
350   typedef std::shared_ptr<CXXFunctionSummaryFormat> SharedPointer;
351
352 private:
353   DISALLOW_COPY_AND_ASSIGN(CXXFunctionSummaryFormat);
354 };
355
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;
361
362   ScriptSummaryFormat(const TypeSummaryImpl::Flags &flags,
363                       const char *function_name,
364                       const char *python_script = nullptr);
365
366   ~ScriptSummaryFormat() override = default;
367
368   const char *GetFunctionName() const { return m_function_name.c_str(); }
369
370   const char *GetPythonScript() const { return m_python_script.c_str(); }
371
372   void SetFunctionName(const char *function_name) {
373     if (function_name)
374       m_function_name.assign(function_name);
375     else
376       m_function_name.clear();
377     m_python_script.clear();
378   }
379
380   void SetPythonScript(const char *script) {
381     if (script)
382       m_python_script.assign(script);
383     else
384       m_python_script.clear();
385   }
386
387   bool FormatObject(ValueObject *valobj, std::string &dest,
388                     const TypeSummaryOptions &options) override;
389
390   std::string GetDescription() override;
391
392   static bool classof(const TypeSummaryImpl *S) {
393     return S->GetKind() == Kind::eScript;
394   }
395
396   typedef std::shared_ptr<ScriptSummaryFormat> SharedPointer;
397
398 private:
399   DISALLOW_COPY_AND_ASSIGN(ScriptSummaryFormat);
400 };
401 } // namespace lldb_private
402
403 #endif // lldb_TypeSummary_h_