LLVM 17.0.0git
Option.cpp
Go to the documentation of this file.
1//===- Option.cpp - Abstract Driver Options -------------------------------===//
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
10#include "llvm/ADT/Twine.h"
11#include "llvm/Config/llvm-config.h"
12#include "llvm/Option/Arg.h"
13#include "llvm/Option/ArgList.h"
14#include "llvm/Option/Option.h"
17#include "llvm/Support/Debug.h"
20#include <cassert>
21#include <cstring>
22
23using namespace llvm;
24using namespace llvm::opt;
25
27 : Info(info), Owner(owner) {
28 // Multi-level aliases are not supported. This just simplifies option
29 // tracking, it is not an inherent limitation.
30 assert((!Info || !getAlias().isValid() || !getAlias().getAlias().isValid()) &&
31 "Multi-level aliases are not supported.");
32
33 if (Info && getAliasArgs()) {
34 assert(getAlias().isValid() && "Only alias options can have alias args.");
35 assert(getKind() == FlagClass && "Only Flag aliases can have alias args.");
37 "Cannot provide alias args to a flag option.");
38 }
39}
40
41void Option::print(raw_ostream &O) const {
42 O << "<";
43 switch (getKind()) {
44#define P(N) case N: O << #N; break
48 P(FlagClass);
58#undef P
59 }
60
61 if (!Info->Prefixes.empty()) {
62 O << " Prefixes:[";
63 for (size_t I = 0, N = Info->Prefixes.size(); I != N; ++I)
64 O << '"' << Info->Prefixes[I] << (I == N - 1 ? "\"" : "\", ");
65 O << ']';
66 }
67
68 O << " Name:\"" << getName() << '"';
69
70 const Option Group = getGroup();
71 if (Group.isValid()) {
72 O << " Group:";
73 Group.print(O);
74 }
75
76 const Option Alias = getAlias();
77 if (Alias.isValid()) {
78 O << " Alias:";
79 Alias.print(O);
80 }
81
82 if (getKind() == MultiArgClass)
83 O << " NumArgs:" << getNumArgs();
84
85 O << ">\n";
86}
87
88#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
90#endif
91
93 // Aliases are never considered in matching, look through them.
94 const Option Alias = getAlias();
95 if (Alias.isValid())
96 return Alias.matches(Opt);
97
98 // Check exact match.
99 if (getID() == Opt.getID())
100 return true;
101
102 const Option Group = getGroup();
103 if (Group.isValid())
104 return Group.matches(Opt);
105 return false;
106}
107
108std::unique_ptr<Arg> Option::acceptInternal(const ArgList &Args,
109 StringRef Spelling,
110 unsigned &Index) const {
111 size_t ArgSize = Spelling.size();
112 switch (getKind()) {
113 case FlagClass: {
114 if (ArgSize != strlen(Args.getArgString(Index)))
115 return nullptr;
116 return std::make_unique<Arg>(*this, Spelling, Index++);
117 }
118 case JoinedClass: {
119 const char *Value = Args.getArgString(Index) + ArgSize;
120 return std::make_unique<Arg>(*this, Spelling, Index++, Value);
121 }
122 case CommaJoinedClass: {
123 // Always matches.
124 const char *Str = Args.getArgString(Index) + ArgSize;
125 auto A = std::make_unique<Arg>(*this, Spelling, Index++);
126
127 // Parse out the comma separated values.
128 const char *Prev = Str;
129 for (;; ++Str) {
130 char c = *Str;
131
132 if (!c || c == ',') {
133 if (Prev != Str) {
134 char *Value = new char[Str - Prev + 1];
135 memcpy(Value, Prev, Str - Prev);
136 Value[Str - Prev] = '\0';
137 A->getValues().push_back(Value);
138 }
139
140 if (!c)
141 break;
142
143 Prev = Str + 1;
144 }
145 }
146 A->setOwnsValues(true);
147
148 return A;
149 }
150 case SeparateClass:
151 // Matches iff this is an exact match.
152 // FIXME: Avoid strlen.
153 if (ArgSize != strlen(Args.getArgString(Index)))
154 return nullptr;
155
156 Index += 2;
157 if (Index > Args.getNumInputArgStrings() ||
158 Args.getArgString(Index - 1) == nullptr)
159 return nullptr;
160
161 return std::make_unique<Arg>(*this, Spelling, Index - 2,
162 Args.getArgString(Index - 1));
163 case MultiArgClass: {
164 // Matches iff this is an exact match.
165 // FIXME: Avoid strlen.
166 if (ArgSize != strlen(Args.getArgString(Index)))
167 return nullptr;
168
169 Index += 1 + getNumArgs();
170 if (Index > Args.getNumInputArgStrings())
171 return nullptr;
172
173 auto A = std::make_unique<Arg>(*this, Spelling, Index - 1 - getNumArgs(),
174 Args.getArgString(Index - getNumArgs()));
175 for (unsigned i = 1; i != getNumArgs(); ++i)
176 A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
177 return A;
178 }
180 // If this is not an exact match, it is a joined arg.
181 // FIXME: Avoid strlen.
182 if (ArgSize != strlen(Args.getArgString(Index))) {
183 const char *Value = Args.getArgString(Index) + ArgSize;
184 return std::make_unique<Arg>(*this, Spelling, Index++, Value);
185 }
186
187 // Otherwise it must be separate.
188 Index += 2;
189 if (Index > Args.getNumInputArgStrings() ||
190 Args.getArgString(Index - 1) == nullptr)
191 return nullptr;
192
193 return std::make_unique<Arg>(*this, Spelling, Index - 2,
194 Args.getArgString(Index - 1));
195 }
197 // Always matches.
198 Index += 2;
199 if (Index > Args.getNumInputArgStrings() ||
200 Args.getArgString(Index - 1) == nullptr)
201 return nullptr;
202
203 return std::make_unique<Arg>(*this, Spelling, Index - 2,
204 Args.getArgString(Index - 2) + ArgSize,
205 Args.getArgString(Index - 1));
206 case RemainingArgsClass: {
207 // Matches iff this is an exact match.
208 // FIXME: Avoid strlen.
209 if (ArgSize != strlen(Args.getArgString(Index)))
210 return nullptr;
211 auto A = std::make_unique<Arg>(*this, Spelling, Index++);
212 while (Index < Args.getNumInputArgStrings() &&
213 Args.getArgString(Index) != nullptr)
214 A->getValues().push_back(Args.getArgString(Index++));
215 return A;
216 }
218 auto A = std::make_unique<Arg>(*this, Spelling, Index);
219 if (ArgSize != strlen(Args.getArgString(Index))) {
220 // An inexact match means there is a joined arg.
221 A->getValues().push_back(Args.getArgString(Index) + ArgSize);
222 }
223 Index++;
224 while (Index < Args.getNumInputArgStrings() &&
225 Args.getArgString(Index) != nullptr)
226 A->getValues().push_back(Args.getArgString(Index++));
227 return A;
228 }
229
230 default:
231 llvm_unreachable("Invalid option kind!");
232 }
233}
234
235std::unique_ptr<Arg> Option::accept(const ArgList &Args, StringRef CurArg,
236 bool GroupedShortOption,
237 unsigned &Index) const {
238 auto A(GroupedShortOption && getKind() == FlagClass
239 ? std::make_unique<Arg>(*this, CurArg, Index)
240 : acceptInternal(Args, CurArg, Index));
241 if (!A)
242 return nullptr;
243
244 const Option &UnaliasedOption = getUnaliasedOption();
245 if (getID() == UnaliasedOption.getID())
246 return A;
247
248 // "A" is an alias for a different flag. For most clients it's more convenient
249 // if this function returns unaliased Args, so create an unaliased arg for
250 // returning.
251
252 // This creates a completely new Arg object for the unaliased Arg because
253 // the alias and the unaliased arg can have different Kinds and different
254 // Values (due to AliasArgs<>).
255
256 // Get the spelling from the unaliased option.
257 StringRef UnaliasedSpelling = Args.MakeArgString(
258 Twine(UnaliasedOption.getPrefix()) + Twine(UnaliasedOption.getName()));
259
260 // It's a bit weird that aliased and unaliased arg share one index, but
261 // the index is mostly use as a memory optimization in render().
262 // Due to this, ArgList::getArgString(A->getIndex()) will return the spelling
263 // of the aliased arg always, while A->getSpelling() returns either the
264 // unaliased or the aliased arg, depending on which Arg object it's called on.
265 auto UnaliasedA =
266 std::make_unique<Arg>(UnaliasedOption, UnaliasedSpelling, A->getIndex());
267 Arg *RawA = A.get();
268 UnaliasedA->setAlias(std::move(A));
269
270 if (getKind() != FlagClass) {
271 // Values are usually owned by the ArgList. The exception are
272 // CommaJoined flags, where the Arg owns the values. For aliased flags,
273 // make the unaliased Arg the owner of the values.
274 // FIXME: There aren't many uses of CommaJoined -- try removing
275 // CommaJoined in favor of just calling StringRef::split(',') instead.
276 UnaliasedA->getValues() = RawA->getValues();
277 UnaliasedA->setOwnsValues(RawA->getOwnsValues());
278 RawA->setOwnsValues(false);
279 return UnaliasedA;
280 }
281
282 // FlagClass aliases can have AliasArgs<>; add those to the unaliased arg.
283 if (const char *Val = getAliasArgs()) {
284 while (*Val != '\0') {
285 UnaliasedA->getValues().push_back(Val);
286
287 // Move past the '\0' to the next argument.
288 Val += strlen(Val) + 1;
289 }
290 }
291 if (UnaliasedOption.getKind() == JoinedClass && !getAliasArgs())
292 // A Flag alias for a Joined option must provide an argument.
293 UnaliasedA->getValues().push_back("");
294 return UnaliasedA;
295}
Defines the llvm::Arg class for parsed arguments.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:492
lazy value info
#define I(x, y, z)
Definition: MD5.cpp:58
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
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
bool getOwnsValues() const
Definition: Arg.h:103
SmallVectorImpl< const char * > & getValues()
Definition: Arg.h:117
void setOwnsValues(bool Value) const
Definition: Arg.h:104
OptSpecifier - Wrapper class for abstracting references to option IDs.
Definition: OptSpecifier.h:18
unsigned getID() const
Definition: OptSpecifier.h:29
Provide access to the Option info table.
Definition: OptTable.h:40
Option - Abstract representation for a single form of driver argument.
Definition: Option.h:51
const Option getAlias() const
Definition: Option.h:109
void dump() const
Definition: Option.cpp:89
unsigned getNumArgs() const
Definition: Option.h:151
const char * getAliasArgs() const
Get the alias arguments as a \0 separated list.
Definition: Option.h:117
const Option getGroup() const
Definition: Option.h:103
const Option getUnaliasedOption() const
getUnaliasedOption - Return the final option this option aliases (itself, if the option has no alias)...
Definition: Option.h:189
bool matches(OptSpecifier ID) const
matches - Predicate for whether this option is part of the given option (which may be a group).
Definition: Option.cpp:92
Option(const OptTable::Info *Info, const OptTable *Owner)
Definition: Option.cpp:26
void print(raw_ostream &O) const
Definition: Option.cpp:41
@ JoinedOrSeparateClass
Definition: Option.h:65
@ RemainingArgsClass
Definition: Option.h:61
@ JoinedAndSeparateClass
Definition: Option.h:66
@ RemainingArgsJoinedClass
Definition: Option.h:62
StringRef getPrefix() const
Get the default prefix for this option.
Definition: Option.h:126
const OptTable::Info * Info
Definition: Option.h:77
bool isValid() const
Definition: Option.h:83
unsigned getID() const
Definition: Option.h:87
StringRef getName() const
Get the name of this option without any prefix.
Definition: Option.h:98
std::unique_ptr< Arg > accept(const ArgList &Args, StringRef CurArg, bool GroupedShortOption, unsigned &Index) const
Potentially accept the current argument, returning a new Arg instance, or 0 if the option does not ac...
Definition: Option.cpp:235
OptionClass getKind() const
Definition: Option.h:92
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: Arg.h:26
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
#define N
Entry for a single option instance in the option data table.
Definition: OptTable.h:43
ArrayRef< StringLiteral > Prefixes
A null terminated array of prefix strings to apply to name while matching.
Definition: OptTable.h:46