1 //===- OptTable.h - Option Table --------------------------------*- C++ -*-===//
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 #ifndef LLVM_OPTION_OPTTABLE_H
10 #define LLVM_OPTION_OPTTABLE_H
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/StringSet.h"
15 #include "llvm/Option/OptSpecifier.h"
16 #include <cassert>
17 #include <string>
18 #include <vector>
19 
20 namespace llvm {
21 
22 class raw_ostream;
23 
24 namespace opt {
25 
26 class Arg;
27 class ArgList;
28 class InputArgList;
29 class Option;
30 
31 /// Provide access to the Option info table.
32 ///
33 /// The OptTable class provides a layer of indirection which allows Option
34 /// instance to be created lazily. In the common case, only a few options will
35 /// be needed at runtime; the OptTable class maintains enough information to
36 /// parse command lines without instantiating Options, while letting other
37 /// parts of the driver still use Option instances where convenient.
38 class OptTable {
39 public:
40   /// Entry for a single option instance in the option data table.
41   struct Info {
42     /// A null terminated array of prefix strings to apply to name while
43     /// matching.
44     const char *const *Prefixes;
45     const char *Name;
46     const char *HelpText;
47     const char *MetaVar;
48     unsigned ID;
49     unsigned char Kind;
50     unsigned char Param;
51     unsigned short Flags;
52     unsigned short GroupID;
53     unsigned short AliasID;
54     const char *AliasArgs;
55     const char *Values;
56   };
57 
58 private:
59   /// The option information table.
60   std::vector<Info> OptionInfos;
61   bool IgnoreCase;
62 
63   unsigned TheInputOptionID = 0;
64   unsigned TheUnknownOptionID = 0;
65 
66   /// The index of the first option which can be parsed (i.e., is not a
67   /// special option like 'input' or 'unknown', and is not an option group).
68   unsigned FirstSearchableIndex = 0;
69 
70   /// The union of all option prefixes. If an argument does not begin with
71   /// one of these, it is an input.
72   StringSet<> PrefixesUnion;
73   std::string PrefixChars;
74 
75 private:
76   const Info &getInfo(OptSpecifier Opt) const {
77     unsigned id = Opt.getID();
78     assert(id > 0 && id - 1 < getNumOptions() && "Invalid Option ID.");
79     return OptionInfos[id - 1];
80   }
81 
82 protected:
83   OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase = false);
84 
85 public:
86   ~OptTable();
87 
88   /// Return the total number of option classes.
89   unsigned getNumOptions() const { return OptionInfos.size(); }
90 
91   /// Get the given Opt's Option instance, lazily creating it
92   /// if necessary.
93   ///
94   /// \return The option, or null for the INVALID option id.
95   const Option getOption(OptSpecifier Opt) const;
96 
97   /// Lookup the name of the given option.
98   const char *getOptionName(OptSpecifier id) const {
99     return getInfo(id).Name;
100   }
101 
102   /// Get the kind of the given option.
103   unsigned getOptionKind(OptSpecifier id) const {
104     return getInfo(id).Kind;
105   }
106 
107   /// Get the group id for the given option.
108   unsigned getOptionGroupID(OptSpecifier id) const {
109     return getInfo(id).GroupID;
110   }
111 
112   /// Get the help text to use to describe this option.
113   const char *getOptionHelpText(OptSpecifier id) const {
114     return getInfo(id).HelpText;
115   }
116 
117   /// Get the meta-variable name to use when describing
118   /// this options values in the help text.
119   const char *getOptionMetaVar(OptSpecifier id) const {
120     return getInfo(id).MetaVar;
121   }
122 
123   /// Find possible value for given flags. This is used for shell
124   /// autocompletion.
125   ///
126   /// \param [in] Option - Key flag like "-stdlib=" when "-stdlib=l"
127   /// was passed to clang.
128   ///
129   /// \param [in] Arg - Value which we want to autocomplete like "l"
130   /// when "-stdlib=l" was passed to clang.
131   ///
132   /// \return The vector of possible values.
133   std::vector<std::string> suggestValueCompletions(StringRef Option,
134                                                    StringRef Arg) const;
135 
136   /// Find flags from OptTable which starts with Cur.
137   ///
138   /// \param [in] Cur - String prefix that all returned flags need
139   //  to start with.
140   ///
141   /// \return The vector of flags which start with Cur.
142   std::vector<std::string> findByPrefix(StringRef Cur,
143                                         unsigned short DisableFlags) const;
144 
145   /// Find the OptTable option that most closely matches the given string.
146   ///
147   /// \param [in] Option - A string, such as "-stdlibs=l", that represents user
148   /// input of an option that may not exist in the OptTable. Note that the
149   /// string includes prefix dashes "-" as well as values "=l".
150   /// \param [out] NearestString - The nearest option string found in the
151   /// OptTable.
152   /// \param [in] FlagsToInclude - Only find options with any of these flags.
153   /// Zero is the default, which includes all flags.
154   /// \param [in] FlagsToExclude - Don't find options with this flag. Zero
155   /// is the default, and means exclude nothing.
156   /// \param [in] MinimumLength - Don't find options shorter than this length.
157   /// For example, a minimum length of 3 prevents "-x" from being considered
158   /// near to "-S".
159   ///
160   /// \return The edit distance of the nearest string found.
161   unsigned findNearest(StringRef Option, std::string &NearestString,
162                        unsigned FlagsToInclude = 0, unsigned FlagsToExclude = 0,
163                        unsigned MinimumLength = 4) const;
164 
165   /// Add Values to Option's Values class
166   ///
167   /// \param [in] Option - Prefix + Name of the flag which Values will be
168   ///  changed. For example, "-analyzer-checker".
169   /// \param [in] Values - String of Values seperated by ",", such as
170   ///  "foo, bar..", where foo and bar is the argument which the Option flag
171   ///  takes
172   ///
173   /// \return true in success, and false in fail.
174   bool addValues(const char *Option, const char *Values);
175 
176   /// Parse a single argument; returning the new argument and
177   /// updating Index.
178   ///
179   /// \param [in,out] Index - The current parsing position in the argument
180   /// string list; on return this will be the index of the next argument
181   /// string to parse.
182   /// \param [in] FlagsToInclude - Only parse options with any of these flags.
183   /// Zero is the default which includes all flags.
184   /// \param [in] FlagsToExclude - Don't parse options with this flag.  Zero
185   /// is the default and means exclude nothing.
186   ///
187   /// \return The parsed argument, or 0 if the argument is missing values
188   /// (in which case Index still points at the conceptual next argument string
189   /// to parse).
190   Arg *ParseOneArg(const ArgList &Args, unsigned &Index,
191                    unsigned FlagsToInclude = 0,
192                    unsigned FlagsToExclude = 0) const;
193 
194   /// Parse an list of arguments into an InputArgList.
195   ///
196   /// The resulting InputArgList will reference the strings in [\p ArgBegin,
197   /// \p ArgEnd), and their lifetime should extend past that of the returned
198   /// InputArgList.
199   ///
200   /// The only error that can occur in this routine is if an argument is
201   /// missing values; in this case \p MissingArgCount will be non-zero.
202   ///
203   /// \param MissingArgIndex - On error, the index of the option which could
204   /// not be parsed.
205   /// \param MissingArgCount - On error, the number of missing options.
206   /// \param FlagsToInclude - Only parse options with any of these flags.
207   /// Zero is the default which includes all flags.
208   /// \param FlagsToExclude - Don't parse options with this flag.  Zero
209   /// is the default and means exclude nothing.
210   /// \return An InputArgList; on error this will contain all the options
211   /// which could be parsed.
212   InputArgList ParseArgs(ArrayRef<const char *> Args, unsigned &MissingArgIndex,
213                          unsigned &MissingArgCount, unsigned FlagsToInclude = 0,
214                          unsigned FlagsToExclude = 0) const;
215 
216   /// Render the help text for an option table.
217   ///
218   /// \param OS - The stream to write the help text to.
219   /// \param Usage - USAGE: Usage
220   /// \param Title - OVERVIEW: Title
221   /// \param FlagsToInclude - If non-zero, only include options with any
222   ///                         of these flags set.
223   /// \param FlagsToExclude - Exclude options with any of these flags set.
224   /// \param ShowAllAliases - If true, display all options including aliases
225   ///                         that don't have help texts. By default, we display
226   ///                         only options that are not hidden and have help
227   ///                         texts.
228   void PrintHelp(raw_ostream &OS, const char *Usage, const char *Title,
229                  unsigned FlagsToInclude, unsigned FlagsToExclude,
230                  bool ShowAllAliases) const;
231 
232   void PrintHelp(raw_ostream &OS, const char *Usage, const char *Title,
233                  bool ShowHidden = false, bool ShowAllAliases = false) const;
234 };
235 
236 } // end namespace opt
237 
238 } // end namespace llvm
239 
240 #endif // LLVM_OPTION_OPTTABLE_H
241