blob: 9abc9fdce4c7221cada4b9af02089d4c93e169b2 [file] [log] [blame]
Eugene Zelenkoaf615892017-06-16 00:43:26 +00001//===- Option.cpp - Abstract Driver Options -------------------------------===//
Michael J. Spencer41ee0412012-12-05 00:29:32 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Michael J. Spencer41ee0412012-12-05 00:29:32 +00006//
7//===----------------------------------------------------------------------===//
8
Eugene Zelenkoaf615892017-06-16 00:43:26 +00009#include "llvm/ADT/StringRef.h"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000010#include "llvm/ADT/Twine.h"
Nico Weber432a3882018-04-30 14:59:11 +000011#include "llvm/Config/llvm-config.h"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000012#include "llvm/Option/Arg.h"
13#include "llvm/Option/ArgList.h"
Eugene Zelenkoaf615892017-06-16 00:43:26 +000014#include "llvm/Option/Option.h"
15#include "llvm/Option/OptTable.h"
16#include "llvm/Support/Compiler.h"
Eric Christopher9a8b5e72015-12-18 18:55:26 +000017#include "llvm/Support/Debug.h"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000018#include "llvm/Support/ErrorHandling.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000019#include "llvm/Support/raw_ostream.h"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000020#include <cassert>
Eugene Zelenkoaf615892017-06-16 00:43:26 +000021#include <cstring>
Michael J. Spencer41ee0412012-12-05 00:29:32 +000022
23using namespace llvm;
24using namespace llvm::opt;
25
26Option::Option(const OptTable::Info *info, const OptTable *owner)
27 : Info(info), Owner(owner) {
Hans Wennborg31d6fd82013-07-22 16:18:13 +000028 // Multi-level aliases are not supported. This just simplifies option
29 // tracking, it is not an inherent limitation.
Richard Trieu46978d42013-07-22 21:29:28 +000030 assert((!Info || !getAlias().isValid() || !getAlias().getAlias().isValid()) &&
Hans Wennborg31d6fd82013-07-22 16:18:13 +000031 "Multi-level aliases are not supported.");
Hans Wennborg5fdcf862013-07-31 22:44:41 +000032
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.");
36 assert(getAlias().getKind() != FlagClass &&
37 "Cannot provide alias args to a flag option.");
38 }
Michael J. Spencer41ee0412012-12-05 00:29:32 +000039}
40
Eric Christopher9a8b5e72015-12-18 18:55:26 +000041void Option::print(raw_ostream &O) const {
42 O << "<";
Michael J. Spencer41ee0412012-12-05 00:29:32 +000043 switch (getKind()) {
Eric Christopher9a8b5e72015-12-18 18:55:26 +000044#define P(N) case N: O << #N; break
Michael J. Spencer41ee0412012-12-05 00:29:32 +000045 P(GroupClass);
46 P(InputClass);
47 P(UnknownClass);
48 P(FlagClass);
49 P(JoinedClass);
Yuka Takahashiba5d4af2017-06-20 16:31:31 +000050 P(ValuesClass);
Michael J. Spencer41ee0412012-12-05 00:29:32 +000051 P(SeparateClass);
52 P(CommaJoinedClass);
53 P(MultiArgClass);
54 P(JoinedOrSeparateClass);
55 P(JoinedAndSeparateClass);
Hans Wennborgd505fbf2013-08-13 21:09:50 +000056 P(RemainingArgsClass);
Hans Wennborg40cfde32016-04-15 00:23:30 +000057 P(RemainingArgsJoinedClass);
Michael J. Spencer41ee0412012-12-05 00:29:32 +000058#undef P
59 }
60
Reid Kleckner7b78d352013-06-26 22:43:37 +000061 if (Info->Prefixes) {
Eric Christopher9a8b5e72015-12-18 18:55:26 +000062 O << " Prefixes:[";
63 for (const char *const *Pre = Info->Prefixes; *Pre != nullptr; ++Pre) {
64 O << '"' << *Pre << (*(Pre + 1) == nullptr ? "\"" : "\", ");
Reid Kleckner7b78d352013-06-26 22:43:37 +000065 }
Eric Christopher9a8b5e72015-12-18 18:55:26 +000066 O << ']';
Michael J. Spencer41ee0412012-12-05 00:29:32 +000067 }
Michael J. Spencer41ee0412012-12-05 00:29:32 +000068
Eric Christopher9a8b5e72015-12-18 18:55:26 +000069 O << " Name:\"" << getName() << '"';
Michael J. Spencer41ee0412012-12-05 00:29:32 +000070
71 const Option Group = getGroup();
72 if (Group.isValid()) {
Eric Christopher9a8b5e72015-12-18 18:55:26 +000073 O << " Group:";
74 Group.print(O);
Michael J. Spencer41ee0412012-12-05 00:29:32 +000075 }
76
77 const Option Alias = getAlias();
78 if (Alias.isValid()) {
Eric Christopher9a8b5e72015-12-18 18:55:26 +000079 O << " Alias:";
80 Alias.print(O);
Michael J. Spencer41ee0412012-12-05 00:29:32 +000081 }
82
83 if (getKind() == MultiArgClass)
Eric Christopher9a8b5e72015-12-18 18:55:26 +000084 O << " NumArgs:" << getNumArgs();
Michael J. Spencer41ee0412012-12-05 00:29:32 +000085
Eric Christopher9a8b5e72015-12-18 18:55:26 +000086 O << ">\n";
Michael J. Spencer41ee0412012-12-05 00:29:32 +000087}
88
Aaron Ballman615eb472017-10-15 14:32:27 +000089#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000090LLVM_DUMP_METHOD void Option::dump() const { print(dbgs()); }
Matthias Braun8c209aa2017-01-28 02:02:38 +000091#endif
Eric Christopher9a8b5e72015-12-18 18:55:26 +000092
Michael J. Spencer41ee0412012-12-05 00:29:32 +000093bool Option::matches(OptSpecifier Opt) const {
94 // Aliases are never considered in matching, look through them.
95 const Option Alias = getAlias();
96 if (Alias.isValid())
97 return Alias.matches(Opt);
98
99 // Check exact match.
100 if (getID() == Opt.getID())
101 return true;
102
103 const Option Group = getGroup();
104 if (Group.isValid())
105 return Group.matches(Opt);
106 return false;
107}
108
Nico Webere3f06b42019-07-09 00:34:08 +0000109Arg *Option::acceptInternal(const ArgList &Args, unsigned &Index,
110 unsigned ArgSize) const {
111 StringRef Spelling = StringRef(Args.getArgString(Index), ArgSize);
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000112 switch (getKind()) {
Hans Wennborg5fdcf862013-07-31 22:44:41 +0000113 case FlagClass: {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000114 if (ArgSize != strlen(Args.getArgString(Index)))
Craig Topper2617dcc2014-04-15 06:32:26 +0000115 return nullptr;
Nico Webere3f06b42019-07-09 00:34:08 +0000116 return new Arg(*this, Spelling, Index++);
Hans Wennborg5fdcf862013-07-31 22:44:41 +0000117 }
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000118 case JoinedClass: {
119 const char *Value = Args.getArgString(Index) + ArgSize;
Nico Webere3f06b42019-07-09 00:34:08 +0000120 return new Arg(*this, Spelling, Index++, Value);
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000121 }
122 case CommaJoinedClass: {
123 // Always matches.
124 const char *Str = Args.getArgString(Index) + ArgSize;
Nico Webere3f06b42019-07-09 00:34:08 +0000125 Arg *A = new Arg(*this, Spelling, Index++);
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000126
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)))
Craig Topper2617dcc2014-04-15 06:32:26 +0000154 return nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000155
156 Index += 2;
Reid Klecknere3f146d2014-08-22 19:29:17 +0000157 if (Index > Args.getNumInputArgStrings() ||
158 Args.getArgString(Index - 1) == nullptr)
Craig Topper2617dcc2014-04-15 06:32:26 +0000159 return nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000160
Nico Webere3f06b42019-07-09 00:34:08 +0000161 return new Arg(*this, Spelling, Index - 2, Args.getArgString(Index - 1));
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000162 case MultiArgClass: {
163 // Matches iff this is an exact match.
164 // FIXME: Avoid strlen.
165 if (ArgSize != strlen(Args.getArgString(Index)))
Craig Topper2617dcc2014-04-15 06:32:26 +0000166 return nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000167
168 Index += 1 + getNumArgs();
169 if (Index > Args.getNumInputArgStrings())
Craig Topper2617dcc2014-04-15 06:32:26 +0000170 return nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000171
Nico Webere3f06b42019-07-09 00:34:08 +0000172 Arg *A = new Arg(*this, Spelling, Index - 1 - getNumArgs(),
173 Args.getArgString(Index - getNumArgs()));
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000174 for (unsigned i = 1; i != getNumArgs(); ++i)
175 A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
176 return A;
177 }
178 case JoinedOrSeparateClass: {
179 // If this is not an exact match, it is a joined arg.
180 // FIXME: Avoid strlen.
181 if (ArgSize != strlen(Args.getArgString(Index))) {
182 const char *Value = Args.getArgString(Index) + ArgSize;
Nico Webere3f06b42019-07-09 00:34:08 +0000183 return new Arg(*this, Spelling, Index++, Value);
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000184 }
185
186 // Otherwise it must be separate.
187 Index += 2;
Reid Klecknere3f146d2014-08-22 19:29:17 +0000188 if (Index > Args.getNumInputArgStrings() ||
189 Args.getArgString(Index - 1) == nullptr)
Craig Topper2617dcc2014-04-15 06:32:26 +0000190 return nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000191
Nico Webere3f06b42019-07-09 00:34:08 +0000192 return new Arg(*this, Spelling, Index - 2, Args.getArgString(Index - 1));
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000193 }
194 case JoinedAndSeparateClass:
195 // Always matches.
196 Index += 2;
Reid Klecknere3f146d2014-08-22 19:29:17 +0000197 if (Index > Args.getNumInputArgStrings() ||
198 Args.getArgString(Index - 1) == nullptr)
Craig Topper2617dcc2014-04-15 06:32:26 +0000199 return nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000200
Nico Webere3f06b42019-07-09 00:34:08 +0000201 return new Arg(*this, Spelling, Index - 2,
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000202 Args.getArgString(Index - 2) + ArgSize,
203 Args.getArgString(Index - 1));
Hans Wennborgd505fbf2013-08-13 21:09:50 +0000204 case RemainingArgsClass: {
205 // Matches iff this is an exact match.
206 // FIXME: Avoid strlen.
207 if (ArgSize != strlen(Args.getArgString(Index)))
Craig Topper2617dcc2014-04-15 06:32:26 +0000208 return nullptr;
Nico Webere3f06b42019-07-09 00:34:08 +0000209 Arg *A = new Arg(*this, Spelling, Index++);
Reid Klecknere3f146d2014-08-22 19:29:17 +0000210 while (Index < Args.getNumInputArgStrings() &&
211 Args.getArgString(Index) != nullptr)
Hans Wennborgd505fbf2013-08-13 21:09:50 +0000212 A->getValues().push_back(Args.getArgString(Index++));
213 return A;
214 }
Hans Wennborg40cfde32016-04-15 00:23:30 +0000215 case RemainingArgsJoinedClass: {
Nico Webere3f06b42019-07-09 00:34:08 +0000216 Arg *A = new Arg(*this, Spelling, Index);
Hans Wennborg40cfde32016-04-15 00:23:30 +0000217 if (ArgSize != strlen(Args.getArgString(Index))) {
218 // An inexact match means there is a joined arg.
219 A->getValues().push_back(Args.getArgString(Index) + ArgSize);
220 }
221 Index++;
222 while (Index < Args.getNumInputArgStrings() &&
223 Args.getArgString(Index) != nullptr)
224 A->getValues().push_back(Args.getArgString(Index++));
225 return A;
226 }
227
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000228 default:
229 llvm_unreachable("Invalid option kind!");
230 }
231}
Nico Webere3f06b42019-07-09 00:34:08 +0000232
233Arg *Option::accept(const ArgList &Args,
234 unsigned &Index,
235 unsigned ArgSize) const {
236 std::unique_ptr<Arg> A(acceptInternal(Args, Index, ArgSize));
237 if (!A)
238 return nullptr;
239
240 const Option &UnaliasedOption = getUnaliasedOption();
241 if (getID() == UnaliasedOption.getID())
242 return A.release();
243
244 // "A" is an alias for a different flag. For most clients it's more convenient
245 // if this function returns unaliased Args, so create an unaliased arg for
246 // returning.
247
248 // This creates a completely new Arg object for the unaliased Arg because
249 // the alias and the unaliased arg can have different Kinds and different
250 // Values (due to AliasArgs<>).
251
252 // Get the spelling from the unaliased option.
253 StringRef UnaliasedSpelling = Args.MakeArgString(
254 Twine(UnaliasedOption.getPrefix()) + Twine(UnaliasedOption.getName()));
255
256 // It's a bit weird that aliased and unaliased arg share one index, but
257 // the index is mostly use as a memory optimization in render().
258 // Due to this, ArgList::getArgString(A->getIndex()) will return the spelling
259 // of the aliased arg always, while A->getSpelling() returns either the
260 // unaliased or the aliased arg, depending on which Arg object it's called on.
261 Arg *UnaliasedA = new Arg(UnaliasedOption, UnaliasedSpelling, A->getIndex());
262 Arg *RawA = A.get();
263 UnaliasedA->setAlias(std::move(A));
264
265 if (getKind() != FlagClass) {
266 // Values are usually owned by the ArgList. The exception are
267 // CommaJoined flags, where the Arg owns the values. For aliased flags,
268 // make the unaliased Arg the owner of the values.
269 // FIXME: There aren't many uses of CommaJoined -- try removing
270 // CommaJoined in favor of just calling StringRef::split(',') instead.
271 UnaliasedA->getValues() = RawA->getValues();
272 UnaliasedA->setOwnsValues(RawA->getOwnsValues());
273 RawA->setOwnsValues(false);
274 return UnaliasedA;
275 }
276
277 // FlagClass aliases can have AliasArgs<>; add those to the unaliased arg.
278 if (const char *Val = getAliasArgs()) {
279 while (*Val != '\0') {
280 UnaliasedA->getValues().push_back(Val);
281
282 // Move past the '\0' to the next argument.
283 Val += strlen(Val) + 1;
284 }
285 }
286 if (UnaliasedOption.getKind() == JoinedClass && !getAliasArgs())
287 // A Flag alias for a Joined option must provide an argument.
288 UnaliasedA->getValues().push_back("");
289 return UnaliasedA;
290}