ValueObjectPrinter.cpp revision 314564
1219820Sjeff//===-- ValueObjectPrinter.cpp -----------------------------------*- C++-*-===//
2219820Sjeff//
3219820Sjeff//                     The LLVM Compiler Infrastructure
4219820Sjeff//
5219820Sjeff// This file is distributed under the University of Illinois Open Source
6219820Sjeff// License. See LICENSE.TXT for details.
7219820Sjeff//
8219820Sjeff//===----------------------------------------------------------------------===//
9219820Sjeff
10219820Sjeff#include "lldb/DataFormatters/ValueObjectPrinter.h"
11219820Sjeff
12219820Sjeff// C Includes
13219820Sjeff// C++ Includes
14219820Sjeff// Other libraries and framework includes
15219820Sjeff// Project includes
16219820Sjeff#include "lldb/Core/Stream.h"
17219820Sjeff#include "lldb/Core/ValueObject.h"
18219820Sjeff#include "lldb/DataFormatters/DataVisualization.h"
19219820Sjeff#include "lldb/Interpreter/CommandInterpreter.h"
20219820Sjeff#include "lldb/Target/Language.h"
21219820Sjeff#include "lldb/Target/Target.h"
22219820Sjeff
23219820Sjeffusing namespace lldb;
24219820Sjeffusing namespace lldb_private;
25219820Sjeff
26219820SjeffValueObjectPrinter::ValueObjectPrinter(ValueObject *valobj, Stream *s) {
27219820Sjeff  if (valobj) {
28219820Sjeff    DumpValueObjectOptions options(*valobj);
29219820Sjeff    Init(valobj, s, options, m_options.m_max_ptr_depth, 0, nullptr);
30219820Sjeff  } else {
31219820Sjeff    DumpValueObjectOptions options;
32219820Sjeff    Init(valobj, s, options, m_options.m_max_ptr_depth, 0, nullptr);
33219820Sjeff  }
34219820Sjeff}
35219820Sjeff
36219820SjeffValueObjectPrinter::ValueObjectPrinter(ValueObject *valobj, Stream *s,
37219820Sjeff                                       const DumpValueObjectOptions &options) {
38219820Sjeff  Init(valobj, s, options, m_options.m_max_ptr_depth, 0, nullptr);
39219820Sjeff}
40219820Sjeff
41219820SjeffValueObjectPrinter::ValueObjectPrinter(
42219820Sjeff    ValueObject *valobj, Stream *s, const DumpValueObjectOptions &options,
43219820Sjeff    const DumpValueObjectOptions::PointerDepth &ptr_depth, uint32_t curr_depth,
44219820Sjeff    InstancePointersSetSP printed_instance_pointers) {
45219820Sjeff  Init(valobj, s, options, ptr_depth, curr_depth, printed_instance_pointers);
46219820Sjeff}
47219820Sjeff
48219820Sjeffvoid ValueObjectPrinter::Init(
49219820Sjeff    ValueObject *valobj, Stream *s, const DumpValueObjectOptions &options,
50219820Sjeff    const DumpValueObjectOptions::PointerDepth &ptr_depth, uint32_t curr_depth,
51219820Sjeff    InstancePointersSetSP printed_instance_pointers) {
52219820Sjeff  m_orig_valobj = valobj;
53219820Sjeff  m_valobj = nullptr;
54219820Sjeff  m_stream = s;
55219820Sjeff  m_options = options;
56219820Sjeff  m_ptr_depth = ptr_depth;
57219820Sjeff  m_curr_depth = curr_depth;
58219820Sjeff  assert(m_orig_valobj && "cannot print a NULL ValueObject");
59219820Sjeff  assert(m_stream && "cannot print to a NULL Stream");
60219820Sjeff  m_should_print = eLazyBoolCalculate;
61219820Sjeff  m_is_nil = eLazyBoolCalculate;
62219820Sjeff  m_is_uninit = eLazyBoolCalculate;
63219820Sjeff  m_is_ptr = eLazyBoolCalculate;
64219820Sjeff  m_is_ref = eLazyBoolCalculate;
65219820Sjeff  m_is_aggregate = eLazyBoolCalculate;
66219820Sjeff  m_is_instance_ptr = eLazyBoolCalculate;
67219820Sjeff  m_summary_formatter = {nullptr, false};
68219820Sjeff  m_value.assign("");
69219820Sjeff  m_summary.assign("");
70219820Sjeff  m_error.assign("");
71219820Sjeff  m_val_summary_ok = false;
72219820Sjeff  m_printed_instance_pointers =
73219820Sjeff      printed_instance_pointers
74219820Sjeff          ? printed_instance_pointers
75219820Sjeff          : InstancePointersSetSP(new InstancePointersSet());
76219820Sjeff}
77219820Sjeff
78219820Sjeffbool ValueObjectPrinter::PrintValueObject() {
79219820Sjeff  if (!GetMostSpecializedValue() || m_valobj == nullptr)
80219820Sjeff    return false;
81219820Sjeff
82219820Sjeff  if (ShouldPrintValueObject()) {
83219820Sjeff    PrintValidationMarkerIfNeeded();
84219820Sjeff
85219820Sjeff    PrintLocationIfNeeded();
86219820Sjeff    m_stream->Indent();
87219820Sjeff
88219820Sjeff    PrintDecl();
89219820Sjeff  }
90219820Sjeff
91219820Sjeff  bool value_printed = false;
92219820Sjeff  bool summary_printed = false;
93219820Sjeff
94219820Sjeff  m_val_summary_ok =
95219820Sjeff      PrintValueAndSummaryIfNeeded(value_printed, summary_printed);
96219820Sjeff
97219820Sjeff  if (m_val_summary_ok)
98219820Sjeff    PrintChildrenIfNeeded(value_printed, summary_printed);
99219820Sjeff  else
100219820Sjeff    m_stream->EOL();
101219820Sjeff
102219820Sjeff  PrintValidationErrorIfNeeded();
103219820Sjeff
104219820Sjeff  return true;
105219820Sjeff}
106219820Sjeff
107219820Sjeffbool ValueObjectPrinter::GetMostSpecializedValue() {
108219820Sjeff  if (m_valobj)
109219820Sjeff    return true;
110219820Sjeff  bool update_success = m_orig_valobj->UpdateValueIfNeeded(true);
111219820Sjeff  if (!update_success) {
112219820Sjeff    m_valobj = m_orig_valobj;
113219820Sjeff  } else {
114219820Sjeff    if (m_orig_valobj->IsDynamic()) {
115219820Sjeff      if (m_options.m_use_dynamic == eNoDynamicValues) {
116219820Sjeff        ValueObject *static_value = m_orig_valobj->GetStaticValue().get();
117219820Sjeff        if (static_value)
118219820Sjeff          m_valobj = static_value;
119219820Sjeff        else
120219820Sjeff          m_valobj = m_orig_valobj;
121219820Sjeff      } else
122219820Sjeff        m_valobj = m_orig_valobj;
123219820Sjeff    } else {
124219820Sjeff      if (m_options.m_use_dynamic != eNoDynamicValues) {
125219820Sjeff        ValueObject *dynamic_value =
126219820Sjeff            m_orig_valobj->GetDynamicValue(m_options.m_use_dynamic).get();
127219820Sjeff        if (dynamic_value)
128219820Sjeff          m_valobj = dynamic_value;
129219820Sjeff        else
130219820Sjeff          m_valobj = m_orig_valobj;
131219820Sjeff      } else
132219820Sjeff        m_valobj = m_orig_valobj;
133219820Sjeff    }
134219820Sjeff
135219820Sjeff    if (m_valobj->IsSynthetic()) {
136219820Sjeff      if (m_options.m_use_synthetic == false) {
137219820Sjeff        ValueObject *non_synthetic = m_valobj->GetNonSyntheticValue().get();
138219820Sjeff        if (non_synthetic)
139219820Sjeff          m_valobj = non_synthetic;
140219820Sjeff      }
141219820Sjeff    } else {
142219820Sjeff      if (m_options.m_use_synthetic == true) {
143219820Sjeff        ValueObject *synthetic = m_valobj->GetSyntheticValue().get();
144219820Sjeff        if (synthetic)
145219820Sjeff          m_valobj = synthetic;
146219820Sjeff      }
147219820Sjeff    }
148219820Sjeff  }
149219820Sjeff  m_compiler_type = m_valobj->GetCompilerType();
150219820Sjeff  m_type_flags = m_compiler_type.GetTypeInfo();
151219820Sjeff  return true;
152219820Sjeff}
153219820Sjeff
154219820Sjeffconst char *ValueObjectPrinter::GetDescriptionForDisplay() {
155219820Sjeff  const char *str = m_valobj->GetObjectDescription();
156219820Sjeff  if (!str)
157219820Sjeff    str = m_valobj->GetSummaryAsCString();
158219820Sjeff  if (!str)
159219820Sjeff    str = m_valobj->GetValueAsCString();
160219820Sjeff  return str;
161219820Sjeff}
162219820Sjeff
163219820Sjeffconst char *ValueObjectPrinter::GetRootNameForDisplay(const char *if_fail) {
164219820Sjeff  const char *root_valobj_name = m_options.m_root_valobj_name.empty()
165219820Sjeff                                     ? m_valobj->GetName().AsCString()
166219820Sjeff                                     : m_options.m_root_valobj_name.c_str();
167219820Sjeff  return root_valobj_name ? root_valobj_name : if_fail;
168219820Sjeff}
169219820Sjeff
170219820Sjeffbool ValueObjectPrinter::ShouldPrintValueObject() {
171219820Sjeff  if (m_should_print == eLazyBoolCalculate)
172219820Sjeff    m_should_print =
173219820Sjeff        (m_options.m_flat_output == false || m_type_flags.Test(eTypeHasValue))
174219820Sjeff            ? eLazyBoolYes
175219820Sjeff            : eLazyBoolNo;
176219820Sjeff  return m_should_print == eLazyBoolYes;
177219820Sjeff}
178219820Sjeff
179219820Sjeffbool ValueObjectPrinter::IsNil() {
180219820Sjeff  if (m_is_nil == eLazyBoolCalculate)
181219820Sjeff    m_is_nil = m_valobj->IsNilReference() ? eLazyBoolYes : eLazyBoolNo;
182219820Sjeff  return m_is_nil == eLazyBoolYes;
183219820Sjeff}
184219820Sjeff
185219820Sjeffbool ValueObjectPrinter::IsUninitialized() {
186219820Sjeff  if (m_is_uninit == eLazyBoolCalculate)
187219820Sjeff    m_is_uninit =
188219820Sjeff        m_valobj->IsUninitializedReference() ? eLazyBoolYes : eLazyBoolNo;
189219820Sjeff  return m_is_uninit == eLazyBoolYes;
190219820Sjeff}
191219820Sjeff
192219820Sjeffbool ValueObjectPrinter::IsPtr() {
193219820Sjeff  if (m_is_ptr == eLazyBoolCalculate)
194219820Sjeff    m_is_ptr = m_type_flags.Test(eTypeIsPointer) ? eLazyBoolYes : eLazyBoolNo;
195219820Sjeff  return m_is_ptr == eLazyBoolYes;
196219820Sjeff}
197219820Sjeff
198219820Sjeffbool ValueObjectPrinter::IsRef() {
199219820Sjeff  if (m_is_ref == eLazyBoolCalculate)
200219820Sjeff    m_is_ref = m_type_flags.Test(eTypeIsReference) ? eLazyBoolYes : eLazyBoolNo;
201219820Sjeff  return m_is_ref == eLazyBoolYes;
202219820Sjeff}
203219820Sjeff
204219820Sjeffbool ValueObjectPrinter::IsAggregate() {
205219820Sjeff  if (m_is_aggregate == eLazyBoolCalculate)
206219820Sjeff    m_is_aggregate =
207219820Sjeff        m_type_flags.Test(eTypeHasChildren) ? eLazyBoolYes : eLazyBoolNo;
208219820Sjeff  return m_is_aggregate == eLazyBoolYes;
209219820Sjeff}
210219820Sjeff
211219820Sjeffbool ValueObjectPrinter::IsInstancePointer() {
212219820Sjeff  // you need to do this check on the value's clang type
213219820Sjeff  if (m_is_instance_ptr == eLazyBoolCalculate)
214219820Sjeff    m_is_instance_ptr = (m_valobj->GetValue().GetCompilerType().GetTypeInfo() &
215219820Sjeff                         eTypeInstanceIsPointer) != 0
216219820Sjeff                            ? eLazyBoolYes
217219820Sjeff                            : eLazyBoolNo;
218219820Sjeff  if ((eLazyBoolYes == m_is_instance_ptr) && m_valobj->IsBaseClass())
219219820Sjeff    m_is_instance_ptr = eLazyBoolNo;
220219820Sjeff  return m_is_instance_ptr == eLazyBoolYes;
221219820Sjeff}
222219820Sjeff
223219820Sjeffbool ValueObjectPrinter::PrintLocationIfNeeded() {
224219820Sjeff  if (m_options.m_show_location) {
225219820Sjeff    m_stream->Printf("%s: ", m_valobj->GetLocationAsCString());
226219820Sjeff    return true;
227219820Sjeff  }
228219820Sjeff  return false;
229219820Sjeff}
230219820Sjeff
231219820Sjeffvoid ValueObjectPrinter::PrintDecl() {
232219820Sjeff  bool show_type = true;
233219820Sjeff  // if we are at the root-level and been asked to hide the root's type, then
234219820Sjeff  // hide it
235219820Sjeff  if (m_curr_depth == 0 && m_options.m_hide_root_type)
236219820Sjeff    show_type = false;
237219820Sjeff  else
238219820Sjeff    // otherwise decide according to the usual rules (asked to show types -
239219820Sjeff    // always at the root level)
240219820Sjeff    show_type = m_options.m_show_types ||
241219820Sjeff                (m_curr_depth == 0 && !m_options.m_flat_output);
242219820Sjeff
243219820Sjeff  StreamString typeName;
244219820Sjeff
245219820Sjeff  // always show the type at the root level if it is invalid
246219820Sjeff  if (show_type) {
247219820Sjeff    // Some ValueObjects don't have types (like registers sets). Only print
248219820Sjeff    // the type if there is one to print
249219820Sjeff    ConstString type_name;
250219820Sjeff    if (m_compiler_type.IsValid()) {
251219820Sjeff      if (m_options.m_use_type_display_name)
252219820Sjeff        type_name = m_valobj->GetDisplayTypeName();
253219820Sjeff      else
254219820Sjeff        type_name = m_valobj->GetQualifiedTypeName();
255219820Sjeff    } else {
256219820Sjeff      // only show an invalid type name if the user explicitly triggered
257219820Sjeff      // show_type
258219820Sjeff      if (m_options.m_show_types)
259219820Sjeff        type_name = ConstString("<invalid type>");
260219820Sjeff      else
261219820Sjeff        type_name.Clear();
262219820Sjeff    }
263219820Sjeff
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        m_stream->Printf("%s\n", object_desc);
461        return true;
462      } else if (value_printed == false && summary_printed == false)
463        return true;
464      else
465        return false;
466    }
467  }
468  return true;
469}
470
471bool DumpValueObjectOptions::PointerDepth::CanAllowExpansion(
472    bool is_root, TypeSummaryImpl *entry, ValueObject *valobj,
473    const std::string &summary) {
474  switch (m_mode) {
475  case Mode::Always:
476    return (m_count > 0);
477  case Mode::Never:
478    return false;
479  case Mode::Default:
480    if (is_root)
481      m_count = std::min<decltype(m_count)>(m_count, 1);
482    return m_count > 0;
483  case Mode::Formatters:
484    if (!entry || entry->DoesPrintChildren(valobj) || summary.empty())
485      return m_count > 0;
486    return false;
487  }
488  return false;
489}
490
491bool DumpValueObjectOptions::PointerDepth::CanAllowExpansion() const {
492  switch (m_mode) {
493  case Mode::Always:
494  case Mode::Default:
495  case Mode::Formatters:
496    return (m_count > 0);
497  case Mode::Never:
498    return false;
499  }
500  return false;
501}
502
503bool ValueObjectPrinter::ShouldPrintChildren(
504    bool is_failed_description,
505    DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
506  const bool is_ref = IsRef();
507  const bool is_ptr = IsPtr();
508  const bool is_uninit = IsUninitialized();
509
510  if (is_uninit)
511    return false;
512
513  // if the user has specified an element count, always print children
514  // as it is explicit user demand being honored
515  if (m_options.m_pointer_as_array)
516    return true;
517
518  TypeSummaryImpl *entry = GetSummaryFormatter();
519
520  if (m_options.m_use_objc)
521    return false;
522
523  if (is_failed_description || m_curr_depth < m_options.m_max_depth) {
524    // We will show children for all concrete types. We won't show
525    // pointer contents unless a pointer depth has been specified.
526    // We won't reference contents unless the reference is the
527    // root object (depth of zero).
528
529    // Use a new temporary pointer depth in case we override the
530    // current pointer depth below...
531
532    if (is_ptr || is_ref) {
533      // We have a pointer or reference whose value is an address.
534      // Make sure that address is not NULL
535      AddressType ptr_address_type;
536      if (m_valobj->GetPointerValue(&ptr_address_type) == 0)
537        return false;
538
539      const bool is_root_level = m_curr_depth == 0;
540
541      if (is_ref && is_root_level) {
542        // If this is the root object (depth is zero) that we are showing
543        // and it is a reference, and no pointer depth has been supplied
544        // print out what it references. Don't do this at deeper depths
545        // otherwise we can end up with infinite recursion...
546        return true;
547      }
548
549      return curr_ptr_depth.CanAllowExpansion(false, entry, m_valobj,
550                                              m_summary);
551    }
552
553    return (!entry || entry->DoesPrintChildren(m_valobj) || m_summary.empty());
554  }
555  return false;
556}
557
558bool ValueObjectPrinter::ShouldExpandEmptyAggregates() {
559  TypeSummaryImpl *entry = GetSummaryFormatter();
560
561  if (!entry)
562    return true;
563
564  return entry->DoesPrintEmptyAggregates();
565}
566
567ValueObject *ValueObjectPrinter::GetValueObjectForChildrenGeneration() {
568  return m_valobj;
569}
570
571void ValueObjectPrinter::PrintChildrenPreamble() {
572  if (m_options.m_flat_output) {
573    if (ShouldPrintValueObject())
574      m_stream->EOL();
575  } else {
576    if (ShouldPrintValueObject())
577      m_stream->PutCString(IsRef() ? ": {\n" : " {\n");
578    m_stream->IndentMore();
579  }
580}
581
582void ValueObjectPrinter::PrintChild(
583    ValueObjectSP child_sp,
584    const DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
585  const uint32_t consumed_depth = (!m_options.m_pointer_as_array) ? 1 : 0;
586  const bool does_consume_ptr_depth =
587      ((IsPtr() && !m_options.m_pointer_as_array) || IsRef());
588
589  DumpValueObjectOptions child_options(m_options);
590  child_options.SetFormat(m_options.m_format)
591      .SetSummary()
592      .SetRootValueObjectName();
593  child_options.SetScopeChecked(true)
594      .SetHideName(m_options.m_hide_name)
595      .SetHideValue(m_options.m_hide_value)
596      .SetOmitSummaryDepth(child_options.m_omit_summary_depth > 1
597                               ? child_options.m_omit_summary_depth -
598                                     consumed_depth
599                               : 0)
600      .SetElementCount(0);
601
602  if (child_sp.get()) {
603    ValueObjectPrinter child_printer(
604        child_sp.get(), m_stream, child_options,
605        does_consume_ptr_depth ? --curr_ptr_depth : curr_ptr_depth,
606        m_curr_depth + consumed_depth, m_printed_instance_pointers);
607    child_printer.PrintValueObject();
608  }
609}
610
611uint32_t ValueObjectPrinter::GetMaxNumChildrenToPrint(bool &print_dotdotdot) {
612  ValueObject *synth_m_valobj = GetValueObjectForChildrenGeneration();
613
614  if (m_options.m_pointer_as_array)
615    return m_options.m_pointer_as_array.m_element_count;
616
617  size_t num_children = synth_m_valobj->GetNumChildren();
618  print_dotdotdot = false;
619  if (num_children) {
620    const size_t max_num_children =
621        m_valobj->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
622
623    if (num_children > max_num_children && !m_options.m_ignore_cap) {
624      print_dotdotdot = true;
625      return max_num_children;
626    }
627  }
628  return num_children;
629}
630
631void ValueObjectPrinter::PrintChildrenPostamble(bool print_dotdotdot) {
632  if (!m_options.m_flat_output) {
633    if (print_dotdotdot) {
634      m_valobj->GetTargetSP()
635          ->GetDebugger()
636          .GetCommandInterpreter()
637          .ChildrenTruncated();
638      m_stream->Indent("...\n");
639    }
640    m_stream->IndentLess();
641    m_stream->Indent("}\n");
642  }
643}
644
645bool ValueObjectPrinter::ShouldPrintEmptyBrackets(bool value_printed,
646                                                  bool summary_printed) {
647  ValueObject *synth_m_valobj = GetValueObjectForChildrenGeneration();
648
649  if (!IsAggregate())
650    return false;
651
652  if (m_options.m_reveal_empty_aggregates == false) {
653    if (value_printed || summary_printed)
654      return false;
655  }
656
657  if (synth_m_valobj->MightHaveChildren())
658    return true;
659
660  if (m_val_summary_ok)
661    return false;
662
663  return true;
664}
665
666static constexpr size_t PhysicalIndexForLogicalIndex(size_t base, size_t stride,
667                                                     size_t logical) {
668  return base + logical * stride;
669}
670
671ValueObjectSP ValueObjectPrinter::GenerateChild(ValueObject *synth_valobj,
672                                                size_t idx) {
673  if (m_options.m_pointer_as_array) {
674    // if generating pointer-as-array children, use GetSyntheticArrayMember
675    return synth_valobj->GetSyntheticArrayMember(
676        PhysicalIndexForLogicalIndex(
677            m_options.m_pointer_as_array.m_base_element,
678            m_options.m_pointer_as_array.m_stride, idx),
679        true);
680  } else {
681    // otherwise, do the usual thing
682    return synth_valobj->GetChildAtIndex(idx, true);
683  }
684}
685
686void ValueObjectPrinter::PrintChildren(
687    bool value_printed, bool summary_printed,
688    const DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
689  ValueObject *synth_m_valobj = GetValueObjectForChildrenGeneration();
690
691  bool print_dotdotdot = false;
692  size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
693  if (num_children) {
694    bool any_children_printed = false;
695
696    for (size_t idx = 0; idx < num_children; ++idx) {
697      if (ValueObjectSP child_sp = GenerateChild(synth_m_valobj, idx)) {
698        if (!any_children_printed) {
699          PrintChildrenPreamble();
700          any_children_printed = true;
701        }
702        PrintChild(child_sp, curr_ptr_depth);
703      }
704    }
705
706    if (any_children_printed)
707      PrintChildrenPostamble(print_dotdotdot);
708    else {
709      if (ShouldPrintEmptyBrackets(value_printed, summary_printed)) {
710        if (ShouldPrintValueObject())
711          m_stream->PutCString(" {}\n");
712        else
713          m_stream->EOL();
714      } else
715        m_stream->EOL();
716    }
717  } else if (ShouldPrintEmptyBrackets(value_printed, summary_printed)) {
718    // Aggregate, no children...
719    if (ShouldPrintValueObject()) {
720      // if it has a synthetic value, then don't print {}, the synthetic
721      // children are probably only being used to vend a value
722      if (m_valobj->DoesProvideSyntheticValue() ||
723          !ShouldExpandEmptyAggregates())
724        m_stream->PutCString("\n");
725      else
726        m_stream->PutCString(" {}\n");
727    }
728  } else {
729    if (ShouldPrintValueObject())
730      m_stream->EOL();
731  }
732}
733
734bool ValueObjectPrinter::PrintChildrenOneLiner(bool hide_names) {
735  if (!GetMostSpecializedValue() || m_valobj == nullptr)
736    return false;
737
738  ValueObject *synth_m_valobj = GetValueObjectForChildrenGeneration();
739
740  bool print_dotdotdot = false;
741  size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
742
743  if (num_children) {
744    m_stream->PutChar('(');
745
746    for (uint32_t idx = 0; idx < num_children; ++idx) {
747      lldb::ValueObjectSP child_sp(synth_m_valobj->GetChildAtIndex(idx, true));
748      if (child_sp)
749        child_sp = child_sp->GetQualifiedRepresentationIfAvailable(
750            m_options.m_use_dynamic, m_options.m_use_synthetic);
751      if (child_sp) {
752        if (idx)
753          m_stream->PutCString(", ");
754        if (!hide_names) {
755          const char *name = child_sp.get()->GetName().AsCString();
756          if (name && *name) {
757            m_stream->PutCString(name);
758            m_stream->PutCString(" = ");
759          }
760        }
761        child_sp->DumpPrintableRepresentation(
762            *m_stream, ValueObject::eValueObjectRepresentationStyleSummary,
763            m_options.m_format,
764            ValueObject::PrintableRepresentationSpecialCases::eDisable);
765      }
766    }
767
768    if (print_dotdotdot)
769      m_stream->PutCString(", ...)");
770    else
771      m_stream->PutChar(')');
772  }
773  return true;
774}
775
776void ValueObjectPrinter::PrintChildrenIfNeeded(bool value_printed,
777                                               bool summary_printed) {
778  // this flag controls whether we tried to display a description for this
779  // object and failed
780  // if that happens, we want to display the children, if any
781  bool is_failed_description =
782      !PrintObjectDescriptionIfNeeded(value_printed, summary_printed);
783
784  auto curr_ptr_depth = m_ptr_depth;
785  bool print_children =
786      ShouldPrintChildren(is_failed_description, curr_ptr_depth);
787  bool print_oneline =
788      (curr_ptr_depth.CanAllowExpansion() || m_options.m_show_types ||
789       !m_options.m_allow_oneliner_mode || m_options.m_flat_output ||
790       (m_options.m_pointer_as_array) || m_options.m_show_location)
791          ? false
792          : DataVisualization::ShouldPrintAsOneLiner(*m_valobj);
793  bool is_instance_ptr = IsInstancePointer();
794  uint64_t instance_ptr_value = LLDB_INVALID_ADDRESS;
795
796  if (print_children && is_instance_ptr) {
797    instance_ptr_value = m_valobj->GetValueAsUnsigned(0);
798    if (m_printed_instance_pointers->count(instance_ptr_value)) {
799      // we already printed this instance-is-pointer thing, so don't expand it
800      m_stream->PutCString(" {...}\n");
801
802      // we're done here - get out fast
803      return;
804    } else
805      m_printed_instance_pointers->emplace(
806          instance_ptr_value); // remember this guy for future reference
807  }
808
809  if (print_children) {
810    if (print_oneline) {
811      m_stream->PutChar(' ');
812      PrintChildrenOneLiner(false);
813      m_stream->EOL();
814    } else
815      PrintChildren(value_printed, summary_printed, curr_ptr_depth);
816  } else if (m_curr_depth >= m_options.m_max_depth && IsAggregate() &&
817             ShouldPrintValueObject()) {
818    m_stream->PutCString("{...}\n");
819  } else
820    m_stream->EOL();
821}
822
823bool ValueObjectPrinter::ShouldPrintValidation() {
824  return m_options.m_run_validator;
825}
826
827bool ValueObjectPrinter::PrintValidationMarkerIfNeeded() {
828  if (!ShouldPrintValidation())
829    return false;
830
831  m_validation = m_valobj->GetValidationStatus();
832
833  if (TypeValidatorResult::Failure == m_validation.first) {
834    m_stream->Printf("! ");
835    return true;
836  }
837
838  return false;
839}
840
841bool ValueObjectPrinter::PrintValidationErrorIfNeeded() {
842  if (!ShouldPrintValidation())
843    return false;
844
845  if (TypeValidatorResult::Success == m_validation.first)
846    return false;
847
848  if (m_validation.second.empty())
849    m_validation.second.assign("unknown error");
850
851  m_stream->Printf(" ! validation error: %s", m_validation.second.c_str());
852  m_stream->EOL();
853
854  return true;
855}
856