Option.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. std::unique_ptr<Arg> Option::acceptInternal(const ArgList &Args,
  94. StringRef Spelling,
  95. unsigned &Index) const {
  96. size_t ArgSize = Spelling.size();
  97. switch (getKind()) {
  98. case FlagClass: {
  99. if (ArgSize != strlen(Args.getArgString(Index)))
  100. return nullptr;
  101. return std::make_unique<Arg>(*this, Spelling, Index++);
  102. }
  103. case JoinedClass: {
  104. const char *Value = Args.getArgString(Index) + ArgSize;
  105. return std::make_unique<Arg>(*this, Spelling, Index++, Value);
  106. }
  107. case CommaJoinedClass: {
  108. // Always matches.
  109. const char *Str = Args.getArgString(Index) + ArgSize;
  110. auto A = std::make_unique<Arg>(*this, Spelling, Index++);
  111. // Parse out the comma separated values.
  112. const char *Prev = Str;
  113. for (;; ++Str) {
  114. char c = *Str;
  115. if (!c || c == ',') {
  116. if (Prev != Str) {
  117. char *Value = new char[Str - Prev + 1];
  118. memcpy(Value, Prev, Str - Prev);
  119. Value[Str - Prev] = '\0';
  120. A->getValues().push_back(Value);
  121. }
  122. if (!c)
  123. break;
  124. Prev = Str + 1;
  125. }
  126. }
  127. A->setOwnsValues(true);
  128. return A;
  129. }
  130. case SeparateClass:
  131. // Matches iff this is an exact match.
  132. // FIXME: Avoid strlen.
  133. if (ArgSize != strlen(Args.getArgString(Index)))
  134. return nullptr;
  135. Index += 2;
  136. if (Index > Args.getNumInputArgStrings() ||
  137. Args.getArgString(Index - 1) == nullptr)
  138. return nullptr;
  139. return std::make_unique<Arg>(*this, Spelling, Index - 2,
  140. Args.getArgString(Index - 1));
  141. case MultiArgClass: {
  142. // Matches iff this is an exact match.
  143. // FIXME: Avoid strlen.
  144. if (ArgSize != strlen(Args.getArgString(Index)))
  145. return nullptr;
  146. Index += 1 + getNumArgs();
  147. if (Index > Args.getNumInputArgStrings())
  148. return nullptr;
  149. auto A = std::make_unique<Arg>(*this, Spelling, Index - 1 - getNumArgs(),
  150. Args.getArgString(Index - getNumArgs()));
  151. for (unsigned i = 1; i != getNumArgs(); ++i)
  152. A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
  153. return A;
  154. }
  155. case JoinedOrSeparateClass: {
  156. // If this is not an exact match, it is a joined arg.
  157. // FIXME: Avoid strlen.
  158. if (ArgSize != strlen(Args.getArgString(Index))) {
  159. const char *Value = Args.getArgString(Index) + ArgSize;
  160. return std::make_unique<Arg>(*this, Spelling, Index++, Value);
  161. }
  162. // Otherwise it must be separate.
  163. Index += 2;
  164. if (Index > Args.getNumInputArgStrings() ||
  165. Args.getArgString(Index - 1) == nullptr)
  166. return nullptr;
  167. return std::make_unique<Arg>(*this, Spelling, Index - 2,
  168. Args.getArgString(Index - 1));
  169. }
  170. case JoinedAndSeparateClass:
  171. // Always matches.
  172. Index += 2;
  173. if (Index > Args.getNumInputArgStrings() ||
  174. Args.getArgString(Index - 1) == nullptr)
  175. return nullptr;
  176. return std::make_unique<Arg>(*this, Spelling, Index - 2,
  177. Args.getArgString(Index - 2) + ArgSize,
  178. Args.getArgString(Index - 1));
  179. case RemainingArgsClass: {
  180. // Matches iff this is an exact match.
  181. // FIXME: Avoid strlen.
  182. if (ArgSize != strlen(Args.getArgString(Index)))
  183. return nullptr;
  184. auto A = std::make_unique<Arg>(*this, Spelling, Index++);
  185. while (Index < Args.getNumInputArgStrings() &&
  186. Args.getArgString(Index) != nullptr)
  187. A->getValues().push_back(Args.getArgString(Index++));
  188. return A;
  189. }
  190. case RemainingArgsJoinedClass: {
  191. auto A = std::make_unique<Arg>(*this, Spelling, Index);
  192. if (ArgSize != strlen(Args.getArgString(Index))) {
  193. // An inexact match means there is a joined arg.
  194. A->getValues().push_back(Args.getArgString(Index) + ArgSize);
  195. }
  196. Index++;
  197. while (Index < Args.getNumInputArgStrings() &&
  198. Args.getArgString(Index) != nullptr)
  199. A->getValues().push_back(Args.getArgString(Index++));
  200. return A;
  201. }
  202. default:
  203. llvm_unreachable("Invalid option kind!");
  204. }
  205. }
  206. std::unique_ptr<Arg> Option::accept(const ArgList &Args, StringRef CurArg,
  207. bool GroupedShortOption,
  208. unsigned &Index) const {
  209. auto A(GroupedShortOption && getKind() == FlagClass
  210. ? std::make_unique<Arg>(*this, CurArg, Index)
  211. : acceptInternal(Args, CurArg, Index));
  212. if (!A)
  213. return nullptr;
  214. const Option &UnaliasedOption = getUnaliasedOption();
  215. if (getID() == UnaliasedOption.getID())
  216. return A;
  217. // "A" is an alias for a different flag. For most clients it's more convenient
  218. // if this function returns unaliased Args, so create an unaliased arg for
  219. // returning.
  220. // This creates a completely new Arg object for the unaliased Arg because
  221. // the alias and the unaliased arg can have different Kinds and different
  222. // Values (due to AliasArgs<>).
  223. // Get the spelling from the unaliased option.
  224. StringRef UnaliasedSpelling = Args.MakeArgString(
  225. Twine(UnaliasedOption.getPrefix()) + Twine(UnaliasedOption.getName()));
  226. // It's a bit weird that aliased and unaliased arg share one index, but
  227. // the index is mostly use as a memory optimization in render().
  228. // Due to this, ArgList::getArgString(A->getIndex()) will return the spelling
  229. // of the aliased arg always, while A->getSpelling() returns either the
  230. // unaliased or the aliased arg, depending on which Arg object it's called on.
  231. auto UnaliasedA =
  232. std::make_unique<Arg>(UnaliasedOption, UnaliasedSpelling, A->getIndex());
  233. Arg *RawA = A.get();
  234. UnaliasedA->setAlias(std::move(A));
  235. if (getKind() != FlagClass) {
  236. // Values are usually owned by the ArgList. The exception are
  237. // CommaJoined flags, where the Arg owns the values. For aliased flags,
  238. // make the unaliased Arg the owner of the values.
  239. // FIXME: There aren't many uses of CommaJoined -- try removing
  240. // CommaJoined in favor of just calling StringRef::split(',') instead.
  241. UnaliasedA->getValues() = RawA->getValues();
  242. UnaliasedA->setOwnsValues(RawA->getOwnsValues());
  243. RawA->setOwnsValues(false);
  244. return UnaliasedA;
  245. }
  246. // FlagClass aliases can have AliasArgs<>; add those to the unaliased arg.
  247. if (const char *Val = getAliasArgs()) {
  248. while (*Val != '\0') {
  249. UnaliasedA->getValues().push_back(Val);
  250. // Move past the '\0' to the next argument.
  251. Val += strlen(Val) + 1;
  252. }
  253. }
  254. if (UnaliasedOption.getKind() == JoinedClass && !getAliasArgs())
  255. // A Flag alias for a Joined option must provide an argument.
  256. UnaliasedA->getValues().push_back("");
  257. return UnaliasedA;
  258. }