1 //===-- Options.cpp -------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Interpreter/Options.h"
10 
11 #include <algorithm>
12 #include <bitset>
13 #include <map>
14 #include <set>
15 
16 #include "lldb/Host/OptionParser.h"
17 #include "lldb/Interpreter/CommandCompletions.h"
18 #include "lldb/Interpreter/CommandInterpreter.h"
19 #include "lldb/Interpreter/CommandObject.h"
20 #include "lldb/Interpreter/CommandReturnObject.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Utility/StreamString.h"
23 #include "llvm/ADT/STLExtras.h"
24 
25 using namespace lldb;
26 using namespace lldb_private;
27 
28 // Options
29 Options::Options() { BuildValidOptionSets(); }
30 
31 Options::~Options() = default;
32 
33 void Options::NotifyOptionParsingStarting(ExecutionContext *execution_context) {
34   m_seen_options.clear();
35   // Let the subclass reset its option values
36   OptionParsingStarting(execution_context);
37 }
38 
39 Status
40 Options::NotifyOptionParsingFinished(ExecutionContext *execution_context) {
41   return OptionParsingFinished(execution_context);
42 }
43 
44 void Options::OptionSeen(int option_idx) { m_seen_options.insert(option_idx); }
45 
46 // Returns true is set_a is a subset of set_b;  Otherwise returns false.
47 
48 bool Options::IsASubset(const OptionSet &set_a, const OptionSet &set_b) {
49   bool is_a_subset = true;
50   OptionSet::const_iterator pos_a;
51   OptionSet::const_iterator pos_b;
52 
53   // set_a is a subset of set_b if every member of set_a is also a member of
54   // set_b
55 
56   for (pos_a = set_a.begin(); pos_a != set_a.end() && is_a_subset; ++pos_a) {
57     pos_b = set_b.find(*pos_a);
58     if (pos_b == set_b.end())
59       is_a_subset = false;
60   }
61 
62   return is_a_subset;
63 }
64 
65 // Returns the set difference set_a - set_b, i.e. { x | ElementOf (x, set_a) &&
66 // !ElementOf (x, set_b) }
67 
68 size_t Options::OptionsSetDiff(const OptionSet &set_a, const OptionSet &set_b,
69                                OptionSet &diffs) {
70   size_t num_diffs = 0;
71   OptionSet::const_iterator pos_a;
72   OptionSet::const_iterator pos_b;
73 
74   for (pos_a = set_a.begin(); pos_a != set_a.end(); ++pos_a) {
75     pos_b = set_b.find(*pos_a);
76     if (pos_b == set_b.end()) {
77       ++num_diffs;
78       diffs.insert(*pos_a);
79     }
80   }
81 
82   return num_diffs;
83 }
84 
85 // Returns the union of set_a and set_b.  Does not put duplicate members into
86 // the union.
87 
88 void Options::OptionsSetUnion(const OptionSet &set_a, const OptionSet &set_b,
89                               OptionSet &union_set) {
90   OptionSet::const_iterator pos;
91   OptionSet::iterator pos_union;
92 
93   // Put all the elements of set_a into the union.
94 
95   for (pos = set_a.begin(); pos != set_a.end(); ++pos)
96     union_set.insert(*pos);
97 
98   // Put all the elements of set_b that are not already there into the union.
99   for (pos = set_b.begin(); pos != set_b.end(); ++pos) {
100     pos_union = union_set.find(*pos);
101     if (pos_union == union_set.end())
102       union_set.insert(*pos);
103   }
104 }
105 
106 bool Options::VerifyOptions(CommandReturnObject &result) {
107   bool options_are_valid = false;
108 
109   int num_levels = GetRequiredOptions().size();
110   if (num_levels) {
111     for (int i = 0; i < num_levels && !options_are_valid; ++i) {
112       // This is the correct set of options if:  1). m_seen_options contains
113       // all of m_required_options[i] (i.e. all the required options at this
114       // level are a subset of m_seen_options); AND 2). { m_seen_options -
115       // m_required_options[i] is a subset of m_options_options[i] (i.e. all
116       // the rest of m_seen_options are in the set of optional options at this
117       // level.
118 
119       // Check to see if all of m_required_options[i] are a subset of
120       // m_seen_options
121       if (IsASubset(GetRequiredOptions()[i], m_seen_options)) {
122         // Construct the set difference: remaining_options = {m_seen_options} -
123         // {m_required_options[i]}
124         OptionSet remaining_options;
125         OptionsSetDiff(m_seen_options, GetRequiredOptions()[i],
126                        remaining_options);
127         // Check to see if remaining_options is a subset of
128         // m_optional_options[i]
129         if (IsASubset(remaining_options, GetOptionalOptions()[i]))
130           options_are_valid = true;
131       }
132     }
133   } else {
134     options_are_valid = true;
135   }
136 
137   if (options_are_valid) {
138     result.SetStatus(eReturnStatusSuccessFinishNoResult);
139   } else {
140     result.AppendError("invalid combination of options for the given command");
141   }
142 
143   return options_are_valid;
144 }
145 
146 // This is called in the Options constructor, though we could call it lazily if
147 // that ends up being a performance problem.
148 
149 void Options::BuildValidOptionSets() {
150   // Check to see if we already did this.
151   if (m_required_options.size() != 0)
152     return;
153 
154   // Check to see if there are any options.
155   int num_options = NumCommandOptions();
156   if (num_options == 0)
157     return;
158 
159   auto opt_defs = GetDefinitions();
160   m_required_options.resize(1);
161   m_optional_options.resize(1);
162 
163   // First count the number of option sets we've got.  Ignore
164   // LLDB_ALL_OPTION_SETS...
165 
166   uint32_t num_option_sets = 0;
167 
168   for (const auto &def : opt_defs) {
169     uint32_t this_usage_mask = def.usage_mask;
170     if (this_usage_mask == LLDB_OPT_SET_ALL) {
171       if (num_option_sets == 0)
172         num_option_sets = 1;
173     } else {
174       for (uint32_t j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++) {
175         if (this_usage_mask & (1 << j)) {
176           if (num_option_sets <= j)
177             num_option_sets = j + 1;
178         }
179       }
180     }
181   }
182 
183   if (num_option_sets > 0) {
184     m_required_options.resize(num_option_sets);
185     m_optional_options.resize(num_option_sets);
186 
187     for (const auto &def : opt_defs) {
188       for (uint32_t j = 0; j < num_option_sets; j++) {
189         if (def.usage_mask & 1 << j) {
190           if (def.required)
191             m_required_options[j].insert(def.short_option);
192           else
193             m_optional_options[j].insert(def.short_option);
194         }
195       }
196     }
197   }
198 }
199 
200 uint32_t Options::NumCommandOptions() { return GetDefinitions().size(); }
201 
202 Option *Options::GetLongOptions() {
203   // Check to see if this has already been done.
204   if (m_getopt_table.empty()) {
205     auto defs = GetDefinitions();
206     if (defs.empty())
207       return nullptr;
208 
209     std::map<int, uint32_t> option_seen;
210 
211     m_getopt_table.resize(defs.size() + 1);
212     for (size_t i = 0; i < defs.size(); ++i) {
213       const int short_opt = defs[i].short_option;
214 
215       m_getopt_table[i].definition = &defs[i];
216       m_getopt_table[i].flag = nullptr;
217       m_getopt_table[i].val = short_opt;
218 
219       if (option_seen.find(short_opt) == option_seen.end()) {
220         option_seen[short_opt] = i;
221       } else if (short_opt) {
222         m_getopt_table[i].val = 0;
223         std::map<int, uint32_t>::const_iterator pos =
224             option_seen.find(short_opt);
225         StreamString strm;
226         if (defs[i].HasShortOption())
227           Debugger::ReportError(
228               llvm::formatv(
229                   "option[{0}] --{1} has a short option -{2} that "
230                   "conflicts with option[{3}] --{4}, short option won't "
231                   "be used for --{5}",
232                   i, defs[i].long_option, short_opt, pos->second,
233                   m_getopt_table[pos->second].definition->long_option,
234                   defs[i].long_option)
235                   .str());
236         else
237           Debugger::ReportError(
238               llvm::formatv(
239                   "option[{0}] --{1} has a short option {2:x} that "
240                   "conflicts with option[{3}] --{4}, short option won't "
241                   "be used for --{5}",
242                   (int)i, defs[i].long_option, short_opt, pos->second,
243                   m_getopt_table[pos->second].definition->long_option,
244                   defs[i].long_option)
245                   .str());
246       }
247     }
248 
249     // getopt_long_only requires a NULL final entry in the table:
250 
251     m_getopt_table.back().definition = nullptr;
252     m_getopt_table.back().flag = nullptr;
253     m_getopt_table.back().val = 0;
254   }
255 
256   if (m_getopt_table.empty())
257     return nullptr;
258 
259   return &m_getopt_table.front();
260 }
261 
262 // This function takes INDENT, which tells how many spaces to output at the
263 // front of each line; SPACES, which is a string containing 80 spaces; and
264 // TEXT, which is the text that is to be output.   It outputs the text, on
265 // multiple lines if necessary, to RESULT, with INDENT spaces at the front of
266 // each line.  It breaks lines on spaces, tabs or newlines, shortening the line
267 // if necessary to not break in the middle of a word.  It assumes that each
268 // output line should contain a maximum of OUTPUT_MAX_COLUMNS characters.
269 
270 void Options::OutputFormattedUsageText(Stream &strm,
271                                        const OptionDefinition &option_def,
272                                        uint32_t output_max_columns) {
273   std::string actual_text;
274   if (option_def.validator) {
275     const char *condition = option_def.validator->ShortConditionString();
276     if (condition) {
277       actual_text = "[";
278       actual_text.append(condition);
279       actual_text.append("] ");
280     }
281   }
282   actual_text.append(option_def.usage_text);
283 
284   // Will it all fit on one line?
285 
286   if (static_cast<uint32_t>(actual_text.length() + strm.GetIndentLevel()) <
287       output_max_columns) {
288     // Output it as a single line.
289     strm.Indent(actual_text);
290     strm.EOL();
291   } else {
292     // We need to break it up into multiple lines.
293 
294     int text_width = output_max_columns - strm.GetIndentLevel() - 1;
295     int start = 0;
296     int end = start;
297     int final_end = actual_text.length();
298     int sub_len;
299 
300     while (end < final_end) {
301       // Don't start the 'text' on a space, since we're already outputting the
302       // indentation.
303       while ((start < final_end) && (actual_text[start] == ' '))
304         start++;
305 
306       end = start + text_width;
307       if (end > final_end)
308         end = final_end;
309       else {
310         // If we're not at the end of the text, make sure we break the line on
311         // white space.
312         while (end > start && actual_text[end] != ' ' &&
313                actual_text[end] != '\t' && actual_text[end] != '\n')
314           end--;
315       }
316 
317       sub_len = end - start;
318       if (start != 0)
319         strm.EOL();
320       strm.Indent();
321       assert(start < final_end);
322       assert(start + sub_len <= final_end);
323       strm.Write(actual_text.c_str() + start, sub_len);
324       start = end + 1;
325     }
326     strm.EOL();
327   }
328 }
329 
330 bool Options::SupportsLongOption(const char *long_option) {
331   if (!long_option || !long_option[0])
332     return false;
333 
334   auto opt_defs = GetDefinitions();
335   if (opt_defs.empty())
336     return false;
337 
338   const char *long_option_name = long_option;
339   if (long_option[0] == '-' && long_option[1] == '-')
340     long_option_name += 2;
341 
342   for (auto &def : opt_defs) {
343     if (!def.long_option)
344       continue;
345 
346     if (strcmp(def.long_option, long_option_name) == 0)
347       return true;
348   }
349 
350   return false;
351 }
352 
353 enum OptionDisplayType {
354   eDisplayBestOption,
355   eDisplayShortOption,
356   eDisplayLongOption
357 };
358 
359 static bool PrintOption(const OptionDefinition &opt_def,
360                         OptionDisplayType display_type, const char *header,
361                         const char *footer, bool show_optional, Stream &strm) {
362   if (display_type == eDisplayShortOption && !opt_def.HasShortOption())
363     return false;
364 
365   if (header && header[0])
366     strm.PutCString(header);
367 
368   if (show_optional && !opt_def.required)
369     strm.PutChar('[');
370   const bool show_short_option =
371       opt_def.HasShortOption() && display_type != eDisplayLongOption;
372   if (show_short_option)
373     strm.Printf("-%c", opt_def.short_option);
374   else
375     strm.Printf("--%s", opt_def.long_option);
376   switch (opt_def.option_has_arg) {
377   case OptionParser::eNoArgument:
378     break;
379   case OptionParser::eRequiredArgument:
380     strm.Printf(" <%s>", CommandObject::GetArgumentName(opt_def.argument_type));
381     break;
382 
383   case OptionParser::eOptionalArgument:
384     strm.Printf("%s[<%s>]", show_short_option ? "" : "=",
385                 CommandObject::GetArgumentName(opt_def.argument_type));
386     break;
387   }
388   if (show_optional && !opt_def.required)
389     strm.PutChar(']');
390   if (footer && footer[0])
391     strm.PutCString(footer);
392   return true;
393 }
394 
395 void Options::GenerateOptionUsage(Stream &strm, CommandObject &cmd,
396                                   uint32_t screen_width) {
397   auto opt_defs = GetDefinitions();
398   const uint32_t save_indent_level = strm.GetIndentLevel();
399   llvm::StringRef name = cmd.GetCommandName();
400   StreamString arguments_str;
401   cmd.GetFormattedCommandArguments(arguments_str);
402 
403   const uint32_t num_options = NumCommandOptions();
404   if (num_options == 0)
405     return;
406 
407   const bool only_print_args = cmd.IsDashDashCommand();
408   if (!only_print_args)
409     strm.PutCString("\nCommand Options Usage:\n");
410 
411   strm.IndentMore(2);
412 
413   // First, show each usage level set of options, e.g. <cmd> [options-for-
414   // level-0]
415   //                                                   <cmd>
416   //                                                   [options-for-level-1]
417   //                                                   etc.
418 
419   if (!only_print_args) {
420     uint32_t num_option_sets = GetRequiredOptions().size();
421     for (uint32_t opt_set = 0; opt_set < num_option_sets; ++opt_set) {
422       if (opt_set > 0)
423         strm.Printf("\n");
424       strm.Indent(name);
425 
426       // Different option sets may require different args.
427       StreamString args_str;
428       uint32_t opt_set_mask = 1 << opt_set;
429       cmd.GetFormattedCommandArguments(args_str, opt_set_mask);
430 
431       // First go through and print all options that take no arguments as a
432       // single string. If a command has "-a" "-b" and "-c", this will show up
433       // as [-abc]
434 
435       // We use a set here so that they will be sorted.
436       std::set<int> required_options;
437       std::set<int> optional_options;
438 
439       for (auto &def : opt_defs) {
440         if (def.usage_mask & opt_set_mask && def.HasShortOption() &&
441             def.option_has_arg == OptionParser::eNoArgument) {
442           if (def.required) {
443             required_options.insert(def.short_option);
444           } else {
445             optional_options.insert(def.short_option);
446           }
447         }
448       }
449 
450       if (!required_options.empty()) {
451         strm.PutCString(" -");
452         for (int short_option : required_options)
453           strm.PutChar(short_option);
454       }
455 
456       if (!optional_options.empty()) {
457         strm.PutCString(" [-");
458         for (int short_option : optional_options)
459           strm.PutChar(short_option);
460         strm.PutChar(']');
461       }
462 
463       // First go through and print the required options (list them up front).
464       for (auto &def : opt_defs) {
465         if (def.usage_mask & opt_set_mask && def.HasShortOption() &&
466             def.required && def.option_has_arg != OptionParser::eNoArgument)
467           PrintOption(def, eDisplayBestOption, " ", nullptr, true, strm);
468       }
469 
470       // Now go through again, and this time only print the optional options.
471       for (auto &def : opt_defs) {
472         if (def.usage_mask & opt_set_mask && !def.required &&
473             def.option_has_arg != OptionParser::eNoArgument)
474           PrintOption(def, eDisplayBestOption, " ", nullptr, true, strm);
475       }
476 
477       if (args_str.GetSize() > 0) {
478         if (cmd.WantsRawCommandString())
479           strm.Printf(" --");
480         strm << " " << args_str.GetString();
481       }
482     }
483   }
484 
485   if ((only_print_args || cmd.WantsRawCommandString()) &&
486       arguments_str.GetSize() > 0) {
487     if (!only_print_args)
488       strm.PutChar('\n');
489     strm.Indent(name);
490     strm << " " << arguments_str.GetString();
491   }
492 
493   if (!only_print_args) {
494     strm.Printf("\n\n");
495 
496     // Now print out all the detailed information about the various options:
497     // long form, short form and help text:
498     //   -short <argument> ( --long_name <argument> )
499     //   help text
500 
501     strm.IndentMore(5);
502 
503     // Put the command options in a sorted container, so we can output
504     // them alphabetically by short_option.
505     std::multimap<int, uint32_t> options_ordered;
506     for (auto def : llvm::enumerate(opt_defs))
507       options_ordered.insert(
508           std::make_pair(def.value().short_option, def.index()));
509 
510     // Go through each option, find the table entry and write out the detailed
511     // help information for that option.
512 
513     bool first_option_printed = false;
514 
515     for (auto pos : options_ordered) {
516       // Put a newline separation between arguments
517       if (first_option_printed)
518         strm.EOL();
519       else
520         first_option_printed = true;
521 
522       OptionDefinition opt_def = opt_defs[pos.second];
523 
524       strm.Indent();
525       if (opt_def.short_option && opt_def.HasShortOption()) {
526         PrintOption(opt_def, eDisplayShortOption, nullptr, nullptr, false,
527                     strm);
528         PrintOption(opt_def, eDisplayLongOption, " ( ", " )", false, strm);
529       } else {
530         // Short option is not printable, just print long option
531         PrintOption(opt_def, eDisplayLongOption, nullptr, nullptr, false, strm);
532       }
533       strm.EOL();
534 
535       strm.IndentMore(5);
536 
537       if (opt_def.usage_text)
538         OutputFormattedUsageText(strm, opt_def, screen_width);
539       if (!opt_def.enum_values.empty()) {
540         strm.Indent();
541         strm.Printf("Values: ");
542         bool is_first = true;
543         for (const auto &enum_value : opt_def.enum_values) {
544           if (is_first) {
545             strm.Printf("%s", enum_value.string_value);
546             is_first = false;
547           }
548           else
549             strm.Printf(" | %s", enum_value.string_value);
550         }
551         strm.EOL();
552       }
553       strm.IndentLess(5);
554     }
555   }
556 
557   // Restore the indent level
558   strm.SetIndentLevel(save_indent_level);
559 }
560 
561 // This function is called when we have been given a potentially incomplete set
562 // of options, such as when an alias has been defined (more options might be
563 // added at at the time the alias is invoked).  We need to verify that the
564 // options in the set m_seen_options are all part of a set that may be used
565 // together, but m_seen_options may be missing some of the "required" options.
566 
567 bool Options::VerifyPartialOptions(CommandReturnObject &result) {
568   bool options_are_valid = false;
569 
570   int num_levels = GetRequiredOptions().size();
571   if (num_levels) {
572     for (int i = 0; i < num_levels && !options_are_valid; ++i) {
573       // In this case we are treating all options as optional rather than
574       // required. Therefore a set of options is correct if m_seen_options is a
575       // subset of the union of m_required_options and m_optional_options.
576       OptionSet union_set;
577       OptionsSetUnion(GetRequiredOptions()[i], GetOptionalOptions()[i],
578                       union_set);
579       if (IsASubset(m_seen_options, union_set))
580         options_are_valid = true;
581     }
582   }
583 
584   return options_are_valid;
585 }
586 
587 bool Options::HandleOptionCompletion(CompletionRequest &request,
588                                      OptionElementVector &opt_element_vector,
589                                      CommandInterpreter &interpreter) {
590   // For now we just scan the completions to see if the cursor position is in
591   // an option or its argument.  Otherwise we'll call HandleArgumentCompletion.
592   // In the future we can use completion to validate options as well if we
593   // want.
594 
595   auto opt_defs = GetDefinitions();
596 
597   llvm::StringRef cur_opt_str = request.GetCursorArgumentPrefix();
598 
599   for (size_t i = 0; i < opt_element_vector.size(); i++) {
600     size_t opt_pos = static_cast<size_t>(opt_element_vector[i].opt_pos);
601     size_t opt_arg_pos = static_cast<size_t>(opt_element_vector[i].opt_arg_pos);
602     int opt_defs_index = opt_element_vector[i].opt_defs_index;
603     if (opt_pos == request.GetCursorIndex()) {
604       // We're completing the option itself.
605 
606       if (opt_defs_index == OptionArgElement::eBareDash) {
607         // We're completing a bare dash.  That means all options are open.
608         // FIXME: We should scan the other options provided and only complete
609         // options
610         // within the option group they belong to.
611         std::string opt_str = "-a";
612 
613         for (auto &def : opt_defs) {
614           if (!def.short_option)
615             continue;
616           opt_str[1] = def.short_option;
617           request.AddCompletion(opt_str, def.usage_text);
618         }
619 
620         return true;
621       } else if (opt_defs_index == OptionArgElement::eBareDoubleDash) {
622         std::string full_name("--");
623         for (auto &def : opt_defs) {
624           if (!def.short_option)
625             continue;
626 
627           full_name.erase(full_name.begin() + 2, full_name.end());
628           full_name.append(def.long_option);
629           request.AddCompletion(full_name, def.usage_text);
630         }
631         return true;
632       } else if (opt_defs_index != OptionArgElement::eUnrecognizedArg) {
633         // We recognized it, if it an incomplete long option, complete it
634         // anyway (getopt_long_only is happy with shortest unique string, but
635         // it's still a nice thing to do.)  Otherwise return The string so the
636         // upper level code will know this is a full match and add the " ".
637         const OptionDefinition &opt = opt_defs[opt_defs_index];
638         llvm::StringRef long_option = opt.long_option;
639         if (cur_opt_str.startswith("--") && cur_opt_str != long_option) {
640           request.AddCompletion("--" + long_option.str(), opt.usage_text);
641           return true;
642         } else
643           request.AddCompletion(request.GetCursorArgumentPrefix());
644         return true;
645       } else {
646         // FIXME - not handling wrong options yet:
647         // Check to see if they are writing a long option & complete it.
648         // I think we will only get in here if the long option table has two
649         // elements
650         // that are not unique up to this point.  getopt_long_only does
651         // shortest unique match for long options already.
652         if (cur_opt_str.consume_front("--")) {
653           for (auto &def : opt_defs) {
654             llvm::StringRef long_option(def.long_option);
655             if (long_option.startswith(cur_opt_str))
656               request.AddCompletion("--" + long_option.str(), def.usage_text);
657           }
658         }
659         return true;
660       }
661 
662     } else if (opt_arg_pos == request.GetCursorIndex()) {
663       // Okay the cursor is on the completion of an argument. See if it has a
664       // completion, otherwise return no matches.
665       if (opt_defs_index != -1) {
666         HandleOptionArgumentCompletion(request, opt_element_vector, i,
667                                        interpreter);
668         return true;
669       } else {
670         // No completion callback means no completions...
671         return true;
672       }
673 
674     } else {
675       // Not the last element, keep going.
676       continue;
677     }
678   }
679   return false;
680 }
681 
682 void Options::HandleOptionArgumentCompletion(
683     CompletionRequest &request, OptionElementVector &opt_element_vector,
684     int opt_element_index, CommandInterpreter &interpreter) {
685   auto opt_defs = GetDefinitions();
686   std::unique_ptr<SearchFilter> filter_up;
687 
688   int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index;
689 
690   // See if this is an enumeration type option, and if so complete it here:
691 
692   const auto &enum_values = opt_defs[opt_defs_index].enum_values;
693   if (!enum_values.empty())
694     for (const auto &enum_value : enum_values)
695       request.TryCompleteCurrentArg(enum_value.string_value);
696 
697   // If this is a source file or symbol type completion, and  there is a -shlib
698   // option somewhere in the supplied arguments, then make a search filter for
699   // that shared library.
700   // FIXME: Do we want to also have an "OptionType" so we don't have to match
701   // string names?
702 
703   uint32_t completion_mask = opt_defs[opt_defs_index].completion_type;
704 
705   if (completion_mask == 0) {
706     lldb::CommandArgumentType option_arg_type =
707         opt_defs[opt_defs_index].argument_type;
708     if (option_arg_type != eArgTypeNone) {
709       const CommandObject::ArgumentTableEntry *arg_entry =
710           CommandObject::FindArgumentDataByType(
711               opt_defs[opt_defs_index].argument_type);
712       if (arg_entry)
713         completion_mask = arg_entry->completion_type;
714     }
715   }
716 
717   if (completion_mask & CommandCompletions::eSourceFileCompletion ||
718       completion_mask & CommandCompletions::eSymbolCompletion) {
719     for (size_t i = 0; i < opt_element_vector.size(); i++) {
720       int cur_defs_index = opt_element_vector[i].opt_defs_index;
721 
722       // trying to use <0 indices will definitely cause problems
723       if (cur_defs_index == OptionArgElement::eUnrecognizedArg ||
724           cur_defs_index == OptionArgElement::eBareDash ||
725           cur_defs_index == OptionArgElement::eBareDoubleDash)
726         continue;
727 
728       int cur_arg_pos = opt_element_vector[i].opt_arg_pos;
729       const char *cur_opt_name = opt_defs[cur_defs_index].long_option;
730 
731       // If this is the "shlib" option and there was an argument provided,
732       // restrict it to that shared library.
733       if (cur_opt_name && strcmp(cur_opt_name, "shlib") == 0 &&
734           cur_arg_pos != -1) {
735         const char *module_name =
736             request.GetParsedLine().GetArgumentAtIndex(cur_arg_pos);
737         if (module_name) {
738           FileSpec module_spec(module_name);
739           lldb::TargetSP target_sp =
740               interpreter.GetDebugger().GetSelectedTarget();
741           // Search filters require a target...
742           if (target_sp)
743             filter_up =
744                 std::make_unique<SearchFilterByModule>(target_sp, module_spec);
745         }
746         break;
747       }
748     }
749   }
750 
751   CommandCompletions::InvokeCommonCompletionCallbacks(
752       interpreter, completion_mask, request, filter_up.get());
753 }
754 
755 void OptionGroupOptions::Append(OptionGroup *group) {
756   auto group_option_defs = group->GetDefinitions();
757   for (uint32_t i = 0; i < group_option_defs.size(); ++i) {
758     m_option_infos.push_back(OptionInfo(group, i));
759     m_option_defs.push_back(group_option_defs[i]);
760   }
761 }
762 
763 const OptionGroup *OptionGroupOptions::GetGroupWithOption(char short_opt) {
764   for (uint32_t i = 0; i < m_option_defs.size(); i++) {
765     OptionDefinition opt_def = m_option_defs[i];
766     if (opt_def.short_option == short_opt)
767       return m_option_infos[i].option_group;
768   }
769   return nullptr;
770 }
771 
772 void OptionGroupOptions::Append(OptionGroup *group, uint32_t src_mask,
773                                 uint32_t dst_mask) {
774   auto group_option_defs = group->GetDefinitions();
775   for (uint32_t i = 0; i < group_option_defs.size(); ++i) {
776     if (group_option_defs[i].usage_mask & src_mask) {
777       m_option_infos.push_back(OptionInfo(group, i));
778       m_option_defs.push_back(group_option_defs[i]);
779       m_option_defs.back().usage_mask = dst_mask;
780     }
781   }
782 }
783 
784 void OptionGroupOptions::Finalize() {
785   m_did_finalize = true;
786 }
787 
788 Status OptionGroupOptions::SetOptionValue(uint32_t option_idx,
789                                           llvm::StringRef option_value,
790                                           ExecutionContext *execution_context) {
791   // After calling OptionGroupOptions::Append(...), you must finalize the
792   // groups by calling OptionGroupOptions::Finlize()
793   assert(m_did_finalize);
794   Status error;
795   if (option_idx < m_option_infos.size()) {
796     error = m_option_infos[option_idx].option_group->SetOptionValue(
797         m_option_infos[option_idx].option_index, option_value,
798         execution_context);
799 
800   } else {
801     error.SetErrorString("invalid option index"); // Shouldn't happen...
802   }
803   return error;
804 }
805 
806 void OptionGroupOptions::OptionParsingStarting(
807     ExecutionContext *execution_context) {
808   std::set<OptionGroup *> group_set;
809   OptionInfos::iterator pos, end = m_option_infos.end();
810   for (pos = m_option_infos.begin(); pos != end; ++pos) {
811     OptionGroup *group = pos->option_group;
812     if (group_set.find(group) == group_set.end()) {
813       group->OptionParsingStarting(execution_context);
814       group_set.insert(group);
815     }
816   }
817 }
818 Status
819 OptionGroupOptions::OptionParsingFinished(ExecutionContext *execution_context) {
820   std::set<OptionGroup *> group_set;
821   Status error;
822   OptionInfos::iterator pos, end = m_option_infos.end();
823   for (pos = m_option_infos.begin(); pos != end; ++pos) {
824     OptionGroup *group = pos->option_group;
825     if (group_set.find(group) == group_set.end()) {
826       error = group->OptionParsingFinished(execution_context);
827       group_set.insert(group);
828       if (error.Fail())
829         return error;
830     }
831   }
832   return error;
833 }
834 
835 // OptionParser permutes the arguments while processing them, so we create a
836 // temporary array holding to avoid modification of the input arguments. The
837 // options themselves are never modified, but the API expects a char * anyway,
838 // hence the const_cast.
839 static std::vector<char *> GetArgvForParsing(const Args &args) {
840   std::vector<char *> result;
841   // OptionParser always skips the first argument as it is based on getopt().
842   result.push_back(const_cast<char *>("<FAKE-ARG0>"));
843   for (const Args::ArgEntry &entry : args)
844     result.push_back(const_cast<char *>(entry.c_str()));
845   result.push_back(nullptr);
846   return result;
847 }
848 
849 // Given a permuted argument, find it's position in the original Args vector.
850 static Args::const_iterator FindOriginalIter(const char *arg,
851                                              const Args &original) {
852   return llvm::find_if(
853       original, [arg](const Args::ArgEntry &D) { return D.c_str() == arg; });
854 }
855 
856 // Given a permuted argument, find it's index in the original Args vector.
857 static size_t FindOriginalIndex(const char *arg, const Args &original) {
858   return std::distance(original.begin(), FindOriginalIter(arg, original));
859 }
860 
861 // Construct a new Args object, consisting of the entries from the original
862 // arguments, but in the permuted order.
863 static Args ReconstituteArgsAfterParsing(llvm::ArrayRef<char *> parsed,
864                                          const Args &original) {
865   Args result;
866   for (const char *arg : parsed) {
867     auto pos = FindOriginalIter(arg, original);
868     assert(pos != original.end());
869     result.AppendArgument(pos->ref(), pos->GetQuoteChar());
870   }
871   return result;
872 }
873 
874 static size_t FindArgumentIndexForOption(const Args &args,
875                                          const Option &long_option) {
876   std::string short_opt = llvm::formatv("-{0}", char(long_option.val)).str();
877   std::string long_opt =
878       std::string(llvm::formatv("--{0}", long_option.definition->long_option));
879   for (const auto &entry : llvm::enumerate(args)) {
880     if (entry.value().ref().startswith(short_opt) ||
881         entry.value().ref().startswith(long_opt))
882       return entry.index();
883   }
884 
885   return size_t(-1);
886 }
887 
888 static std::string BuildShortOptions(const Option *long_options) {
889   std::string storage;
890   llvm::raw_string_ostream sstr(storage);
891 
892   // Leading : tells getopt to return a : for a missing option argument AND to
893   // suppress error messages.
894   sstr << ":";
895 
896   for (size_t i = 0; long_options[i].definition != nullptr; ++i) {
897     if (long_options[i].flag == nullptr) {
898       sstr << (char)long_options[i].val;
899       switch (long_options[i].definition->option_has_arg) {
900       default:
901       case OptionParser::eNoArgument:
902         break;
903       case OptionParser::eRequiredArgument:
904         sstr << ":";
905         break;
906       case OptionParser::eOptionalArgument:
907         sstr << "::";
908         break;
909       }
910     }
911   }
912   return std::move(sstr.str());
913 }
914 
915 llvm::Expected<Args> Options::ParseAlias(const Args &args,
916                                          OptionArgVector *option_arg_vector,
917                                          std::string &input_line) {
918   Option *long_options = GetLongOptions();
919 
920   if (long_options == nullptr) {
921     return llvm::make_error<llvm::StringError>("Invalid long options",
922                                                llvm::inconvertibleErrorCode());
923   }
924 
925   std::string short_options = BuildShortOptions(long_options);
926 
927   Args args_copy = args;
928   std::vector<char *> argv = GetArgvForParsing(args);
929 
930   std::unique_lock<std::mutex> lock;
931   OptionParser::Prepare(lock);
932   int val;
933   while (true) {
934     int long_options_index = -1;
935     val = OptionParser::Parse(argv, short_options, long_options,
936                               &long_options_index);
937 
938     if (val == ':') {
939       return llvm::createStringError(llvm::inconvertibleErrorCode(),
940                                      "last option requires an argument");
941     }
942 
943     if (val == -1)
944       break;
945 
946     if (val == '?') {
947       return llvm::make_error<llvm::StringError>(
948           "Unknown or ambiguous option", llvm::inconvertibleErrorCode());
949     }
950 
951     if (val == 0)
952       continue;
953 
954     OptionSeen(val);
955 
956     // Look up the long option index
957     if (long_options_index == -1) {
958       for (int j = 0; long_options[j].definition || long_options[j].flag ||
959                       long_options[j].val;
960            ++j) {
961         if (long_options[j].val == val) {
962           long_options_index = j;
963           break;
964         }
965       }
966     }
967 
968     // See if the option takes an argument, and see if one was supplied.
969     if (long_options_index == -1) {
970       return llvm::make_error<llvm::StringError>(
971           llvm::formatv("Invalid option with value '{0}'.", char(val)).str(),
972           llvm::inconvertibleErrorCode());
973     }
974 
975     StreamString option_str;
976     option_str.Printf("-%c", val);
977     const OptionDefinition *def = long_options[long_options_index].definition;
978     int has_arg =
979         (def == nullptr) ? OptionParser::eNoArgument : def->option_has_arg;
980 
981     const char *option_arg = nullptr;
982     switch (has_arg) {
983     case OptionParser::eRequiredArgument:
984       if (OptionParser::GetOptionArgument() == nullptr) {
985         return llvm::make_error<llvm::StringError>(
986             llvm::formatv("Option '{0}' is missing argument specifier.",
987                           option_str.GetString())
988                 .str(),
989             llvm::inconvertibleErrorCode());
990       }
991       [[fallthrough]];
992     case OptionParser::eOptionalArgument:
993       option_arg = OptionParser::GetOptionArgument();
994       [[fallthrough]];
995     case OptionParser::eNoArgument:
996       break;
997     default:
998       return llvm::make_error<llvm::StringError>(
999           llvm::formatv("error with options table; invalid value in has_arg "
1000                         "field for option '{0}'.",
1001                         char(val))
1002               .str(),
1003           llvm::inconvertibleErrorCode());
1004     }
1005     // Find option in the argument list; also see if it was supposed to take an
1006     // argument and if one was supplied.  Remove option (and argument, if
1007     // given) from the argument list.  Also remove them from the
1008     // raw_input_string, if one was passed in.
1009     // Note: We also need to preserve any option argument values that were
1010     // surrounded by backticks, as we lose track of them in the
1011     // option_args_vector.
1012     size_t idx =
1013         FindArgumentIndexForOption(args_copy, long_options[long_options_index]);
1014     std::string option_to_insert;
1015     if (option_arg) {
1016       if (idx != size_t(-1) && has_arg) {
1017         bool arg_has_backtick = args_copy[idx + 1].GetQuoteChar() == '`';
1018         if (arg_has_backtick)
1019           option_to_insert = "`";
1020         option_to_insert += option_arg;
1021         if (arg_has_backtick)
1022           option_to_insert += "`";
1023       } else
1024         option_to_insert = option_arg;
1025     } else
1026       option_to_insert = CommandInterpreter::g_no_argument;
1027 
1028     option_arg_vector->emplace_back(std::string(option_str.GetString()),
1029                                     has_arg, option_to_insert);
1030 
1031     if (idx == size_t(-1))
1032       continue;
1033 
1034     if (!input_line.empty()) {
1035       llvm::StringRef tmp_arg = args_copy[idx].ref();
1036       size_t pos = input_line.find(std::string(tmp_arg));
1037       if (pos != std::string::npos)
1038         input_line.erase(pos, tmp_arg.size());
1039     }
1040     args_copy.DeleteArgumentAtIndex(idx);
1041     if ((option_to_insert != CommandInterpreter::g_no_argument) &&
1042         (OptionParser::GetOptionArgument() != nullptr) &&
1043         (idx < args_copy.GetArgumentCount()) &&
1044         (args_copy[idx].ref() == OptionParser::GetOptionArgument())) {
1045       if (input_line.size() > 0) {
1046         size_t pos = input_line.find(option_to_insert);
1047         if (pos != std::string::npos)
1048           input_line.erase(pos, option_to_insert.size());
1049       }
1050       args_copy.DeleteArgumentAtIndex(idx);
1051     }
1052   }
1053 
1054   return std::move(args_copy);
1055 }
1056 
1057 OptionElementVector Options::ParseForCompletion(const Args &args,
1058                                                 uint32_t cursor_index) {
1059   OptionElementVector option_element_vector;
1060   Option *long_options = GetLongOptions();
1061   option_element_vector.clear();
1062 
1063   if (long_options == nullptr)
1064     return option_element_vector;
1065 
1066   std::string short_options = BuildShortOptions(long_options);
1067 
1068   std::unique_lock<std::mutex> lock;
1069   OptionParser::Prepare(lock);
1070   OptionParser::EnableError(false);
1071 
1072   int val;
1073   auto opt_defs = GetDefinitions();
1074 
1075   std::vector<char *> dummy_vec = GetArgvForParsing(args);
1076 
1077   bool failed_once = false;
1078   uint32_t dash_dash_pos = -1;
1079 
1080   while (true) {
1081     bool missing_argument = false;
1082     int long_options_index = -1;
1083 
1084     val = OptionParser::Parse(dummy_vec, short_options, long_options,
1085                               &long_options_index);
1086 
1087     if (val == -1) {
1088       // When we're completing a "--" which is the last option on line,
1089       if (failed_once)
1090         break;
1091 
1092       failed_once = true;
1093 
1094       // If this is a bare  "--" we mark it as such so we can complete it
1095       // successfully later.  Handling the "--" is a little tricky, since that
1096       // may mean end of options or arguments, or the user might want to
1097       // complete options by long name.  I make this work by checking whether
1098       // the cursor is in the "--" argument, and if so I assume we're
1099       // completing the long option, otherwise I let it pass to
1100       // OptionParser::Parse which will terminate the option parsing.  Note, in
1101       // either case we continue parsing the line so we can figure out what
1102       // other options were passed.  This will be useful when we come to
1103       // restricting completions based on what other options we've seen on the
1104       // line.
1105 
1106       if (static_cast<size_t>(OptionParser::GetOptionIndex()) <
1107               dummy_vec.size() &&
1108           (strcmp(dummy_vec[OptionParser::GetOptionIndex() - 1], "--") == 0)) {
1109         dash_dash_pos = FindOriginalIndex(
1110             dummy_vec[OptionParser::GetOptionIndex() - 1], args);
1111         if (dash_dash_pos == cursor_index) {
1112           option_element_vector.push_back(
1113               OptionArgElement(OptionArgElement::eBareDoubleDash, dash_dash_pos,
1114                                OptionArgElement::eBareDoubleDash));
1115           continue;
1116         } else
1117           break;
1118       } else
1119         break;
1120     } else if (val == '?') {
1121       option_element_vector.push_back(OptionArgElement(
1122           OptionArgElement::eUnrecognizedArg,
1123           FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1],
1124                             args),
1125           OptionArgElement::eUnrecognizedArg));
1126       continue;
1127     } else if (val == 0) {
1128       continue;
1129     } else if (val == ':') {
1130       // This is a missing argument.
1131       val = OptionParser::GetOptionErrorCause();
1132       missing_argument = true;
1133     }
1134 
1135     OptionSeen(val);
1136 
1137     // Look up the long option index
1138     if (long_options_index == -1) {
1139       for (int j = 0; long_options[j].definition || long_options[j].flag ||
1140                       long_options[j].val;
1141            ++j) {
1142         if (long_options[j].val == val) {
1143           long_options_index = j;
1144           break;
1145         }
1146       }
1147     }
1148 
1149     // See if the option takes an argument, and see if one was supplied.
1150     if (long_options_index >= 0) {
1151       int opt_defs_index = -1;
1152       for (size_t i = 0; i < opt_defs.size(); i++) {
1153         if (opt_defs[i].short_option != val)
1154           continue;
1155         opt_defs_index = i;
1156         break;
1157       }
1158 
1159       const OptionDefinition *def = long_options[long_options_index].definition;
1160       int has_arg =
1161           (def == nullptr) ? OptionParser::eNoArgument : def->option_has_arg;
1162       switch (has_arg) {
1163       case OptionParser::eNoArgument:
1164         option_element_vector.push_back(OptionArgElement(
1165             opt_defs_index,
1166             FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1],
1167                               args),
1168             0));
1169         break;
1170       case OptionParser::eRequiredArgument:
1171         if (OptionParser::GetOptionArgument() != nullptr) {
1172           int arg_index;
1173           if (missing_argument)
1174             arg_index = -1;
1175           else
1176             arg_index = OptionParser::GetOptionIndex() - 2;
1177 
1178           option_element_vector.push_back(OptionArgElement(
1179               opt_defs_index,
1180               FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 2],
1181                                 args),
1182               arg_index));
1183         } else {
1184           option_element_vector.push_back(OptionArgElement(
1185               opt_defs_index,
1186               FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1],
1187                                 args),
1188               -1));
1189         }
1190         break;
1191       case OptionParser::eOptionalArgument:
1192         if (OptionParser::GetOptionArgument() != nullptr) {
1193           option_element_vector.push_back(OptionArgElement(
1194               opt_defs_index,
1195               FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 2],
1196                                 args),
1197               FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1],
1198                                 args)));
1199         } else {
1200           option_element_vector.push_back(OptionArgElement(
1201               opt_defs_index,
1202               FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 2],
1203                                 args),
1204               FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1],
1205                                 args)));
1206         }
1207         break;
1208       default:
1209         // The options table is messed up.  Here we'll just continue
1210         option_element_vector.push_back(OptionArgElement(
1211             OptionArgElement::eUnrecognizedArg,
1212             FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1],
1213                               args),
1214             OptionArgElement::eUnrecognizedArg));
1215         break;
1216       }
1217     } else {
1218       option_element_vector.push_back(OptionArgElement(
1219           OptionArgElement::eUnrecognizedArg,
1220           FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1],
1221                             args),
1222           OptionArgElement::eUnrecognizedArg));
1223     }
1224   }
1225 
1226   // Finally we have to handle the case where the cursor index points at a
1227   // single "-".  We want to mark that in the option_element_vector, but only
1228   // if it is not after the "--".  But it turns out that OptionParser::Parse
1229   // just ignores an isolated "-".  So we have to look it up by hand here.  We
1230   // only care if it is AT the cursor position. Note, a single quoted dash is
1231   // not the same as a single dash...
1232 
1233   const Args::ArgEntry &cursor = args[cursor_index];
1234   if ((static_cast<int32_t>(dash_dash_pos) == -1 ||
1235        cursor_index < dash_dash_pos) &&
1236       !cursor.IsQuoted() && cursor.ref() == "-") {
1237     option_element_vector.push_back(
1238         OptionArgElement(OptionArgElement::eBareDash, cursor_index,
1239                          OptionArgElement::eBareDash));
1240   }
1241   return option_element_vector;
1242 }
1243 
1244 llvm::Expected<Args> Options::Parse(const Args &args,
1245                                     ExecutionContext *execution_context,
1246                                     lldb::PlatformSP platform_sp,
1247                                     bool require_validation) {
1248   Status error;
1249   Option *long_options = GetLongOptions();
1250   if (long_options == nullptr) {
1251     return llvm::make_error<llvm::StringError>("Invalid long options.",
1252                                                llvm::inconvertibleErrorCode());
1253   }
1254 
1255   std::string short_options = BuildShortOptions(long_options);
1256   std::vector<char *> argv = GetArgvForParsing(args);
1257   std::unique_lock<std::mutex> lock;
1258   OptionParser::Prepare(lock);
1259   int val;
1260   while (true) {
1261     int long_options_index = -1;
1262     val = OptionParser::Parse(argv, short_options, long_options,
1263                               &long_options_index);
1264 
1265     if (val == ':') {
1266       error.SetErrorString("last option requires an argument");
1267       break;
1268     }
1269 
1270     if (val == -1)
1271       break;
1272 
1273     // Did we get an error?
1274     if (val == '?') {
1275       error.SetErrorString("unknown or ambiguous option");
1276       break;
1277     }
1278     // The option auto-set itself
1279     if (val == 0)
1280       continue;
1281 
1282     OptionSeen(val);
1283 
1284     // Lookup the long option index
1285     if (long_options_index == -1) {
1286       for (int i = 0; long_options[i].definition || long_options[i].flag ||
1287                       long_options[i].val;
1288            ++i) {
1289         if (long_options[i].val == val) {
1290           long_options_index = i;
1291           break;
1292         }
1293       }
1294     }
1295     // Call the callback with the option
1296     if (long_options_index >= 0 &&
1297         long_options[long_options_index].definition) {
1298       const OptionDefinition *def = long_options[long_options_index].definition;
1299 
1300       if (!platform_sp) {
1301         // User did not pass in an explicit platform.  Try to grab from the
1302         // execution context.
1303         TargetSP target_sp =
1304             execution_context ? execution_context->GetTargetSP() : TargetSP();
1305         platform_sp = target_sp ? target_sp->GetPlatform() : PlatformSP();
1306       }
1307       OptionValidator *validator = def->validator;
1308 
1309       if (!platform_sp && require_validation) {
1310         // Caller requires validation but we cannot validate as we don't have
1311         // the mandatory platform against which to validate.
1312         return llvm::make_error<llvm::StringError>(
1313             "cannot validate options: no platform available",
1314             llvm::inconvertibleErrorCode());
1315       }
1316 
1317       bool validation_failed = false;
1318       if (platform_sp) {
1319         // Ensure we have an execution context, empty or not.
1320         ExecutionContext dummy_context;
1321         ExecutionContext *exe_ctx_p =
1322             execution_context ? execution_context : &dummy_context;
1323         if (validator && !validator->IsValid(*platform_sp, *exe_ctx_p)) {
1324           validation_failed = true;
1325           error.SetErrorStringWithFormat("Option \"%s\" invalid.  %s",
1326                                          def->long_option,
1327                                          def->validator->LongConditionString());
1328         }
1329       }
1330 
1331       // As long as validation didn't fail, we set the option value.
1332       if (!validation_failed)
1333         error =
1334             SetOptionValue(long_options_index,
1335                            (def->option_has_arg == OptionParser::eNoArgument)
1336                                ? nullptr
1337                                : OptionParser::GetOptionArgument(),
1338                            execution_context);
1339       // If the Option setting returned an error, we should stop parsing
1340       // and return the error.
1341       if (error.Fail())
1342         break;
1343     } else {
1344       error.SetErrorStringWithFormat("invalid option with value '%i'", val);
1345     }
1346   }
1347 
1348   if (error.Fail())
1349     return error.ToError();
1350 
1351   argv.pop_back();
1352   argv.erase(argv.begin(), argv.begin() + OptionParser::GetOptionIndex());
1353   return ReconstituteArgsAfterParsing(argv, args);
1354 }
1355