blob: 5db30436069dbe7c5f3f494f948c34194dce8f4f [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- OptTable.h - Option Table --------------------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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
20namespace llvm {
21
22class raw_ostream;
23
24namespace opt {
25
26class Arg;
27class ArgList;
28class InputArgList;
29class Option;
30
Andrew Scullcdfcccc2018-10-05 20:58:37 +010031/// Provide access to the Option info table.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010032///
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.
38class OptTable {
39public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +010040 /// Entry for a single option instance in the option data table.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010041 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
58private:
Andrew Scullcdfcccc2018-10-05 20:58:37 +010059 /// The option information table.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010060 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
75private:
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
82protected:
83 OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase = false);
84
85public:
86 ~OptTable();
87
Andrew Scullcdfcccc2018-10-05 20:58:37 +010088 /// Return the total number of option classes.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010089 unsigned getNumOptions() const { return OptionInfos.size(); }
90
Andrew Scullcdfcccc2018-10-05 20:58:37 +010091 /// Get the given Opt's Option instance, lazily creating it
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010092 /// if necessary.
93 ///
94 /// \return The option, or null for the INVALID option id.
95 const Option getOption(OptSpecifier Opt) const;
96
Andrew Scullcdfcccc2018-10-05 20:58:37 +010097 /// Lookup the name of the given option.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010098 const char *getOptionName(OptSpecifier id) const {
99 return getInfo(id).Name;
100 }
101
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100102 /// Get the kind of the given option.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100103 unsigned getOptionKind(OptSpecifier id) const {
104 return getInfo(id).Kind;
105 }
106
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100107 /// Get the group id for the given option.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100108 unsigned getOptionGroupID(OptSpecifier id) const {
109 return getInfo(id).GroupID;
110 }
111
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100112 /// Get the help text to use to describe this option.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100113 const char *getOptionHelpText(OptSpecifier id) const {
114 return getInfo(id).HelpText;
115 }
116
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100117 /// Get the meta-variable name to use when describing
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100118 /// 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100176 /// Parse a single argument; returning the new argument and
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100177 /// 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100194 /// Parse an list of arguments into an InputArgList.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100195 ///
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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100216 /// Render the help text for an option table.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100217 ///
218 /// \param OS - The stream to write the help text to.
Andrew Scull0372a572018-11-16 15:47:06 +0000219 /// \param Usage - USAGE: Usage
220 /// \param Title - OVERVIEW: Title
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100221 /// \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.
Andrew Scull0372a572018-11-16 15:47:06 +0000228 void PrintHelp(raw_ostream &OS, const char *Usage, const char *Title,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100229 unsigned FlagsToInclude, unsigned FlagsToExclude,
230 bool ShowAllAliases) const;
231
Andrew Scull0372a572018-11-16 15:47:06 +0000232 void PrintHelp(raw_ostream &OS, const char *Usage, const char *Title,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100233 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