Option.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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.empty()) {
  56. O << " Prefixes:[";
  57. for (size_t I = 0, N = Info->Prefixes.size(); I != N; ++I)
  58. O << '"' << Info->Prefixes[I] << (I == N - 1 ? "\"" : "\", ");
  59. O << ']';
  60. }
  61. O << " Name:\"" << getName() << '"';
  62. const Option Group = getGroup();
  63. if (Group.isValid()) {
  64. O << " Group:";
  65. Group.print(O);
  66. }
  67. const Option Alias = getAlias();
  68. if (Alias.isValid()) {
  69. O << " Alias:";
  70. Alias.print(O);
  71. }
  72. if (getKind() == MultiArgClass)
  73. O << " NumArgs:" << getNumArgs();
  74. O << ">\n";
  75. }
  76. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  77. LLVM_DUMP_METHOD void Option::dump() const { print(dbgs()); }
  78. #endif
  79. bool Option::matches(OptSpecifier Opt) const {
  80. // Aliases are never considered in matching, look through them.
  81. const Option Alias = getAlias();
  82. if (Alias.isValid())
  83. return Alias.matches(Opt);
  84. // Check exact match.
  85. if (getID() == Opt.getID())
  86. return true;
  87. const Option Group = getGroup();
  88. if (Group.isValid())
  89. return Group.matches(Opt);
  90. return false;
  91. }
  92. std::unique_ptr<Arg> Option::acceptInternal(const ArgList &Args,
  93. 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 std::make_unique<Arg>(*this, Spelling, Index++);
  101. }
  102. case JoinedClass: {
  103. const char *Value = Args.getArgString(Index) + ArgSize;
  104. return std::make_unique<Arg>(*this, Spelling, Index++, Value);
  105. }
  106. case CommaJoinedClass: {
  107. // Always matches.
  108. const char *Str = Args.getArgString(Index) + ArgSize;
  109. auto A = std::make_unique<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 std::make_unique<Arg>(*this, Spelling, Index - 2,
  139. Args.getArgString(Index - 1));
  140. case MultiArgClass: {
  141. // Matches iff this is an exact match.
  142. // FIXME: Avoid strlen.
  143. if (ArgSize != strlen(Args.getArgString(Index)))
  144. return nullptr;
  145. Index += 1 + getNumArgs();
  146. if (Index > Args.getNumInputArgStrings())
  147. return nullptr;
  148. auto A = std::make_unique<Arg>(*this, Spelling, Index - 1 - getNumArgs(),
  149. Args.getArgString(Index - getNumArgs()));
  150. for (unsigned i = 1; i != getNumArgs(); ++i)
  151. A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
  152. return A;
  153. }
  154. case JoinedOrSeparateClass: {
  155. // If this is not an exact match, it is a joined arg.
  156. // FIXME: Avoid strlen.
  157. if (ArgSize != strlen(Args.getArgString(Index))) {
  158. const char *Value = Args.getArgString(Index) + ArgSize;
  159. return std::make_unique<Arg>(*this, Spelling, Index++, Value);
  160. }
  161. // Otherwise it must be separate.
  162. Index += 2;
  163. if (Index > Args.getNumInputArgStrings() ||
  164. Args.getArgString(Index - 1) == nullptr)
  165. return nullptr;
  166. return std::make_unique<Arg>(*this, Spelling, Index - 2,
  167. Args.getArgString(Index - 1));
  168. }
  169. case JoinedAndSeparateClass:
  170. // Always matches.
  171. Index += 2;
  172. if (Index > Args.getNumInputArgStrings() ||
  173. Args.getArgString(Index - 1) == nullptr)
  174. return nullptr;
  175. return std::make_unique<Arg>(*this, Spelling, Index - 2,
  176. Args.getArgString(Index - 2) + ArgSize,
  177. Args.getArgString(Index - 1));
  178. case RemainingArgsClass: {
  179. // Matches iff this is an exact match.
  180. // FIXME: Avoid strlen.
  181. if (ArgSize != strlen(Args.getArgString(Index)))
  182. return nullptr;
  183. auto A = std::make_unique<Arg>(*this, Spelling, Index++);
  184. while (Index < Args.getNumInputArgStrings() &&
  185. Args.getArgString(Index) != nullptr)
  186. A->getValues().push_back(Args.getArgString(Index++));
  187. return A;
  188. }
  189. case RemainingArgsJoinedClass: {
  190. auto A = std::make_unique<Arg>(*this, Spelling, Index);
  191. if (ArgSize != strlen(Args.getArgString(Index))) {
  192. // An inexact match means there is a joined arg.
  193. A->getValues().push_back(Args.getArgString(Index) + ArgSize);
  194. }
  195. 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. }
  205. std::unique_ptr<Arg> Option::accept(const ArgList &Args, StringRef CurArg,
  206. bool GroupedShortOption,
  207. unsigned &Index) const {
  208. auto A(GroupedShortOption && getKind() == FlagClass
  209. ? std::make_unique<Arg>(*this, CurArg, Index)
  210. : acceptInternal(Args, CurArg, Index));
  211. if (!A)
  212. return nullptr;
  213. const Option &UnaliasedOption = getUnaliasedOption();
  214. if (getID() == UnaliasedOption.getID())
  215. return A;
  216. // "A" is an alias for a different flag. For most clients it's more convenient
  217. // if this function returns unaliased Args, so create an unaliased arg for
  218. // returning.
  219. // This creates a completely new Arg object for the unaliased Arg because
  220. // the alias and the unaliased arg can have different Kinds and different
  221. // Values (due to AliasArgs<>).
  222. // Get the spelling from the unaliased option.
  223. StringRef UnaliasedSpelling = Args.MakeArgString(
  224. Twine(UnaliasedOption.getPrefix()) + Twine(UnaliasedOption.getName()));
  225. // It's a bit weird that aliased and unaliased arg share one index, but
  226. // the index is mostly use as a memory optimization in render().
  227. // Due to this, ArgList::getArgString(A->getIndex()) will return the spelling
  228. // of the aliased arg always, while A->getSpelling() returns either the
  229. // unaliased or the aliased arg, depending on which Arg object it's called on.
  230. auto UnaliasedA =
  231. std::make_unique<Arg>(UnaliasedOption, UnaliasedSpelling, A->getIndex());
  232. Arg *RawA = A.get();
  233. UnaliasedA->setAlias(std::move(A));
  234. if (getKind() != FlagClass) {
  235. // Values are usually owned by the ArgList. The exception are
  236. // CommaJoined flags, where the Arg owns the values. For aliased flags,
  237. // make the unaliased Arg the owner of the values.
  238. // FIXME: There aren't many uses of CommaJoined -- try removing
  239. // CommaJoined in favor of just calling StringRef::split(',') instead.
  240. UnaliasedA->getValues() = RawA->getValues();
  241. UnaliasedA->setOwnsValues(RawA->getOwnsValues());
  242. RawA->setOwnsValues(false);
  243. return UnaliasedA;
  244. }
  245. // FlagClass aliases can have AliasArgs<>; add those to the unaliased arg.
  246. if (const char *Val = getAliasArgs()) {
  247. while (*Val != '\0') {
  248. UnaliasedA->getValues().push_back(Val);
  249. // Move past the '\0' to the next argument.
  250. Val += strlen(Val) + 1;
  251. }
  252. }
  253. if (UnaliasedOption.getKind() == JoinedClass && !getAliasArgs())
  254. // A Flag alias for a Joined option must provide an argument.
  255. UnaliasedA->getValues().push_back("");
  256. return UnaliasedA;
  257. }