Option.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. // HLSL Change Start: Count spaces immediately after option name.
  97. // This is to handle the case where the argument was provided as
  98. // "-opt value" instead of two arguments: "-opt", "value"
  99. unsigned JoinedSpaces = 0;
  100. {
  101. const char *Val = Args.getArgString(Index) + ArgSize;
  102. while (*Val++ == ' ')
  103. ++JoinedSpaces;
  104. }
  105. // HLSL Change End
  106. switch (getKind()) {
  107. case FlagClass: {
  108. if (ArgSize != strlen(Args.getArgString(Index)))
  109. return nullptr;
  110. Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
  111. if (getAliasArgs()) {
  112. const char *Val = getAliasArgs();
  113. while (*Val != '\0') {
  114. A->getValues().push_back(Val);
  115. // Move past the '\0' to the next argument.
  116. Val += strlen(Val) + 1;
  117. }
  118. }
  119. if (UnaliasedOption.getKind() == JoinedClass && !getAliasArgs())
  120. // A Flag alias for a Joined option must provide an argument.
  121. A->getValues().push_back("");
  122. return A;
  123. }
  124. case JoinedClass: {
  125. const char *Value = Args.getArgString(Index) + ArgSize;
  126. return new Arg(UnaliasedOption, Spelling, Index++, Value);
  127. }
  128. case CommaJoinedClass: {
  129. // Always matches.
  130. const char *Str = Args.getArgString(Index) + ArgSize;
  131. Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
  132. // Parse out the comma separated values.
  133. const char *Prev = Str;
  134. for (;; ++Str) {
  135. char c = *Str;
  136. if (!c || c == ',') {
  137. if (Prev != Str) {
  138. char *Value = new char[Str - Prev + 1];
  139. memcpy(Value, Prev, Str - Prev);
  140. Value[Str - Prev] = '\0';
  141. A->getValues().push_back(Value);
  142. }
  143. if (!c)
  144. break;
  145. Prev = Str + 1;
  146. }
  147. }
  148. A->setOwnsValues(true);
  149. return A;
  150. }
  151. case SeparateClass:
  152. // Matches iff this is an exact match.
  153. // FIXME: Avoid strlen.
  154. if (ArgSize != strlen(Args.getArgString(Index)))
  155. { // HLSL Change Begin: Except if value separated by spaces here
  156. if (JoinedSpaces) {
  157. const char *Value = Args.getArgString(Index) + ArgSize + JoinedSpaces;
  158. if (*Value != '\0')
  159. return new Arg(*this, Spelling, Index++, Value);
  160. } else
  161. // HLSL Change End
  162. return nullptr;
  163. }
  164. Index += 2;
  165. if (Index > Args.getNumInputArgStrings() ||
  166. Args.getArgString(Index - 1) == nullptr)
  167. return nullptr;
  168. return new Arg(UnaliasedOption, Spelling,
  169. Index - 2, Args.getArgString(Index - 1));
  170. case MultiArgClass: {
  171. // Matches iff this is an exact match.
  172. // FIXME: Avoid strlen.
  173. if (ArgSize != strlen(Args.getArgString(Index)))
  174. return nullptr;
  175. Index += 1 + getNumArgs();
  176. if (Index > Args.getNumInputArgStrings())
  177. return nullptr;
  178. Arg *A = new Arg(UnaliasedOption, Spelling, Index - 1 - getNumArgs(),
  179. Args.getArgString(Index - getNumArgs()));
  180. for (unsigned i = 1; i != getNumArgs(); ++i)
  181. A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
  182. return A;
  183. }
  184. case JoinedOrSeparateClass: {
  185. // If this is not an exact match, it is a joined arg.
  186. // FIXME: Avoid strlen.
  187. if (ArgSize != strlen(Args.getArgString(Index))) {
  188. const char *Value = Args.getArgString(Index) + ArgSize
  189. + JoinedSpaces; // HLSL Change: Skip spaces in joined case
  190. // HLSL Change: don't interpret trailing spaces as empty value:
  191. if (*Value != '\0')
  192. return new Arg(*this, Spelling, Index++, Value);
  193. }
  194. // Otherwise it must be separate.
  195. Index += 2;
  196. if (Index > Args.getNumInputArgStrings() ||
  197. Args.getArgString(Index - 1) == nullptr)
  198. return nullptr;
  199. return new Arg(UnaliasedOption, Spelling,
  200. Index - 2, Args.getArgString(Index - 1));
  201. }
  202. case JoinedAndSeparateClass:
  203. // Always matches.
  204. Index += 2;
  205. if (Index > Args.getNumInputArgStrings() ||
  206. Args.getArgString(Index - 1) == nullptr)
  207. return nullptr;
  208. return new Arg(UnaliasedOption, Spelling, Index - 2,
  209. Args.getArgString(Index - 2) + ArgSize,
  210. Args.getArgString(Index - 1));
  211. case RemainingArgsClass: {
  212. // Matches iff this is an exact match.
  213. // FIXME: Avoid strlen.
  214. if (ArgSize != strlen(Args.getArgString(Index)))
  215. return nullptr;
  216. Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
  217. while (Index < Args.getNumInputArgStrings() &&
  218. Args.getArgString(Index) != nullptr)
  219. A->getValues().push_back(Args.getArgString(Index++));
  220. return A;
  221. }
  222. default:
  223. llvm_unreachable("Invalid option kind!");
  224. }
  225. }