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