OptTable.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #ifndef LLVM_OPTION_OPTTABLE_H
  10. #define LLVM_OPTION_OPTTABLE_H
  11. #include "llvm/ADT/ArrayRef.h"
  12. #include "llvm/ADT/StringSet.h"
  13. #include "llvm/Option/OptSpecifier.h"
  14. namespace llvm {
  15. class raw_ostream;
  16. namespace opt {
  17. class Arg;
  18. class ArgList;
  19. class InputArgList;
  20. class Option;
  21. /// \brief Provide access to the Option info table.
  22. ///
  23. /// The OptTable class provides a layer of indirection which allows Option
  24. /// instance to be created lazily. In the common case, only a few options will
  25. /// be needed at runtime; the OptTable class maintains enough information to
  26. /// parse command lines without instantiating Options, while letting other
  27. /// parts of the driver still use Option instances where convenient.
  28. class OptTable {
  29. public:
  30. /// \brief Entry for a single option instance in the option data table.
  31. struct Info {
  32. /// A null terminated array of prefix strings to apply to name while
  33. /// matching.
  34. const char *const *Prefixes;
  35. const char *Name;
  36. const char *HelpText;
  37. const char *MetaVar;
  38. unsigned ID;
  39. unsigned char Kind;
  40. unsigned char Param;
  41. unsigned long Flags;
  42. unsigned short GroupID;
  43. unsigned short AliasID;
  44. const char *AliasArgs;
  45. };
  46. private:
  47. /// \brief The static option information table.
  48. const Info *OptionInfos;
  49. unsigned NumOptionInfos;
  50. bool IgnoreCase;
  51. unsigned TheInputOptionID;
  52. unsigned TheUnknownOptionID;
  53. /// The index of the first option which can be parsed (i.e., is not a
  54. /// special option like 'input' or 'unknown', and is not an option group).
  55. unsigned FirstSearchableIndex;
  56. /// The union of all option prefixes. If an argument does not begin with
  57. /// one of these, it is an input.
  58. StringSet<> PrefixesUnion;
  59. std::string PrefixChars;
  60. private:
  61. const Info &getInfo(OptSpecifier Opt) const {
  62. unsigned id = Opt.getID();
  63. assert(id > 0 && id - 1 < getNumOptions() && "Invalid Option ID.");
  64. return OptionInfos[id - 1];
  65. }
  66. protected:
  67. OptTable(const Info *OptionInfos, unsigned NumOptionInfos,
  68. bool IgnoreCase = false);
  69. public:
  70. ~OptTable();
  71. /// \brief Return the total number of option classes.
  72. unsigned getNumOptions() const { return NumOptionInfos; }
  73. /// \brief Get the given Opt's Option instance, lazily creating it
  74. /// if necessary.
  75. ///
  76. /// \return The option, or null for the INVALID option id.
  77. const Option getOption(OptSpecifier Opt) const;
  78. /// \brief Lookup the name of the given option.
  79. const char *getOptionName(OptSpecifier id) const {
  80. return getInfo(id).Name;
  81. }
  82. /// \brief Get the kind of the given option.
  83. unsigned getOptionKind(OptSpecifier id) const {
  84. return getInfo(id).Kind;
  85. }
  86. /// \brief Get the group id for the given option.
  87. unsigned getOptionGroupID(OptSpecifier id) const {
  88. return getInfo(id).GroupID;
  89. }
  90. /// \brief Get the help text to use to describe this option.
  91. const char *getOptionHelpText(OptSpecifier id) const {
  92. return getInfo(id).HelpText;
  93. }
  94. /// \brief Get the meta-variable name to use when describing
  95. /// this options values in the help text.
  96. const char *getOptionMetaVar(OptSpecifier id) const {
  97. return getInfo(id).MetaVar;
  98. }
  99. /// \brief Parse a single argument; returning the new argument and
  100. /// updating Index.
  101. ///
  102. /// \param [in,out] Index - The current parsing position in the argument
  103. /// string list; on return this will be the index of the next argument
  104. /// string to parse.
  105. /// \param [in] FlagsToInclude - Only parse options with any of these flags.
  106. /// Zero is the default which includes all flags.
  107. /// \param [in] FlagsToExclude - Don't parse options with this flag. Zero
  108. /// is the default and means exclude nothing.
  109. ///
  110. /// \return The parsed argument, or 0 if the argument is missing values
  111. /// (in which case Index still points at the conceptual next argument string
  112. /// to parse).
  113. Arg *ParseOneArg(const ArgList &Args, unsigned &Index,
  114. unsigned FlagsToInclude = 0,
  115. unsigned FlagsToExclude = 0) const;
  116. Option findOption(const char *normalizedName, unsigned FlagsToInclude = 0, unsigned FlagsToExclude = 0) const; // HLSL Change
  117. /// \brief Parse an list of arguments into an InputArgList.
  118. ///
  119. /// The resulting InputArgList will reference the strings in [\p ArgBegin,
  120. /// \p ArgEnd), and their lifetime should extend past that of the returned
  121. /// InputArgList.
  122. ///
  123. /// The only error that can occur in this routine is if an argument is
  124. /// missing values; in this case \p MissingArgCount will be non-zero.
  125. ///
  126. /// \param MissingArgIndex - On error, the index of the option which could
  127. /// not be parsed.
  128. /// \param MissingArgCount - On error, the number of missing options.
  129. /// \param FlagsToInclude - Only parse options with any of these flags.
  130. /// Zero is the default which includes all flags.
  131. /// \param FlagsToExclude - Don't parse options with this flag. Zero
  132. /// is the default and means exclude nothing.
  133. /// \return An InputArgList; on error this will contain all the options
  134. /// which could be parsed.
  135. InputArgList ParseArgs(ArrayRef<const char *> Args, unsigned &MissingArgIndex,
  136. unsigned &MissingArgCount, unsigned FlagsToInclude = 0,
  137. unsigned FlagsToExclude = 0) const;
  138. /// \brief Render the help text for an option table.
  139. ///
  140. /// \param OS - The stream to write the help text to.
  141. /// \param Name - The name to use in the usage line.
  142. /// \param Title - The title to use in the usage line.
  143. /// \param FlagsToInclude - If non-zero, only include options with any
  144. /// of these flags set.
  145. /// \param FlagsToExclude - Exclude options with any of these flags set.
  146. void PrintHelp(raw_ostream &OS, const char *Name, const char *Title,
  147. /* HLSL Change - version info */ const char *VersionInfo,
  148. unsigned FlagsToInclude, unsigned FlagsToExclude) const;
  149. void PrintHelp(raw_ostream &OS, const char *Name, const char *Title,
  150. /* HLSL Change - version info */ const char *VersionInfo,
  151. bool ShowHidden = false) const;
  152. };
  153. } // end namespace opt
  154. } // end namespace llvm
  155. #endif