1ac7ddfbfSEd Maste //===-- OptionGroupValueObjectDisplay.cpp -----------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
10ac7ddfbfSEd Maste #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
11ac7ddfbfSEd Maste 
1235617911SEd Maste #include "lldb/DataFormatters/ValueObjectPrinter.h"
13f678e45dSDimitry Andric #include "lldb/Host/OptionParser.h"
14ac7ddfbfSEd Maste #include "lldb/Interpreter/CommandInterpreter.h"
154ba319b5SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h"
16435933ddSDimitry Andric #include "lldb/Target/Target.h"
17ac7ddfbfSEd Maste 
18435933ddSDimitry Andric #include "llvm/ADT/ArrayRef.h"
19435933ddSDimitry Andric 
20ac7ddfbfSEd Maste using namespace lldb;
21ac7ddfbfSEd Maste using namespace lldb_private;
22ac7ddfbfSEd Maste 
OptionGroupValueObjectDisplay()23435933ddSDimitry Andric OptionGroupValueObjectDisplay::OptionGroupValueObjectDisplay() {}
24435933ddSDimitry Andric 
~OptionGroupValueObjectDisplay()25435933ddSDimitry Andric OptionGroupValueObjectDisplay::~OptionGroupValueObjectDisplay() {}
26435933ddSDimitry Andric 
27*b5893f02SDimitry Andric static const OptionDefinition g_option_table[] = {
28435933ddSDimitry Andric     {LLDB_OPT_SET_1, false, "dynamic-type", 'd',
29*b5893f02SDimitry Andric      OptionParser::eRequiredArgument, nullptr, GetDynamicValueTypes(), 0,
30435933ddSDimitry Andric      eArgTypeNone, "Show the object as its full dynamic type, not its static "
31435933ddSDimitry Andric                    "type, if available."},
32435933ddSDimitry Andric     {LLDB_OPT_SET_1, false, "synthetic-type", 'S',
33*b5893f02SDimitry Andric      OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean,
34435933ddSDimitry Andric      "Show the object obeying its synthetic provider, if available."},
35435933ddSDimitry Andric     {LLDB_OPT_SET_1, false, "depth", 'D', OptionParser::eRequiredArgument,
36*b5893f02SDimitry Andric      nullptr, {}, 0, eArgTypeCount, "Set the max recurse depth when dumping "
37*b5893f02SDimitry Andric                                     "aggregate types (default is infinity)."},
38435933ddSDimitry Andric     {LLDB_OPT_SET_1, false, "flat", 'F', OptionParser::eNoArgument, nullptr,
39*b5893f02SDimitry Andric      {}, 0, eArgTypeNone, "Display results in a flat format that uses "
40435933ddSDimitry Andric                           "expression paths for each variable or member."},
41435933ddSDimitry Andric     {LLDB_OPT_SET_1, false, "location", 'L', OptionParser::eNoArgument, nullptr,
42*b5893f02SDimitry Andric      {}, 0, eArgTypeNone, "Show variable location information."},
43435933ddSDimitry Andric     {LLDB_OPT_SET_1, false, "object-description", 'O',
44*b5893f02SDimitry Andric      OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
45435933ddSDimitry Andric      "Print as an Objective-C object."},
46435933ddSDimitry Andric     {LLDB_OPT_SET_1, false, "ptr-depth", 'P', OptionParser::eRequiredArgument,
47*b5893f02SDimitry Andric      nullptr, {}, 0, eArgTypeCount, "The number of pointers to be traversed "
48*b5893f02SDimitry Andric                                     "when dumping values (default is zero)."},
49435933ddSDimitry Andric     {LLDB_OPT_SET_1, false, "show-types", 'T', OptionParser::eNoArgument,
50*b5893f02SDimitry Andric      nullptr, {}, 0, eArgTypeNone,
51435933ddSDimitry Andric      "Show variable types when dumping values."},
52435933ddSDimitry Andric     {LLDB_OPT_SET_1, false, "no-summary-depth", 'Y',
53*b5893f02SDimitry Andric      OptionParser::eOptionalArgument, nullptr, {}, 0, eArgTypeCount,
54435933ddSDimitry Andric      "Set the depth at which omitting summary information stops (default is "
55435933ddSDimitry Andric      "1)."},
56435933ddSDimitry Andric     {LLDB_OPT_SET_1, false, "raw-output", 'R', OptionParser::eNoArgument,
57*b5893f02SDimitry Andric      nullptr, {}, 0, eArgTypeNone, "Don't use formatting options."},
58435933ddSDimitry Andric     {LLDB_OPT_SET_1, false, "show-all-children", 'A', OptionParser::eNoArgument,
59*b5893f02SDimitry Andric      nullptr, {}, 0, eArgTypeNone,
60435933ddSDimitry Andric      "Ignore the upper bound on the number of children to show."},
61435933ddSDimitry Andric     {LLDB_OPT_SET_1, false, "validate", 'V', OptionParser::eRequiredArgument,
62*b5893f02SDimitry Andric      nullptr, {}, 0, eArgTypeBoolean, "Show results of type validators."},
63435933ddSDimitry Andric     {LLDB_OPT_SET_1, false, "element-count", 'Z',
64*b5893f02SDimitry Andric      OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCount,
65435933ddSDimitry Andric      "Treat the result of the expression as if its type is an array of this "
66435933ddSDimitry Andric      "many values."}};
67435933ddSDimitry Andric 
68435933ddSDimitry Andric llvm::ArrayRef<OptionDefinition>
GetDefinitions()69435933ddSDimitry Andric OptionGroupValueObjectDisplay::GetDefinitions() {
70435933ddSDimitry Andric   return llvm::makeArrayRef(g_option_table);
71ac7ddfbfSEd Maste }
72ac7ddfbfSEd Maste 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)735517e702SDimitry Andric Status OptionGroupValueObjectDisplay::SetOptionValue(
74435933ddSDimitry Andric     uint32_t option_idx, llvm::StringRef option_arg,
75435933ddSDimitry Andric     ExecutionContext *execution_context) {
765517e702SDimitry Andric   Status error;
77ac7ddfbfSEd Maste   const int short_option = g_option_table[option_idx].short_option;
78ac7ddfbfSEd Maste   bool success = false;
79ac7ddfbfSEd Maste 
80435933ddSDimitry Andric   switch (short_option) {
81435933ddSDimitry Andric   case 'd': {
82ac7ddfbfSEd Maste     int32_t result;
83*b5893f02SDimitry Andric     result = OptionArgParser::ToOptionEnum(option_arg, GetDynamicValueTypes(),
84*b5893f02SDimitry Andric                                            2, error);
85ac7ddfbfSEd Maste     if (error.Success())
86ac7ddfbfSEd Maste       use_dynamic = (lldb::DynamicValueType)result;
87435933ddSDimitry Andric   } break;
88435933ddSDimitry Andric   case 'T':
89435933ddSDimitry Andric     show_types = true;
90ac7ddfbfSEd Maste     break;
91435933ddSDimitry Andric   case 'L':
92435933ddSDimitry Andric     show_location = true;
93435933ddSDimitry Andric     break;
94435933ddSDimitry Andric   case 'F':
95435933ddSDimitry Andric     flat_output = true;
96435933ddSDimitry Andric     break;
97435933ddSDimitry Andric   case 'O':
98435933ddSDimitry Andric     use_objc = true;
99435933ddSDimitry Andric     break;
100435933ddSDimitry Andric   case 'R':
101435933ddSDimitry Andric     be_raw = true;
102435933ddSDimitry Andric     break;
103435933ddSDimitry Andric   case 'A':
104435933ddSDimitry Andric     ignore_cap = true;
105435933ddSDimitry Andric     break;
106ac7ddfbfSEd Maste 
107ac7ddfbfSEd Maste   case 'D':
108435933ddSDimitry Andric     if (option_arg.getAsInteger(0, max_depth)) {
109435933ddSDimitry Andric       max_depth = UINT32_MAX;
110435933ddSDimitry Andric       error.SetErrorStringWithFormat("invalid max depth '%s'",
111435933ddSDimitry Andric                                      option_arg.str().c_str());
112435933ddSDimitry Andric     }
113ac7ddfbfSEd Maste     break;
114ac7ddfbfSEd Maste 
1154bb0738eSEd Maste   case 'Z':
116435933ddSDimitry Andric     if (option_arg.getAsInteger(0, elem_count)) {
117435933ddSDimitry Andric       elem_count = UINT32_MAX;
118435933ddSDimitry Andric       error.SetErrorStringWithFormat("invalid element count '%s'",
119435933ddSDimitry Andric                                      option_arg.str().c_str());
120435933ddSDimitry Andric     }
1214bb0738eSEd Maste     break;
1224bb0738eSEd Maste 
123ac7ddfbfSEd Maste   case 'P':
124435933ddSDimitry Andric     if (option_arg.getAsInteger(0, ptr_depth)) {
125435933ddSDimitry Andric       ptr_depth = 0;
126435933ddSDimitry Andric       error.SetErrorStringWithFormat("invalid pointer depth '%s'",
127435933ddSDimitry Andric                                      option_arg.str().c_str());
128435933ddSDimitry Andric     }
129ac7ddfbfSEd Maste     break;
130ac7ddfbfSEd Maste 
131ac7ddfbfSEd Maste   case 'Y':
132435933ddSDimitry Andric     if (option_arg.empty())
133ac7ddfbfSEd Maste       no_summary_depth = 1;
134435933ddSDimitry Andric     else if (option_arg.getAsInteger(0, no_summary_depth)) {
135435933ddSDimitry Andric       no_summary_depth = 0;
136435933ddSDimitry Andric       error.SetErrorStringWithFormat("invalid pointer depth '%s'",
137435933ddSDimitry Andric                                      option_arg.str().c_str());
138435933ddSDimitry Andric     }
139ac7ddfbfSEd Maste     break;
140ac7ddfbfSEd Maste 
141ac7ddfbfSEd Maste   case 'S':
1424ba319b5SDimitry Andric     use_synth = OptionArgParser::ToBoolean(option_arg, true, &success);
143ac7ddfbfSEd Maste     if (!success)
144435933ddSDimitry Andric       error.SetErrorStringWithFormat("invalid synthetic-type '%s'",
145435933ddSDimitry Andric                                      option_arg.str().c_str());
146ac7ddfbfSEd Maste     break;
1477aa51b79SEd Maste 
1487aa51b79SEd Maste   case 'V':
1494ba319b5SDimitry Andric     run_validator = OptionArgParser::ToBoolean(option_arg, true, &success);
1507aa51b79SEd Maste     if (!success)
151435933ddSDimitry Andric       error.SetErrorStringWithFormat("invalid validate '%s'",
152435933ddSDimitry Andric                                      option_arg.str().c_str());
1537aa51b79SEd Maste     break;
1547aa51b79SEd Maste 
155ac7ddfbfSEd Maste   default:
156ac7ddfbfSEd Maste     error.SetErrorStringWithFormat("unrecognized option '%c'", short_option);
157ac7ddfbfSEd Maste     break;
158ac7ddfbfSEd Maste   }
159ac7ddfbfSEd Maste 
160ac7ddfbfSEd Maste   return error;
161ac7ddfbfSEd Maste }
162ac7ddfbfSEd Maste 
OptionParsingStarting(ExecutionContext * execution_context)163435933ddSDimitry Andric void OptionGroupValueObjectDisplay::OptionParsingStarting(
164435933ddSDimitry Andric     ExecutionContext *execution_context) {
165ac7ddfbfSEd Maste   // If these defaults change, be sure to modify AnyOptionWasSet().
166ac7ddfbfSEd Maste   show_types = false;
167ac7ddfbfSEd Maste   no_summary_depth = 0;
168ac7ddfbfSEd Maste   show_location = false;
169ac7ddfbfSEd Maste   flat_output = false;
170ac7ddfbfSEd Maste   use_objc = false;
171ac7ddfbfSEd Maste   max_depth = UINT32_MAX;
172ac7ddfbfSEd Maste   ptr_depth = 0;
1734bb0738eSEd Maste   elem_count = 0;
174ac7ddfbfSEd Maste   use_synth = true;
175ac7ddfbfSEd Maste   be_raw = false;
176ac7ddfbfSEd Maste   ignore_cap = false;
1777aa51b79SEd Maste   run_validator = false;
178ac7ddfbfSEd Maste 
179435933ddSDimitry Andric   TargetSP target_sp =
180435933ddSDimitry Andric       execution_context ? execution_context->GetTargetSP() : TargetSP();
181435933ddSDimitry Andric   if (target_sp)
182435933ddSDimitry Andric     use_dynamic = target_sp->GetPreferDynamicValue();
183435933ddSDimitry Andric   else {
184ac7ddfbfSEd Maste     // If we don't have any targets, then dynamic values won't do us much good.
185ac7ddfbfSEd Maste     use_dynamic = lldb::eNoDynamicValues;
186ac7ddfbfSEd Maste   }
187ac7ddfbfSEd Maste }
188ac7ddfbfSEd Maste 
GetAsDumpOptions(LanguageRuntimeDescriptionDisplayVerbosity lang_descr_verbosity,lldb::Format format,lldb::TypeSummaryImplSP summary_sp)189435933ddSDimitry Andric DumpValueObjectOptions OptionGroupValueObjectDisplay::GetAsDumpOptions(
190435933ddSDimitry Andric     LanguageRuntimeDescriptionDisplayVerbosity lang_descr_verbosity,
191435933ddSDimitry Andric     lldb::Format format, lldb::TypeSummaryImplSP summary_sp) {
19235617911SEd Maste   DumpValueObjectOptions options;
193435933ddSDimitry Andric   options.SetMaximumPointerDepth(
194435933ddSDimitry Andric       {DumpValueObjectOptions::PointerDepth::Mode::Always, ptr_depth});
195ac7ddfbfSEd Maste   if (use_objc)
196ac7ddfbfSEd Maste     options.SetShowSummary(false);
197ac7ddfbfSEd Maste   else
198ac7ddfbfSEd Maste     options.SetOmitSummaryDepth(no_summary_depth);
199ac7ddfbfSEd Maste   options.SetMaximumDepth(max_depth)
200ac7ddfbfSEd Maste       .SetShowTypes(show_types)
201ac7ddfbfSEd Maste       .SetShowLocation(show_location)
202ac7ddfbfSEd Maste       .SetUseObjectiveC(use_objc)
203ac7ddfbfSEd Maste       .SetUseDynamicType(use_dynamic)
204ac7ddfbfSEd Maste       .SetUseSyntheticValue(use_synth)
205ac7ddfbfSEd Maste       .SetFlatOutput(flat_output)
206ac7ddfbfSEd Maste       .SetIgnoreCap(ignore_cap)
207ac7ddfbfSEd Maste       .SetFormat(format)
208ac7ddfbfSEd Maste       .SetSummary(summary_sp);
209ac7ddfbfSEd Maste 
210435933ddSDimitry Andric   if (lang_descr_verbosity ==
211435933ddSDimitry Andric       eLanguageRuntimeDescriptionDisplayVerbosityCompact)
212435933ddSDimitry Andric     options.SetHideRootType(use_objc).SetHideName(use_objc).SetHideValue(
213435933ddSDimitry Andric         use_objc);
214ac7ddfbfSEd Maste 
215ac7ddfbfSEd Maste   if (be_raw)
2167aa51b79SEd Maste     options.SetRawDisplay();
2177aa51b79SEd Maste 
2187aa51b79SEd Maste   options.SetRunValidator(run_validator);
219ac7ddfbfSEd Maste 
2204bb0738eSEd Maste   options.SetElementCount(elem_count);
2214bb0738eSEd Maste 
222ac7ddfbfSEd Maste   return options;
223ac7ddfbfSEd Maste }
224