xref: /src/contrib/llvm-project/lldb/source/DataFormatters/ValueObjectPrinter.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1cfca06d7SDimitry Andric //===-- ValueObjectPrinter.cpp --------------------------------------------===//
2f21a844fSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f21a844fSEd Maste //
7f21a844fSEd Maste //===----------------------------------------------------------------------===//
8f21a844fSEd Maste 
9f21a844fSEd Maste #include "lldb/DataFormatters/ValueObjectPrinter.h"
10f21a844fSEd Maste 
11e81d9d49SDimitry Andric #include "lldb/Core/ValueObject.h"
12f21a844fSEd Maste #include "lldb/DataFormatters/DataVisualization.h"
13f21a844fSEd Maste #include "lldb/Interpreter/CommandInterpreter.h"
14e81d9d49SDimitry Andric #include "lldb/Target/Language.h"
15f21a844fSEd Maste #include "lldb/Target/Target.h"
1674a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
17ac9a064cSDimitry Andric #include "llvm/Support/MathExtras.h"
18ac9a064cSDimitry Andric #include <cstdint>
19f21a844fSEd Maste 
20f21a844fSEd Maste using namespace lldb;
21f21a844fSEd Maste using namespace lldb_private;
22f21a844fSEd Maste 
ValueObjectPrinter(ValueObject & valobj,Stream * s)23ac9a064cSDimitry Andric ValueObjectPrinter::ValueObjectPrinter(ValueObject &valobj, Stream *s)
24ac9a064cSDimitry Andric     : m_orig_valobj(valobj) {
25ac9a064cSDimitry Andric   DumpValueObjectOptions options(valobj);
26e81d9d49SDimitry Andric   Init(valobj, s, options, m_options.m_max_ptr_depth, 0, nullptr);
275e95aa85SEd Maste }
285e95aa85SEd Maste 
ValueObjectPrinter(ValueObject & valobj,Stream * s,const DumpValueObjectOptions & options)29ac9a064cSDimitry Andric ValueObjectPrinter::ValueObjectPrinter(ValueObject &valobj, Stream *s,
30ac9a064cSDimitry Andric                                        const DumpValueObjectOptions &options)
31ac9a064cSDimitry Andric     : m_orig_valobj(valobj) {
32e81d9d49SDimitry Andric   Init(valobj, s, options, m_options.m_max_ptr_depth, 0, nullptr);
33f21a844fSEd Maste }
34f21a844fSEd Maste 
ValueObjectPrinter(ValueObject & valobj,Stream * s,const DumpValueObjectOptions & options,const DumpValueObjectOptions::PointerDepth & ptr_depth,uint32_t curr_depth,InstancePointersSetSP printed_instance_pointers)3514f1b3e8SDimitry Andric ValueObjectPrinter::ValueObjectPrinter(
36ac9a064cSDimitry Andric     ValueObject &valobj, Stream *s, const DumpValueObjectOptions &options,
3714f1b3e8SDimitry Andric     const DumpValueObjectOptions::PointerDepth &ptr_depth, uint32_t curr_depth,
38ac9a064cSDimitry Andric     InstancePointersSetSP printed_instance_pointers)
39ac9a064cSDimitry Andric     : m_orig_valobj(valobj) {
40e81d9d49SDimitry Andric   Init(valobj, s, options, ptr_depth, curr_depth, printed_instance_pointers);
41f21a844fSEd Maste }
42f21a844fSEd Maste 
Init(ValueObject & valobj,Stream * s,const DumpValueObjectOptions & options,const DumpValueObjectOptions::PointerDepth & ptr_depth,uint32_t curr_depth,InstancePointersSetSP printed_instance_pointers)4314f1b3e8SDimitry Andric void ValueObjectPrinter::Init(
44ac9a064cSDimitry Andric     ValueObject &valobj, Stream *s, const DumpValueObjectOptions &options,
4514f1b3e8SDimitry Andric     const DumpValueObjectOptions::PointerDepth &ptr_depth, uint32_t curr_depth,
4614f1b3e8SDimitry Andric     InstancePointersSetSP printed_instance_pointers) {
47ac9a064cSDimitry Andric   m_cached_valobj = nullptr;
48f21a844fSEd Maste   m_stream = s;
49e81d9d49SDimitry Andric   m_options = options;
50f21a844fSEd Maste   m_ptr_depth = ptr_depth;
51f21a844fSEd Maste   m_curr_depth = curr_depth;
52f21a844fSEd Maste   assert(m_stream && "cannot print to a NULL Stream");
53f21a844fSEd Maste   m_should_print = eLazyBoolCalculate;
54f21a844fSEd Maste   m_is_nil = eLazyBoolCalculate;
55e81d9d49SDimitry Andric   m_is_uninit = eLazyBoolCalculate;
56f21a844fSEd Maste   m_is_ptr = eLazyBoolCalculate;
57f21a844fSEd Maste   m_is_ref = eLazyBoolCalculate;
58f21a844fSEd Maste   m_is_aggregate = eLazyBoolCalculate;
59e81d9d49SDimitry Andric   m_is_instance_ptr = eLazyBoolCalculate;
60f21a844fSEd Maste   m_summary_formatter = {nullptr, false};
61f21a844fSEd Maste   m_value.assign("");
62f21a844fSEd Maste   m_summary.assign("");
63f21a844fSEd Maste   m_error.assign("");
64e81d9d49SDimitry Andric   m_val_summary_ok = false;
6514f1b3e8SDimitry Andric   m_printed_instance_pointers =
6614f1b3e8SDimitry Andric       printed_instance_pointers
6714f1b3e8SDimitry Andric           ? printed_instance_pointers
6814f1b3e8SDimitry Andric           : InstancePointersSetSP(new InstancePointersSet());
69ac9a064cSDimitry Andric   SetupMostSpecializedValue();
70f21a844fSEd Maste }
71f21a844fSEd Maste 
PrintValueObject()72ac9a064cSDimitry Andric llvm::Error ValueObjectPrinter::PrintValueObject() {
737fa27ce4SDimitry Andric   // If the incoming ValueObject is in an error state, the best we're going to
747fa27ce4SDimitry Andric   // get out of it is its type.  But if we don't even have that, just print
757fa27ce4SDimitry Andric   // the error and exit early.
76ac9a064cSDimitry Andric   if (m_orig_valobj.GetError().Fail() &&
77ac9a064cSDimitry Andric       !m_orig_valobj.GetCompilerType().IsValid())
78ac9a064cSDimitry Andric     return m_orig_valobj.GetError().ToError();
79f21a844fSEd Maste 
8014f1b3e8SDimitry Andric   if (ShouldPrintValueObject()) {
81f21a844fSEd Maste     PrintLocationIfNeeded();
82f21a844fSEd Maste     m_stream->Indent();
83f21a844fSEd Maste 
84e81d9d49SDimitry Andric     PrintDecl();
85f21a844fSEd Maste   }
86f21a844fSEd Maste 
87f21a844fSEd Maste   bool value_printed = false;
88f21a844fSEd Maste   bool summary_printed = false;
89f21a844fSEd Maste 
9014f1b3e8SDimitry Andric   m_val_summary_ok =
9114f1b3e8SDimitry Andric       PrintValueAndSummaryIfNeeded(value_printed, summary_printed);
92f21a844fSEd Maste 
93e81d9d49SDimitry Andric   if (m_val_summary_ok)
94ac9a064cSDimitry Andric     return PrintChildrenIfNeeded(value_printed, summary_printed);
95f21a844fSEd Maste   m_stream->EOL();
96f21a844fSEd Maste 
97ac9a064cSDimitry Andric   return llvm::Error::success();
98f21a844fSEd Maste }
99f21a844fSEd Maste 
GetMostSpecializedValue()100ac9a064cSDimitry Andric ValueObject &ValueObjectPrinter::GetMostSpecializedValue() {
101ac9a064cSDimitry Andric   assert(m_cached_valobj && "ValueObjectPrinter must have a valid ValueObject");
102ac9a064cSDimitry Andric   return *m_cached_valobj;
103ac9a064cSDimitry Andric }
104ac9a064cSDimitry Andric 
SetupMostSpecializedValue()105ac9a064cSDimitry Andric void ValueObjectPrinter::SetupMostSpecializedValue() {
106ac9a064cSDimitry Andric   bool update_success = m_orig_valobj.UpdateValueIfNeeded(true);
107ac9a064cSDimitry Andric   // If we can't find anything better, we'll fall back on the original
108ac9a064cSDimitry Andric   // ValueObject.
109ac9a064cSDimitry Andric   m_cached_valobj = &m_orig_valobj;
110ac9a064cSDimitry Andric   if (update_success) {
111ac9a064cSDimitry Andric     if (m_orig_valobj.IsDynamic()) {
11214f1b3e8SDimitry Andric       if (m_options.m_use_dynamic == eNoDynamicValues) {
113ac9a064cSDimitry Andric         ValueObject *static_value = m_orig_valobj.GetStaticValue().get();
114f21a844fSEd Maste         if (static_value)
115ac9a064cSDimitry Andric           m_cached_valobj = static_value;
116ac9a064cSDimitry Andric       }
11714f1b3e8SDimitry Andric     } else {
11814f1b3e8SDimitry Andric       if (m_options.m_use_dynamic != eNoDynamicValues) {
11914f1b3e8SDimitry Andric         ValueObject *dynamic_value =
120ac9a064cSDimitry Andric             m_orig_valobj.GetDynamicValue(m_options.m_use_dynamic).get();
121f21a844fSEd Maste         if (dynamic_value)
122ac9a064cSDimitry Andric           m_cached_valobj = dynamic_value;
123ac9a064cSDimitry Andric       }
124f21a844fSEd Maste     }
125205afe67SEd Maste 
126ac9a064cSDimitry Andric     if (m_cached_valobj->IsSynthetic()) {
12794994d37SDimitry Andric       if (!m_options.m_use_synthetic) {
128ac9a064cSDimitry Andric         ValueObject *non_synthetic =
129ac9a064cSDimitry Andric             m_cached_valobj->GetNonSyntheticValue().get();
130205afe67SEd Maste         if (non_synthetic)
131ac9a064cSDimitry Andric           m_cached_valobj = non_synthetic;
132205afe67SEd Maste       }
13314f1b3e8SDimitry Andric     } else {
13494994d37SDimitry Andric       if (m_options.m_use_synthetic) {
135ac9a064cSDimitry Andric         ValueObject *synthetic = m_cached_valobj->GetSyntheticValue().get();
136205afe67SEd Maste         if (synthetic)
137ac9a064cSDimitry Andric           m_cached_valobj = synthetic;
138205afe67SEd Maste       }
139205afe67SEd Maste     }
140f21a844fSEd Maste   }
141ac9a064cSDimitry Andric   m_compiler_type = m_cached_valobj->GetCompilerType();
142e81d9d49SDimitry Andric   m_type_flags = m_compiler_type.GetTypeInfo();
143ac9a064cSDimitry Andric   assert(m_cached_valobj &&
144ac9a064cSDimitry Andric          "SetupMostSpecialized value must compute a valid ValueObject");
145f21a844fSEd Maste }
146f21a844fSEd Maste 
GetDescriptionForDisplay()147ac9a064cSDimitry Andric llvm::Expected<std::string> ValueObjectPrinter::GetDescriptionForDisplay() {
148ac9a064cSDimitry Andric   ValueObject &valobj = GetMostSpecializedValue();
149ac9a064cSDimitry Andric   llvm::Expected<std::string> maybe_str = valobj.GetObjectDescription();
150ac9a064cSDimitry Andric   if (maybe_str)
151ac9a064cSDimitry Andric     return maybe_str;
152ac9a064cSDimitry Andric 
153ac9a064cSDimitry Andric   const char *str = nullptr;
154f21a844fSEd Maste   if (!str)
155ac9a064cSDimitry Andric     str = valobj.GetSummaryAsCString();
156f21a844fSEd Maste   if (!str)
157ac9a064cSDimitry Andric     str = valobj.GetValueAsCString();
158ac9a064cSDimitry Andric 
159ac9a064cSDimitry Andric   if (!str)
160ac9a064cSDimitry Andric     return maybe_str;
161ac9a064cSDimitry Andric   llvm::consumeError(maybe_str.takeError());
162f21a844fSEd Maste   return str;
163f21a844fSEd Maste }
164f21a844fSEd Maste 
GetRootNameForDisplay()165cfca06d7SDimitry Andric const char *ValueObjectPrinter::GetRootNameForDisplay() {
166ac9a064cSDimitry Andric   const char *root_valobj_name =
167ac9a064cSDimitry Andric       m_options.m_root_valobj_name.empty()
168ac9a064cSDimitry Andric           ? GetMostSpecializedValue().GetName().AsCString()
16914f1b3e8SDimitry Andric           : m_options.m_root_valobj_name.c_str();
170cfca06d7SDimitry Andric   return root_valobj_name ? root_valobj_name : "";
171f21a844fSEd Maste }
172f21a844fSEd Maste 
ShouldPrintValueObject()17314f1b3e8SDimitry Andric bool ValueObjectPrinter::ShouldPrintValueObject() {
174f21a844fSEd Maste   if (m_should_print == eLazyBoolCalculate)
17514f1b3e8SDimitry Andric     m_should_print =
17694994d37SDimitry Andric         (!m_options.m_flat_output || m_type_flags.Test(eTypeHasValue))
17714f1b3e8SDimitry Andric             ? eLazyBoolYes
17814f1b3e8SDimitry Andric             : eLazyBoolNo;
179f21a844fSEd Maste   return m_should_print == eLazyBoolYes;
180f21a844fSEd Maste }
181f21a844fSEd Maste 
IsNil()18214f1b3e8SDimitry Andric bool ValueObjectPrinter::IsNil() {
183f21a844fSEd Maste   if (m_is_nil == eLazyBoolCalculate)
184ac9a064cSDimitry Andric     m_is_nil =
185ac9a064cSDimitry Andric         GetMostSpecializedValue().IsNilReference() ? eLazyBoolYes : eLazyBoolNo;
186f21a844fSEd Maste   return m_is_nil == eLazyBoolYes;
187f21a844fSEd Maste }
188f21a844fSEd Maste 
IsUninitialized()18914f1b3e8SDimitry Andric bool ValueObjectPrinter::IsUninitialized() {
190e81d9d49SDimitry Andric   if (m_is_uninit == eLazyBoolCalculate)
191ac9a064cSDimitry Andric     m_is_uninit = GetMostSpecializedValue().IsUninitializedReference()
192ac9a064cSDimitry Andric                       ? eLazyBoolYes
193ac9a064cSDimitry Andric                       : eLazyBoolNo;
194e81d9d49SDimitry Andric   return m_is_uninit == eLazyBoolYes;
195e81d9d49SDimitry Andric }
196e81d9d49SDimitry Andric 
IsPtr()19714f1b3e8SDimitry Andric bool ValueObjectPrinter::IsPtr() {
198f21a844fSEd Maste   if (m_is_ptr == eLazyBoolCalculate)
199205afe67SEd Maste     m_is_ptr = m_type_flags.Test(eTypeIsPointer) ? eLazyBoolYes : eLazyBoolNo;
200f21a844fSEd Maste   return m_is_ptr == eLazyBoolYes;
201f21a844fSEd Maste }
202f21a844fSEd Maste 
IsRef()20314f1b3e8SDimitry Andric bool ValueObjectPrinter::IsRef() {
204f21a844fSEd Maste   if (m_is_ref == eLazyBoolCalculate)
205205afe67SEd Maste     m_is_ref = m_type_flags.Test(eTypeIsReference) ? eLazyBoolYes : eLazyBoolNo;
206f21a844fSEd Maste   return m_is_ref == eLazyBoolYes;
207f21a844fSEd Maste }
208f21a844fSEd Maste 
IsAggregate()20914f1b3e8SDimitry Andric bool ValueObjectPrinter::IsAggregate() {
210f21a844fSEd Maste   if (m_is_aggregate == eLazyBoolCalculate)
21114f1b3e8SDimitry Andric     m_is_aggregate =
21214f1b3e8SDimitry Andric         m_type_flags.Test(eTypeHasChildren) ? eLazyBoolYes : eLazyBoolNo;
213f21a844fSEd Maste   return m_is_aggregate == eLazyBoolYes;
214f21a844fSEd Maste }
215f21a844fSEd Maste 
IsInstancePointer()21614f1b3e8SDimitry Andric bool ValueObjectPrinter::IsInstancePointer() {
217e81d9d49SDimitry Andric   // you need to do this check on the value's clang type
218ac9a064cSDimitry Andric   ValueObject &valobj = GetMostSpecializedValue();
219e81d9d49SDimitry Andric   if (m_is_instance_ptr == eLazyBoolCalculate)
220ac9a064cSDimitry Andric     m_is_instance_ptr = (valobj.GetValue().GetCompilerType().GetTypeInfo() &
22114f1b3e8SDimitry Andric                          eTypeInstanceIsPointer) != 0
22214f1b3e8SDimitry Andric                             ? eLazyBoolYes
22314f1b3e8SDimitry Andric                             : eLazyBoolNo;
224ac9a064cSDimitry Andric   if ((eLazyBoolYes == m_is_instance_ptr) && valobj.IsBaseClass())
225e81d9d49SDimitry Andric     m_is_instance_ptr = eLazyBoolNo;
226e81d9d49SDimitry Andric   return m_is_instance_ptr == eLazyBoolYes;
227e81d9d49SDimitry Andric }
228e81d9d49SDimitry Andric 
PrintLocationIfNeeded()22914f1b3e8SDimitry Andric bool ValueObjectPrinter::PrintLocationIfNeeded() {
23014f1b3e8SDimitry Andric   if (m_options.m_show_location) {
231ac9a064cSDimitry Andric     m_stream->Printf("%s: ", GetMostSpecializedValue().GetLocationAsCString());
232f21a844fSEd Maste     return true;
233f21a844fSEd Maste   }
234f21a844fSEd Maste   return false;
235f21a844fSEd Maste }
236f21a844fSEd Maste 
PrintDecl()23714f1b3e8SDimitry Andric void ValueObjectPrinter::PrintDecl() {
238f21a844fSEd Maste   bool show_type = true;
23914f1b3e8SDimitry Andric   // if we are at the root-level and been asked to hide the root's type, then
24014f1b3e8SDimitry Andric   // hide it
241e81d9d49SDimitry Andric   if (m_curr_depth == 0 && m_options.m_hide_root_type)
242f21a844fSEd Maste     show_type = false;
243f21a844fSEd Maste   else
24414f1b3e8SDimitry Andric     // otherwise decide according to the usual rules (asked to show types -
24514f1b3e8SDimitry Andric     // always at the root level)
24614f1b3e8SDimitry Andric     show_type = m_options.m_show_types ||
24714f1b3e8SDimitry Andric                 (m_curr_depth == 0 && !m_options.m_flat_output);
248f21a844fSEd Maste 
249e81d9d49SDimitry Andric   StreamString typeName;
250ac9a064cSDimitry Andric   // Figure out which ValueObject we're acting on
251ac9a064cSDimitry Andric   ValueObject &valobj = GetMostSpecializedValue();
252e81d9d49SDimitry Andric 
253e81d9d49SDimitry Andric   // always show the type at the root level if it is invalid
25414f1b3e8SDimitry Andric   if (show_type) {
255f73363f1SDimitry Andric     // Some ValueObjects don't have types (like registers sets). Only print the
256f73363f1SDimitry Andric     // type if there is one to print
257205afe67SEd Maste     ConstString type_name;
25814f1b3e8SDimitry Andric     if (m_compiler_type.IsValid()) {
259cfca06d7SDimitry Andric       type_name = m_options.m_use_type_display_name
260ac9a064cSDimitry Andric                       ? valobj.GetDisplayTypeName()
261ac9a064cSDimitry Andric                       : valobj.GetQualifiedTypeName();
26214f1b3e8SDimitry Andric     } else {
26314f1b3e8SDimitry Andric       // only show an invalid type name if the user explicitly triggered
26414f1b3e8SDimitry Andric       // show_type
265e81d9d49SDimitry Andric       if (m_options.m_show_types)
266e81d9d49SDimitry Andric         type_name = ConstString("<invalid type>");
267f21a844fSEd Maste     }
268f21a844fSEd Maste 
26914f1b3e8SDimitry Andric     if (type_name) {
270e81d9d49SDimitry Andric       std::string type_name_str(type_name.GetCString());
27114f1b3e8SDimitry Andric       if (m_options.m_hide_pointer_value) {
27214f1b3e8SDimitry Andric         for (auto iter = type_name_str.find(" *"); iter != std::string::npos;
27314f1b3e8SDimitry Andric              iter = type_name_str.find(" *")) {
274e81d9d49SDimitry Andric           type_name_str.erase(iter, 2);
275e81d9d49SDimitry Andric         }
276e81d9d49SDimitry Andric       }
277cfca06d7SDimitry Andric       typeName << type_name_str.c_str();
278e81d9d49SDimitry Andric     }
279e81d9d49SDimitry Andric   }
280e81d9d49SDimitry Andric 
281e81d9d49SDimitry Andric   StreamString varName;
282e81d9d49SDimitry Andric 
2837fa27ce4SDimitry Andric   if (ShouldShowName()) {
284cfca06d7SDimitry Andric     if (m_options.m_flat_output)
285ac9a064cSDimitry Andric       valobj.GetExpressionPath(varName);
286cfca06d7SDimitry Andric     else
287cfca06d7SDimitry Andric       varName << GetRootNameForDisplay();
288f21a844fSEd Maste   }
289e81d9d49SDimitry Andric 
290e81d9d49SDimitry Andric   bool decl_printed = false;
29114f1b3e8SDimitry Andric   if (!m_options.m_decl_printing_helper) {
29214f1b3e8SDimitry Andric     // if the user didn't give us a custom helper, pick one based upon the
29314f1b3e8SDimitry Andric     // language, either the one that this printer is bound to, or the preferred
29414f1b3e8SDimitry Andric     // one for the ValueObject
29514f1b3e8SDimitry Andric     lldb::LanguageType lang_type =
29614f1b3e8SDimitry Andric         (m_options.m_varformat_language == lldb::eLanguageTypeUnknown)
297ac9a064cSDimitry Andric             ? valobj.GetPreferredDisplayLanguage()
29814f1b3e8SDimitry Andric             : m_options.m_varformat_language;
29914f1b3e8SDimitry Andric     if (Language *lang_plugin = Language::FindPlugin(lang_type)) {
300e81d9d49SDimitry Andric       m_options.m_decl_printing_helper = lang_plugin->GetDeclPrintingHelper();
301e81d9d49SDimitry Andric     }
302e81d9d49SDimitry Andric   }
303e81d9d49SDimitry Andric 
30414f1b3e8SDimitry Andric   if (m_options.m_decl_printing_helper) {
30514f1b3e8SDimitry Andric     ConstString type_name_cstr(typeName.GetString());
30614f1b3e8SDimitry Andric     ConstString var_name_cstr(varName.GetString());
307e81d9d49SDimitry Andric 
3087fa27ce4SDimitry Andric     DumpValueObjectOptions decl_print_options = m_options;
3097fa27ce4SDimitry Andric     // Pass printing helpers an option object that indicates whether the name
3107fa27ce4SDimitry Andric     // should be shown or hidden.
3117fa27ce4SDimitry Andric     decl_print_options.SetHideName(!ShouldShowName());
3127fa27ce4SDimitry Andric 
313e81d9d49SDimitry Andric     StreamString dest_stream;
31414f1b3e8SDimitry Andric     if (m_options.m_decl_printing_helper(type_name_cstr, var_name_cstr,
3157fa27ce4SDimitry Andric                                          decl_print_options, dest_stream)) {
316e81d9d49SDimitry Andric       decl_printed = true;
31714f1b3e8SDimitry Andric       m_stream->PutCString(dest_stream.GetString());
318e81d9d49SDimitry Andric     }
319e81d9d49SDimitry Andric   }
320e81d9d49SDimitry Andric 
321e81d9d49SDimitry Andric   // if the helper failed, or there is none, do a default thing
32214f1b3e8SDimitry Andric   if (!decl_printed) {
32314f1b3e8SDimitry Andric     if (!typeName.Empty())
324e81d9d49SDimitry Andric       m_stream->Printf("(%s) ", typeName.GetData());
32514f1b3e8SDimitry Andric     if (!varName.Empty())
326e81d9d49SDimitry Andric       m_stream->Printf("%s =", varName.GetData());
3277fa27ce4SDimitry Andric     else if (ShouldShowName())
328e81d9d49SDimitry Andric       m_stream->Printf(" =");
329e81d9d49SDimitry Andric   }
330f21a844fSEd Maste }
331f21a844fSEd Maste 
CheckScopeIfNeeded()33214f1b3e8SDimitry Andric bool ValueObjectPrinter::CheckScopeIfNeeded() {
333e81d9d49SDimitry Andric   if (m_options.m_scope_already_checked)
334f21a844fSEd Maste     return true;
335ac9a064cSDimitry Andric   return GetMostSpecializedValue().IsInScope();
336f21a844fSEd Maste }
337f21a844fSEd Maste 
GetSummaryFormatter(bool null_if_omitted)33814f1b3e8SDimitry Andric TypeSummaryImpl *ValueObjectPrinter::GetSummaryFormatter(bool null_if_omitted) {
33994994d37SDimitry Andric   if (!m_summary_formatter.second) {
340ac9a064cSDimitry Andric     TypeSummaryImpl *entry =
341ac9a064cSDimitry Andric         m_options.m_summary_sp
34214f1b3e8SDimitry Andric             ? m_options.m_summary_sp.get()
343ac9a064cSDimitry Andric             : GetMostSpecializedValue().GetSummaryFormat().get();
344f21a844fSEd Maste 
345e81d9d49SDimitry Andric     if (m_options.m_omit_summary_depth > 0)
3465f29bb8aSDimitry Andric       entry = nullptr;
347f21a844fSEd Maste     m_summary_formatter.first = entry;
348f21a844fSEd Maste     m_summary_formatter.second = true;
349f21a844fSEd Maste   }
350e81d9d49SDimitry Andric   if (m_options.m_omit_summary_depth > 0 && null_if_omitted)
351e81d9d49SDimitry Andric     return nullptr;
352f21a844fSEd Maste   return m_summary_formatter.first;
353f21a844fSEd Maste }
354f21a844fSEd Maste 
IsPointerValue(const CompilerType & type)35514f1b3e8SDimitry Andric static bool IsPointerValue(const CompilerType &type) {
356e81d9d49SDimitry Andric   Flags type_flags(type.GetTypeInfo());
357e81d9d49SDimitry Andric   if (type_flags.AnySet(eTypeInstanceIsPointer | eTypeIsPointer))
358e81d9d49SDimitry Andric     return type_flags.AllClear(eTypeIsBuiltIn);
359e81d9d49SDimitry Andric   return false;
360e81d9d49SDimitry Andric }
361e81d9d49SDimitry Andric 
GetValueSummaryError(std::string & value,std::string & summary,std::string & error)36214f1b3e8SDimitry Andric void ValueObjectPrinter::GetValueSummaryError(std::string &value,
363f21a844fSEd Maste                                               std::string &summary,
36414f1b3e8SDimitry Andric                                               std::string &error) {
365f3fbd1c0SDimitry Andric   lldb::Format format = m_options.m_format;
366ac9a064cSDimitry Andric   ValueObject &valobj = GetMostSpecializedValue();
36714f1b3e8SDimitry Andric   // if I am printing synthetized elements, apply the format to those elements
36814f1b3e8SDimitry Andric   // only
36914f1b3e8SDimitry Andric   if (m_options.m_pointer_as_array)
370ac9a064cSDimitry Andric     valobj.GetValueAsCString(lldb::eFormatDefault, value);
371ac9a064cSDimitry Andric   else if (format != eFormatDefault && format != valobj.GetFormat())
372ac9a064cSDimitry Andric     valobj.GetValueAsCString(format, value);
37314f1b3e8SDimitry Andric   else {
374ac9a064cSDimitry Andric     const char *val_cstr = valobj.GetValueAsCString();
375f21a844fSEd Maste     if (val_cstr)
376f21a844fSEd Maste       value.assign(val_cstr);
377f21a844fSEd Maste   }
378ac9a064cSDimitry Andric   const char *err_cstr = valobj.GetError().AsCString();
379f21a844fSEd Maste   if (err_cstr)
380f21a844fSEd Maste     error.assign(err_cstr);
381f21a844fSEd Maste 
382b60736ecSDimitry Andric   if (!ShouldPrintValueObject())
383b60736ecSDimitry Andric     return;
384b60736ecSDimitry Andric 
385b60736ecSDimitry Andric   if (IsNil()) {
386b60736ecSDimitry Andric     lldb::LanguageType lang_type =
387b60736ecSDimitry Andric         (m_options.m_varformat_language == lldb::eLanguageTypeUnknown)
388ac9a064cSDimitry Andric             ? valobj.GetPreferredDisplayLanguage()
389b60736ecSDimitry Andric             : m_options.m_varformat_language;
390b60736ecSDimitry Andric     if (Language *lang_plugin = Language::FindPlugin(lang_type)) {
391b60736ecSDimitry Andric       summary.assign(lang_plugin->GetNilReferenceSummaryString().str());
392b60736ecSDimitry Andric     } else {
393b60736ecSDimitry Andric       // We treat C as the fallback language rather than as a separate Language
394b60736ecSDimitry Andric       // plugin.
395b60736ecSDimitry Andric       summary.assign("NULL");
396b60736ecSDimitry Andric     }
397b60736ecSDimitry Andric   } else if (IsUninitialized()) {
398e81d9d49SDimitry Andric     summary.assign("<uninitialized>");
399b60736ecSDimitry Andric   } else if (m_options.m_omit_summary_depth == 0) {
400f21a844fSEd Maste     TypeSummaryImpl *entry = GetSummaryFormatter();
401b60736ecSDimitry Andric     if (entry) {
402ac9a064cSDimitry Andric       valobj.GetSummaryAsCString(entry, summary,
40314f1b3e8SDimitry Andric                                  m_options.m_varformat_language);
404b60736ecSDimitry Andric     } else {
40514f1b3e8SDimitry Andric       const char *sum_cstr =
406ac9a064cSDimitry Andric           valobj.GetSummaryAsCString(m_options.m_varformat_language);
407f21a844fSEd Maste       if (sum_cstr)
408f21a844fSEd Maste         summary.assign(sum_cstr);
409f21a844fSEd Maste     }
410f21a844fSEd Maste   }
411f21a844fSEd Maste }
412f21a844fSEd Maste 
PrintValueAndSummaryIfNeeded(bool & value_printed,bool & summary_printed)41314f1b3e8SDimitry Andric bool ValueObjectPrinter::PrintValueAndSummaryIfNeeded(bool &value_printed,
41414f1b3e8SDimitry Andric                                                       bool &summary_printed) {
415f21a844fSEd Maste   bool error_printed = false;
41614f1b3e8SDimitry Andric   if (ShouldPrintValueObject()) {
417f21a844fSEd Maste     if (!CheckScopeIfNeeded())
418f21a844fSEd Maste       m_error.assign("out of scope");
41914f1b3e8SDimitry Andric     if (m_error.empty()) {
420f21a844fSEd Maste       GetValueSummaryError(m_value, m_summary, m_error);
421f21a844fSEd Maste     }
42214f1b3e8SDimitry Andric     if (m_error.size()) {
42314f1b3e8SDimitry Andric       // we need to support scenarios in which it is actually fine for a value
424f73363f1SDimitry Andric       // to have no type but - on the other hand - if we get an error *AND*
425f73363f1SDimitry Andric       // have no type, we try to get out gracefully, since most often that
426f73363f1SDimitry Andric       // combination means "could not resolve a type" and the default failure
427f73363f1SDimitry Andric       // mode is quite ugly
42814f1b3e8SDimitry Andric       if (!m_compiler_type.IsValid()) {
429e81d9d49SDimitry Andric         m_stream->Printf(" <could not resolve type>");
430e81d9d49SDimitry Andric         return false;
431e81d9d49SDimitry Andric       }
432e81d9d49SDimitry Andric 
433f21a844fSEd Maste       error_printed = true;
434f21a844fSEd Maste       m_stream->Printf(" <%s>\n", m_error.c_str());
43514f1b3e8SDimitry Andric     } else {
436f73363f1SDimitry Andric       // Make sure we have a value and make sure the summary didn't specify
437f73363f1SDimitry Andric       // that the value should not be printed - and do not print the value if
438f73363f1SDimitry Andric       // this thing is nil (but show the value if the user passes a format
439f73363f1SDimitry Andric       // explicitly)
440f21a844fSEd Maste       TypeSummaryImpl *entry = GetSummaryFormatter();
441ac9a064cSDimitry Andric       ValueObject &valobj = GetMostSpecializedValue();
442b60736ecSDimitry Andric       const bool has_nil_or_uninitialized_summary =
443b60736ecSDimitry Andric           (IsNil() || IsUninitialized()) && !m_summary.empty();
444b60736ecSDimitry Andric       if (!has_nil_or_uninitialized_summary && !m_value.empty() &&
4455f29bb8aSDimitry Andric           (entry == nullptr ||
446ac9a064cSDimitry Andric            (entry->DoesPrintValue(&valobj) ||
44714f1b3e8SDimitry Andric             m_options.m_format != eFormatDefault) ||
44814f1b3e8SDimitry Andric            m_summary.empty()) &&
44914f1b3e8SDimitry Andric           !m_options.m_hide_value) {
45014f1b3e8SDimitry Andric         if (m_options.m_hide_pointer_value &&
451ac9a064cSDimitry Andric             IsPointerValue(valobj.GetCompilerType())) {
45214f1b3e8SDimitry Andric         } else {
4537fa27ce4SDimitry Andric           if (ShouldShowName())
4547fa27ce4SDimitry Andric             m_stream->PutChar(' ');
4557fa27ce4SDimitry Andric           m_stream->PutCString(m_value);
456f21a844fSEd Maste           value_printed = true;
457f21a844fSEd Maste         }
458e81d9d49SDimitry Andric       }
459f21a844fSEd Maste 
46014f1b3e8SDimitry Andric       if (m_summary.size()) {
4617fa27ce4SDimitry Andric         if (ShouldShowName() || value_printed)
4627fa27ce4SDimitry Andric           m_stream->PutChar(' ');
4637fa27ce4SDimitry Andric         m_stream->PutCString(m_summary);
464f21a844fSEd Maste         summary_printed = true;
465f21a844fSEd Maste       }
466f21a844fSEd Maste     }
467f21a844fSEd Maste   }
468f21a844fSEd Maste   return !error_printed;
469f21a844fSEd Maste }
470f21a844fSEd Maste 
471ac9a064cSDimitry Andric llvm::Error
PrintObjectDescriptionIfNeeded(bool value_printed,bool summary_printed)472ac9a064cSDimitry Andric ValueObjectPrinter::PrintObjectDescriptionIfNeeded(bool value_printed,
47314f1b3e8SDimitry Andric                                                    bool summary_printed) {
47414f1b3e8SDimitry Andric   if (ShouldPrintValueObject()) {
475f21a844fSEd Maste     // let's avoid the overly verbose no description error for a nil thing
47614f1b3e8SDimitry Andric     if (m_options.m_use_objc && !IsNil() && !IsUninitialized() &&
47714f1b3e8SDimitry Andric         (!m_options.m_pointer_as_array)) {
4787fa27ce4SDimitry Andric       if (!m_options.m_hide_value || ShouldShowName())
479ac9a064cSDimitry Andric         *m_stream << ' ';
480ac9a064cSDimitry Andric       llvm::Expected<std::string> object_desc =
481ac9a064cSDimitry Andric           (value_printed || summary_printed)
482ac9a064cSDimitry Andric               ? GetMostSpecializedValue().GetObjectDescription()
483ac9a064cSDimitry Andric               : GetDescriptionForDisplay();
484ac9a064cSDimitry Andric       if (!object_desc) {
485ac9a064cSDimitry Andric         // If no value or summary was printed, surface the error.
486ac9a064cSDimitry Andric         if (!value_printed && !summary_printed)
487ac9a064cSDimitry Andric           return object_desc.takeError();
488ac9a064cSDimitry Andric         // Otherwise gently nudge the user that they should have used
489ac9a064cSDimitry Andric         // `p` instead of `po`. Unfortunately we cannot be more direct
490ac9a064cSDimitry Andric         // about this, because we don't actually know what the user did.
491ac9a064cSDimitry Andric         *m_stream << "warning: no object description available\n";
492ac9a064cSDimitry Andric         llvm::consumeError(object_desc.takeError());
493ac9a064cSDimitry Andric       } else {
494ac9a064cSDimitry Andric         *m_stream << *object_desc;
49574a628f7SDimitry Andric         // If the description already ends with a \n don't add another one.
496ac9a064cSDimitry Andric         if (object_desc->empty() || object_desc->back() != '\n')
497ac9a064cSDimitry Andric           *m_stream << '\n';
498ac9a064cSDimitry Andric       }
499ac9a064cSDimitry Andric       return llvm::Error::success();
500f21a844fSEd Maste     }
501f21a844fSEd Maste   }
502ac9a064cSDimitry Andric   return llvm::Error::success();
503f21a844fSEd Maste }
504f21a844fSEd Maste 
CanAllowExpansion() const50514f1b3e8SDimitry Andric bool DumpValueObjectOptions::PointerDepth::CanAllowExpansion() const {
50614f1b3e8SDimitry Andric   switch (m_mode) {
507e81d9d49SDimitry Andric   case Mode::Always:
508e81d9d49SDimitry Andric   case Mode::Default:
50974a628f7SDimitry Andric     return m_count > 0;
510e81d9d49SDimitry Andric   case Mode::Never:
511e81d9d49SDimitry Andric     return false;
512e81d9d49SDimitry Andric   }
513e81d9d49SDimitry Andric   return false;
514e81d9d49SDimitry Andric }
515e81d9d49SDimitry Andric 
ShouldPrintChildren(DumpValueObjectOptions::PointerDepth & curr_ptr_depth)51614f1b3e8SDimitry Andric bool ValueObjectPrinter::ShouldPrintChildren(
51714f1b3e8SDimitry Andric     DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
518f21a844fSEd Maste   const bool is_ref = IsRef();
519f21a844fSEd Maste   const bool is_ptr = IsPtr();
520e81d9d49SDimitry Andric   const bool is_uninit = IsUninitialized();
521f21a844fSEd Maste 
522e81d9d49SDimitry Andric   if (is_uninit)
523e81d9d49SDimitry Andric     return false;
524e81d9d49SDimitry Andric 
5257fa27ce4SDimitry Andric   // If we have reached the maximum depth we shouldn't print any more children.
5267fa27ce4SDimitry Andric   if (HasReachedMaximumDepth())
5277fa27ce4SDimitry Andric     return false;
5287fa27ce4SDimitry Andric 
529f73363f1SDimitry Andric   // if the user has specified an element count, always print children as it is
530f73363f1SDimitry Andric   // explicit user demand being honored
53114f1b3e8SDimitry Andric   if (m_options.m_pointer_as_array)
532f3fbd1c0SDimitry Andric     return true;
533f3fbd1c0SDimitry Andric 
534e81d9d49SDimitry Andric   if (m_options.m_use_objc)
535e81d9d49SDimitry Andric     return false;
536e81d9d49SDimitry Andric 
5377fa27ce4SDimitry Andric   bool print_children = true;
538ac9a064cSDimitry Andric   ValueObject &valobj = GetMostSpecializedValue();
5397fa27ce4SDimitry Andric   if (TypeSummaryImpl *type_summary = GetSummaryFormatter())
540ac9a064cSDimitry Andric     print_children = type_summary->DoesPrintChildren(&valobj);
5417fa27ce4SDimitry Andric 
542f73363f1SDimitry Andric   // We will show children for all concrete types. We won't show pointer
543f73363f1SDimitry Andric   // contents unless a pointer depth has been specified. We won't reference
544f73363f1SDimitry Andric   // contents unless the reference is the root object (depth of zero).
545f21a844fSEd Maste 
546f73363f1SDimitry Andric   // Use a new temporary pointer depth in case we override the current
547f73363f1SDimitry Andric   // pointer depth below...
548f21a844fSEd Maste 
54914f1b3e8SDimitry Andric   if (is_ptr || is_ref) {
550f73363f1SDimitry Andric     // We have a pointer or reference whose value is an address. Make sure
551f73363f1SDimitry Andric     // that address is not NULL
552f21a844fSEd Maste     AddressType ptr_address_type;
553ac9a064cSDimitry Andric     if (valobj.GetPointerValue(&ptr_address_type) == 0)
554f21a844fSEd Maste       return false;
555f21a844fSEd Maste 
556e81d9d49SDimitry Andric     const bool is_root_level = m_curr_depth == 0;
557e81d9d49SDimitry Andric 
5587fa27ce4SDimitry Andric     if (is_ref && is_root_level && print_children) {
559f73363f1SDimitry Andric       // If this is the root object (depth is zero) that we are showing and
560f73363f1SDimitry Andric       // it is a reference, and no pointer depth has been supplied print out
561f73363f1SDimitry Andric       // what it references. Don't do this at deeper depths otherwise we can
562f73363f1SDimitry Andric       // end up with infinite recursion...
563e81d9d49SDimitry Andric       return true;
564f21a844fSEd Maste     }
565f21a844fSEd Maste 
56674a628f7SDimitry Andric     return curr_ptr_depth.CanAllowExpansion();
567f21a844fSEd Maste   }
568f21a844fSEd Maste 
5697fa27ce4SDimitry Andric   return print_children || m_summary.empty();
570f21a844fSEd Maste }
571f21a844fSEd Maste 
ShouldExpandEmptyAggregates()57214f1b3e8SDimitry Andric bool ValueObjectPrinter::ShouldExpandEmptyAggregates() {
573e81d9d49SDimitry Andric   TypeSummaryImpl *entry = GetSummaryFormatter();
574e81d9d49SDimitry Andric 
575e81d9d49SDimitry Andric   if (!entry)
576e81d9d49SDimitry Andric     return true;
577e81d9d49SDimitry Andric 
578e81d9d49SDimitry Andric   return entry->DoesPrintEmptyAggregates();
579e81d9d49SDimitry Andric }
580e81d9d49SDimitry Andric 
GetValueObjectForChildrenGeneration()581ac9a064cSDimitry Andric ValueObject &ValueObjectPrinter::GetValueObjectForChildrenGeneration() {
582ac9a064cSDimitry Andric   return GetMostSpecializedValue();
583f21a844fSEd Maste }
584f21a844fSEd Maste 
PrintChildrenPreamble(bool value_printed,bool summary_printed)5857fa27ce4SDimitry Andric void ValueObjectPrinter::PrintChildrenPreamble(bool value_printed,
5867fa27ce4SDimitry Andric                                                bool summary_printed) {
58714f1b3e8SDimitry Andric   if (m_options.m_flat_output) {
588f21a844fSEd Maste     if (ShouldPrintValueObject())
589f21a844fSEd Maste       m_stream->EOL();
59014f1b3e8SDimitry Andric   } else {
5917fa27ce4SDimitry Andric     if (ShouldPrintValueObject()) {
5927fa27ce4SDimitry Andric       if (IsRef()) {
5937fa27ce4SDimitry Andric         m_stream->PutCString(": ");
5947fa27ce4SDimitry Andric       } else if (value_printed || summary_printed || ShouldShowName()) {
5957fa27ce4SDimitry Andric         m_stream->PutChar(' ');
5967fa27ce4SDimitry Andric       }
5977fa27ce4SDimitry Andric       m_stream->PutCString("{\n");
5987fa27ce4SDimitry Andric     }
599f21a844fSEd Maste     m_stream->IndentMore();
600f21a844fSEd Maste   }
601f21a844fSEd Maste }
602f21a844fSEd Maste 
PrintChild(ValueObjectSP child_sp,const DumpValueObjectOptions::PointerDepth & curr_ptr_depth)60314f1b3e8SDimitry Andric void ValueObjectPrinter::PrintChild(
60414f1b3e8SDimitry Andric     ValueObjectSP child_sp,
60514f1b3e8SDimitry Andric     const DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
6067fa27ce4SDimitry Andric   const uint32_t consumed_summary_depth = m_options.m_pointer_as_array ? 0 : 1;
60714f1b3e8SDimitry Andric   const bool does_consume_ptr_depth =
60814f1b3e8SDimitry Andric       ((IsPtr() && !m_options.m_pointer_as_array) || IsRef());
609f3fbd1c0SDimitry Andric 
610e81d9d49SDimitry Andric   DumpValueObjectOptions child_options(m_options);
61114f1b3e8SDimitry Andric   child_options.SetFormat(m_options.m_format)
61214f1b3e8SDimitry Andric       .SetSummary()
61314f1b3e8SDimitry Andric       .SetRootValueObjectName();
61414f1b3e8SDimitry Andric   child_options.SetScopeChecked(true)
61514f1b3e8SDimitry Andric       .SetHideName(m_options.m_hide_name)
61614f1b3e8SDimitry Andric       .SetHideValue(m_options.m_hide_value)
61714f1b3e8SDimitry Andric       .SetOmitSummaryDepth(child_options.m_omit_summary_depth > 1
61814f1b3e8SDimitry Andric                                ? child_options.m_omit_summary_depth -
6197fa27ce4SDimitry Andric                                      consumed_summary_depth
62014f1b3e8SDimitry Andric                                : 0)
621f3fbd1c0SDimitry Andric       .SetElementCount(0);
622e81d9d49SDimitry Andric 
62314f1b3e8SDimitry Andric   if (child_sp.get()) {
6247fa27ce4SDimitry Andric     auto ptr_depth = curr_ptr_depth;
6257fa27ce4SDimitry Andric     if (does_consume_ptr_depth)
6267fa27ce4SDimitry Andric       ptr_depth = curr_ptr_depth.Decremented();
6277fa27ce4SDimitry Andric 
628ac9a064cSDimitry Andric     ValueObjectPrinter child_printer(*(child_sp.get()), m_stream, child_options,
6297fa27ce4SDimitry Andric                                      ptr_depth, m_curr_depth + 1,
6307fa27ce4SDimitry Andric                                      m_printed_instance_pointers);
631ac9a064cSDimitry Andric     llvm::Error error = child_printer.PrintValueObject();
632ac9a064cSDimitry Andric     if (error) {
633ac9a064cSDimitry Andric       if (m_stream)
634ac9a064cSDimitry Andric         *m_stream << "error: " << toString(std::move(error));
635ac9a064cSDimitry Andric       else
636ac9a064cSDimitry Andric         llvm::consumeError(std::move(error));
637ac9a064cSDimitry Andric     }
638f21a844fSEd Maste   }
639f21a844fSEd Maste }
640f21a844fSEd Maste 
641ac9a064cSDimitry Andric llvm::Expected<uint32_t>
GetMaxNumChildrenToPrint(bool & print_dotdotdot)642ac9a064cSDimitry Andric ValueObjectPrinter::GetMaxNumChildrenToPrint(bool &print_dotdotdot) {
643ac9a064cSDimitry Andric   ValueObject &synth_valobj = GetValueObjectForChildrenGeneration();
644f21a844fSEd Maste 
64514f1b3e8SDimitry Andric   if (m_options.m_pointer_as_array)
64614f1b3e8SDimitry Andric     return m_options.m_pointer_as_array.m_element_count;
647f3fbd1c0SDimitry Andric 
648ac9a064cSDimitry Andric   const uint32_t max_num_children =
649ac9a064cSDimitry Andric       m_options.m_ignore_cap ? UINT32_MAX
650ac9a064cSDimitry Andric                              : GetMostSpecializedValue()
651ac9a064cSDimitry Andric                                    .GetTargetSP()
652ac9a064cSDimitry Andric                                    ->GetMaximumNumberOfChildrenToDisplay();
653ac9a064cSDimitry Andric   // Ask for one more child than the maximum to see if we should print "...".
654ac9a064cSDimitry Andric   auto num_children_or_err = synth_valobj.GetNumChildren(
655ac9a064cSDimitry Andric       llvm::SaturatingAdd(max_num_children, uint32_t(1)));
656ac9a064cSDimitry Andric   if (!num_children_or_err)
657ac9a064cSDimitry Andric     return num_children_or_err;
658ac9a064cSDimitry Andric   if (*num_children_or_err > max_num_children) {
659f21a844fSEd Maste     print_dotdotdot = true;
660f21a844fSEd Maste     return max_num_children;
661f21a844fSEd Maste   }
662ac9a064cSDimitry Andric   return num_children_or_err;
663f21a844fSEd Maste }
664f21a844fSEd Maste 
PrintChildrenPostamble(bool print_dotdotdot)66514f1b3e8SDimitry Andric void ValueObjectPrinter::PrintChildrenPostamble(bool print_dotdotdot) {
66614f1b3e8SDimitry Andric   if (!m_options.m_flat_output) {
66714f1b3e8SDimitry Andric     if (print_dotdotdot) {
668ac9a064cSDimitry Andric       GetMostSpecializedValue()
669ac9a064cSDimitry Andric           .GetTargetSP()
67014f1b3e8SDimitry Andric           ->GetDebugger()
67114f1b3e8SDimitry Andric           .GetCommandInterpreter()
67214f1b3e8SDimitry Andric           .ChildrenTruncated();
673f21a844fSEd Maste       m_stream->Indent("...\n");
674f21a844fSEd Maste     }
675f21a844fSEd Maste     m_stream->IndentLess();
676f21a844fSEd Maste     m_stream->Indent("}\n");
677f21a844fSEd Maste   }
678f21a844fSEd Maste }
679f21a844fSEd Maste 
ShouldPrintEmptyBrackets(bool value_printed,bool summary_printed)68014f1b3e8SDimitry Andric bool ValueObjectPrinter::ShouldPrintEmptyBrackets(bool value_printed,
68114f1b3e8SDimitry Andric                                                   bool summary_printed) {
682ac9a064cSDimitry Andric   ValueObject &synth_valobj = GetValueObjectForChildrenGeneration();
683e81d9d49SDimitry Andric 
684e81d9d49SDimitry Andric   if (!IsAggregate())
685e81d9d49SDimitry Andric     return false;
686e81d9d49SDimitry Andric 
68794994d37SDimitry Andric   if (!m_options.m_reveal_empty_aggregates) {
688e81d9d49SDimitry Andric     if (value_printed || summary_printed)
689e81d9d49SDimitry Andric       return false;
690e81d9d49SDimitry Andric   }
691e81d9d49SDimitry Andric 
692ac9a064cSDimitry Andric   if (synth_valobj.MightHaveChildren())
693e81d9d49SDimitry Andric     return true;
694e81d9d49SDimitry Andric 
695e81d9d49SDimitry Andric   if (m_val_summary_ok)
696e81d9d49SDimitry Andric     return false;
697e81d9d49SDimitry Andric 
698e81d9d49SDimitry Andric   return true;
699e81d9d49SDimitry Andric }
700e81d9d49SDimitry Andric 
PhysicalIndexForLogicalIndex(size_t base,size_t stride,size_t logical)70114f1b3e8SDimitry Andric static constexpr size_t PhysicalIndexForLogicalIndex(size_t base, size_t stride,
70214f1b3e8SDimitry Andric                                                      size_t logical) {
70314f1b3e8SDimitry Andric   return base + logical * stride;
704f3fbd1c0SDimitry Andric }
70514f1b3e8SDimitry Andric 
GenerateChild(ValueObject & synth_valobj,size_t idx)706ac9a064cSDimitry Andric ValueObjectSP ValueObjectPrinter::GenerateChild(ValueObject &synth_valobj,
70714f1b3e8SDimitry Andric                                                 size_t idx) {
70814f1b3e8SDimitry Andric   if (m_options.m_pointer_as_array) {
70914f1b3e8SDimitry Andric     // if generating pointer-as-array children, use GetSyntheticArrayMember
710ac9a064cSDimitry Andric     return synth_valobj.GetSyntheticArrayMember(
71114f1b3e8SDimitry Andric         PhysicalIndexForLogicalIndex(
71214f1b3e8SDimitry Andric             m_options.m_pointer_as_array.m_base_element,
71314f1b3e8SDimitry Andric             m_options.m_pointer_as_array.m_stride, idx),
71414f1b3e8SDimitry Andric         true);
71514f1b3e8SDimitry Andric   } else {
716f3fbd1c0SDimitry Andric     // otherwise, do the usual thing
717ac9a064cSDimitry Andric     return synth_valobj.GetChildAtIndex(idx);
718f3fbd1c0SDimitry Andric   }
719f3fbd1c0SDimitry Andric }
720f3fbd1c0SDimitry Andric 
PrintChildren(bool value_printed,bool summary_printed,const DumpValueObjectOptions::PointerDepth & curr_ptr_depth)72114f1b3e8SDimitry Andric void ValueObjectPrinter::PrintChildren(
72214f1b3e8SDimitry Andric     bool value_printed, bool summary_printed,
72314f1b3e8SDimitry Andric     const DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
724ac9a064cSDimitry Andric   ValueObject &synth_valobj = GetValueObjectForChildrenGeneration();
725f21a844fSEd Maste 
726f21a844fSEd Maste   bool print_dotdotdot = false;
727ac9a064cSDimitry Andric   auto num_children_or_err = GetMaxNumChildrenToPrint(print_dotdotdot);
728ac9a064cSDimitry Andric   if (!num_children_or_err) {
729ac9a064cSDimitry Andric     *m_stream << " <" << llvm::toString(num_children_or_err.takeError()) << '>';
730ac9a064cSDimitry Andric     return;
731ac9a064cSDimitry Andric   }
732ac9a064cSDimitry Andric   uint32_t num_children = *num_children_or_err;
73314f1b3e8SDimitry Andric   if (num_children) {
734e81d9d49SDimitry Andric     bool any_children_printed = false;
735f21a844fSEd Maste 
73614f1b3e8SDimitry Andric     for (size_t idx = 0; idx < num_children; ++idx) {
737ac9a064cSDimitry Andric       if (ValueObjectSP child_sp = GenerateChild(synth_valobj, idx)) {
7387fa27ce4SDimitry Andric         if (m_options.m_child_printing_decider &&
7397fa27ce4SDimitry Andric             !m_options.m_child_printing_decider(child_sp->GetName()))
7407fa27ce4SDimitry Andric           continue;
74114f1b3e8SDimitry Andric         if (!any_children_printed) {
7427fa27ce4SDimitry Andric           PrintChildrenPreamble(value_printed, summary_printed);
743e81d9d49SDimitry Andric           any_children_printed = true;
744e81d9d49SDimitry Andric         }
745f21a844fSEd Maste         PrintChild(child_sp, curr_ptr_depth);
746f21a844fSEd Maste       }
747f21a844fSEd Maste     }
748e81d9d49SDimitry Andric 
749e81d9d49SDimitry Andric     if (any_children_printed)
750e81d9d49SDimitry Andric       PrintChildrenPostamble(print_dotdotdot);
75114f1b3e8SDimitry Andric     else {
75214f1b3e8SDimitry Andric       if (ShouldPrintEmptyBrackets(value_printed, summary_printed)) {
753e81d9d49SDimitry Andric         if (ShouldPrintValueObject())
754e81d9d49SDimitry Andric           m_stream->PutCString(" {}\n");
755e81d9d49SDimitry Andric         else
756e81d9d49SDimitry Andric           m_stream->EOL();
75714f1b3e8SDimitry Andric       } else
758e81d9d49SDimitry Andric         m_stream->EOL();
759e81d9d49SDimitry Andric     }
76014f1b3e8SDimitry Andric   } else if (ShouldPrintEmptyBrackets(value_printed, summary_printed)) {
761f21a844fSEd Maste     // Aggregate, no children...
76214f1b3e8SDimitry Andric     if (ShouldPrintValueObject()) {
76314f1b3e8SDimitry Andric       // if it has a synthetic value, then don't print {}, the synthetic
76414f1b3e8SDimitry Andric       // children are probably only being used to vend a value
765ac9a064cSDimitry Andric       if (GetMostSpecializedValue().DoesProvideSyntheticValue() ||
76614f1b3e8SDimitry Andric           !ShouldExpandEmptyAggregates())
767205afe67SEd Maste         m_stream->PutCString("\n");
768205afe67SEd Maste       else
769f21a844fSEd Maste         m_stream->PutCString(" {}\n");
770f21a844fSEd Maste     }
77114f1b3e8SDimitry Andric   } else {
772f21a844fSEd Maste     if (ShouldPrintValueObject())
773f21a844fSEd Maste       m_stream->EOL();
774f21a844fSEd Maste   }
775f21a844fSEd Maste }
776f21a844fSEd Maste 
PrintChildrenOneLiner(bool hide_names)77714f1b3e8SDimitry Andric bool ValueObjectPrinter::PrintChildrenOneLiner(bool hide_names) {
778ac9a064cSDimitry Andric   ValueObject &synth_valobj = GetValueObjectForChildrenGeneration();
779f21a844fSEd Maste 
780f21a844fSEd Maste   bool print_dotdotdot = false;
781ac9a064cSDimitry Andric   auto num_children_or_err = GetMaxNumChildrenToPrint(print_dotdotdot);
782ac9a064cSDimitry Andric   if (!num_children_or_err) {
783ac9a064cSDimitry Andric     *m_stream << '<' << llvm::toString(num_children_or_err.takeError()) << '>';
784ac9a064cSDimitry Andric     return true;
785ac9a064cSDimitry Andric   }
786ac9a064cSDimitry Andric   uint32_t num_children = *num_children_or_err;
787f21a844fSEd Maste 
78814f1b3e8SDimitry Andric   if (num_children) {
789f21a844fSEd Maste     m_stream->PutChar('(');
790f21a844fSEd Maste 
7917fa27ce4SDimitry Andric     bool did_print_children = false;
79214f1b3e8SDimitry Andric     for (uint32_t idx = 0; idx < num_children; ++idx) {
793ac9a064cSDimitry Andric       lldb::ValueObjectSP child_sp(synth_valobj.GetChildAtIndex(idx));
794205afe67SEd Maste       if (child_sp)
79514f1b3e8SDimitry Andric         child_sp = child_sp->GetQualifiedRepresentationIfAvailable(
79614f1b3e8SDimitry Andric             m_options.m_use_dynamic, m_options.m_use_synthetic);
79714f1b3e8SDimitry Andric       if (child_sp) {
7987fa27ce4SDimitry Andric         if (m_options.m_child_printing_decider &&
7997fa27ce4SDimitry Andric             !m_options.m_child_printing_decider(child_sp->GetName()))
8007fa27ce4SDimitry Andric           continue;
8017fa27ce4SDimitry Andric         if (idx && did_print_children)
802f21a844fSEd Maste           m_stream->PutCString(", ");
8037fa27ce4SDimitry Andric         did_print_children = true;
80414f1b3e8SDimitry Andric         if (!hide_names) {
805f21a844fSEd Maste           const char *name = child_sp.get()->GetName().AsCString();
80614f1b3e8SDimitry Andric           if (name && *name) {
807f21a844fSEd Maste             m_stream->PutCString(name);
808f21a844fSEd Maste             m_stream->PutCString(" = ");
809f21a844fSEd Maste           }
810f21a844fSEd Maste         }
81114f1b3e8SDimitry Andric         child_sp->DumpPrintableRepresentation(
81214f1b3e8SDimitry Andric             *m_stream, ValueObject::eValueObjectRepresentationStyleSummary,
813e81d9d49SDimitry Andric             m_options.m_format,
81414f1b3e8SDimitry Andric             ValueObject::PrintableRepresentationSpecialCases::eDisable);
815f21a844fSEd Maste       }
816f21a844fSEd Maste     }
817f21a844fSEd Maste 
818f21a844fSEd Maste     if (print_dotdotdot)
819f21a844fSEd Maste       m_stream->PutCString(", ...)");
820f21a844fSEd Maste     else
821f21a844fSEd Maste       m_stream->PutChar(')');
822f21a844fSEd Maste   }
823f21a844fSEd Maste   return true;
824f21a844fSEd Maste }
825f21a844fSEd Maste 
PrintChildrenIfNeeded(bool value_printed,bool summary_printed)826ac9a064cSDimitry Andric llvm::Error ValueObjectPrinter::PrintChildrenIfNeeded(bool value_printed,
82714f1b3e8SDimitry Andric                                                       bool summary_printed) {
828ac9a064cSDimitry Andric   auto error = PrintObjectDescriptionIfNeeded(value_printed, summary_printed);
829ac9a064cSDimitry Andric   if (error)
830ac9a064cSDimitry Andric     return error;
831ac9a064cSDimitry Andric 
832ac9a064cSDimitry Andric   ValueObject &valobj = GetMostSpecializedValue();
833f21a844fSEd Maste 
834ead24645SDimitry Andric   DumpValueObjectOptions::PointerDepth curr_ptr_depth = m_ptr_depth;
8357fa27ce4SDimitry Andric   const bool print_children = ShouldPrintChildren(curr_ptr_depth);
836ead24645SDimitry Andric   const bool print_oneline =
83714f1b3e8SDimitry Andric       (curr_ptr_depth.CanAllowExpansion() || m_options.m_show_types ||
83814f1b3e8SDimitry Andric        !m_options.m_allow_oneliner_mode || m_options.m_flat_output ||
83914f1b3e8SDimitry Andric        (m_options.m_pointer_as_array) || m_options.m_show_location)
84014f1b3e8SDimitry Andric           ? false
841ac9a064cSDimitry Andric           : DataVisualization::ShouldPrintAsOneLiner(valobj);
842ead24645SDimitry Andric   if (print_children && IsInstancePointer()) {
843ac9a064cSDimitry Andric     uint64_t instance_ptr_value = valobj.GetValueAsUnsigned(0);
84414f1b3e8SDimitry Andric     if (m_printed_instance_pointers->count(instance_ptr_value)) {
845ead24645SDimitry Andric       // We already printed this instance-is-pointer thing, so don't expand it.
846e81d9d49SDimitry Andric       m_stream->PutCString(" {...}\n");
847ac9a064cSDimitry Andric       return llvm::Error::success();
848ead24645SDimitry Andric     } else {
849ead24645SDimitry Andric       // Remember this guy for future reference.
850ead24645SDimitry Andric       m_printed_instance_pointers->emplace(instance_ptr_value);
851ead24645SDimitry Andric     }
852e81d9d49SDimitry Andric   }
853f21a844fSEd Maste 
85414f1b3e8SDimitry Andric   if (print_children) {
85514f1b3e8SDimitry Andric     if (print_oneline) {
856f21a844fSEd Maste       m_stream->PutChar(' ');
857f21a844fSEd Maste       PrintChildrenOneLiner(false);
858f21a844fSEd Maste       m_stream->EOL();
85914f1b3e8SDimitry Andric     } else
860e81d9d49SDimitry Andric       PrintChildren(value_printed, summary_printed, curr_ptr_depth);
861145449b1SDimitry Andric   } else if (HasReachedMaximumDepth() && IsAggregate() &&
86214f1b3e8SDimitry Andric              ShouldPrintValueObject()) {
863f21a844fSEd Maste     m_stream->PutCString("{...}\n");
864145449b1SDimitry Andric     // The maximum child depth has been reached. If `m_max_depth` is the default
865145449b1SDimitry Andric     // (i.e. the user has _not_ customized it), then lldb presents a warning to
866145449b1SDimitry Andric     // the user. The warning tells the user that the limit has been reached, but
867145449b1SDimitry Andric     // more importantly tells them how to expand the limit if desired.
868145449b1SDimitry Andric     if (m_options.m_max_depth_is_default)
869ac9a064cSDimitry Andric       valobj.GetTargetSP()
870145449b1SDimitry Andric           ->GetDebugger()
871145449b1SDimitry Andric           .GetCommandInterpreter()
872145449b1SDimitry Andric           .SetReachedMaximumDepth();
87314f1b3e8SDimitry Andric   } else
874f21a844fSEd Maste     m_stream->EOL();
875ac9a064cSDimitry Andric   return llvm::Error::success();
876f21a844fSEd Maste }
877145449b1SDimitry Andric 
HasReachedMaximumDepth()878145449b1SDimitry Andric bool ValueObjectPrinter::HasReachedMaximumDepth() {
879145449b1SDimitry Andric   return m_curr_depth >= m_options.m_max_depth;
880145449b1SDimitry Andric }
8817fa27ce4SDimitry Andric 
ShouldShowName() const8827fa27ce4SDimitry Andric bool ValueObjectPrinter::ShouldShowName() const {
8837fa27ce4SDimitry Andric   if (m_curr_depth == 0)
8847fa27ce4SDimitry Andric     return !m_options.m_hide_root_name && !m_options.m_hide_name;
8857fa27ce4SDimitry Andric   return !m_options.m_hide_name;
8867fa27ce4SDimitry Andric }
887