blob: c2ace05aa4d13860af95042ee3f9499647c2427a [file] [log] [blame]
Daniel Dunbar1eb4e642009-03-03 05:55:11 +00001//===--- Option.cpp - Abstract Driver Options ---------------------------*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/Driver/Option.h"
Daniel Dunbarbbf842b2009-03-04 23:22:02 +000011
12#include "clang/Driver/Arg.h"
13#include "clang/Driver/ArgList.h"
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000014#include "llvm/Support/raw_ostream.h"
Daniel Dunbar1eb4e642009-03-03 05:55:11 +000015#include <cassert>
Daniel Dunbarbbf842b2009-03-04 23:22:02 +000016#include <algorithm>
Daniel Dunbar1eb4e642009-03-03 05:55:11 +000017using namespace clang::driver;
18
Daniel Dunbar30b055f2009-03-04 21:53:04 +000019Option::Option(OptionClass _Kind, options::ID _ID, const char *_Name,
Mike Stump1eb44332009-09-09 15:08:12 +000020 const OptionGroup *_Group, const Option *_Alias)
Daniel Dunbar30b055f2009-03-04 21:53:04 +000021 : Kind(_Kind), ID(_ID), Name(_Name), Group(_Group), Alias(_Alias),
Daniel Dunbar0f9098e2009-03-04 21:05:23 +000022 Unsupported(false), LinkerInput(false), NoOptAsInput(false),
Daniel Dunbar6d954d72009-03-18 08:01:15 +000023 ForceSeparateRender(false), ForceJoinedRender(false),
Mike Stump1eb44332009-09-09 15:08:12 +000024 DriverOption(false), NoArgumentUnused(false) {
Daniel Dunbar1eb4e642009-03-03 05:55:11 +000025
26 // Multi-level aliases are not supported, and alias options cannot
27 // have groups. This just simplifies option tracking, it is not an
28 // inherent limitation.
29 assert((!Alias || (!Alias->Alias && !Group)) &&
Mike Stump1eb44332009-09-09 15:08:12 +000030 "Multi-level aliases and aliases with groups are unsupported.");
Daniel Dunbar1eb4e642009-03-03 05:55:11 +000031}
32
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000033Option::~Option() {
34}
35
36void Option::dump() const {
37 llvm::errs() << "<";
38 switch (Kind) {
39 default:
40 assert(0 && "Invalid kind");
41#define P(N) case N: llvm::errs() << #N; break
42 P(GroupClass);
43 P(InputClass);
44 P(UnknownClass);
45 P(FlagClass);
46 P(JoinedClass);
47 P(SeparateClass);
48 P(CommaJoinedClass);
49 P(MultiArgClass);
50 P(JoinedOrSeparateClass);
51 P(JoinedAndSeparateClass);
52#undef P
53 }
54
55 llvm::errs() << " Name:\"" << Name << '"';
56
57 if (Group) {
58 llvm::errs() << " Group:";
59 Group->dump();
60 }
Mike Stump1eb44332009-09-09 15:08:12 +000061
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000062 if (Alias) {
63 llvm::errs() << " Alias:";
64 Alias->dump();
65 }
Mike Stump1eb44332009-09-09 15:08:12 +000066
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000067 if (const MultiArgOption *MOA = dyn_cast<MultiArgOption>(this))
68 llvm::errs() << " NumArgs:" << MOA->getNumArgs();
69
70 llvm::errs() << ">\n";
71}
72
Daniel Dunbar1eb4e642009-03-03 05:55:11 +000073bool Option::matches(const Option *Opt) const {
74 // Aliases are never considered in matching.
75 if (Opt->getAlias())
76 return matches(Opt->getAlias());
77 if (Alias)
78 return Alias->matches(Opt);
Mike Stump1eb44332009-09-09 15:08:12 +000079
Daniel Dunbar1eb4e642009-03-03 05:55:11 +000080 if (this == Opt)
81 return true;
Mike Stump1eb44332009-09-09 15:08:12 +000082
Daniel Dunbar1eb4e642009-03-03 05:55:11 +000083 if (Group)
84 return Group->matches(Opt);
85 return false;
86}
87
Daniel Dunbarcf0dd152009-03-12 01:34:20 +000088bool Option::matches(options::ID Id) const {
89 // FIXME: Decide what to do here; we should either pull out the
90 // handling of alias on the option for Id from the other matches, or
91 // find some other solution (which hopefully doesn't require using
92 // the option table).
93 if (Alias)
94 return Alias->matches(Id);
Mike Stump1eb44332009-09-09 15:08:12 +000095
Daniel Dunbarcf0dd152009-03-12 01:34:20 +000096 if (ID == Id)
97 return true;
Mike Stump1eb44332009-09-09 15:08:12 +000098
Daniel Dunbarcf0dd152009-03-12 01:34:20 +000099 if (Group)
100 return Group->matches(Id);
101 return false;
102}
103
Mike Stump1eb44332009-09-09 15:08:12 +0000104OptionGroup::OptionGroup(options::ID ID, const char *Name,
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000105 const OptionGroup *Group)
106 : Option(Option::GroupClass, ID, Name, Group, 0) {
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000107}
108
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000109Arg *OptionGroup::accept(const InputArgList &Args, unsigned &Index) const {
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000110 assert(0 && "accept() should never be called on an OptionGroup");
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000111 return 0;
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000112}
113
114InputOption::InputOption()
Daniel Dunbarb349fcc2009-03-12 03:42:54 +0000115 : Option(Option::InputClass, options::OPT_INPUT, "<input>", 0, 0) {
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000116}
117
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000118Arg *InputOption::accept(const InputArgList &Args, unsigned &Index) const {
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000119 assert(0 && "accept() should never be called on an InputOption");
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000120 return 0;
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000121}
122
123UnknownOption::UnknownOption()
Daniel Dunbarb349fcc2009-03-12 03:42:54 +0000124 : Option(Option::UnknownClass, options::OPT_UNKNOWN, "<unknown>", 0, 0) {
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000125}
126
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000127Arg *UnknownOption::accept(const InputArgList &Args, unsigned &Index) const {
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000128 assert(0 && "accept() should never be called on an UnknownOption");
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000129 return 0;
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000130}
131
Mike Stump1eb44332009-09-09 15:08:12 +0000132FlagOption::FlagOption(options::ID ID, const char *Name,
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000133 const OptionGroup *Group, const Option *Alias)
134 : Option(Option::FlagClass, ID, Name, Group, Alias) {
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000135}
136
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000137Arg *FlagOption::accept(const InputArgList &Args, unsigned &Index) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000138 // Matches iff this is an exact match.
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000139 // FIXME: Avoid strlen.
140 if (strlen(getName()) != strlen(Args.getArgString(Index)))
141 return 0;
142
Daniel Dunbar06482622009-03-05 06:38:47 +0000143 return new FlagArg(this, Index++);
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000144}
145
Mike Stump1eb44332009-09-09 15:08:12 +0000146JoinedOption::JoinedOption(options::ID ID, const char *Name,
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000147 const OptionGroup *Group, const Option *Alias)
148 : Option(Option::JoinedClass, ID, Name, Group, Alias) {
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000149}
150
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000151Arg *JoinedOption::accept(const InputArgList &Args, unsigned &Index) const {
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000152 // Always matches.
153 return new JoinedArg(this, Index++);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000154}
155
Mike Stump1eb44332009-09-09 15:08:12 +0000156CommaJoinedOption::CommaJoinedOption(options::ID ID, const char *Name,
157 const OptionGroup *Group,
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000158 const Option *Alias)
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000159 : Option(Option::CommaJoinedClass, ID, Name, Group, Alias) {
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000160}
161
Mike Stump1eb44332009-09-09 15:08:12 +0000162Arg *CommaJoinedOption::accept(const InputArgList &Args,
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000163 unsigned &Index) const {
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000164 // Always matches. We count the commas now so we can answer
165 // getNumValues easily.
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000167 // Get the suffix string.
168 // FIXME: Avoid strlen, and move to helper method?
169 const char *Suffix = Args.getArgString(Index) + strlen(getName());
Daniel Dunbar06482622009-03-05 06:38:47 +0000170 return new CommaJoinedArg(this, Index++, Suffix);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000171}
172
Mike Stump1eb44332009-09-09 15:08:12 +0000173SeparateOption::SeparateOption(options::ID ID, const char *Name,
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000174 const OptionGroup *Group, const Option *Alias)
175 : Option(Option::SeparateClass, ID, Name, Group, Alias) {
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000176}
177
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000178Arg *SeparateOption::accept(const InputArgList &Args, unsigned &Index) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000179 // Matches iff this is an exact match.
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000180 // FIXME: Avoid strlen.
181 if (strlen(getName()) != strlen(Args.getArgString(Index)))
182 return 0;
183
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000184 Index += 2;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000185 if (Index > Args.getNumInputArgStrings())
186 return 0;
187
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000188 return new SeparateArg(this, Index - 2, 1);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000189}
190
Mike Stump1eb44332009-09-09 15:08:12 +0000191MultiArgOption::MultiArgOption(options::ID ID, const char *Name,
192 const OptionGroup *Group, const Option *Alias,
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000193 unsigned _NumArgs)
194 : Option(Option::MultiArgClass, ID, Name, Group, Alias), NumArgs(_NumArgs) {
Daniel Dunbar644eade2009-03-12 05:46:32 +0000195 assert(NumArgs > 1 && "Invalid MultiArgOption!");
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000196}
197
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000198Arg *MultiArgOption::accept(const InputArgList &Args, unsigned &Index) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000199 // Matches iff this is an exact match.
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000200 // FIXME: Avoid strlen.
201 if (strlen(getName()) != strlen(Args.getArgString(Index)))
202 return 0;
203
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000204 Index += 1 + NumArgs;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000205 if (Index > Args.getNumInputArgStrings())
206 return 0;
207
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000208 return new SeparateArg(this, Index - 1 - NumArgs, NumArgs);
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000209}
210
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000211JoinedOrSeparateOption::JoinedOrSeparateOption(options::ID ID, const char *Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000212 const OptionGroup *Group,
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000213 const Option *Alias)
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000214 : Option(Option::JoinedOrSeparateClass, ID, Name, Group, Alias) {
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000215}
216
Mike Stump1eb44332009-09-09 15:08:12 +0000217Arg *JoinedOrSeparateOption::accept(const InputArgList &Args,
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000218 unsigned &Index) const {
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000219 // If this is not an exact match, it is a joined arg.
220 // FIXME: Avoid strlen.
221 if (strlen(getName()) != strlen(Args.getArgString(Index)))
222 return new JoinedArg(this, Index++);
223
224 // Otherwise it must be separate.
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000225 Index += 2;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000226 if (Index > Args.getNumInputArgStrings())
227 return 0;
228
Mike Stump1eb44332009-09-09 15:08:12 +0000229 return new SeparateArg(this, Index - 2, 1);
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000230}
231
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000232JoinedAndSeparateOption::JoinedAndSeparateOption(options::ID ID,
Mike Stump1eb44332009-09-09 15:08:12 +0000233 const char *Name,
234 const OptionGroup *Group,
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000235 const Option *Alias)
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000236 : Option(Option::JoinedAndSeparateClass, ID, Name, Group, Alias) {
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000237}
238
Mike Stump1eb44332009-09-09 15:08:12 +0000239Arg *JoinedAndSeparateOption::accept(const InputArgList &Args,
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000240 unsigned &Index) const {
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000241 // Always matches.
242
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000243 Index += 2;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000244 if (Index > Args.getNumInputArgStrings())
245 return 0;
246
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000247 return new JoinedAndSeparateArg(this, Index - 2);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000248}
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000249