LLVM  6.0.0svn
OptTable.h
Go to the documentation of this file.
1 //===- 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"
17 #include <cassert>
18 #include <string>
19 #include <vector>
20 
21 namespace llvm {
22 
23 class raw_ostream;
24 
25 namespace opt {
26 
27 class Arg;
28 class ArgList;
29 class InputArgList;
30 class Option;
31 
32 /// \brief Provide access to the Option info table.
33 ///
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.
39 class OptTable {
40 public:
41  /// \brief Entry for a single option instance in the option data table.
42  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 
59 private:
60  /// \brief The option information table.
61  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 
76 private:
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 
83 protected:
84  OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase = false);
85 
86 public:
87  ~OptTable();
88 
89  /// \brief Return the total number of option classes.
90  unsigned getNumOptions() const { return OptionInfos.size(); }
91 
92  /// \brief Get the given Opt's Option instance, lazily creating it
93  /// if necessary.
94  ///
95  /// \return The option, or null for the INVALID option id.
96  const Option getOption(OptSpecifier Opt) const;
97 
98  /// \brief Lookup the name of the given option.
99  const char *getOptionName(OptSpecifier id) const {
100  return getInfo(id).Name;
101  }
102 
103  /// \brief Get the kind of the given option.
104  unsigned getOptionKind(OptSpecifier id) const {
105  return getInfo(id).Kind;
106  }
107 
108  /// \brief Get the group id for the given option.
109  unsigned getOptionGroupID(OptSpecifier id) const {
110  return getInfo(id).GroupID;
111  }
112 
113  /// \brief Get the help text to use to describe this option.
114  const char *getOptionHelpText(OptSpecifier id) const {
115  return getInfo(id).HelpText;
116  }
117 
118  /// \brief Get the meta-variable name to use when describing
119  /// 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  /// Add Values to Option's Values class
147  ///
148  /// \param [in] Option - Prefix + Name of the flag which Values will be
149  /// changed. For example, "-analyzer-checker".
150  /// \param [in] Values - String of Values seperated by ",", such as
151  /// "foo, bar..", where foo and bar is the argument which the Option flag
152  /// takes
153  ///
154  /// \return true in success, and false in fail.
155  bool addValues(const char *Option, const char *Values);
156 
157  /// \brief Parse a single argument; returning the new argument and
158  /// updating Index.
159  ///
160  /// \param [in,out] Index - The current parsing position in the argument
161  /// string list; on return this will be the index of the next argument
162  /// string to parse.
163  /// \param [in] FlagsToInclude - Only parse options with any of these flags.
164  /// Zero is the default which includes all flags.
165  /// \param [in] FlagsToExclude - Don't parse options with this flag. Zero
166  /// is the default and means exclude nothing.
167  ///
168  /// \return The parsed argument, or 0 if the argument is missing values
169  /// (in which case Index still points at the conceptual next argument string
170  /// to parse).
171  Arg *ParseOneArg(const ArgList &Args, unsigned &Index,
172  unsigned FlagsToInclude = 0,
173  unsigned FlagsToExclude = 0) const;
174 
175  /// \brief Parse an list of arguments into an InputArgList.
176  ///
177  /// The resulting InputArgList will reference the strings in [\p ArgBegin,
178  /// \p ArgEnd), and their lifetime should extend past that of the returned
179  /// InputArgList.
180  ///
181  /// The only error that can occur in this routine is if an argument is
182  /// missing values; in this case \p MissingArgCount will be non-zero.
183  ///
184  /// \param MissingArgIndex - On error, the index of the option which could
185  /// not be parsed.
186  /// \param MissingArgCount - On error, the number of missing options.
187  /// \param FlagsToInclude - Only parse options with any of these flags.
188  /// Zero is the default which includes all flags.
189  /// \param FlagsToExclude - Don't parse options with this flag. Zero
190  /// is the default and means exclude nothing.
191  /// \return An InputArgList; on error this will contain all the options
192  /// which could be parsed.
193  InputArgList ParseArgs(ArrayRef<const char *> Args, unsigned &MissingArgIndex,
194  unsigned &MissingArgCount, unsigned FlagsToInclude = 0,
195  unsigned FlagsToExclude = 0) const;
196 
197  /// \brief Render the help text for an option table.
198  ///
199  /// \param OS - The stream to write the help text to.
200  /// \param Name - The name to use in the usage line.
201  /// \param Title - The title to use in the usage line.
202  /// \param FlagsToInclude - If non-zero, only include options with any
203  /// of these flags set.
204  /// \param FlagsToExclude - Exclude options with any of these flags set.
205  /// \param ShowAllAliases - If true, display all options including aliases
206  /// that don't have help texts. By default, we display
207  /// only options that are not hidden and have help
208  /// texts.
209  void PrintHelp(raw_ostream &OS, const char *Name, const char *Title,
210  unsigned FlagsToInclude, unsigned FlagsToExclude,
211  bool ShowAllAliases) const;
212 
213  void PrintHelp(raw_ostream &OS, const char *Name, const char *Title,
214  bool ShowHidden = false, bool ShowAllAliases = false) const;
215 };
216 
217 } // end namespace opt
218 
219 } // end namespace llvm
220 
221 #endif // LLVM_OPTION_OPTTABLE_H
const char * AliasArgs
Definition: OptTable.h:55
unsigned getNumOptions() const
Return the total number of option classes.
Definition: OptTable.h:90
unsigned short GroupID
Definition: OptTable.h:53
unsigned short Flags
Definition: OptTable.h:52
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
InputArgList ParseArgs(ArrayRef< const char *> Args, unsigned &MissingArgIndex, unsigned &MissingArgCount, unsigned FlagsToInclude=0, unsigned FlagsToExclude=0) const
Parse an list of arguments into an InputArgList.
Definition: OptTable.cpp:320
unsigned getOptionGroupID(OptSpecifier id) const
Get the group id for the given option.
Definition: OptTable.h:109
unsigned char Param
Definition: OptTable.h:51
std::vector< std::string > findByPrefix(StringRef Cur, unsigned short DisableFlags) const
Find flags from OptTable which starts with Cur.
Definition: OptTable.cpp:230
unsigned short AliasID
Definition: OptTable.h:54
unsigned getID() const
Definition: OptSpecifier.h:30
Arg * ParseOneArg(const ArgList &Args, unsigned &Index, unsigned FlagsToInclude=0, unsigned FlagsToExclude=0) const
Parse a single argument; returning the new argument and updating Index.
Definition: OptTable.cpp:261
unsigned char Kind
Definition: OptTable.h:50
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
OptTable(ArrayRef< Info > OptionInfos, bool IgnoreCase=false)
Definition: OptTable.cpp:98
Option - Abstract representation for a single form of driver argument.
Definition: Option.h:52
A concrete instance of a particular driver option.
Definition: Arg.h:35
Provide access to the Option info table.
Definition: OptTable.h:39
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
const char * HelpText
Definition: OptTable.h:47
const char *const * Prefixes
A null terminated array of prefix strings to apply to name while matching.
Definition: OptTable.h:45
const Option getOption(OptSpecifier Opt) const
Get the given Opt&#39;s Option instance, lazily creating it if necessary.
Definition: OptTable.cpp:162
const char * getOptionName(OptSpecifier id) const
Lookup the name of the given option.
Definition: OptTable.h:99
const char * Values
Definition: OptTable.h:56
std::vector< std::string > suggestValueCompletions(StringRef Option, StringRef Arg) const
Find possible value for given flags.
Definition: OptTable.cpp:210
unsigned getOptionKind(OptSpecifier id) const
Get the kind of the given option.
Definition: OptTable.h:104
amdgpu Simplify well known AMD library false Value Value * Arg
OptSpecifier - Wrapper class for abstracting references to option IDs.
Definition: OptSpecifier.h:19
void PrintHelp(raw_ostream &OS, const char *Name, const char *Title, unsigned FlagsToInclude, unsigned FlagsToExclude, bool ShowAllAliases) const
Render the help text for an option table.
Definition: OptTable.cpp:467
Entry for a single option instance in the option data table.
Definition: OptTable.h:42
const char * MetaVar
Definition: OptTable.h:48
bool addValues(const char *Option, const char *Values)
Add Values to Option&#39;s Values class.
Definition: OptTable.cpp:250
const char * getOptionHelpText(OptSpecifier id) const
Get the help text to use to describe this option.
Definition: OptTable.h:114
const char * getOptionMetaVar(OptSpecifier id) const
Get the meta-variable name to use when describing this options values in the help text...
Definition: OptTable.h:120
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:28
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
hexagon cext opt
ArgList - Ordered collection of driver arguments.
Definition: ArgList.h:120
constexpr char Args[]
Key for Kernel::Metadata::mArgs.