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