Option.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //===- Option.cpp - Abstract Driver Options -------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/ADT/StringRef.h"
  9. #include "llvm/ADT/Twine.h"
  10. #include "llvm/Config/llvm-config.h"
  11. #include "llvm/Option/Arg.h"
  12. #include "llvm/Option/ArgList.h"
  13. #include "llvm/Option/Option.h"
  14. #include "llvm/Option/OptTable.h"
  15. #include "llvm/Support/Compiler.h"
  16. #include "llvm/Support/Debug.h"
  17. #include "llvm/Support/ErrorHandling.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. #include <cassert>
  20. #include <cstring>
  21. using namespace llvm;
  22. using namespace llvm::opt;
  23. Option::Option(const OptTable::Info *info, const OptTable *owner)
  24. : Info(info), Owner(owner) {
  25. // Multi-level aliases are not supported. This just simplifies option
  26. // tracking, it is not an inherent limitation.
  27. assert((!Info || !getAlias().isValid() || !getAlias().getAlias().isValid()) &&
  28. "Multi-level aliases are not supported.");
  29. if (Info && getAliasArgs()) {
  30. assert(getAlias().isValid() && "Only alias options can have alias args.");
  31. assert(getKind() == FlagClass && "Only Flag aliases can have alias args.");
  32. assert(getAlias().getKind() != FlagClass &&
  33. "Cannot provide alias args to a flag option.");
  34. }
  35. }
  36. void Option::print(raw_ostream &O) const {
  37. O << "<";
  38. switch (getKind()) {
  39. #define P(N) case N: O << #N; break
  40. P(GroupClass);
  41. P(InputClass);
  42. P(UnknownClass);
  43. P(FlagClass);
  44. P(JoinedClass);
  45. P(ValuesClass);
  46. P(SeparateClass);
  47. P(CommaJoinedClass);
  48. P(MultiArgClass);
  49. P(JoinedOrSeparateClass);
  50. P(JoinedAndSeparateClass);
  51. P(RemainingArgsClass);
  52. P(RemainingArgsJoinedClass);
  53. #undef P
  54. }
  55. if (Info->Prefixes) {
  56. O << " Prefixes:[";
  57. for (const char *const *Pre = Info->Prefixes; *Pre != nullptr; ++Pre) {
  58. O << '"' << *Pre << (*(Pre + 1) == nullptr ? "\"" : "\", ");
  59. }
  60. O << ']';
  61. }
  62. O << " Name:\"" << getName() << '"';
  63. const Option Group = getGroup();
  64. if (Group.isValid()) {
  65. O << " Group:";
  66. Group.print(O);
  67. }
  68. const Option Alias = getAlias();
  69. if (Alias.isValid()) {
  70. O << " Alias:";
  71. Alias.print(O);
  72. }
  73. if (getKind() == MultiArgClass)
  74. O << " NumArgs:" << getNumArgs();
  75. O << ">\n";
  76. }
  77. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  78. LLVM_DUMP_METHOD void Option::dump() const { print(dbgs()); }
  79. #endif
  80. bool Option::matches(OptSpecifier Opt) const {
  81. // Aliases are never considered in matching, look through them.
  82. const Option Alias = getAlias();
  83. if (Alias.isValid())
  84. return Alias.matches(Opt);
  85. // Check exact match.
  86. if (getID() == Opt.getID())
  87. return true;
  88. const Option Group = getGroup();
  89. if (Group.isValid())
  90. return Group.matches(Opt);
  91. return false;
  92. }
  93. Arg *Option::acceptInternal(const ArgList &Args, StringRef Spelling,
  94. unsigned &Index) const {
  95. size_t ArgSize = Spelling.size();
  96. switch (getKind()) {
  97. case FlagClass: {
  98. if (ArgSize != strlen(Args.getArgString(Index)))
  99. return nullptr;
  100. return new Arg(*this, Spelling, Index++);
  101. }
  102. case JoinedClass: {
  103. const char *Value = Args.getArgString(Index) + ArgSize;
  104. return new Arg(*this, Spelling, Index++, Value);
  105. }
  106. case CommaJoinedClass: {
  107. // Always matches.
  108. const char *Str = Args.getArgString(Index) + ArgSize;
  109. Arg *A = new Arg(*this, Spelling, Index++);
  110. // Parse out the comma separated values.
  111. const char *Prev = Str;
  112. for (;; ++Str) {
  113. char c = *Str;
  114. if (!c || c == ',') {
  115. if (Prev != Str) {
  116. char *Value = new char[Str - Prev + 1];
  117. memcpy(Value, Prev, Str - Prev);
  118. Value[Str - Prev] = '\0';
  119. A->getValues().push_back(Value);
  120. }
  121. if (!c)
  122. break;
  123. Prev = Str + 1;
  124. }
  125. }
  126. A->setOwnsValues(true);
  127. return A;
  128. }
  129. case SeparateClass:
  130. // Matches iff this is an exact match.
  131. // FIXME: Avoid strlen.
  132. if (ArgSize != strlen(Args.getArgString(Index)))
  133. return nullptr;
  134. Index += 2;
  135. if (Index > Args.getNumInputArgStrings() ||
  136. Args.getArgString(Index - 1) == nullptr)
  137. return nullptr;
  138. return new Arg(*this, Spelling, Index - 2, Args.getArgString(Index - 1));
  139. case MultiArgClass: {
  140. // Matches iff this is an exact match.
  141. // FIXME: Avoid strlen.
  142. if (ArgSize != strlen(Args.getArgString(Index)))
  143. return nullptr;
  144. Index += 1 + getNumArgs();
  145. if (Index > Args.getNumInputArgStrings())
  146. return nullptr;
  147. Arg *A = new Arg(*this, Spelling, Index - 1 - getNumArgs(),
  148. Args.getArgString(Index - getNumArgs()));
  149. for (unsigned i = 1; i != getNumArgs(); ++i)
  150. A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
  151. return A;
  152. }
  153. case JoinedOrSeparateClass: {
  154. // If this is not an exact match, it is a joined arg.
  155. // FIXME: Avoid strlen.
  156. if (ArgSize != strlen(Args.getArgString(Index))) {
  157. const char *Value = Args.getArgString(Index) + ArgSize;
  158. return new Arg(*this, Spelling, Index++, Value);
  159. }
  160. // Otherwise it must be separate.
  161. Index += 2;
  162. if (Index > Args.getNumInputArgStrings() ||
  163. Args.getArgString(Index - 1) == nullptr)
  164. return nullptr;
  165. return new Arg(*this, Spelling, Index - 2, Args.getArgString(Index - 1));
  166. }
  167. case JoinedAndSeparateClass:
  168. // Always matches.
  169. Index += 2;
  170. if (Index > Args.getNumInputArgStrings() ||
  171. Args.getArgString(Index - 1) == nullptr)
  172. return nullptr;
  173. return new Arg(*this, Spelling, Index - 2,
  174. Args.getArgString(Index - 2) + ArgSize,
  175. Args.getArgString(Index - 1));
  176. case RemainingArgsClass: {
  177. // Matches iff this is an exact match.
  178. // FIXME: Avoid strlen.
  179. if (ArgSize != strlen(Args.getArgString(Index)))
  180. return nullptr;
  181. Arg *A = new Arg(*this, Spelling, Index++);
  182. while (Index < Args.getNumInputArgStrings() &&
  183. Args.getArgString(Index) != nullptr)
  184. A->getValues().push_back(Args.getArgString(Index++));
  185. return A;
  186. }
  187. case RemainingArgsJoinedClass: {
  188. Arg *A = new Arg(*this, Spelling, Index);
  189. if (ArgSize != strlen(Args.getArgString(Index))) {
  190. // An inexact match means there is a joined arg.
  191. A->getValues().push_back(Args.getArgString(Index) + ArgSize);
  192. }
  193. Index++;
  194. while (Index < Args.getNumInputArgStrings() &&
  195. Args.getArgString(Index) != nullptr)
  196. A->getValues().push_back(Args.getArgString(Index++));
  197. return A;
  198. }
  199. default:
  200. llvm_unreachable("Invalid option kind!");
  201. }
  202. }
  203. Arg *Option::accept(const ArgList &Args, StringRef CurArg,
  204. bool GroupedShortOption, unsigned &Index) const {
  205. std::unique_ptr<Arg> A(GroupedShortOption && getKind() == FlagClass
  206. ? new Arg(*this, CurArg, Index)
  207. : acceptInternal(Args, CurArg, Index));
  208. if (!A)
  209. return nullptr;
  210. const Option &UnaliasedOption = getUnaliasedOption();
  211. if (getID() == UnaliasedOption.getID())
  212. return A.release();
  213. // "A" is an alias for a different flag. For most clients it's more convenient
  214. // if this function returns unaliased Args, so create an unaliased arg for
  215. // returning.
  216. // This creates a completely new Arg object for the unaliased Arg because
  217. // the alias and the unaliased arg can have different Kinds and different
  218. // Values (due to AliasArgs<>).
  219. // Get the spelling from the unaliased option.
  220. StringRef UnaliasedSpelling = Args.MakeArgString(
  221. Twine(UnaliasedOption.getPrefix()) + Twine(UnaliasedOption.getName()));
  222. // It's a bit weird that aliased and unaliased arg share one index, but
  223. // the index is mostly use as a memory optimization in render().
  224. // Due to this, ArgList::getArgString(A->getIndex()) will return the spelling
  225. // of the aliased arg always, while A->getSpelling() returns either the
  226. // unaliased or the aliased arg, depending on which Arg object it's called on.
  227. Arg *UnaliasedA = new Arg(UnaliasedOption, UnaliasedSpelling, A->getIndex());
  228. Arg *RawA = A.get();
  229. UnaliasedA->setAlias(std::move(A));
  230. if (getKind() != FlagClass) {
  231. // Values are usually owned by the ArgList. The exception are
  232. // CommaJoined flags, where the Arg owns the values. For aliased flags,
  233. // make the unaliased Arg the owner of the values.
  234. // FIXME: There aren't many uses of CommaJoined -- try removing
  235. // CommaJoined in favor of just calling StringRef::split(',') instead.
  236. UnaliasedA->getValues() = RawA->getValues();
  237. UnaliasedA->setOwnsValues(RawA->getOwnsValues());
  238. RawA->setOwnsValues(false);
  239. return UnaliasedA;
  240. }
  241. // FlagClass aliases can have AliasArgs<>; add those to the unaliased arg.
  242. if (const char *Val = getAliasArgs()) {
  243. while (*Val != '\0') {
  244. UnaliasedA->getValues().push_back(Val);
  245. // Move past the '\0' to the next argument.
  246. Val += strlen(Val) + 1;
  247. }
  248. }
  249. if (UnaliasedOption.getKind() == JoinedClass && !getAliasArgs())
  250. // A Flag alias for a Joined option must provide an argument.
  251. UnaliasedA->getValues().push_back("");
  252. return UnaliasedA;
  253. }