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"
11 #include "clang/Driver/Arg.h"
12 #include "clang/Driver/ArgList.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include "llvm/Support/raw_ostream.h"
18 using namespace clang::driver;
20 Option::Option(const OptTable::Info *info, const OptTable *owner)
21 : Info(info), Owner(owner) {
23 // Multi-level aliases are not supported, and alias options cannot
24 // have groups. This just simplifies option tracking, it is not an
25 // inherent limitation.
26 assert((!Info || !getAlias().isValid() || (!getAlias().getAlias().isValid() &&
27 !getGroup().isValid())) &&
28 "Multi-level aliases and aliases with groups are unsupported.");
34 void Option::dump() const {
37 #define P(N) case N: llvm::errs() << #N; break
46 P(JoinedOrSeparateClass);
47 P(JoinedAndSeparateClass);
51 llvm::errs() << " Prefixes:[";
52 for (const char * const *Pre = Info->Prefixes; *Pre != 0; ++Pre) {
53 llvm::errs() << '"' << *Pre << (*(Pre + 1) == 0 ? "\"" : "\", ");
57 llvm::errs() << " Name:\"" << getName() << '"';
59 const Option Group = getGroup();
60 if (Group.isValid()) {
61 llvm::errs() << " Group:";
65 const Option Alias = getAlias();
66 if (Alias.isValid()) {
67 llvm::errs() << " Alias:";
71 if (getKind() == MultiArgClass)
72 llvm::errs() << " NumArgs:" << getNumArgs();
74 llvm::errs() << ">\n";
77 bool Option::matches(OptSpecifier Opt) const {
78 // Aliases are never considered in matching, look through them.
79 const Option Alias = getAlias();
81 return Alias.matches(Opt);
84 if (getID() == Opt.getID())
87 const Option Group = getGroup();
89 return Group.matches(Opt);
93 Arg *Option::accept(const ArgList &Args,
95 unsigned ArgSize) const {
96 const Option &UnaliasedOption = getUnaliasedOption();
98 // If the option was an alias, get the spelling from the unaliased one.
99 if (getID() == UnaliasedOption.getID()) {
100 Spelling = StringRef(Args.getArgString(Index), ArgSize);
102 Spelling = Args.MakeArgString(Twine(UnaliasedOption.getPrefix()) +
103 Twine(UnaliasedOption.getName()));
108 if (ArgSize != strlen(Args.getArgString(Index)))
111 return new Arg(UnaliasedOption, Spelling, Index++);
113 const char *Value = Args.getArgString(Index) + ArgSize;
114 return new Arg(UnaliasedOption, Spelling, Index++, Value);
116 case CommaJoinedClass: {
118 const char *Str = Args.getArgString(Index) + ArgSize;
119 Arg *A = new Arg(UnaliasedOption, Spelling, Index++);
121 // Parse out the comma separated values.
122 const char *Prev = Str;
126 if (!c || c == ',') {
128 char *Value = new char[Str - Prev + 1];
129 memcpy(Value, Prev, Str - Prev);
130 Value[Str - Prev] = '\0';
131 A->getValues().push_back(Value);
140 A->setOwnsValues(true);
145 // Matches iff this is an exact match.
146 // FIXME: Avoid strlen.
147 if (ArgSize != strlen(Args.getArgString(Index)))
151 if (Index > Args.getNumInputArgStrings())
154 return new Arg(UnaliasedOption, Spelling,
155 Index - 2, Args.getArgString(Index - 1));
156 case MultiArgClass: {
157 // Matches iff this is an exact match.
158 // FIXME: Avoid strlen.
159 if (ArgSize != strlen(Args.getArgString(Index)))
162 Index += 1 + getNumArgs();
163 if (Index > Args.getNumInputArgStrings())
166 Arg *A = new Arg(UnaliasedOption, Spelling, Index - 1 - getNumArgs(),
167 Args.getArgString(Index - getNumArgs()));
168 for (unsigned i = 1; i != getNumArgs(); ++i)
169 A->getValues().push_back(Args.getArgString(Index - getNumArgs() + i));
172 case JoinedOrSeparateClass: {
173 // If this is not an exact match, it is a joined arg.
174 // FIXME: Avoid strlen.
175 if (ArgSize != strlen(Args.getArgString(Index))) {
176 const char *Value = Args.getArgString(Index) + ArgSize;
177 return new Arg(*this, Spelling, Index++, Value);
180 // Otherwise it must be separate.
182 if (Index > Args.getNumInputArgStrings())
185 return new Arg(UnaliasedOption, Spelling,
186 Index - 2, Args.getArgString(Index - 1));
188 case JoinedAndSeparateClass:
191 if (Index > Args.getNumInputArgStrings())
194 return new Arg(UnaliasedOption, Spelling, Index - 2,
195 Args.getArgString(Index - 2) + ArgSize,
196 Args.getArgString(Index - 1));
198 llvm_unreachable("Invalid option kind!");