Arg.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //===--- Arg.h - Parsed Argument Classes ------------------------*- C++ -*-===//
  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. ///
  10. /// \file
  11. /// \brief Defines the llvm::Arg class for parsed arguments.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_OPTION_ARG_H
  15. #define LLVM_OPTION_ARG_H
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/Option/Option.h"
  19. #include <string>
  20. namespace llvm {
  21. namespace opt {
  22. class ArgList;
  23. /// \brief A concrete instance of a particular driver option.
  24. ///
  25. /// The Arg class encodes just enough information to be able to
  26. /// derive the argument values efficiently.
  27. class Arg {
  28. Arg(const Arg &) = delete;
  29. void operator=(const Arg &) = delete;
  30. private:
  31. /// \brief The option this argument is an instance of.
  32. const Option Opt;
  33. /// \brief The argument this argument was derived from (during tool chain
  34. /// argument translation), if any.
  35. const Arg *BaseArg;
  36. /// \brief How this instance of the option was spelled.
  37. StringRef Spelling;
  38. /// \brief The index at which this argument appears in the containing
  39. /// ArgList.
  40. unsigned Index;
  41. /// \brief Was this argument used to effect compilation?
  42. ///
  43. /// This is used for generating "argument unused" diagnostics.
  44. mutable unsigned Claimed : 1;
  45. /// \brief Does this argument own its values?
  46. mutable unsigned OwnsValues : 1;
  47. /// \brief The argument values, as C strings.
  48. SmallVector<const char *, 2> Values;
  49. public:
  50. Arg(const Option Opt, StringRef Spelling, unsigned Index,
  51. const Arg *BaseArg = nullptr);
  52. Arg(const Option Opt, StringRef Spelling, unsigned Index,
  53. const char *Value0, const Arg *BaseArg = nullptr);
  54. Arg(const Option Opt, StringRef Spelling, unsigned Index,
  55. const char *Value0, const char *Value1, const Arg *BaseArg = nullptr);
  56. ~Arg();
  57. const Option &getOption() const { return Opt; }
  58. StringRef getSpelling() const { return Spelling; }
  59. unsigned getIndex() const { return Index; }
  60. /// \brief Return the base argument which generated this arg.
  61. ///
  62. /// This is either the argument itself or the argument it was
  63. /// derived from during tool chain specific argument translation.
  64. const Arg &getBaseArg() const {
  65. return BaseArg ? *BaseArg : *this;
  66. }
  67. void setBaseArg(const Arg *BaseArg) { this->BaseArg = BaseArg; }
  68. bool getOwnsValues() const { return OwnsValues; }
  69. void setOwnsValues(bool Value) const { OwnsValues = Value; }
  70. bool isClaimed() const { return getBaseArg().Claimed; }
  71. /// \brief Set the Arg claimed bit.
  72. void claim() const { getBaseArg().Claimed = true; }
  73. unsigned getNumValues() const { return Values.size(); }
  74. const char *getValue(unsigned N = 0) const {
  75. return Values[N];
  76. }
  77. SmallVectorImpl<const char *> &getValues() { return Values; }
  78. const SmallVectorImpl<const char *> &getValues() const { return Values; }
  79. bool containsValue(StringRef Value) const {
  80. for (unsigned i = 0, e = getNumValues(); i != e; ++i)
  81. if (Values[i] == Value)
  82. return true;
  83. return false;
  84. }
  85. /// \brief Append the argument onto the given array as strings.
  86. void render(const ArgList &Args, ArgStringList &Output) const;
  87. /// \brief Append the argument, render as an input, onto the given
  88. /// array as strings.
  89. ///
  90. /// The distinction is that some options only render their values
  91. /// when rendered as a input (e.g., Xlinker).
  92. void renderAsInput(const ArgList &Args, ArgStringList &Output) const;
  93. void dump() const;
  94. /// \brief Return a formatted version of the argument and
  95. /// its values, for debugging and diagnostics.
  96. std::string getAsString(const ArgList &Args) const;
  97. };
  98. } // end namespace opt
  99. } // end namespace llvm
  100. #endif