blob: 67e3019e0f2cb14945fb5536027a44b46f677ad1 [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 Dunbara79a2b52009-11-18 20:19:36 +000047static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
Daniel Dunbarb3556922009-03-23 18:41:45 +000048 if (&A == &B)
49 return false;
50
51 if (int N = StrCmpOptionName(A.Name, B.Name))
52 return N == -1;
53
54 // Names are the same, check that classes are in order; exactly one
55 // should be joined, and it should succeed the other.
Daniel Dunbarcf51ece2009-03-25 03:06:26 +000056 assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
Daniel Dunbarb3556922009-03-23 18:41:45 +000057 "Unexpected classes for options with same name.");
58 return B.Kind == Option::JoinedClass;
59}
60
61//
62
Daniel Dunbara79a2b52009-11-18 20:19:36 +000063OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos)
64 : OptionInfos(_OptionInfos), NumOptionInfos(_NumOptionInfos),
65 Options(new Option*[NumOptionInfos]),
66 TheInputOption(0), TheUnknownOption(0), FirstSearchableIndex(0)
67{
Daniel Dunbar47393ba2009-07-13 21:50:47 +000068 // Explicitly zero initialize the error to work around a bug in array
69 // value-initialization on MinGW with gcc 4.3.5.
Daniel Dunbara79a2b52009-11-18 20:19:36 +000070 memset(Options, 0, sizeof(*Options) * NumOptionInfos);
Daniel Dunbar47393ba2009-07-13 21:50:47 +000071
Daniel Dunbarb3556922009-03-23 18:41:45 +000072 // Find start of normal options.
Daniel Dunbara79a2b52009-11-18 20:19:36 +000073 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
74 unsigned Kind = getInfo(i + 1).Kind;
75 if (Kind == Option::InputClass) {
76 assert(!TheInputOption && "Cannot have multiple input options!");
77 TheInputOption = getOption(i + 1);
78 } else if (Kind == Option::UnknownClass) {
79 assert(!TheUnknownOption && "Cannot have multiple input options!");
80 TheUnknownOption = getOption(i + 1);
81 } else if (Kind != Option::GroupClass) {
82 FirstSearchableIndex = i;
Daniel Dunbarb3556922009-03-23 18:41:45 +000083 break;
84 }
85 }
Daniel Dunbara79a2b52009-11-18 20:19:36 +000086 assert(FirstSearchableIndex != 0 && "No searchable options?");
Daniel Dunbarb3556922009-03-23 18:41:45 +000087
88#ifndef NDEBUG
89 // Check that everything after the first searchable option is a
90 // regular option class.
Daniel Dunbara79a2b52009-11-18 20:19:36 +000091 for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) {
92 Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind;
Daniel Dunbarb3556922009-03-23 18:41:45 +000093 assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
94 Kind != Option::GroupClass) &&
95 "Special options should be defined first!");
96 }
97
98 // Check that options are in order.
Daniel Dunbara79a2b52009-11-18 20:19:36 +000099 for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) {
100 if (!(getInfo(i) < getInfo(i + 1))) {
101 getOption(i)->dump();
102 getOption(i + 1)->dump();
Daniel Dunbarb3556922009-03-23 18:41:45 +0000103 assert(0 && "Options are not in order!");
104 }
105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106#endif
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000107}
108
Mike Stump1eb44332009-09-09 15:08:12 +0000109OptTable::~OptTable() {
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000110 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000111 delete Options[i];
112 delete[] Options;
113}
114
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000115Option *OptTable::CreateOption(unsigned id) const {
116 const Info &info = getInfo(id);
Mike Stump1eb44332009-09-09 15:08:12 +0000117 const OptionGroup *Group =
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000118 cast_or_null<OptionGroup>(getOption(info.GroupID));
119 const Option *Alias = getOption(info.AliasID);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000120
121 Option *Opt = 0;
122 switch (info.Kind) {
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000123 case Option::InputClass:
124 Opt = new InputOption(); break;
125 case Option::UnknownClass:
126 Opt = new UnknownOption(); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000127 case Option::GroupClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000128 Opt = new OptionGroup(id, info.Name, Group); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000129 case Option::FlagClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000130 Opt = new FlagOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000131 case Option::JoinedClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000132 Opt = new JoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000133 case Option::SeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000134 Opt = new SeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000135 case Option::CommaJoinedClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000136 Opt = new CommaJoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000137 case Option::MultiArgClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000138 Opt = new MultiArgOption(id, info.Name, Group, Alias, info.Param); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000139 case Option::JoinedOrSeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000140 Opt = new JoinedOrSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000141 case Option::JoinedAndSeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000142 Opt = new JoinedAndSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000143 }
144
Daniel Dunbara4f64802009-11-18 17:42:34 +0000145 if (info.Flags & DriverOption)
146 Opt->setDriverOption(true);
147 if (info.Flags & LinkerInput)
148 Opt->setLinkerInput(true);
149 if (info.Flags & NoArgumentUnused)
150 Opt->setNoArgumentUnused(true);
151 if (info.Flags & RenderAsInput)
152 Opt->setNoOptAsInput(true);
153 if (info.Flags & RenderJoined) {
154 assert(info.Kind == Option::SeparateClass && "Invalid option.");
155 Opt->setForceJoinedRender(true);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000156 }
Daniel Dunbara4f64802009-11-18 17:42:34 +0000157 if (info.Flags & RenderSeparate) {
158 assert(info.Kind == Option::JoinedClass && "Invalid option.");
159 Opt->setForceSeparateRender(true);
160 }
161 if (info.Flags & Unsupported)
162 Opt->setUnsupported(true);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000163
164 return Opt;
165}
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000166
Daniel Dunbar7547f742009-03-23 21:50:40 +0000167// Support lower_bound between info and an option name.
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000168namespace clang {
169namespace driver {
170static inline bool operator<(const OptTable::Info &I, const char *Name) {
Daniel Dunbar7547f742009-03-23 21:50:40 +0000171 return StrCmpOptionName(I.Name, Name) == -1;
172}
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000173static inline bool operator<(const char *Name, const OptTable::Info &I) {
Daniel Dunbar7547f742009-03-23 21:50:40 +0000174 return StrCmpOptionName(Name, I.Name) == -1;
175}
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000176}
177}
Daniel Dunbar7547f742009-03-23 21:50:40 +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}