ArgList.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //===- ArgList.cpp - Argument List Management -----------------------------===//
  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/ArrayRef.h"
  9. #include "llvm/ADT/SmallVector.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/ADT/Twine.h"
  13. #include "llvm/Config/llvm-config.h"
  14. #include "llvm/Option/Arg.h"
  15. #include "llvm/Option/ArgList.h"
  16. #include "llvm/Option/Option.h"
  17. #include "llvm/Option/OptSpecifier.h"
  18. #include "llvm/Support/Compiler.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include <algorithm>
  22. #include <cassert>
  23. #include <memory>
  24. #include <string>
  25. #include <utility>
  26. #include <vector>
  27. using namespace llvm;
  28. using namespace llvm::opt;
  29. void ArgList::append(Arg *A) {
  30. Args.push_back(A);
  31. // Update ranges for the option and all of its groups.
  32. for (Option O = A->getOption().getUnaliasedOption(); O.isValid();
  33. O = O.getGroup()) {
  34. auto &R =
  35. OptRanges.insert(std::make_pair(O.getID(), emptyRange())).first->second;
  36. R.first = std::min<unsigned>(R.first, Args.size() - 1);
  37. R.second = Args.size();
  38. }
  39. }
  40. void ArgList::eraseArg(OptSpecifier Id) {
  41. // Zero out the removed entries but keep them around so that we don't
  42. // need to invalidate OptRanges.
  43. for (Arg *const &A : filtered(Id)) {
  44. // Avoid the need for a non-const filtered iterator variant.
  45. Arg **ArgsBegin = Args.data();
  46. ArgsBegin[&A - ArgsBegin] = nullptr;
  47. }
  48. OptRanges.erase(Id.getID());
  49. }
  50. ArgList::OptRange
  51. ArgList::getRange(std::initializer_list<OptSpecifier> Ids) const {
  52. OptRange R = emptyRange();
  53. for (auto Id : Ids) {
  54. auto I = OptRanges.find(Id.getID());
  55. if (I != OptRanges.end()) {
  56. R.first = std::min(R.first, I->second.first);
  57. R.second = std::max(R.second, I->second.second);
  58. }
  59. }
  60. // Map an empty {-1, 0} range to {0, 0} so it can be used to form iterators.
  61. if (R.first == -1u)
  62. R.first = 0;
  63. return R;
  64. }
  65. bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
  66. if (Arg *A = getLastArg(Pos, Neg))
  67. return A->getOption().matches(Pos);
  68. return Default;
  69. }
  70. bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
  71. bool Default) const {
  72. if (Arg *A = getLastArg(Pos, PosAlias, Neg))
  73. return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);
  74. return Default;
  75. }
  76. StringRef ArgList::getLastArgValue(OptSpecifier Id, StringRef Default) const {
  77. if (Arg *A = getLastArg(Id))
  78. return A->getValue();
  79. return Default;
  80. }
  81. std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
  82. SmallVector<const char *, 16> Values;
  83. AddAllArgValues(Values, Id);
  84. return std::vector<std::string>(Values.begin(), Values.end());
  85. }
  86. void ArgList::addOptInFlag(ArgStringList &Output, OptSpecifier Pos,
  87. OptSpecifier Neg) const {
  88. if (Arg *A = getLastArg(Pos, Neg))
  89. if (A->getOption().matches(Pos))
  90. A->render(*this, Output);
  91. }
  92. void ArgList::AddAllArgsExcept(ArgStringList &Output,
  93. ArrayRef<OptSpecifier> Ids,
  94. ArrayRef<OptSpecifier> ExcludeIds) const {
  95. for (const Arg *Arg : *this) {
  96. bool Excluded = false;
  97. for (OptSpecifier Id : ExcludeIds) {
  98. if (Arg->getOption().matches(Id)) {
  99. Excluded = true;
  100. break;
  101. }
  102. }
  103. if (!Excluded) {
  104. for (OptSpecifier Id : Ids) {
  105. if (Arg->getOption().matches(Id)) {
  106. Arg->claim();
  107. Arg->render(*this, Output);
  108. break;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. /// This is a nicer interface when you don't have a list of Ids to exclude.
  115. void ArgList::AddAllArgs(ArgStringList &Output,
  116. ArrayRef<OptSpecifier> Ids) const {
  117. ArrayRef<OptSpecifier> Exclude = std::nullopt;
  118. AddAllArgsExcept(Output, Ids, Exclude);
  119. }
  120. /// This 3-opt variant of AddAllArgs could be eliminated in favor of one
  121. /// that accepts a single specifier, given the above which accepts any number.
  122. void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
  123. OptSpecifier Id1, OptSpecifier Id2) const {
  124. for (auto *Arg : filtered(Id0, Id1, Id2)) {
  125. Arg->claim();
  126. Arg->render(*this, Output);
  127. }
  128. }
  129. void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
  130. OptSpecifier Id1, OptSpecifier Id2) const {
  131. for (auto *Arg : filtered(Id0, Id1, Id2)) {
  132. Arg->claim();
  133. const auto &Values = Arg->getValues();
  134. Output.append(Values.begin(), Values.end());
  135. }
  136. }
  137. void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
  138. const char *Translation,
  139. bool Joined) const {
  140. for (auto *Arg : filtered(Id0)) {
  141. Arg->claim();
  142. if (Joined) {
  143. Output.push_back(MakeArgString(StringRef(Translation) +
  144. Arg->getValue(0)));
  145. } else {
  146. Output.push_back(Translation);
  147. Output.push_back(Arg->getValue(0));
  148. }
  149. }
  150. }
  151. void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
  152. for (auto *Arg : filtered(Id0))
  153. Arg->claim();
  154. }
  155. void ArgList::ClaimAllArgs() const {
  156. for (auto *Arg : *this)
  157. if (!Arg->isClaimed())
  158. Arg->claim();
  159. }
  160. const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
  161. StringRef LHS,
  162. StringRef RHS) const {
  163. StringRef Cur = getArgString(Index);
  164. if (Cur.size() == LHS.size() + RHS.size() &&
  165. Cur.startswith(LHS) && Cur.endswith(RHS))
  166. return Cur.data();
  167. return MakeArgString(LHS + RHS);
  168. }
  169. void ArgList::print(raw_ostream &O) const {
  170. for (Arg *A : *this) {
  171. O << "* ";
  172. A->print(O);
  173. }
  174. }
  175. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  176. LLVM_DUMP_METHOD void ArgList::dump() const { print(dbgs()); }
  177. #endif
  178. void InputArgList::releaseMemory() {
  179. // An InputArgList always owns its arguments.
  180. for (Arg *A : *this)
  181. delete A;
  182. }
  183. InputArgList::InputArgList(const char* const *ArgBegin,
  184. const char* const *ArgEnd)
  185. : NumInputArgStrings(ArgEnd - ArgBegin) {
  186. ArgStrings.append(ArgBegin, ArgEnd);
  187. }
  188. unsigned InputArgList::MakeIndex(StringRef String0) const {
  189. unsigned Index = ArgStrings.size();
  190. // Tuck away so we have a reliable const char *.
  191. SynthesizedStrings.push_back(std::string(String0));
  192. ArgStrings.push_back(SynthesizedStrings.back().c_str());
  193. return Index;
  194. }
  195. unsigned InputArgList::MakeIndex(StringRef String0,
  196. StringRef String1) const {
  197. unsigned Index0 = MakeIndex(String0);
  198. unsigned Index1 = MakeIndex(String1);
  199. assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
  200. (void) Index1;
  201. return Index0;
  202. }
  203. const char *InputArgList::MakeArgStringRef(StringRef Str) const {
  204. return getArgString(MakeIndex(Str));
  205. }
  206. DerivedArgList::DerivedArgList(const InputArgList &BaseArgs)
  207. : BaseArgs(BaseArgs) {}
  208. const char *DerivedArgList::MakeArgStringRef(StringRef Str) const {
  209. return BaseArgs.MakeArgString(Str);
  210. }
  211. void DerivedArgList::AddSynthesizedArg(Arg *A) {
  212. SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
  213. }
  214. Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
  215. SynthesizedArgs.push_back(
  216. std::make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
  217. BaseArgs.MakeIndex(Opt.getName()), BaseArg));
  218. return SynthesizedArgs.back().get();
  219. }
  220. Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
  221. StringRef Value) const {
  222. unsigned Index = BaseArgs.MakeIndex(Value);
  223. SynthesizedArgs.push_back(
  224. std::make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
  225. Index, BaseArgs.getArgString(Index), BaseArg));
  226. return SynthesizedArgs.back().get();
  227. }
  228. Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
  229. StringRef Value) const {
  230. unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
  231. SynthesizedArgs.push_back(
  232. std::make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
  233. Index, BaseArgs.getArgString(Index + 1), BaseArg));
  234. return SynthesizedArgs.back().get();
  235. }
  236. Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
  237. StringRef Value) const {
  238. unsigned Index = BaseArgs.MakeIndex((Opt.getName() + Value).str());
  239. SynthesizedArgs.push_back(std::make_unique<Arg>(
  240. Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
  241. BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
  242. return SynthesizedArgs.back().get();
  243. }