Arg.cpp 3.3 KB

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