1 //===- Arg.h - Parsed Argument Classes --------------------------*- C++ -*-===// 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 /// 9 /// \file 10 /// Defines the llvm::Arg class for parsed arguments. 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 30 /// A concrete instance of a particular driver option. 31 /// 32 /// The Arg class encodes just enough information to be able to 33 /// derive the argument values efficiently. 34 class Arg { 35 private: 36 /// The option this argument is an instance of. 37 const Option Opt; 38 39 /// The argument this argument was derived from (during tool chain 40 /// argument translation), if any. 41 const Arg *BaseArg; 42 43 /// How this instance of the option was spelled. 44 StringRef Spelling; 45 46 /// The index at which this argument appears in the containing 47 /// ArgList. 48 unsigned Index; 49 50 /// Was this argument used to affect compilation? 51 /// 52 /// This is used to generate an "argument unused" warning (without 53 /// clang::driver::options::TargetSpecific) or "unsupported option" error 54 /// (with TargetSpecific). 55 mutable unsigned Claimed : 1; 56 57 /// Used by an unclaimed option with the TargetSpecific flag. If set, report 58 /// an "argument unused" warning instead of an "unsupported option" error. 59 unsigned IgnoredTargetSpecific : 1; 60 61 /// Does this argument own its values? 62 mutable unsigned OwnsValues : 1; 63 64 /// The argument values, as C strings. 65 SmallVector<const char *, 2> Values; 66 67 /// If this arg was created through an alias, this is the original alias arg. 68 /// For example, *this might be "-finput-charset=utf-8" and Alias might 69 /// point to an arg representing "/source-charset:utf-8". 70 std::unique_ptr<Arg> Alias; 71 72 public: 73 Arg(const Option Opt, StringRef Spelling, unsigned Index, 74 const Arg *BaseArg = nullptr); 75 Arg(const Option Opt, StringRef Spelling, unsigned Index, 76 const char *Value0, const Arg *BaseArg = nullptr); 77 Arg(const Option Opt, StringRef Spelling, unsigned Index, 78 const char *Value0, const char *Value1, const Arg *BaseArg = nullptr); 79 Arg(const Arg &) = delete; 80 Arg &operator=(const Arg &) = delete; 81 ~Arg(); 82 getOption()83 const Option &getOption() const { return Opt; } 84 85 /// Returns the used prefix and name of the option: 86 /// For `--foo=bar`, returns `--foo=`. 87 /// This is often the wrong function to call: 88 /// * Use `getValue()` to get `bar`. 89 /// * Use `getAsString()` to get a string suitable for printing an Arg in 90 /// a diagnostic. getSpelling()91 StringRef getSpelling() const { return Spelling; } 92 getIndex()93 unsigned getIndex() const { return Index; } 94 95 /// Return the base argument which generated this arg. 96 /// 97 /// This is either the argument itself or the argument it was 98 /// derived from during tool chain specific argument translation. getBaseArg()99 const Arg &getBaseArg() const { 100 return BaseArg ? *BaseArg : *this; 101 } getBaseArg()102 Arg &getBaseArg() { return BaseArg ? const_cast<Arg &>(*BaseArg) : *this; } setBaseArg(const Arg * BaseArg)103 void setBaseArg(const Arg *BaseArg) { this->BaseArg = BaseArg; } 104 105 /// Args are converted to their unaliased form. For args that originally 106 /// came from an alias, this returns the alias the arg was produced from. getAlias()107 const Arg* getAlias() const { return Alias.get(); } setAlias(std::unique_ptr<Arg> Alias)108 void setAlias(std::unique_ptr<Arg> Alias) { this->Alias = std::move(Alias); } 109 getOwnsValues()110 bool getOwnsValues() const { return OwnsValues; } setOwnsValues(bool Value)111 void setOwnsValues(bool Value) const { OwnsValues = Value; } 112 isClaimed()113 bool isClaimed() const { return getBaseArg().Claimed; } claim()114 void claim() const { getBaseArg().Claimed = true; } 115 isIgnoredTargetSpecific()116 bool isIgnoredTargetSpecific() const { 117 return getBaseArg().IgnoredTargetSpecific; 118 } ignoreTargetSpecific()119 void ignoreTargetSpecific() { 120 getBaseArg().IgnoredTargetSpecific = true; 121 } 122 getNumValues()123 unsigned getNumValues() const { return Values.size(); } 124 125 const char *getValue(unsigned N = 0) const { 126 return Values[N]; 127 } 128 getValues()129 SmallVectorImpl<const char *> &getValues() { return Values; } getValues()130 const SmallVectorImpl<const char *> &getValues() const { return Values; } 131 containsValue(StringRef Value)132 bool containsValue(StringRef Value) const { 133 return llvm::is_contained(Values, Value); 134 } 135 136 /// Append the argument onto the given array as strings. 137 void render(const ArgList &Args, ArgStringList &Output) const; 138 139 /// Append the argument, render as an input, onto the given 140 /// array as strings. 141 /// 142 /// The distinction is that some options only render their values 143 /// when rendered as a input (e.g., Xlinker). 144 void renderAsInput(const ArgList &Args, ArgStringList &Output) const; 145 146 void print(raw_ostream &O) const; 147 void dump() const; 148 149 /// Return a formatted version of the argument and its values, for 150 /// diagnostics. Since this is for diagnostics, if this Arg was produced 151 /// through an alias, this returns the string representation of the alias 152 /// that the user wrote. 153 std::string getAsString(const ArgList &Args) const; 154 }; 155 156 } // end namespace opt 157 158 } // end namespace llvm 159 160 #endif // LLVM_OPTION_ARG_H 161