blob: 4f5390b6e26bd09e3fa2f5b51a20164810717f5e [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
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000132Option *OptTable::CreateOption(unsigned id) const {
133 const Info &info = getInfo(id);
Mike Stump1eb44332009-09-09 15:08:12 +0000134 const OptionGroup *Group =
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000135 cast_or_null<OptionGroup>(getOption(info.GroupID));
136 const Option *Alias = getOption(info.AliasID);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000137
138 Option *Opt = 0;
139 switch (info.Kind) {
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000140 case Option::InputClass:
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000141 Opt = new InputOption(id); break;
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000142 case Option::UnknownClass:
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000143 Opt = new UnknownOption(id); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000144 case Option::GroupClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000145 Opt = new OptionGroup(id, info.Name, Group); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000146 case Option::FlagClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000147 Opt = new FlagOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000148 case Option::JoinedClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000149 Opt = new JoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000150 case Option::SeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000151 Opt = new SeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000152 case Option::CommaJoinedClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000153 Opt = new CommaJoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000154 case Option::MultiArgClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000155 Opt = new MultiArgOption(id, info.Name, Group, Alias, info.Param); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000156 case Option::JoinedOrSeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000157 Opt = new JoinedOrSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000158 case Option::JoinedAndSeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000159 Opt = new JoinedAndSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000160 }
161
Daniel Dunbara4f64802009-11-18 17:42:34 +0000162 if (info.Flags & DriverOption)
163 Opt->setDriverOption(true);
164 if (info.Flags & LinkerInput)
165 Opt->setLinkerInput(true);
166 if (info.Flags & NoArgumentUnused)
167 Opt->setNoArgumentUnused(true);
Daniel Dunbarcaeed1d2010-08-13 04:44:20 +0000168 if (info.Flags & NoForward)
169 Opt->setNoForward(true);
Daniel Dunbara4f64802009-11-18 17:42:34 +0000170 if (info.Flags & RenderAsInput)
171 Opt->setNoOptAsInput(true);
172 if (info.Flags & RenderJoined) {
Daniel Dunbarbbd2a042010-03-20 01:12:00 +0000173 assert((info.Kind == Option::JoinedOrSeparateClass ||
174 info.Kind == Option::SeparateClass) && "Invalid option.");
Daniel Dunbare375c4a2010-06-09 22:31:04 +0000175 Opt->setRenderStyle(Option::RenderJoinedStyle);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000176 }
Daniel Dunbara4f64802009-11-18 17:42:34 +0000177 if (info.Flags & RenderSeparate) {
Daniel Dunbarbbd2a042010-03-20 01:12:00 +0000178 assert((info.Kind == Option::JoinedOrSeparateClass ||
179 info.Kind == Option::JoinedClass) && "Invalid option.");
Daniel Dunbare375c4a2010-06-09 22:31:04 +0000180 Opt->setRenderStyle(Option::RenderSeparateStyle);
Daniel Dunbara4f64802009-11-18 17:42:34 +0000181 }
182 if (info.Flags & Unsupported)
183 Opt->setUnsupported(true);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000184
185 return Opt;
186}
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000187
Daniel Dunbar785e7962010-06-11 22:00:17 +0000188Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index) const {
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000189 unsigned Prev = Index;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000190 const char *Str = Args.getArgString(Index);
191
Daniel Dunbara9480452009-03-12 08:44:47 +0000192 // Anything that doesn't start with '-' is an input, as is '-' itself.
193 if (Str[0] != '-' || Str[1] == '\0')
Daniel Dunbar532c1ec2010-06-09 22:31:08 +0000194 return new Arg(TheInputOption, Index++, Str);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000195
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000196 const Info *Start = OptionInfos + FirstSearchableIndex;
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000197 const Info *End = OptionInfos + getNumOptions();
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000198
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000199 // Search for the first next option which could be a prefix.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000200 Start = std::lower_bound(Start, End, Str);
201
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000202 // Options are stored in sorted order, with '\0' at the end of the
203 // alphabet. Since the only options which can accept a string must
204 // prefix it, we iteratively search for the next option which could
205 // be a prefix.
206 //
207 // FIXME: This is searching much more than necessary, but I am
208 // blanking on the simplest way to make it fast. We can solve this
209 // problem when we move to TableGen.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000210 for (; Start != End; ++Start) {
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000211 // Scan for first option which is a proper prefix.
212 for (; Start != End; ++Start)
213 if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0)
214 break;
215 if (Start == End)
Daniel Dunbar7547f742009-03-23 21:50:40 +0000216 break;
217
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000218 // See if this option matches.
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000219 if (Arg *A = getOption(Start - OptionInfos + 1)->accept(Args, Index))
Daniel Dunbar7547f742009-03-23 21:50:40 +0000220 return A;
221
222 // Otherwise, see if this argument was missing values.
223 if (Prev != Index)
224 return 0;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000225 }
226
Daniel Dunbar532c1ec2010-06-09 22:31:08 +0000227 return new Arg(TheUnknownOption, Index++, Str);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000228}
Daniel Dunbar847abaa2009-11-19 06:35:06 +0000229
Axel Naumann9d520c52010-10-11 09:18:43 +0000230InputArgList *OptTable::ParseArgs(const char* const *ArgBegin,
231 const char* const *ArgEnd,
Daniel Dunbar847abaa2009-11-19 06:35:06 +0000232 unsigned &MissingArgIndex,
233 unsigned &MissingArgCount) const {
234 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
235
236 // FIXME: Handle '@' args (or at least error on them).
237
238 MissingArgIndex = MissingArgCount = 0;
239 unsigned Index = 0, End = ArgEnd - ArgBegin;
240 while (Index < End) {
241 // Ignore empty arguments (other things may still take them as arguments).
242 if (Args->getArgString(Index)[0] == '\0') {
243 ++Index;
244 continue;
245 }
246
247 unsigned Prev = Index;
248 Arg *A = ParseOneArg(*Args, Index);
249 assert(Index > Prev && "Parser failed to consume argument.");
250
251 // Check for missing argument error.
252 if (!A) {
253 assert(Index >= End && "Unexpected parser error.");
254 assert(Index - Prev - 1 && "No missing arguments!");
255 MissingArgIndex = Prev;
256 MissingArgCount = Index - Prev - 1;
257 break;
258 }
259
260 Args->append(A);
261 }
262
263 return Args;
264}
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000265
266static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
267 std::string Name = Opts.getOptionName(Id);
268
269 // Add metavar, if used.
270 switch (Opts.getOptionKind(Id)) {
271 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000272 llvm_unreachable("Invalid option with help text.");
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000273
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000274 case Option::MultiArgClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000275 llvm_unreachable("Cannot print metavar for this kind of option.");
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000276
277 case Option::FlagClass:
278 break;
279
280 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
281 Name += ' ';
282 // FALLTHROUGH
283 case Option::JoinedClass: case Option::CommaJoinedClass:
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000284 case Option::JoinedAndSeparateClass:
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000285 if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
286 Name += MetaVarName;
287 else
288 Name += "<value>";
289 break;
290 }
291
292 return Name;
293}
294
Chris Lattner5f9e2722011-07-23 10:55:15 +0000295static void PrintHelpOptionList(raw_ostream &OS, StringRef Title,
Daniel Dunbar2658f052009-12-04 21:08:40 +0000296 std::vector<std::pair<std::string,
297 const char*> > &OptionHelp) {
298 OS << Title << ":\n";
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000299
300 // Find the maximum option length.
301 unsigned OptionFieldWidth = 0;
302 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
303 // Skip titles.
304 if (!OptionHelp[i].second)
305 continue;
306
307 // Limit the amount of padding we are willing to give up for alignment.
308 unsigned Length = OptionHelp[i].first.size();
309 if (Length <= 23)
310 OptionFieldWidth = std::max(OptionFieldWidth, Length);
311 }
312
313 const unsigned InitialPad = 2;
314 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
315 const std::string &Option = OptionHelp[i].first;
316 int Pad = OptionFieldWidth - int(Option.size());
317 OS.indent(InitialPad) << Option;
318
319 // Break on long option names.
320 if (Pad < 0) {
321 OS << "\n";
322 Pad = OptionFieldWidth + InitialPad;
323 }
324 OS.indent(Pad + 1) << OptionHelp[i].second << '\n';
325 }
Daniel Dunbar2658f052009-12-04 21:08:40 +0000326}
327
328static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
329 unsigned GroupID = Opts.getOptionGroupID(Id);
330
331 // If not in a group, return the default help group.
332 if (!GroupID)
333 return "OPTIONS";
334
335 // Abuse the help text of the option groups to store the "help group"
336 // name.
337 //
338 // FIXME: Split out option groups.
339 if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
340 return GroupHelp;
341
342 // Otherwise keep looking.
343 return getOptionHelpGroup(Opts, GroupID);
344}
345
Chris Lattner5f9e2722011-07-23 10:55:15 +0000346void OptTable::PrintHelp(raw_ostream &OS, const char *Name,
Daniel Dunbar2658f052009-12-04 21:08:40 +0000347 const char *Title, bool ShowHidden) const {
348 OS << "OVERVIEW: " << Title << "\n";
349 OS << '\n';
350 OS << "USAGE: " << Name << " [options] <inputs>\n";
351 OS << '\n';
352
353 // Render help text into a map of group-name to a list of (option, help)
354 // pairs.
355 typedef std::map<std::string,
356 std::vector<std::pair<std::string, const char*> > > helpmap_ty;
357 helpmap_ty GroupedOptionHelp;
358
359 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
360 unsigned Id = i + 1;
361
362 // FIXME: Split out option groups.
363 if (getOptionKind(Id) == Option::GroupClass)
364 continue;
365
366 if (!ShowHidden && isOptionHelpHidden(Id))
367 continue;
368
369 if (const char *Text = getOptionHelpText(Id)) {
370 const char *HelpGroup = getOptionHelpGroup(*this, Id);
371 const std::string &OptName = getOptionHelpName(*this, Id);
372 GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text));
373 }
374 }
375
376 for (helpmap_ty::iterator it = GroupedOptionHelp .begin(),
377 ie = GroupedOptionHelp.end(); it != ie; ++it) {
378 if (it != GroupedOptionHelp .begin())
379 OS << "\n";
380 PrintHelpOptionList(OS, it->first, it->second);
381 }
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000382
383 OS.flush();
384}