blob: 3391fe60c1afd7058accfc20111868e815b5ca34 [file] [log] [blame]
Daniel Dunbar56372082009-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 Dunbard9621da2009-03-04 22:41:37 +000012#include "clang/Driver/Arg.h"
13#include "clang/Driver/ArgList.h"
Daniel Dunbar56372082009-03-04 08:33:23 +000014#include "clang/Driver/Option.h"
Daniel Dunbar95953ce2009-03-23 21:50:40 +000015#include <algorithm>
Daniel Dunbar56372082009-03-04 08:33:23 +000016#include <cassert>
17
Daniel Dunbar56372082009-03-04 08:33:23 +000018using namespace clang::driver;
19using namespace clang::driver::options;
20
Daniel Dunbar21432d32009-11-18 17:42:34 +000021enum DriverFlag {
22 DriverOption = (1 << 0),
23 LinkerInput = (1 << 1),
24 NoArgumentUnused = (1 << 2),
25 RenderAsInput = (1 << 3),
26 RenderJoined = (1 << 4),
27 RenderSeparate = (1 << 5),
28 Unsupported = (1 << 6)
29};
30
Daniel Dunbar56372082009-03-04 08:33:23 +000031struct Info {
Daniel Dunbar56372082009-03-04 08:33:23 +000032 const char *Name;
Daniel Dunbara2072542009-03-31 20:12:05 +000033 const char *HelpText;
34 const char *MetaVar;
Daniel Dunbar71dc3752009-03-12 01:46:53 +000035
Daniel Dunbar21432d32009-11-18 17:42:34 +000036 unsigned char Kind;
37 unsigned char Flags;
38 unsigned char Param;
39 unsigned short GroupID;
40 unsigned short AliasID;
Daniel Dunbar56372082009-03-04 08:33:23 +000041};
42
Daniel Dunbar453d79a2009-03-23 18:41:45 +000043// Ordering on Info. The ordering is *almost* lexicographic, with two
44// exceptions. First, '\0' comes at the end of the alphabet instead of
45// the beginning (thus options preceed any other options which prefix
46// them). Second, for options with the same name, the less permissive
47// version should come first; a Flag option should preceed a Joined
48// option, for example.
49
50static int StrCmpOptionName(const char *A, const char *B) {
51 char a = *A, b = *B;
52 while (a == b) {
53 if (a == '\0')
54 return 0;
55
56 a = *++A;
57 b = *++B;
58 }
59
60 if (a == '\0') // A is a prefix of B.
61 return 1;
62 if (b == '\0') // B is a prefix of A.
63 return -1;
64
65 // Otherwise lexicographic.
66 return (a < b) ? -1 : 1;
67}
68
69static inline bool operator<(const Info &A, const Info &B) {
70 if (&A == &B)
71 return false;
72
73 if (int N = StrCmpOptionName(A.Name, B.Name))
74 return N == -1;
75
76 // Names are the same, check that classes are in order; exactly one
77 // should be joined, and it should succeed the other.
Daniel Dunbar0e0cf392009-03-25 03:06:26 +000078 assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
Daniel Dunbar453d79a2009-03-23 18:41:45 +000079 "Unexpected classes for options with same name.");
80 return B.Kind == Option::JoinedClass;
81}
82
83//
84
Daniel Dunbar56372082009-03-04 08:33:23 +000085static Info OptionInfos[] = {
Daniel Dunbara59194c2009-03-04 21:53:04 +000086 // The InputOption info
Daniel Dunbar21432d32009-11-18 17:42:34 +000087 { "<input>", 0, 0, Option::InputClass, DriverOption, 0, OPT_INVALID, OPT_INVALID },
Daniel Dunbara59194c2009-03-04 21:53:04 +000088 // The UnknownOption info
Daniel Dunbar21432d32009-11-18 17:42:34 +000089 { "<unknown>", 0, 0, Option::UnknownClass, 0, 0, OPT_INVALID, OPT_INVALID },
Mike Stump11289f42009-09-09 15:08:12 +000090
Daniel Dunbara2072542009-03-31 20:12:05 +000091#define OPTION(NAME, ID, KIND, GROUP, ALIAS, FLAGS, PARAM, \
92 HELPTEXT, METAVAR) \
Daniel Dunbar21432d32009-11-18 17:42:34 +000093 { NAME, HELPTEXT, METAVAR, Option::KIND##Class, FLAGS, PARAM, \
94 OPT_##GROUP, OPT_##ALIAS },
Daniel Dunbar56372082009-03-04 08:33:23 +000095#include "clang/Driver/Options.def"
96};
97static const unsigned numOptions = sizeof(OptionInfos) / sizeof(OptionInfos[0]);
98
99static Info &getInfo(unsigned id) {
100 assert(id > 0 && id - 1 < numOptions && "Invalid Option ID.");
101 return OptionInfos[id - 1];
102}
103
Daniel Dunbar9ab53d22009-07-13 21:50:47 +0000104OptTable::OptTable() : Options(new Option*[numOptions]) {
105 // Explicitly zero initialize the error to work around a bug in array
106 // value-initialization on MinGW with gcc 4.3.5.
107 memset(Options, 0, sizeof(*Options) * numOptions);
108
Daniel Dunbar453d79a2009-03-23 18:41:45 +0000109 // Find start of normal options.
110 FirstSearchableOption = 0;
111 for (unsigned i = OPT_UNKNOWN + 1; i < LastOption; ++i) {
112 if (getInfo(i).Kind != Option::GroupClass) {
Daniel Dunbaree4d1fe2009-03-23 19:19:19 +0000113 FirstSearchableOption = i;
Daniel Dunbar453d79a2009-03-23 18:41:45 +0000114 break;
115 }
116 }
117 assert(FirstSearchableOption != 0 && "No searchable options?");
118
119#ifndef NDEBUG
120 // Check that everything after the first searchable option is a
121 // regular option class.
122 for (unsigned i = FirstSearchableOption; i < LastOption; ++i) {
Daniel Dunbar21432d32009-11-18 17:42:34 +0000123 Option::OptionClass Kind = (Option::OptionClass) getInfo(i).Kind;
Daniel Dunbar453d79a2009-03-23 18:41:45 +0000124 assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
125 Kind != Option::GroupClass) &&
126 "Special options should be defined first!");
127 }
128
129 // Check that options are in order.
130 for (unsigned i = FirstSearchableOption + 1; i < LastOption; ++i) {
131 if (!(getInfo(i - 1) < getInfo(i))) {
132 getOption((options::ID) (i - 1))->dump();
133 getOption((options::ID) i)->dump();
134 assert(0 && "Options are not in order!");
135 }
136 }
Mike Stump11289f42009-09-09 15:08:12 +0000137#endif
Daniel Dunbar56372082009-03-04 08:33:23 +0000138}
139
Mike Stump11289f42009-09-09 15:08:12 +0000140OptTable::~OptTable() {
Daniel Dunbar453d79a2009-03-23 18:41:45 +0000141 for (unsigned i = 0; i < numOptions; ++i)
Daniel Dunbar56372082009-03-04 08:33:23 +0000142 delete Options[i];
143 delete[] Options;
144}
145
146unsigned OptTable::getNumOptions() const {
147 return numOptions;
148}
149
150const char *OptTable::getOptionName(options::ID id) const {
151 return getInfo(id).Name;
152}
153
Daniel Dunbar77684ea2009-03-31 21:26:12 +0000154unsigned OptTable::getOptionKind(options::ID id) const {
155 return getInfo(id).Kind;
156}
157
Daniel Dunbara2072542009-03-31 20:12:05 +0000158const char *OptTable::getOptionHelpText(options::ID id) const {
159 return getInfo(id).HelpText;
160}
161
162const char *OptTable::getOptionMetaVar(options::ID id) const {
Daniel Dunbar77684ea2009-03-31 21:26:12 +0000163 return getInfo(id).MetaVar;
Daniel Dunbara2072542009-03-31 20:12:05 +0000164}
165
Daniel Dunbar56372082009-03-04 08:33:23 +0000166const Option *OptTable::getOption(options::ID id) const {
Daniel Dunbarc727e932009-03-12 03:42:54 +0000167 if (id == OPT_INVALID)
Daniel Dunbar56372082009-03-04 08:33:23 +0000168 return 0;
169
170 assert((unsigned) (id - 1) < numOptions && "Invalid ID.");
171
172 Option *&Entry = Options[id - 1];
173 if (!Entry)
174 Entry = constructOption(id);
175
176 return Entry;
177}
178
179Option *OptTable::constructOption(options::ID id) const {
180 Info &info = getInfo(id);
Mike Stump11289f42009-09-09 15:08:12 +0000181 const OptionGroup *Group =
Daniel Dunbar56372082009-03-04 08:33:23 +0000182 cast_or_null<OptionGroup>(getOption((options::ID) info.GroupID));
183 const Option *Alias = getOption((options::ID) info.AliasID);
184
185 Option *Opt = 0;
186 switch (info.Kind) {
Daniel Dunbara59194c2009-03-04 21:53:04 +0000187 case Option::InputClass:
188 Opt = new InputOption(); break;
189 case Option::UnknownClass:
190 Opt = new UnknownOption(); break;
Daniel Dunbar56372082009-03-04 08:33:23 +0000191 case Option::GroupClass:
Daniel Dunbara59194c2009-03-04 21:53:04 +0000192 Opt = new OptionGroup(id, info.Name, Group); break;
Daniel Dunbar56372082009-03-04 08:33:23 +0000193 case Option::FlagClass:
Daniel Dunbara59194c2009-03-04 21:53:04 +0000194 Opt = new FlagOption(id, info.Name, Group, Alias); break;
Daniel Dunbar56372082009-03-04 08:33:23 +0000195 case Option::JoinedClass:
Daniel Dunbara59194c2009-03-04 21:53:04 +0000196 Opt = new JoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar56372082009-03-04 08:33:23 +0000197 case Option::SeparateClass:
Daniel Dunbara59194c2009-03-04 21:53:04 +0000198 Opt = new SeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar56372082009-03-04 08:33:23 +0000199 case Option::CommaJoinedClass:
Daniel Dunbara59194c2009-03-04 21:53:04 +0000200 Opt = new CommaJoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar56372082009-03-04 08:33:23 +0000201 case Option::MultiArgClass:
Daniel Dunbara59194c2009-03-04 21:53:04 +0000202 Opt = new MultiArgOption(id, info.Name, Group, Alias, info.Param); break;
Daniel Dunbar56372082009-03-04 08:33:23 +0000203 case Option::JoinedOrSeparateClass:
Daniel Dunbara59194c2009-03-04 21:53:04 +0000204 Opt = new JoinedOrSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar56372082009-03-04 08:33:23 +0000205 case Option::JoinedAndSeparateClass:
Daniel Dunbara59194c2009-03-04 21:53:04 +0000206 Opt = new JoinedAndSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar56372082009-03-04 08:33:23 +0000207 }
208
Daniel Dunbar21432d32009-11-18 17:42:34 +0000209 if (info.Flags & DriverOption)
210 Opt->setDriverOption(true);
211 if (info.Flags & LinkerInput)
212 Opt->setLinkerInput(true);
213 if (info.Flags & NoArgumentUnused)
214 Opt->setNoArgumentUnused(true);
215 if (info.Flags & RenderAsInput)
216 Opt->setNoOptAsInput(true);
217 if (info.Flags & RenderJoined) {
218 assert(info.Kind == Option::SeparateClass && "Invalid option.");
219 Opt->setForceJoinedRender(true);
Daniel Dunbar56372082009-03-04 08:33:23 +0000220 }
Daniel Dunbar21432d32009-11-18 17:42:34 +0000221 if (info.Flags & RenderSeparate) {
222 assert(info.Kind == Option::JoinedClass && "Invalid option.");
223 Opt->setForceSeparateRender(true);
224 }
225 if (info.Flags & Unsupported)
226 Opt->setUnsupported(true);
Daniel Dunbar56372082009-03-04 08:33:23 +0000227
228 return Opt;
229}
Daniel Dunbard9621da2009-03-04 22:41:37 +0000230
Daniel Dunbar95953ce2009-03-23 21:50:40 +0000231// Support lower_bound between info and an option name.
232static inline bool operator<(struct Info &I, const char *Name) {
233 return StrCmpOptionName(I.Name, Name) == -1;
234}
235static inline bool operator<(const char *Name, struct Info &I) {
236 return StrCmpOptionName(Name, I.Name) == -1;
237}
238
Daniel Dunbardac54a82009-03-25 04:13:45 +0000239Arg *OptTable::ParseOneArg(const InputArgList &Args, unsigned &Index) const {
Daniel Dunbard8500f32009-03-22 23:26:43 +0000240 unsigned Prev = Index;
Daniel Dunbard9621da2009-03-04 22:41:37 +0000241 const char *Str = Args.getArgString(Index);
242
Daniel Dunbard971762d2009-03-12 08:44:47 +0000243 // Anything that doesn't start with '-' is an input, as is '-' itself.
244 if (Str[0] != '-' || Str[1] == '\0')
Daniel Dunbarc727e932009-03-12 03:42:54 +0000245 return new PositionalArg(getOption(OPT_INPUT), Index++);
Daniel Dunbard9621da2009-03-04 22:41:37 +0000246
Daniel Dunbar95953ce2009-03-23 21:50:40 +0000247 struct Info *Start = OptionInfos + FirstSearchableOption - 1;
248 struct Info *End = OptionInfos + LastOption - 1;
Daniel Dunbard8500f32009-03-22 23:26:43 +0000249
Daniel Dunbarb2a7c062009-04-07 18:21:47 +0000250 // Search for the first next option which could be a prefix.
Daniel Dunbar95953ce2009-03-23 21:50:40 +0000251 Start = std::lower_bound(Start, End, Str);
252
Daniel Dunbarb2a7c062009-04-07 18:21:47 +0000253 // Options are stored in sorted order, with '\0' at the end of the
254 // alphabet. Since the only options which can accept a string must
255 // prefix it, we iteratively search for the next option which could
256 // be a prefix.
257 //
258 // FIXME: This is searching much more than necessary, but I am
259 // blanking on the simplest way to make it fast. We can solve this
260 // problem when we move to TableGen.
Daniel Dunbar95953ce2009-03-23 21:50:40 +0000261 for (; Start != End; ++Start) {
Daniel Dunbarb2a7c062009-04-07 18:21:47 +0000262 // Scan for first option which is a proper prefix.
263 for (; Start != End; ++Start)
264 if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0)
265 break;
266 if (Start == End)
Daniel Dunbar95953ce2009-03-23 21:50:40 +0000267 break;
268
Daniel Dunbarb2a7c062009-04-07 18:21:47 +0000269 // See if this option matches.
Daniel Dunbar95953ce2009-03-23 21:50:40 +0000270 options::ID id = (options::ID) (Start - OptionInfos + 1);
271 if (Arg *A = getOption(id)->accept(Args, Index))
272 return A;
273
274 // Otherwise, see if this argument was missing values.
275 if (Prev != Index)
276 return 0;
Daniel Dunbard9621da2009-03-04 22:41:37 +0000277 }
278
Daniel Dunbarc727e932009-03-12 03:42:54 +0000279 return new PositionalArg(getOption(OPT_UNKNOWN), Index++);
Daniel Dunbard9621da2009-03-04 22:41:37 +0000280}
281