1 //===-- OptionGroupVariable.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/Interpreter/OptionGroupVariable.h"
11
12 #include "lldb/DataFormatters/DataVisualization.h"
13 #include "lldb/Host/OptionParser.h"
14 #include "lldb/Interpreter/CommandInterpreter.h"
15 #include "lldb/Target/Target.h"
16 #include "lldb/Utility/Status.h"
17
18 using namespace lldb;
19 using namespace lldb_private;
20
21 // if you add any options here, remember to update the counters in
22 // OptionGroupVariable::GetNumDefinitions()
23 static constexpr OptionDefinition g_variable_options[] = {
24 {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "no-args", 'a',
25 OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
26 "Omit function arguments."},
27 {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "no-recognized-args", 't',
28 OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
29 "Omit recognized function arguments."},
30 {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "no-locals", 'l',
31 OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
32 "Omit local variables."},
33 {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "show-globals", 'g',
34 OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
35 "Show the current frame source file global and static variables."},
36 {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "show-declaration", 'c',
37 OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
38 "Show variable declaration information (source file and line where the "
39 "variable was declared)."},
40 {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "regex", 'r',
41 OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeRegularExpression,
42 "The <variable-name> argument for name lookups are regular expressions."},
43 {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "scope", 's',
44 OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
45 "Show variable scope (argument, local, global, static)."},
46 {LLDB_OPT_SET_1, false, "summary", 'y', OptionParser::eRequiredArgument,
47 nullptr, {}, 0, eArgTypeName,
48 "Specify the summary that the variable output should use."},
49 {LLDB_OPT_SET_2, false, "summary-string", 'z',
50 OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeName,
51 "Specify a summary string to use to format the variable output."},
52 };
53
ValidateNamedSummary(const char * str,void *)54 static Status ValidateNamedSummary(const char *str, void *) {
55 if (!str || !str[0])
56 return Status("must specify a valid named summary");
57 TypeSummaryImplSP summary_sp;
58 if (!DataVisualization::NamedSummaryFormats::GetSummaryFormat(
59 ConstString(str), summary_sp))
60 return Status("must specify a valid named summary");
61 return Status();
62 }
63
ValidateSummaryString(const char * str,void *)64 static Status ValidateSummaryString(const char *str, void *) {
65 if (!str || !str[0])
66 return Status("must specify a non-empty summary string");
67 return Status();
68 }
69
OptionGroupVariable(bool show_frame_options)70 OptionGroupVariable::OptionGroupVariable(bool show_frame_options)
71 : OptionGroup(), include_frame_options(show_frame_options),
72 summary(ValidateNamedSummary), summary_string(ValidateSummaryString) {}
73
~OptionGroupVariable()74 OptionGroupVariable::~OptionGroupVariable() {}
75
76 Status
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)77 OptionGroupVariable::SetOptionValue(uint32_t option_idx,
78 llvm::StringRef option_arg,
79 ExecutionContext *execution_context) {
80 Status error;
81 if (!include_frame_options)
82 option_idx += 3;
83 const int short_option = g_variable_options[option_idx].short_option;
84 switch (short_option) {
85 case 'r':
86 use_regex = true;
87 break;
88 case 'a':
89 show_args = false;
90 break;
91 case 'l':
92 show_locals = false;
93 break;
94 case 'g':
95 show_globals = true;
96 break;
97 case 'c':
98 show_decl = true;
99 break;
100 case 's':
101 show_scope = true;
102 break;
103 case 't':
104 show_recognized_args = false;
105 break;
106 case 'y':
107 error = summary.SetCurrentValue(option_arg);
108 break;
109 case 'z':
110 error = summary_string.SetCurrentValue(option_arg);
111 break;
112 default:
113 error.SetErrorStringWithFormat("unrecognized short option '%c'",
114 short_option);
115 break;
116 }
117
118 return error;
119 }
120
OptionParsingStarting(ExecutionContext * execution_context)121 void OptionGroupVariable::OptionParsingStarting(
122 ExecutionContext *execution_context) {
123 show_args = true; // Frame option only
124 show_recognized_args = true; // Frame option only
125 show_locals = true; // Frame option only
126 show_globals = false; // Frame option only
127 show_decl = false;
128 use_regex = false;
129 show_scope = false;
130 summary.Clear();
131 summary_string.Clear();
132 }
133
134 #define NUM_FRAME_OPTS 3
135
GetDefinitions()136 llvm::ArrayRef<OptionDefinition> OptionGroupVariable::GetDefinitions() {
137 auto result = llvm::makeArrayRef(g_variable_options);
138 // Show the "--no-args", "--no-locals" and "--show-globals" options if we are
139 // showing frame specific options
140 if (include_frame_options)
141 return result;
142
143 // Skip the "--no-args", "--no-locals" and "--show-globals" options if we are
144 // not showing frame specific options (globals only)
145 return result.drop_front(NUM_FRAME_OPTS);
146 }
147