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() {
74*fe013be4SDimitry Andric   if (!m_orig_valobj)
75*fe013be4SDimitry Andric     return false;
76*fe013be4SDimitry Andric 
77*fe013be4SDimitry Andric   // If the incoming ValueObject is in an error state, the best we're going to
78*fe013be4SDimitry Andric   // get out of it is its type.  But if we don't even have that, just print
79*fe013be4SDimitry Andric   // the error and exit early.
80*fe013be4SDimitry Andric   if (m_orig_valobj->GetError().Fail()
81*fe013be4SDimitry Andric       && !m_orig_valobj->GetCompilerType().IsValid()) {
82*fe013be4SDimitry Andric     m_stream->Printf("Error: '%s'", m_orig_valobj->GetError().AsCString());
83*fe013be4SDimitry Andric     return true;
84*fe013be4SDimitry Andric   }
85*fe013be4SDimitry Andric 
860b57cec5SDimitry Andric   if (!GetMostSpecializedValue() || m_valobj == nullptr)
870b57cec5SDimitry Andric     return false;
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric   if (ShouldPrintValueObject()) {
900b57cec5SDimitry Andric     PrintLocationIfNeeded();
910b57cec5SDimitry Andric     m_stream->Indent();
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric     PrintDecl();
940b57cec5SDimitry Andric   }
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   bool value_printed = false;
970b57cec5SDimitry Andric   bool summary_printed = false;
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric   m_val_summary_ok =
1000b57cec5SDimitry Andric       PrintValueAndSummaryIfNeeded(value_printed, summary_printed);
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric   if (m_val_summary_ok)
1030b57cec5SDimitry Andric     PrintChildrenIfNeeded(value_printed, summary_printed);
1040b57cec5SDimitry Andric   else
1050b57cec5SDimitry Andric     m_stream->EOL();
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric   return true;
1080b57cec5SDimitry Andric }
1090b57cec5SDimitry Andric 
GetMostSpecializedValue()1100b57cec5SDimitry Andric bool ValueObjectPrinter::GetMostSpecializedValue() {
1110b57cec5SDimitry Andric   if (m_valobj)
1120b57cec5SDimitry Andric     return true;
1130b57cec5SDimitry Andric   bool update_success = m_orig_valobj->UpdateValueIfNeeded(true);
1140b57cec5SDimitry Andric   if (!update_success) {
1150b57cec5SDimitry Andric     m_valobj = m_orig_valobj;
1160b57cec5SDimitry Andric   } else {
1170b57cec5SDimitry Andric     if (m_orig_valobj->IsDynamic()) {
1180b57cec5SDimitry Andric       if (m_options.m_use_dynamic == eNoDynamicValues) {
1190b57cec5SDimitry Andric         ValueObject *static_value = m_orig_valobj->GetStaticValue().get();
1200b57cec5SDimitry Andric         if (static_value)
1210b57cec5SDimitry Andric           m_valobj = static_value;
1220b57cec5SDimitry Andric         else
1230b57cec5SDimitry Andric           m_valobj = m_orig_valobj;
1240b57cec5SDimitry Andric       } else
1250b57cec5SDimitry Andric         m_valobj = m_orig_valobj;
1260b57cec5SDimitry Andric     } else {
1270b57cec5SDimitry Andric       if (m_options.m_use_dynamic != eNoDynamicValues) {
1280b57cec5SDimitry Andric         ValueObject *dynamic_value =
1290b57cec5SDimitry Andric             m_orig_valobj->GetDynamicValue(m_options.m_use_dynamic).get();
1300b57cec5SDimitry Andric         if (dynamic_value)
1310b57cec5SDimitry Andric           m_valobj = dynamic_value;
1320b57cec5SDimitry Andric         else
1330b57cec5SDimitry Andric           m_valobj = m_orig_valobj;
1340b57cec5SDimitry Andric       } else
1350b57cec5SDimitry Andric         m_valobj = m_orig_valobj;
1360b57cec5SDimitry Andric     }
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric     if (m_valobj->IsSynthetic()) {
1390b57cec5SDimitry Andric       if (!m_options.m_use_synthetic) {
1400b57cec5SDimitry Andric         ValueObject *non_synthetic = m_valobj->GetNonSyntheticValue().get();
1410b57cec5SDimitry Andric         if (non_synthetic)
1420b57cec5SDimitry Andric           m_valobj = non_synthetic;
1430b57cec5SDimitry Andric       }
1440b57cec5SDimitry Andric     } else {
1450b57cec5SDimitry Andric       if (m_options.m_use_synthetic) {
1460b57cec5SDimitry Andric         ValueObject *synthetic = m_valobj->GetSyntheticValue().get();
1470b57cec5SDimitry Andric         if (synthetic)
1480b57cec5SDimitry Andric           m_valobj = synthetic;
1490b57cec5SDimitry Andric       }
1500b57cec5SDimitry Andric     }
1510b57cec5SDimitry Andric   }
1520b57cec5SDimitry Andric   m_compiler_type = m_valobj->GetCompilerType();
1530b57cec5SDimitry Andric   m_type_flags = m_compiler_type.GetTypeInfo();
1540b57cec5SDimitry Andric   return true;
1550b57cec5SDimitry Andric }
1560b57cec5SDimitry Andric 
GetDescriptionForDisplay()1570b57cec5SDimitry Andric const char *ValueObjectPrinter::GetDescriptionForDisplay() {
1580b57cec5SDimitry Andric   const char *str = m_valobj->GetObjectDescription();
1590b57cec5SDimitry Andric   if (!str)
1600b57cec5SDimitry Andric     str = m_valobj->GetSummaryAsCString();
1610b57cec5SDimitry Andric   if (!str)
1620b57cec5SDimitry Andric     str = m_valobj->GetValueAsCString();
1630b57cec5SDimitry Andric   return str;
1640b57cec5SDimitry Andric }
1650b57cec5SDimitry Andric 
GetRootNameForDisplay()1665ffd83dbSDimitry Andric const char *ValueObjectPrinter::GetRootNameForDisplay() {
1670b57cec5SDimitry Andric   const char *root_valobj_name = m_options.m_root_valobj_name.empty()
1680b57cec5SDimitry Andric                                      ? m_valobj->GetName().AsCString()
1690b57cec5SDimitry Andric                                      : m_options.m_root_valobj_name.c_str();
1705ffd83dbSDimitry Andric   return root_valobj_name ? root_valobj_name : "";
1710b57cec5SDimitry Andric }
1720b57cec5SDimitry Andric 
ShouldPrintValueObject()1730b57cec5SDimitry Andric bool ValueObjectPrinter::ShouldPrintValueObject() {
1740b57cec5SDimitry Andric   if (m_should_print == eLazyBoolCalculate)
1750b57cec5SDimitry Andric     m_should_print =
1760b57cec5SDimitry Andric         (!m_options.m_flat_output || m_type_flags.Test(eTypeHasValue))
1770b57cec5SDimitry Andric             ? eLazyBoolYes
1780b57cec5SDimitry Andric             : eLazyBoolNo;
1790b57cec5SDimitry Andric   return m_should_print == eLazyBoolYes;
1800b57cec5SDimitry Andric }
1810b57cec5SDimitry Andric 
IsNil()1820b57cec5SDimitry Andric bool ValueObjectPrinter::IsNil() {
1830b57cec5SDimitry Andric   if (m_is_nil == eLazyBoolCalculate)
1840b57cec5SDimitry Andric     m_is_nil = m_valobj->IsNilReference() ? eLazyBoolYes : eLazyBoolNo;
1850b57cec5SDimitry Andric   return m_is_nil == eLazyBoolYes;
1860b57cec5SDimitry Andric }
1870b57cec5SDimitry Andric 
IsUninitialized()1880b57cec5SDimitry Andric bool ValueObjectPrinter::IsUninitialized() {
1890b57cec5SDimitry Andric   if (m_is_uninit == eLazyBoolCalculate)
1900b57cec5SDimitry Andric     m_is_uninit =
1910b57cec5SDimitry Andric         m_valobj->IsUninitializedReference() ? eLazyBoolYes : eLazyBoolNo;
1920b57cec5SDimitry Andric   return m_is_uninit == eLazyBoolYes;
1930b57cec5SDimitry Andric }
1940b57cec5SDimitry Andric 
IsPtr()1950b57cec5SDimitry Andric bool ValueObjectPrinter::IsPtr() {
1960b57cec5SDimitry Andric   if (m_is_ptr == eLazyBoolCalculate)
1970b57cec5SDimitry Andric     m_is_ptr = m_type_flags.Test(eTypeIsPointer) ? eLazyBoolYes : eLazyBoolNo;
1980b57cec5SDimitry Andric   return m_is_ptr == eLazyBoolYes;
1990b57cec5SDimitry Andric }
2000b57cec5SDimitry Andric 
IsRef()2010b57cec5SDimitry Andric bool ValueObjectPrinter::IsRef() {
2020b57cec5SDimitry Andric   if (m_is_ref == eLazyBoolCalculate)
2030b57cec5SDimitry Andric     m_is_ref = m_type_flags.Test(eTypeIsReference) ? eLazyBoolYes : eLazyBoolNo;
2040b57cec5SDimitry Andric   return m_is_ref == eLazyBoolYes;
2050b57cec5SDimitry Andric }
2060b57cec5SDimitry Andric 
IsAggregate()2070b57cec5SDimitry Andric bool ValueObjectPrinter::IsAggregate() {
2080b57cec5SDimitry Andric   if (m_is_aggregate == eLazyBoolCalculate)
2090b57cec5SDimitry Andric     m_is_aggregate =
2100b57cec5SDimitry Andric         m_type_flags.Test(eTypeHasChildren) ? eLazyBoolYes : eLazyBoolNo;
2110b57cec5SDimitry Andric   return m_is_aggregate == eLazyBoolYes;
2120b57cec5SDimitry Andric }
2130b57cec5SDimitry Andric 
IsInstancePointer()2140b57cec5SDimitry Andric bool ValueObjectPrinter::IsInstancePointer() {
2150b57cec5SDimitry Andric   // you need to do this check on the value's clang type
2160b57cec5SDimitry Andric   if (m_is_instance_ptr == eLazyBoolCalculate)
2170b57cec5SDimitry Andric     m_is_instance_ptr = (m_valobj->GetValue().GetCompilerType().GetTypeInfo() &
2180b57cec5SDimitry Andric                          eTypeInstanceIsPointer) != 0
2190b57cec5SDimitry Andric                             ? eLazyBoolYes
2200b57cec5SDimitry Andric                             : eLazyBoolNo;
2210b57cec5SDimitry Andric   if ((eLazyBoolYes == m_is_instance_ptr) && m_valobj->IsBaseClass())
2220b57cec5SDimitry Andric     m_is_instance_ptr = eLazyBoolNo;
2230b57cec5SDimitry Andric   return m_is_instance_ptr == eLazyBoolYes;
2240b57cec5SDimitry Andric }
2250b57cec5SDimitry Andric 
PrintLocationIfNeeded()2260b57cec5SDimitry Andric bool ValueObjectPrinter::PrintLocationIfNeeded() {
2270b57cec5SDimitry Andric   if (m_options.m_show_location) {
2280b57cec5SDimitry Andric     m_stream->Printf("%s: ", m_valobj->GetLocationAsCString());
2290b57cec5SDimitry Andric     return true;
2300b57cec5SDimitry Andric   }
2310b57cec5SDimitry Andric   return false;
2320b57cec5SDimitry Andric }
2330b57cec5SDimitry Andric 
PrintDecl()2340b57cec5SDimitry Andric void ValueObjectPrinter::PrintDecl() {
2350b57cec5SDimitry Andric   bool show_type = true;
2360b57cec5SDimitry Andric   // if we are at the root-level and been asked to hide the root's type, then
2370b57cec5SDimitry Andric   // hide it
2380b57cec5SDimitry Andric   if (m_curr_depth == 0 && m_options.m_hide_root_type)
2390b57cec5SDimitry Andric     show_type = false;
2400b57cec5SDimitry Andric   else
2410b57cec5SDimitry Andric     // otherwise decide according to the usual rules (asked to show types -
2420b57cec5SDimitry Andric     // always at the root level)
2430b57cec5SDimitry Andric     show_type = m_options.m_show_types ||
2440b57cec5SDimitry Andric                 (m_curr_depth == 0 && !m_options.m_flat_output);
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric   StreamString typeName;
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric   // always show the type at the root level if it is invalid
2490b57cec5SDimitry Andric   if (show_type) {
2500b57cec5SDimitry Andric     // Some ValueObjects don't have types (like registers sets). Only print the
2510b57cec5SDimitry Andric     // type if there is one to print
2520b57cec5SDimitry Andric     ConstString type_name;
2530b57cec5SDimitry Andric     if (m_compiler_type.IsValid()) {
2545ffd83dbSDimitry Andric       type_name = m_options.m_use_type_display_name
2555ffd83dbSDimitry Andric                       ? m_valobj->GetDisplayTypeName()
2565ffd83dbSDimitry Andric                       : m_valobj->GetQualifiedTypeName();
2570b57cec5SDimitry Andric     } else {
2580b57cec5SDimitry Andric       // only show an invalid type name if the user explicitly triggered
2590b57cec5SDimitry Andric       // show_type
2600b57cec5SDimitry Andric       if (m_options.m_show_types)
2610b57cec5SDimitry Andric         type_name = ConstString("<invalid type>");
2620b57cec5SDimitry Andric     }
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric     if (type_name) {
2650b57cec5SDimitry Andric       std::string type_name_str(type_name.GetCString());
2660b57cec5SDimitry Andric       if (m_options.m_hide_pointer_value) {
2670b57cec5SDimitry Andric         for (auto iter = type_name_str.find(" *"); iter != std::string::npos;
2680b57cec5SDimitry Andric              iter = type_name_str.find(" *")) {
2690b57cec5SDimitry Andric           type_name_str.erase(iter, 2);
2700b57cec5SDimitry Andric         }
2710b57cec5SDimitry Andric       }
2725ffd83dbSDimitry Andric       typeName << type_name_str.c_str();
2730b57cec5SDimitry Andric     }
2740b57cec5SDimitry Andric   }
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric   StreamString varName;
2770b57cec5SDimitry Andric 
278*fe013be4SDimitry Andric   if (ShouldShowName()) {
2795ffd83dbSDimitry Andric     if (m_options.m_flat_output)
2805ffd83dbSDimitry Andric       m_valobj->GetExpressionPath(varName);
2815ffd83dbSDimitry Andric     else
2825ffd83dbSDimitry Andric       varName << GetRootNameForDisplay();
2830b57cec5SDimitry Andric   }
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric   bool decl_printed = false;
2860b57cec5SDimitry Andric   if (!m_options.m_decl_printing_helper) {
2870b57cec5SDimitry Andric     // if the user didn't give us a custom helper, pick one based upon the
2880b57cec5SDimitry Andric     // language, either the one that this printer is bound to, or the preferred
2890b57cec5SDimitry Andric     // one for the ValueObject
2900b57cec5SDimitry Andric     lldb::LanguageType lang_type =
2910b57cec5SDimitry Andric         (m_options.m_varformat_language == lldb::eLanguageTypeUnknown)
2920b57cec5SDimitry Andric             ? m_valobj->GetPreferredDisplayLanguage()
2930b57cec5SDimitry Andric             : m_options.m_varformat_language;
2940b57cec5SDimitry Andric     if (Language *lang_plugin = Language::FindPlugin(lang_type)) {
2950b57cec5SDimitry Andric       m_options.m_decl_printing_helper = lang_plugin->GetDeclPrintingHelper();
2960b57cec5SDimitry Andric     }
2970b57cec5SDimitry Andric   }
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric   if (m_options.m_decl_printing_helper) {
3000b57cec5SDimitry Andric     ConstString type_name_cstr(typeName.GetString());
3010b57cec5SDimitry Andric     ConstString var_name_cstr(varName.GetString());
3020b57cec5SDimitry Andric 
303*fe013be4SDimitry Andric     DumpValueObjectOptions decl_print_options = m_options;
304*fe013be4SDimitry Andric     // Pass printing helpers an option object that indicates whether the name
305*fe013be4SDimitry Andric     // should be shown or hidden.
306*fe013be4SDimitry Andric     decl_print_options.SetHideName(!ShouldShowName());
307*fe013be4SDimitry Andric 
3080b57cec5SDimitry Andric     StreamString dest_stream;
3090b57cec5SDimitry Andric     if (m_options.m_decl_printing_helper(type_name_cstr, var_name_cstr,
310*fe013be4SDimitry Andric                                          decl_print_options, dest_stream)) {
3110b57cec5SDimitry Andric       decl_printed = true;
3120b57cec5SDimitry Andric       m_stream->PutCString(dest_stream.GetString());
3130b57cec5SDimitry Andric     }
3140b57cec5SDimitry Andric   }
3150b57cec5SDimitry Andric 
3160b57cec5SDimitry Andric   // if the helper failed, or there is none, do a default thing
3170b57cec5SDimitry Andric   if (!decl_printed) {
3180b57cec5SDimitry Andric     if (!typeName.Empty())
3190b57cec5SDimitry Andric       m_stream->Printf("(%s) ", typeName.GetData());
3200b57cec5SDimitry Andric     if (!varName.Empty())
3210b57cec5SDimitry Andric       m_stream->Printf("%s =", varName.GetData());
322*fe013be4SDimitry Andric     else if (ShouldShowName())
3230b57cec5SDimitry Andric       m_stream->Printf(" =");
3240b57cec5SDimitry Andric   }
3250b57cec5SDimitry Andric }
3260b57cec5SDimitry Andric 
CheckScopeIfNeeded()3270b57cec5SDimitry Andric bool ValueObjectPrinter::CheckScopeIfNeeded() {
3280b57cec5SDimitry Andric   if (m_options.m_scope_already_checked)
3290b57cec5SDimitry Andric     return true;
3300b57cec5SDimitry Andric   return m_valobj->IsInScope();
3310b57cec5SDimitry Andric }
3320b57cec5SDimitry Andric 
GetSummaryFormatter(bool null_if_omitted)3330b57cec5SDimitry Andric TypeSummaryImpl *ValueObjectPrinter::GetSummaryFormatter(bool null_if_omitted) {
3340b57cec5SDimitry Andric   if (!m_summary_formatter.second) {
3350b57cec5SDimitry Andric     TypeSummaryImpl *entry = m_options.m_summary_sp
3360b57cec5SDimitry Andric                                  ? m_options.m_summary_sp.get()
3370b57cec5SDimitry Andric                                  : m_valobj->GetSummaryFormat().get();
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric     if (m_options.m_omit_summary_depth > 0)
3400b57cec5SDimitry Andric       entry = nullptr;
3410b57cec5SDimitry Andric     m_summary_formatter.first = entry;
3420b57cec5SDimitry Andric     m_summary_formatter.second = true;
3430b57cec5SDimitry Andric   }
3440b57cec5SDimitry Andric   if (m_options.m_omit_summary_depth > 0 && null_if_omitted)
3450b57cec5SDimitry Andric     return nullptr;
3460b57cec5SDimitry Andric   return m_summary_formatter.first;
3470b57cec5SDimitry Andric }
3480b57cec5SDimitry Andric 
IsPointerValue(const CompilerType & type)3490b57cec5SDimitry Andric static bool IsPointerValue(const CompilerType &type) {
3500b57cec5SDimitry Andric   Flags type_flags(type.GetTypeInfo());
3510b57cec5SDimitry Andric   if (type_flags.AnySet(eTypeInstanceIsPointer | eTypeIsPointer))
3520b57cec5SDimitry Andric     return type_flags.AllClear(eTypeIsBuiltIn);
3530b57cec5SDimitry Andric   return false;
3540b57cec5SDimitry Andric }
3550b57cec5SDimitry Andric 
GetValueSummaryError(std::string & value,std::string & summary,std::string & error)3560b57cec5SDimitry Andric void ValueObjectPrinter::GetValueSummaryError(std::string &value,
3570b57cec5SDimitry Andric                                               std::string &summary,
3580b57cec5SDimitry Andric                                               std::string &error) {
3590b57cec5SDimitry Andric   lldb::Format format = m_options.m_format;
3600b57cec5SDimitry Andric   // if I am printing synthetized elements, apply the format to those elements
3610b57cec5SDimitry Andric   // only
3620b57cec5SDimitry Andric   if (m_options.m_pointer_as_array)
3630b57cec5SDimitry Andric     m_valobj->GetValueAsCString(lldb::eFormatDefault, value);
3640b57cec5SDimitry Andric   else if (format != eFormatDefault && format != m_valobj->GetFormat())
3650b57cec5SDimitry Andric     m_valobj->GetValueAsCString(format, value);
3660b57cec5SDimitry Andric   else {
3670b57cec5SDimitry Andric     const char *val_cstr = m_valobj->GetValueAsCString();
3680b57cec5SDimitry Andric     if (val_cstr)
3690b57cec5SDimitry Andric       value.assign(val_cstr);
3700b57cec5SDimitry Andric   }
3710b57cec5SDimitry Andric   const char *err_cstr = m_valobj->GetError().AsCString();
3720b57cec5SDimitry Andric   if (err_cstr)
3730b57cec5SDimitry Andric     error.assign(err_cstr);
3740b57cec5SDimitry Andric 
375e8d8bef9SDimitry Andric   if (!ShouldPrintValueObject())
376e8d8bef9SDimitry Andric     return;
377e8d8bef9SDimitry Andric 
378e8d8bef9SDimitry Andric   if (IsNil()) {
379e8d8bef9SDimitry Andric     lldb::LanguageType lang_type =
380e8d8bef9SDimitry Andric         (m_options.m_varformat_language == lldb::eLanguageTypeUnknown)
381e8d8bef9SDimitry Andric             ? m_valobj->GetPreferredDisplayLanguage()
382e8d8bef9SDimitry Andric             : m_options.m_varformat_language;
383e8d8bef9SDimitry Andric     if (Language *lang_plugin = Language::FindPlugin(lang_type)) {
384e8d8bef9SDimitry Andric       summary.assign(lang_plugin->GetNilReferenceSummaryString().str());
385e8d8bef9SDimitry Andric     } else {
386e8d8bef9SDimitry Andric       // We treat C as the fallback language rather than as a separate Language
387e8d8bef9SDimitry Andric       // plugin.
388e8d8bef9SDimitry Andric       summary.assign("NULL");
389e8d8bef9SDimitry Andric     }
390e8d8bef9SDimitry Andric   } else if (IsUninitialized()) {
3910b57cec5SDimitry Andric     summary.assign("<uninitialized>");
392e8d8bef9SDimitry Andric   } else if (m_options.m_omit_summary_depth == 0) {
3930b57cec5SDimitry Andric     TypeSummaryImpl *entry = GetSummaryFormatter();
394e8d8bef9SDimitry Andric     if (entry) {
3950b57cec5SDimitry Andric       m_valobj->GetSummaryAsCString(entry, summary,
3960b57cec5SDimitry Andric                                     m_options.m_varformat_language);
397e8d8bef9SDimitry Andric     } else {
3980b57cec5SDimitry Andric       const char *sum_cstr =
3990b57cec5SDimitry Andric           m_valobj->GetSummaryAsCString(m_options.m_varformat_language);
4000b57cec5SDimitry Andric       if (sum_cstr)
4010b57cec5SDimitry Andric         summary.assign(sum_cstr);
4020b57cec5SDimitry Andric     }
4030b57cec5SDimitry Andric   }
4040b57cec5SDimitry Andric }
4050b57cec5SDimitry Andric 
PrintValueAndSummaryIfNeeded(bool & value_printed,bool & summary_printed)4060b57cec5SDimitry Andric bool ValueObjectPrinter::PrintValueAndSummaryIfNeeded(bool &value_printed,
4070b57cec5SDimitry Andric                                                       bool &summary_printed) {
4080b57cec5SDimitry Andric   bool error_printed = false;
4090b57cec5SDimitry Andric   if (ShouldPrintValueObject()) {
4100b57cec5SDimitry Andric     if (!CheckScopeIfNeeded())
4110b57cec5SDimitry Andric       m_error.assign("out of scope");
4120b57cec5SDimitry Andric     if (m_error.empty()) {
4130b57cec5SDimitry Andric       GetValueSummaryError(m_value, m_summary, m_error);
4140b57cec5SDimitry Andric     }
4150b57cec5SDimitry Andric     if (m_error.size()) {
4160b57cec5SDimitry Andric       // we need to support scenarios in which it is actually fine for a value
4170b57cec5SDimitry Andric       // to have no type but - on the other hand - if we get an error *AND*
4180b57cec5SDimitry Andric       // have no type, we try to get out gracefully, since most often that
4190b57cec5SDimitry Andric       // combination means "could not resolve a type" and the default failure
4200b57cec5SDimitry Andric       // mode is quite ugly
4210b57cec5SDimitry Andric       if (!m_compiler_type.IsValid()) {
4220b57cec5SDimitry Andric         m_stream->Printf(" <could not resolve type>");
4230b57cec5SDimitry Andric         return false;
4240b57cec5SDimitry Andric       }
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric       error_printed = true;
4270b57cec5SDimitry Andric       m_stream->Printf(" <%s>\n", m_error.c_str());
4280b57cec5SDimitry Andric     } else {
4290b57cec5SDimitry Andric       // Make sure we have a value and make sure the summary didn't specify
4300b57cec5SDimitry Andric       // that the value should not be printed - and do not print the value if
4310b57cec5SDimitry Andric       // this thing is nil (but show the value if the user passes a format
4320b57cec5SDimitry Andric       // explicitly)
4330b57cec5SDimitry Andric       TypeSummaryImpl *entry = GetSummaryFormatter();
434e8d8bef9SDimitry Andric       const bool has_nil_or_uninitialized_summary =
435e8d8bef9SDimitry Andric           (IsNil() || IsUninitialized()) && !m_summary.empty();
436e8d8bef9SDimitry Andric       if (!has_nil_or_uninitialized_summary && !m_value.empty() &&
4370b57cec5SDimitry Andric           (entry == nullptr ||
4380b57cec5SDimitry Andric            (entry->DoesPrintValue(m_valobj) ||
4390b57cec5SDimitry Andric             m_options.m_format != eFormatDefault) ||
4400b57cec5SDimitry Andric            m_summary.empty()) &&
4410b57cec5SDimitry Andric           !m_options.m_hide_value) {
4420b57cec5SDimitry Andric         if (m_options.m_hide_pointer_value &&
4430b57cec5SDimitry Andric             IsPointerValue(m_valobj->GetCompilerType())) {
4440b57cec5SDimitry Andric         } else {
445*fe013be4SDimitry Andric           if (ShouldShowName())
446*fe013be4SDimitry Andric             m_stream->PutChar(' ');
447*fe013be4SDimitry Andric           m_stream->PutCString(m_value);
4480b57cec5SDimitry Andric           value_printed = true;
4490b57cec5SDimitry Andric         }
4500b57cec5SDimitry Andric       }
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric       if (m_summary.size()) {
453*fe013be4SDimitry Andric         if (ShouldShowName() || value_printed)
454*fe013be4SDimitry Andric           m_stream->PutChar(' ');
455*fe013be4SDimitry Andric         m_stream->PutCString(m_summary);
4560b57cec5SDimitry Andric         summary_printed = true;
4570b57cec5SDimitry Andric       }
4580b57cec5SDimitry Andric     }
4590b57cec5SDimitry Andric   }
4600b57cec5SDimitry Andric   return !error_printed;
4610b57cec5SDimitry Andric }
4620b57cec5SDimitry Andric 
PrintObjectDescriptionIfNeeded(bool value_printed,bool summary_printed)4630b57cec5SDimitry Andric bool ValueObjectPrinter::PrintObjectDescriptionIfNeeded(bool value_printed,
4640b57cec5SDimitry Andric                                                         bool summary_printed) {
4650b57cec5SDimitry Andric   if (ShouldPrintValueObject()) {
4660b57cec5SDimitry Andric     // let's avoid the overly verbose no description error for a nil thing
4670b57cec5SDimitry Andric     if (m_options.m_use_objc && !IsNil() && !IsUninitialized() &&
4680b57cec5SDimitry Andric         (!m_options.m_pointer_as_array)) {
469*fe013be4SDimitry Andric       if (!m_options.m_hide_value || ShouldShowName())
4700b57cec5SDimitry Andric         m_stream->Printf(" ");
4710b57cec5SDimitry Andric       const char *object_desc = nullptr;
4720b57cec5SDimitry Andric       if (value_printed || summary_printed)
4730b57cec5SDimitry Andric         object_desc = m_valobj->GetObjectDescription();
4740b57cec5SDimitry Andric       else
4750b57cec5SDimitry Andric         object_desc = GetDescriptionForDisplay();
4760b57cec5SDimitry Andric       if (object_desc && *object_desc) {
4770b57cec5SDimitry Andric         // If the description already ends with a \n don't add another one.
4780b57cec5SDimitry Andric         size_t object_end = strlen(object_desc) - 1;
4790b57cec5SDimitry Andric         if (object_desc[object_end] == '\n')
4800b57cec5SDimitry Andric           m_stream->Printf("%s", object_desc);
4810b57cec5SDimitry Andric         else
4820b57cec5SDimitry Andric           m_stream->Printf("%s\n", object_desc);
4830b57cec5SDimitry Andric         return true;
4840b57cec5SDimitry Andric       } else if (!value_printed && !summary_printed)
4850b57cec5SDimitry Andric         return true;
4860b57cec5SDimitry Andric       else
4870b57cec5SDimitry Andric         return false;
4880b57cec5SDimitry Andric     }
4890b57cec5SDimitry Andric   }
4900b57cec5SDimitry Andric   return true;
4910b57cec5SDimitry Andric }
4920b57cec5SDimitry Andric 
CanAllowExpansion() const4930b57cec5SDimitry Andric bool DumpValueObjectOptions::PointerDepth::CanAllowExpansion() const {
4940b57cec5SDimitry Andric   switch (m_mode) {
4950b57cec5SDimitry Andric   case Mode::Always:
4960b57cec5SDimitry Andric   case Mode::Default:
4970b57cec5SDimitry Andric     return m_count > 0;
4980b57cec5SDimitry Andric   case Mode::Never:
4990b57cec5SDimitry Andric     return false;
5000b57cec5SDimitry Andric   }
5010b57cec5SDimitry Andric   return false;
5020b57cec5SDimitry Andric }
5030b57cec5SDimitry Andric 
ShouldPrintChildren(DumpValueObjectOptions::PointerDepth & curr_ptr_depth)5040b57cec5SDimitry Andric bool ValueObjectPrinter::ShouldPrintChildren(
5050b57cec5SDimitry Andric     DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
5060b57cec5SDimitry Andric   const bool is_ref = IsRef();
5070b57cec5SDimitry Andric   const bool is_ptr = IsPtr();
5080b57cec5SDimitry Andric   const bool is_uninit = IsUninitialized();
5090b57cec5SDimitry Andric 
5100b57cec5SDimitry Andric   if (is_uninit)
5110b57cec5SDimitry Andric     return false;
5120b57cec5SDimitry Andric 
513*fe013be4SDimitry Andric   // If we have reached the maximum depth we shouldn't print any more children.
514*fe013be4SDimitry Andric   if (HasReachedMaximumDepth())
515*fe013be4SDimitry Andric     return false;
516*fe013be4SDimitry Andric 
5170b57cec5SDimitry Andric   // if the user has specified an element count, always print children as it is
5180b57cec5SDimitry Andric   // explicit user demand being honored
5190b57cec5SDimitry Andric   if (m_options.m_pointer_as_array)
5200b57cec5SDimitry Andric     return true;
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric   if (m_options.m_use_objc)
5230b57cec5SDimitry Andric     return false;
5240b57cec5SDimitry Andric 
525*fe013be4SDimitry Andric   bool print_children = true;
526*fe013be4SDimitry Andric   if (TypeSummaryImpl *type_summary = GetSummaryFormatter())
527*fe013be4SDimitry Andric     print_children = type_summary->DoesPrintChildren(m_valobj);
528*fe013be4SDimitry Andric 
5290b57cec5SDimitry Andric   // We will show children for all concrete types. We won't show pointer
5300b57cec5SDimitry Andric   // contents unless a pointer depth has been specified. We won't reference
5310b57cec5SDimitry Andric   // contents unless the reference is the root object (depth of zero).
5320b57cec5SDimitry Andric 
5330b57cec5SDimitry Andric   // Use a new temporary pointer depth in case we override the current
5340b57cec5SDimitry Andric   // pointer depth below...
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric   if (is_ptr || is_ref) {
5370b57cec5SDimitry Andric     // We have a pointer or reference whose value is an address. Make sure
5380b57cec5SDimitry Andric     // that address is not NULL
5390b57cec5SDimitry Andric     AddressType ptr_address_type;
5400b57cec5SDimitry Andric     if (m_valobj->GetPointerValue(&ptr_address_type) == 0)
5410b57cec5SDimitry Andric       return false;
5420b57cec5SDimitry Andric 
5430b57cec5SDimitry Andric     const bool is_root_level = m_curr_depth == 0;
5440b57cec5SDimitry Andric 
545*fe013be4SDimitry Andric     if (is_ref && is_root_level && print_children) {
5460b57cec5SDimitry Andric       // If this is the root object (depth is zero) that we are showing and
5470b57cec5SDimitry Andric       // it is a reference, and no pointer depth has been supplied print out
5480b57cec5SDimitry Andric       // what it references. Don't do this at deeper depths otherwise we can
5490b57cec5SDimitry Andric       // end up with infinite recursion...
5500b57cec5SDimitry Andric       return true;
5510b57cec5SDimitry Andric     }
5520b57cec5SDimitry Andric 
5530b57cec5SDimitry Andric     return curr_ptr_depth.CanAllowExpansion();
5540b57cec5SDimitry Andric   }
5550b57cec5SDimitry Andric 
556*fe013be4SDimitry Andric   return print_children || m_summary.empty();
5570b57cec5SDimitry Andric }
5580b57cec5SDimitry Andric 
ShouldExpandEmptyAggregates()5590b57cec5SDimitry Andric bool ValueObjectPrinter::ShouldExpandEmptyAggregates() {
5600b57cec5SDimitry Andric   TypeSummaryImpl *entry = GetSummaryFormatter();
5610b57cec5SDimitry Andric 
5620b57cec5SDimitry Andric   if (!entry)
5630b57cec5SDimitry Andric     return true;
5640b57cec5SDimitry Andric 
5650b57cec5SDimitry Andric   return entry->DoesPrintEmptyAggregates();
5660b57cec5SDimitry Andric }
5670b57cec5SDimitry Andric 
GetValueObjectForChildrenGeneration()5680b57cec5SDimitry Andric ValueObject *ValueObjectPrinter::GetValueObjectForChildrenGeneration() {
5690b57cec5SDimitry Andric   return m_valobj;
5700b57cec5SDimitry Andric }
5710b57cec5SDimitry Andric 
PrintChildrenPreamble(bool value_printed,bool summary_printed)572*fe013be4SDimitry Andric void ValueObjectPrinter::PrintChildrenPreamble(bool value_printed,
573*fe013be4SDimitry Andric                                                bool summary_printed) {
5740b57cec5SDimitry Andric   if (m_options.m_flat_output) {
5750b57cec5SDimitry Andric     if (ShouldPrintValueObject())
5760b57cec5SDimitry Andric       m_stream->EOL();
5770b57cec5SDimitry Andric   } else {
578*fe013be4SDimitry Andric     if (ShouldPrintValueObject()) {
579*fe013be4SDimitry Andric       if (IsRef()) {
580*fe013be4SDimitry Andric         m_stream->PutCString(": ");
581*fe013be4SDimitry Andric       } else if (value_printed || summary_printed || ShouldShowName()) {
582*fe013be4SDimitry Andric         m_stream->PutChar(' ');
583*fe013be4SDimitry Andric       }
584*fe013be4SDimitry Andric       m_stream->PutCString("{\n");
585*fe013be4SDimitry Andric     }
5860b57cec5SDimitry Andric     m_stream->IndentMore();
5870b57cec5SDimitry Andric   }
5880b57cec5SDimitry Andric }
5890b57cec5SDimitry Andric 
PrintChild(ValueObjectSP child_sp,const DumpValueObjectOptions::PointerDepth & curr_ptr_depth)5900b57cec5SDimitry Andric void ValueObjectPrinter::PrintChild(
5910b57cec5SDimitry Andric     ValueObjectSP child_sp,
5920b57cec5SDimitry Andric     const DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
593*fe013be4SDimitry Andric   const uint32_t consumed_summary_depth = m_options.m_pointer_as_array ? 0 : 1;
5940b57cec5SDimitry Andric   const bool does_consume_ptr_depth =
5950b57cec5SDimitry Andric       ((IsPtr() && !m_options.m_pointer_as_array) || IsRef());
5960b57cec5SDimitry Andric 
5970b57cec5SDimitry Andric   DumpValueObjectOptions child_options(m_options);
5980b57cec5SDimitry Andric   child_options.SetFormat(m_options.m_format)
5990b57cec5SDimitry Andric       .SetSummary()
6000b57cec5SDimitry Andric       .SetRootValueObjectName();
6010b57cec5SDimitry Andric   child_options.SetScopeChecked(true)
6020b57cec5SDimitry Andric       .SetHideName(m_options.m_hide_name)
6030b57cec5SDimitry Andric       .SetHideValue(m_options.m_hide_value)
6040b57cec5SDimitry Andric       .SetOmitSummaryDepth(child_options.m_omit_summary_depth > 1
6050b57cec5SDimitry Andric                                ? child_options.m_omit_summary_depth -
606*fe013be4SDimitry Andric                                      consumed_summary_depth
6070b57cec5SDimitry Andric                                : 0)
6080b57cec5SDimitry Andric       .SetElementCount(0);
6090b57cec5SDimitry Andric 
6100b57cec5SDimitry Andric   if (child_sp.get()) {
611*fe013be4SDimitry Andric     auto ptr_depth = curr_ptr_depth;
612*fe013be4SDimitry Andric     if (does_consume_ptr_depth)
613*fe013be4SDimitry Andric       ptr_depth = curr_ptr_depth.Decremented();
614*fe013be4SDimitry Andric 
615*fe013be4SDimitry Andric     ValueObjectPrinter child_printer(child_sp.get(), m_stream, child_options,
616*fe013be4SDimitry Andric                                      ptr_depth, m_curr_depth + 1,
617*fe013be4SDimitry Andric                                      m_printed_instance_pointers);
6180b57cec5SDimitry Andric     child_printer.PrintValueObject();
6190b57cec5SDimitry Andric   }
6200b57cec5SDimitry Andric }
6210b57cec5SDimitry Andric 
GetMaxNumChildrenToPrint(bool & print_dotdotdot)6220b57cec5SDimitry Andric uint32_t ValueObjectPrinter::GetMaxNumChildrenToPrint(bool &print_dotdotdot) {
6230b57cec5SDimitry Andric   ValueObject *synth_m_valobj = GetValueObjectForChildrenGeneration();
6240b57cec5SDimitry Andric 
6250b57cec5SDimitry Andric   if (m_options.m_pointer_as_array)
6260b57cec5SDimitry Andric     return m_options.m_pointer_as_array.m_element_count;
6270b57cec5SDimitry Andric 
6280b57cec5SDimitry Andric   size_t num_children = synth_m_valobj->GetNumChildren();
6290b57cec5SDimitry Andric   print_dotdotdot = false;
6300b57cec5SDimitry Andric   if (num_children) {
6310b57cec5SDimitry Andric     const size_t max_num_children =
6320b57cec5SDimitry Andric         m_valobj->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
6330b57cec5SDimitry Andric 
6340b57cec5SDimitry Andric     if (num_children > max_num_children && !m_options.m_ignore_cap) {
6350b57cec5SDimitry Andric       print_dotdotdot = true;
6360b57cec5SDimitry Andric       return max_num_children;
6370b57cec5SDimitry Andric     }
6380b57cec5SDimitry Andric   }
6390b57cec5SDimitry Andric   return num_children;
6400b57cec5SDimitry Andric }
6410b57cec5SDimitry Andric 
PrintChildrenPostamble(bool print_dotdotdot)6420b57cec5SDimitry Andric void ValueObjectPrinter::PrintChildrenPostamble(bool print_dotdotdot) {
6430b57cec5SDimitry Andric   if (!m_options.m_flat_output) {
6440b57cec5SDimitry Andric     if (print_dotdotdot) {
6450b57cec5SDimitry Andric       m_valobj->GetTargetSP()
6460b57cec5SDimitry Andric           ->GetDebugger()
6470b57cec5SDimitry Andric           .GetCommandInterpreter()
6480b57cec5SDimitry Andric           .ChildrenTruncated();
6490b57cec5SDimitry Andric       m_stream->Indent("...\n");
6500b57cec5SDimitry Andric     }
6510b57cec5SDimitry Andric     m_stream->IndentLess();
6520b57cec5SDimitry Andric     m_stream->Indent("}\n");
6530b57cec5SDimitry Andric   }
6540b57cec5SDimitry Andric }
6550b57cec5SDimitry Andric 
ShouldPrintEmptyBrackets(bool value_printed,bool summary_printed)6560b57cec5SDimitry Andric bool ValueObjectPrinter::ShouldPrintEmptyBrackets(bool value_printed,
6570b57cec5SDimitry Andric                                                   bool summary_printed) {
6580b57cec5SDimitry Andric   ValueObject *synth_m_valobj = GetValueObjectForChildrenGeneration();
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric   if (!IsAggregate())
6610b57cec5SDimitry Andric     return false;
6620b57cec5SDimitry Andric 
6630b57cec5SDimitry Andric   if (!m_options.m_reveal_empty_aggregates) {
6640b57cec5SDimitry Andric     if (value_printed || summary_printed)
6650b57cec5SDimitry Andric       return false;
6660b57cec5SDimitry Andric   }
6670b57cec5SDimitry Andric 
6680b57cec5SDimitry Andric   if (synth_m_valobj->MightHaveChildren())
6690b57cec5SDimitry Andric     return true;
6700b57cec5SDimitry Andric 
6710b57cec5SDimitry Andric   if (m_val_summary_ok)
6720b57cec5SDimitry Andric     return false;
6730b57cec5SDimitry Andric 
6740b57cec5SDimitry Andric   return true;
6750b57cec5SDimitry Andric }
6760b57cec5SDimitry Andric 
PhysicalIndexForLogicalIndex(size_t base,size_t stride,size_t logical)6770b57cec5SDimitry Andric static constexpr size_t PhysicalIndexForLogicalIndex(size_t base, size_t stride,
6780b57cec5SDimitry Andric                                                      size_t logical) {
6790b57cec5SDimitry Andric   return base + logical * stride;
6800b57cec5SDimitry Andric }
6810b57cec5SDimitry Andric 
GenerateChild(ValueObject * synth_valobj,size_t idx)6820b57cec5SDimitry Andric ValueObjectSP ValueObjectPrinter::GenerateChild(ValueObject *synth_valobj,
6830b57cec5SDimitry Andric                                                 size_t idx) {
6840b57cec5SDimitry Andric   if (m_options.m_pointer_as_array) {
6850b57cec5SDimitry Andric     // if generating pointer-as-array children, use GetSyntheticArrayMember
6860b57cec5SDimitry Andric     return synth_valobj->GetSyntheticArrayMember(
6870b57cec5SDimitry Andric         PhysicalIndexForLogicalIndex(
6880b57cec5SDimitry Andric             m_options.m_pointer_as_array.m_base_element,
6890b57cec5SDimitry Andric             m_options.m_pointer_as_array.m_stride, idx),
6900b57cec5SDimitry Andric         true);
6910b57cec5SDimitry Andric   } else {
6920b57cec5SDimitry Andric     // otherwise, do the usual thing
693*fe013be4SDimitry Andric     return synth_valobj->GetChildAtIndex(idx);
6940b57cec5SDimitry Andric   }
6950b57cec5SDimitry Andric }
6960b57cec5SDimitry Andric 
PrintChildren(bool value_printed,bool summary_printed,const DumpValueObjectOptions::PointerDepth & curr_ptr_depth)6970b57cec5SDimitry Andric void ValueObjectPrinter::PrintChildren(
6980b57cec5SDimitry Andric     bool value_printed, bool summary_printed,
6990b57cec5SDimitry Andric     const DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
7000b57cec5SDimitry Andric   ValueObject *synth_m_valobj = GetValueObjectForChildrenGeneration();
7010b57cec5SDimitry Andric 
7020b57cec5SDimitry Andric   bool print_dotdotdot = false;
7030b57cec5SDimitry Andric   size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
7040b57cec5SDimitry Andric   if (num_children) {
7050b57cec5SDimitry Andric     bool any_children_printed = false;
7060b57cec5SDimitry Andric 
7070b57cec5SDimitry Andric     for (size_t idx = 0; idx < num_children; ++idx) {
7080b57cec5SDimitry Andric       if (ValueObjectSP child_sp = GenerateChild(synth_m_valobj, idx)) {
709*fe013be4SDimitry Andric         if (m_options.m_child_printing_decider &&
710*fe013be4SDimitry Andric             !m_options.m_child_printing_decider(child_sp->GetName()))
711*fe013be4SDimitry Andric           continue;
7120b57cec5SDimitry Andric         if (!any_children_printed) {
713*fe013be4SDimitry Andric           PrintChildrenPreamble(value_printed, summary_printed);
7140b57cec5SDimitry Andric           any_children_printed = true;
7150b57cec5SDimitry Andric         }
7160b57cec5SDimitry Andric         PrintChild(child_sp, curr_ptr_depth);
7170b57cec5SDimitry Andric       }
7180b57cec5SDimitry Andric     }
7190b57cec5SDimitry Andric 
7200b57cec5SDimitry Andric     if (any_children_printed)
7210b57cec5SDimitry Andric       PrintChildrenPostamble(print_dotdotdot);
7220b57cec5SDimitry Andric     else {
7230b57cec5SDimitry Andric       if (ShouldPrintEmptyBrackets(value_printed, summary_printed)) {
7240b57cec5SDimitry Andric         if (ShouldPrintValueObject())
7250b57cec5SDimitry Andric           m_stream->PutCString(" {}\n");
7260b57cec5SDimitry Andric         else
7270b57cec5SDimitry Andric           m_stream->EOL();
7280b57cec5SDimitry Andric       } else
7290b57cec5SDimitry Andric         m_stream->EOL();
7300b57cec5SDimitry Andric     }
7310b57cec5SDimitry Andric   } else if (ShouldPrintEmptyBrackets(value_printed, summary_printed)) {
7320b57cec5SDimitry Andric     // Aggregate, no children...
7330b57cec5SDimitry Andric     if (ShouldPrintValueObject()) {
7340b57cec5SDimitry Andric       // if it has a synthetic value, then don't print {}, the synthetic
7350b57cec5SDimitry Andric       // children are probably only being used to vend a value
7360b57cec5SDimitry Andric       if (m_valobj->DoesProvideSyntheticValue() ||
7370b57cec5SDimitry Andric           !ShouldExpandEmptyAggregates())
7380b57cec5SDimitry Andric         m_stream->PutCString("\n");
7390b57cec5SDimitry Andric       else
7400b57cec5SDimitry Andric         m_stream->PutCString(" {}\n");
7410b57cec5SDimitry Andric     }
7420b57cec5SDimitry Andric   } else {
7430b57cec5SDimitry Andric     if (ShouldPrintValueObject())
7440b57cec5SDimitry Andric       m_stream->EOL();
7450b57cec5SDimitry Andric   }
7460b57cec5SDimitry Andric }
7470b57cec5SDimitry Andric 
PrintChildrenOneLiner(bool hide_names)7480b57cec5SDimitry Andric bool ValueObjectPrinter::PrintChildrenOneLiner(bool hide_names) {
7490b57cec5SDimitry Andric   if (!GetMostSpecializedValue() || m_valobj == nullptr)
7500b57cec5SDimitry Andric     return false;
7510b57cec5SDimitry Andric 
7520b57cec5SDimitry Andric   ValueObject *synth_m_valobj = GetValueObjectForChildrenGeneration();
7530b57cec5SDimitry Andric 
7540b57cec5SDimitry Andric   bool print_dotdotdot = false;
7550b57cec5SDimitry Andric   size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
7560b57cec5SDimitry Andric 
7570b57cec5SDimitry Andric   if (num_children) {
7580b57cec5SDimitry Andric     m_stream->PutChar('(');
7590b57cec5SDimitry Andric 
760*fe013be4SDimitry Andric     bool did_print_children = false;
7610b57cec5SDimitry Andric     for (uint32_t idx = 0; idx < num_children; ++idx) {
762*fe013be4SDimitry Andric       lldb::ValueObjectSP child_sp(synth_m_valobj->GetChildAtIndex(idx));
7630b57cec5SDimitry Andric       if (child_sp)
7640b57cec5SDimitry Andric         child_sp = child_sp->GetQualifiedRepresentationIfAvailable(
7650b57cec5SDimitry Andric             m_options.m_use_dynamic, m_options.m_use_synthetic);
7660b57cec5SDimitry Andric       if (child_sp) {
767*fe013be4SDimitry Andric         if (m_options.m_child_printing_decider &&
768*fe013be4SDimitry Andric             !m_options.m_child_printing_decider(child_sp->GetName()))
769*fe013be4SDimitry Andric           continue;
770*fe013be4SDimitry Andric         if (idx && did_print_children)
7710b57cec5SDimitry Andric           m_stream->PutCString(", ");
772*fe013be4SDimitry Andric         did_print_children = true;
7730b57cec5SDimitry Andric         if (!hide_names) {
7740b57cec5SDimitry Andric           const char *name = child_sp.get()->GetName().AsCString();
7750b57cec5SDimitry Andric           if (name && *name) {
7760b57cec5SDimitry Andric             m_stream->PutCString(name);
7770b57cec5SDimitry Andric             m_stream->PutCString(" = ");
7780b57cec5SDimitry Andric           }
7790b57cec5SDimitry Andric         }
7800b57cec5SDimitry Andric         child_sp->DumpPrintableRepresentation(
7810b57cec5SDimitry Andric             *m_stream, ValueObject::eValueObjectRepresentationStyleSummary,
7820b57cec5SDimitry Andric             m_options.m_format,
7830b57cec5SDimitry Andric             ValueObject::PrintableRepresentationSpecialCases::eDisable);
7840b57cec5SDimitry Andric       }
7850b57cec5SDimitry Andric     }
7860b57cec5SDimitry Andric 
7870b57cec5SDimitry Andric     if (print_dotdotdot)
7880b57cec5SDimitry Andric       m_stream->PutCString(", ...)");
7890b57cec5SDimitry Andric     else
7900b57cec5SDimitry Andric       m_stream->PutChar(')');
7910b57cec5SDimitry Andric   }
7920b57cec5SDimitry Andric   return true;
7930b57cec5SDimitry Andric }
7940b57cec5SDimitry Andric 
PrintChildrenIfNeeded(bool value_printed,bool summary_printed)7950b57cec5SDimitry Andric void ValueObjectPrinter::PrintChildrenIfNeeded(bool value_printed,
7960b57cec5SDimitry Andric                                                bool summary_printed) {
797*fe013be4SDimitry Andric   PrintObjectDescriptionIfNeeded(value_printed, summary_printed);
7980b57cec5SDimitry Andric 
7999dba64beSDimitry Andric   DumpValueObjectOptions::PointerDepth curr_ptr_depth = m_ptr_depth;
800*fe013be4SDimitry Andric   const bool print_children = ShouldPrintChildren(curr_ptr_depth);
8019dba64beSDimitry Andric   const bool print_oneline =
8020b57cec5SDimitry Andric       (curr_ptr_depth.CanAllowExpansion() || m_options.m_show_types ||
8030b57cec5SDimitry Andric        !m_options.m_allow_oneliner_mode || m_options.m_flat_output ||
8040b57cec5SDimitry Andric        (m_options.m_pointer_as_array) || m_options.m_show_location)
8050b57cec5SDimitry Andric           ? false
8060b57cec5SDimitry Andric           : DataVisualization::ShouldPrintAsOneLiner(*m_valobj);
8079dba64beSDimitry Andric   if (print_children && IsInstancePointer()) {
8089dba64beSDimitry Andric     uint64_t instance_ptr_value = m_valobj->GetValueAsUnsigned(0);
8090b57cec5SDimitry Andric     if (m_printed_instance_pointers->count(instance_ptr_value)) {
8109dba64beSDimitry Andric       // We already printed this instance-is-pointer thing, so don't expand it.
8110b57cec5SDimitry Andric       m_stream->PutCString(" {...}\n");
8120b57cec5SDimitry Andric       return;
8139dba64beSDimitry Andric     } else {
8149dba64beSDimitry Andric       // Remember this guy for future reference.
8159dba64beSDimitry Andric       m_printed_instance_pointers->emplace(instance_ptr_value);
8169dba64beSDimitry Andric     }
8170b57cec5SDimitry Andric   }
8180b57cec5SDimitry Andric 
8190b57cec5SDimitry Andric   if (print_children) {
8200b57cec5SDimitry Andric     if (print_oneline) {
8210b57cec5SDimitry Andric       m_stream->PutChar(' ');
8220b57cec5SDimitry Andric       PrintChildrenOneLiner(false);
8230b57cec5SDimitry Andric       m_stream->EOL();
8240b57cec5SDimitry Andric     } else
8250b57cec5SDimitry Andric       PrintChildren(value_printed, summary_printed, curr_ptr_depth);
82681ad6265SDimitry Andric   } else if (HasReachedMaximumDepth() && IsAggregate() &&
8270b57cec5SDimitry Andric              ShouldPrintValueObject()) {
8280b57cec5SDimitry Andric     m_stream->PutCString("{...}\n");
82981ad6265SDimitry Andric     // The maximum child depth has been reached. If `m_max_depth` is the default
83081ad6265SDimitry Andric     // (i.e. the user has _not_ customized it), then lldb presents a warning to
83181ad6265SDimitry Andric     // the user. The warning tells the user that the limit has been reached, but
83281ad6265SDimitry Andric     // more importantly tells them how to expand the limit if desired.
83381ad6265SDimitry Andric     if (m_options.m_max_depth_is_default)
83481ad6265SDimitry Andric       m_valobj->GetTargetSP()
83581ad6265SDimitry Andric           ->GetDebugger()
83681ad6265SDimitry Andric           .GetCommandInterpreter()
83781ad6265SDimitry Andric           .SetReachedMaximumDepth();
8380b57cec5SDimitry Andric   } else
8390b57cec5SDimitry Andric     m_stream->EOL();
8400b57cec5SDimitry Andric }
84181ad6265SDimitry Andric 
HasReachedMaximumDepth()84281ad6265SDimitry Andric bool ValueObjectPrinter::HasReachedMaximumDepth() {
84381ad6265SDimitry Andric   return m_curr_depth >= m_options.m_max_depth;
84481ad6265SDimitry Andric }
845*fe013be4SDimitry Andric 
ShouldShowName() const846*fe013be4SDimitry Andric bool ValueObjectPrinter::ShouldShowName() const {
847*fe013be4SDimitry Andric   if (m_curr_depth == 0)
848*fe013be4SDimitry Andric     return !m_options.m_hide_root_name && !m_options.m_hide_name;
849*fe013be4SDimitry Andric   return !m_options.m_hide_name;
850*fe013be4SDimitry Andric }
851