OptTable.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- OptTable.h - Option Table --------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_OPTION_OPTTABLE_H
  14. #define LLVM_OPTION_OPTTABLE_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/ADT/StringSet.h"
  18. #include "llvm/Option/OptSpecifier.h"
  19. #include "llvm/Support/StringSaver.h"
  20. #include <cassert>
  21. #include <string>
  22. #include <vector>
  23. namespace llvm {
  24. class raw_ostream;
  25. template <typename Fn> class function_ref;
  26. namespace opt {
  27. class Arg;
  28. class ArgList;
  29. class InputArgList;
  30. class Option;
  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 int Flags;
  52. unsigned short GroupID;
  53. unsigned short AliasID;
  54. const char *AliasArgs;
  55. const char *Values;
  56. };
  57. private:
  58. /// The option information table.
  59. std::vector<Info> OptionInfos;
  60. bool IgnoreCase;
  61. bool GroupedShortOptions = false;
  62. const char *EnvVar = nullptr;
  63. unsigned InputOptionID = 0;
  64. unsigned UnknownOptionID = 0;
  65. /// The index of the first option which can be parsed (i.e., is not a
  66. /// special option like 'input' or 'unknown', and is not an option group).
  67. unsigned FirstSearchableIndex = 0;
  68. /// The union of all option prefixes. If an argument does not begin with
  69. /// one of these, it is an input.
  70. StringSet<> PrefixesUnion;
  71. std::string PrefixChars;
  72. private:
  73. const Info &getInfo(OptSpecifier Opt) const {
  74. unsigned id = Opt.getID();
  75. assert(id > 0 && id - 1 < getNumOptions() && "Invalid Option ID.");
  76. return OptionInfos[id - 1];
  77. }
  78. std::unique_ptr<Arg> parseOneArgGrouped(InputArgList &Args,
  79. unsigned &Index) const;
  80. protected:
  81. OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase = false);
  82. public:
  83. ~OptTable();
  84. /// Return the total number of option classes.
  85. unsigned getNumOptions() const { return OptionInfos.size(); }
  86. /// Get the given Opt's Option instance, lazily creating it
  87. /// if necessary.
  88. ///
  89. /// \return The option, or null for the INVALID option id.
  90. const Option getOption(OptSpecifier Opt) const;
  91. /// Lookup the name of the given option.
  92. const char *getOptionName(OptSpecifier id) const {
  93. return getInfo(id).Name;
  94. }
  95. /// Get the kind of the given option.
  96. unsigned getOptionKind(OptSpecifier id) const {
  97. return getInfo(id).Kind;
  98. }
  99. /// Get the group id for the given option.
  100. unsigned getOptionGroupID(OptSpecifier id) const {
  101. return getInfo(id).GroupID;
  102. }
  103. /// Get the help text to use to describe this option.
  104. const char *getOptionHelpText(OptSpecifier id) const {
  105. return getInfo(id).HelpText;
  106. }
  107. /// Get the meta-variable name to use when describing
  108. /// this options values in the help text.
  109. const char *getOptionMetaVar(OptSpecifier id) const {
  110. return getInfo(id).MetaVar;
  111. }
  112. /// Specify the environment variable where initial options should be read.
  113. void setInitialOptionsFromEnvironment(const char *E) { EnvVar = E; }
  114. /// Support grouped short options. e.g. -ab represents -a -b.
  115. void setGroupedShortOptions(bool Value) { GroupedShortOptions = Value; }
  116. /// Find possible value for given flags. This is used for shell
  117. /// autocompletion.
  118. ///
  119. /// \param [in] Option - Key flag like "-stdlib=" when "-stdlib=l"
  120. /// was passed to clang.
  121. ///
  122. /// \param [in] Arg - Value which we want to autocomplete like "l"
  123. /// when "-stdlib=l" was passed to clang.
  124. ///
  125. /// \return The vector of possible values.
  126. std::vector<std::string> suggestValueCompletions(StringRef Option,
  127. StringRef Arg) const;
  128. /// Find flags from OptTable which starts with Cur.
  129. ///
  130. /// \param [in] Cur - String prefix that all returned flags need
  131. // to start with.
  132. ///
  133. /// \return The vector of flags which start with Cur.
  134. std::vector<std::string> findByPrefix(StringRef Cur,
  135. unsigned int DisableFlags) const;
  136. /// Find the OptTable option that most closely matches the given string.
  137. ///
  138. /// \param [in] Option - A string, such as "-stdlibs=l", that represents user
  139. /// input of an option that may not exist in the OptTable. Note that the
  140. /// string includes prefix dashes "-" as well as values "=l".
  141. /// \param [out] NearestString - The nearest option string found in the
  142. /// OptTable.
  143. /// \param [in] FlagsToInclude - Only find options with any of these flags.
  144. /// Zero is the default, which includes all flags.
  145. /// \param [in] FlagsToExclude - Don't find options with this flag. Zero
  146. /// is the default, and means exclude nothing.
  147. /// \param [in] MinimumLength - Don't find options shorter than this length.
  148. /// For example, a minimum length of 3 prevents "-x" from being considered
  149. /// near to "-S".
  150. ///
  151. /// \return The edit distance of the nearest string found.
  152. unsigned findNearest(StringRef Option, std::string &NearestString,
  153. unsigned FlagsToInclude = 0, unsigned FlagsToExclude = 0,
  154. unsigned MinimumLength = 4) const;
  155. /// Add Values to Option's Values class
  156. ///
  157. /// \param [in] Option - Prefix + Name of the flag which Values will be
  158. /// changed. For example, "-analyzer-checker".
  159. /// \param [in] Values - String of Values seperated by ",", such as
  160. /// "foo, bar..", where foo and bar is the argument which the Option flag
  161. /// takes
  162. ///
  163. /// \return true in success, and false in fail.
  164. bool addValues(const char *Option, const char *Values);
  165. /// Parse a single argument; returning the new argument and
  166. /// updating Index.
  167. ///
  168. /// \param [in,out] Index - The current parsing position in the argument
  169. /// string list; on return this will be the index of the next argument
  170. /// string to parse.
  171. /// \param [in] FlagsToInclude - Only parse options with any of these flags.
  172. /// Zero is the default which includes all flags.
  173. /// \param [in] FlagsToExclude - Don't parse options with this flag. Zero
  174. /// is the default and means exclude nothing.
  175. ///
  176. /// \return The parsed argument, or 0 if the argument is missing values
  177. /// (in which case Index still points at the conceptual next argument string
  178. /// to parse).
  179. std::unique_ptr<Arg> ParseOneArg(const ArgList &Args, unsigned &Index,
  180. unsigned FlagsToInclude = 0,
  181. unsigned FlagsToExclude = 0) const;
  182. /// Parse an list of arguments into an InputArgList.
  183. ///
  184. /// The resulting InputArgList will reference the strings in [\p ArgBegin,
  185. /// \p ArgEnd), and their lifetime should extend past that of the returned
  186. /// InputArgList.
  187. ///
  188. /// The only error that can occur in this routine is if an argument is
  189. /// missing values; in this case \p MissingArgCount will be non-zero.
  190. ///
  191. /// \param MissingArgIndex - On error, the index of the option which could
  192. /// not be parsed.
  193. /// \param MissingArgCount - On error, the number of missing options.
  194. /// \param FlagsToInclude - Only parse options with any of these flags.
  195. /// Zero is the default which includes all flags.
  196. /// \param FlagsToExclude - Don't parse options with this flag. Zero
  197. /// is the default and means exclude nothing.
  198. /// \return An InputArgList; on error this will contain all the options
  199. /// which could be parsed.
  200. InputArgList ParseArgs(ArrayRef<const char *> Args, unsigned &MissingArgIndex,
  201. unsigned &MissingArgCount, unsigned FlagsToInclude = 0,
  202. unsigned FlagsToExclude = 0) const;
  203. /// A convenience helper which handles optional initial options populated from
  204. /// an environment variable, expands response files recursively and parses
  205. /// options.
  206. ///
  207. /// \param ErrorFn - Called on a formatted error message for missing arguments
  208. /// or unknown options.
  209. /// \return An InputArgList; on error this will contain all the options which
  210. /// could be parsed.
  211. InputArgList parseArgs(int Argc, char *const *Argv, OptSpecifier Unknown,
  212. StringSaver &Saver,
  213. function_ref<void(StringRef)> ErrorFn) const;
  214. /// Render the help text for an option table.
  215. ///
  216. /// \param OS - The stream to write the help text to.
  217. /// \param Usage - USAGE: Usage
  218. /// \param Title - OVERVIEW: Title
  219. /// \param FlagsToInclude - If non-zero, only include options with any
  220. /// of these flags set.
  221. /// \param FlagsToExclude - Exclude options with any of these flags set.
  222. /// \param ShowAllAliases - If true, display all options including aliases
  223. /// that don't have help texts. By default, we display
  224. /// only options that are not hidden and have help
  225. /// texts.
  226. void printHelp(raw_ostream &OS, const char *Usage, const char *Title,
  227. unsigned FlagsToInclude, unsigned FlagsToExclude,
  228. bool ShowAllAliases) const;
  229. void printHelp(raw_ostream &OS, const char *Usage, const char *Title,
  230. bool ShowHidden = false, bool ShowAllAliases = false) const;
  231. };
  232. } // end namespace opt
  233. } // end namespace llvm
  234. #endif // LLVM_OPTION_OPTTABLE_H
  235. #ifdef __GNUC__
  236. #pragma GCC diagnostic pop
  237. #endif