blob: 4c198fc24f75d90ca4cd0d5cd07fa9762f4f0341 [file] [log] [blame]
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +00001//===--- Options.cpp - Option info table --------------------------------*-===//
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/Options.h"
11
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +000012#include "clang/Driver/Arg.h"
13#include "clang/Driver/ArgList.h"
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000014#include "clang/Driver/Option.h"
Daniel Dunbar7547f742009-03-23 21:50:40 +000015#include <algorithm>
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000016#include <cassert>
17
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000018using namespace clang::driver;
19using namespace clang::driver::options;
20
21struct Info {
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000022 const char *Name;
Daniel Dunbare1495ec2009-03-12 01:46:53 +000023 const char *Flags;
Daniel Dunbarc0d12e92009-03-31 20:12:05 +000024 const char *HelpText;
25 const char *MetaVar;
Daniel Dunbare1495ec2009-03-12 01:46:53 +000026
27 Option::OptionClass Kind;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000028 unsigned GroupID;
29 unsigned AliasID;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000030 unsigned Param;
31};
32
Daniel Dunbarb3556922009-03-23 18:41:45 +000033// Ordering on Info. The ordering is *almost* lexicographic, with two
34// exceptions. First, '\0' comes at the end of the alphabet instead of
35// the beginning (thus options preceed any other options which prefix
36// them). Second, for options with the same name, the less permissive
37// version should come first; a Flag option should preceed a Joined
38// option, for example.
39
40static int StrCmpOptionName(const char *A, const char *B) {
41 char a = *A, b = *B;
42 while (a == b) {
43 if (a == '\0')
44 return 0;
45
46 a = *++A;
47 b = *++B;
48 }
49
50 if (a == '\0') // A is a prefix of B.
51 return 1;
52 if (b == '\0') // B is a prefix of A.
53 return -1;
54
55 // Otherwise lexicographic.
56 return (a < b) ? -1 : 1;
57}
58
59static inline bool operator<(const Info &A, const Info &B) {
60 if (&A == &B)
61 return false;
62
63 if (int N = StrCmpOptionName(A.Name, B.Name))
64 return N == -1;
65
66 // Names are the same, check that classes are in order; exactly one
67 // should be joined, and it should succeed the other.
Daniel Dunbarcf51ece2009-03-25 03:06:26 +000068 assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
Daniel Dunbarb3556922009-03-23 18:41:45 +000069 "Unexpected classes for options with same name.");
70 return B.Kind == Option::JoinedClass;
71}
72
73//
74
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000075static Info OptionInfos[] = {
Daniel Dunbar30b055f2009-03-04 21:53:04 +000076 // The InputOption info
Daniel Dunbarc0d12e92009-03-31 20:12:05 +000077 { "<input>", "d", 0, 0, Option::InputClass, OPT_INVALID, OPT_INVALID, 0 },
Daniel Dunbar30b055f2009-03-04 21:53:04 +000078 // The UnknownOption info
Daniel Dunbarc0d12e92009-03-31 20:12:05 +000079 { "<unknown>", "", 0, 0, Option::UnknownClass, OPT_INVALID, OPT_INVALID, 0 },
Daniel Dunbar30b055f2009-03-04 21:53:04 +000080
Daniel Dunbarc0d12e92009-03-31 20:12:05 +000081#define OPTION(NAME, ID, KIND, GROUP, ALIAS, FLAGS, PARAM, \
82 HELPTEXT, METAVAR) \
83 { NAME, FLAGS, HELPTEXT, METAVAR, \
84 Option::KIND##Class, OPT_##GROUP, OPT_##ALIAS, PARAM },
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000085#include "clang/Driver/Options.def"
86};
87static const unsigned numOptions = sizeof(OptionInfos) / sizeof(OptionInfos[0]);
88
89static Info &getInfo(unsigned id) {
90 assert(id > 0 && id - 1 < numOptions && "Invalid Option ID.");
91 return OptionInfos[id - 1];
92}
93
Daniel Dunbarb3556922009-03-23 18:41:45 +000094OptTable::OptTable() : Options(new Option*[numOptions]()) {
95 // Find start of normal options.
96 FirstSearchableOption = 0;
97 for (unsigned i = OPT_UNKNOWN + 1; i < LastOption; ++i) {
98 if (getInfo(i).Kind != Option::GroupClass) {
Daniel Dunbar8a7e66d2009-03-23 19:19:19 +000099 FirstSearchableOption = i;
Daniel Dunbarb3556922009-03-23 18:41:45 +0000100 break;
101 }
102 }
103 assert(FirstSearchableOption != 0 && "No searchable options?");
104
105#ifndef NDEBUG
106 // Check that everything after the first searchable option is a
107 // regular option class.
108 for (unsigned i = FirstSearchableOption; i < LastOption; ++i) {
109 Option::OptionClass Kind = getInfo(i).Kind;
110 assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
111 Kind != Option::GroupClass) &&
112 "Special options should be defined first!");
113 }
114
115 // Check that options are in order.
116 for (unsigned i = FirstSearchableOption + 1; i < LastOption; ++i) {
117 if (!(getInfo(i - 1) < getInfo(i))) {
118 getOption((options::ID) (i - 1))->dump();
119 getOption((options::ID) i)->dump();
120 assert(0 && "Options are not in order!");
121 }
122 }
123#endif
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000124}
125
126OptTable::~OptTable() {
Daniel Dunbarb3556922009-03-23 18:41:45 +0000127 for (unsigned i = 0; i < numOptions; ++i)
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000128 delete Options[i];
129 delete[] Options;
130}
131
132unsigned OptTable::getNumOptions() const {
133 return numOptions;
134}
135
136const char *OptTable::getOptionName(options::ID id) const {
137 return getInfo(id).Name;
138}
139
Daniel Dunbarc0d12e92009-03-31 20:12:05 +0000140const char *OptTable::getOptionHelpText(options::ID id) const {
141 return getInfo(id).HelpText;
142}
143
144const char *OptTable::getOptionMetaVar(options::ID id) const {
145 const char *Name = getInfo(id).MetaVar;
146 // FIXME: This will need translation.
147 return Name ? Name : "<var>";
148}
149
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000150const Option *OptTable::getOption(options::ID id) const {
Daniel Dunbarb349fcc2009-03-12 03:42:54 +0000151 if (id == OPT_INVALID)
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000152 return 0;
153
154 assert((unsigned) (id - 1) < numOptions && "Invalid ID.");
155
156 Option *&Entry = Options[id - 1];
157 if (!Entry)
158 Entry = constructOption(id);
159
160 return Entry;
161}
162
163Option *OptTable::constructOption(options::ID id) const {
164 Info &info = getInfo(id);
165 const OptionGroup *Group =
166 cast_or_null<OptionGroup>(getOption((options::ID) info.GroupID));
167 const Option *Alias = getOption((options::ID) info.AliasID);
168
169 Option *Opt = 0;
170 switch (info.Kind) {
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000171 case Option::InputClass:
172 Opt = new InputOption(); break;
173 case Option::UnknownClass:
174 Opt = new UnknownOption(); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000175 case Option::GroupClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000176 Opt = new OptionGroup(id, info.Name, Group); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000177 case Option::FlagClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000178 Opt = new FlagOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000179 case Option::JoinedClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000180 Opt = new JoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000181 case Option::SeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000182 Opt = new SeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000183 case Option::CommaJoinedClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000184 Opt = new CommaJoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000185 case Option::MultiArgClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000186 Opt = new MultiArgOption(id, info.Name, Group, Alias, info.Param); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000187 case Option::JoinedOrSeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000188 Opt = new JoinedOrSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000189 case Option::JoinedAndSeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000190 Opt = new JoinedAndSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000191 }
192
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000193 for (const char *s = info.Flags; *s; ++s) {
194 switch (*s) {
Daniel Dunbar0f9098e2009-03-04 21:05:23 +0000195 default: assert(0 && "Invalid option flag.");
Daniel Dunbar6d954d72009-03-18 08:01:15 +0000196 case 'J':
197 assert(info.Kind == Option::SeparateClass && "Invalid option.");
198 Opt->setForceJoinedRender(true); break;
199 case 'S':
200 assert(info.Kind == Option::JoinedClass && "Invalid option.");
201 Opt->setForceSeparateRender(true); break;
Daniel Dunbarf6dd66b2009-03-25 06:08:46 +0000202 case 'd': Opt->setDriverOption(true); break;
Daniel Dunbar644eade2009-03-12 05:46:32 +0000203 case 'i': Opt->setNoOptAsInput(true); break;
204 case 'l': Opt->setLinkerInput(true); break;
205 case 'u': Opt->setUnsupported(true); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000206 }
207 }
208
209 return Opt;
210}
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000211
Daniel Dunbar7547f742009-03-23 21:50:40 +0000212// Support lower_bound between info and an option name.
213static inline bool operator<(struct Info &I, const char *Name) {
214 return StrCmpOptionName(I.Name, Name) == -1;
215}
216static inline bool operator<(const char *Name, struct Info &I) {
217 return StrCmpOptionName(Name, I.Name) == -1;
218}
219
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000220Arg *OptTable::ParseOneArg(const InputArgList &Args, unsigned &Index) const {
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000221 unsigned Prev = Index;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000222 const char *Str = Args.getArgString(Index);
223
Daniel Dunbara9480452009-03-12 08:44:47 +0000224 // Anything that doesn't start with '-' is an input, as is '-' itself.
225 if (Str[0] != '-' || Str[1] == '\0')
Daniel Dunbarb349fcc2009-03-12 03:42:54 +0000226 return new PositionalArg(getOption(OPT_INPUT), Index++);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000227
Daniel Dunbar7547f742009-03-23 21:50:40 +0000228 struct Info *Start = OptionInfos + FirstSearchableOption - 1;
229 struct Info *End = OptionInfos + LastOption - 1;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000230
Daniel Dunbar7547f742009-03-23 21:50:40 +0000231 // Find the first option which could be a prefix.
232 Start = std::lower_bound(Start, End, Str);
233
234 // Scan for first option which is a proper prefix.
235 for (; Start != End; ++Start)
236 if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0)
237 break;
238
239 // Look for a match until we don't have a prefix.
240 for (; Start != End; ++Start) {
241 if (memcmp(Start->Name, Str, strlen(Start->Name)) != 0)
242 break;
243
244 options::ID id = (options::ID) (Start - OptionInfos + 1);
245 if (Arg *A = getOption(id)->accept(Args, Index))
246 return A;
247
248 // Otherwise, see if this argument was missing values.
249 if (Prev != Index)
250 return 0;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000251 }
252
Daniel Dunbarb349fcc2009-03-12 03:42:54 +0000253 return new PositionalArg(getOption(OPT_UNKNOWN), Index++);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000254}
255