ArgList.cpp 8.6 KB

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