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/ErrorHandling.h"
15 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm::opt;
22 Option::Option(const OptTable::Info *info, const OptTable *owner)
23 : Info(info), Owner(owner) {
25 // Multi-level aliases are not supported. This just simplifies option
26 // tracking, it is not an inherent limitation.
27 assert((!Info || !getAlias().isValid() || !getAlias().getAlias().isValid()) &&
28 "Multi-level aliases are not supported.");
30 if (Info && getAliasArgs()) {
31 assert(getAlias().isValid() && "Only alias options can have alias args.");
32 assert(getKind() == FlagClass && "Only Flag aliases can have alias args.");
33 assert(getAlias().getKind() != FlagClass &&
34 "Cannot provide alias args to a flag option.");
41 void Option::dump() const {
44 #define P(N) case N: llvm::errs() << #N; break
53 P(JoinedOrSeparateClass);
54 P(JoinedAndSeparateClass);
55 P(RemainingArgsClass);
60 llvm::errs() << " Prefixes:[";
61 for (const char * const *Pre = Info->Prefixes; *Pre != 0; ++Pre) {
62 llvm::errs() << '"' << *Pre << (*(Pre + 1) == 0 ? "\"" : "\", ");
67 llvm::errs() << " Name:\"" << getName() << '"';
69 const Option Group = getGroup();
70 if (Group.isValid()) {
71 llvm::errs() << " Group:";
75 const Option Alias = getAlias();
76 if (Alias.isValid()) {
77 llvm::errs() << " Alias:";
81 if (getKind() == MultiArgClass)
82 llvm::errs() << " NumArgs:" << getNumArgs();
84 llvm::errs() << ">\n";
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;
134 const char *Value = Args.getArgString(Index) + ArgSize;
135 return new Arg(UnaliasedOption, Spelling, Index++, Value);
137 case CommaJoinedClass: {
139 const char *Str = Args.getArgString(Index) + ArgSize;
140 Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
142 // Parse out the comma separated values.
143 const char *Prev = Str;
147 if (!c || c == ',') {
149 char *Value = new char[Str - Prev + 1];
150 memcpy(Value, Prev, Str - Prev);
151 Value[Str - Prev] = '\0';
152 A->getValues().push_back(Value);
161 A->setOwnsValues(true);
166 // Matches iff this is an exact match.
167 // FIXME: Avoid strlen.
168 if (ArgSize != strlen(Args.getArgString(Index)))
172 if (Index > Args.getNumInputArgStrings())
175 return new Arg(UnaliasedOption, Spelling,
176 Index - 2, Args.getArgString(Index - 1));
177 case MultiArgClass: {
178 // Matches iff this is an exact match.
179 // FIXME: Avoid strlen.
180 if (ArgSize != strlen(Args.getArgString(Index)))
183 Index += 1 + getNumArgs();
184 if (Index > Args.getNumInputArgStrings())
187 Arg *A = new Arg(UnaliasedOption, Spelling, Index - 1 - getNumArgs(),
188 Args.getArgString(Index - getNumArgs()));
189 for (unsigned i = 1; i != getNumArgs(); ++i)
190 A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
193 case JoinedOrSeparateClass: {
194 // If this is not an exact match, it is a joined arg.
195 // FIXME: Avoid strlen.
196 if (ArgSize != strlen(Args.getArgString(Index))) {
197 const char *Value = Args.getArgString(Index) + ArgSize;
198 return new Arg(*this, Spelling, Index++, Value);
201 // Otherwise it must be separate.
203 if (Index > Args.getNumInputArgStrings())
206 return new Arg(UnaliasedOption, Spelling,
207 Index - 2, Args.getArgString(Index - 1));
209 case JoinedAndSeparateClass:
212 if (Index > Args.getNumInputArgStrings())
215 return new Arg(UnaliasedOption, Spelling, Index - 2,
216 Args.getArgString(Index - 2) + ArgSize,
217 Args.getArgString(Index - 1));
218 case RemainingArgsClass: {
219 // Matches iff this is an exact match.
220 // FIXME: Avoid strlen.
221 if (ArgSize != strlen(Args.getArgString(Index)))
223 Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
224 while (Index < Args.getNumInputArgStrings())
225 A->getValues().push_back(Args.getArgString(Index++));
229 llvm_unreachable("Invalid option kind!");