Arg.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Arg.h - Parsed Argument Classes --------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// Defines the llvm::Arg class for parsed arguments.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_OPTION_ARG_H
  19. #define LLVM_OPTION_ARG_H
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/Option/Option.h"
  23. #include <string>
  24. namespace llvm {
  25. class raw_ostream;
  26. namespace opt {
  27. class ArgList;
  28. /// A concrete instance of a particular driver option.
  29. ///
  30. /// The Arg class encodes just enough information to be able to
  31. /// derive the argument values efficiently.
  32. class Arg {
  33. private:
  34. /// The option this argument is an instance of.
  35. const Option Opt;
  36. /// The argument this argument was derived from (during tool chain
  37. /// argument translation), if any.
  38. const Arg *BaseArg;
  39. /// How this instance of the option was spelled.
  40. StringRef Spelling;
  41. /// The index at which this argument appears in the containing
  42. /// ArgList.
  43. unsigned Index;
  44. /// Was this argument used to effect compilation?
  45. ///
  46. /// This is used for generating "argument unused" diagnostics.
  47. mutable unsigned Claimed : 1;
  48. /// Does this argument own its values?
  49. mutable unsigned OwnsValues : 1;
  50. /// The argument values, as C strings.
  51. SmallVector<const char *, 2> Values;
  52. /// If this arg was created through an alias, this is the original alias arg.
  53. /// For example, *this might be "-finput-charset=utf-8" and Alias might
  54. /// point to an arg representing "/source-charset:utf-8".
  55. std::unique_ptr<Arg> Alias;
  56. public:
  57. Arg(const Option Opt, StringRef Spelling, unsigned Index,
  58. const Arg *BaseArg = nullptr);
  59. Arg(const Option Opt, StringRef Spelling, unsigned Index,
  60. const char *Value0, const Arg *BaseArg = nullptr);
  61. Arg(const Option Opt, StringRef Spelling, unsigned Index,
  62. const char *Value0, const char *Value1, const Arg *BaseArg = nullptr);
  63. Arg(const Arg &) = delete;
  64. Arg &operator=(const Arg &) = delete;
  65. ~Arg();
  66. const Option &getOption() const { return Opt; }
  67. /// Returns the used prefix and name of the option:
  68. /// For `--foo=bar`, returns `--foo=`.
  69. /// This is often the wrong function to call:
  70. /// * Use `getValue()` to get `bar`.
  71. /// * Use `getAsString()` to get a string suitable for printing an Arg in
  72. /// a diagnostic.
  73. StringRef getSpelling() const { return Spelling; }
  74. unsigned getIndex() const { return Index; }
  75. /// Return the base argument which generated this arg.
  76. ///
  77. /// This is either the argument itself or the argument it was
  78. /// derived from during tool chain specific argument translation.
  79. const Arg &getBaseArg() const {
  80. return BaseArg ? *BaseArg : *this;
  81. }
  82. void setBaseArg(const Arg *BaseArg) { this->BaseArg = BaseArg; }
  83. /// Args are converted to their unaliased form. For args that originally
  84. /// came from an alias, this returns the alias the arg was produced from.
  85. const Arg* getAlias() const { return Alias.get(); }
  86. void setAlias(std::unique_ptr<Arg> Alias) { this->Alias = std::move(Alias); }
  87. bool getOwnsValues() const { return OwnsValues; }
  88. void setOwnsValues(bool Value) const { OwnsValues = Value; }
  89. bool isClaimed() const { return getBaseArg().Claimed; }
  90. /// Set the Arg claimed bit.
  91. void claim() const { getBaseArg().Claimed = true; }
  92. unsigned getNumValues() const { return Values.size(); }
  93. const char *getValue(unsigned N = 0) const {
  94. return Values[N];
  95. }
  96. SmallVectorImpl<const char *> &getValues() { return Values; }
  97. const SmallVectorImpl<const char *> &getValues() const { return Values; }
  98. bool containsValue(StringRef Value) const {
  99. return llvm::is_contained(Values, Value);
  100. }
  101. /// Append the argument onto the given array as strings.
  102. void render(const ArgList &Args, ArgStringList &Output) const;
  103. /// Append the argument, render as an input, onto the given
  104. /// array as strings.
  105. ///
  106. /// The distinction is that some options only render their values
  107. /// when rendered as a input (e.g., Xlinker).
  108. void renderAsInput(const ArgList &Args, ArgStringList &Output) const;
  109. void print(raw_ostream &O) const;
  110. void dump() const;
  111. /// Return a formatted version of the argument and its values, for
  112. /// diagnostics. Since this is for diagnostics, if this Arg was produced
  113. /// through an alias, this returns the string representation of the alias
  114. /// that the user wrote.
  115. std::string getAsString(const ArgList &Args) const;
  116. };
  117. } // end namespace opt
  118. } // end namespace llvm
  119. #endif // LLVM_OPTION_ARG_H
  120. #ifdef __GNUC__
  121. #pragma GCC diagnostic pop
  122. #endif