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);
54 llvm::errs() << " Prefixes:[";
55 for (const char * const *Pre = Info->Prefixes; *Pre != 0; ++Pre) {
56 llvm::errs() << '"' << *Pre << (*(Pre + 1) == 0 ? "\"" : "\", ");
61 llvm::errs() << " Name:\"" << getName() << '"';
63 const Option Group = getGroup();
64 if (Group.isValid()) {
65 llvm::errs() << " Group:";
69 const Option Alias = getAlias();
70 if (Alias.isValid()) {
71 llvm::errs() << " Alias:";
75 if (getKind() == MultiArgClass)
76 llvm::errs() << " NumArgs:" << getNumArgs();
78 llvm::errs() << ">\n";
81 bool Option::matches(OptSpecifier Opt) const {
82 // Aliases are never considered in matching, look through them.
83 const Option Alias = getAlias();
85 return Alias.matches(Opt);
88 if (getID() == Opt.getID())
91 const Option Group = getGroup();
93 return Group.matches(Opt);
97 Arg *Option::accept(const ArgList &Args,
99 unsigned ArgSize) const {
100 const Option &UnaliasedOption = getUnaliasedOption();
102 // If the option was an alias, get the spelling from the unaliased one.
103 if (getID() == UnaliasedOption.getID()) {
104 Spelling = StringRef(Args.getArgString(Index), ArgSize);
106 Spelling = Args.MakeArgString(Twine(UnaliasedOption.getPrefix()) +
107 Twine(UnaliasedOption.getName()));
112 if (ArgSize != strlen(Args.getArgString(Index)))
115 return new Arg(UnaliasedOption, Spelling, Index++);
117 const char *Value = Args.getArgString(Index) + ArgSize;
118 return new Arg(UnaliasedOption, Spelling, Index++, Value);
120 case CommaJoinedClass: {
122 const char *Str = Args.getArgString(Index) + ArgSize;
123 Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
125 // Parse out the comma separated values.
126 const char *Prev = Str;
130 if (!c || c == ',') {
132 char *Value = new char[Str - Prev + 1];
133 memcpy(Value, Prev, Str - Prev);
134 Value[Str - Prev] = '\0';
135 A->getValues().push_back(Value);
144 A->setOwnsValues(true);
149 // Matches iff this is an exact match.
150 // FIXME: Avoid strlen.
151 if (ArgSize != strlen(Args.getArgString(Index)))
155 if (Index > Args.getNumInputArgStrings())
158 return new Arg(UnaliasedOption, Spelling,
159 Index - 2, Args.getArgString(Index - 1));
160 case MultiArgClass: {
161 // Matches iff this is an exact match.
162 // FIXME: Avoid strlen.
163 if (ArgSize != strlen(Args.getArgString(Index)))
166 Index += 1 + getNumArgs();
167 if (Index > Args.getNumInputArgStrings())
170 Arg *A = new Arg(UnaliasedOption, Spelling, Index - 1 - getNumArgs(),
171 Args.getArgString(Index - getNumArgs()));
172 for (unsigned i = 1; i != getNumArgs(); ++i)
173 A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
176 case JoinedOrSeparateClass: {
177 // If this is not an exact match, it is a joined arg.
178 // FIXME: Avoid strlen.
179 if (ArgSize != strlen(Args.getArgString(Index))) {
180 const char *Value = Args.getArgString(Index) + ArgSize;
181 return new Arg(*this, Spelling, Index++, Value);
184 // Otherwise it must be separate.
186 if (Index > Args.getNumInputArgStrings())
189 return new Arg(UnaliasedOption, Spelling,
190 Index - 2, Args.getArgString(Index - 1));
192 case JoinedAndSeparateClass:
195 if (Index > Args.getNumInputArgStrings())
198 return new Arg(UnaliasedOption, Spelling, Index - 2,
199 Args.getArgString(Index - 2) + ArgSize,
200 Args.getArgString(Index - 1));
202 llvm_unreachable("Invalid option kind!");