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);
59 llvm::errs() << " Prefixes:[";
60 for (const char * const *Pre = Info->Prefixes; *Pre != 0; ++Pre) {
61 llvm::errs() << '"' << *Pre << (*(Pre + 1) == 0 ? "\"" : "\", ");
66 llvm::errs() << " Name:\"" << getName() << '"';
68 const Option Group = getGroup();
69 if (Group.isValid()) {
70 llvm::errs() << " Group:";
74 const Option Alias = getAlias();
75 if (Alias.isValid()) {
76 llvm::errs() << " Alias:";
80 if (getKind() == MultiArgClass)
81 llvm::errs() << " NumArgs:" << getNumArgs();
83 llvm::errs() << ">\n";
86 bool Option::matches(OptSpecifier Opt) const {
87 // Aliases are never considered in matching, look through them.
88 const Option Alias = getAlias();
90 return Alias.matches(Opt);
93 if (getID() == Opt.getID())
96 const Option Group = getGroup();
98 return Group.matches(Opt);
102 Arg *Option::accept(const ArgList &Args,
104 unsigned ArgSize) const {
105 const Option &UnaliasedOption = getUnaliasedOption();
107 // If the option was an alias, get the spelling from the unaliased one.
108 if (getID() == UnaliasedOption.getID()) {
109 Spelling = StringRef(Args.getArgString(Index), ArgSize);
111 Spelling = Args.MakeArgString(Twine(UnaliasedOption.getPrefix()) +
112 Twine(UnaliasedOption.getName()));
117 if (ArgSize != strlen(Args.getArgString(Index)))
120 Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
121 if (getAliasArgs()) {
122 const char *Val = getAliasArgs();
123 while (*Val != '\0') {
124 A->getValues().push_back(Val);
126 // Move past the '\0' to the next argument.
127 Val += strlen(Val) + 1;
133 const char *Value = Args.getArgString(Index) + ArgSize;
134 return new Arg(UnaliasedOption, Spelling, Index++, Value);
136 case CommaJoinedClass: {
138 const char *Str = Args.getArgString(Index) + ArgSize;
139 Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
141 // Parse out the comma separated values.
142 const char *Prev = Str;
146 if (!c || c == ',') {
148 char *Value = new char[Str - Prev + 1];
149 memcpy(Value, Prev, Str - Prev);
150 Value[Str - Prev] = '\0';
151 A->getValues().push_back(Value);
160 A->setOwnsValues(true);
165 // Matches iff this is an exact match.
166 // FIXME: Avoid strlen.
167 if (ArgSize != strlen(Args.getArgString(Index)))
171 if (Index > Args.getNumInputArgStrings())
174 return new Arg(UnaliasedOption, Spelling,
175 Index - 2, Args.getArgString(Index - 1));
176 case MultiArgClass: {
177 // Matches iff this is an exact match.
178 // FIXME: Avoid strlen.
179 if (ArgSize != strlen(Args.getArgString(Index)))
182 Index += 1 + getNumArgs();
183 if (Index > Args.getNumInputArgStrings())
186 Arg *A = new Arg(UnaliasedOption, Spelling, Index - 1 - getNumArgs(),
187 Args.getArgString(Index - getNumArgs()));
188 for (unsigned i = 1; i != getNumArgs(); ++i)
189 A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
192 case JoinedOrSeparateClass: {
193 // If this is not an exact match, it is a joined arg.
194 // FIXME: Avoid strlen.
195 if (ArgSize != strlen(Args.getArgString(Index))) {
196 const char *Value = Args.getArgString(Index) + ArgSize;
197 return new Arg(*this, Spelling, Index++, Value);
200 // Otherwise it must be separate.
202 if (Index > Args.getNumInputArgStrings())
205 return new Arg(UnaliasedOption, Spelling,
206 Index - 2, Args.getArgString(Index - 1));
208 case JoinedAndSeparateClass:
211 if (Index > Args.getNumInputArgStrings())
214 return new Arg(UnaliasedOption, Spelling, Index - 2,
215 Args.getArgString(Index - 2) + ArgSize,
216 Args.getArgString(Index - 1));
218 llvm_unreachable("Invalid option kind!");