blob: 890907b2a7300da22cbd9a221874ab27cdbbcff6 [file] [log] [blame]
Daniel Dunbar27e738d2009-11-19 00:15:11 +00001//===--- OptTable.cpp - Option Table Implementation ---------------------*-===//
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +00002//
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
Daniel Dunbar27e738d2009-11-19 00:15:11 +000010#include "clang/Driver/OptTable.h"
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +000011#include "clang/Driver/Arg.h"
12#include "clang/Driver/ArgList.h"
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000013#include "clang/Driver/Option.h"
Daniel Dunbar7547f742009-03-23 21:50:40 +000014#include <algorithm>
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000015#include <cassert>
16
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000017using namespace clang::driver;
18using namespace clang::driver::options;
19
Daniel Dunbarb3556922009-03-23 18:41:45 +000020// Ordering on Info. The ordering is *almost* lexicographic, with two
21// exceptions. First, '\0' comes at the end of the alphabet instead of
22// the beginning (thus options preceed any other options which prefix
23// them). Second, for options with the same name, the less permissive
24// version should come first; a Flag option should preceed a Joined
25// option, for example.
26
27static int StrCmpOptionName(const char *A, const char *B) {
28 char a = *A, b = *B;
29 while (a == b) {
30 if (a == '\0')
31 return 0;
32
33 a = *++A;
34 b = *++B;
35 }
36
37 if (a == '\0') // A is a prefix of B.
38 return 1;
39 if (b == '\0') // B is a prefix of A.
40 return -1;
41
42 // Otherwise lexicographic.
43 return (a < b) ? -1 : 1;
44}
45
Daniel Dunbar1ce9cf02009-11-18 21:42:57 +000046namespace clang {
47namespace driver {
Daniel Dunbara79a2b52009-11-18 20:19:36 +000048static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
Daniel Dunbarb3556922009-03-23 18:41:45 +000049 if (&A == &B)
50 return false;
51
52 if (int N = StrCmpOptionName(A.Name, B.Name))
53 return N == -1;
54
55 // Names are the same, check that classes are in order; exactly one
56 // should be joined, and it should succeed the other.
Daniel Dunbarcf51ece2009-03-25 03:06:26 +000057 assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
Daniel Dunbarb3556922009-03-23 18:41:45 +000058 "Unexpected classes for options with same name.");
59 return B.Kind == Option::JoinedClass;
60}
61
Daniel Dunbar1ce9cf02009-11-18 21:42:57 +000062// Support lower_bound between info and an option name.
63static inline bool operator<(const OptTable::Info &I, const char *Name) {
64 return StrCmpOptionName(I.Name, Name) == -1;
65}
66static inline bool operator<(const char *Name, const OptTable::Info &I) {
67 return StrCmpOptionName(Name, I.Name) == -1;
68}
69}
70}
71
Daniel Dunbarb3556922009-03-23 18:41:45 +000072//
73
Daniel Dunbar9e1f9822009-11-19 04:14:53 +000074OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
75
76//
77
Daniel Dunbara79a2b52009-11-18 20:19:36 +000078OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos)
79 : OptionInfos(_OptionInfos), NumOptionInfos(_NumOptionInfos),
80 Options(new Option*[NumOptionInfos]),
81 TheInputOption(0), TheUnknownOption(0), FirstSearchableIndex(0)
82{
Daniel Dunbar47393ba2009-07-13 21:50:47 +000083 // Explicitly zero initialize the error to work around a bug in array
84 // value-initialization on MinGW with gcc 4.3.5.
Daniel Dunbara79a2b52009-11-18 20:19:36 +000085 memset(Options, 0, sizeof(*Options) * NumOptionInfos);
Daniel Dunbar47393ba2009-07-13 21:50:47 +000086
Daniel Dunbarb3556922009-03-23 18:41:45 +000087 // Find start of normal options.
Daniel Dunbara79a2b52009-11-18 20:19:36 +000088 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
89 unsigned Kind = getInfo(i + 1).Kind;
90 if (Kind == Option::InputClass) {
91 assert(!TheInputOption && "Cannot have multiple input options!");
92 TheInputOption = getOption(i + 1);
93 } else if (Kind == Option::UnknownClass) {
94 assert(!TheUnknownOption && "Cannot have multiple input options!");
95 TheUnknownOption = getOption(i + 1);
96 } else if (Kind != Option::GroupClass) {
97 FirstSearchableIndex = i;
Daniel Dunbarb3556922009-03-23 18:41:45 +000098 break;
99 }
100 }
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000101 assert(FirstSearchableIndex != 0 && "No searchable options?");
Daniel Dunbarb3556922009-03-23 18:41:45 +0000102
103#ifndef NDEBUG
104 // Check that everything after the first searchable option is a
105 // regular option class.
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000106 for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) {
107 Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind;
Daniel Dunbarb3556922009-03-23 18:41:45 +0000108 assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
109 Kind != Option::GroupClass) &&
110 "Special options should be defined first!");
111 }
112
113 // Check that options are in order.
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000114 for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) {
115 if (!(getInfo(i) < getInfo(i + 1))) {
116 getOption(i)->dump();
117 getOption(i + 1)->dump();
Daniel Dunbarb3556922009-03-23 18:41:45 +0000118 assert(0 && "Options are not in order!");
119 }
120 }
Mike Stump1eb44332009-09-09 15:08:12 +0000121#endif
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000122}
123
Mike Stump1eb44332009-09-09 15:08:12 +0000124OptTable::~OptTable() {
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000125 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000126 delete Options[i];
127 delete[] Options;
128}
129
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000130Option *OptTable::CreateOption(unsigned id) const {
131 const Info &info = getInfo(id);
Mike Stump1eb44332009-09-09 15:08:12 +0000132 const OptionGroup *Group =
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000133 cast_or_null<OptionGroup>(getOption(info.GroupID));
134 const Option *Alias = getOption(info.AliasID);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000135
136 Option *Opt = 0;
137 switch (info.Kind) {
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000138 case Option::InputClass:
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000139 Opt = new InputOption(id); break;
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000140 case Option::UnknownClass:
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000141 Opt = new UnknownOption(id); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000142 case Option::GroupClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000143 Opt = new OptionGroup(id, info.Name, Group); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000144 case Option::FlagClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000145 Opt = new FlagOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000146 case Option::JoinedClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000147 Opt = new JoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000148 case Option::SeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000149 Opt = new SeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000150 case Option::CommaJoinedClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000151 Opt = new CommaJoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000152 case Option::MultiArgClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000153 Opt = new MultiArgOption(id, info.Name, Group, Alias, info.Param); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000154 case Option::JoinedOrSeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000155 Opt = new JoinedOrSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000156 case Option::JoinedAndSeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000157 Opt = new JoinedAndSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000158 }
159
Daniel Dunbara4f64802009-11-18 17:42:34 +0000160 if (info.Flags & DriverOption)
161 Opt->setDriverOption(true);
162 if (info.Flags & LinkerInput)
163 Opt->setLinkerInput(true);
164 if (info.Flags & NoArgumentUnused)
165 Opt->setNoArgumentUnused(true);
166 if (info.Flags & RenderAsInput)
167 Opt->setNoOptAsInput(true);
168 if (info.Flags & RenderJoined) {
169 assert(info.Kind == Option::SeparateClass && "Invalid option.");
170 Opt->setForceJoinedRender(true);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000171 }
Daniel Dunbara4f64802009-11-18 17:42:34 +0000172 if (info.Flags & RenderSeparate) {
173 assert(info.Kind == Option::JoinedClass && "Invalid option.");
174 Opt->setForceSeparateRender(true);
175 }
176 if (info.Flags & Unsupported)
177 Opt->setUnsupported(true);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000178
179 return Opt;
180}
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000181
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000182Arg *OptTable::ParseOneArg(const InputArgList &Args, unsigned &Index) const {
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000183 unsigned Prev = Index;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000184 const char *Str = Args.getArgString(Index);
185
Daniel Dunbara9480452009-03-12 08:44:47 +0000186 // Anything that doesn't start with '-' is an input, as is '-' itself.
187 if (Str[0] != '-' || Str[1] == '\0')
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000188 return new PositionalArg(TheInputOption, Index++);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000189
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000190 const Info *Start = OptionInfos + FirstSearchableIndex;
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000191 const Info *End = OptionInfos + getNumOptions();
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000192
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000193 // Search for the first next option which could be a prefix.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000194 Start = std::lower_bound(Start, End, Str);
195
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000196 // Options are stored in sorted order, with '\0' at the end of the
197 // alphabet. Since the only options which can accept a string must
198 // prefix it, we iteratively search for the next option which could
199 // be a prefix.
200 //
201 // FIXME: This is searching much more than necessary, but I am
202 // blanking on the simplest way to make it fast. We can solve this
203 // problem when we move to TableGen.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000204 for (; Start != End; ++Start) {
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000205 // Scan for first option which is a proper prefix.
206 for (; Start != End; ++Start)
207 if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0)
208 break;
209 if (Start == End)
Daniel Dunbar7547f742009-03-23 21:50:40 +0000210 break;
211
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000212 // See if this option matches.
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000213 if (Arg *A = getOption(Start - OptionInfos + 1)->accept(Args, Index))
Daniel Dunbar7547f742009-03-23 21:50:40 +0000214 return A;
215
216 // Otherwise, see if this argument was missing values.
217 if (Prev != Index)
218 return 0;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000219 }
220
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000221 return new PositionalArg(TheUnknownOption, Index++);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000222}