blob: f69d5d806d2822fb7904bc6827f6c95af4bcdc6e [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 Dunbar60e107f2009-12-03 07:01:38 +000014#include "llvm/Support/raw_ostream.h"
Daniel Dunbar7547f742009-03-23 21:50:40 +000015#include <algorithm>
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000016#include <cassert>
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;
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 Dunbar9e1f9822009-11-19 04:14:53 +000075OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
76
77//
78
Daniel Dunbara79a2b52009-11-18 20:19:36 +000079OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos)
80 : OptionInfos(_OptionInfos), NumOptionInfos(_NumOptionInfos),
81 Options(new Option*[NumOptionInfos]),
82 TheInputOption(0), TheUnknownOption(0), FirstSearchableIndex(0)
83{
Daniel Dunbar47393ba2009-07-13 21:50:47 +000084 // Explicitly zero initialize the error to work around a bug in array
85 // value-initialization on MinGW with gcc 4.3.5.
Daniel Dunbara79a2b52009-11-18 20:19:36 +000086 memset(Options, 0, sizeof(*Options) * NumOptionInfos);
Daniel Dunbar47393ba2009-07-13 21:50:47 +000087
Daniel Dunbarb3556922009-03-23 18:41:45 +000088 // Find start of normal options.
Daniel Dunbara79a2b52009-11-18 20:19:36 +000089 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
90 unsigned Kind = getInfo(i + 1).Kind;
91 if (Kind == Option::InputClass) {
92 assert(!TheInputOption && "Cannot have multiple input options!");
93 TheInputOption = getOption(i + 1);
94 } else if (Kind == Option::UnknownClass) {
95 assert(!TheUnknownOption && "Cannot have multiple input options!");
96 TheUnknownOption = getOption(i + 1);
97 } else if (Kind != Option::GroupClass) {
98 FirstSearchableIndex = i;
Daniel Dunbarb3556922009-03-23 18:41:45 +000099 break;
100 }
101 }
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000102 assert(FirstSearchableIndex != 0 && "No searchable options?");
Daniel Dunbarb3556922009-03-23 18:41:45 +0000103
104#ifndef NDEBUG
105 // Check that everything after the first searchable option is a
106 // regular option class.
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000107 for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) {
108 Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind;
Daniel Dunbarb3556922009-03-23 18:41:45 +0000109 assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
110 Kind != Option::GroupClass) &&
111 "Special options should be defined first!");
112 }
113
114 // Check that options are in order.
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000115 for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) {
116 if (!(getInfo(i) < getInfo(i + 1))) {
117 getOption(i)->dump();
118 getOption(i + 1)->dump();
Daniel Dunbarb3556922009-03-23 18:41:45 +0000119 assert(0 && "Options are not in order!");
120 }
121 }
Mike Stump1eb44332009-09-09 15:08:12 +0000122#endif
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000123}
124
Mike Stump1eb44332009-09-09 15:08:12 +0000125OptTable::~OptTable() {
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000126 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000127 delete Options[i];
128 delete[] Options;
129}
130
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000131Option *OptTable::CreateOption(unsigned id) const {
132 const Info &info = getInfo(id);
Mike Stump1eb44332009-09-09 15:08:12 +0000133 const OptionGroup *Group =
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000134 cast_or_null<OptionGroup>(getOption(info.GroupID));
135 const Option *Alias = getOption(info.AliasID);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000136
137 Option *Opt = 0;
138 switch (info.Kind) {
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000139 case Option::InputClass:
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000140 Opt = new InputOption(id); break;
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000141 case Option::UnknownClass:
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000142 Opt = new UnknownOption(id); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000143 case Option::GroupClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000144 Opt = new OptionGroup(id, info.Name, Group); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000145 case Option::FlagClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000146 Opt = new FlagOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000147 case Option::JoinedClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000148 Opt = new JoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000149 case Option::SeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000150 Opt = new SeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000151 case Option::CommaJoinedClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000152 Opt = new CommaJoinedOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000153 case Option::MultiArgClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000154 Opt = new MultiArgOption(id, info.Name, Group, Alias, info.Param); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000155 case Option::JoinedOrSeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000156 Opt = new JoinedOrSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000157 case Option::JoinedAndSeparateClass:
Daniel Dunbar30b055f2009-03-04 21:53:04 +0000158 Opt = new JoinedAndSeparateOption(id, info.Name, Group, Alias); break;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000159 }
160
Daniel Dunbara4f64802009-11-18 17:42:34 +0000161 if (info.Flags & DriverOption)
162 Opt->setDriverOption(true);
163 if (info.Flags & LinkerInput)
164 Opt->setLinkerInput(true);
165 if (info.Flags & NoArgumentUnused)
166 Opt->setNoArgumentUnused(true);
167 if (info.Flags & RenderAsInput)
168 Opt->setNoOptAsInput(true);
169 if (info.Flags & RenderJoined) {
170 assert(info.Kind == Option::SeparateClass && "Invalid option.");
171 Opt->setForceJoinedRender(true);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000172 }
Daniel Dunbara4f64802009-11-18 17:42:34 +0000173 if (info.Flags & RenderSeparate) {
174 assert(info.Kind == Option::JoinedClass && "Invalid option.");
175 Opt->setForceSeparateRender(true);
176 }
177 if (info.Flags & Unsupported)
178 Opt->setUnsupported(true);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000179
180 return Opt;
181}
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000182
Daniel Dunbarf3cad362009-03-25 04:13:45 +0000183Arg *OptTable::ParseOneArg(const InputArgList &Args, unsigned &Index) const {
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000184 unsigned Prev = Index;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000185 const char *Str = Args.getArgString(Index);
186
Daniel Dunbara9480452009-03-12 08:44:47 +0000187 // Anything that doesn't start with '-' is an input, as is '-' itself.
188 if (Str[0] != '-' || Str[1] == '\0')
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000189 return new PositionalArg(TheInputOption, Index++);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000190
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000191 const Info *Start = OptionInfos + FirstSearchableIndex;
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000192 const Info *End = OptionInfos + getNumOptions();
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000193
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000194 // Search for the first next option which could be a prefix.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000195 Start = std::lower_bound(Start, End, Str);
196
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000197 // Options are stored in sorted order, with '\0' at the end of the
198 // alphabet. Since the only options which can accept a string must
199 // prefix it, we iteratively search for the next option which could
200 // be a prefix.
201 //
202 // FIXME: This is searching much more than necessary, but I am
203 // blanking on the simplest way to make it fast. We can solve this
204 // problem when we move to TableGen.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000205 for (; Start != End; ++Start) {
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000206 // Scan for first option which is a proper prefix.
207 for (; Start != End; ++Start)
208 if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0)
209 break;
210 if (Start == End)
Daniel Dunbar7547f742009-03-23 21:50:40 +0000211 break;
212
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000213 // See if this option matches.
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000214 if (Arg *A = getOption(Start - OptionInfos + 1)->accept(Args, Index))
Daniel Dunbar7547f742009-03-23 21:50:40 +0000215 return A;
216
217 // Otherwise, see if this argument was missing values.
218 if (Prev != Index)
219 return 0;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000220 }
221
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000222 return new PositionalArg(TheUnknownOption, Index++);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000223}
Daniel Dunbar847abaa2009-11-19 06:35:06 +0000224
225InputArgList *OptTable::ParseArgs(const char **ArgBegin, const char **ArgEnd,
226 unsigned &MissingArgIndex,
227 unsigned &MissingArgCount) const {
228 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
229
230 // FIXME: Handle '@' args (or at least error on them).
231
232 MissingArgIndex = MissingArgCount = 0;
233 unsigned Index = 0, End = ArgEnd - ArgBegin;
234 while (Index < End) {
235 // Ignore empty arguments (other things may still take them as arguments).
236 if (Args->getArgString(Index)[0] == '\0') {
237 ++Index;
238 continue;
239 }
240
241 unsigned Prev = Index;
242 Arg *A = ParseOneArg(*Args, Index);
243 assert(Index > Prev && "Parser failed to consume argument.");
244
245 // Check for missing argument error.
246 if (!A) {
247 assert(Index >= End && "Unexpected parser error.");
248 assert(Index - Prev - 1 && "No missing arguments!");
249 MissingArgIndex = Prev;
250 MissingArgCount = Index - Prev - 1;
251 break;
252 }
253
254 Args->append(A);
255 }
256
257 return Args;
258}
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000259
260static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
261 std::string Name = Opts.getOptionName(Id);
262
263 // Add metavar, if used.
264 switch (Opts.getOptionKind(Id)) {
265 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
266 assert(0 && "Invalid option with help text.");
267
268 case Option::MultiArgClass: case Option::JoinedAndSeparateClass:
269 assert(0 && "Cannot print metavar for this kind of option.");
270
271 case Option::FlagClass:
272 break;
273
274 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
275 Name += ' ';
276 // FALLTHROUGH
277 case Option::JoinedClass: case Option::CommaJoinedClass:
278 if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
279 Name += MetaVarName;
280 else
281 Name += "<value>";
282 break;
283 }
284
285 return Name;
286}
287
Daniel Dunbar2658f052009-12-04 21:08:40 +0000288static void PrintHelpOptionList(llvm::raw_ostream &OS, llvm::StringRef Title,
289 std::vector<std::pair<std::string,
290 const char*> > &OptionHelp) {
291 OS << Title << ":\n";
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000292
293 // Find the maximum option length.
294 unsigned OptionFieldWidth = 0;
295 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
296 // Skip titles.
297 if (!OptionHelp[i].second)
298 continue;
299
300 // Limit the amount of padding we are willing to give up for alignment.
301 unsigned Length = OptionHelp[i].first.size();
302 if (Length <= 23)
303 OptionFieldWidth = std::max(OptionFieldWidth, Length);
304 }
305
306 const unsigned InitialPad = 2;
307 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
308 const std::string &Option = OptionHelp[i].first;
309 int Pad = OptionFieldWidth - int(Option.size());
310 OS.indent(InitialPad) << Option;
311
312 // Break on long option names.
313 if (Pad < 0) {
314 OS << "\n";
315 Pad = OptionFieldWidth + InitialPad;
316 }
317 OS.indent(Pad + 1) << OptionHelp[i].second << '\n';
318 }
Daniel Dunbar2658f052009-12-04 21:08:40 +0000319}
320
321static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
322 unsigned GroupID = Opts.getOptionGroupID(Id);
323
324 // If not in a group, return the default help group.
325 if (!GroupID)
326 return "OPTIONS";
327
328 // Abuse the help text of the option groups to store the "help group"
329 // name.
330 //
331 // FIXME: Split out option groups.
332 if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
333 return GroupHelp;
334
335 // Otherwise keep looking.
336 return getOptionHelpGroup(Opts, GroupID);
337}
338
339void OptTable::PrintHelp(llvm::raw_ostream &OS, const char *Name,
340 const char *Title, bool ShowHidden) const {
341 OS << "OVERVIEW: " << Title << "\n";
342 OS << '\n';
343 OS << "USAGE: " << Name << " [options] <inputs>\n";
344 OS << '\n';
345
346 // Render help text into a map of group-name to a list of (option, help)
347 // pairs.
348 typedef std::map<std::string,
349 std::vector<std::pair<std::string, const char*> > > helpmap_ty;
350 helpmap_ty GroupedOptionHelp;
351
352 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
353 unsigned Id = i + 1;
354
355 // FIXME: Split out option groups.
356 if (getOptionKind(Id) == Option::GroupClass)
357 continue;
358
359 if (!ShowHidden && isOptionHelpHidden(Id))
360 continue;
361
362 if (const char *Text = getOptionHelpText(Id)) {
363 const char *HelpGroup = getOptionHelpGroup(*this, Id);
364 const std::string &OptName = getOptionHelpName(*this, Id);
365 GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text));
366 }
367 }
368
369 for (helpmap_ty::iterator it = GroupedOptionHelp .begin(),
370 ie = GroupedOptionHelp.end(); it != ie; ++it) {
371 if (it != GroupedOptionHelp .begin())
372 OS << "\n";
373 PrintHelpOptionList(OS, it->first, it->second);
374 }
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000375
376 OS.flush();
377}