blob: 6065b2df228f19db8bce4c7a207dbd0b9fb4e421 [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
Daniel Dunbarb3556922009-03-23 18:41:45 +000021// Ordering on Info. The ordering is *almost* lexicographic, with two
22// exceptions. First, '\0' comes at the end of the alphabet instead of
23// the beginning (thus options preceed any other options which prefix
24// them). Second, for options with the same name, the less permissive
25// version should come first; a Flag option should preceed a Joined
26// option, for example.
27
28static int StrCmpOptionName(const char *A, const char *B) {
29 char a = *A, b = *B;
30 while (a == b) {
31 if (a == '\0')
32 return 0;
33
34 a = *++A;
35 b = *++B;
36 }
37
38 if (a == '\0') // A is a prefix of B.
39 return 1;
40 if (b == '\0') // B is a prefix of A.
41 return -1;
42
43 // Otherwise lexicographic.
44 return (a < b) ? -1 : 1;
45}
46
Daniel Dunbar1ce9cf02009-11-18 21:42:57 +000047namespace clang {
48namespace driver {
Daniel Dunbara79a2b52009-11-18 20:19:36 +000049static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
Daniel Dunbarb3556922009-03-23 18:41:45 +000050 if (&A == &B)
51 return false;
52
53 if (int N = StrCmpOptionName(A.Name, B.Name))
54 return N == -1;
55
56 // Names are the same, check that classes are in order; exactly one
57 // should be joined, and it should succeed the other.
Daniel Dunbarcf51ece2009-03-25 03:06:26 +000058 assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
Daniel Dunbarb3556922009-03-23 18:41:45 +000059 "Unexpected classes for options with same name.");
60 return B.Kind == Option::JoinedClass;
61}
62
Daniel Dunbar1ce9cf02009-11-18 21:42:57 +000063// Support lower_bound between info and an option name.
64static inline bool operator<(const OptTable::Info &I, const char *Name) {
65 return StrCmpOptionName(I.Name, Name) == -1;
66}
67static inline bool operator<(const char *Name, const OptTable::Info &I) {
68 return StrCmpOptionName(Name, I.Name) == -1;
69}
70}
71}
72
Daniel Dunbarb3556922009-03-23 18:41:45 +000073//
74
Daniel Dunbara79a2b52009-11-18 20:19:36 +000075OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos)
76 : OptionInfos(_OptionInfos), NumOptionInfos(_NumOptionInfos),
77 Options(new Option*[NumOptionInfos]),
78 TheInputOption(0), TheUnknownOption(0), FirstSearchableIndex(0)
79{
Daniel Dunbar47393ba2009-07-13 21:50:47 +000080 // Explicitly zero initialize the error to work around a bug in array
81 // value-initialization on MinGW with gcc 4.3.5.
Daniel Dunbara79a2b52009-11-18 20:19:36 +000082 memset(Options, 0, sizeof(*Options) * NumOptionInfos);
Daniel Dunbar47393ba2009-07-13 21:50:47 +000083
Daniel Dunbarb3556922009-03-23 18:41:45 +000084 // Find start of normal options.
Daniel Dunbara79a2b52009-11-18 20:19:36 +000085 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
86 unsigned Kind = getInfo(i + 1).Kind;
87 if (Kind == Option::InputClass) {
88 assert(!TheInputOption && "Cannot have multiple input options!");
89 TheInputOption = getOption(i + 1);
90 } else if (Kind == Option::UnknownClass) {
91 assert(!TheUnknownOption && "Cannot have multiple input options!");
92 TheUnknownOption = getOption(i + 1);
93 } else if (Kind != Option::GroupClass) {
94 FirstSearchableIndex = i;
Daniel Dunbarb3556922009-03-23 18:41:45 +000095 break;
96 }
97 }
Daniel Dunbara79a2b52009-11-18 20:19:36 +000098 assert(FirstSearchableIndex != 0 && "No searchable options?");
Daniel Dunbarb3556922009-03-23 18:41:45 +000099
100#ifndef NDEBUG
101 // Check that everything after the first searchable option is a
102 // regular option class.
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000103 for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) {
104 Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind;
Daniel Dunbarb3556922009-03-23 18:41:45 +0000105 assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
106 Kind != Option::GroupClass) &&
107 "Special options should be defined first!");
108 }
109
110 // Check that options are in order.
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000111 for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) {
112 if (!(getInfo(i) < getInfo(i + 1))) {
113 getOption(i)->dump();
114 getOption(i + 1)->dump();
Daniel Dunbarb3556922009-03-23 18:41:45 +0000115 assert(0 && "Options are not in order!");
116 }
117 }
Mike Stump1eb44332009-09-09 15:08:12 +0000118#endif
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000119}
120
Mike Stump1eb44332009-09-09 15:08:12 +0000121OptTable::~OptTable() {
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000122 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000123 delete Options[i];
124 delete[] Options;
125}
126
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000127Option *OptTable::CreateOption(unsigned id) const {
128 const Info &info = getInfo(id);
Mike Stump1eb44332009-09-09 15:08:12 +0000129 const OptionGroup *Group =
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000130 cast_or_null<OptionGroup>(getOption(info.GroupID));
131 const Option *Alias = getOption(info.AliasID);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000132
133 Option *Opt = 0;
134 switch (info.Kind) {
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000135 case Option::InputClass:
136 Opt = new InputOption(); break;
137 case Option::UnknownClass:
138 Opt = new UnknownOption(); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000139 case Option::GroupClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000140 Opt = new OptionGroup(id, info.Name, Group); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000141 case Option::FlagClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000142 Opt = new FlagOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000143 case Option::JoinedClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000144 Opt = new JoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000145 case Option::SeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000146 Opt = new SeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000147 case Option::CommaJoinedClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000148 Opt = new CommaJoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000149 case Option::MultiArgClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000150 Opt = new MultiArgOption(id, info.Name, Group, Alias, info.Param); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000151 case Option::JoinedOrSeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000152 Opt = new JoinedOrSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000153 case Option::JoinedAndSeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000154 Opt = new JoinedAndSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000155 }
156
Daniel Dunbara4f64802009-11-18 17:42:34 +0000157 if (info.Flags & DriverOption)
158 Opt->setDriverOption(true);
159 if (info.Flags & LinkerInput)
160 Opt->setLinkerInput(true);
161 if (info.Flags & NoArgumentUnused)
162 Opt->setNoArgumentUnused(true);
163 if (info.Flags & RenderAsInput)
164 Opt->setNoOptAsInput(true);
165 if (info.Flags & RenderJoined) {
166 assert(info.Kind == Option::SeparateClass && "Invalid option.");
167 Opt->setForceJoinedRender(true);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000168 }
Daniel Dunbara4f64802009-11-18 17:42:34 +0000169 if (info.Flags & RenderSeparate) {
170 assert(info.Kind == Option::JoinedClass && "Invalid option.");
171 Opt->setForceSeparateRender(true);
172 }
173 if (info.Flags & Unsupported)
174 Opt->setUnsupported(true);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000175
176 return Opt;
177}
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000178
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000179Arg *OptTable::ParseOneArg(const InputArgList &Args, unsigned &Index) const {
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000180 unsigned Prev = Index;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000181 const char *Str = Args.getArgString(Index);
182
Daniel Dunbara9480452009-03-12 08:44:47 +0000183 // Anything that doesn't start with '-' is an input, as is '-' itself.
184 if (Str[0] != '-' || Str[1] == '\0')
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000185 return new PositionalArg(TheInputOption, Index++);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000186
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000187 const Info *Start = OptionInfos + FirstSearchableIndex;
188 const Info *End = OptionInfos + LastOption - 1;
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000189
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000190 // Search for the first next option which could be a prefix.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000191 Start = std::lower_bound(Start, End, Str);
192
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000193 // Options are stored in sorted order, with '\0' at the end of the
194 // alphabet. Since the only options which can accept a string must
195 // prefix it, we iteratively search for the next option which could
196 // be a prefix.
197 //
198 // FIXME: This is searching much more than necessary, but I am
199 // blanking on the simplest way to make it fast. We can solve this
200 // problem when we move to TableGen.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000201 for (; Start != End; ++Start) {
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000202 // Scan for first option which is a proper prefix.
203 for (; Start != End; ++Start)
204 if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0)
205 break;
206 if (Start == End)
Daniel Dunbar7547f742009-03-23 21:50:40 +0000207 break;
208
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000209 // See if this option matches.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000210 options::ID id = (options::ID) (Start - OptionInfos + 1);
211 if (Arg *A = getOption(id)->accept(Args, Index))
212 return A;
213
214 // Otherwise, see if this argument was missing values.
215 if (Prev != Index)
216 return 0;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000217 }
218
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000219 return new PositionalArg(TheUnknownOption, Index++);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000220}
221
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000222//
223
224static OptTable::Info InfoTable[] = {
225 // The InputOption info
226 { "<input>", 0, 0, Option::InputClass, DriverOption, 0, OPT_INVALID, OPT_INVALID },
227 // The UnknownOption info
228 { "<unknown>", 0, 0, Option::UnknownClass, 0, 0, OPT_INVALID, OPT_INVALID },
229
230#define OPTION(NAME, ID, KIND, GROUP, ALIAS, FLAGS, PARAM, \
231 HELPTEXT, METAVAR) \
232 { NAME, HELPTEXT, METAVAR, Option::KIND##Class, FLAGS, PARAM, \
233 OPT_##GROUP, OPT_##ALIAS },
234#include "clang/Driver/Options.def"
235};
236
237namespace {
238
239class DriverOptTable : public OptTable {
240public:
241 DriverOptTable()
242 : OptTable(InfoTable, sizeof(InfoTable) / sizeof(InfoTable[0])) {}
243};
244
245}
246
247OptTable *clang::driver::createDriverOptTable() {
248 return new DriverOptTable();
249}