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);
59 for (const char *const *Pre = Info->Prefixes; *Pre != nullptr; ++Pre) {
60 O << '"' << *Pre << (*(Pre + 1) == nullptr ? "\"" : "\", ");
65 O << " Name:\"" << getName() << '"';
67 const Option Group = getGroup();
68 if (Group.isValid()) {
73 const Option Alias = getAlias();
74 if (Alias.isValid()) {
79 if (getKind() == MultiArgClass)
80 O << " NumArgs:" << getNumArgs();
85 void Option::dump() const { print(dbgs()); }
87 bool Option::matches(OptSpecifier Opt) const {
88 // Aliases are never considered in matching, look through them.
89 const Option Alias = getAlias();
91 return Alias.matches(Opt);
94 if (getID() == Opt.getID())
97 const Option Group = getGroup();
99 return Group.matches(Opt);
103 Arg *Option::accept(const ArgList &Args,
105 unsigned ArgSize) const {
106 const Option &UnaliasedOption = getUnaliasedOption();
108 // If the option was an alias, get the spelling from the unaliased one.
109 if (getID() == UnaliasedOption.getID()) {
110 Spelling = StringRef(Args.getArgString(Index), ArgSize);
112 Spelling = Args.MakeArgString(Twine(UnaliasedOption.getPrefix()) +
113 Twine(UnaliasedOption.getName()));
118 if (ArgSize != strlen(Args.getArgString(Index)))
121 Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
122 if (getAliasArgs()) {
123 const char *Val = getAliasArgs();
124 while (*Val != '\0') {
125 A->getValues().push_back(Val);
127 // Move past the '\0' to the next argument.
128 Val += strlen(Val) + 1;
132 if (UnaliasedOption.getKind() == JoinedClass && !getAliasArgs())
133 // A Flag alias for a Joined option must provide an argument.
134 A->getValues().push_back("");
139 const char *Value = Args.getArgString(Index) + ArgSize;
140 return new Arg(UnaliasedOption, Spelling, Index++, Value);
142 case CommaJoinedClass: {
144 const char *Str = Args.getArgString(Index) + ArgSize;
145 Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
147 // Parse out the comma separated values.
148 const char *Prev = Str;
152 if (!c || c == ',') {
154 char *Value = new char[Str - Prev + 1];
155 memcpy(Value, Prev, Str - Prev);
156 Value[Str - Prev] = '\0';
157 A->getValues().push_back(Value);
166 A->setOwnsValues(true);
171 // Matches iff this is an exact match.
172 // FIXME: Avoid strlen.
173 if (ArgSize != strlen(Args.getArgString(Index)))
177 if (Index > Args.getNumInputArgStrings() ||
178 Args.getArgString(Index - 1) == nullptr)
181 return new Arg(UnaliasedOption, Spelling,
182 Index - 2, Args.getArgString(Index - 1));
183 case MultiArgClass: {
184 // Matches iff this is an exact match.
185 // FIXME: Avoid strlen.
186 if (ArgSize != strlen(Args.getArgString(Index)))
189 Index += 1 + getNumArgs();
190 if (Index > Args.getNumInputArgStrings())
193 Arg *A = new Arg(UnaliasedOption, Spelling, Index - 1 - getNumArgs(),
194 Args.getArgString(Index - getNumArgs()));
195 for (unsigned i = 1; i != getNumArgs(); ++i)
196 A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
199 case JoinedOrSeparateClass: {
200 // If this is not an exact match, it is a joined arg.
201 // FIXME: Avoid strlen.
202 if (ArgSize != strlen(Args.getArgString(Index))) {
203 const char *Value = Args.getArgString(Index) + ArgSize;
204 return new Arg(*this, Spelling, Index++, Value);
207 // Otherwise it must be separate.
209 if (Index > Args.getNumInputArgStrings() ||
210 Args.getArgString(Index - 1) == nullptr)
213 return new Arg(UnaliasedOption, Spelling,
214 Index - 2, Args.getArgString(Index - 1));
216 case JoinedAndSeparateClass:
219 if (Index > Args.getNumInputArgStrings() ||
220 Args.getArgString(Index - 1) == nullptr)
223 return new Arg(UnaliasedOption, Spelling, Index - 2,
224 Args.getArgString(Index - 2) + ArgSize,
225 Args.getArgString(Index - 1));
226 case RemainingArgsClass: {
227 // Matches iff this is an exact match.
228 // FIXME: Avoid strlen.
229 if (ArgSize != strlen(Args.getArgString(Index)))
231 Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
232 while (Index < Args.getNumInputArgStrings() &&
233 Args.getArgString(Index) != nullptr)
234 A->getValues().push_back(Args.getArgString(Index++));
238 llvm_unreachable("Invalid option kind!");