LLVM 20.0.0git
Arg.h
Go to the documentation of this file.
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
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.
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
117 return getBaseArg().IgnoredTargetSpecific;
118 }
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
130 const SmallVectorImpl<const char *> &getValues() const { return Values; }
131
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
arm prera ldst opt
This file defines the SmallVector class.
size_t size() const
Definition: SmallVector.h:78
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
LLVM Value Representation.
Definition: Value.h:74
ArgList - Ordered collection of driver arguments.
Definition: ArgList.h:116
A concrete instance of a particular driver option.
Definition: Arg.h:34
void render(const ArgList &Args, ArgStringList &Output) const
Append the argument onto the given array as strings.
Definition: Arg.cpp:94
const Arg & getBaseArg() const
Return the base argument which generated this arg.
Definition: Arg.h:99
bool containsValue(StringRef Value) const
Definition: Arg.h:132
bool getOwnsValues() const
Definition: Arg.h:110
void setAlias(std::unique_ptr< Arg > Alias)
Definition: Arg.h:108
void print(raw_ostream &O) const
Definition: Arg.cpp:47
void renderAsInput(const ArgList &Args, ArgStringList &Output) const
Append the argument, render as an input, onto the given array as strings.
Definition: Arg.cpp:85
StringRef getSpelling() const
Returns the used prefix and name of the option: For --foo=bar, returns --foo=.
Definition: Arg.h:91
SmallVectorImpl< const char * > & getValues()
Definition: Arg.h:129
Arg & operator=(const Arg &)=delete
std::string getAsString(const ArgList &Args) const
Return a formatted version of the argument and its values, for diagnostics.
Definition: Arg.cpp:66
const Option & getOption() const
Definition: Arg.h:83
Arg & getBaseArg()
Definition: Arg.h:102
bool isClaimed() const
Definition: Arg.h:113
void setOwnsValues(bool Value) const
Definition: Arg.h:111
Arg(const Arg &)=delete
void ignoreTargetSpecific()
Definition: Arg.h:119
void setBaseArg(const Arg *BaseArg)
Definition: Arg.h:103
unsigned getNumValues() const
Definition: Arg.h:123
unsigned getIndex() const
Definition: Arg.h:93
void dump() const
Definition: Arg.cpp:63
const SmallVectorImpl< const char * > & getValues() const
Definition: Arg.h:130
const Arg * getAlias() const
Args are converted to their unaliased form.
Definition: Arg.h:107
const char * getValue(unsigned N=0) const
Definition: Arg.h:125
void claim() const
Definition: Arg.h:114
bool isIgnoredTargetSpecific() const
Definition: Arg.h:116
Option - Abstract representation for a single form of driver argument.
Definition: Option.h:54
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1903
#define N