Option.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //===--- Option.h - Abstract Driver Options ---------------------*- 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_OPTION_H
  10. #define LLVM_OPTION_OPTION_H
  11. #include "llvm/ADT/SmallVector.h"
  12. #include "llvm/ADT/StringRef.h"
  13. #include "llvm/Option/OptTable.h"
  14. #include "llvm/Support/ErrorHandling.h"
  15. namespace llvm {
  16. namespace opt {
  17. class Arg;
  18. class ArgList;
  19. /// ArgStringList - Type used for constructing argv lists for subprocesses.
  20. typedef SmallVector<const char*, 16> ArgStringList;
  21. /// Base flags for all options. Custom flags may be added after.
  22. enum DriverFlag {
  23. HelpHidden = (1 << 0),
  24. RenderAsInput = (1 << 1),
  25. RenderJoined = (1 << 2),
  26. RenderSeparate = (1 << 3)
  27. };
  28. /// Option - Abstract representation for a single form of driver
  29. /// argument.
  30. ///
  31. /// An Option class represents a form of option that the driver
  32. /// takes, for example how many arguments the option has and how
  33. /// they can be provided. Individual option instances store
  34. /// additional information about what group the option is a member
  35. /// of (if any), if the option is an alias, and a number of
  36. /// flags. At runtime the driver parses the command line into
  37. /// concrete Arg instances, each of which corresponds to a
  38. /// particular Option instance.
  39. class Option {
  40. public:
  41. enum OptionClass {
  42. GroupClass = 0,
  43. InputClass,
  44. UnknownClass,
  45. FlagClass,
  46. JoinedClass,
  47. SeparateClass,
  48. RemainingArgsClass,
  49. CommaJoinedClass,
  50. MultiArgClass,
  51. JoinedOrSeparateClass,
  52. JoinedAndSeparateClass
  53. };
  54. enum RenderStyleKind {
  55. RenderCommaJoinedStyle,
  56. RenderJoinedStyle,
  57. RenderSeparateStyle,
  58. RenderValuesStyle
  59. };
  60. protected:
  61. const OptTable::Info *Info;
  62. const OptTable *Owner;
  63. public:
  64. Option(const OptTable::Info *Info, const OptTable *Owner);
  65. bool isValid() const {
  66. return Info != nullptr;
  67. }
  68. unsigned getID() const {
  69. assert(Info && "Must have a valid info!");
  70. return Info->ID;
  71. }
  72. OptionClass getKind() const {
  73. assert(Info && "Must have a valid info!");
  74. return OptionClass(Info->Kind);
  75. }
  76. /// \brief Get the name of this option without any prefix.
  77. StringRef getName() const {
  78. assert(Info && "Must have a valid info!");
  79. return Info->Name;
  80. }
  81. const Option getGroup() const {
  82. assert(Info && "Must have a valid info!");
  83. assert(Owner && "Must have a valid owner!");
  84. return Owner->getOption(Info->GroupID);
  85. }
  86. const Option getAlias() const {
  87. assert(Info && "Must have a valid info!");
  88. assert(Owner && "Must have a valid owner!");
  89. return Owner->getOption(Info->AliasID);
  90. }
  91. /// \brief Get the alias arguments as a \0 separated list.
  92. /// E.g. ["foo", "bar"] would be returned as "foo\0bar\0".
  93. const char *getAliasArgs() const {
  94. assert(Info && "Must have a valid info!");
  95. assert((!Info->AliasArgs || Info->AliasArgs[0] != 0) &&
  96. "AliasArgs should be either 0 or non-empty.");
  97. return Info->AliasArgs;
  98. }
  99. /// \brief Get the default prefix for this option.
  100. StringRef getPrefix() const {
  101. const char *Prefix = *Info->Prefixes;
  102. return Prefix ? Prefix : StringRef();
  103. }
  104. /// \brief Get the name of this option with the default prefix.
  105. std::string getPrefixedName() const {
  106. std::string Ret = getPrefix();
  107. Ret += getName();
  108. return Ret;
  109. }
  110. unsigned getNumArgs() const { return Info->Param; }
  111. bool hasNoOptAsInput() const { return Info->Flags & RenderAsInput;}
  112. RenderStyleKind getRenderStyle() const {
  113. if (Info->Flags & RenderJoined)
  114. return RenderJoinedStyle;
  115. if (Info->Flags & RenderSeparate)
  116. return RenderSeparateStyle;
  117. switch (getKind()) {
  118. case GroupClass:
  119. case InputClass:
  120. case UnknownClass:
  121. return RenderValuesStyle;
  122. case JoinedClass:
  123. case JoinedAndSeparateClass:
  124. return RenderJoinedStyle;
  125. case CommaJoinedClass:
  126. return RenderCommaJoinedStyle;
  127. case FlagClass:
  128. case SeparateClass:
  129. case MultiArgClass:
  130. case JoinedOrSeparateClass:
  131. case RemainingArgsClass:
  132. return RenderSeparateStyle;
  133. }
  134. llvm_unreachable("Unexpected kind!");
  135. }
  136. /// Test if this option has the flag \a Val.
  137. bool hasFlag(unsigned Val) const {
  138. return Info->Flags & Val;
  139. }
  140. /// getUnaliasedOption - Return the final option this option
  141. /// aliases (itself, if the option has no alias).
  142. const Option getUnaliasedOption() const {
  143. const Option Alias = getAlias();
  144. if (Alias.isValid()) return Alias.getUnaliasedOption();
  145. return *this;
  146. }
  147. /// getRenderName - Return the name to use when rendering this
  148. /// option.
  149. StringRef getRenderName() const {
  150. return getUnaliasedOption().getName();
  151. }
  152. /// matches - Predicate for whether this option is part of the
  153. /// given option (which may be a group).
  154. ///
  155. /// Note that matches against options which are an alias should never be
  156. /// done -- aliases do not participate in matching and so such a query will
  157. /// always be false.
  158. bool matches(OptSpecifier ID) const;
  159. /// accept - Potentially accept the current argument, returning a
  160. /// new Arg instance, or 0 if the option does not accept this
  161. /// argument (or the argument is missing values).
  162. ///
  163. /// If the option accepts the current argument, accept() sets
  164. /// Index to the position where argument parsing should resume
  165. /// (even if the argument is missing values).
  166. ///
  167. /// \param ArgSize The number of bytes taken up by the matched Option prefix
  168. /// and name. This is used to determine where joined values
  169. /// start.
  170. Arg *accept(const ArgList &Args, unsigned &Index, unsigned ArgSize) const;
  171. void dump() const;
  172. };
  173. } // end namespace opt
  174. } // end namespace llvm
  175. #endif