blob: 17dfd8be3678966d6a7034336396ea9ddbc7fd6f [file] [log] [blame]
Enrico Granata4d93b8c2013-09-30 19:11:51 +00001//===-- 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/Debugger.h"
Enrico Granataa29cb0b2013-10-04 23:14:13 +000017#include "lldb/DataFormatters/DataVisualization.h"
Enrico Granata4d93b8c2013-09-30 19:11:51 +000018#include "lldb/Interpreter/CommandInterpreter.h"
19#include "lldb/Target/Target.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
Enrico Granatad5957332015-06-03 20:43:54 +000024DumpValueObjectOptions::DumpValueObjectOptions (ValueObject& valobj) :
25DumpValueObjectOptions()
26{
27 m_use_dynamic = valobj.GetDynamicValueType();
28 m_use_synthetic = valobj.IsSynthetic();
Enrico Granata73e8c4d2015-10-07 02:36:35 +000029 m_varformat_language = valobj.GetPreferredDisplayLanguage();
Enrico Granatad5957332015-06-03 20:43:54 +000030}
31
32ValueObjectPrinter::ValueObjectPrinter (ValueObject* valobj,
33 Stream* s)
34{
35 if (valobj)
36 {
37 DumpValueObjectOptions options(*valobj);
38 Init (valobj,s,options,options.m_max_ptr_depth,0);
39 }
40 else
41 {
42 DumpValueObjectOptions options;
43 Init (valobj,s,options,options.m_max_ptr_depth,0);
44 }
45}
46
Enrico Granata4d93b8c2013-09-30 19:11:51 +000047ValueObjectPrinter::ValueObjectPrinter (ValueObject* valobj,
48 Stream* s,
Enrico Granata938d1d62013-10-05 00:20:27 +000049 const DumpValueObjectOptions& options)
Enrico Granata4d93b8c2013-09-30 19:11:51 +000050{
Enrico Granata938d1d62013-10-05 00:20:27 +000051 Init(valobj,s,options,options.m_max_ptr_depth,0);
Enrico Granata4d93b8c2013-09-30 19:11:51 +000052}
53
54ValueObjectPrinter::ValueObjectPrinter (ValueObject* valobj,
55 Stream* s,
56 const DumpValueObjectOptions& options,
Enrico Granatac1b7c092015-07-27 18:34:14 +000057 const DumpValueObjectOptions::PointerDepth& ptr_depth,
Enrico Granata938d1d62013-10-05 00:20:27 +000058 uint32_t curr_depth)
59{
60 Init(valobj,s,options,ptr_depth,curr_depth);
61}
62
63void
64ValueObjectPrinter::Init (ValueObject* valobj,
65 Stream* s,
66 const DumpValueObjectOptions& options,
Enrico Granatac1b7c092015-07-27 18:34:14 +000067 const DumpValueObjectOptions::PointerDepth& ptr_depth,
Enrico Granata938d1d62013-10-05 00:20:27 +000068 uint32_t curr_depth)
69{
70 m_orig_valobj = valobj;
71 m_valobj = nullptr;
72 m_stream = s;
73 this->options = options;
74 m_ptr_depth = ptr_depth;
75 m_curr_depth = curr_depth;
76 assert (m_orig_valobj && "cannot print a NULL ValueObject");
77 assert (m_stream && "cannot print to a NULL Stream");
78 m_should_print = eLazyBoolCalculate;
79 m_is_nil = eLazyBoolCalculate;
80 m_is_ptr = eLazyBoolCalculate;
81 m_is_ref = eLazyBoolCalculate;
82 m_is_aggregate = eLazyBoolCalculate;
83 m_summary_formatter = {nullptr,false};
84 m_value.assign("");
85 m_summary.assign("");
86 m_error.assign("");
87}
Enrico Granata4d93b8c2013-09-30 19:11:51 +000088
89bool
90ValueObjectPrinter::PrintValueObject ()
91{
Enrico Granatad07cfd32014-10-08 18:27:36 +000092 if (!GetMostSpecializedValue () || m_valobj == nullptr)
Enrico Granata4d93b8c2013-09-30 19:11:51 +000093 return false;
94
95 if (ShouldPrintValueObject())
96 {
Enrico Granata0f883ff2014-09-06 02:20:19 +000097 PrintValidationMarkerIfNeeded();
98
Enrico Granata4d93b8c2013-09-30 19:11:51 +000099 PrintLocationIfNeeded();
100 m_stream->Indent();
101
Enrico Granatae0ee1e12015-10-17 01:05:50 +0000102 PrintDecl();
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000103 }
104
105 bool value_printed = false;
106 bool summary_printed = false;
107
108 bool val_summary_ok = PrintValueAndSummaryIfNeeded (value_printed,summary_printed);
109
110 if (val_summary_ok)
111 PrintChildrenIfNeeded (value_printed, summary_printed);
Enrico Granata39938932013-10-03 02:06:02 +0000112 else
113 m_stream->EOL();
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000114
Enrico Granata0f883ff2014-09-06 02:20:19 +0000115 PrintValidationErrorIfNeeded();
116
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000117 return true;
118}
119
120bool
Enrico Granatad07cfd32014-10-08 18:27:36 +0000121ValueObjectPrinter::GetMostSpecializedValue ()
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000122{
Enrico Granataa29cb0b2013-10-04 23:14:13 +0000123 if (m_valobj)
124 return true;
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000125 bool update_success = m_orig_valobj->UpdateValueIfNeeded (true);
126 if (!update_success)
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000127 {
Enrico Granata106260c2013-10-22 22:42:14 +0000128 m_valobj = m_orig_valobj;
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000129 }
130 else
Enrico Granata106260c2013-10-22 22:42:14 +0000131 {
132 if (m_orig_valobj->IsDynamic())
133 {
134 if (options.m_use_dynamic == eNoDynamicValues)
135 {
136 ValueObject *static_value = m_orig_valobj->GetStaticValue().get();
137 if (static_value)
138 m_valobj = static_value;
139 else
140 m_valobj = m_orig_valobj;
141 }
142 else
143 m_valobj = m_orig_valobj;
144 }
145 else
146 {
147 if (options.m_use_dynamic != eNoDynamicValues)
148 {
149 ValueObject *dynamic_value = m_orig_valobj->GetDynamicValue(options.m_use_dynamic).get();
150 if (dynamic_value)
151 m_valobj = dynamic_value;
152 else
153 m_valobj = m_orig_valobj;
154 }
155 else
156 m_valobj = m_orig_valobj;
157 }
Enrico Granatad07cfd32014-10-08 18:27:36 +0000158
159 if (m_valobj->IsSynthetic())
160 {
161 if (options.m_use_synthetic == false)
162 {
163 ValueObject *non_synthetic = m_valobj->GetNonSyntheticValue().get();
164 if (non_synthetic)
165 m_valobj = non_synthetic;
166 }
167 }
168 else
169 {
170 if (options.m_use_synthetic == true)
171 {
172 ValueObject *synthetic = m_valobj->GetSyntheticValue().get();
173 if (synthetic)
174 m_valobj = synthetic;
175 }
176 }
Enrico Granata106260c2013-10-22 22:42:14 +0000177 }
Bruce Mitchener59b5a372015-09-17 18:43:40 +0000178 m_compiler_type = m_valobj->GetCompilerType();
179 m_type_flags = m_compiler_type.GetTypeInfo ();
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000180 return true;
181}
182
183const char*
184ValueObjectPrinter::GetDescriptionForDisplay ()
185{
186 const char* str = m_valobj->GetObjectDescription();
187 if (!str)
188 str = m_valobj->GetSummaryAsCString();
189 if (!str)
190 str = m_valobj->GetValueAsCString();
191 return str;
192}
193
194const char*
195ValueObjectPrinter::GetRootNameForDisplay (const char* if_fail)
196{
197 const char *root_valobj_name = options.m_root_valobj_name.empty() ?
198 m_valobj->GetName().AsCString() :
199 options.m_root_valobj_name.c_str();
200 return root_valobj_name ? root_valobj_name : if_fail;
201}
202
203bool
204ValueObjectPrinter::ShouldPrintValueObject ()
205{
206 if (m_should_print == eLazyBoolCalculate)
Enrico Granata622be232014-10-21 20:52:14 +0000207 m_should_print = (options.m_flat_output == false || m_type_flags.Test (eTypeHasValue)) ? eLazyBoolYes : eLazyBoolNo;
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000208 return m_should_print == eLazyBoolYes;
209}
210
211bool
212ValueObjectPrinter::IsNil ()
213{
214 if (m_is_nil == eLazyBoolCalculate)
215 m_is_nil = m_valobj->IsObjCNil() ? eLazyBoolYes : eLazyBoolNo;
216 return m_is_nil == eLazyBoolYes;
217}
218
219bool
220ValueObjectPrinter::IsPtr ()
221{
222 if (m_is_ptr == eLazyBoolCalculate)
Enrico Granata622be232014-10-21 20:52:14 +0000223 m_is_ptr = m_type_flags.Test (eTypeIsPointer) ? eLazyBoolYes : eLazyBoolNo;
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000224 return m_is_ptr == eLazyBoolYes;
225}
226
227bool
228ValueObjectPrinter::IsRef ()
229{
230 if (m_is_ref == eLazyBoolCalculate)
Enrico Granata622be232014-10-21 20:52:14 +0000231 m_is_ref = m_type_flags.Test (eTypeIsReference) ? eLazyBoolYes : eLazyBoolNo;
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000232 return m_is_ref == eLazyBoolYes;
233}
234
235bool
236ValueObjectPrinter::IsAggregate ()
237{
238 if (m_is_aggregate == eLazyBoolCalculate)
Enrico Granata622be232014-10-21 20:52:14 +0000239 m_is_aggregate = m_type_flags.Test (eTypeHasChildren) ? eLazyBoolYes : eLazyBoolNo;
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000240 return m_is_aggregate == eLazyBoolYes;
241}
242
243bool
244ValueObjectPrinter::PrintLocationIfNeeded ()
245{
246 if (options.m_show_location)
247 {
248 m_stream->Printf("%s: ", m_valobj->GetLocationAsCString());
249 return true;
250 }
251 return false;
252}
253
Enrico Granatae0ee1e12015-10-17 01:05:50 +0000254void
255ValueObjectPrinter::PrintDecl ()
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000256{
257 bool show_type = true;
258 // if we are at the root-level and been asked to hide the root's type, then hide it
259 if (m_curr_depth == 0 && options.m_hide_root_type)
260 show_type = false;
261 else
262 // otherwise decide according to the usual rules (asked to show types - always at the root level)
263 show_type = options.m_show_types || (m_curr_depth == 0 && !options.m_flat_output);
264
Enrico Granatae0ee1e12015-10-17 01:05:50 +0000265 StreamString typeName;
266
267 // always show the type at the root level if it is invalid
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000268 if (show_type)
269 {
270 // Some ValueObjects don't have types (like registers sets). Only print
271 // the type if there is one to print
Enrico Granataa126e462014-11-21 18:47:26 +0000272 ConstString type_name;
Enrico Granatae0ee1e12015-10-17 01:05:50 +0000273 if (m_compiler_type.IsValid())
274 {
275 if (options.m_use_type_display_name)
276 type_name = m_valobj->GetDisplayTypeName();
277 else
278 type_name = m_valobj->GetQualifiedTypeName();
279 }
Enrico Granatae8daa2f2014-05-17 19:14:17 +0000280 else
Enrico Granatae0ee1e12015-10-17 01:05:50 +0000281 {
282 // only show an invalid type name if the user explicitly triggered show_type
283 if (options.m_show_types)
284 type_name = ConstString("<invalid type>");
285 else
286 type_name.Clear();
287 }
288
Enrico Granataa126e462014-11-21 18:47:26 +0000289 if (type_name)
Enrico Granatae0ee1e12015-10-17 01:05:50 +0000290 {
291 std::string type_name_str(type_name.GetCString());
292 if (options.m_hide_pointer_value)
293 {
294 for(auto iter = type_name_str.find(" *");
295 iter != std::string::npos;
296 iter = type_name_str.find(" *"))
297 {
298 type_name_str.erase(iter, 2);
299 }
300 }
301 typeName.Printf("(%s)", type_name_str.c_str());
302 }
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000303 }
Enrico Granatae0ee1e12015-10-17 01:05:50 +0000304
305 StreamString varName;
306
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000307 if (options.m_flat_output)
308 {
309 // If we are showing types, also qualify the C++ base classes
310 const bool qualify_cxx_base_classes = show_type;
311 if (!options.m_hide_name)
312 {
Enrico Granatae0ee1e12015-10-17 01:05:50 +0000313 m_valobj->GetExpressionPath(varName, qualify_cxx_base_classes);
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000314 }
315 }
316 else if (!options.m_hide_name)
317 {
318 const char *name_cstr = GetRootNameForDisplay("");
Enrico Granatae0ee1e12015-10-17 01:05:50 +0000319 varName.Printf ("%s", name_cstr);
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000320 }
Enrico Granatae0ee1e12015-10-17 01:05:50 +0000321
322 bool decl_printed = false;
323 if (options.m_decl_printing_helper)
324 {
325 ConstString type_name_cstr(typeName.GetData());
326 ConstString var_name_cstr(varName.GetData());
327
328 StreamString dest_stream;
329 if (options.m_decl_printing_helper(type_name_cstr,
330 var_name_cstr,
331 options,
332 dest_stream))
333 {
334 decl_printed = true;
335 m_stream->Printf("%s", dest_stream.GetData());
336 }
337 }
338
339 if (!decl_printed)
340 {
341 if (typeName.GetSize())
342 m_stream->Printf("%s ", typeName.GetData());
343 if (varName.GetSize())
344 m_stream->Printf("%s =", varName.GetData());
345 else if (!options.m_hide_name)
346 m_stream->Printf(" =");
347 }
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000348}
349
350bool
351ValueObjectPrinter::CheckScopeIfNeeded ()
352{
353 if (options.m_scope_already_checked)
354 return true;
355 return m_valobj->IsInScope();
356}
357
358TypeSummaryImpl*
359ValueObjectPrinter::GetSummaryFormatter ()
360{
361 if (m_summary_formatter.second == false)
362 {
363 TypeSummaryImpl* entry = options.m_summary_sp ? options.m_summary_sp.get() : m_valobj->GetSummaryFormat().get();
364
365 if (options.m_omit_summary_depth > 0)
366 entry = NULL;
367 m_summary_formatter.first = entry;
368 m_summary_formatter.second = true;
369 }
370 return m_summary_formatter.first;
371}
372
Enrico Granatae0ee1e12015-10-17 01:05:50 +0000373static bool
374IsPointerValue (const CompilerType &type)
375{
376 Flags type_flags(type.GetTypeInfo());
377 if (type_flags.AnySet(eTypeIsPointer))
378 return type_flags.AllClear(eTypeIsBuiltIn);
379 return false;
380}
381
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000382void
383ValueObjectPrinter::GetValueSummaryError (std::string& value,
384 std::string& summary,
385 std::string& error)
386{
Enrico Granata465f4bc2014-02-15 01:24:44 +0000387 if (options.m_format != eFormatDefault && options.m_format != m_valobj->GetFormat())
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000388 {
Enrico Granata465f4bc2014-02-15 01:24:44 +0000389 m_valobj->GetValueAsCString(options.m_format,
390 value);
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000391 }
Enrico Granata465f4bc2014-02-15 01:24:44 +0000392 else
393 {
394 const char* val_cstr = m_valobj->GetValueAsCString();
395 if (val_cstr)
396 value.assign(val_cstr);
397 }
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000398 const char* err_cstr = m_valobj->GetError().AsCString();
399 if (err_cstr)
400 error.assign(err_cstr);
401
402 if (ShouldPrintValueObject())
403 {
404 if (IsNil())
405 summary.assign("nil");
406 else if (options.m_omit_summary_depth == 0)
407 {
408 TypeSummaryImpl* entry = GetSummaryFormatter();
409 if (entry)
Enrico Granata73e8c4d2015-10-07 02:36:35 +0000410 m_valobj->GetSummaryAsCString(entry, summary, options.m_varformat_language);
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000411 else
412 {
Enrico Granata73e8c4d2015-10-07 02:36:35 +0000413 const char* sum_cstr = m_valobj->GetSummaryAsCString(options.m_varformat_language);
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000414 if (sum_cstr)
415 summary.assign(sum_cstr);
416 }
417 }
418 }
419}
420
421bool
422ValueObjectPrinter::PrintValueAndSummaryIfNeeded (bool& value_printed,
423 bool& summary_printed)
424{
425 bool error_printed = false;
426 if (ShouldPrintValueObject())
427 {
428 if (!CheckScopeIfNeeded())
429 m_error.assign("out of scope");
430 if (m_error.empty())
431 {
432 GetValueSummaryError(m_value, m_summary, m_error);
433 }
434 if (m_error.size())
435 {
436 error_printed = true;
437 m_stream->Printf (" <%s>\n", m_error.c_str());
438 }
439 else
440 {
441 // Make sure we have a value and make sure the summary didn't
442 // specify that the value should not be printed - and do not print
443 // the value if this thing is nil
444 // (but show the value if the user passes a format explicitly)
445 TypeSummaryImpl* entry = GetSummaryFormatter();
Enrico Granata8a068e62014-04-23 23:16:25 +0000446 if (!IsNil() && !m_value.empty() && (entry == NULL || (entry->DoesPrintValue(m_valobj) || options.m_format != eFormatDefault) || m_summary.empty()) && !options.m_hide_value)
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000447 {
Enrico Granatae0ee1e12015-10-17 01:05:50 +0000448 if (options.m_hide_pointer_value && IsPointerValue(m_valobj->GetCompilerType())) {}
449 else
450 {
451 m_stream->Printf(" %s", m_value.c_str());
452 value_printed = true;
453 }
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000454 }
455
456 if (m_summary.size())
457 {
458 m_stream->Printf(" %s", m_summary.c_str());
459 summary_printed = true;
460 }
461 }
462 }
463 return !error_printed;
464}
465
466bool
467ValueObjectPrinter::PrintObjectDescriptionIfNeeded (bool value_printed,
468 bool summary_printed)
469{
470 if (ShouldPrintValueObject())
471 {
472 // let's avoid the overly verbose no description error for a nil thing
473 if (options.m_use_objc && !IsNil())
474 {
475 if (!options.m_hide_value || !options.m_hide_name)
476 m_stream->Printf(" ");
477 const char *object_desc = nullptr;
478 if (value_printed || summary_printed)
479 object_desc = m_valobj->GetObjectDescription();
480 else
481 object_desc = GetDescriptionForDisplay();
482 if (object_desc && *object_desc)
483 {
484 m_stream->Printf("%s\n", object_desc);
485 return true;
486 }
487 else if (value_printed == false && summary_printed == false)
488 return true;
489 else
490 return false;
491 }
492 }
493 return true;
494}
495
496bool
Enrico Granatac1b7c092015-07-27 18:34:14 +0000497DumpValueObjectOptions::PointerDepth::CanAllowExpansion (bool is_root,
498 TypeSummaryImpl* entry,
499 ValueObject *valobj,
500 const std::string& summary)
501{
502 switch (m_mode)
503 {
504 case Mode::Always:
505 return (m_count > 0);
506 case Mode::Never:
507 return false;
508 case Mode::Default:
509 if (is_root)
510 m_count = std::min<decltype(m_count)>(m_count,1);
511 return m_count > 0;
512 case Mode::Formatters:
513 if (!entry || entry->DoesPrintChildren(valobj) || summary.empty())
514 return m_count > 0;
515 return false;
516 }
Zachary Turner84f5b0d2015-09-09 17:25:43 +0000517 return false;
Enrico Granatac1b7c092015-07-27 18:34:14 +0000518}
519
520bool
521DumpValueObjectOptions::PointerDepth::CanAllowExpansion () const
522{
523 switch (m_mode)
524 {
525 case Mode::Always:
526 case Mode::Default:
527 case Mode::Formatters:
528 return (m_count > 0);
529 case Mode::Never:
530 return false;
531 }
Zachary Turner84f5b0d2015-09-09 17:25:43 +0000532 return false;
Enrico Granatac1b7c092015-07-27 18:34:14 +0000533}
534
535bool
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000536ValueObjectPrinter::ShouldPrintChildren (bool is_failed_description,
Enrico Granatac1b7c092015-07-27 18:34:14 +0000537 DumpValueObjectOptions::PointerDepth& curr_ptr_depth)
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000538{
539 const bool is_ref = IsRef ();
540 const bool is_ptr = IsPtr ();
541
Enrico Granatac1b7c092015-07-27 18:34:14 +0000542 TypeSummaryImpl* entry = GetSummaryFormatter();
543
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000544 if (is_failed_description || m_curr_depth < options.m_max_depth)
545 {
546 // We will show children for all concrete types. We won't show
547 // pointer contents unless a pointer depth has been specified.
548 // We won't reference contents unless the reference is the
549 // root object (depth of zero).
550
551 // Use a new temporary pointer depth in case we override the
552 // current pointer depth below...
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000553
554 if (is_ptr || is_ref)
555 {
556 // We have a pointer or reference whose value is an address.
557 // Make sure that address is not NULL
558 AddressType ptr_address_type;
559 if (m_valobj->GetPointerValue (&ptr_address_type) == 0)
560 return false;
561
Enrico Granatac1b7c092015-07-27 18:34:14 +0000562 const bool is_root_level = m_curr_depth == 0;
563
564 if (is_ref &&
565 is_root_level)
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000566 {
567 // If this is the root object (depth is zero) that we are showing
568 // and it is a reference, and no pointer depth has been supplied
569 // print out what it references. Don't do this at deeper depths
570 // otherwise we can end up with infinite recursion...
Enrico Granatac1b7c092015-07-27 18:34:14 +0000571 return true;
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000572 }
573
Enrico Granatac1b7c092015-07-27 18:34:14 +0000574 return curr_ptr_depth.CanAllowExpansion(false, entry, m_valobj, m_summary);
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000575 }
576
Enrico Granata8a068e62014-04-23 23:16:25 +0000577 return (!entry || entry->DoesPrintChildren(m_valobj) || m_summary.empty());
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000578 }
579 return false;
580}
581
Siva Chandrad26eb902015-07-24 21:30:58 +0000582bool
583ValueObjectPrinter::ShouldExpandEmptyAggregates ()
584{
585 TypeSummaryImpl* entry = GetSummaryFormatter();
586
587 if (!entry)
588 return true;
589
590 return entry->DoesPrintEmptyAggregates();
591}
592
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000593ValueObject*
594ValueObjectPrinter::GetValueObjectForChildrenGeneration ()
595{
Enrico Granatad07cfd32014-10-08 18:27:36 +0000596 return m_valobj;
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000597}
598
599void
600ValueObjectPrinter::PrintChildrenPreamble ()
601{
602 if (options.m_flat_output)
603 {
604 if (ShouldPrintValueObject())
605 m_stream->EOL();
606 }
607 else
608 {
609 if (ShouldPrintValueObject())
610 m_stream->PutCString(IsRef () ? ": {\n" : " {\n");
611 m_stream->IndentMore();
612 }
613}
614
615void
616ValueObjectPrinter::PrintChild (ValueObjectSP child_sp,
Enrico Granatac1b7c092015-07-27 18:34:14 +0000617 const DumpValueObjectOptions::PointerDepth& curr_ptr_depth)
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000618{
619 DumpValueObjectOptions child_options(options);
Enrico Granata443844c2015-06-17 02:11:48 +0000620 child_options.SetFormat(options.m_format).SetSummary().SetRootValueObjectName();
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000621 child_options.SetScopeChecked(true).SetHideName(options.m_hide_name).SetHideValue(options.m_hide_value)
622 .SetOmitSummaryDepth(child_options.m_omit_summary_depth > 1 ? child_options.m_omit_summary_depth - 1 : 0);
623 if (child_sp.get())
624 {
625 ValueObjectPrinter child_printer(child_sp.get(),
626 m_stream,
627 child_options,
Enrico Granatac1b7c092015-07-27 18:34:14 +0000628 (IsPtr() || IsRef()) ? --curr_ptr_depth : curr_ptr_depth,
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000629 m_curr_depth + 1);
630 child_printer.PrintValueObject();
631 }
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000632}
633
634uint32_t
635ValueObjectPrinter::GetMaxNumChildrenToPrint (bool& print_dotdotdot)
636{
637 ValueObject* synth_m_valobj = GetValueObjectForChildrenGeneration();
638
639 size_t num_children = synth_m_valobj->GetNumChildren();
640 print_dotdotdot = false;
641 if (num_children)
642 {
643 const size_t max_num_children = m_valobj->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
644
645 if (num_children > max_num_children && !options.m_ignore_cap)
646 {
647 print_dotdotdot = true;
648 return max_num_children;
649 }
650 }
651 return num_children;
652}
653
654void
655ValueObjectPrinter::PrintChildrenPostamble (bool print_dotdotdot)
656{
657 if (!options.m_flat_output)
658 {
659 if (print_dotdotdot)
660 {
661 m_valobj->GetTargetSP()->GetDebugger().GetCommandInterpreter().ChildrenTruncated();
662 m_stream->Indent("...\n");
663 }
664 m_stream->IndentLess();
665 m_stream->Indent("}\n");
666 }
667}
668
669void
Enrico Granatac1b7c092015-07-27 18:34:14 +0000670ValueObjectPrinter::PrintChildren (bool value_printed,
671 bool summary_printed,
672 const DumpValueObjectOptions::PointerDepth& curr_ptr_depth)
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000673{
674 ValueObject* synth_m_valobj = GetValueObjectForChildrenGeneration();
675
676 bool print_dotdotdot = false;
677 size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
678 if (num_children)
679 {
680 PrintChildrenPreamble ();
681
682 for (size_t idx=0; idx<num_children; ++idx)
683 {
684 ValueObjectSP child_sp(synth_m_valobj->GetChildAtIndex(idx, true));
685 PrintChild (child_sp, curr_ptr_depth);
686 }
687
688 PrintChildrenPostamble (print_dotdotdot);
689 }
690 else if (IsAggregate())
691 {
692 // Aggregate, no children...
693 if (ShouldPrintValueObject())
Enrico Granatad07cfd32014-10-08 18:27:36 +0000694 {
695 // if it has a synthetic value, then don't print {}, the synthetic children are probably only being used to vend a value
Siva Chandrad26eb902015-07-24 21:30:58 +0000696 if (m_valobj->DoesProvideSyntheticValue() || !ShouldExpandEmptyAggregates())
Enrico Granatad07cfd32014-10-08 18:27:36 +0000697 m_stream->PutCString( "\n");
698 else
699 m_stream->PutCString(" {}\n");
700 }
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000701 }
702 else
703 {
704 if (ShouldPrintValueObject())
705 m_stream->EOL();
706 }
707}
708
Enrico Granataa29cb0b2013-10-04 23:14:13 +0000709bool
710ValueObjectPrinter::PrintChildrenOneLiner (bool hide_names)
711{
Enrico Granatad07cfd32014-10-08 18:27:36 +0000712 if (!GetMostSpecializedValue () || m_valobj == nullptr)
Enrico Granataa29cb0b2013-10-04 23:14:13 +0000713 return false;
714
715 ValueObject* synth_m_valobj = GetValueObjectForChildrenGeneration();
716
717 bool print_dotdotdot = false;
718 size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
719
720 if (num_children)
721 {
722 m_stream->PutChar('(');
723
724 for (uint32_t idx=0; idx<num_children; ++idx)
725 {
726 lldb::ValueObjectSP child_sp(synth_m_valobj->GetChildAtIndex(idx, true));
Enrico Granataddac7612014-10-09 18:47:36 +0000727 if (child_sp)
728 child_sp = child_sp->GetQualifiedRepresentationIfAvailable(options.m_use_dynamic, options.m_use_synthetic);
Enrico Granataa29cb0b2013-10-04 23:14:13 +0000729 if (child_sp)
730 {
731 if (idx)
732 m_stream->PutCString(", ");
733 if (!hide_names)
734 {
735 const char* name = child_sp.get()->GetName().AsCString();
736 if (name && *name)
737 {
738 m_stream->PutCString(name);
739 m_stream->PutCString(" = ");
740 }
741 }
742 child_sp->DumpPrintableRepresentation(*m_stream,
743 ValueObject::eValueObjectRepresentationStyleSummary,
Enrico Granata443844c2015-06-17 02:11:48 +0000744 lldb::eFormatInvalid,
Enrico Granataa29cb0b2013-10-04 23:14:13 +0000745 ValueObject::ePrintableRepresentationSpecialCasesDisable);
746 }
747 }
748
749 if (print_dotdotdot)
750 m_stream->PutCString(", ...)");
751 else
752 m_stream->PutChar(')');
753 }
754 return true;
755}
756
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000757void
758ValueObjectPrinter::PrintChildrenIfNeeded (bool value_printed,
759 bool summary_printed)
760{
761 // this flag controls whether we tried to display a description for this object and failed
762 // if that happens, we want to display the children, if any
763 bool is_failed_description = !PrintObjectDescriptionIfNeeded(value_printed, summary_printed);
764
Enrico Granatac1b7c092015-07-27 18:34:14 +0000765 auto curr_ptr_depth = m_ptr_depth;
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000766 bool print_children = ShouldPrintChildren (is_failed_description,curr_ptr_depth);
Enrico Granatac1b7c092015-07-27 18:34:14 +0000767 bool print_oneline = (curr_ptr_depth.CanAllowExpansion() ||
Enrico Granata3fa6dc92015-03-12 22:16:20 +0000768 options.m_show_types ||
769 !options.m_allow_oneliner_mode ||
770 options.m_flat_output ||
771 options.m_show_location) ? false : DataVisualization::ShouldPrintAsOneLiner(*m_valobj);
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000772
773 if (print_children)
774 {
Enrico Granataa29cb0b2013-10-04 23:14:13 +0000775 if (print_oneline)
776 {
777 m_stream->PutChar(' ');
778 PrintChildrenOneLiner (false);
779 m_stream->EOL();
780 }
781 else
Enrico Granatac1b7c092015-07-27 18:34:14 +0000782 PrintChildren (value_printed, summary_printed, curr_ptr_depth);
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000783 }
784 else if (m_curr_depth >= options.m_max_depth && IsAggregate() && ShouldPrintValueObject())
785 {
786 m_stream->PutCString("{...}\n");
787 }
Enrico Granata245b3ca2013-10-03 18:11:24 +0000788 else
789 m_stream->EOL();
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000790}
Enrico Granata0f883ff2014-09-06 02:20:19 +0000791
792bool
793ValueObjectPrinter::ShouldPrintValidation ()
794{
795 return options.m_run_validator;
796}
797
798bool
799ValueObjectPrinter::PrintValidationMarkerIfNeeded ()
800{
801 if (!ShouldPrintValidation())
802 return false;
803
804 m_validation = m_valobj->GetValidationStatus();
805
806 if (TypeValidatorResult::Failure == m_validation.first)
807 {
808 m_stream->Printf("! ");
809 return true;
810 }
811
812 return false;
813}
814
815bool
816ValueObjectPrinter::PrintValidationErrorIfNeeded ()
817{
818 if (!ShouldPrintValidation())
819 return false;
820
821 if (TypeValidatorResult::Success == m_validation.first)
822 return false;
823
824 if (m_validation.second.empty())
825 m_validation.second.assign("unknown error");
826
827 m_stream->Printf(" ! validation error: %s", m_validation.second.c_str());
828 m_stream->EOL();
829
830 return true;
831}