15ffd83dbSDimitry Andric //===-- ValueObjectPrinter.cpp --------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/DataFormatters/ValueObjectPrinter.h"
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #include "lldb/Core/ValueObject.h"
120b57cec5SDimitry Andric #include "lldb/DataFormatters/DataVisualization.h"
130b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
140b57cec5SDimitry Andric #include "lldb/Target/Language.h"
150b57cec5SDimitry Andric #include "lldb/Target/Target.h"
160b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric using namespace lldb;
190b57cec5SDimitry Andric using namespace lldb_private;
200b57cec5SDimitry Andric 
ValueObjectPrinter(ValueObject * valobj,Stream * s)210b57cec5SDimitry Andric ValueObjectPrinter::ValueObjectPrinter(ValueObject *valobj, Stream *s) {
220b57cec5SDimitry Andric   if (valobj) {
230b57cec5SDimitry Andric     DumpValueObjectOptions options(*valobj);
240b57cec5SDimitry Andric     Init(valobj, s, options, m_options.m_max_ptr_depth, 0, nullptr);
250b57cec5SDimitry Andric   } else {
260b57cec5SDimitry Andric     DumpValueObjectOptions options;
270b57cec5SDimitry Andric     Init(valobj, s, options, m_options.m_max_ptr_depth, 0, nullptr);
280b57cec5SDimitry Andric   }
290b57cec5SDimitry Andric }
300b57cec5SDimitry Andric 
ValueObjectPrinter(ValueObject * valobj,Stream * s,const DumpValueObjectOptions & options)310b57cec5SDimitry Andric ValueObjectPrinter::ValueObjectPrinter(ValueObject *valobj, Stream *s,
320b57cec5SDimitry Andric                                        const DumpValueObjectOptions &options) {
330b57cec5SDimitry Andric   Init(valobj, s, options, m_options.m_max_ptr_depth, 0, nullptr);
340b57cec5SDimitry Andric }
350b57cec5SDimitry Andric 
ValueObjectPrinter(ValueObject * valobj,Stream * s,const DumpValueObjectOptions & options,const DumpValueObjectOptions::PointerDepth & ptr_depth,uint32_t curr_depth,InstancePointersSetSP printed_instance_pointers)360b57cec5SDimitry Andric ValueObjectPrinter::ValueObjectPrinter(
370b57cec5SDimitry Andric     ValueObject *valobj, Stream *s, const DumpValueObjectOptions &options,
380b57cec5SDimitry Andric     const DumpValueObjectOptions::PointerDepth &ptr_depth, uint32_t curr_depth,
390b57cec5SDimitry Andric     InstancePointersSetSP printed_instance_pointers) {
400b57cec5SDimitry Andric   Init(valobj, s, options, ptr_depth, curr_depth, printed_instance_pointers);
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric 
Init(ValueObject * valobj,Stream * s,const DumpValueObjectOptions & options,const DumpValueObjectOptions::PointerDepth & ptr_depth,uint32_t curr_depth,InstancePointersSetSP printed_instance_pointers)430b57cec5SDimitry Andric void ValueObjectPrinter::Init(
440b57cec5SDimitry Andric     ValueObject *valobj, Stream *s, const DumpValueObjectOptions &options,
450b57cec5SDimitry Andric     const DumpValueObjectOptions::PointerDepth &ptr_depth, uint32_t curr_depth,
460b57cec5SDimitry Andric     InstancePointersSetSP printed_instance_pointers) {
470b57cec5SDimitry Andric   m_orig_valobj = valobj;
480b57cec5SDimitry Andric   m_valobj = nullptr;
490b57cec5SDimitry Andric   m_stream = s;
500b57cec5SDimitry Andric   m_options = options;
510b57cec5SDimitry Andric   m_ptr_depth = ptr_depth;
520b57cec5SDimitry Andric   m_curr_depth = curr_depth;
530b57cec5SDimitry Andric   assert(m_orig_valobj && "cannot print a NULL ValueObject");
540b57cec5SDimitry Andric   assert(m_stream && "cannot print to a NULL Stream");
550b57cec5SDimitry Andric   m_should_print = eLazyBoolCalculate;
560b57cec5SDimitry Andric   m_is_nil = eLazyBoolCalculate;
570b57cec5SDimitry Andric   m_is_uninit = eLazyBoolCalculate;
580b57cec5SDimitry Andric   m_is_ptr = eLazyBoolCalculate;
590b57cec5SDimitry Andric   m_is_ref = eLazyBoolCalculate;
600b57cec5SDimitry Andric   m_is_aggregate = eLazyBoolCalculate;
610b57cec5SDimitry Andric   m_is_instance_ptr = eLazyBoolCalculate;
620b57cec5SDimitry Andric   m_summary_formatter = {nullptr, false};
630b57cec5SDimitry Andric   m_value.assign("");
640b57cec5SDimitry Andric   m_summary.assign("");
650b57cec5SDimitry Andric   m_error.assign("");
660b57cec5SDimitry Andric   m_val_summary_ok = false;
670b57cec5SDimitry Andric   m_printed_instance_pointers =
680b57cec5SDimitry Andric       printed_instance_pointers
690b57cec5SDimitry Andric           ? printed_instance_pointers
700b57cec5SDimitry Andric           : InstancePointersSetSP(new InstancePointersSet());
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric 
PrintValueObject()730b57cec5SDimitry Andric bool ValueObjectPrinter::PrintValueObject() {
740b57cec5SDimitry Andric   if (!GetMostSpecializedValue() || m_valobj == nullptr)
750b57cec5SDimitry Andric     return false;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   if (ShouldPrintValueObject()) {
780b57cec5SDimitry Andric     PrintLocationIfNeeded();
790b57cec5SDimitry Andric     m_stream->Indent();
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric     PrintDecl();
820b57cec5SDimitry Andric   }
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric   bool value_printed = false;
850b57cec5SDimitry Andric   bool summary_printed = false;
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric   m_val_summary_ok =
880b57cec5SDimitry Andric       PrintValueAndSummaryIfNeeded(value_printed, summary_printed);
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric   if (m_val_summary_ok)
910b57cec5SDimitry Andric     PrintChildrenIfNeeded(value_printed, summary_printed);
920b57cec5SDimitry Andric   else
930b57cec5SDimitry Andric     m_stream->EOL();
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric   return true;
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric 
GetMostSpecializedValue()980b57cec5SDimitry Andric bool ValueObjectPrinter::GetMostSpecializedValue() {
990b57cec5SDimitry Andric   if (m_valobj)
1000b57cec5SDimitry Andric     return true;
1010b57cec5SDimitry Andric   bool update_success = m_orig_valobj->UpdateValueIfNeeded(true);
1020b57cec5SDimitry Andric   if (!update_success) {
1030b57cec5SDimitry Andric     m_valobj = m_orig_valobj;
1040b57cec5SDimitry Andric   } else {
1050b57cec5SDimitry Andric     if (m_orig_valobj->IsDynamic()) {
1060b57cec5SDimitry Andric       if (m_options.m_use_dynamic == eNoDynamicValues) {
1070b57cec5SDimitry Andric         ValueObject *static_value = m_orig_valobj->GetStaticValue().get();
1080b57cec5SDimitry Andric         if (static_value)
1090b57cec5SDimitry Andric           m_valobj = static_value;
1100b57cec5SDimitry Andric         else
1110b57cec5SDimitry Andric           m_valobj = m_orig_valobj;
1120b57cec5SDimitry Andric       } else
1130b57cec5SDimitry Andric         m_valobj = m_orig_valobj;
1140b57cec5SDimitry Andric     } else {
1150b57cec5SDimitry Andric       if (m_options.m_use_dynamic != eNoDynamicValues) {
1160b57cec5SDimitry Andric         ValueObject *dynamic_value =
1170b57cec5SDimitry Andric             m_orig_valobj->GetDynamicValue(m_options.m_use_dynamic).get();
1180b57cec5SDimitry Andric         if (dynamic_value)
1190b57cec5SDimitry Andric           m_valobj = dynamic_value;
1200b57cec5SDimitry Andric         else
1210b57cec5SDimitry Andric           m_valobj = m_orig_valobj;
1220b57cec5SDimitry Andric       } else
1230b57cec5SDimitry Andric         m_valobj = m_orig_valobj;
1240b57cec5SDimitry Andric     }
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric     if (m_valobj->IsSynthetic()) {
1270b57cec5SDimitry Andric       if (!m_options.m_use_synthetic) {
1280b57cec5SDimitry Andric         ValueObject *non_synthetic = m_valobj->GetNonSyntheticValue().get();
1290b57cec5SDimitry Andric         if (non_synthetic)
1300b57cec5SDimitry Andric           m_valobj = non_synthetic;
1310b57cec5SDimitry Andric       }
1320b57cec5SDimitry Andric     } else {
1330b57cec5SDimitry Andric       if (m_options.m_use_synthetic) {
1340b57cec5SDimitry Andric         ValueObject *synthetic = m_valobj->GetSyntheticValue().get();
1350b57cec5SDimitry Andric         if (synthetic)
1360b57cec5SDimitry Andric           m_valobj = synthetic;
1370b57cec5SDimitry Andric       }
1380b57cec5SDimitry Andric     }
1390b57cec5SDimitry Andric   }
1400b57cec5SDimitry Andric   m_compiler_type = m_valobj->GetCompilerType();
1410b57cec5SDimitry Andric   m_type_flags = m_compiler_type.GetTypeInfo();
1420b57cec5SDimitry Andric   return true;
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric 
GetDescriptionForDisplay()1450b57cec5SDimitry Andric const char *ValueObjectPrinter::GetDescriptionForDisplay() {
1460b57cec5SDimitry Andric   const char *str = m_valobj->GetObjectDescription();
1470b57cec5SDimitry Andric   if (!str)
1480b57cec5SDimitry Andric     str = m_valobj->GetSummaryAsCString();
1490b57cec5SDimitry Andric   if (!str)
1500b57cec5SDimitry Andric     str = m_valobj->GetValueAsCString();
1510b57cec5SDimitry Andric   return str;
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric 
GetRootNameForDisplay()1545ffd83dbSDimitry Andric const char *ValueObjectPrinter::GetRootNameForDisplay() {
1550b57cec5SDimitry Andric   const char *root_valobj_name = m_options.m_root_valobj_name.empty()
1560b57cec5SDimitry Andric                                      ? m_valobj->GetName().AsCString()
1570b57cec5SDimitry Andric                                      : m_options.m_root_valobj_name.c_str();
1585ffd83dbSDimitry Andric   return root_valobj_name ? root_valobj_name : "";
1590b57cec5SDimitry Andric }
1600b57cec5SDimitry Andric 
ShouldPrintValueObject()1610b57cec5SDimitry Andric bool ValueObjectPrinter::ShouldPrintValueObject() {
1620b57cec5SDimitry Andric   if (m_should_print == eLazyBoolCalculate)
1630b57cec5SDimitry Andric     m_should_print =
1640b57cec5SDimitry Andric         (!m_options.m_flat_output || m_type_flags.Test(eTypeHasValue))
1650b57cec5SDimitry Andric             ? eLazyBoolYes
1660b57cec5SDimitry Andric             : eLazyBoolNo;
1670b57cec5SDimitry Andric   return m_should_print == eLazyBoolYes;
1680b57cec5SDimitry Andric }
1690b57cec5SDimitry Andric 
IsNil()1700b57cec5SDimitry Andric bool ValueObjectPrinter::IsNil() {
1710b57cec5SDimitry Andric   if (m_is_nil == eLazyBoolCalculate)
1720b57cec5SDimitry Andric     m_is_nil = m_valobj->IsNilReference() ? eLazyBoolYes : eLazyBoolNo;
1730b57cec5SDimitry Andric   return m_is_nil == eLazyBoolYes;
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric 
IsUninitialized()1760b57cec5SDimitry Andric bool ValueObjectPrinter::IsUninitialized() {
1770b57cec5SDimitry Andric   if (m_is_uninit == eLazyBoolCalculate)
1780b57cec5SDimitry Andric     m_is_uninit =
1790b57cec5SDimitry Andric         m_valobj->IsUninitializedReference() ? eLazyBoolYes : eLazyBoolNo;
1800b57cec5SDimitry Andric   return m_is_uninit == eLazyBoolYes;
1810b57cec5SDimitry Andric }
1820b57cec5SDimitry Andric 
IsPtr()1830b57cec5SDimitry Andric bool ValueObjectPrinter::IsPtr() {
1840b57cec5SDimitry Andric   if (m_is_ptr == eLazyBoolCalculate)
1850b57cec5SDimitry Andric     m_is_ptr = m_type_flags.Test(eTypeIsPointer) ? eLazyBoolYes : eLazyBoolNo;
1860b57cec5SDimitry Andric   return m_is_ptr == eLazyBoolYes;
1870b57cec5SDimitry Andric }
1880b57cec5SDimitry Andric 
IsRef()1890b57cec5SDimitry Andric bool ValueObjectPrinter::IsRef() {
1900b57cec5SDimitry Andric   if (m_is_ref == eLazyBoolCalculate)
1910b57cec5SDimitry Andric     m_is_ref = m_type_flags.Test(eTypeIsReference) ? eLazyBoolYes : eLazyBoolNo;
1920b57cec5SDimitry Andric   return m_is_ref == eLazyBoolYes;
1930b57cec5SDimitry Andric }
1940b57cec5SDimitry Andric 
IsAggregate()1950b57cec5SDimitry Andric bool ValueObjectPrinter::IsAggregate() {
1960b57cec5SDimitry Andric   if (m_is_aggregate == eLazyBoolCalculate)
1970b57cec5SDimitry Andric     m_is_aggregate =
1980b57cec5SDimitry Andric         m_type_flags.Test(eTypeHasChildren) ? eLazyBoolYes : eLazyBoolNo;
1990b57cec5SDimitry Andric   return m_is_aggregate == eLazyBoolYes;
2000b57cec5SDimitry Andric }
2010b57cec5SDimitry Andric 
IsInstancePointer()2020b57cec5SDimitry Andric bool ValueObjectPrinter::IsInstancePointer() {
2030b57cec5SDimitry Andric   // you need to do this check on the value's clang type
2040b57cec5SDimitry Andric   if (m_is_instance_ptr == eLazyBoolCalculate)
2050b57cec5SDimitry Andric     m_is_instance_ptr = (m_valobj->GetValue().GetCompilerType().GetTypeInfo() &
2060b57cec5SDimitry Andric                          eTypeInstanceIsPointer) != 0
2070b57cec5SDimitry Andric                             ? eLazyBoolYes
2080b57cec5SDimitry Andric                             : eLazyBoolNo;
2090b57cec5SDimitry Andric   if ((eLazyBoolYes == m_is_instance_ptr) && m_valobj->IsBaseClass())
2100b57cec5SDimitry Andric     m_is_instance_ptr = eLazyBoolNo;
2110b57cec5SDimitry Andric   return m_is_instance_ptr == eLazyBoolYes;
2120b57cec5SDimitry Andric }
2130b57cec5SDimitry Andric 
PrintLocationIfNeeded()2140b57cec5SDimitry Andric bool ValueObjectPrinter::PrintLocationIfNeeded() {
2150b57cec5SDimitry Andric   if (m_options.m_show_location) {
2160b57cec5SDimitry Andric     m_stream->Printf("%s: ", m_valobj->GetLocationAsCString());
2170b57cec5SDimitry Andric     return true;
2180b57cec5SDimitry Andric   }
2190b57cec5SDimitry Andric   return false;
2200b57cec5SDimitry Andric }
2210b57cec5SDimitry Andric 
PrintDecl()2220b57cec5SDimitry Andric void ValueObjectPrinter::PrintDecl() {
2230b57cec5SDimitry Andric   bool show_type = true;
2240b57cec5SDimitry Andric   // if we are at the root-level and been asked to hide the root's type, then
2250b57cec5SDimitry Andric   // hide it
2260b57cec5SDimitry Andric   if (m_curr_depth == 0 && m_options.m_hide_root_type)
2270b57cec5SDimitry Andric     show_type = false;
2280b57cec5SDimitry Andric   else
2290b57cec5SDimitry Andric     // otherwise decide according to the usual rules (asked to show types -
2300b57cec5SDimitry Andric     // always at the root level)
2310b57cec5SDimitry Andric     show_type = m_options.m_show_types ||
2320b57cec5SDimitry Andric                 (m_curr_depth == 0 && !m_options.m_flat_output);
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric   StreamString typeName;
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric   // always show the type at the root level if it is invalid
2370b57cec5SDimitry Andric   if (show_type) {
2380b57cec5SDimitry Andric     // Some ValueObjects don't have types (like registers sets). Only print the
2390b57cec5SDimitry Andric     // type if there is one to print
2400b57cec5SDimitry Andric     ConstString type_name;
2410b57cec5SDimitry Andric     if (m_compiler_type.IsValid()) {
2425ffd83dbSDimitry Andric       type_name = m_options.m_use_type_display_name
2435ffd83dbSDimitry Andric                       ? m_valobj->GetDisplayTypeName()
2445ffd83dbSDimitry Andric                       : m_valobj->GetQualifiedTypeName();
2450b57cec5SDimitry Andric     } else {
2460b57cec5SDimitry Andric       // only show an invalid type name if the user explicitly triggered
2470b57cec5SDimitry Andric       // show_type
2480b57cec5SDimitry Andric       if (m_options.m_show_types)
2490b57cec5SDimitry Andric         type_name = ConstString("<invalid type>");
2500b57cec5SDimitry Andric     }
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric     if (type_name) {
2530b57cec5SDimitry Andric       std::string type_name_str(type_name.GetCString());
2540b57cec5SDimitry Andric       if (m_options.m_hide_pointer_value) {
2550b57cec5SDimitry Andric         for (auto iter = type_name_str.find(" *"); iter != std::string::npos;
2560b57cec5SDimitry Andric              iter = type_name_str.find(" *")) {
2570b57cec5SDimitry Andric           type_name_str.erase(iter, 2);
2580b57cec5SDimitry Andric         }
2590b57cec5SDimitry Andric       }
2605ffd83dbSDimitry Andric       typeName << type_name_str.c_str();
2610b57cec5SDimitry Andric     }
2620b57cec5SDimitry Andric   }
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric   StreamString varName;
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric   if (!m_options.m_hide_name) {
2675ffd83dbSDimitry Andric     if (m_options.m_flat_output)
2685ffd83dbSDimitry Andric       m_valobj->GetExpressionPath(varName);
2695ffd83dbSDimitry Andric     else
2705ffd83dbSDimitry Andric       varName << GetRootNameForDisplay();
2710b57cec5SDimitry Andric   }
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric   bool decl_printed = false;
2740b57cec5SDimitry Andric   if (!m_options.m_decl_printing_helper) {
2750b57cec5SDimitry Andric     // if the user didn't give us a custom helper, pick one based upon the
2760b57cec5SDimitry Andric     // language, either the one that this printer is bound to, or the preferred
2770b57cec5SDimitry Andric     // one for the ValueObject
2780b57cec5SDimitry Andric     lldb::LanguageType lang_type =
2790b57cec5SDimitry Andric         (m_options.m_varformat_language == lldb::eLanguageTypeUnknown)
2800b57cec5SDimitry Andric             ? m_valobj->GetPreferredDisplayLanguage()
2810b57cec5SDimitry Andric             : m_options.m_varformat_language;
2820b57cec5SDimitry Andric     if (Language *lang_plugin = Language::FindPlugin(lang_type)) {
2830b57cec5SDimitry Andric       m_options.m_decl_printing_helper = lang_plugin->GetDeclPrintingHelper();
2840b57cec5SDimitry Andric     }
2850b57cec5SDimitry Andric   }
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric   if (m_options.m_decl_printing_helper) {
2880b57cec5SDimitry Andric     ConstString type_name_cstr(typeName.GetString());
2890b57cec5SDimitry Andric     ConstString var_name_cstr(varName.GetString());
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric     StreamString dest_stream;
2920b57cec5SDimitry Andric     if (m_options.m_decl_printing_helper(type_name_cstr, var_name_cstr,
2930b57cec5SDimitry Andric                                          m_options, dest_stream)) {
2940b57cec5SDimitry Andric       decl_printed = true;
2950b57cec5SDimitry Andric       m_stream->PutCString(dest_stream.GetString());
2960b57cec5SDimitry Andric     }
2970b57cec5SDimitry Andric   }
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric   // if the helper failed, or there is none, do a default thing
3000b57cec5SDimitry Andric   if (!decl_printed) {
3010b57cec5SDimitry Andric     if (!typeName.Empty())
3020b57cec5SDimitry Andric       m_stream->Printf("(%s) ", typeName.GetData());
3030b57cec5SDimitry Andric     if (!varName.Empty())
3040b57cec5SDimitry Andric       m_stream->Printf("%s =", varName.GetData());
3050b57cec5SDimitry Andric     else if (!m_options.m_hide_name)
3060b57cec5SDimitry Andric       m_stream->Printf(" =");
3070b57cec5SDimitry Andric   }
3080b57cec5SDimitry Andric }
3090b57cec5SDimitry Andric 
CheckScopeIfNeeded()3100b57cec5SDimitry Andric bool ValueObjectPrinter::CheckScopeIfNeeded() {
3110b57cec5SDimitry Andric   if (m_options.m_scope_already_checked)
3120b57cec5SDimitry Andric     return true;
3130b57cec5SDimitry Andric   return m_valobj->IsInScope();
3140b57cec5SDimitry Andric }
3150b57cec5SDimitry Andric 
GetSummaryFormatter(bool null_if_omitted)3160b57cec5SDimitry Andric TypeSummaryImpl *ValueObjectPrinter::GetSummaryFormatter(bool null_if_omitted) {
3170b57cec5SDimitry Andric   if (!m_summary_formatter.second) {
3180b57cec5SDimitry Andric     TypeSummaryImpl *entry = m_options.m_summary_sp
3190b57cec5SDimitry Andric                                  ? m_options.m_summary_sp.get()
3200b57cec5SDimitry Andric                                  : m_valobj->GetSummaryFormat().get();
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric     if (m_options.m_omit_summary_depth > 0)
3230b57cec5SDimitry Andric       entry = nullptr;
3240b57cec5SDimitry Andric     m_summary_formatter.first = entry;
3250b57cec5SDimitry Andric     m_summary_formatter.second = true;
3260b57cec5SDimitry Andric   }
3270b57cec5SDimitry Andric   if (m_options.m_omit_summary_depth > 0 && null_if_omitted)
3280b57cec5SDimitry Andric     return nullptr;
3290b57cec5SDimitry Andric   return m_summary_formatter.first;
3300b57cec5SDimitry Andric }
3310b57cec5SDimitry Andric 
IsPointerValue(const CompilerType & type)3320b57cec5SDimitry Andric static bool IsPointerValue(const CompilerType &type) {
3330b57cec5SDimitry Andric   Flags type_flags(type.GetTypeInfo());
3340b57cec5SDimitry Andric   if (type_flags.AnySet(eTypeInstanceIsPointer | eTypeIsPointer))
3350b57cec5SDimitry Andric     return type_flags.AllClear(eTypeIsBuiltIn);
3360b57cec5SDimitry Andric   return false;
3370b57cec5SDimitry Andric }
3380b57cec5SDimitry Andric 
GetValueSummaryError(std::string & value,std::string & summary,std::string & error)3390b57cec5SDimitry Andric void ValueObjectPrinter::GetValueSummaryError(std::string &value,
3400b57cec5SDimitry Andric                                               std::string &summary,
3410b57cec5SDimitry Andric                                               std::string &error) {
3420b57cec5SDimitry Andric   lldb::Format format = m_options.m_format;
3430b57cec5SDimitry Andric   // if I am printing synthetized elements, apply the format to those elements
3440b57cec5SDimitry Andric   // only
3450b57cec5SDimitry Andric   if (m_options.m_pointer_as_array)
3460b57cec5SDimitry Andric     m_valobj->GetValueAsCString(lldb::eFormatDefault, value);
3470b57cec5SDimitry Andric   else if (format != eFormatDefault && format != m_valobj->GetFormat())
3480b57cec5SDimitry Andric     m_valobj->GetValueAsCString(format, value);
3490b57cec5SDimitry Andric   else {
3500b57cec5SDimitry Andric     const char *val_cstr = m_valobj->GetValueAsCString();
3510b57cec5SDimitry Andric     if (val_cstr)
3520b57cec5SDimitry Andric       value.assign(val_cstr);
3530b57cec5SDimitry Andric   }
3540b57cec5SDimitry Andric   const char *err_cstr = m_valobj->GetError().AsCString();
3550b57cec5SDimitry Andric   if (err_cstr)
3560b57cec5SDimitry Andric     error.assign(err_cstr);
3570b57cec5SDimitry Andric 
358*af732203SDimitry Andric   if (!ShouldPrintValueObject())
359*af732203SDimitry Andric     return;
360*af732203SDimitry Andric 
361*af732203SDimitry Andric   if (IsNil()) {
362*af732203SDimitry Andric     lldb::LanguageType lang_type =
363*af732203SDimitry Andric         (m_options.m_varformat_language == lldb::eLanguageTypeUnknown)
364*af732203SDimitry Andric             ? m_valobj->GetPreferredDisplayLanguage()
365*af732203SDimitry Andric             : m_options.m_varformat_language;
366*af732203SDimitry Andric     if (Language *lang_plugin = Language::FindPlugin(lang_type)) {
367*af732203SDimitry Andric       summary.assign(lang_plugin->GetNilReferenceSummaryString().str());
368*af732203SDimitry Andric     } else {
369*af732203SDimitry Andric       // We treat C as the fallback language rather than as a separate Language
370*af732203SDimitry Andric       // plugin.
371*af732203SDimitry Andric       summary.assign("NULL");
372*af732203SDimitry Andric     }
373*af732203SDimitry Andric   } else if (IsUninitialized()) {
3740b57cec5SDimitry Andric     summary.assign("<uninitialized>");
375*af732203SDimitry Andric   } else if (m_options.m_omit_summary_depth == 0) {
3760b57cec5SDimitry Andric     TypeSummaryImpl *entry = GetSummaryFormatter();
377*af732203SDimitry Andric     if (entry) {
3780b57cec5SDimitry Andric       m_valobj->GetSummaryAsCString(entry, summary,
3790b57cec5SDimitry Andric                                     m_options.m_varformat_language);
380*af732203SDimitry Andric     } else {
3810b57cec5SDimitry Andric       const char *sum_cstr =
3820b57cec5SDimitry Andric           m_valobj->GetSummaryAsCString(m_options.m_varformat_language);
3830b57cec5SDimitry Andric       if (sum_cstr)
3840b57cec5SDimitry Andric         summary.assign(sum_cstr);
3850b57cec5SDimitry Andric     }
3860b57cec5SDimitry Andric   }
3870b57cec5SDimitry Andric }
3880b57cec5SDimitry Andric 
PrintValueAndSummaryIfNeeded(bool & value_printed,bool & summary_printed)3890b57cec5SDimitry Andric bool ValueObjectPrinter::PrintValueAndSummaryIfNeeded(bool &value_printed,
3900b57cec5SDimitry Andric                                                       bool &summary_printed) {
3910b57cec5SDimitry Andric   bool error_printed = false;
3920b57cec5SDimitry Andric   if (ShouldPrintValueObject()) {
3930b57cec5SDimitry Andric     if (!CheckScopeIfNeeded())
3940b57cec5SDimitry Andric       m_error.assign("out of scope");
3950b57cec5SDimitry Andric     if (m_error.empty()) {
3960b57cec5SDimitry Andric       GetValueSummaryError(m_value, m_summary, m_error);
3970b57cec5SDimitry Andric     }
3980b57cec5SDimitry Andric     if (m_error.size()) {
3990b57cec5SDimitry Andric       // we need to support scenarios in which it is actually fine for a value
4000b57cec5SDimitry Andric       // to have no type but - on the other hand - if we get an error *AND*
4010b57cec5SDimitry Andric       // have no type, we try to get out gracefully, since most often that
4020b57cec5SDimitry Andric       // combination means "could not resolve a type" and the default failure
4030b57cec5SDimitry Andric       // mode is quite ugly
4040b57cec5SDimitry Andric       if (!m_compiler_type.IsValid()) {
4050b57cec5SDimitry Andric         m_stream->Printf(" <could not resolve type>");
4060b57cec5SDimitry Andric         return false;
4070b57cec5SDimitry Andric       }
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric       error_printed = true;
4100b57cec5SDimitry Andric       m_stream->Printf(" <%s>\n", m_error.c_str());
4110b57cec5SDimitry Andric     } else {
4120b57cec5SDimitry Andric       // Make sure we have a value and make sure the summary didn't specify
4130b57cec5SDimitry Andric       // that the value should not be printed - and do not print the value if
4140b57cec5SDimitry Andric       // this thing is nil (but show the value if the user passes a format
4150b57cec5SDimitry Andric       // explicitly)
4160b57cec5SDimitry Andric       TypeSummaryImpl *entry = GetSummaryFormatter();
417*af732203SDimitry Andric       const bool has_nil_or_uninitialized_summary =
418*af732203SDimitry Andric           (IsNil() || IsUninitialized()) && !m_summary.empty();
419*af732203SDimitry Andric       if (!has_nil_or_uninitialized_summary && !m_value.empty() &&
4200b57cec5SDimitry Andric           (entry == nullptr ||
4210b57cec5SDimitry Andric            (entry->DoesPrintValue(m_valobj) ||
4220b57cec5SDimitry Andric             m_options.m_format != eFormatDefault) ||
4230b57cec5SDimitry Andric            m_summary.empty()) &&
4240b57cec5SDimitry Andric           !m_options.m_hide_value) {
4250b57cec5SDimitry Andric         if (m_options.m_hide_pointer_value &&
4260b57cec5SDimitry Andric             IsPointerValue(m_valobj->GetCompilerType())) {
4270b57cec5SDimitry Andric         } else {
4280b57cec5SDimitry Andric           m_stream->Printf(" %s", m_value.c_str());
4290b57cec5SDimitry Andric           value_printed = true;
4300b57cec5SDimitry Andric         }
4310b57cec5SDimitry Andric       }
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric       if (m_summary.size()) {
4340b57cec5SDimitry Andric         m_stream->Printf(" %s", m_summary.c_str());
4350b57cec5SDimitry Andric         summary_printed = true;
4360b57cec5SDimitry Andric       }
4370b57cec5SDimitry Andric     }
4380b57cec5SDimitry Andric   }
4390b57cec5SDimitry Andric   return !error_printed;
4400b57cec5SDimitry Andric }
4410b57cec5SDimitry Andric 
PrintObjectDescriptionIfNeeded(bool value_printed,bool summary_printed)4420b57cec5SDimitry Andric bool ValueObjectPrinter::PrintObjectDescriptionIfNeeded(bool value_printed,
4430b57cec5SDimitry Andric                                                         bool summary_printed) {
4440b57cec5SDimitry Andric   if (ShouldPrintValueObject()) {
4450b57cec5SDimitry Andric     // let's avoid the overly verbose no description error for a nil thing
4460b57cec5SDimitry Andric     if (m_options.m_use_objc && !IsNil() && !IsUninitialized() &&
4470b57cec5SDimitry Andric         (!m_options.m_pointer_as_array)) {
4480b57cec5SDimitry Andric       if (!m_options.m_hide_value || !m_options.m_hide_name)
4490b57cec5SDimitry Andric         m_stream->Printf(" ");
4500b57cec5SDimitry Andric       const char *object_desc = nullptr;
4510b57cec5SDimitry Andric       if (value_printed || summary_printed)
4520b57cec5SDimitry Andric         object_desc = m_valobj->GetObjectDescription();
4530b57cec5SDimitry Andric       else
4540b57cec5SDimitry Andric         object_desc = GetDescriptionForDisplay();
4550b57cec5SDimitry Andric       if (object_desc && *object_desc) {
4560b57cec5SDimitry Andric         // If the description already ends with a \n don't add another one.
4570b57cec5SDimitry Andric         size_t object_end = strlen(object_desc) - 1;
4580b57cec5SDimitry Andric         if (object_desc[object_end] == '\n')
4590b57cec5SDimitry Andric           m_stream->Printf("%s", object_desc);
4600b57cec5SDimitry Andric         else
4610b57cec5SDimitry Andric           m_stream->Printf("%s\n", object_desc);
4620b57cec5SDimitry Andric         return true;
4630b57cec5SDimitry Andric       } else if (!value_printed && !summary_printed)
4640b57cec5SDimitry Andric         return true;
4650b57cec5SDimitry Andric       else
4660b57cec5SDimitry Andric         return false;
4670b57cec5SDimitry Andric     }
4680b57cec5SDimitry Andric   }
4690b57cec5SDimitry Andric   return true;
4700b57cec5SDimitry Andric }
4710b57cec5SDimitry Andric 
CanAllowExpansion() const4720b57cec5SDimitry Andric bool DumpValueObjectOptions::PointerDepth::CanAllowExpansion() const {
4730b57cec5SDimitry Andric   switch (m_mode) {
4740b57cec5SDimitry Andric   case Mode::Always:
4750b57cec5SDimitry Andric   case Mode::Default:
4760b57cec5SDimitry Andric     return m_count > 0;
4770b57cec5SDimitry Andric   case Mode::Never:
4780b57cec5SDimitry Andric     return false;
4790b57cec5SDimitry Andric   }
4800b57cec5SDimitry Andric   return false;
4810b57cec5SDimitry Andric }
4820b57cec5SDimitry Andric 
ShouldPrintChildren(bool is_failed_description,DumpValueObjectOptions::PointerDepth & curr_ptr_depth)4830b57cec5SDimitry Andric bool ValueObjectPrinter::ShouldPrintChildren(
4840b57cec5SDimitry Andric     bool is_failed_description,
4850b57cec5SDimitry Andric     DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
4860b57cec5SDimitry Andric   const bool is_ref = IsRef();
4870b57cec5SDimitry Andric   const bool is_ptr = IsPtr();
4880b57cec5SDimitry Andric   const bool is_uninit = IsUninitialized();
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric   if (is_uninit)
4910b57cec5SDimitry Andric     return false;
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric   // if the user has specified an element count, always print children as it is
4940b57cec5SDimitry Andric   // explicit user demand being honored
4950b57cec5SDimitry Andric   if (m_options.m_pointer_as_array)
4960b57cec5SDimitry Andric     return true;
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric   TypeSummaryImpl *entry = GetSummaryFormatter();
4990b57cec5SDimitry Andric 
5000b57cec5SDimitry Andric   if (m_options.m_use_objc)
5010b57cec5SDimitry Andric     return false;
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric   if (is_failed_description || m_curr_depth < m_options.m_max_depth) {
5040b57cec5SDimitry Andric     // We will show children for all concrete types. We won't show pointer
5050b57cec5SDimitry Andric     // contents unless a pointer depth has been specified. We won't reference
5060b57cec5SDimitry Andric     // contents unless the reference is the root object (depth of zero).
5070b57cec5SDimitry Andric 
5080b57cec5SDimitry Andric     // Use a new temporary pointer depth in case we override the current
5090b57cec5SDimitry Andric     // pointer depth below...
5100b57cec5SDimitry Andric 
5110b57cec5SDimitry Andric     if (is_ptr || is_ref) {
5120b57cec5SDimitry Andric       // We have a pointer or reference whose value is an address. Make sure
5130b57cec5SDimitry Andric       // that address is not NULL
5140b57cec5SDimitry Andric       AddressType ptr_address_type;
5150b57cec5SDimitry Andric       if (m_valobj->GetPointerValue(&ptr_address_type) == 0)
5160b57cec5SDimitry Andric         return false;
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric       const bool is_root_level = m_curr_depth == 0;
5190b57cec5SDimitry Andric 
5200b57cec5SDimitry Andric       if (is_ref && is_root_level) {
5210b57cec5SDimitry Andric         // If this is the root object (depth is zero) that we are showing and
5220b57cec5SDimitry Andric         // it is a reference, and no pointer depth has been supplied print out
5230b57cec5SDimitry Andric         // what it references. Don't do this at deeper depths otherwise we can
5240b57cec5SDimitry Andric         // end up with infinite recursion...
5250b57cec5SDimitry Andric         return true;
5260b57cec5SDimitry Andric       }
5270b57cec5SDimitry Andric 
5280b57cec5SDimitry Andric       return curr_ptr_depth.CanAllowExpansion();
5290b57cec5SDimitry Andric     }
5300b57cec5SDimitry Andric 
5310b57cec5SDimitry Andric     return (!entry || entry->DoesPrintChildren(m_valobj) || m_summary.empty());
5320b57cec5SDimitry Andric   }
5330b57cec5SDimitry Andric   return false;
5340b57cec5SDimitry Andric }
5350b57cec5SDimitry Andric 
ShouldExpandEmptyAggregates()5360b57cec5SDimitry Andric bool ValueObjectPrinter::ShouldExpandEmptyAggregates() {
5370b57cec5SDimitry Andric   TypeSummaryImpl *entry = GetSummaryFormatter();
5380b57cec5SDimitry Andric 
5390b57cec5SDimitry Andric   if (!entry)
5400b57cec5SDimitry Andric     return true;
5410b57cec5SDimitry Andric 
5420b57cec5SDimitry Andric   return entry->DoesPrintEmptyAggregates();
5430b57cec5SDimitry Andric }
5440b57cec5SDimitry Andric 
GetValueObjectForChildrenGeneration()5450b57cec5SDimitry Andric ValueObject *ValueObjectPrinter::GetValueObjectForChildrenGeneration() {
5460b57cec5SDimitry Andric   return m_valobj;
5470b57cec5SDimitry Andric }
5480b57cec5SDimitry Andric 
PrintChildrenPreamble()5490b57cec5SDimitry Andric void ValueObjectPrinter::PrintChildrenPreamble() {
5500b57cec5SDimitry Andric   if (m_options.m_flat_output) {
5510b57cec5SDimitry Andric     if (ShouldPrintValueObject())
5520b57cec5SDimitry Andric       m_stream->EOL();
5530b57cec5SDimitry Andric   } else {
5540b57cec5SDimitry Andric     if (ShouldPrintValueObject())
5550b57cec5SDimitry Andric       m_stream->PutCString(IsRef() ? ": {\n" : " {\n");
5560b57cec5SDimitry Andric     m_stream->IndentMore();
5570b57cec5SDimitry Andric   }
5580b57cec5SDimitry Andric }
5590b57cec5SDimitry Andric 
PrintChild(ValueObjectSP child_sp,const DumpValueObjectOptions::PointerDepth & curr_ptr_depth)5600b57cec5SDimitry Andric void ValueObjectPrinter::PrintChild(
5610b57cec5SDimitry Andric     ValueObjectSP child_sp,
5620b57cec5SDimitry Andric     const DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
5630b57cec5SDimitry Andric   const uint32_t consumed_depth = (!m_options.m_pointer_as_array) ? 1 : 0;
5640b57cec5SDimitry Andric   const bool does_consume_ptr_depth =
5650b57cec5SDimitry Andric       ((IsPtr() && !m_options.m_pointer_as_array) || IsRef());
5660b57cec5SDimitry Andric 
5670b57cec5SDimitry Andric   DumpValueObjectOptions child_options(m_options);
5680b57cec5SDimitry Andric   child_options.SetFormat(m_options.m_format)
5690b57cec5SDimitry Andric       .SetSummary()
5700b57cec5SDimitry Andric       .SetRootValueObjectName();
5710b57cec5SDimitry Andric   child_options.SetScopeChecked(true)
5720b57cec5SDimitry Andric       .SetHideName(m_options.m_hide_name)
5730b57cec5SDimitry Andric       .SetHideValue(m_options.m_hide_value)
5740b57cec5SDimitry Andric       .SetOmitSummaryDepth(child_options.m_omit_summary_depth > 1
5750b57cec5SDimitry Andric                                ? child_options.m_omit_summary_depth -
5760b57cec5SDimitry Andric                                      consumed_depth
5770b57cec5SDimitry Andric                                : 0)
5780b57cec5SDimitry Andric       .SetElementCount(0);
5790b57cec5SDimitry Andric 
5800b57cec5SDimitry Andric   if (child_sp.get()) {
5810b57cec5SDimitry Andric     ValueObjectPrinter child_printer(
5820b57cec5SDimitry Andric         child_sp.get(), m_stream, child_options,
5830b57cec5SDimitry Andric         does_consume_ptr_depth ? --curr_ptr_depth : curr_ptr_depth,
5840b57cec5SDimitry Andric         m_curr_depth + consumed_depth, m_printed_instance_pointers);
5850b57cec5SDimitry Andric     child_printer.PrintValueObject();
5860b57cec5SDimitry Andric   }
5870b57cec5SDimitry Andric }
5880b57cec5SDimitry Andric 
GetMaxNumChildrenToPrint(bool & print_dotdotdot)5890b57cec5SDimitry Andric uint32_t ValueObjectPrinter::GetMaxNumChildrenToPrint(bool &print_dotdotdot) {
5900b57cec5SDimitry Andric   ValueObject *synth_m_valobj = GetValueObjectForChildrenGeneration();
5910b57cec5SDimitry Andric 
5920b57cec5SDimitry Andric   if (m_options.m_pointer_as_array)
5930b57cec5SDimitry Andric     return m_options.m_pointer_as_array.m_element_count;
5940b57cec5SDimitry Andric 
5950b57cec5SDimitry Andric   size_t num_children = synth_m_valobj->GetNumChildren();
5960b57cec5SDimitry Andric   print_dotdotdot = false;
5970b57cec5SDimitry Andric   if (num_children) {
5980b57cec5SDimitry Andric     const size_t max_num_children =
5990b57cec5SDimitry Andric         m_valobj->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
6000b57cec5SDimitry Andric 
6010b57cec5SDimitry Andric     if (num_children > max_num_children && !m_options.m_ignore_cap) {
6020b57cec5SDimitry Andric       print_dotdotdot = true;
6030b57cec5SDimitry Andric       return max_num_children;
6040b57cec5SDimitry Andric     }
6050b57cec5SDimitry Andric   }
6060b57cec5SDimitry Andric   return num_children;
6070b57cec5SDimitry Andric }
6080b57cec5SDimitry Andric 
PrintChildrenPostamble(bool print_dotdotdot)6090b57cec5SDimitry Andric void ValueObjectPrinter::PrintChildrenPostamble(bool print_dotdotdot) {
6100b57cec5SDimitry Andric   if (!m_options.m_flat_output) {
6110b57cec5SDimitry Andric     if (print_dotdotdot) {
6120b57cec5SDimitry Andric       m_valobj->GetTargetSP()
6130b57cec5SDimitry Andric           ->GetDebugger()
6140b57cec5SDimitry Andric           .GetCommandInterpreter()
6150b57cec5SDimitry Andric           .ChildrenTruncated();
6160b57cec5SDimitry Andric       m_stream->Indent("...\n");
6170b57cec5SDimitry Andric     }
6180b57cec5SDimitry Andric     m_stream->IndentLess();
6190b57cec5SDimitry Andric     m_stream->Indent("}\n");
6200b57cec5SDimitry Andric   }
6210b57cec5SDimitry Andric }
6220b57cec5SDimitry Andric 
ShouldPrintEmptyBrackets(bool value_printed,bool summary_printed)6230b57cec5SDimitry Andric bool ValueObjectPrinter::ShouldPrintEmptyBrackets(bool value_printed,
6240b57cec5SDimitry Andric                                                   bool summary_printed) {
6250b57cec5SDimitry Andric   ValueObject *synth_m_valobj = GetValueObjectForChildrenGeneration();
6260b57cec5SDimitry Andric 
6270b57cec5SDimitry Andric   if (!IsAggregate())
6280b57cec5SDimitry Andric     return false;
6290b57cec5SDimitry Andric 
6300b57cec5SDimitry Andric   if (!m_options.m_reveal_empty_aggregates) {
6310b57cec5SDimitry Andric     if (value_printed || summary_printed)
6320b57cec5SDimitry Andric       return false;
6330b57cec5SDimitry Andric   }
6340b57cec5SDimitry Andric 
6350b57cec5SDimitry Andric   if (synth_m_valobj->MightHaveChildren())
6360b57cec5SDimitry Andric     return true;
6370b57cec5SDimitry Andric 
6380b57cec5SDimitry Andric   if (m_val_summary_ok)
6390b57cec5SDimitry Andric     return false;
6400b57cec5SDimitry Andric 
6410b57cec5SDimitry Andric   return true;
6420b57cec5SDimitry Andric }
6430b57cec5SDimitry Andric 
PhysicalIndexForLogicalIndex(size_t base,size_t stride,size_t logical)6440b57cec5SDimitry Andric static constexpr size_t PhysicalIndexForLogicalIndex(size_t base, size_t stride,
6450b57cec5SDimitry Andric                                                      size_t logical) {
6460b57cec5SDimitry Andric   return base + logical * stride;
6470b57cec5SDimitry Andric }
6480b57cec5SDimitry Andric 
GenerateChild(ValueObject * synth_valobj,size_t idx)6490b57cec5SDimitry Andric ValueObjectSP ValueObjectPrinter::GenerateChild(ValueObject *synth_valobj,
6500b57cec5SDimitry Andric                                                 size_t idx) {
6510b57cec5SDimitry Andric   if (m_options.m_pointer_as_array) {
6520b57cec5SDimitry Andric     // if generating pointer-as-array children, use GetSyntheticArrayMember
6530b57cec5SDimitry Andric     return synth_valobj->GetSyntheticArrayMember(
6540b57cec5SDimitry Andric         PhysicalIndexForLogicalIndex(
6550b57cec5SDimitry Andric             m_options.m_pointer_as_array.m_base_element,
6560b57cec5SDimitry Andric             m_options.m_pointer_as_array.m_stride, idx),
6570b57cec5SDimitry Andric         true);
6580b57cec5SDimitry Andric   } else {
6590b57cec5SDimitry Andric     // otherwise, do the usual thing
6600b57cec5SDimitry Andric     return synth_valobj->GetChildAtIndex(idx, true);
6610b57cec5SDimitry Andric   }
6620b57cec5SDimitry Andric }
6630b57cec5SDimitry Andric 
PrintChildren(bool value_printed,bool summary_printed,const DumpValueObjectOptions::PointerDepth & curr_ptr_depth)6640b57cec5SDimitry Andric void ValueObjectPrinter::PrintChildren(
6650b57cec5SDimitry Andric     bool value_printed, bool summary_printed,
6660b57cec5SDimitry Andric     const DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
6670b57cec5SDimitry Andric   ValueObject *synth_m_valobj = GetValueObjectForChildrenGeneration();
6680b57cec5SDimitry Andric 
6690b57cec5SDimitry Andric   bool print_dotdotdot = false;
6700b57cec5SDimitry Andric   size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
6710b57cec5SDimitry Andric   if (num_children) {
6720b57cec5SDimitry Andric     bool any_children_printed = false;
6730b57cec5SDimitry Andric 
6740b57cec5SDimitry Andric     for (size_t idx = 0; idx < num_children; ++idx) {
6750b57cec5SDimitry Andric       if (ValueObjectSP child_sp = GenerateChild(synth_m_valobj, idx)) {
6760b57cec5SDimitry Andric         if (!any_children_printed) {
6770b57cec5SDimitry Andric           PrintChildrenPreamble();
6780b57cec5SDimitry Andric           any_children_printed = true;
6790b57cec5SDimitry Andric         }
6800b57cec5SDimitry Andric         PrintChild(child_sp, curr_ptr_depth);
6810b57cec5SDimitry Andric       }
6820b57cec5SDimitry Andric     }
6830b57cec5SDimitry Andric 
6840b57cec5SDimitry Andric     if (any_children_printed)
6850b57cec5SDimitry Andric       PrintChildrenPostamble(print_dotdotdot);
6860b57cec5SDimitry Andric     else {
6870b57cec5SDimitry Andric       if (ShouldPrintEmptyBrackets(value_printed, summary_printed)) {
6880b57cec5SDimitry Andric         if (ShouldPrintValueObject())
6890b57cec5SDimitry Andric           m_stream->PutCString(" {}\n");
6900b57cec5SDimitry Andric         else
6910b57cec5SDimitry Andric           m_stream->EOL();
6920b57cec5SDimitry Andric       } else
6930b57cec5SDimitry Andric         m_stream->EOL();
6940b57cec5SDimitry Andric     }
6950b57cec5SDimitry Andric   } else if (ShouldPrintEmptyBrackets(value_printed, summary_printed)) {
6960b57cec5SDimitry Andric     // Aggregate, no children...
6970b57cec5SDimitry Andric     if (ShouldPrintValueObject()) {
6980b57cec5SDimitry Andric       // if it has a synthetic value, then don't print {}, the synthetic
6990b57cec5SDimitry Andric       // children are probably only being used to vend a value
7000b57cec5SDimitry Andric       if (m_valobj->DoesProvideSyntheticValue() ||
7010b57cec5SDimitry Andric           !ShouldExpandEmptyAggregates())
7020b57cec5SDimitry Andric         m_stream->PutCString("\n");
7030b57cec5SDimitry Andric       else
7040b57cec5SDimitry Andric         m_stream->PutCString(" {}\n");
7050b57cec5SDimitry Andric     }
7060b57cec5SDimitry Andric   } else {
7070b57cec5SDimitry Andric     if (ShouldPrintValueObject())
7080b57cec5SDimitry Andric       m_stream->EOL();
7090b57cec5SDimitry Andric   }
7100b57cec5SDimitry Andric }
7110b57cec5SDimitry Andric 
PrintChildrenOneLiner(bool hide_names)7120b57cec5SDimitry Andric bool ValueObjectPrinter::PrintChildrenOneLiner(bool hide_names) {
7130b57cec5SDimitry Andric   if (!GetMostSpecializedValue() || m_valobj == nullptr)
7140b57cec5SDimitry Andric     return false;
7150b57cec5SDimitry Andric 
7160b57cec5SDimitry Andric   ValueObject *synth_m_valobj = GetValueObjectForChildrenGeneration();
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric   bool print_dotdotdot = false;
7190b57cec5SDimitry Andric   size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
7200b57cec5SDimitry Andric 
7210b57cec5SDimitry Andric   if (num_children) {
7220b57cec5SDimitry Andric     m_stream->PutChar('(');
7230b57cec5SDimitry Andric 
7240b57cec5SDimitry Andric     for (uint32_t idx = 0; idx < num_children; ++idx) {
7250b57cec5SDimitry Andric       lldb::ValueObjectSP child_sp(synth_m_valobj->GetChildAtIndex(idx, true));
7260b57cec5SDimitry Andric       if (child_sp)
7270b57cec5SDimitry Andric         child_sp = child_sp->GetQualifiedRepresentationIfAvailable(
7280b57cec5SDimitry Andric             m_options.m_use_dynamic, m_options.m_use_synthetic);
7290b57cec5SDimitry Andric       if (child_sp) {
7300b57cec5SDimitry Andric         if (idx)
7310b57cec5SDimitry Andric           m_stream->PutCString(", ");
7320b57cec5SDimitry Andric         if (!hide_names) {
7330b57cec5SDimitry Andric           const char *name = child_sp.get()->GetName().AsCString();
7340b57cec5SDimitry Andric           if (name && *name) {
7350b57cec5SDimitry Andric             m_stream->PutCString(name);
7360b57cec5SDimitry Andric             m_stream->PutCString(" = ");
7370b57cec5SDimitry Andric           }
7380b57cec5SDimitry Andric         }
7390b57cec5SDimitry Andric         child_sp->DumpPrintableRepresentation(
7400b57cec5SDimitry Andric             *m_stream, ValueObject::eValueObjectRepresentationStyleSummary,
7410b57cec5SDimitry Andric             m_options.m_format,
7420b57cec5SDimitry Andric             ValueObject::PrintableRepresentationSpecialCases::eDisable);
7430b57cec5SDimitry Andric       }
7440b57cec5SDimitry Andric     }
7450b57cec5SDimitry Andric 
7460b57cec5SDimitry Andric     if (print_dotdotdot)
7470b57cec5SDimitry Andric       m_stream->PutCString(", ...)");
7480b57cec5SDimitry Andric     else
7490b57cec5SDimitry Andric       m_stream->PutChar(')');
7500b57cec5SDimitry Andric   }
7510b57cec5SDimitry Andric   return true;
7520b57cec5SDimitry Andric }
7530b57cec5SDimitry Andric 
PrintChildrenIfNeeded(bool value_printed,bool summary_printed)7540b57cec5SDimitry Andric void ValueObjectPrinter::PrintChildrenIfNeeded(bool value_printed,
7550b57cec5SDimitry Andric                                                bool summary_printed) {
7569dba64beSDimitry Andric   // This flag controls whether we tried to display a description for this
7579dba64beSDimitry Andric   // object and failed if that happens, we want to display the children if any.
7580b57cec5SDimitry Andric   bool is_failed_description =
7590b57cec5SDimitry Andric       !PrintObjectDescriptionIfNeeded(value_printed, summary_printed);
7600b57cec5SDimitry Andric 
7619dba64beSDimitry Andric   DumpValueObjectOptions::PointerDepth curr_ptr_depth = m_ptr_depth;
7629dba64beSDimitry Andric   const bool print_children =
7630b57cec5SDimitry Andric       ShouldPrintChildren(is_failed_description, curr_ptr_depth);
7649dba64beSDimitry Andric   const bool print_oneline =
7650b57cec5SDimitry Andric       (curr_ptr_depth.CanAllowExpansion() || m_options.m_show_types ||
7660b57cec5SDimitry Andric        !m_options.m_allow_oneliner_mode || m_options.m_flat_output ||
7670b57cec5SDimitry Andric        (m_options.m_pointer_as_array) || m_options.m_show_location)
7680b57cec5SDimitry Andric           ? false
7690b57cec5SDimitry Andric           : DataVisualization::ShouldPrintAsOneLiner(*m_valobj);
7709dba64beSDimitry Andric   if (print_children && IsInstancePointer()) {
7719dba64beSDimitry Andric     uint64_t instance_ptr_value = m_valobj->GetValueAsUnsigned(0);
7720b57cec5SDimitry Andric     if (m_printed_instance_pointers->count(instance_ptr_value)) {
7739dba64beSDimitry Andric       // We already printed this instance-is-pointer thing, so don't expand it.
7740b57cec5SDimitry Andric       m_stream->PutCString(" {...}\n");
7750b57cec5SDimitry Andric       return;
7769dba64beSDimitry Andric     } else {
7779dba64beSDimitry Andric       // Remember this guy for future reference.
7789dba64beSDimitry Andric       m_printed_instance_pointers->emplace(instance_ptr_value);
7799dba64beSDimitry Andric     }
7800b57cec5SDimitry Andric   }
7810b57cec5SDimitry Andric 
7820b57cec5SDimitry Andric   if (print_children) {
7830b57cec5SDimitry Andric     if (print_oneline) {
7840b57cec5SDimitry Andric       m_stream->PutChar(' ');
7850b57cec5SDimitry Andric       PrintChildrenOneLiner(false);
7860b57cec5SDimitry Andric       m_stream->EOL();
7870b57cec5SDimitry Andric     } else
7880b57cec5SDimitry Andric       PrintChildren(value_printed, summary_printed, curr_ptr_depth);
7890b57cec5SDimitry Andric   } else if (m_curr_depth >= m_options.m_max_depth && IsAggregate() &&
7900b57cec5SDimitry Andric              ShouldPrintValueObject()) {
7910b57cec5SDimitry Andric     m_stream->PutCString("{...}\n");
7920b57cec5SDimitry Andric   } else
7930b57cec5SDimitry Andric     m_stream->EOL();
7940b57cec5SDimitry Andric }
795