xref: /freebsd/contrib/llvm-project/lldb/include/lldb/DataFormatters/ValueObjectPrinter.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===-- ValueObjectPrinter.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_DATAFORMATTERS_VALUEOBJECTPRINTER_H
10 #define LLDB_DATAFORMATTERS_VALUEOBJECTPRINTER_H
11 
12 #include "lldb/lldb-private.h"
13 #include "lldb/lldb-public.h"
14 
15 #include "lldb/Utility/Flags.h"
16 
17 #include "lldb/DataFormatters/DumpValueObjectOptions.h"
18 #include "lldb/Symbol/CompilerType.h"
19 
20 namespace lldb_private {
21 
22 class ValueObjectPrinter {
23   /// The ValueObjectPrinter is a one-shot printer for ValueObjects.  It
24   /// does not retain the ValueObject it is printing, that is the job of
25   /// its caller.  It also doesn't attempt to track changes in the
26   /// ValueObject, e.g. changing synthetic child providers or changing
27   /// dynamic versus static versus synthetic settings.
28 public:
29   ValueObjectPrinter(ValueObject &valobj, Stream *s);
30 
31   ValueObjectPrinter(ValueObject &valobj, Stream *s,
32                      const DumpValueObjectOptions &options);
33 
34   ~ValueObjectPrinter() = default;
35 
36   llvm::Error PrintValueObject();
37 
38 protected:
39   typedef std::set<uint64_t> InstancePointersSet;
40   typedef std::shared_ptr<InstancePointersSet> InstancePointersSetSP;
41 
42   InstancePointersSetSP m_printed_instance_pointers;
43 
44   /// Only this class (and subclasses, if any) should ever be
45   /// concerned with the depth mechanism.
46   ValueObjectPrinter(ValueObject &valobj, Stream *s,
47                      const DumpValueObjectOptions &options,
48                      const DumpValueObjectOptions::PointerDepth &ptr_depth,
49                      uint32_t curr_depth,
50                      InstancePointersSetSP printed_instance_pointers);
51 
52   /// Ee should actually be using delegating constructors here but
53   /// some versions of GCC still have trouble with those.
54   void Init(ValueObject &valobj, Stream *s,
55             const DumpValueObjectOptions &options,
56             const DumpValueObjectOptions::PointerDepth &ptr_depth,
57             uint32_t curr_depth,
58             InstancePointersSetSP printed_instance_pointers);
59 
60   /// Cache the ValueObject we are actually going to print.  If this
61   /// ValueObject has a Dynamic type, we return that, if either the original
62   /// ValueObject or its Dynamic type has a Synthetic provider, return that.
63   /// This will never return an empty ValueObject, since we use the ValueObject
64   /// to carry errors.
65   /// Note, this gets called when making the printer object, and uses the
66   /// use dynamic and use synthetic settings of the ValueObject being printed,
67   /// so changes made to these settings won't affect already made
68   /// ValueObjectPrinters. SetupMostSpecializedValue();
69   ///
70   /// Access the cached "most specialized value" - that is the one to use for
71   /// printing the value object's value.  However, be sure to use
72   /// GetValueForChildGeneration when you are generating the children of this
73   /// value.
74   ValueObject &GetMostSpecializedValue();
75 
76   void SetupMostSpecializedValue();
77 
78   llvm::Expected<std::string> GetDescriptionForDisplay();
79 
80   const char *GetRootNameForDisplay();
81 
82   bool ShouldPrintValueObject();
83 
84   bool IsNil();
85 
86   bool IsUninitialized();
87 
88   bool IsPtr();
89 
90   bool IsRef();
91 
92   bool IsInstancePointer();
93 
94   bool IsAggregate();
95 
96   bool PrintLocationIfNeeded();
97 
98   void PrintDecl();
99 
100   bool CheckScopeIfNeeded();
101 
102   bool ShouldPrintEmptyBrackets(bool value_printed, bool summary_printed);
103 
104   TypeSummaryImpl *GetSummaryFormatter(bool null_if_omitted = true);
105 
106   void GetValueSummaryError(std::string &value, std::string &summary,
107                             std::string &error);
108 
109   bool PrintValueAndSummaryIfNeeded(bool &value_printed, bool &summary_printed);
110 
111   llvm::Error PrintObjectDescriptionIfNeeded(bool value_printed,
112                                              bool summary_printed);
113 
114   bool
115   ShouldPrintChildren(DumpValueObjectOptions::PointerDepth &curr_ptr_depth);
116 
117   bool ShouldExpandEmptyAggregates();
118 
119   ValueObject &GetValueObjectForChildrenGeneration();
120 
121   void PrintChildrenPreamble(bool value_printed, bool summary_printed);
122 
123   void PrintChildrenPostamble(bool print_dotdotdot);
124 
125   lldb::ValueObjectSP GenerateChild(ValueObject &synth_valobj, size_t idx);
126 
127   void PrintChild(lldb::ValueObjectSP child_sp,
128                   const DumpValueObjectOptions::PointerDepth &curr_ptr_depth);
129 
130   llvm::Expected<uint32_t> GetMaxNumChildrenToPrint(bool &print_dotdotdot);
131 
132   void
133   PrintChildren(bool value_printed, bool summary_printed,
134                 const DumpValueObjectOptions::PointerDepth &curr_ptr_depth);
135 
136   llvm::Error PrintChildrenIfNeeded(bool value_printed, bool summary_printed);
137 
138   bool PrintChildrenOneLiner(bool hide_names);
139 
140   bool HasReachedMaximumDepth();
141 
142 private:
143   bool ShouldShowName() const;
144 
145   ValueObject &m_orig_valobj;
146   /// Cache the current "most specialized" value.  Don't use this
147   /// directly, use GetMostSpecializedValue.
148   ValueObject *m_cached_valobj;
149   Stream *m_stream;
150   DumpValueObjectOptions m_options;
151   Flags m_type_flags;
152   CompilerType m_compiler_type;
153   DumpValueObjectOptions::PointerDepth m_ptr_depth;
154   uint32_t m_curr_depth;
155   LazyBool m_should_print;
156   LazyBool m_is_nil;
157   LazyBool m_is_uninit;
158   LazyBool m_is_ptr;
159   LazyBool m_is_ref;
160   LazyBool m_is_aggregate;
161   LazyBool m_is_instance_ptr;
162   std::pair<TypeSummaryImpl *, bool> m_summary_formatter;
163   std::string m_value;
164   std::string m_summary;
165   std::string m_error;
166   bool m_val_summary_ok;
167 
168   friend struct StringSummaryFormat;
169 
170   ValueObjectPrinter(const ValueObjectPrinter &) = delete;
171   const ValueObjectPrinter &operator=(const ValueObjectPrinter &) = delete;
172 };
173 
174 } // namespace lldb_private
175 
176 #endif // LLDB_DATAFORMATTERS_VALUEOBJECTPRINTER_H
177