ValueObjectPrinter.cpp revision 321369
1//===-- ValueObjectPrinter.cpp -----------------------------------*- C++-*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/DataFormatters/ValueObjectPrinter.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Core/ValueObject.h"
17#include "lldb/DataFormatters/DataVisualization.h"
18#include "lldb/Interpreter/CommandInterpreter.h"
19#include "lldb/Target/Language.h"
20#include "lldb/Target/Target.h"
21#include "lldb/Utility/Stream.h"
22
23using namespace lldb;
24using namespace lldb_private;
25
26ValueObjectPrinter::ValueObjectPrinter(ValueObject *valobj, Stream *s) {
27  if (valobj) {
28    DumpValueObjectOptions options(*valobj);
29    Init(valobj, s, options, m_options.m_max_ptr_depth, 0, nullptr);
30  } else {
31    DumpValueObjectOptions options;
32    Init(valobj, s, options, m_options.m_max_ptr_depth, 0, nullptr);
33  }
34}
35
36ValueObjectPrinter::ValueObjectPrinter(ValueObject *valobj, Stream *s,
37                                       const DumpValueObjectOptions &options) {
38  Init(valobj, s, options, m_options.m_max_ptr_depth, 0, nullptr);
39}
40
41ValueObjectPrinter::ValueObjectPrinter(
42    ValueObject *valobj, Stream *s, const DumpValueObjectOptions &options,
43    const DumpValueObjectOptions::PointerDepth &ptr_depth, uint32_t curr_depth,
44    InstancePointersSetSP printed_instance_pointers) {
45  Init(valobj, s, options, ptr_depth, curr_depth, printed_instance_pointers);
46}
47
48void ValueObjectPrinter::Init(
49    ValueObject *valobj, Stream *s, const DumpValueObjectOptions &options,
50    const DumpValueObjectOptions::PointerDepth &ptr_depth, uint32_t curr_depth,
51    InstancePointersSetSP printed_instance_pointers) {
52  m_orig_valobj = valobj;
53  m_valobj = nullptr;
54  m_stream = s;
55  m_options = options;
56  m_ptr_depth = ptr_depth;
57  m_curr_depth = curr_depth;
58  assert(m_orig_valobj && "cannot print a NULL ValueObject");
59  assert(m_stream && "cannot print to a NULL Stream");
60  m_should_print = eLazyBoolCalculate;
61  m_is_nil = eLazyBoolCalculate;
62  m_is_uninit = eLazyBoolCalculate;
63  m_is_ptr = eLazyBoolCalculate;
64  m_is_ref = eLazyBoolCalculate;
65  m_is_aggregate = eLazyBoolCalculate;
66  m_is_instance_ptr = eLazyBoolCalculate;
67  m_summary_formatter = {nullptr, false};
68  m_value.assign("");
69  m_summary.assign("");
70  m_error.assign("");
71  m_val_summary_ok = false;
72  m_printed_instance_pointers =
73      printed_instance_pointers
74          ? printed_instance_pointers
75          : InstancePointersSetSP(new InstancePointersSet());
76}
77
78bool ValueObjectPrinter::PrintValueObject() {
79  if (!GetMostSpecializedValue() || m_valobj == nullptr)
80    return false;
81
82  if (ShouldPrintValueObject()) {
83    PrintValidationMarkerIfNeeded();
84
85    PrintLocationIfNeeded();
86    m_stream->Indent();
87
88    PrintDecl();
89  }
90
91  bool value_printed = false;
92  bool summary_printed = false;
93
94  m_val_summary_ok =
95      PrintValueAndSummaryIfNeeded(value_printed, summary_printed);
96
97  if (m_val_summary_ok)
98    PrintChildrenIfNeeded(value_printed, summary_printed);
99  else
100    m_stream->EOL();
101
102  PrintValidationErrorIfNeeded();
103
104  return true;
105}
106
107bool ValueObjectPrinter::GetMostSpecializedValue() {
108  if (m_valobj)
109    return true;
110  bool update_success = m_orig_valobj->UpdateValueIfNeeded(true);
111  if (!update_success) {
112    m_valobj = m_orig_valobj;
113  } else {
114    if (m_orig_valobj->IsDynamic()) {
115      if (m_options.m_use_dynamic == eNoDynamicValues) {
116        ValueObject *static_value = m_orig_valobj->GetStaticValue().get();
117        if (static_value)
118          m_valobj = static_value;
119        else
120          m_valobj = m_orig_valobj;
121      } else
122        m_valobj = m_orig_valobj;
123    } else {
124      if (m_options.m_use_dynamic != eNoDynamicValues) {
125        ValueObject *dynamic_value =
126            m_orig_valobj->GetDynamicValue(m_options.m_use_dynamic).get();
127        if (dynamic_value)
128          m_valobj = dynamic_value;
129        else
130          m_valobj = m_orig_valobj;
131      } else
132        m_valobj = m_orig_valobj;
133    }
134
135    if (m_valobj->IsSynthetic()) {
136      if (m_options.m_use_synthetic == false) {
137        ValueObject *non_synthetic = m_valobj->GetNonSyntheticValue().get();
138        if (non_synthetic)
139          m_valobj = non_synthetic;
140      }
141    } else {
142      if (m_options.m_use_synthetic == true) {
143        ValueObject *synthetic = m_valobj->GetSyntheticValue().get();
144        if (synthetic)
145          m_valobj = synthetic;
146      }
147    }
148  }
149  m_compiler_type = m_valobj->GetCompilerType();
150  m_type_flags = m_compiler_type.GetTypeInfo();
151  return true;
152}
153
154const char *ValueObjectPrinter::GetDescriptionForDisplay() {
155  const char *str = m_valobj->GetObjectDescription();
156  if (!str)
157    str = m_valobj->GetSummaryAsCString();
158  if (!str)
159    str = m_valobj->GetValueAsCString();
160  return str;
161}
162
163const char *ValueObjectPrinter::GetRootNameForDisplay(const char *if_fail) {
164  const char *root_valobj_name = m_options.m_root_valobj_name.empty()
165                                     ? m_valobj->GetName().AsCString()
166                                     : m_options.m_root_valobj_name.c_str();
167  return root_valobj_name ? root_valobj_name : if_fail;
168}
169
170bool ValueObjectPrinter::ShouldPrintValueObject() {
171  if (m_should_print == eLazyBoolCalculate)
172    m_should_print =
173        (m_options.m_flat_output == false || m_type_flags.Test(eTypeHasValue))
174            ? eLazyBoolYes
175            : eLazyBoolNo;
176  return m_should_print == eLazyBoolYes;
177}
178
179bool ValueObjectPrinter::IsNil() {
180  if (m_is_nil == eLazyBoolCalculate)
181    m_is_nil = m_valobj->IsNilReference() ? eLazyBoolYes : eLazyBoolNo;
182  return m_is_nil == eLazyBoolYes;
183}
184
185bool ValueObjectPrinter::IsUninitialized() {
186  if (m_is_uninit == eLazyBoolCalculate)
187    m_is_uninit =
188        m_valobj->IsUninitializedReference() ? eLazyBoolYes : eLazyBoolNo;
189  return m_is_uninit == eLazyBoolYes;
190}
191
192bool ValueObjectPrinter::IsPtr() {
193  if (m_is_ptr == eLazyBoolCalculate)
194    m_is_ptr = m_type_flags.Test(eTypeIsPointer) ? eLazyBoolYes : eLazyBoolNo;
195  return m_is_ptr == eLazyBoolYes;
196}
197
198bool ValueObjectPrinter::IsRef() {
199  if (m_is_ref == eLazyBoolCalculate)
200    m_is_ref = m_type_flags.Test(eTypeIsReference) ? eLazyBoolYes : eLazyBoolNo;
201  return m_is_ref == eLazyBoolYes;
202}
203
204bool ValueObjectPrinter::IsAggregate() {
205  if (m_is_aggregate == eLazyBoolCalculate)
206    m_is_aggregate =
207        m_type_flags.Test(eTypeHasChildren) ? eLazyBoolYes : eLazyBoolNo;
208  return m_is_aggregate == eLazyBoolYes;
209}
210
211bool ValueObjectPrinter::IsInstancePointer() {
212  // you need to do this check on the value's clang type
213  if (m_is_instance_ptr == eLazyBoolCalculate)
214    m_is_instance_ptr = (m_valobj->GetValue().GetCompilerType().GetTypeInfo() &
215                         eTypeInstanceIsPointer) != 0
216                            ? eLazyBoolYes
217                            : eLazyBoolNo;
218  if ((eLazyBoolYes == m_is_instance_ptr) && m_valobj->IsBaseClass())
219    m_is_instance_ptr = eLazyBoolNo;
220  return m_is_instance_ptr == eLazyBoolYes;
221}
222
223bool ValueObjectPrinter::PrintLocationIfNeeded() {
224  if (m_options.m_show_location) {
225    m_stream->Printf("%s: ", m_valobj->GetLocationAsCString());
226    return true;
227  }
228  return false;
229}
230
231void ValueObjectPrinter::PrintDecl() {
232  bool show_type = true;
233  // if we are at the root-level and been asked to hide the root's type, then
234  // hide it
235  if (m_curr_depth == 0 && m_options.m_hide_root_type)
236    show_type = false;
237  else
238    // otherwise decide according to the usual rules (asked to show types -
239    // always at the root level)
240    show_type = m_options.m_show_types ||
241                (m_curr_depth == 0 && !m_options.m_flat_output);
242
243  StreamString typeName;
244
245  // always show the type at the root level if it is invalid
246  if (show_type) {
247    // Some ValueObjects don't have types (like registers sets). Only print
248    // the type if there is one to print
249    ConstString type_name;
250    if (m_compiler_type.IsValid()) {
251      if (m_options.m_use_type_display_name)
252        type_name = m_valobj->GetDisplayTypeName();
253      else
254        type_name = m_valobj->GetQualifiedTypeName();
255    } else {
256      // only show an invalid type name if the user explicitly triggered
257      // show_type
258      if (m_options.m_show_types)
259        type_name = ConstString("<invalid type>");
260      else
261        type_name.Clear();
262    }
263
264    if (type_name) {
265      std::string type_name_str(type_name.GetCString());
266      if (m_options.m_hide_pointer_value) {
267        for (auto iter = type_name_str.find(" *"); iter != std::string::npos;
268             iter = type_name_str.find(" *")) {
269          type_name_str.erase(iter, 2);
270        }
271      }
272      typeName.Printf("%s", type_name_str.c_str());
273    }
274  }
275
276  StreamString varName;
277
278  if (m_options.m_flat_output) {
279    // If we are showing types, also qualify the C++ base classes
280    const bool qualify_cxx_base_classes = show_type;
281    if (!m_options.m_hide_name) {
282      m_valobj->GetExpressionPath(varName, qualify_cxx_base_classes);
283    }
284  } else if (!m_options.m_hide_name) {
285    const char *name_cstr = GetRootNameForDisplay("");
286    varName.Printf("%s", name_cstr);
287  }
288
289  bool decl_printed = false;
290  if (!m_options.m_decl_printing_helper) {
291    // if the user didn't give us a custom helper, pick one based upon the
292    // language, either the one that this printer is bound to, or the preferred
293    // one for the ValueObject
294    lldb::LanguageType lang_type =
295        (m_options.m_varformat_language == lldb::eLanguageTypeUnknown)
296            ? m_valobj->GetPreferredDisplayLanguage()
297            : m_options.m_varformat_language;
298    if (Language *lang_plugin = Language::FindPlugin(lang_type)) {
299      m_options.m_decl_printing_helper = lang_plugin->GetDeclPrintingHelper();
300    }
301  }
302
303  if (m_options.m_decl_printing_helper) {
304    ConstString type_name_cstr(typeName.GetString());
305    ConstString var_name_cstr(varName.GetString());
306
307    StreamString dest_stream;
308    if (m_options.m_decl_printing_helper(type_name_cstr, var_name_cstr,
309                                         m_options, dest_stream)) {
310      decl_printed = true;
311      m_stream->PutCString(dest_stream.GetString());
312    }
313  }
314
315  // if the helper failed, or there is none, do a default thing
316  if (!decl_printed) {
317    if (!typeName.Empty())
318      m_stream->Printf("(%s) ", typeName.GetData());
319    if (!varName.Empty())
320      m_stream->Printf("%s =", varName.GetData());
321    else if (!m_options.m_hide_name)
322      m_stream->Printf(" =");
323  }
324}
325
326bool ValueObjectPrinter::CheckScopeIfNeeded() {
327  if (m_options.m_scope_already_checked)
328    return true;
329  return m_valobj->IsInScope();
330}
331
332TypeSummaryImpl *ValueObjectPrinter::GetSummaryFormatter(bool null_if_omitted) {
333  if (m_summary_formatter.second == false) {
334    TypeSummaryImpl *entry = m_options.m_summary_sp
335                                 ? m_options.m_summary_sp.get()
336                                 : m_valobj->GetSummaryFormat().get();
337
338    if (m_options.m_omit_summary_depth > 0)
339      entry = NULL;
340    m_summary_formatter.first = entry;
341    m_summary_formatter.second = true;
342  }
343  if (m_options.m_omit_summary_depth > 0 && null_if_omitted)
344    return nullptr;
345  return m_summary_formatter.first;
346}
347
348static bool IsPointerValue(const CompilerType &type) {
349  Flags type_flags(type.GetTypeInfo());
350  if (type_flags.AnySet(eTypeInstanceIsPointer | eTypeIsPointer))
351    return type_flags.AllClear(eTypeIsBuiltIn);
352  return false;
353}
354
355void ValueObjectPrinter::GetValueSummaryError(std::string &value,
356                                              std::string &summary,
357                                              std::string &error) {
358  lldb::Format format = m_options.m_format;
359  // if I am printing synthetized elements, apply the format to those elements
360  // only
361  if (m_options.m_pointer_as_array)
362    m_valobj->GetValueAsCString(lldb::eFormatDefault, value);
363  else if (format != eFormatDefault && format != m_valobj->GetFormat())
364    m_valobj->GetValueAsCString(format, value);
365  else {
366    const char *val_cstr = m_valobj->GetValueAsCString();
367    if (val_cstr)
368      value.assign(val_cstr);
369  }
370  const char *err_cstr = m_valobj->GetError().AsCString();
371  if (err_cstr)
372    error.assign(err_cstr);
373
374  if (ShouldPrintValueObject()) {
375    if (IsNil())
376      summary.assign("nil");
377    else if (IsUninitialized())
378      summary.assign("<uninitialized>");
379    else if (m_options.m_omit_summary_depth == 0) {
380      TypeSummaryImpl *entry = GetSummaryFormatter();
381      if (entry)
382        m_valobj->GetSummaryAsCString(entry, summary,
383                                      m_options.m_varformat_language);
384      else {
385        const char *sum_cstr =
386            m_valobj->GetSummaryAsCString(m_options.m_varformat_language);
387        if (sum_cstr)
388          summary.assign(sum_cstr);
389      }
390    }
391  }
392}
393
394bool ValueObjectPrinter::PrintValueAndSummaryIfNeeded(bool &value_printed,
395                                                      bool &summary_printed) {
396  bool error_printed = false;
397  if (ShouldPrintValueObject()) {
398    if (!CheckScopeIfNeeded())
399      m_error.assign("out of scope");
400    if (m_error.empty()) {
401      GetValueSummaryError(m_value, m_summary, m_error);
402    }
403    if (m_error.size()) {
404      // we need to support scenarios in which it is actually fine for a value
405      // to have no type
406      // but - on the other hand - if we get an error *AND* have no type, we try
407      // to get out
408      // gracefully, since most often that combination means "could not resolve
409      // a type"
410      // and the default failure mode is quite ugly
411      if (!m_compiler_type.IsValid()) {
412        m_stream->Printf(" <could not resolve type>");
413        return false;
414      }
415
416      error_printed = true;
417      m_stream->Printf(" <%s>\n", m_error.c_str());
418    } else {
419      // Make sure we have a value and make sure the summary didn't
420      // specify that the value should not be printed - and do not print
421      // the value if this thing is nil
422      // (but show the value if the user passes a format explicitly)
423      TypeSummaryImpl *entry = GetSummaryFormatter();
424      if (!IsNil() && !IsUninitialized() && !m_value.empty() &&
425          (entry == NULL || (entry->DoesPrintValue(m_valobj) ||
426                             m_options.m_format != eFormatDefault) ||
427           m_summary.empty()) &&
428          !m_options.m_hide_value) {
429        if (m_options.m_hide_pointer_value &&
430            IsPointerValue(m_valobj->GetCompilerType())) {
431        } else {
432          m_stream->Printf(" %s", m_value.c_str());
433          value_printed = true;
434        }
435      }
436
437      if (m_summary.size()) {
438        m_stream->Printf(" %s", m_summary.c_str());
439        summary_printed = true;
440      }
441    }
442  }
443  return !error_printed;
444}
445
446bool ValueObjectPrinter::PrintObjectDescriptionIfNeeded(bool value_printed,
447                                                        bool summary_printed) {
448  if (ShouldPrintValueObject()) {
449    // let's avoid the overly verbose no description error for a nil thing
450    if (m_options.m_use_objc && !IsNil() && !IsUninitialized() &&
451        (!m_options.m_pointer_as_array)) {
452      if (!m_options.m_hide_value || !m_options.m_hide_name)
453        m_stream->Printf(" ");
454      const char *object_desc = nullptr;
455      if (value_printed || summary_printed)
456        object_desc = m_valobj->GetObjectDescription();
457      else
458        object_desc = GetDescriptionForDisplay();
459      if (object_desc && *object_desc) {
460        // If the description already ends with a \n don't add another one.
461        size_t object_end = strlen(object_desc) - 1;
462        if (object_desc[object_end] == '\n')
463            m_stream->Printf("%s", object_desc);
464        else
465            m_stream->Printf("%s\n", object_desc);
466        return true;
467      } else if (value_printed == false && summary_printed == false)
468        return true;
469      else
470        return false;
471    }
472  }
473  return true;
474}
475
476bool DumpValueObjectOptions::PointerDepth::CanAllowExpansion() const {
477  switch (m_mode) {
478  case Mode::Always:
479  case Mode::Default:
480    return m_count > 0;
481  case Mode::Never:
482    return false;
483  }
484  return false;
485}
486
487bool ValueObjectPrinter::ShouldPrintChildren(
488    bool is_failed_description,
489    DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
490  const bool is_ref = IsRef();
491  const bool is_ptr = IsPtr();
492  const bool is_uninit = IsUninitialized();
493
494  if (is_uninit)
495    return false;
496
497  // if the user has specified an element count, always print children
498  // as it is explicit user demand being honored
499  if (m_options.m_pointer_as_array)
500    return true;
501
502  TypeSummaryImpl *entry = GetSummaryFormatter();
503
504  if (m_options.m_use_objc)
505    return false;
506
507  if (is_failed_description || m_curr_depth < m_options.m_max_depth) {
508    // We will show children for all concrete types. We won't show
509    // pointer contents unless a pointer depth has been specified.
510    // We won't reference contents unless the reference is the
511    // root object (depth of zero).
512
513    // Use a new temporary pointer depth in case we override the
514    // current pointer depth below...
515
516    if (is_ptr || is_ref) {
517      // We have a pointer or reference whose value is an address.
518      // Make sure that address is not NULL
519      AddressType ptr_address_type;
520      if (m_valobj->GetPointerValue(&ptr_address_type) == 0)
521        return false;
522
523      const bool is_root_level = m_curr_depth == 0;
524
525      if (is_ref && is_root_level) {
526        // If this is the root object (depth is zero) that we are showing
527        // and it is a reference, and no pointer depth has been supplied
528        // print out what it references. Don't do this at deeper depths
529        // otherwise we can end up with infinite recursion...
530        return true;
531      }
532
533      return curr_ptr_depth.CanAllowExpansion();
534    }
535
536    return (!entry || entry->DoesPrintChildren(m_valobj) || m_summary.empty());
537  }
538  return false;
539}
540
541bool ValueObjectPrinter::ShouldExpandEmptyAggregates() {
542  TypeSummaryImpl *entry = GetSummaryFormatter();
543
544  if (!entry)
545    return true;
546
547  return entry->DoesPrintEmptyAggregates();
548}
549
550ValueObject *ValueObjectPrinter::GetValueObjectForChildrenGeneration() {
551  return m_valobj;
552}
553
554void ValueObjectPrinter::PrintChildrenPreamble() {
555  if (m_options.m_flat_output) {
556    if (ShouldPrintValueObject())
557      m_stream->EOL();
558  } else {
559    if (ShouldPrintValueObject())
560      m_stream->PutCString(IsRef() ? ": {\n" : " {\n");
561    m_stream->IndentMore();
562  }
563}
564
565void ValueObjectPrinter::PrintChild(
566    ValueObjectSP child_sp,
567    const DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
568  const uint32_t consumed_depth = (!m_options.m_pointer_as_array) ? 1 : 0;
569  const bool does_consume_ptr_depth =
570      ((IsPtr() && !m_options.m_pointer_as_array) || IsRef());
571
572  DumpValueObjectOptions child_options(m_options);
573  child_options.SetFormat(m_options.m_format)
574      .SetSummary()
575      .SetRootValueObjectName();
576  child_options.SetScopeChecked(true)
577      .SetHideName(m_options.m_hide_name)
578      .SetHideValue(m_options.m_hide_value)
579      .SetOmitSummaryDepth(child_options.m_omit_summary_depth > 1
580                               ? child_options.m_omit_summary_depth -
581                                     consumed_depth
582                               : 0)
583      .SetElementCount(0);
584
585  if (child_sp.get()) {
586    ValueObjectPrinter child_printer(
587        child_sp.get(), m_stream, child_options,
588        does_consume_ptr_depth ? --curr_ptr_depth : curr_ptr_depth,
589        m_curr_depth + consumed_depth, m_printed_instance_pointers);
590    child_printer.PrintValueObject();
591  }
592}
593
594uint32_t ValueObjectPrinter::GetMaxNumChildrenToPrint(bool &print_dotdotdot) {
595  ValueObject *synth_m_valobj = GetValueObjectForChildrenGeneration();
596
597  if (m_options.m_pointer_as_array)
598    return m_options.m_pointer_as_array.m_element_count;
599
600  size_t num_children = synth_m_valobj->GetNumChildren();
601  print_dotdotdot = false;
602  if (num_children) {
603    const size_t max_num_children =
604        m_valobj->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
605
606    if (num_children > max_num_children && !m_options.m_ignore_cap) {
607      print_dotdotdot = true;
608      return max_num_children;
609    }
610  }
611  return num_children;
612}
613
614void ValueObjectPrinter::PrintChildrenPostamble(bool print_dotdotdot) {
615  if (!m_options.m_flat_output) {
616    if (print_dotdotdot) {
617      m_valobj->GetTargetSP()
618          ->GetDebugger()
619          .GetCommandInterpreter()
620          .ChildrenTruncated();
621      m_stream->Indent("...\n");
622    }
623    m_stream->IndentLess();
624    m_stream->Indent("}\n");
625  }
626}
627
628bool ValueObjectPrinter::ShouldPrintEmptyBrackets(bool value_printed,
629                                                  bool summary_printed) {
630  ValueObject *synth_m_valobj = GetValueObjectForChildrenGeneration();
631
632  if (!IsAggregate())
633    return false;
634
635  if (m_options.m_reveal_empty_aggregates == false) {
636    if (value_printed || summary_printed)
637      return false;
638  }
639
640  if (synth_m_valobj->MightHaveChildren())
641    return true;
642
643  if (m_val_summary_ok)
644    return false;
645
646  return true;
647}
648
649static constexpr size_t PhysicalIndexForLogicalIndex(size_t base, size_t stride,
650                                                     size_t logical) {
651  return base + logical * stride;
652}
653
654ValueObjectSP ValueObjectPrinter::GenerateChild(ValueObject *synth_valobj,
655                                                size_t idx) {
656  if (m_options.m_pointer_as_array) {
657    // if generating pointer-as-array children, use GetSyntheticArrayMember
658    return synth_valobj->GetSyntheticArrayMember(
659        PhysicalIndexForLogicalIndex(
660            m_options.m_pointer_as_array.m_base_element,
661            m_options.m_pointer_as_array.m_stride, idx),
662        true);
663  } else {
664    // otherwise, do the usual thing
665    return synth_valobj->GetChildAtIndex(idx, true);
666  }
667}
668
669void ValueObjectPrinter::PrintChildren(
670    bool value_printed, bool summary_printed,
671    const DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
672  ValueObject *synth_m_valobj = GetValueObjectForChildrenGeneration();
673
674  bool print_dotdotdot = false;
675  size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
676  if (num_children) {
677    bool any_children_printed = false;
678
679    for (size_t idx = 0; idx < num_children; ++idx) {
680      if (ValueObjectSP child_sp = GenerateChild(synth_m_valobj, idx)) {
681        if (!any_children_printed) {
682          PrintChildrenPreamble();
683          any_children_printed = true;
684        }
685        PrintChild(child_sp, curr_ptr_depth);
686      }
687    }
688
689    if (any_children_printed)
690      PrintChildrenPostamble(print_dotdotdot);
691    else {
692      if (ShouldPrintEmptyBrackets(value_printed, summary_printed)) {
693        if (ShouldPrintValueObject())
694          m_stream->PutCString(" {}\n");
695        else
696          m_stream->EOL();
697      } else
698        m_stream->EOL();
699    }
700  } else if (ShouldPrintEmptyBrackets(value_printed, summary_printed)) {
701    // Aggregate, no children...
702    if (ShouldPrintValueObject()) {
703      // if it has a synthetic value, then don't print {}, the synthetic
704      // children are probably only being used to vend a value
705      if (m_valobj->DoesProvideSyntheticValue() ||
706          !ShouldExpandEmptyAggregates())
707        m_stream->PutCString("\n");
708      else
709        m_stream->PutCString(" {}\n");
710    }
711  } else {
712    if (ShouldPrintValueObject())
713      m_stream->EOL();
714  }
715}
716
717bool ValueObjectPrinter::PrintChildrenOneLiner(bool hide_names) {
718  if (!GetMostSpecializedValue() || m_valobj == nullptr)
719    return false;
720
721  ValueObject *synth_m_valobj = GetValueObjectForChildrenGeneration();
722
723  bool print_dotdotdot = false;
724  size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
725
726  if (num_children) {
727    m_stream->PutChar('(');
728
729    for (uint32_t idx = 0; idx < num_children; ++idx) {
730      lldb::ValueObjectSP child_sp(synth_m_valobj->GetChildAtIndex(idx, true));
731      if (child_sp)
732        child_sp = child_sp->GetQualifiedRepresentationIfAvailable(
733            m_options.m_use_dynamic, m_options.m_use_synthetic);
734      if (child_sp) {
735        if (idx)
736          m_stream->PutCString(", ");
737        if (!hide_names) {
738          const char *name = child_sp.get()->GetName().AsCString();
739          if (name && *name) {
740            m_stream->PutCString(name);
741            m_stream->PutCString(" = ");
742          }
743        }
744        child_sp->DumpPrintableRepresentation(
745            *m_stream, ValueObject::eValueObjectRepresentationStyleSummary,
746            m_options.m_format,
747            ValueObject::PrintableRepresentationSpecialCases::eDisable);
748      }
749    }
750
751    if (print_dotdotdot)
752      m_stream->PutCString(", ...)");
753    else
754      m_stream->PutChar(')');
755  }
756  return true;
757}
758
759void ValueObjectPrinter::PrintChildrenIfNeeded(bool value_printed,
760                                               bool summary_printed) {
761  // this flag controls whether we tried to display a description for this
762  // object and failed
763  // if that happens, we want to display the children, if any
764  bool is_failed_description =
765      !PrintObjectDescriptionIfNeeded(value_printed, summary_printed);
766
767  auto curr_ptr_depth = m_ptr_depth;
768  bool print_children =
769      ShouldPrintChildren(is_failed_description, curr_ptr_depth);
770  bool print_oneline =
771      (curr_ptr_depth.CanAllowExpansion() || m_options.m_show_types ||
772       !m_options.m_allow_oneliner_mode || m_options.m_flat_output ||
773       (m_options.m_pointer_as_array) || m_options.m_show_location)
774          ? false
775          : DataVisualization::ShouldPrintAsOneLiner(*m_valobj);
776  bool is_instance_ptr = IsInstancePointer();
777  uint64_t instance_ptr_value = LLDB_INVALID_ADDRESS;
778
779  if (print_children && is_instance_ptr) {
780    instance_ptr_value = m_valobj->GetValueAsUnsigned(0);
781    if (m_printed_instance_pointers->count(instance_ptr_value)) {
782      // we already printed this instance-is-pointer thing, so don't expand it
783      m_stream->PutCString(" {...}\n");
784
785      // we're done here - get out fast
786      return;
787    } else
788      m_printed_instance_pointers->emplace(
789          instance_ptr_value); // remember this guy for future reference
790  }
791
792  if (print_children) {
793    if (print_oneline) {
794      m_stream->PutChar(' ');
795      PrintChildrenOneLiner(false);
796      m_stream->EOL();
797    } else
798      PrintChildren(value_printed, summary_printed, curr_ptr_depth);
799  } else if (m_curr_depth >= m_options.m_max_depth && IsAggregate() &&
800             ShouldPrintValueObject()) {
801    m_stream->PutCString("{...}\n");
802  } else
803    m_stream->EOL();
804}
805
806bool ValueObjectPrinter::ShouldPrintValidation() {
807  return m_options.m_run_validator;
808}
809
810bool ValueObjectPrinter::PrintValidationMarkerIfNeeded() {
811  if (!ShouldPrintValidation())
812    return false;
813
814  m_validation = m_valobj->GetValidationStatus();
815
816  if (TypeValidatorResult::Failure == m_validation.first) {
817    m_stream->Printf("! ");
818    return true;
819  }
820
821  return false;
822}
823
824bool ValueObjectPrinter::PrintValidationErrorIfNeeded() {
825  if (!ShouldPrintValidation())
826    return false;
827
828  if (TypeValidatorResult::Success == m_validation.first)
829    return false;
830
831  if (m_validation.second.empty())
832    m_validation.second.assign("unknown error");
833
834  m_stream->Printf(" ! validation error: %s", m_validation.second.c_str());
835  m_stream->EOL();
836
837  return true;
838}
839