Option.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //===--- Option.cpp - Abstract Driver Options -----------------------------===//
  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. #include "llvm/Option/Option.h"
  10. #include "llvm/ADT/Twine.h"
  11. #include "llvm/Option/Arg.h"
  12. #include "llvm/Option/ArgList.h"
  13. #include "llvm/Support/ErrorHandling.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. #include <algorithm>
  16. #include <cassert>
  17. using namespace llvm;
  18. using namespace llvm::opt;
  19. Option::Option(const OptTable::Info *info, const OptTable *owner)
  20. : Info(info), Owner(owner) {
  21. // Multi-level aliases are not supported. This just simplifies option
  22. // tracking, it is not an inherent limitation.
  23. assert((!Info || !getAlias().isValid() || !getAlias().getAlias().isValid()) &&
  24. "Multi-level aliases are not supported.");
  25. if (Info && getAliasArgs()) {
  26. assert(getAlias().isValid() && "Only alias options can have alias args.");
  27. assert(getKind() == FlagClass && "Only Flag aliases can have alias args.");
  28. assert(getAlias().getKind() != FlagClass &&
  29. "Cannot provide alias args to a flag option.");
  30. }
  31. }
  32. void Option::dump() const {
  33. llvm::errs() << "<";
  34. switch (getKind()) {
  35. #define P(N) case N: llvm::errs() << #N; break
  36. P(GroupClass);
  37. P(InputClass);
  38. P(UnknownClass);
  39. P(FlagClass);
  40. P(JoinedClass);
  41. P(SeparateClass);
  42. P(CommaJoinedClass);
  43. P(MultiArgClass);
  44. P(JoinedOrSeparateClass);
  45. P(JoinedAndSeparateClass);
  46. P(RemainingArgsClass);
  47. #undef P
  48. }
  49. if (Info->Prefixes) {
  50. llvm::errs() << " Prefixes:[";
  51. for (const char * const *Pre = Info->Prefixes; *Pre != nullptr; ++Pre) {
  52. llvm::errs() << '"' << *Pre << (*(Pre + 1) == nullptr ? "\"" : "\", ");
  53. }
  54. llvm::errs() << ']';
  55. }
  56. llvm::errs() << " Name:\"" << getName() << '"';
  57. const Option Group = getGroup();
  58. if (Group.isValid()) {
  59. llvm::errs() << " Group:";
  60. Group.dump();
  61. }
  62. const Option Alias = getAlias();
  63. if (Alias.isValid()) {
  64. llvm::errs() << " Alias:";
  65. Alias.dump();
  66. }
  67. if (getKind() == MultiArgClass)
  68. llvm::errs() << " NumArgs:" << getNumArgs();
  69. llvm::errs() << ">\n";
  70. }
  71. bool Option::matches(OptSpecifier Opt) const {
  72. // Aliases are never considered in matching, look through them.
  73. const Option Alias = getAlias();
  74. if (Alias.isValid())
  75. return Alias.matches(Opt);
  76. // Check exact match.
  77. if (getID() == Opt.getID())
  78. return true;
  79. const Option Group = getGroup();
  80. if (Group.isValid())
  81. return Group.matches(Opt);
  82. return false;
  83. }
  84. Arg *Option::accept(const ArgList &Args,
  85. unsigned &Index,
  86. unsigned ArgSize) const {
  87. const Option &UnaliasedOption = getUnaliasedOption();
  88. StringRef Spelling;
  89. // If the option was an alias, get the spelling from the unaliased one.
  90. if (getID() == UnaliasedOption.getID()) {
  91. Spelling = StringRef(Args.getArgString(Index), ArgSize);
  92. } else {
  93. Spelling = Args.MakeArgString(Twine(UnaliasedOption.getPrefix()) +
  94. Twine(UnaliasedOption.getName()));
  95. }
  96. switch (getKind()) {
  97. case FlagClass: {
  98. if (ArgSize != strlen(Args.getArgString(Index)))
  99. return nullptr;
  100. Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
  101. if (getAliasArgs()) {
  102. const char *Val = getAliasArgs();
  103. while (*Val != '\0') {
  104. A->getValues().push_back(Val);
  105. // Move past the '\0' to the next argument.
  106. Val += strlen(Val) + 1;
  107. }
  108. }
  109. if (UnaliasedOption.getKind() == JoinedClass && !getAliasArgs())
  110. // A Flag alias for a Joined option must provide an argument.
  111. A->getValues().push_back("");
  112. return A;
  113. }
  114. case JoinedClass: {
  115. const char *Value = Args.getArgString(Index) + ArgSize;
  116. return new Arg(UnaliasedOption, Spelling, Index++, Value);
  117. }
  118. case CommaJoinedClass: {
  119. // Always matches.
  120. const char *Str = Args.getArgString(Index) + ArgSize;
  121. Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
  122. // Parse out the comma separated values.
  123. const char *Prev = Str;
  124. for (;; ++Str) {
  125. char c = *Str;
  126. if (!c || c == ',') {
  127. if (Prev != Str) {
  128. char *Value = new char[Str - Prev + 1];
  129. memcpy(Value, Prev, Str - Prev);
  130. Value[Str - Prev] = '\0';
  131. A->getValues().push_back(Value);
  132. }
  133. if (!c)
  134. break;
  135. Prev = Str + 1;
  136. }
  137. }
  138. A->setOwnsValues(true);
  139. return A;
  140. }
  141. case SeparateClass:
  142. // Matches iff this is an exact match.
  143. // FIXME: Avoid strlen.
  144. if (ArgSize != strlen(Args.getArgString(Index)))
  145. return nullptr;
  146. Index += 2;
  147. if (Index > Args.getNumInputArgStrings() ||
  148. Args.getArgString(Index - 1) == nullptr)
  149. return nullptr;
  150. return new Arg(UnaliasedOption, Spelling,
  151. Index - 2, Args.getArgString(Index - 1));
  152. case MultiArgClass: {
  153. // Matches iff this is an exact match.
  154. // FIXME: Avoid strlen.
  155. if (ArgSize != strlen(Args.getArgString(Index)))
  156. return nullptr;
  157. Index += 1 + getNumArgs();
  158. if (Index > Args.getNumInputArgStrings())
  159. return nullptr;
  160. Arg *A = new Arg(UnaliasedOption, Spelling, Index - 1 - getNumArgs(),
  161. Args.getArgString(Index - getNumArgs()));
  162. for (unsigned i = 1; i != getNumArgs(); ++i)
  163. A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
  164. return A;
  165. }
  166. case JoinedOrSeparateClass: {
  167. // If this is not an exact match, it is a joined arg.
  168. // FIXME: Avoid strlen.
  169. if (ArgSize != strlen(Args.getArgString(Index))) {
  170. const char *Value = Args.getArgString(Index) + ArgSize;
  171. return new Arg(*this, Spelling, Index++, Value);
  172. }
  173. // Otherwise it must be separate.
  174. Index += 2;
  175. if (Index > Args.getNumInputArgStrings() ||
  176. Args.getArgString(Index - 1) == nullptr)
  177. return nullptr;
  178. return new Arg(UnaliasedOption, Spelling,
  179. Index - 2, Args.getArgString(Index - 1));
  180. }
  181. case JoinedAndSeparateClass:
  182. // Always matches.
  183. Index += 2;
  184. if (Index > Args.getNumInputArgStrings() ||
  185. Args.getArgString(Index - 1) == nullptr)
  186. return nullptr;
  187. return new Arg(UnaliasedOption, Spelling, Index - 2,
  188. Args.getArgString(Index - 2) + ArgSize,
  189. Args.getArgString(Index - 1));
  190. case RemainingArgsClass: {
  191. // Matches iff this is an exact match.
  192. // FIXME: Avoid strlen.
  193. if (ArgSize != strlen(Args.getArgString(Index)))
  194. return nullptr;
  195. Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
  196. while (Index < Args.getNumInputArgStrings() &&
  197. Args.getArgString(Index) != nullptr)
  198. A->getValues().push_back(Args.getArgString(Index++));
  199. return A;
  200. }
  201. default:
  202. llvm_unreachable("Invalid option kind!");
  203. }
  204. }