blob: ee1963fd331dc63c2d526b3ce4c0fe9b761dc756 [file] [log] [blame]
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001//===--- Option.cpp - Abstract Driver Options -----------------------------===//
Daniel Dunbar1eb4e642009-03-03 05:55:11 +00002//
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"
David Blaikie548f6c82011-09-23 05:57:42 +000015#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar1eb4e642009-03-03 05:55:11 +000016#include <cassert>
Daniel Dunbarbbf842b2009-03-04 23:22:02 +000017#include <algorithm>
Daniel Dunbar1eb4e642009-03-03 05:55:11 +000018using namespace clang::driver;
19
Daniel Dunbar9e1f9822009-11-19 04:14:53 +000020Option::Option(OptionClass _Kind, OptSpecifier _ID, const char *_Name,
Mike Stump1eb44332009-09-09 15:08:12 +000021 const OptionGroup *_Group, const Option *_Alias)
Daniel Dunbar9e1f9822009-11-19 04:14:53 +000022 : Kind(_Kind), ID(_ID.getID()), Name(_Name), Group(_Group), Alias(_Alias),
Daniel Dunbar0f9098e2009-03-04 21:05:23 +000023 Unsupported(false), LinkerInput(false), NoOptAsInput(false),
Daniel Dunbarcaeed1d2010-08-13 04:44:20 +000024 DriverOption(false), NoArgumentUnused(false), NoForward(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 Dunbare375c4a2010-06-09 22:31:04 +000031
32 // Initialize rendering options based on the class.
33 switch (Kind) {
34 case GroupClass:
35 case InputClass:
36 case UnknownClass:
37 RenderStyle = RenderValuesStyle;
38 break;
39
40 case JoinedClass:
41 case JoinedAndSeparateClass:
42 RenderStyle = RenderJoinedStyle;
43 break;
44
45 case CommaJoinedClass:
46 RenderStyle = RenderCommaJoinedStyle;
47 break;
48
49 case FlagClass:
50 case SeparateClass:
51 case MultiArgClass:
52 case JoinedOrSeparateClass:
53 RenderStyle = RenderSeparateStyle;
54 break;
55 }
Daniel Dunbar1eb4e642009-03-03 05:55:11 +000056}
57
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000058Option::~Option() {
59}
60
61void Option::dump() const {
62 llvm::errs() << "<";
63 switch (Kind) {
64 default:
David Blaikieb219cfc2011-09-23 05:06:16 +000065 llvm_unreachable("Invalid kind");
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000066#define P(N) case N: llvm::errs() << #N; break
67 P(GroupClass);
68 P(InputClass);
69 P(UnknownClass);
70 P(FlagClass);
71 P(JoinedClass);
72 P(SeparateClass);
73 P(CommaJoinedClass);
74 P(MultiArgClass);
75 P(JoinedOrSeparateClass);
76 P(JoinedAndSeparateClass);
77#undef P
78 }
79
80 llvm::errs() << " Name:\"" << Name << '"';
81
82 if (Group) {
83 llvm::errs() << " Group:";
84 Group->dump();
85 }
Mike Stump1eb44332009-09-09 15:08:12 +000086
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000087 if (Alias) {
88 llvm::errs() << " Alias:";
89 Alias->dump();
90 }
Mike Stump1eb44332009-09-09 15:08:12 +000091
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000092 if (const MultiArgOption *MOA = dyn_cast<MultiArgOption>(this))
93 llvm::errs() << " NumArgs:" << MOA->getNumArgs();
94
95 llvm::errs() << ">\n";
96}
97
Daniel Dunbar9e1f9822009-11-19 04:14:53 +000098bool Option::matches(OptSpecifier Opt) const {
Daniel Dunbarb32aa512009-11-19 03:26:50 +000099 // Aliases are never considered in matching, look through them.
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000100 if (Alias)
101 return Alias->matches(Opt);
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000103 // Check exact match.
104 if (ID == Opt)
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000105 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000107 if (Group)
108 return Group->matches(Opt);
109 return false;
110}
111
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000112OptionGroup::OptionGroup(OptSpecifier ID, const char *Name,
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000113 const OptionGroup *Group)
114 : Option(Option::GroupClass, ID, Name, Group, 0) {
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000115}
116
Daniel Dunbar785e7962010-06-11 22:00:17 +0000117Arg *OptionGroup::accept(const ArgList &Args, unsigned &Index) const {
David Blaikieb219cfc2011-09-23 05:06:16 +0000118 llvm_unreachable("accept() should never be called on an OptionGroup");
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000119}
120
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000121InputOption::InputOption(OptSpecifier ID)
122 : Option(Option::InputClass, ID, "<input>", 0, 0) {
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000123}
124
Daniel Dunbar785e7962010-06-11 22:00:17 +0000125Arg *InputOption::accept(const ArgList &Args, unsigned &Index) const {
David Blaikieb219cfc2011-09-23 05:06:16 +0000126 llvm_unreachable("accept() should never be called on an InputOption");
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000127}
128
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000129UnknownOption::UnknownOption(OptSpecifier ID)
130 : Option(Option::UnknownClass, ID, "<unknown>", 0, 0) {
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000131}
132
Daniel Dunbar785e7962010-06-11 22:00:17 +0000133Arg *UnknownOption::accept(const ArgList &Args, unsigned &Index) const {
David Blaikieb219cfc2011-09-23 05:06:16 +0000134 llvm_unreachable("accept() should never be called on an UnknownOption");
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000135}
136
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000137FlagOption::FlagOption(OptSpecifier ID, const char *Name,
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000138 const OptionGroup *Group, const Option *Alias)
139 : Option(Option::FlagClass, ID, Name, Group, Alias) {
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000140}
141
Daniel Dunbar785e7962010-06-11 22:00:17 +0000142Arg *FlagOption::accept(const ArgList &Args, unsigned &Index) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000143 // Matches iff this is an exact match.
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000144 // FIXME: Avoid strlen.
Douglas Gregorbcf6a802011-07-05 16:56:25 +0000145 if (getName().size() != strlen(Args.getArgString(Index)))
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000146 return 0;
147
Daniel Dunbar3856ab32010-06-09 22:44:34 +0000148 return new Arg(getUnaliasedOption(), Index++);
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000149}
150
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000151JoinedOption::JoinedOption(OptSpecifier ID, const char *Name,
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000152 const OptionGroup *Group, const Option *Alias)
153 : Option(Option::JoinedClass, ID, Name, Group, Alias) {
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000154}
155
Daniel Dunbar785e7962010-06-11 22:00:17 +0000156Arg *JoinedOption::accept(const ArgList &Args, unsigned &Index) const {
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000157 // Always matches.
Douglas Gregorbcf6a802011-07-05 16:56:25 +0000158 const char *Value = Args.getArgString(Index) + getName().size();
Daniel Dunbar3856ab32010-06-09 22:44:34 +0000159 return new Arg(getUnaliasedOption(), Index++, Value);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000160}
161
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000162CommaJoinedOption::CommaJoinedOption(OptSpecifier ID, const char *Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000163 const OptionGroup *Group,
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000164 const Option *Alias)
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000165 : Option(Option::CommaJoinedClass, ID, Name, Group, Alias) {
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000166}
167
Daniel Dunbar785e7962010-06-11 22:00:17 +0000168Arg *CommaJoinedOption::accept(const ArgList &Args,
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000169 unsigned &Index) const {
Daniel Dunbar532c1ec2010-06-09 22:31:08 +0000170 // Always matches.
Douglas Gregorbcf6a802011-07-05 16:56:25 +0000171 const char *Str = Args.getArgString(Index) + getName().size();
Daniel Dunbar3856ab32010-06-09 22:44:34 +0000172 Arg *A = new Arg(getUnaliasedOption(), Index++);
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Daniel Dunbar532c1ec2010-06-09 22:31:08 +0000174 // Parse out the comma separated values.
175 const char *Prev = Str;
176 for (;; ++Str) {
177 char c = *Str;
178
179 if (!c || c == ',') {
180 if (Prev != Str) {
181 char *Value = new char[Str - Prev + 1];
182 memcpy(Value, Prev, Str - Prev);
183 Value[Str - Prev] = '\0';
184 A->getValues().push_back(Value);
185 }
186
187 if (!c)
188 break;
189
190 Prev = Str + 1;
191 }
192 }
193 A->setOwnsValues(true);
194
195 return A;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000196}
197
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000198SeparateOption::SeparateOption(OptSpecifier ID, const char *Name,
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000199 const OptionGroup *Group, const Option *Alias)
200 : Option(Option::SeparateClass, ID, Name, Group, Alias) {
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000201}
202
Daniel Dunbar785e7962010-06-11 22:00:17 +0000203Arg *SeparateOption::accept(const ArgList &Args, unsigned &Index) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000204 // Matches iff this is an exact match.
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000205 // FIXME: Avoid strlen.
Douglas Gregorbcf6a802011-07-05 16:56:25 +0000206 if (getName().size() != strlen(Args.getArgString(Index)))
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000207 return 0;
208
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000209 Index += 2;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000210 if (Index > Args.getNumInputArgStrings())
211 return 0;
212
Daniel Dunbar3856ab32010-06-09 22:44:34 +0000213 return new Arg(getUnaliasedOption(), Index - 2, Args.getArgString(Index - 1));
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000214}
215
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000216MultiArgOption::MultiArgOption(OptSpecifier ID, const char *Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000217 const OptionGroup *Group, const Option *Alias,
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000218 unsigned _NumArgs)
219 : Option(Option::MultiArgClass, ID, Name, Group, Alias), NumArgs(_NumArgs) {
Daniel Dunbar644eade2009-03-12 05:46:32 +0000220 assert(NumArgs > 1 && "Invalid MultiArgOption!");
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000221}
222
Daniel Dunbar785e7962010-06-11 22:00:17 +0000223Arg *MultiArgOption::accept(const ArgList &Args, unsigned &Index) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000224 // Matches iff this is an exact match.
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000225 // FIXME: Avoid strlen.
Douglas Gregorbcf6a802011-07-05 16:56:25 +0000226 if (getName().size() != strlen(Args.getArgString(Index)))
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000227 return 0;
228
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000229 Index += 1 + NumArgs;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000230 if (Index > Args.getNumInputArgStrings())
231 return 0;
232
Daniel Dunbar3856ab32010-06-09 22:44:34 +0000233 Arg *A = new Arg(getUnaliasedOption(), Index - 1 - NumArgs,
234 Args.getArgString(Index - NumArgs));
Daniel Dunbar4465a772010-06-09 22:31:00 +0000235 for (unsigned i = 1; i != NumArgs; ++i)
236 A->getValues().push_back(Args.getArgString(Index - NumArgs + i));
237 return A;
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000238}
239
Daniel Dunbar312a8b72010-06-09 18:49:38 +0000240JoinedOrSeparateOption::JoinedOrSeparateOption(OptSpecifier ID,
241 const char *Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000242 const OptionGroup *Group,
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000243 const Option *Alias)
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000244 : Option(Option::JoinedOrSeparateClass, ID, Name, Group, Alias) {
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000245}
246
Daniel Dunbar785e7962010-06-11 22:00:17 +0000247Arg *JoinedOrSeparateOption::accept(const ArgList &Args,
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000248 unsigned &Index) const {
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000249 // If this is not an exact match, it is a joined arg.
250 // FIXME: Avoid strlen.
Douglas Gregorbcf6a802011-07-05 16:56:25 +0000251 if (getName().size() != strlen(Args.getArgString(Index))) {
252 const char *Value = Args.getArgString(Index) + getName().size();
Daniel Dunbar532c1ec2010-06-09 22:31:08 +0000253 return new Arg(this, Index++, Value);
Daniel Dunbar4465a772010-06-09 22:31:00 +0000254 }
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000255
256 // Otherwise it must be separate.
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000257 Index += 2;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000258 if (Index > Args.getNumInputArgStrings())
259 return 0;
260
Daniel Dunbar3856ab32010-06-09 22:44:34 +0000261 return new Arg(getUnaliasedOption(), Index - 2, Args.getArgString(Index - 1));
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000262}
263
Daniel Dunbar9e1f9822009-11-19 04:14:53 +0000264JoinedAndSeparateOption::JoinedAndSeparateOption(OptSpecifier ID,
Mike Stump1eb44332009-09-09 15:08:12 +0000265 const char *Name,
266 const OptionGroup *Group,
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000267 const Option *Alias)
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000268 : Option(Option::JoinedAndSeparateClass, ID, Name, Group, Alias) {
Daniel Dunbar1eb4e642009-03-03 05:55:11 +0000269}
270
Daniel Dunbar785e7962010-06-11 22:00:17 +0000271Arg *JoinedAndSeparateOption::accept(const ArgList &Args,
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000272 unsigned &Index) const {
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000273 // Always matches.
274
Daniel Dunbarbbf842b2009-03-04 23:22:02 +0000275 Index += 2;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000276 if (Index > Args.getNumInputArgStrings())
277 return 0;
278
Daniel Dunbar3856ab32010-06-09 22:44:34 +0000279 return new Arg(getUnaliasedOption(), Index - 2,
Douglas Gregorbcf6a802011-07-05 16:56:25 +0000280 Args.getArgString(Index-2)+getName().size(),
Daniel Dunbar3856ab32010-06-09 22:44:34 +0000281 Args.getArgString(Index-1));
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000282}