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