1 //===--- Option.cpp - Abstract Driver Options -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Option/Option.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/Option/Arg.h"
13 #include "llvm/Option/ArgList.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm::opt;
23 Option::Option(const OptTable::Info *info, const OptTable *owner)
24 : Info(info), Owner(owner) {
26 // Multi-level aliases are not supported. This just simplifies option
27 // tracking, it is not an inherent limitation.
28 assert((!Info || !getAlias().isValid() || !getAlias().getAlias().isValid()) &&
29 "Multi-level aliases are not supported.");
31 if (Info && getAliasArgs()) {
32 assert(getAlias().isValid() && "Only alias options can have alias args.");
33 assert(getKind() == FlagClass && "Only Flag aliases can have alias args.");
34 assert(getAlias().getKind() != FlagClass &&
35 "Cannot provide alias args to a flag option.");
39 void Option::print(raw_ostream &O) const {
42 #define P(N) case N: O << #N; break
51 P(JoinedOrSeparateClass);
52 P(JoinedAndSeparateClass);
53 P(RemainingArgsClass);
54 P(RemainingArgsJoinedClass);
60 for (const char *const *Pre = Info->Prefixes; *Pre != nullptr; ++Pre) {
61 O << '"' << *Pre << (*(Pre + 1) == nullptr ? "\"" : "\", ");
66 O << " Name:\"" << getName() << '"';
68 const Option Group = getGroup();
69 if (Group.isValid()) {
74 const Option Alias = getAlias();
75 if (Alias.isValid()) {
80 if (getKind() == MultiArgClass)
81 O << " NumArgs:" << getNumArgs();
86 LLVM_DUMP_METHOD void Option::dump() const { print(dbgs()); }
88 bool Option::matches(OptSpecifier Opt) const {
89 // Aliases are never considered in matching, look through them.
90 const Option Alias = getAlias();
92 return Alias.matches(Opt);
95 if (getID() == Opt.getID())
98 const Option Group = getGroup();
100 return Group.matches(Opt);
104 Arg *Option::accept(const ArgList &Args,
106 unsigned ArgSize) const {
107 const Option &UnaliasedOption = getUnaliasedOption();
109 // If the option was an alias, get the spelling from the unaliased one.
110 if (getID() == UnaliasedOption.getID()) {
111 Spelling = StringRef(Args.getArgString(Index), ArgSize);
113 Spelling = Args.MakeArgString(Twine(UnaliasedOption.getPrefix()) +
114 Twine(UnaliasedOption.getName()));
119 if (ArgSize != strlen(Args.getArgString(Index)))
122 Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
123 if (getAliasArgs()) {
124 const char *Val = getAliasArgs();
125 while (*Val != '\0') {
126 A->getValues().push_back(Val);
128 // Move past the '\0' to the next argument.
129 Val += strlen(Val) + 1;
133 if (UnaliasedOption.getKind() == JoinedClass && !getAliasArgs())
134 // A Flag alias for a Joined option must provide an argument.
135 A->getValues().push_back("");
140 const char *Value = Args.getArgString(Index) + ArgSize;
141 return new Arg(UnaliasedOption, Spelling, Index++, Value);
143 case CommaJoinedClass: {
145 const char *Str = Args.getArgString(Index) + ArgSize;
146 Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
148 // Parse out the comma separated values.
149 const char *Prev = Str;
153 if (!c || c == ',') {
155 char *Value = new char[Str - Prev + 1];
156 memcpy(Value, Prev, Str - Prev);
157 Value[Str - Prev] = '\0';
158 A->getValues().push_back(Value);
167 A->setOwnsValues(true);
172 // Matches iff this is an exact match.
173 // FIXME: Avoid strlen.
174 if (ArgSize != strlen(Args.getArgString(Index)))
178 if (Index > Args.getNumInputArgStrings() ||
179 Args.getArgString(Index - 1) == nullptr)
182 return new Arg(UnaliasedOption, Spelling,
183 Index - 2, Args.getArgString(Index - 1));
184 case MultiArgClass: {
185 // Matches iff this is an exact match.
186 // FIXME: Avoid strlen.
187 if (ArgSize != strlen(Args.getArgString(Index)))
190 Index += 1 + getNumArgs();
191 if (Index > Args.getNumInputArgStrings())
194 Arg *A = new Arg(UnaliasedOption, Spelling, Index - 1 - getNumArgs(),
195 Args.getArgString(Index - getNumArgs()));
196 for (unsigned i = 1; i != getNumArgs(); ++i)
197 A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
200 case JoinedOrSeparateClass: {
201 // If this is not an exact match, it is a joined arg.
202 // FIXME: Avoid strlen.
203 if (ArgSize != strlen(Args.getArgString(Index))) {
204 const char *Value = Args.getArgString(Index) + ArgSize;
205 return new Arg(*this, Spelling, Index++, Value);
208 // Otherwise it must be separate.
210 if (Index > Args.getNumInputArgStrings() ||
211 Args.getArgString(Index - 1) == nullptr)
214 return new Arg(UnaliasedOption, Spelling,
215 Index - 2, Args.getArgString(Index - 1));
217 case JoinedAndSeparateClass:
220 if (Index > Args.getNumInputArgStrings() ||
221 Args.getArgString(Index - 1) == nullptr)
224 return new Arg(UnaliasedOption, Spelling, Index - 2,
225 Args.getArgString(Index - 2) + ArgSize,
226 Args.getArgString(Index - 1));
227 case RemainingArgsClass: {
228 // Matches iff this is an exact match.
229 // FIXME: Avoid strlen.
230 if (ArgSize != strlen(Args.getArgString(Index)))
232 Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
233 while (Index < Args.getNumInputArgStrings() &&
234 Args.getArgString(Index) != nullptr)
235 A->getValues().push_back(Args.getArgString(Index++));
238 case RemainingArgsJoinedClass: {
239 Arg *A = new Arg(UnaliasedOption, Spelling, Index);
240 if (ArgSize != strlen(Args.getArgString(Index))) {
241 // An inexact match means there is a joined arg.
242 A->getValues().push_back(Args.getArgString(Index) + ArgSize);
245 while (Index < Args.getNumInputArgStrings() &&
246 Args.getArgString(Index) != nullptr)
247 A->getValues().push_back(Args.getArgString(Index++));
252 llvm_unreachable("Invalid option kind!");