1cfca06d7SDimitry Andric //===-- OptionGroupVariable.cpp -------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/Interpreter/OptionGroupVariable.h"
10f034231aSEd Maste
11f034231aSEd Maste #include "lldb/DataFormatters/DataVisualization.h"
1274a628f7SDimitry Andric #include "lldb/Host/OptionParser.h"
13f034231aSEd Maste #include "lldb/Interpreter/CommandInterpreter.h"
14f034231aSEd Maste #include "lldb/Target/Target.h"
15b76161e4SDimitry Andric #include "lldb/Utility/Status.h"
16f034231aSEd Maste
17f034231aSEd Maste using namespace lldb;
18f034231aSEd Maste using namespace lldb_private;
19f034231aSEd Maste
2014f1b3e8SDimitry Andric // if you add any options here, remember to update the counters in
2114f1b3e8SDimitry Andric // OptionGroupVariable::GetNumDefinitions()
2294994d37SDimitry Andric static constexpr OptionDefinition g_variable_options[] = {
2314f1b3e8SDimitry Andric {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "no-args", 'a',
2494994d37SDimitry Andric OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
2514f1b3e8SDimitry Andric "Omit function arguments."},
2694994d37SDimitry Andric {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "no-recognized-args", 't',
2794994d37SDimitry Andric OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
2894994d37SDimitry Andric "Omit recognized function arguments."},
2914f1b3e8SDimitry Andric {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "no-locals", 'l',
3094994d37SDimitry Andric OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
3114f1b3e8SDimitry Andric "Omit local variables."},
3214f1b3e8SDimitry Andric {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "show-globals", 'g',
3394994d37SDimitry Andric OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
3414f1b3e8SDimitry Andric "Show the current frame source file global and static variables."},
3514f1b3e8SDimitry Andric {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "show-declaration", 'c',
3694994d37SDimitry Andric OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
3714f1b3e8SDimitry Andric "Show variable declaration information (source file and line where the "
3814f1b3e8SDimitry Andric "variable was declared)."},
3914f1b3e8SDimitry Andric {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "regex", 'r',
4094994d37SDimitry Andric OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeRegularExpression,
4114f1b3e8SDimitry Andric "The <variable-name> argument for name lookups are regular expressions."},
4214f1b3e8SDimitry Andric {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "scope", 's',
4394994d37SDimitry Andric OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
4414f1b3e8SDimitry Andric "Show variable scope (argument, local, global, static)."},
4514f1b3e8SDimitry Andric {LLDB_OPT_SET_1, false, "summary", 'y', OptionParser::eRequiredArgument,
4694994d37SDimitry Andric nullptr, {}, 0, eArgTypeName,
4714f1b3e8SDimitry Andric "Specify the summary that the variable output should use."},
4814f1b3e8SDimitry Andric {LLDB_OPT_SET_2, false, "summary-string", 'z',
4994994d37SDimitry Andric OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeName,
5014f1b3e8SDimitry Andric "Specify a summary string to use to format the variable output."},
51f034231aSEd Maste };
52f034231aSEd Maste
53ac9a064cSDimitry Andric static constexpr auto g_num_frame_options = 4;
54ac9a064cSDimitry Andric static const auto g_variable_options_noframe =
55ac9a064cSDimitry Andric llvm::ArrayRef<OptionDefinition>(g_variable_options)
56ac9a064cSDimitry Andric .drop_front(g_num_frame_options);
57ac9a064cSDimitry Andric
ValidateNamedSummary(const char * str,void *)58b76161e4SDimitry Andric static Status ValidateNamedSummary(const char *str, void *) {
59f034231aSEd Maste if (!str || !str[0])
60b76161e4SDimitry Andric return Status("must specify a valid named summary");
61f034231aSEd Maste TypeSummaryImplSP summary_sp;
6294994d37SDimitry Andric if (!DataVisualization::NamedSummaryFormats::GetSummaryFormat(
6394994d37SDimitry Andric ConstString(str), summary_sp))
64b76161e4SDimitry Andric return Status("must specify a valid named summary");
65b76161e4SDimitry Andric return Status();
66f034231aSEd Maste }
67f034231aSEd Maste
ValidateSummaryString(const char * str,void *)68b76161e4SDimitry Andric static Status ValidateSummaryString(const char *str, void *) {
69f034231aSEd Maste if (!str || !str[0])
70b76161e4SDimitry Andric return Status("must specify a non-empty summary string");
71b76161e4SDimitry Andric return Status();
72f034231aSEd Maste }
73f034231aSEd Maste
OptionGroupVariable(bool show_frame_options)7414f1b3e8SDimitry Andric OptionGroupVariable::OptionGroupVariable(bool show_frame_options)
75e3b55780SDimitry Andric : include_frame_options(show_frame_options), show_args(false),
76e3b55780SDimitry Andric show_recognized_args(false), show_locals(false), show_globals(false),
77e3b55780SDimitry Andric use_regex(false), show_scope(false), show_decl(false),
78e3b55780SDimitry Andric summary(ValidateNamedSummary), summary_string(ValidateSummaryString) {}
79f034231aSEd Maste
80b76161e4SDimitry Andric Status
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)81b76161e4SDimitry Andric OptionGroupVariable::SetOptionValue(uint32_t option_idx,
8214f1b3e8SDimitry Andric llvm::StringRef option_arg,
8314f1b3e8SDimitry Andric ExecutionContext *execution_context) {
84b76161e4SDimitry Andric Status error;
85ac9a064cSDimitry Andric llvm::ArrayRef<OptionDefinition> variable_options =
86ac9a064cSDimitry Andric include_frame_options ? g_variable_options : g_variable_options_noframe;
87ac9a064cSDimitry Andric const int short_option = variable_options[option_idx].short_option;
8814f1b3e8SDimitry Andric switch (short_option) {
8914f1b3e8SDimitry Andric case 'r':
9014f1b3e8SDimitry Andric use_regex = true;
9114f1b3e8SDimitry Andric break;
9214f1b3e8SDimitry Andric case 'a':
9314f1b3e8SDimitry Andric show_args = false;
9414f1b3e8SDimitry Andric break;
9514f1b3e8SDimitry Andric case 'l':
9614f1b3e8SDimitry Andric show_locals = false;
9714f1b3e8SDimitry Andric break;
9814f1b3e8SDimitry Andric case 'g':
9914f1b3e8SDimitry Andric show_globals = true;
10014f1b3e8SDimitry Andric break;
10114f1b3e8SDimitry Andric case 'c':
10214f1b3e8SDimitry Andric show_decl = true;
10314f1b3e8SDimitry Andric break;
104f034231aSEd Maste case 's':
105f034231aSEd Maste show_scope = true;
106f034231aSEd Maste break;
10794994d37SDimitry Andric case 't':
10894994d37SDimitry Andric show_recognized_args = false;
10994994d37SDimitry Andric break;
110f034231aSEd Maste case 'y':
111f034231aSEd Maste error = summary.SetCurrentValue(option_arg);
112f034231aSEd Maste break;
113f034231aSEd Maste case 'z':
114f034231aSEd Maste error = summary_string.SetCurrentValue(option_arg);
115f034231aSEd Maste break;
116f034231aSEd Maste default:
117ead24645SDimitry Andric llvm_unreachable("Unimplemented option");
118f034231aSEd Maste }
119f034231aSEd Maste
120f034231aSEd Maste return error;
121f034231aSEd Maste }
122f034231aSEd Maste
OptionParsingStarting(ExecutionContext * execution_context)12314f1b3e8SDimitry Andric void OptionGroupVariable::OptionParsingStarting(
12414f1b3e8SDimitry Andric ExecutionContext *execution_context) {
125f034231aSEd Maste show_args = true; // Frame option only
12694994d37SDimitry Andric show_recognized_args = true; // Frame option only
127f034231aSEd Maste show_locals = true; // Frame option only
128f034231aSEd Maste show_globals = false; // Frame option only
129f034231aSEd Maste show_decl = false;
130f034231aSEd Maste use_regex = false;
131f034231aSEd Maste show_scope = false;
132f034231aSEd Maste summary.Clear();
133f034231aSEd Maste summary_string.Clear();
134f034231aSEd Maste }
135f034231aSEd Maste
GetDefinitions()13614f1b3e8SDimitry Andric llvm::ArrayRef<OptionDefinition> OptionGroupVariable::GetDefinitions() {
137ac9a064cSDimitry Andric // Show the "--no-args", "--no-recognized-args", "--no-locals" and
138ac9a064cSDimitry Andric // "--show-globals" options if we are showing frame specific options
139ac9a064cSDimitry Andric return include_frame_options ? g_variable_options
140ac9a064cSDimitry Andric : g_variable_options_noframe;
141f034231aSEd Maste }
142