ArgList.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. //===--- ArgList.cpp - Argument List Management ---------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/Driver/ArgList.h"
  10. #include "clang/Driver/Arg.h"
  11. #include "clang/Driver/DriverDiagnostic.h"
  12. #include "clang/Driver/Option.h"
  13. #include "llvm/ADT/SmallString.h"
  14. #include "llvm/ADT/Twine.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. using namespace clang;
  17. using namespace clang::driver;
  18. void arg_iterator::SkipToNextArg() {
  19. for (; Current != Args.end(); ++Current) {
  20. // Done if there are no filters.
  21. if (!Id0.isValid())
  22. break;
  23. // Otherwise require a match.
  24. const Option &O = (*Current)->getOption();
  25. if (O.matches(Id0) ||
  26. (Id1.isValid() && O.matches(Id1)) ||
  27. (Id2.isValid() && O.matches(Id2)))
  28. break;
  29. }
  30. }
  31. //
  32. ArgList::ArgList() {
  33. }
  34. ArgList::~ArgList() {
  35. }
  36. void ArgList::append(Arg *A) {
  37. Args.push_back(A);
  38. }
  39. void ArgList::eraseArg(OptSpecifier Id) {
  40. for (iterator it = begin(), ie = end(); it != ie; ) {
  41. if ((*it)->getOption().matches(Id)) {
  42. it = Args.erase(it);
  43. ie = end();
  44. } else {
  45. ++it;
  46. }
  47. }
  48. }
  49. Arg *ArgList::getLastArgNoClaim(OptSpecifier Id) const {
  50. // FIXME: Make search efficient?
  51. for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it)
  52. if ((*it)->getOption().matches(Id))
  53. return *it;
  54. return 0;
  55. }
  56. Arg *ArgList::getLastArg(OptSpecifier Id) const {
  57. Arg *Res = 0;
  58. for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
  59. if ((*it)->getOption().matches(Id)) {
  60. Res = *it;
  61. Res->claim();
  62. }
  63. }
  64. return Res;
  65. }
  66. Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1) const {
  67. Arg *Res = 0;
  68. for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
  69. if ((*it)->getOption().matches(Id0) ||
  70. (*it)->getOption().matches(Id1)) {
  71. Res = *it;
  72. Res->claim();
  73. }
  74. }
  75. return Res;
  76. }
  77. Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
  78. OptSpecifier Id2) const {
  79. Arg *Res = 0;
  80. for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
  81. if ((*it)->getOption().matches(Id0) ||
  82. (*it)->getOption().matches(Id1) ||
  83. (*it)->getOption().matches(Id2)) {
  84. Res = *it;
  85. Res->claim();
  86. }
  87. }
  88. return Res;
  89. }
  90. Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
  91. OptSpecifier Id2, OptSpecifier Id3) const {
  92. Arg *Res = 0;
  93. for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
  94. if ((*it)->getOption().matches(Id0) ||
  95. (*it)->getOption().matches(Id1) ||
  96. (*it)->getOption().matches(Id2) ||
  97. (*it)->getOption().matches(Id3)) {
  98. Res = *it;
  99. Res->claim();
  100. }
  101. }
  102. return Res;
  103. }
  104. Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1,
  105. OptSpecifier Id2, OptSpecifier Id3,
  106. OptSpecifier Id4) const {
  107. Arg *Res = 0;
  108. for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
  109. if ((*it)->getOption().matches(Id0) ||
  110. (*it)->getOption().matches(Id1) ||
  111. (*it)->getOption().matches(Id2) ||
  112. (*it)->getOption().matches(Id3) ||
  113. (*it)->getOption().matches(Id4)) {
  114. Res = *it;
  115. Res->claim();
  116. }
  117. }
  118. return Res;
  119. }
  120. bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
  121. if (Arg *A = getLastArg(Pos, Neg))
  122. return A->getOption().matches(Pos);
  123. return Default;
  124. }
  125. StringRef ArgList::getLastArgValue(OptSpecifier Id,
  126. StringRef Default) const {
  127. if (Arg *A = getLastArg(Id))
  128. return A->getValue(*this);
  129. return Default;
  130. }
  131. int ArgList::getLastArgIntValue(OptSpecifier Id, int Default,
  132. clang::DiagnosticsEngine &Diags) const {
  133. int Res = Default;
  134. if (Arg *A = getLastArg(Id)) {
  135. if (StringRef(A->getValue(*this)).getAsInteger(10, Res))
  136. Diags.Report(diag::err_drv_invalid_int_value)
  137. << A->getAsString(*this) << A->getValue(*this);
  138. }
  139. return Res;
  140. }
  141. std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
  142. SmallVector<const char *, 16> Values;
  143. AddAllArgValues(Values, Id);
  144. return std::vector<std::string>(Values.begin(), Values.end());
  145. }
  146. void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
  147. if (Arg *A = getLastArg(Id)) {
  148. A->claim();
  149. A->render(*this, Output);
  150. }
  151. }
  152. void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
  153. OptSpecifier Id1, OptSpecifier Id2) const {
  154. for (arg_iterator it = filtered_begin(Id0, Id1, Id2),
  155. ie = filtered_end(); it != ie; ++it) {
  156. (*it)->claim();
  157. (*it)->render(*this, Output);
  158. }
  159. }
  160. void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
  161. OptSpecifier Id1, OptSpecifier Id2) const {
  162. for (arg_iterator it = filtered_begin(Id0, Id1, Id2),
  163. ie = filtered_end(); it != ie; ++it) {
  164. (*it)->claim();
  165. for (unsigned i = 0, e = (*it)->getNumValues(); i != e; ++i)
  166. Output.push_back((*it)->getValue(*this, i));
  167. }
  168. }
  169. void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
  170. const char *Translation,
  171. bool Joined) const {
  172. for (arg_iterator it = filtered_begin(Id0),
  173. ie = filtered_end(); it != ie; ++it) {
  174. (*it)->claim();
  175. if (Joined) {
  176. Output.push_back(MakeArgString(StringRef(Translation) +
  177. (*it)->getValue(*this, 0)));
  178. } else {
  179. Output.push_back(Translation);
  180. Output.push_back((*it)->getValue(*this, 0));
  181. }
  182. }
  183. }
  184. void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
  185. for (arg_iterator it = filtered_begin(Id0),
  186. ie = filtered_end(); it != ie; ++it)
  187. (*it)->claim();
  188. }
  189. void ArgList::ClaimAllArgs() const {
  190. for (const_iterator it = begin(), ie = end(); it != ie; ++it)
  191. if (!(*it)->isClaimed())
  192. (*it)->claim();
  193. }
  194. const char *ArgList::MakeArgString(const Twine &T) const {
  195. SmallString<256> Str;
  196. T.toVector(Str);
  197. return MakeArgString(Str.str());
  198. }
  199. const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
  200. StringRef LHS,
  201. StringRef RHS) const {
  202. StringRef Cur = getArgString(Index);
  203. if (Cur.size() == LHS.size() + RHS.size() &&
  204. Cur.startswith(LHS) && Cur.endswith(RHS))
  205. return Cur.data();
  206. return MakeArgString(LHS + RHS);
  207. }
  208. //
  209. InputArgList::InputArgList(const char* const *ArgBegin,
  210. const char* const *ArgEnd)
  211. : NumInputArgStrings(ArgEnd - ArgBegin) {
  212. ArgStrings.append(ArgBegin, ArgEnd);
  213. }
  214. InputArgList::~InputArgList() {
  215. // An InputArgList always owns its arguments.
  216. for (iterator it = begin(), ie = end(); it != ie; ++it)
  217. delete *it;
  218. }
  219. unsigned InputArgList::MakeIndex(StringRef String0) const {
  220. unsigned Index = ArgStrings.size();
  221. // Tuck away so we have a reliable const char *.
  222. SynthesizedStrings.push_back(String0);
  223. ArgStrings.push_back(SynthesizedStrings.back().c_str());
  224. return Index;
  225. }
  226. unsigned InputArgList::MakeIndex(StringRef String0,
  227. StringRef String1) const {
  228. unsigned Index0 = MakeIndex(String0);
  229. unsigned Index1 = MakeIndex(String1);
  230. assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
  231. (void) Index1;
  232. return Index0;
  233. }
  234. const char *InputArgList::MakeArgString(StringRef Str) const {
  235. return getArgString(MakeIndex(Str));
  236. }
  237. //
  238. DerivedArgList::DerivedArgList(const InputArgList &_BaseArgs)
  239. : BaseArgs(_BaseArgs) {
  240. }
  241. DerivedArgList::~DerivedArgList() {
  242. // We only own the arguments we explicitly synthesized.
  243. for (iterator it = SynthesizedArgs.begin(), ie = SynthesizedArgs.end();
  244. it != ie; ++it)
  245. delete *it;
  246. }
  247. const char *DerivedArgList::MakeArgString(StringRef Str) const {
  248. return BaseArgs.MakeArgString(Str);
  249. }
  250. Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option *Opt) const {
  251. Arg *A = new Arg(Opt, BaseArgs.MakeIndex(Opt->getName()), BaseArg);
  252. SynthesizedArgs.push_back(A);
  253. return A;
  254. }
  255. Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option *Opt,
  256. StringRef Value) const {
  257. unsigned Index = BaseArgs.MakeIndex(Value);
  258. Arg *A = new Arg(Opt, Index, BaseArgs.getArgString(Index), BaseArg);
  259. SynthesizedArgs.push_back(A);
  260. return A;
  261. }
  262. Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option *Opt,
  263. StringRef Value) const {
  264. unsigned Index = BaseArgs.MakeIndex(Opt->getName(), Value);
  265. Arg *A = new Arg(Opt, Index, BaseArgs.getArgString(Index + 1), BaseArg);
  266. SynthesizedArgs.push_back(A);
  267. return A;
  268. }
  269. Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option *Opt,
  270. StringRef Value) const {
  271. unsigned Index = BaseArgs.MakeIndex(Opt->getName().str() + Value.str());
  272. Arg *A = new Arg(Opt, Index,
  273. BaseArgs.getArgString(Index) + Opt->getName().size(),
  274. BaseArg);
  275. SynthesizedArgs.push_back(A);
  276. return A;
  277. }