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