blob: 2e449995809785760ca187e38509dbe26d59fb8c [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;
24
25 Option::OptionClass Kind;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000026 unsigned GroupID;
27 unsigned AliasID;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000028 unsigned Param;
29};
30
Daniel Dunbarb3556922009-03-23 18:41:45 +000031// Ordering on Info. The ordering is *almost* lexicographic, with two
32// exceptions. First, '\0' comes at the end of the alphabet instead of
33// the beginning (thus options preceed any other options which prefix
34// them). Second, for options with the same name, the less permissive
35// version should come first; a Flag option should preceed a Joined
36// option, for example.
37
38static int StrCmpOptionName(const char *A, const char *B) {
39 char a = *A, b = *B;
40 while (a == b) {
41 if (a == '\0')
42 return 0;
43
44 a = *++A;
45 b = *++B;
46 }
47
48 if (a == '\0') // A is a prefix of B.
49 return 1;
50 if (b == '\0') // B is a prefix of A.
51 return -1;
52
53 // Otherwise lexicographic.
54 return (a < b) ? -1 : 1;
55}
56
57static inline bool operator<(const Info &A, const Info &B) {
58 if (&A == &B)
59 return false;
60
61 if (int N = StrCmpOptionName(A.Name, B.Name))
62 return N == -1;
63
64 // Names are the same, check that classes are in order; exactly one
65 // should be joined, and it should succeed the other.
Daniel Dunbarcf51ece2009-03-25 03:06:26 +000066 assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
Daniel Dunbarb3556922009-03-23 18:41:45 +000067 "Unexpected classes for options with same name.");
68 return B.Kind == Option::JoinedClass;
69}
70
71//
72
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000073static Info OptionInfos[] = {
Daniel Dunbar30b055f2009-03-04 21:53:04 +000074 // The InputOption info
Daniel Dunbar6d954d72009-03-18 08:01:15 +000075 { "<input>", "d", Option::InputClass, OPT_INVALID, OPT_INVALID, 0 },
Daniel Dunbar30b055f2009-03-04 21:53:04 +000076 // The UnknownOption info
Daniel Dunbarb349fcc2009-03-12 03:42:54 +000077 { "<unknown>", "", Option::UnknownClass, OPT_INVALID, OPT_INVALID, 0 },
Daniel Dunbar30b055f2009-03-04 21:53:04 +000078
Daniel Dunbarb349fcc2009-03-12 03:42:54 +000079#define OPTION(NAME, ID, KIND, GROUP, ALIAS, FLAGS, PARAM) \
80 { NAME, FLAGS, Option::KIND##Class, OPT_##GROUP, OPT_##ALIAS, PARAM },
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000081#include "clang/Driver/Options.def"
82};
83static const unsigned numOptions = sizeof(OptionInfos) / sizeof(OptionInfos[0]);
84
85static Info &getInfo(unsigned id) {
86 assert(id > 0 && id - 1 < numOptions && "Invalid Option ID.");
87 return OptionInfos[id - 1];
88}
89
Daniel Dunbarb3556922009-03-23 18:41:45 +000090OptTable::OptTable() : Options(new Option*[numOptions]()) {
91 // Find start of normal options.
92 FirstSearchableOption = 0;
93 for (unsigned i = OPT_UNKNOWN + 1; i < LastOption; ++i) {
94 if (getInfo(i).Kind != Option::GroupClass) {
Daniel Dunbar8a7e66d2009-03-23 19:19:19 +000095 FirstSearchableOption = i;
Daniel Dunbarb3556922009-03-23 18:41:45 +000096 break;
97 }
98 }
99 assert(FirstSearchableOption != 0 && "No searchable options?");
100
101#ifndef NDEBUG
102 // Check that everything after the first searchable option is a
103 // regular option class.
104 for (unsigned i = FirstSearchableOption; i < LastOption; ++i) {
105 Option::OptionClass Kind = getInfo(i).Kind;
106 assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
107 Kind != Option::GroupClass) &&
108 "Special options should be defined first!");
109 }
110
111 // Check that options are in order.
112 for (unsigned i = FirstSearchableOption + 1; i < LastOption; ++i) {
113 if (!(getInfo(i - 1) < getInfo(i))) {
114 getOption((options::ID) (i - 1))->dump();
115 getOption((options::ID) i)->dump();
116 assert(0 && "Options are not in order!");
117 }
118 }
119#endif
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000120}
121
122OptTable::~OptTable() {
Daniel Dunbarb3556922009-03-23 18:41:45 +0000123 for (unsigned i = 0; i < numOptions; ++i)
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000124 delete Options[i];
125 delete[] Options;
126}
127
128unsigned OptTable::getNumOptions() const {
129 return numOptions;
130}
131
132const char *OptTable::getOptionName(options::ID id) const {
133 return getInfo(id).Name;
134}
135
136const Option *OptTable::getOption(options::ID id) const {
Daniel Dunbarb349fcc2009-03-12 03:42:54 +0000137 if (id == OPT_INVALID)
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000138 return 0;
139
140 assert((unsigned) (id - 1) < numOptions && "Invalid ID.");
141
142 Option *&Entry = Options[id - 1];
143 if (!Entry)
144 Entry = constructOption(id);
145
146 return Entry;
147}
148
149Option *OptTable::constructOption(options::ID id) const {
150 Info &info = getInfo(id);
151 const OptionGroup *Group =
152 cast_or_null<OptionGroup>(getOption((options::ID) info.GroupID));
153 const Option *Alias = getOption((options::ID) info.AliasID);
154
155 Option *Opt = 0;
156 switch (info.Kind) {
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000157 case Option::InputClass:
158 Opt = new InputOption(); break;
159 case Option::UnknownClass:
160 Opt = new UnknownOption(); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000161 case Option::GroupClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000162 Opt = new OptionGroup(id, info.Name, Group); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000163 case Option::FlagClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000164 Opt = new FlagOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000165 case Option::JoinedClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000166 Opt = new JoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000167 case Option::SeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000168 Opt = new SeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000169 case Option::CommaJoinedClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000170 Opt = new CommaJoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000171 case Option::MultiArgClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000172 Opt = new MultiArgOption(id, info.Name, Group, Alias, info.Param); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000173 case Option::JoinedOrSeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000174 Opt = new JoinedOrSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000175 case Option::JoinedAndSeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000176 Opt = new JoinedAndSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000177 }
178
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000179 for (const char *s = info.Flags; *s; ++s) {
180 switch (*s) {
Daniel Dunbar0f9098e2009-03-04 21:05:23 +0000181 default: assert(0 && "Invalid option flag.");
Daniel Dunbar6d954d72009-03-18 08:01:15 +0000182 case 'J':
183 assert(info.Kind == Option::SeparateClass && "Invalid option.");
184 Opt->setForceJoinedRender(true); break;
185 case 'S':
186 assert(info.Kind == Option::JoinedClass && "Invalid option.");
187 Opt->setForceSeparateRender(true); break;
Daniel Dunbarf6dd66b2009-03-25 06:08:46 +0000188 case 'd': Opt->setDriverOption(true); break;
Daniel Dunbar644eade2009-03-12 05:46:32 +0000189 case 'i': Opt->setNoOptAsInput(true); break;
190 case 'l': Opt->setLinkerInput(true); break;
191 case 'u': Opt->setUnsupported(true); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000192 }
193 }
194
195 return Opt;
196}
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000197
Daniel Dunbar7547f742009-03-23 21:50:40 +0000198// Support lower_bound between info and an option name.
199static inline bool operator<(struct Info &I, const char *Name) {
200 return StrCmpOptionName(I.Name, Name) == -1;
201}
202static inline bool operator<(const char *Name, struct Info &I) {
203 return StrCmpOptionName(Name, I.Name) == -1;
204}
205
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000206Arg *OptTable::ParseOneArg(const InputArgList &Args, unsigned &Index) const {
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000207 unsigned Prev = Index;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000208 const char *Str = Args.getArgString(Index);
209
Daniel Dunbara9480452009-03-12 08:44:47 +0000210 // Anything that doesn't start with '-' is an input, as is '-' itself.
211 if (Str[0] != '-' || Str[1] == '\0')
Daniel Dunbarb349fcc2009-03-12 03:42:54 +0000212 return new PositionalArg(getOption(OPT_INPUT), Index++);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000213
Daniel Dunbar7547f742009-03-23 21:50:40 +0000214 struct Info *Start = OptionInfos + FirstSearchableOption - 1;
215 struct Info *End = OptionInfos + LastOption - 1;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000216
Daniel Dunbar7547f742009-03-23 21:50:40 +0000217 // Find the first option which could be a prefix.
218 Start = std::lower_bound(Start, End, Str);
219
220 // Scan for first option which is a proper prefix.
221 for (; Start != End; ++Start)
222 if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0)
223 break;
224
225 // Look for a match until we don't have a prefix.
226 for (; Start != End; ++Start) {
227 if (memcmp(Start->Name, Str, strlen(Start->Name)) != 0)
228 break;
229
230 options::ID id = (options::ID) (Start - OptionInfos + 1);
231 if (Arg *A = getOption(id)->accept(Args, Index))
232 return A;
233
234 // Otherwise, see if this argument was missing values.
235 if (Prev != Index)
236 return 0;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000237 }
238
Daniel Dunbarb349fcc2009-03-12 03:42:54 +0000239 return new PositionalArg(getOption(OPT_UNKNOWN), Index++);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000240}
241