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 "clang/Driver/Option.h"
12 #include "clang/Driver/Arg.h"
13 #include "clang/Driver/ArgList.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/ADT/Twine.h"
19 using namespace clang::driver;
21 Option::Option(const OptTable::Info *info, const OptTable *owner)
22 : Info(info), Owner(owner) {
24 // Multi-level aliases are not supported, and alias options cannot
25 // have groups. This just simplifies option tracking, it is not an
26 // inherent limitation.
27 assert((!Info || !getAlias().isValid() || (!getAlias().getAlias().isValid() &&
28 !getGroup().isValid())) &&
29 "Multi-level aliases and aliases with groups are unsupported.");
35 void Option::dump() const {
38 #define P(N) case N: llvm::errs() << #N; break
47 P(JoinedOrSeparateClass);
48 P(JoinedAndSeparateClass);
52 llvm::errs() << " Prefixes:[";
53 for (const char * const *Pre = Info->Prefixes; *Pre != 0; ++Pre) {
54 llvm::errs() << '"' << *Pre << (*(Pre + 1) == 0 ? "\"" : "\", ");
58 llvm::errs() << " Name:\"" << getName() << '"';
60 const Option Group = getGroup();
61 if (Group.isValid()) {
62 llvm::errs() << " Group:";
66 const Option Alias = getAlias();
67 if (Alias.isValid()) {
68 llvm::errs() << " Alias:";
72 if (getKind() == MultiArgClass)
73 llvm::errs() << " NumArgs:" << getNumArgs();
75 llvm::errs() << ">\n";
78 bool Option::matches(OptSpecifier Opt) const {
79 // Aliases are never considered in matching, look through them.
80 const Option Alias = getAlias();
82 return Alias.matches(Opt);
85 if (getID() == Opt.getID())
88 const Option Group = getGroup();
90 return Group.matches(Opt);
94 Arg *Option::accept(const ArgList &Args,
96 unsigned ArgSize) const {
97 const Option &UnaliasedOption = getUnaliasedOption();
99 // If the option was an alias, get the spelling from the unaliased one.
100 if (getID() == UnaliasedOption.getID()) {
101 Spelling = StringRef(Args.getArgString(Index), ArgSize);
103 Spelling = Args.MakeArgString(Twine(UnaliasedOption.getPrefix()) +
104 Twine(UnaliasedOption.getName()));
109 if (ArgSize != strlen(Args.getArgString(Index)))
112 return new Arg(UnaliasedOption, Spelling, Index++);
114 const char *Value = Args.getArgString(Index) + ArgSize;
115 return new Arg(UnaliasedOption, Spelling, Index++, Value);
117 case CommaJoinedClass: {
119 const char *Str = Args.getArgString(Index) + ArgSize;
120 Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
122 // Parse out the comma separated values.
123 const char *Prev = Str;
127 if (!c || c == ',') {
129 char *Value = new char[Str - Prev + 1];
130 memcpy(Value, Prev, Str - Prev);
131 Value[Str - Prev] = '\0';
132 A->getValues().push_back(Value);
141 A->setOwnsValues(true);
146 // Matches iff this is an exact match.
147 // FIXME: Avoid strlen.
148 if (ArgSize != strlen(Args.getArgString(Index)))
152 if (Index > Args.getNumInputArgStrings())
155 return new Arg(UnaliasedOption, Spelling,
156 Index - 2, Args.getArgString(Index - 1));
157 case MultiArgClass: {
158 // Matches iff this is an exact match.
159 // FIXME: Avoid strlen.
160 if (ArgSize != strlen(Args.getArgString(Index)))
163 Index += 1 + getNumArgs();
164 if (Index > Args.getNumInputArgStrings())
167 Arg *A = new Arg(UnaliasedOption, Spelling, Index - 1 - getNumArgs(),
168 Args.getArgString(Index - getNumArgs()));
169 for (unsigned i = 1; i != getNumArgs(); ++i)
170 A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
173 case JoinedOrSeparateClass: {
174 // If this is not an exact match, it is a joined arg.
175 // FIXME: Avoid strlen.
176 if (ArgSize != strlen(Args.getArgString(Index))) {
177 const char *Value = Args.getArgString(Index) + ArgSize;
178 return new Arg(*this, Spelling, Index++, Value);
181 // Otherwise it must be separate.
183 if (Index > Args.getNumInputArgStrings())
186 return new Arg(UnaliasedOption, Spelling,
187 Index - 2, Args.getArgString(Index - 1));
189 case JoinedAndSeparateClass:
192 if (Index > Args.getNumInputArgStrings())
195 return new Arg(UnaliasedOption, Spelling, Index - 2,
196 Args.getArgString(Index - 2) + ArgSize,
197 Args.getArgString(Index - 1));
199 llvm_unreachable("Invalid option kind!");