blob: 8c88575764507655d93167b1288a8c52163db05d [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 Dunbar47393ba2009-07-13 21:50:47 +000094OptTable::OptTable() : Options(new Option*[numOptions]) {
95 // Explicitly zero initialize the error to work around a bug in array
96 // value-initialization on MinGW with gcc 4.3.5.
97 memset(Options, 0, sizeof(*Options) * numOptions);
98
Daniel Dunbarb3556922009-03-23 18:41:45 +000099 // Find start of normal options.
100 FirstSearchableOption = 0;
101 for (unsigned i = OPT_UNKNOWN + 1; i < LastOption; ++i) {
102 if (getInfo(i).Kind != Option::GroupClass) {
Daniel Dunbar8a7e66d2009-03-23 19:19:19 +0000103 FirstSearchableOption = i;
Daniel Dunbarb3556922009-03-23 18:41:45 +0000104 break;
105 }
106 }
107 assert(FirstSearchableOption != 0 && "No searchable options?");
108
109#ifndef NDEBUG
110 // Check that everything after the first searchable option is a
111 // regular option class.
112 for (unsigned i = FirstSearchableOption; i < LastOption; ++i) {
113 Option::OptionClass Kind = getInfo(i).Kind;
114 assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
115 Kind != Option::GroupClass) &&
116 "Special options should be defined first!");
117 }
118
119 // Check that options are in order.
120 for (unsigned i = FirstSearchableOption + 1; i < LastOption; ++i) {
121 if (!(getInfo(i - 1) < getInfo(i))) {
122 getOption((options::ID) (i - 1))->dump();
123 getOption((options::ID) i)->dump();
124 assert(0 && "Options are not in order!");
125 }
126 }
127#endif
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000128}
129
130OptTable::~OptTable() {
Daniel Dunbarb3556922009-03-23 18:41:45 +0000131 for (unsigned i = 0; i < numOptions; ++i)
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000132 delete Options[i];
133 delete[] Options;
134}
135
136unsigned OptTable::getNumOptions() const {
137 return numOptions;
138}
139
140const char *OptTable::getOptionName(options::ID id) const {
141 return getInfo(id).Name;
142}
143
Daniel Dunbarfebdf7d2009-03-31 21:26:12 +0000144unsigned OptTable::getOptionKind(options::ID id) const {
145 return getInfo(id).Kind;
146}
147
Daniel Dunbarc0d12e92009-03-31 20:12:05 +0000148const char *OptTable::getOptionHelpText(options::ID id) const {
149 return getInfo(id).HelpText;
150}
151
152const char *OptTable::getOptionMetaVar(options::ID id) const {
Daniel Dunbarfebdf7d2009-03-31 21:26:12 +0000153 return getInfo(id).MetaVar;
Daniel Dunbarc0d12e92009-03-31 20:12:05 +0000154}
155
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000156const Option *OptTable::getOption(options::ID id) const {
Daniel Dunbarb349fcc2009-03-12 03:42:54 +0000157 if (id == OPT_INVALID)
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000158 return 0;
159
160 assert((unsigned) (id - 1) < numOptions && "Invalid ID.");
161
162 Option *&Entry = Options[id - 1];
163 if (!Entry)
164 Entry = constructOption(id);
165
166 return Entry;
167}
168
169Option *OptTable::constructOption(options::ID id) const {
170 Info &info = getInfo(id);
171 const OptionGroup *Group =
172 cast_or_null<OptionGroup>(getOption((options::ID) info.GroupID));
173 const Option *Alias = getOption((options::ID) info.AliasID);
174
175 Option *Opt = 0;
176 switch (info.Kind) {
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000177 case Option::InputClass:
178 Opt = new InputOption(); break;
179 case Option::UnknownClass:
180 Opt = new UnknownOption(); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000181 case Option::GroupClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000182 Opt = new OptionGroup(id, info.Name, Group); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000183 case Option::FlagClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000184 Opt = new FlagOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000185 case Option::JoinedClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000186 Opt = new JoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000187 case Option::SeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000188 Opt = new SeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000189 case Option::CommaJoinedClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000190 Opt = new CommaJoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000191 case Option::MultiArgClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000192 Opt = new MultiArgOption(id, info.Name, Group, Alias, info.Param); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000193 case Option::JoinedOrSeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000194 Opt = new JoinedOrSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000195 case Option::JoinedAndSeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000196 Opt = new JoinedAndSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000197 }
198
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000199 for (const char *s = info.Flags; *s; ++s) {
200 switch (*s) {
Daniel Dunbar0f9098e2009-03-04 21:05:23 +0000201 default: assert(0 && "Invalid option flag.");
Daniel Dunbar6d954d72009-03-18 08:01:15 +0000202 case 'J':
203 assert(info.Kind == Option::SeparateClass && "Invalid option.");
204 Opt->setForceJoinedRender(true); break;
205 case 'S':
206 assert(info.Kind == Option::JoinedClass && "Invalid option.");
207 Opt->setForceSeparateRender(true); break;
Daniel Dunbarf6dd66b2009-03-25 06:08:46 +0000208 case 'd': Opt->setDriverOption(true); break;
Daniel Dunbar644eade2009-03-12 05:46:32 +0000209 case 'i': Opt->setNoOptAsInput(true); break;
210 case 'l': Opt->setLinkerInput(true); break;
Daniel Dunbar1e23f5f2009-04-07 19:04:18 +0000211 case 'q': Opt->setNoArgumentUnused(true); break;
Daniel Dunbar644eade2009-03-12 05:46:32 +0000212 case 'u': Opt->setUnsupported(true); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000213 }
214 }
215
216 return Opt;
217}
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000218
Daniel Dunbar7547f742009-03-23 21:50:40 +0000219// Support lower_bound between info and an option name.
220static inline bool operator<(struct Info &I, const char *Name) {
221 return StrCmpOptionName(I.Name, Name) == -1;
222}
223static inline bool operator<(const char *Name, struct Info &I) {
224 return StrCmpOptionName(Name, I.Name) == -1;
225}
226
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000227Arg *OptTable::ParseOneArg(const InputArgList &Args, unsigned &Index) const {
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000228 unsigned Prev = Index;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000229 const char *Str = Args.getArgString(Index);
230
Daniel Dunbara9480452009-03-12 08:44:47 +0000231 // Anything that doesn't start with '-' is an input, as is '-' itself.
232 if (Str[0] != '-' || Str[1] == '\0')
Daniel Dunbarb349fcc2009-03-12 03:42:54 +0000233 return new PositionalArg(getOption(OPT_INPUT), Index++);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000234
Daniel Dunbar7547f742009-03-23 21:50:40 +0000235 struct Info *Start = OptionInfos + FirstSearchableOption - 1;
236 struct Info *End = OptionInfos + LastOption - 1;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000237
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000238 // Search for the first next option which could be a prefix.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000239 Start = std::lower_bound(Start, End, Str);
240
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000241 // Options are stored in sorted order, with '\0' at the end of the
242 // alphabet. Since the only options which can accept a string must
243 // prefix it, we iteratively search for the next option which could
244 // be a prefix.
245 //
246 // FIXME: This is searching much more than necessary, but I am
247 // blanking on the simplest way to make it fast. We can solve this
248 // problem when we move to TableGen.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000249 for (; Start != End; ++Start) {
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000250 // Scan for first option which is a proper prefix.
251 for (; Start != End; ++Start)
252 if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0)
253 break;
254 if (Start == End)
Daniel Dunbar7547f742009-03-23 21:50:40 +0000255 break;
256
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000257 // See if this option matches.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000258 options::ID id = (options::ID) (Start - OptionInfos + 1);
259 if (Arg *A = getOption(id)->accept(Args, Index))
260 return A;
261
262 // Otherwise, see if this argument was missing values.
263 if (Prev != Index)
264 return 0;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000265 }
266
Daniel Dunbarb349fcc2009-03-12 03:42:54 +0000267 return new PositionalArg(getOption(OPT_UNKNOWN), Index++);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000268}
269