File indexing completed on 2026-05-10 08:42:50
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLDB_INTERPRETER_OPTIONS_H
0010 #define LLDB_INTERPRETER_OPTIONS_H
0011
0012 #include <set>
0013 #include <vector>
0014
0015 #include "lldb/Utility/Args.h"
0016 #include "lldb/Utility/CompletionRequest.h"
0017 #include "lldb/Utility/OptionDefinition.h"
0018 #include "lldb/Utility/Status.h"
0019 #include "lldb/lldb-defines.h"
0020 #include "lldb/lldb-private.h"
0021
0022 #include "llvm/ADT/ArrayRef.h"
0023 #include "llvm/ADT/StringRef.h"
0024
0025 namespace lldb_private {
0026
0027 struct Option;
0028
0029 typedef std::vector<std::tuple<std::string, int, std::string>> OptionArgVector;
0030 typedef std::shared_ptr<OptionArgVector> OptionArgVectorSP;
0031
0032 struct OptionArgElement {
0033 enum { eUnrecognizedArg = -1, eBareDash = -2, eBareDoubleDash = -3 };
0034
0035 OptionArgElement(int defs_index, int pos, int arg_pos)
0036 : opt_defs_index(defs_index), opt_pos(pos), opt_arg_pos(arg_pos) {}
0037
0038 int opt_defs_index;
0039 int opt_pos;
0040 int opt_arg_pos;
0041 };
0042
0043 typedef std::vector<OptionArgElement> OptionElementVector;
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058 class Options {
0059 public:
0060 Options();
0061
0062 virtual ~Options();
0063
0064 void BuildGetoptTable();
0065
0066 void BuildValidOptionSets();
0067
0068 uint32_t NumCommandOptions();
0069
0070
0071
0072
0073
0074 Option *GetLongOptions();
0075
0076
0077 void OptionSeen(int short_option);
0078
0079 bool VerifyOptions(CommandReturnObject &result);
0080
0081
0082
0083
0084 bool VerifyPartialOptions(CommandReturnObject &result);
0085
0086 void OutputFormattedUsageText(Stream &strm,
0087 const OptionDefinition &option_def,
0088 uint32_t output_max_columns);
0089
0090 void GenerateOptionUsage(Stream &strm, CommandObject &cmd,
0091 uint32_t screen_width);
0092
0093 bool SupportsLongOption(const char *long_option);
0094
0095
0096
0097
0098 virtual llvm::ArrayRef<OptionDefinition> GetDefinitions() {
0099 return llvm::ArrayRef<OptionDefinition>();
0100 }
0101
0102
0103
0104
0105
0106
0107 void NotifyOptionParsingStarting(ExecutionContext *execution_context);
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125 llvm::Expected<Args> Parse(const Args &args,
0126 ExecutionContext *execution_context,
0127 lldb::PlatformSP platform_sp,
0128 bool require_validation);
0129
0130 llvm::Expected<Args> ParseAlias(const Args &args,
0131 OptionArgVector *option_arg_vector,
0132 std::string &input_line);
0133
0134 OptionElementVector ParseForCompletion(const Args &args,
0135 uint32_t cursor_index);
0136
0137 Status NotifyOptionParsingFinished(ExecutionContext *execution_context);
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156 virtual Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
0157 ExecutionContext *execution_context) = 0;
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174 bool HandleOptionCompletion(lldb_private::CompletionRequest &request,
0175 OptionElementVector &option_map,
0176 CommandInterpreter &interpreter);
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186 virtual void
0187 HandleOptionArgumentCompletion(lldb_private::CompletionRequest &request,
0188 OptionElementVector &opt_element_vector,
0189 int opt_element_index,
0190 CommandInterpreter &interpreter);
0191
0192 protected:
0193
0194
0195 typedef std::set<int> OptionSet;
0196 typedef std::vector<OptionSet> OptionSetVector;
0197
0198 std::vector<Option> m_getopt_table;
0199 OptionSet m_seen_options;
0200 OptionSetVector m_required_options;
0201 OptionSetVector m_optional_options;
0202
0203 OptionSetVector &GetRequiredOptions() {
0204 BuildValidOptionSets();
0205 return m_required_options;
0206 }
0207
0208 OptionSetVector &GetOptionalOptions() {
0209 BuildValidOptionSets();
0210 return m_optional_options;
0211 }
0212
0213 bool IsASubset(const OptionSet &set_a, const OptionSet &set_b);
0214
0215 size_t OptionsSetDiff(const OptionSet &set_a, const OptionSet &set_b,
0216 OptionSet &diffs);
0217
0218 void OptionsSetUnion(const OptionSet &set_a, const OptionSet &set_b,
0219 OptionSet &union_set);
0220
0221
0222
0223
0224 virtual void OptionParsingStarting(ExecutionContext *execution_context) = 0;
0225
0226 virtual Status OptionParsingFinished(ExecutionContext *execution_context) {
0227
0228
0229 Status error;
0230 return error;
0231 }
0232 };
0233
0234 class OptionGroup {
0235 public:
0236 OptionGroup() = default;
0237
0238 virtual ~OptionGroup() = default;
0239
0240 virtual llvm::ArrayRef<OptionDefinition> GetDefinitions() = 0;
0241
0242 virtual Status SetOptionValue(uint32_t option_idx,
0243 llvm::StringRef option_value,
0244 ExecutionContext *execution_context) = 0;
0245
0246 virtual void OptionParsingStarting(ExecutionContext *execution_context) = 0;
0247
0248 virtual Status OptionParsingFinished(ExecutionContext *execution_context) {
0249
0250
0251 Status error;
0252 return error;
0253 }
0254 };
0255
0256 class OptionGroupOptions : public Options {
0257 public:
0258 OptionGroupOptions() = default;
0259
0260 ~OptionGroupOptions() override = default;
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270 void Append(OptionGroup *group);
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292 void Append(OptionGroup *group, uint32_t src_mask, uint32_t dst_mask);
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306 void Append(OptionGroup *group,
0307 llvm::ArrayRef<llvm::StringRef> exclude_long_options);
0308
0309 void Finalize();
0310
0311 bool DidFinalize() { return m_did_finalize; }
0312
0313 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
0314 ExecutionContext *execution_context) override;
0315
0316 void OptionParsingStarting(ExecutionContext *execution_context) override;
0317
0318 Status OptionParsingFinished(ExecutionContext *execution_context) override;
0319
0320 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
0321 assert(m_did_finalize);
0322 return m_option_defs;
0323 }
0324
0325 const OptionGroup *GetGroupWithOption(char short_opt);
0326
0327 struct OptionInfo {
0328 OptionInfo(OptionGroup *g, uint32_t i) : option_group(g), option_index(i) {}
0329 OptionGroup *option_group;
0330 uint32_t option_index;
0331 };
0332 typedef std::vector<OptionInfo> OptionInfos;
0333
0334 std::vector<OptionDefinition> m_option_defs;
0335 OptionInfos m_option_infos;
0336 bool m_did_finalize = false;
0337 };
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362 llvm::Error CreateOptionParsingError(llvm::StringRef option_arg,
0363 const char short_option,
0364 llvm::StringRef long_option = {},
0365 llvm::StringRef additional_context = {});
0366
0367 static constexpr llvm::StringLiteral g_bool_parsing_error_message =
0368 "Failed to parse as boolean";
0369 static constexpr llvm::StringLiteral g_int_parsing_error_message =
0370 "Failed to parse as integer";
0371 static constexpr llvm::StringLiteral g_language_parsing_error_message =
0372 "Unknown language";
0373
0374 }
0375
0376 #endif