blob: 3ebc6d8725860b2038abd1d181f60b10f70d90dc [file] [log] [blame]
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +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 Dunbar60e107f2009-12-03 07:01:38 +000014#include "llvm/Support/raw_ostream.h"
David Blaikie548f6c82011-09-23 05:57:42 +000015#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar7547f742009-03-23 21:50:40 +000016#include <algorithm>
Daniel Dunbar2658f052009-12-04 21:08:40 +000017#include <map>
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000018using namespace clang::driver;
19using namespace clang::driver::options;
Chris Lattner5f9e2722011-07-23 10:55:15 +000020using namespace clang;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000021
Daniel Dunbarb3556922009-03-23 18:41:45 +000022// Ordering on Info. The ordering is *almost* lexicographic, with two
23// exceptions. First, '\0' comes at the end of the alphabet instead of
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000024// the beginning (thus options precede any other options which prefix
Daniel Dunbarb3556922009-03-23 18:41:45 +000025// them). Second, for options with the same name, the less permissive
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000026// version should come first; a Flag option should precede a Joined
Daniel Dunbarb3556922009-03-23 18:41:45 +000027// option, for example.
28
29static int StrCmpOptionName(const char *A, const char *B) {
30 char a = *A, b = *B;
31 while (a == b) {
32 if (a == '\0')
33 return 0;
34
35 a = *++A;
36 b = *++B;
37 }
38
39 if (a == '\0') // A is a prefix of B.
40 return 1;
41 if (b == '\0') // B is a prefix of A.
42 return -1;
43
44 // Otherwise lexicographic.
45 return (a < b) ? -1 : 1;
46}
47
Daniel Dunbar1ce9cf02009-11-18 21:42:57 +000048namespace clang {
49namespace driver {
Daniel Dunbara79a2b52009-11-18 20:19:36 +000050static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
Daniel Dunbarb3556922009-03-23 18:41:45 +000051 if (&A == &B)
52 return false;
53
54 if (int N = StrCmpOptionName(A.Name, B.Name))
55 return N == -1;
56
57 // Names are the same, check that classes are in order; exactly one
58 // should be joined, and it should succeed the other.
Daniel Dunbarcf51ece2009-03-25 03:06:26 +000059 assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
Daniel Dunbarb3556922009-03-23 18:41:45 +000060 "Unexpected classes for options with same name.");
61 return B.Kind == Option::JoinedClass;
62}
63
Daniel Dunbar1ce9cf02009-11-18 21:42:57 +000064// Support lower_bound between info and an option name.
65static inline bool operator<(const OptTable::Info &I, const char *Name) {
66 return StrCmpOptionName(I.Name, Name) == -1;
67}
68static inline bool operator<(const char *Name, const OptTable::Info &I) {
69 return StrCmpOptionName(Name, I.Name) == -1;
70}
71}
72}
73
Daniel Dunbarb3556922009-03-23 18:41:45 +000074//
75
Daniel Dunbar9e1f9822009-11-19 04:14:53 +000076OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
77
78//
79
Daniel Dunbara79a2b52009-11-18 20:19:36 +000080OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos)
81 : OptionInfos(_OptionInfos), NumOptionInfos(_NumOptionInfos),
82 Options(new Option*[NumOptionInfos]),
83 TheInputOption(0), TheUnknownOption(0), FirstSearchableIndex(0)
84{
Daniel Dunbar47393ba2009-07-13 21:50:47 +000085 // Explicitly zero initialize the error to work around a bug in array
86 // value-initialization on MinGW with gcc 4.3.5.
Daniel Dunbara79a2b52009-11-18 20:19:36 +000087 memset(Options, 0, sizeof(*Options) * NumOptionInfos);
Daniel Dunbar47393ba2009-07-13 21:50:47 +000088
Daniel Dunbarb3556922009-03-23 18:41:45 +000089 // Find start of normal options.
Daniel Dunbara79a2b52009-11-18 20:19:36 +000090 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
91 unsigned Kind = getInfo(i + 1).Kind;
92 if (Kind == Option::InputClass) {
93 assert(!TheInputOption && "Cannot have multiple input options!");
94 TheInputOption = getOption(i + 1);
95 } else if (Kind == Option::UnknownClass) {
96 assert(!TheUnknownOption && "Cannot have multiple input options!");
97 TheUnknownOption = getOption(i + 1);
98 } else if (Kind != Option::GroupClass) {
99 FirstSearchableIndex = i;
Daniel Dunbarb3556922009-03-23 18:41:45 +0000100 break;
101 }
102 }
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000103 assert(FirstSearchableIndex != 0 && "No searchable options?");
Daniel Dunbarb3556922009-03-23 18:41:45 +0000104
105#ifndef NDEBUG
106 // Check that everything after the first searchable option is a
107 // regular option class.
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000108 for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) {
109 Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind;
Daniel Dunbarb3556922009-03-23 18:41:45 +0000110 assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
111 Kind != Option::GroupClass) &&
112 "Special options should be defined first!");
113 }
114
115 // Check that options are in order.
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000116 for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) {
117 if (!(getInfo(i) < getInfo(i + 1))) {
118 getOption(i)->dump();
119 getOption(i + 1)->dump();
David Blaikieb219cfc2011-09-23 05:06:16 +0000120 llvm_unreachable("Options are not in order!");
Daniel Dunbarb3556922009-03-23 18:41:45 +0000121 }
122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123#endif
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000124}
125
Mike Stump1eb44332009-09-09 15:08:12 +0000126OptTable::~OptTable() {
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000127 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000128 delete Options[i];
129 delete[] Options;
130}
131
Michael J. Spencer04a42792012-08-21 18:51:17 +0000132bool OptTable::isOptionHelpHidden(OptSpecifier id) const {
133 return getInfo(id).Flags & options::HelpHidden;
134}
135
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000136Option *OptTable::CreateOption(unsigned id) const {
137 const Info &info = getInfo(id);
Michael J. Spencer43275572012-08-20 21:41:17 +0000138 const Option *Group = getOption(info.GroupID);
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000139 const Option *Alias = getOption(info.AliasID);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000140
Michael J. Spencer04a42792012-08-21 18:51:17 +0000141 Option *Opt = new Option(&info, id, Group, Alias);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000142
143 return Opt;
144}
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000145
Daniel Dunbar785e7962010-06-11 22:00:17 +0000146Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index) const {
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000147 unsigned Prev = Index;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000148 const char *Str = Args.getArgString(Index);
149
Daniel Dunbara9480452009-03-12 08:44:47 +0000150 // Anything that doesn't start with '-' is an input, as is '-' itself.
151 if (Str[0] != '-' || Str[1] == '\0')
Daniel Dunbar532c1ec2010-06-09 22:31:08 +0000152 return new Arg(TheInputOption, Index++, Str);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000153
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000154 const Info *Start = OptionInfos + FirstSearchableIndex;
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000155 const Info *End = OptionInfos + getNumOptions();
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000156
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000157 // Search for the first next option which could be a prefix.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000158 Start = std::lower_bound(Start, End, Str);
159
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000160 // Options are stored in sorted order, with '\0' at the end of the
161 // alphabet. Since the only options which can accept a string must
162 // prefix it, we iteratively search for the next option which could
163 // be a prefix.
164 //
165 // FIXME: This is searching much more than necessary, but I am
166 // blanking on the simplest way to make it fast. We can solve this
167 // problem when we move to TableGen.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000168 for (; Start != End; ++Start) {
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000169 // Scan for first option which is a proper prefix.
170 for (; Start != End; ++Start)
171 if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0)
172 break;
173 if (Start == End)
Daniel Dunbar7547f742009-03-23 21:50:40 +0000174 break;
175
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000176 // See if this option matches.
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000177 if (Arg *A = getOption(Start - OptionInfos + 1)->accept(Args, Index))
Daniel Dunbar7547f742009-03-23 21:50:40 +0000178 return A;
179
180 // Otherwise, see if this argument was missing values.
181 if (Prev != Index)
182 return 0;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000183 }
184
Daniel Dunbar532c1ec2010-06-09 22:31:08 +0000185 return new Arg(TheUnknownOption, Index++, Str);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000186}
Daniel Dunbar847abaa2009-11-19 06:35:06 +0000187
Axel Naumann9d520c52010-10-11 09:18:43 +0000188InputArgList *OptTable::ParseArgs(const char* const *ArgBegin,
189 const char* const *ArgEnd,
Daniel Dunbar847abaa2009-11-19 06:35:06 +0000190 unsigned &MissingArgIndex,
191 unsigned &MissingArgCount) const {
192 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
193
194 // FIXME: Handle '@' args (or at least error on them).
195
196 MissingArgIndex = MissingArgCount = 0;
197 unsigned Index = 0, End = ArgEnd - ArgBegin;
198 while (Index < End) {
199 // Ignore empty arguments (other things may still take them as arguments).
200 if (Args->getArgString(Index)[0] == '\0') {
201 ++Index;
202 continue;
203 }
204
205 unsigned Prev = Index;
206 Arg *A = ParseOneArg(*Args, Index);
207 assert(Index > Prev && "Parser failed to consume argument.");
208
209 // Check for missing argument error.
210 if (!A) {
211 assert(Index >= End && "Unexpected parser error.");
212 assert(Index - Prev - 1 && "No missing arguments!");
213 MissingArgIndex = Prev;
214 MissingArgCount = Index - Prev - 1;
215 break;
216 }
217
218 Args->append(A);
219 }
220
221 return Args;
222}
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000223
224static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
225 std::string Name = Opts.getOptionName(Id);
226
227 // Add metavar, if used.
228 switch (Opts.getOptionKind(Id)) {
229 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000230 llvm_unreachable("Invalid option with help text.");
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000231
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000232 case Option::MultiArgClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000233 llvm_unreachable("Cannot print metavar for this kind of option.");
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000234
235 case Option::FlagClass:
236 break;
237
238 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
239 Name += ' ';
240 // FALLTHROUGH
241 case Option::JoinedClass: case Option::CommaJoinedClass:
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000242 case Option::JoinedAndSeparateClass:
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000243 if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
244 Name += MetaVarName;
245 else
246 Name += "<value>";
247 break;
248 }
249
250 return Name;
251}
252
Chris Lattner5f9e2722011-07-23 10:55:15 +0000253static void PrintHelpOptionList(raw_ostream &OS, StringRef Title,
Daniel Dunbar2658f052009-12-04 21:08:40 +0000254 std::vector<std::pair<std::string,
255 const char*> > &OptionHelp) {
256 OS << Title << ":\n";
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000257
258 // Find the maximum option length.
259 unsigned OptionFieldWidth = 0;
260 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
261 // Skip titles.
262 if (!OptionHelp[i].second)
263 continue;
264
265 // Limit the amount of padding we are willing to give up for alignment.
266 unsigned Length = OptionHelp[i].first.size();
267 if (Length <= 23)
268 OptionFieldWidth = std::max(OptionFieldWidth, Length);
269 }
270
271 const unsigned InitialPad = 2;
272 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
273 const std::string &Option = OptionHelp[i].first;
274 int Pad = OptionFieldWidth - int(Option.size());
275 OS.indent(InitialPad) << Option;
276
277 // Break on long option names.
278 if (Pad < 0) {
279 OS << "\n";
280 Pad = OptionFieldWidth + InitialPad;
281 }
282 OS.indent(Pad + 1) << OptionHelp[i].second << '\n';
283 }
Daniel Dunbar2658f052009-12-04 21:08:40 +0000284}
285
286static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
287 unsigned GroupID = Opts.getOptionGroupID(Id);
288
289 // If not in a group, return the default help group.
290 if (!GroupID)
291 return "OPTIONS";
292
293 // Abuse the help text of the option groups to store the "help group"
294 // name.
295 //
296 // FIXME: Split out option groups.
297 if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
298 return GroupHelp;
299
300 // Otherwise keep looking.
301 return getOptionHelpGroup(Opts, GroupID);
302}
303
Chris Lattner5f9e2722011-07-23 10:55:15 +0000304void OptTable::PrintHelp(raw_ostream &OS, const char *Name,
Daniel Dunbar2658f052009-12-04 21:08:40 +0000305 const char *Title, bool ShowHidden) const {
306 OS << "OVERVIEW: " << Title << "\n";
307 OS << '\n';
308 OS << "USAGE: " << Name << " [options] <inputs>\n";
309 OS << '\n';
310
311 // Render help text into a map of group-name to a list of (option, help)
312 // pairs.
313 typedef std::map<std::string,
314 std::vector<std::pair<std::string, const char*> > > helpmap_ty;
315 helpmap_ty GroupedOptionHelp;
316
317 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
318 unsigned Id = i + 1;
319
320 // FIXME: Split out option groups.
321 if (getOptionKind(Id) == Option::GroupClass)
322 continue;
323
324 if (!ShowHidden && isOptionHelpHidden(Id))
325 continue;
326
327 if (const char *Text = getOptionHelpText(Id)) {
328 const char *HelpGroup = getOptionHelpGroup(*this, Id);
329 const std::string &OptName = getOptionHelpName(*this, Id);
330 GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text));
331 }
332 }
333
334 for (helpmap_ty::iterator it = GroupedOptionHelp .begin(),
335 ie = GroupedOptionHelp.end(); it != ie; ++it) {
336 if (it != GroupedOptionHelp .begin())
337 OS << "\n";
338 PrintHelpOptionList(OS, it->first, it->second);
339 }
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000340
341 OS.flush();
342}