1 //===- OptTable.h - Option Table --------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_OPTION_OPTTABLE_H
10 #define LLVM_OPTION_OPTTABLE_H
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/StringSet.h"
15 #include "llvm/Option/OptSpecifier.h"
31 /// Provide access to the Option info table.
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.
40 /// Entry for a single option instance in the option data table.
42 /// A null terminated array of prefix strings to apply to name while
44 const char *const *Prefixes
;
52 unsigned short GroupID
;
53 unsigned short AliasID
;
54 const char *AliasArgs
;
59 /// The option information table.
60 std::vector
<Info
> OptionInfos
;
63 unsigned TheInputOptionID
= 0;
64 unsigned TheUnknownOptionID
= 0;
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;
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
;
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];
83 OptTable(ArrayRef
<Info
> OptionInfos
, bool IgnoreCase
= false);
88 /// Return the total number of option classes.
89 unsigned getNumOptions() const { return OptionInfos
.size(); }
91 /// Get the given Opt's Option instance, lazily creating it
94 /// \return The option, or null for the INVALID option id.
95 const Option
getOption(OptSpecifier Opt
) const;
97 /// Lookup the name of the given option.
98 const char *getOptionName(OptSpecifier id
) const {
99 return getInfo(id
).Name
;
102 /// Get the kind of the given option.
103 unsigned getOptionKind(OptSpecifier id
) const {
104 return getInfo(id
).Kind
;
107 /// Get the group id for the given option.
108 unsigned getOptionGroupID(OptSpecifier id
) const {
109 return getInfo(id
).GroupID
;
112 /// Get the help text to use to describe this option.
113 const char *getOptionHelpText(OptSpecifier id
) const {
114 return getInfo(id
).HelpText
;
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
;
123 /// Find possible value for given flags. This is used for shell
126 /// \param [in] Option - Key flag like "-stdlib=" when "-stdlib=l"
127 /// was passed to clang.
129 /// \param [in] Arg - Value which we want to autocomplete like "l"
130 /// when "-stdlib=l" was passed to clang.
132 /// \return The vector of possible values.
133 std::vector
<std::string
> suggestValueCompletions(StringRef Option
,
134 StringRef Arg
) const;
136 /// Find flags from OptTable which starts with Cur.
138 /// \param [in] Cur - String prefix that all returned flags need
141 /// \return The vector of flags which start with Cur.
142 std::vector
<std::string
> findByPrefix(StringRef Cur
,
143 unsigned short DisableFlags
) const;
145 /// Find the OptTable option that most closely matches the given string.
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
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
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;
165 /// Add Values to Option's Values class
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
173 /// \return true in success, and false in fail.
174 bool addValues(const char *Option
, const char *Values
);
176 /// Parse a single argument; returning the new argument and
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
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.
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
190 Arg
*ParseOneArg(const ArgList
&Args
, unsigned &Index
,
191 unsigned FlagsToInclude
= 0,
192 unsigned FlagsToExclude
= 0) const;
194 /// Parse an list of arguments into an InputArgList.
196 /// The resulting InputArgList will reference the strings in [\p ArgBegin,
197 /// \p ArgEnd), and their lifetime should extend past that of the returned
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.
203 /// \param MissingArgIndex - On error, the index of the option which could
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;
216 /// Render the help text for an option table.
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
228 void PrintHelp(raw_ostream
&OS
, const char *Usage
, const char *Title
,
229 unsigned FlagsToInclude
, unsigned FlagsToExclude
,
230 bool ShowAllAliases
) const;
232 void PrintHelp(raw_ostream
&OS
, const char *Usage
, const char *Title
,
233 bool ShowHidden
= false, bool ShowAllAliases
= false) const;
236 } // end namespace opt
238 } // end namespace llvm
240 #endif // LLVM_OPTION_OPTTABLE_H