Add LLVM runtime checks to build path
[clang/acc.git] / lib / Driver / OptTable.cpp
blob8c88575764507655d93167b1288a8c52163db05d
1 //===--- 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 //===----------------------------------------------------------------------===//
10 #include "clang/Driver/Options.h"
12 #include "clang/Driver/Arg.h"
13 #include "clang/Driver/ArgList.h"
14 #include "clang/Driver/Option.h"
15 #include <algorithm>
16 #include <cassert>
18 using namespace clang::driver;
19 using namespace clang::driver::options;
21 struct Info {
22 const char *Name;
23 const char *Flags;
24 const char *HelpText;
25 const char *MetaVar;
27 Option::OptionClass Kind;
28 unsigned GroupID;
29 unsigned AliasID;
30 unsigned Param;
33 // 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.
40 static 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;
46 a = *++A;
47 b = *++B;
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;
55 // Otherwise lexicographic.
56 return (a < b) ? -1 : 1;
59 static inline bool operator<(const Info &A, const Info &B) {
60 if (&A == &B)
61 return false;
63 if (int N = StrCmpOptionName(A.Name, B.Name))
64 return N == -1;
66 // Names are the same, check that classes are in order; exactly one
67 // should be joined, and it should succeed the other.
68 assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
69 "Unexpected classes for options with same name.");
70 return B.Kind == Option::JoinedClass;
75 static Info OptionInfos[] = {
76 // The InputOption info
77 { "<input>", "d", 0, 0, Option::InputClass, OPT_INVALID, OPT_INVALID, 0 },
78 // The UnknownOption info
79 { "<unknown>", "", 0, 0, Option::UnknownClass, OPT_INVALID, OPT_INVALID, 0 },
81 #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 },
85 #include "clang/Driver/Options.def"
87 static const unsigned numOptions = sizeof(OptionInfos) / sizeof(OptionInfos[0]);
89 static Info &getInfo(unsigned id) {
90 assert(id > 0 && id - 1 < numOptions && "Invalid Option ID.");
91 return OptionInfos[id - 1];
94 OptTable::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);
99 // 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) {
103 FirstSearchableOption = i;
104 break;
107 assert(FirstSearchableOption != 0 && "No searchable options?");
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!");
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!");
127 #endif
130 OptTable::~OptTable() {
131 for (unsigned i = 0; i < numOptions; ++i)
132 delete Options[i];
133 delete[] Options;
136 unsigned OptTable::getNumOptions() const {
137 return numOptions;
140 const char *OptTable::getOptionName(options::ID id) const {
141 return getInfo(id).Name;
144 unsigned OptTable::getOptionKind(options::ID id) const {
145 return getInfo(id).Kind;
148 const char *OptTable::getOptionHelpText(options::ID id) const {
149 return getInfo(id).HelpText;
152 const char *OptTable::getOptionMetaVar(options::ID id) const {
153 return getInfo(id).MetaVar;
156 const Option *OptTable::getOption(options::ID id) const {
157 if (id == OPT_INVALID)
158 return 0;
160 assert((unsigned) (id - 1) < numOptions && "Invalid ID.");
162 Option *&Entry = Options[id - 1];
163 if (!Entry)
164 Entry = constructOption(id);
166 return Entry;
169 Option *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);
175 Option *Opt = 0;
176 switch (info.Kind) {
177 case Option::InputClass:
178 Opt = new InputOption(); break;
179 case Option::UnknownClass:
180 Opt = new UnknownOption(); break;
181 case Option::GroupClass:
182 Opt = new OptionGroup(id, info.Name, Group); break;
183 case Option::FlagClass:
184 Opt = new FlagOption(id, info.Name, Group, Alias); break;
185 case Option::JoinedClass:
186 Opt = new JoinedOption(id, info.Name, Group, Alias); break;
187 case Option::SeparateClass:
188 Opt = new SeparateOption(id, info.Name, Group, Alias); break;
189 case Option::CommaJoinedClass:
190 Opt = new CommaJoinedOption(id, info.Name, Group, Alias); break;
191 case Option::MultiArgClass:
192 Opt = new MultiArgOption(id, info.Name, Group, Alias, info.Param); break;
193 case Option::JoinedOrSeparateClass:
194 Opt = new JoinedOrSeparateOption(id, info.Name, Group, Alias); break;
195 case Option::JoinedAndSeparateClass:
196 Opt = new JoinedAndSeparateOption(id, info.Name, Group, Alias); break;
199 for (const char *s = info.Flags; *s; ++s) {
200 switch (*s) {
201 default: assert(0 && "Invalid option flag.");
202 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;
208 case 'd': Opt->setDriverOption(true); break;
209 case 'i': Opt->setNoOptAsInput(true); break;
210 case 'l': Opt->setLinkerInput(true); break;
211 case 'q': Opt->setNoArgumentUnused(true); break;
212 case 'u': Opt->setUnsupported(true); break;
216 return Opt;
219 // Support lower_bound between info and an option name.
220 static inline bool operator<(struct Info &I, const char *Name) {
221 return StrCmpOptionName(I.Name, Name) == -1;
223 static inline bool operator<(const char *Name, struct Info &I) {
224 return StrCmpOptionName(Name, I.Name) == -1;
227 Arg *OptTable::ParseOneArg(const InputArgList &Args, unsigned &Index) const {
228 unsigned Prev = Index;
229 const char *Str = Args.getArgString(Index);
231 // Anything that doesn't start with '-' is an input, as is '-' itself.
232 if (Str[0] != '-' || Str[1] == '\0')
233 return new PositionalArg(getOption(OPT_INPUT), Index++);
235 struct Info *Start = OptionInfos + FirstSearchableOption - 1;
236 struct Info *End = OptionInfos + LastOption - 1;
238 // Search for the first next option which could be a prefix.
239 Start = std::lower_bound(Start, End, Str);
241 // 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.
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.
249 for (; Start != End; ++Start) {
250 // 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)
255 break;
257 // See if this option matches.
258 options::ID id = (options::ID) (Start - OptionInfos + 1);
259 if (Arg *A = getOption(id)->accept(Args, Index))
260 return A;
262 // Otherwise, see if this argument was missing values.
263 if (Prev != Index)
264 return 0;
267 return new PositionalArg(getOption(OPT_UNKNOWN), Index++);