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