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, and alias options cannot
26 // have groups. This just simplifies option tracking, it is not an
27 // inherent limitation.
28 assert((!Info || !getAlias().isValid() || (!getAlias().getAlias().isValid() &&
29 !getGroup().isValid())) &&
30 "Multi-level aliases and aliases with groups are unsupported.");
36 void Option::dump() const {
39 #define P(N) case N: llvm::errs() << #N; break
48 P(JoinedOrSeparateClass);
49 P(JoinedAndSeparateClass);
53 llvm::errs() << " Prefixes:[";
54 for (const char * const *Pre = Info->Prefixes; *Pre != 0; ++Pre) {
55 llvm::errs() << '"' << *Pre << (*(Pre + 1) == 0 ? "\"" : "\", ");
59 llvm::errs() << " Name:\"" << getName() << '"';
61 const Option Group = getGroup();
62 if (Group.isValid()) {
63 llvm::errs() << " Group:";
67 const Option Alias = getAlias();
68 if (Alias.isValid()) {
69 llvm::errs() << " Alias:";
73 if (getKind() == MultiArgClass)
74 llvm::errs() << " NumArgs:" << getNumArgs();
76 llvm::errs() << ">\n";
79 bool Option::matches(OptSpecifier Opt) const {
80 // Aliases are never considered in matching, look through them.
81 const Option Alias = getAlias();
83 return Alias.matches(Opt);
86 if (getID() == Opt.getID())
89 const Option Group = getGroup();
91 return Group.matches(Opt);
95 Arg *Option::accept(const ArgList &Args,
97 unsigned ArgSize) const {
98 const Option &UnaliasedOption = getUnaliasedOption();
100 // If the option was an alias, get the spelling from the unaliased one.
101 if (getID() == UnaliasedOption.getID()) {
102 Spelling = StringRef(Args.getArgString(Index), ArgSize);
104 Spelling = Args.MakeArgString(Twine(UnaliasedOption.getPrefix()) +
105 Twine(UnaliasedOption.getName()));
110 if (ArgSize != strlen(Args.getArgString(Index)))
113 return new Arg(UnaliasedOption, Spelling, Index++);
115 const char *Value = Args.getArgString(Index) + ArgSize;
116 return new Arg(UnaliasedOption, Spelling, Index++, Value);
118 case CommaJoinedClass: {
120 const char *Str = Args.getArgString(Index) + ArgSize;
121 Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
123 // Parse out the comma separated values.
124 const char *Prev = Str;
128 if (!c || c == ',') {
130 char *Value = new char[Str - Prev + 1];
131 memcpy(Value, Prev, Str - Prev);
132 Value[Str - Prev] = '\0';
133 A->getValues().push_back(Value);
142 A->setOwnsValues(true);
147 // Matches iff this is an exact match.
148 // FIXME: Avoid strlen.
149 if (ArgSize != strlen(Args.getArgString(Index)))
153 if (Index > Args.getNumInputArgStrings())
156 return new Arg(UnaliasedOption, Spelling,
157 Index - 2, Args.getArgString(Index - 1));
158 case MultiArgClass: {
159 // Matches iff this is an exact match.
160 // FIXME: Avoid strlen.
161 if (ArgSize != strlen(Args.getArgString(Index)))
164 Index += 1 + getNumArgs();
165 if (Index > Args.getNumInputArgStrings())
168 Arg *A = new Arg(UnaliasedOption, Spelling, Index - 1 - getNumArgs(),
169 Args.getArgString(Index - getNumArgs()));
170 for (unsigned i = 1; i != getNumArgs(); ++i)
171 A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
174 case JoinedOrSeparateClass: {
175 // If this is not an exact match, it is a joined arg.
176 // FIXME: Avoid strlen.
177 if (ArgSize != strlen(Args.getArgString(Index))) {
178 const char *Value = Args.getArgString(Index) + ArgSize;
179 return new Arg(*this, Spelling, Index++, Value);
182 // Otherwise it must be separate.
184 if (Index > Args.getNumInputArgStrings())
187 return new Arg(UnaliasedOption, Spelling,
188 Index - 2, Args.getArgString(Index - 1));
190 case JoinedAndSeparateClass:
193 if (Index > Args.getNumInputArgStrings())
196 return new Arg(UnaliasedOption, Spelling, Index - 2,
197 Args.getArgString(Index - 2) + ArgSize,
198 Args.getArgString(Index - 1));
200 llvm_unreachable("Invalid option kind!");