Arg.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //===--- Arg.cpp - Argument Implementations -------------------------------===//
  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/Arg.h"
  10. #include "clang/Driver/ArgList.h"
  11. #include "clang/Driver/Option.h"
  12. #include "llvm/ADT/SmallString.h"
  13. #include "llvm/ADT/Twine.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. using namespace clang::driver;
  16. Arg::Arg(const Option *_Opt, unsigned _Index, const Arg *_BaseArg)
  17. : Opt(_Opt), BaseArg(_BaseArg), Index(_Index),
  18. Claimed(false), OwnsValues(false) {
  19. }
  20. Arg::Arg(const Option *_Opt, unsigned _Index,
  21. const char *Value0, const Arg *_BaseArg)
  22. : Opt(_Opt), BaseArg(_BaseArg), Index(_Index),
  23. Claimed(false), OwnsValues(false) {
  24. Values.push_back(Value0);
  25. }
  26. Arg::Arg(const Option *_Opt, unsigned _Index,
  27. const char *Value0, const char *Value1, const Arg *_BaseArg)
  28. : Opt(_Opt), BaseArg(_BaseArg), Index(_Index),
  29. Claimed(false), OwnsValues(false) {
  30. Values.push_back(Value0);
  31. Values.push_back(Value1);
  32. }
  33. Arg::~Arg() {
  34. if (OwnsValues) {
  35. for (unsigned i = 0, e = Values.size(); i != e; ++i)
  36. delete[] Values[i];
  37. }
  38. }
  39. void Arg::dump() const {
  40. llvm::errs() << "<";
  41. llvm::errs() << " Opt:";
  42. Opt->dump();
  43. llvm::errs() << " Index:" << Index;
  44. llvm::errs() << " Values: [";
  45. for (unsigned i = 0, e = Values.size(); i != e; ++i) {
  46. if (i) llvm::errs() << ", ";
  47. llvm::errs() << "'" << Values[i] << "'";
  48. }
  49. llvm::errs() << "]>\n";
  50. }
  51. std::string Arg::getAsString(const ArgList &Args) const {
  52. SmallString<256> Res;
  53. llvm::raw_svector_ostream OS(Res);
  54. ArgStringList ASL;
  55. render(Args, ASL);
  56. for (ArgStringList::iterator
  57. it = ASL.begin(), ie = ASL.end(); it != ie; ++it) {
  58. if (it != ASL.begin())
  59. OS << ' ';
  60. OS << *it;
  61. }
  62. return OS.str();
  63. }
  64. void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const {
  65. if (!getOption().hasNoOptAsInput()) {
  66. render(Args, Output);
  67. return;
  68. }
  69. for (unsigned i = 0, e = getNumValues(); i != e; ++i)
  70. Output.push_back(getValue(Args, i));
  71. }
  72. void Arg::render(const ArgList &Args, ArgStringList &Output) const {
  73. switch (getOption().getRenderStyle()) {
  74. case Option::RenderValuesStyle:
  75. for (unsigned i = 0, e = getNumValues(); i != e; ++i)
  76. Output.push_back(getValue(Args, i));
  77. break;
  78. case Option::RenderCommaJoinedStyle: {
  79. SmallString<256> Res;
  80. llvm::raw_svector_ostream OS(Res);
  81. OS << getOption().getName();
  82. for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
  83. if (i) OS << ',';
  84. OS << getValue(Args, i);
  85. }
  86. Output.push_back(Args.MakeArgString(OS.str()));
  87. break;
  88. }
  89. case Option::RenderJoinedStyle:
  90. Output.push_back(Args.GetOrMakeJoinedArgString(
  91. getIndex(), getOption().getName(), getValue(Args, 0)));
  92. for (unsigned i = 1, e = getNumValues(); i != e; ++i)
  93. Output.push_back(getValue(Args, i));
  94. break;
  95. case Option::RenderSeparateStyle:
  96. Output.push_back(getOption().getName().data());
  97. for (unsigned i = 0, e = getNumValues(); i != e; ++i)
  98. Output.push_back(getValue(Args, i));
  99. break;
  100. }
  101. }