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
25using namespace lldb;
26using namespace lldb_private;
27
28// Options
29Options::Options() { BuildValidOptionSets(); }
30
31Options::~Options() = default;
32
33void Options::NotifyOptionParsingStarting(ExecutionContext *execution_context) {
34  m_seen_options.clear();
35  // Let the subclass reset its option values
36  OptionParsingStarting(execution_context);
37}
38
39Status
40Options::NotifyOptionParsingFinished(ExecutionContext *execution_context) {
41  return OptionParsingFinished(execution_context);
42}
43
44void 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
48bool 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
68size_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
88void 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
106bool 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
149void 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
200uint32_t Options::NumCommandOptions() { return GetDefinitions().size(); }
201
202Option *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
270void 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
330bool 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
353enum OptionDisplayType {
354  eDisplayBestOption,
355  eDisplayShortOption,
356  eDisplayLongOption
357};
358
359static 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
395void 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
567bool 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
587bool 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.starts_with("--") && 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.starts_with(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
682void 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 & lldb::eSourceFileCompletion ||
718      completion_mask & lldb::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  lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
752      interpreter, completion_mask, request, filter_up.get());
753}
754
755void 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
763const 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
772void 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
784void OptionGroupOptions::Append(
785    OptionGroup *group, llvm::ArrayRef<llvm::StringRef> exclude_long_options) {
786  auto group_option_defs = group->GetDefinitions();
787  for (uint32_t i = 0; i < group_option_defs.size(); ++i) {
788    const auto &definition = group_option_defs[i];
789    if (llvm::is_contained(exclude_long_options, definition.long_option))
790      continue;
791
792    m_option_infos.push_back(OptionInfo(group, i));
793    m_option_defs.push_back(definition);
794  }
795}
796
797void OptionGroupOptions::Finalize() {
798  m_did_finalize = true;
799}
800
801Status OptionGroupOptions::SetOptionValue(uint32_t option_idx,
802                                          llvm::StringRef option_value,
803                                          ExecutionContext *execution_context) {
804  // After calling OptionGroupOptions::Append(...), you must finalize the
805  // groups by calling OptionGroupOptions::Finlize()
806  assert(m_did_finalize);
807  Status error;
808  if (option_idx < m_option_infos.size()) {
809    error = m_option_infos[option_idx].option_group->SetOptionValue(
810        m_option_infos[option_idx].option_index, option_value,
811        execution_context);
812
813  } else {
814    error.SetErrorString("invalid option index"); // Shouldn't happen...
815  }
816  return error;
817}
818
819void OptionGroupOptions::OptionParsingStarting(
820    ExecutionContext *execution_context) {
821  std::set<OptionGroup *> group_set;
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      group->OptionParsingStarting(execution_context);
827      group_set.insert(group);
828    }
829  }
830}
831Status
832OptionGroupOptions::OptionParsingFinished(ExecutionContext *execution_context) {
833  std::set<OptionGroup *> group_set;
834  Status error;
835  OptionInfos::iterator pos, end = m_option_infos.end();
836  for (pos = m_option_infos.begin(); pos != end; ++pos) {
837    OptionGroup *group = pos->option_group;
838    if (group_set.find(group) == group_set.end()) {
839      error = group->OptionParsingFinished(execution_context);
840      group_set.insert(group);
841      if (error.Fail())
842        return error;
843    }
844  }
845  return error;
846}
847
848// OptionParser permutes the arguments while processing them, so we create a
849// temporary array holding to avoid modification of the input arguments. The
850// options themselves are never modified, but the API expects a char * anyway,
851// hence the const_cast.
852static std::vector<char *> GetArgvForParsing(const Args &args) {
853  std::vector<char *> result;
854  // OptionParser always skips the first argument as it is based on getopt().
855  result.push_back(const_cast<char *>("<FAKE-ARG0>"));
856  for (const Args::ArgEntry &entry : args)
857    result.push_back(const_cast<char *>(entry.c_str()));
858  result.push_back(nullptr);
859  return result;
860}
861
862// Given a permuted argument, find it's position in the original Args vector.
863static Args::const_iterator FindOriginalIter(const char *arg,
864                                             const Args &original) {
865  return llvm::find_if(
866      original, [arg](const Args::ArgEntry &D) { return D.c_str() == arg; });
867}
868
869// Given a permuted argument, find it's index in the original Args vector.
870static size_t FindOriginalIndex(const char *arg, const Args &original) {
871  return std::distance(original.begin(), FindOriginalIter(arg, original));
872}
873
874// Construct a new Args object, consisting of the entries from the original
875// arguments, but in the permuted order.
876static Args ReconstituteArgsAfterParsing(llvm::ArrayRef<char *> parsed,
877                                         const Args &original) {
878  Args result;
879  for (const char *arg : parsed) {
880    auto pos = FindOriginalIter(arg, original);
881    assert(pos != original.end());
882    result.AppendArgument(pos->ref(), pos->GetQuoteChar());
883  }
884  return result;
885}
886
887static size_t FindArgumentIndexForOption(const Args &args,
888                                         const Option &long_option) {
889  std::string short_opt = llvm::formatv("-{0}", char(long_option.val)).str();
890  std::string long_opt =
891      std::string(llvm::formatv("--{0}", long_option.definition->long_option));
892  for (const auto &entry : llvm::enumerate(args)) {
893    if (entry.value().ref().starts_with(short_opt) ||
894        entry.value().ref().starts_with(long_opt))
895      return entry.index();
896  }
897
898  return size_t(-1);
899}
900
901static std::string BuildShortOptions(const Option *long_options) {
902  std::string storage;
903  llvm::raw_string_ostream sstr(storage);
904
905  // Leading : tells getopt to return a : for a missing option argument AND to
906  // suppress error messages.
907  sstr << ":";
908
909  for (size_t i = 0; long_options[i].definition != nullptr; ++i) {
910    if (long_options[i].flag == nullptr) {
911      sstr << (char)long_options[i].val;
912      switch (long_options[i].definition->option_has_arg) {
913      default:
914      case OptionParser::eNoArgument:
915        break;
916      case OptionParser::eRequiredArgument:
917        sstr << ":";
918        break;
919      case OptionParser::eOptionalArgument:
920        sstr << "::";
921        break;
922      }
923    }
924  }
925  return std::move(sstr.str());
926}
927
928llvm::Expected<Args> Options::ParseAlias(const Args &args,
929                                         OptionArgVector *option_arg_vector,
930                                         std::string &input_line) {
931  Option *long_options = GetLongOptions();
932
933  if (long_options == nullptr) {
934    return llvm::make_error<llvm::StringError>("Invalid long options",
935                                               llvm::inconvertibleErrorCode());
936  }
937
938  std::string short_options = BuildShortOptions(long_options);
939
940  Args args_copy = args;
941  std::vector<char *> argv = GetArgvForParsing(args);
942
943  std::unique_lock<std::mutex> lock;
944  OptionParser::Prepare(lock);
945  int val;
946  while (true) {
947    int long_options_index = -1;
948    val = OptionParser::Parse(argv, short_options, long_options,
949                              &long_options_index);
950
951    if (val == ':') {
952      return llvm::createStringError(llvm::inconvertibleErrorCode(),
953                                     "last option requires an argument");
954    }
955
956    if (val == -1)
957      break;
958
959    if (val == '?') {
960      return llvm::make_error<llvm::StringError>(
961          "Unknown or ambiguous option", llvm::inconvertibleErrorCode());
962    }
963
964    if (val == 0)
965      continue;
966
967    OptionSeen(val);
968
969    // Look up the long option index
970    if (long_options_index == -1) {
971      for (int j = 0; long_options[j].definition || long_options[j].flag ||
972                      long_options[j].val;
973           ++j) {
974        if (long_options[j].val == val) {
975          long_options_index = j;
976          break;
977        }
978      }
979    }
980
981    // See if the option takes an argument, and see if one was supplied.
982    if (long_options_index == -1) {
983      return llvm::make_error<llvm::StringError>(
984          llvm::formatv("Invalid option with value '{0}'.", char(val)).str(),
985          llvm::inconvertibleErrorCode());
986    }
987
988    StreamString option_str;
989    option_str.Printf("-%c", val);
990    const OptionDefinition *def = long_options[long_options_index].definition;
991    int has_arg =
992        (def == nullptr) ? OptionParser::eNoArgument : def->option_has_arg;
993
994    const char *option_arg = nullptr;
995    switch (has_arg) {
996    case OptionParser::eRequiredArgument:
997      if (OptionParser::GetOptionArgument() == nullptr) {
998        return llvm::make_error<llvm::StringError>(
999            llvm::formatv("Option '{0}' is missing argument specifier.",
1000                          option_str.GetString())
1001                .str(),
1002            llvm::inconvertibleErrorCode());
1003      }
1004      [[fallthrough]];
1005    case OptionParser::eOptionalArgument:
1006      option_arg = OptionParser::GetOptionArgument();
1007      [[fallthrough]];
1008    case OptionParser::eNoArgument:
1009      break;
1010    default:
1011      return llvm::make_error<llvm::StringError>(
1012          llvm::formatv("error with options table; invalid value in has_arg "
1013                        "field for option '{0}'.",
1014                        char(val))
1015              .str(),
1016          llvm::inconvertibleErrorCode());
1017    }
1018    // Find option in the argument list; also see if it was supposed to take an
1019    // argument and if one was supplied.  Remove option (and argument, if
1020    // given) from the argument list.  Also remove them from the
1021    // raw_input_string, if one was passed in.
1022    // Note: We also need to preserve any option argument values that were
1023    // surrounded by backticks, as we lose track of them in the
1024    // option_args_vector.
1025    size_t idx =
1026        FindArgumentIndexForOption(args_copy, long_options[long_options_index]);
1027    std::string option_to_insert;
1028    if (option_arg) {
1029      if (idx != size_t(-1) && has_arg) {
1030        bool arg_has_backtick = args_copy[idx + 1].GetQuoteChar() == '`';
1031        if (arg_has_backtick)
1032          option_to_insert = "`";
1033        option_to_insert += option_arg;
1034        if (arg_has_backtick)
1035          option_to_insert += "`";
1036      } else
1037        option_to_insert = option_arg;
1038    } else
1039      option_to_insert = CommandInterpreter::g_no_argument;
1040
1041    option_arg_vector->emplace_back(std::string(option_str.GetString()),
1042                                    has_arg, option_to_insert);
1043
1044    if (idx == size_t(-1))
1045      continue;
1046
1047    if (!input_line.empty()) {
1048      llvm::StringRef tmp_arg = args_copy[idx].ref();
1049      size_t pos = input_line.find(std::string(tmp_arg));
1050      if (pos != std::string::npos)
1051        input_line.erase(pos, tmp_arg.size());
1052    }
1053    args_copy.DeleteArgumentAtIndex(idx);
1054    if ((option_to_insert != CommandInterpreter::g_no_argument) &&
1055        (OptionParser::GetOptionArgument() != nullptr) &&
1056        (idx < args_copy.GetArgumentCount()) &&
1057        (args_copy[idx].ref() == OptionParser::GetOptionArgument())) {
1058      if (input_line.size() > 0) {
1059        size_t pos = input_line.find(option_to_insert);
1060        if (pos != std::string::npos)
1061          input_line.erase(pos, option_to_insert.size());
1062      }
1063      args_copy.DeleteArgumentAtIndex(idx);
1064    }
1065  }
1066
1067  return std::move(args_copy);
1068}
1069
1070OptionElementVector Options::ParseForCompletion(const Args &args,
1071                                                uint32_t cursor_index) {
1072  OptionElementVector option_element_vector;
1073  Option *long_options = GetLongOptions();
1074  option_element_vector.clear();
1075
1076  if (long_options == nullptr)
1077    return option_element_vector;
1078
1079  std::string short_options = BuildShortOptions(long_options);
1080
1081  std::unique_lock<std::mutex> lock;
1082  OptionParser::Prepare(lock);
1083  OptionParser::EnableError(false);
1084
1085  int val;
1086  auto opt_defs = GetDefinitions();
1087
1088  std::vector<char *> dummy_vec = GetArgvForParsing(args);
1089
1090  bool failed_once = false;
1091  uint32_t dash_dash_pos = -1;
1092
1093  while (true) {
1094    bool missing_argument = false;
1095    int long_options_index = -1;
1096
1097    val = OptionParser::Parse(dummy_vec, short_options, long_options,
1098                              &long_options_index);
1099
1100    if (val == -1) {
1101      // When we're completing a "--" which is the last option on line,
1102      if (failed_once)
1103        break;
1104
1105      failed_once = true;
1106
1107      // If this is a bare  "--" we mark it as such so we can complete it
1108      // successfully later.  Handling the "--" is a little tricky, since that
1109      // may mean end of options or arguments, or the user might want to
1110      // complete options by long name.  I make this work by checking whether
1111      // the cursor is in the "--" argument, and if so I assume we're
1112      // completing the long option, otherwise I let it pass to
1113      // OptionParser::Parse which will terminate the option parsing.  Note, in
1114      // either case we continue parsing the line so we can figure out what
1115      // other options were passed.  This will be useful when we come to
1116      // restricting completions based on what other options we've seen on the
1117      // line.
1118
1119      if (static_cast<size_t>(OptionParser::GetOptionIndex()) <
1120              dummy_vec.size() &&
1121          (strcmp(dummy_vec[OptionParser::GetOptionIndex() - 1], "--") == 0)) {
1122        dash_dash_pos = FindOriginalIndex(
1123            dummy_vec[OptionParser::GetOptionIndex() - 1], args);
1124        if (dash_dash_pos == cursor_index) {
1125          option_element_vector.push_back(
1126              OptionArgElement(OptionArgElement::eBareDoubleDash, dash_dash_pos,
1127                               OptionArgElement::eBareDoubleDash));
1128          continue;
1129        } else
1130          break;
1131      } else
1132        break;
1133    } else if (val == '?') {
1134      option_element_vector.push_back(OptionArgElement(
1135          OptionArgElement::eUnrecognizedArg,
1136          FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1],
1137                            args),
1138          OptionArgElement::eUnrecognizedArg));
1139      continue;
1140    } else if (val == 0) {
1141      continue;
1142    } else if (val == ':') {
1143      // This is a missing argument.
1144      val = OptionParser::GetOptionErrorCause();
1145      missing_argument = true;
1146    }
1147
1148    OptionSeen(val);
1149
1150    // Look up the long option index
1151    if (long_options_index == -1) {
1152      for (int j = 0; long_options[j].definition || long_options[j].flag ||
1153                      long_options[j].val;
1154           ++j) {
1155        if (long_options[j].val == val) {
1156          long_options_index = j;
1157          break;
1158        }
1159      }
1160    }
1161
1162    // See if the option takes an argument, and see if one was supplied.
1163    if (long_options_index >= 0) {
1164      int opt_defs_index = -1;
1165      for (size_t i = 0; i < opt_defs.size(); i++) {
1166        if (opt_defs[i].short_option != val)
1167          continue;
1168        opt_defs_index = i;
1169        break;
1170      }
1171
1172      const OptionDefinition *def = long_options[long_options_index].definition;
1173      int has_arg =
1174          (def == nullptr) ? OptionParser::eNoArgument : def->option_has_arg;
1175      switch (has_arg) {
1176      case OptionParser::eNoArgument:
1177        option_element_vector.push_back(OptionArgElement(
1178            opt_defs_index,
1179            FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1],
1180                              args),
1181            0));
1182        break;
1183      case OptionParser::eRequiredArgument:
1184        if (OptionParser::GetOptionArgument() != nullptr) {
1185          int arg_index;
1186          if (missing_argument)
1187            arg_index = -1;
1188          else
1189            arg_index = OptionParser::GetOptionIndex() - 2;
1190
1191          option_element_vector.push_back(OptionArgElement(
1192              opt_defs_index,
1193              FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 2],
1194                                args),
1195              arg_index));
1196        } else {
1197          option_element_vector.push_back(OptionArgElement(
1198              opt_defs_index,
1199              FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1],
1200                                args),
1201              -1));
1202        }
1203        break;
1204      case OptionParser::eOptionalArgument:
1205        if (OptionParser::GetOptionArgument() != nullptr) {
1206          option_element_vector.push_back(OptionArgElement(
1207              opt_defs_index,
1208              FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 2],
1209                                args),
1210              FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1],
1211                                args)));
1212        } else {
1213          option_element_vector.push_back(OptionArgElement(
1214              opt_defs_index,
1215              FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 2],
1216                                args),
1217              FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1],
1218                                args)));
1219        }
1220        break;
1221      default:
1222        // The options table is messed up.  Here we'll just continue
1223        option_element_vector.push_back(OptionArgElement(
1224            OptionArgElement::eUnrecognizedArg,
1225            FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1],
1226                              args),
1227            OptionArgElement::eUnrecognizedArg));
1228        break;
1229      }
1230    } else {
1231      option_element_vector.push_back(OptionArgElement(
1232          OptionArgElement::eUnrecognizedArg,
1233          FindOriginalIndex(dummy_vec[OptionParser::GetOptionIndex() - 1],
1234                            args),
1235          OptionArgElement::eUnrecognizedArg));
1236    }
1237  }
1238
1239  // Finally we have to handle the case where the cursor index points at a
1240  // single "-".  We want to mark that in the option_element_vector, but only
1241  // if it is not after the "--".  But it turns out that OptionParser::Parse
1242  // just ignores an isolated "-".  So we have to look it up by hand here.  We
1243  // only care if it is AT the cursor position. Note, a single quoted dash is
1244  // not the same as a single dash...
1245
1246  const Args::ArgEntry &cursor = args[cursor_index];
1247  if ((static_cast<int32_t>(dash_dash_pos) == -1 ||
1248       cursor_index < dash_dash_pos) &&
1249      !cursor.IsQuoted() && cursor.ref() == "-") {
1250    option_element_vector.push_back(
1251        OptionArgElement(OptionArgElement::eBareDash, cursor_index,
1252                         OptionArgElement::eBareDash));
1253  }
1254  return option_element_vector;
1255}
1256
1257llvm::Expected<Args> Options::Parse(const Args &args,
1258                                    ExecutionContext *execution_context,
1259                                    lldb::PlatformSP platform_sp,
1260                                    bool require_validation) {
1261  Status error;
1262  Option *long_options = GetLongOptions();
1263  if (long_options == nullptr) {
1264    return llvm::make_error<llvm::StringError>("Invalid long options.",
1265                                               llvm::inconvertibleErrorCode());
1266  }
1267
1268  std::string short_options = BuildShortOptions(long_options);
1269  std::vector<char *> argv = GetArgvForParsing(args);
1270  std::unique_lock<std::mutex> lock;
1271  OptionParser::Prepare(lock);
1272  int val;
1273  while (true) {
1274    int long_options_index = -1;
1275    val = OptionParser::Parse(argv, short_options, long_options,
1276                              &long_options_index);
1277
1278    if (val == ':') {
1279      error.SetErrorString("last option requires an argument");
1280      break;
1281    }
1282
1283    if (val == -1)
1284      break;
1285
1286    // Did we get an error?
1287    if (val == '?') {
1288      error.SetErrorString("unknown or ambiguous option");
1289      break;
1290    }
1291    // The option auto-set itself
1292    if (val == 0)
1293      continue;
1294
1295    OptionSeen(val);
1296
1297    // Lookup the long option index
1298    if (long_options_index == -1) {
1299      for (int i = 0; long_options[i].definition || long_options[i].flag ||
1300                      long_options[i].val;
1301           ++i) {
1302        if (long_options[i].val == val) {
1303          long_options_index = i;
1304          break;
1305        }
1306      }
1307    }
1308    // Call the callback with the option
1309    if (long_options_index >= 0 &&
1310        long_options[long_options_index].definition) {
1311      const OptionDefinition *def = long_options[long_options_index].definition;
1312
1313      if (!platform_sp) {
1314        // User did not pass in an explicit platform.  Try to grab from the
1315        // execution context.
1316        TargetSP target_sp =
1317            execution_context ? execution_context->GetTargetSP() : TargetSP();
1318        platform_sp = target_sp ? target_sp->GetPlatform() : PlatformSP();
1319      }
1320      OptionValidator *validator = def->validator;
1321
1322      if (!platform_sp && require_validation) {
1323        // Caller requires validation but we cannot validate as we don't have
1324        // the mandatory platform against which to validate.
1325        return llvm::make_error<llvm::StringError>(
1326            "cannot validate options: no platform available",
1327            llvm::inconvertibleErrorCode());
1328      }
1329
1330      bool validation_failed = false;
1331      if (platform_sp) {
1332        // Ensure we have an execution context, empty or not.
1333        ExecutionContext dummy_context;
1334        ExecutionContext *exe_ctx_p =
1335            execution_context ? execution_context : &dummy_context;
1336        if (validator && !validator->IsValid(*platform_sp, *exe_ctx_p)) {
1337          validation_failed = true;
1338          error.SetErrorStringWithFormat("Option \"%s\" invalid.  %s",
1339                                         def->long_option,
1340                                         def->validator->LongConditionString());
1341        }
1342      }
1343
1344      // As long as validation didn't fail, we set the option value.
1345      if (!validation_failed)
1346        error =
1347            SetOptionValue(long_options_index,
1348                           (def->option_has_arg == OptionParser::eNoArgument)
1349                               ? nullptr
1350                               : OptionParser::GetOptionArgument(),
1351                           execution_context);
1352      // If the Option setting returned an error, we should stop parsing
1353      // and return the error.
1354      if (error.Fail())
1355        break;
1356    } else {
1357      error.SetErrorStringWithFormat("invalid option with value '%i'", val);
1358    }
1359  }
1360
1361  if (error.Fail())
1362    return error.ToError();
1363
1364  argv.pop_back();
1365  argv.erase(argv.begin(), argv.begin() + OptionParser::GetOptionIndex());
1366  return ReconstituteArgsAfterParsing(argv, args);
1367}
1368