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
22namespace llvm {
23
24class raw_ostream;
25
26namespace opt {
27
28class 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.
34class Arg {
35private:
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
72public:
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
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.
91 StringRef getSpelling() const { return Spelling; }
92
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.
99 const Arg &getBaseArg() const {
100 return BaseArg ? *BaseArg : *this;
101 }
102 Arg &getBaseArg() { return BaseArg ? const_cast<Arg &>(*BaseArg) : *this; }
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.
107 const Arg* getAlias() const { return Alias.get(); }
108 void setAlias(std::unique_ptr<Arg> Alias) { this->Alias = std::move(Alias); }
109
110 bool getOwnsValues() const { return OwnsValues; }
111 void setOwnsValues(bool Value) const { OwnsValues = Value; }
112
113 bool isClaimed() const { return getBaseArg().Claimed; }
114 void claim() const { getBaseArg().Claimed = true; }
115
116 bool isIgnoredTargetSpecific() const {
117 return getBaseArg().IgnoredTargetSpecific;
118 }
119 void ignoreTargetSpecific() {
120 getBaseArg().IgnoredTargetSpecific = true;
121 }
122
123 unsigned getNumValues() const { return Values.size(); }
124
125 const char *getValue(unsigned N = 0) const {
126 return Values[N];
127 }
128
129 SmallVectorImpl<const char *> &getValues() { return Values; }
130 const SmallVectorImpl<const char *> &getValues() const { return Values; }
131
132 bool containsValue(StringRef Value) const {
133 return llvm::is_contained(Range: Values, Element: 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

source code of llvm/include/llvm/Option/Arg.h