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