blob: db592987431f427a7521e8deee2544d7f69e4e5b [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"
Michael J. Spencere4151c52012-10-19 22:36:40 +000014#include "clang/Driver/Options.h"
Daniel Dunbar60e107f2009-12-03 07:01:38 +000015#include "llvm/Support/raw_ostream.h"
David Blaikie548f6c82011-09-23 05:57:42 +000016#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar7547f742009-03-23 21:50:40 +000017#include <algorithm>
Daniel Dunbar2658f052009-12-04 21:08:40 +000018#include <map>
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000019using namespace clang::driver;
20using namespace clang::driver::options;
Chris Lattner5f9e2722011-07-23 10:55:15 +000021using namespace clang;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000022
Daniel Dunbarb3556922009-03-23 18:41:45 +000023// Ordering on Info. The ordering is *almost* lexicographic, with two
24// exceptions. First, '\0' comes at the end of the alphabet instead of
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000025// the beginning (thus options precede any other options which prefix
Daniel Dunbarb3556922009-03-23 18:41:45 +000026// them). Second, for options with the same name, the less permissive
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000027// version should come first; a Flag option should precede a Joined
Daniel Dunbarb3556922009-03-23 18:41:45 +000028// option, for example.
29
30static int StrCmpOptionName(const char *A, const char *B) {
31 char a = *A, b = *B;
32 while (a == b) {
33 if (a == '\0')
34 return 0;
35
36 a = *++A;
37 b = *++B;
38 }
39
40 if (a == '\0') // A is a prefix of B.
41 return 1;
42 if (b == '\0') // B is a prefix of A.
43 return -1;
44
45 // Otherwise lexicographic.
46 return (a < b) ? -1 : 1;
47}
48
Daniel Dunbar1ce9cf02009-11-18 21:42:57 +000049namespace clang {
50namespace driver {
Daniel Dunbara79a2b52009-11-18 20:19:36 +000051static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
Daniel Dunbarb3556922009-03-23 18:41:45 +000052 if (&A == &B)
53 return false;
54
55 if (int N = StrCmpOptionName(A.Name, B.Name))
56 return N == -1;
57
Michael J. Spencerc6357102012-10-22 22:13:48 +000058 for (const char * const *APre = A.Prefixes,
59 * const *BPre = B.Prefixes;
60 *APre != 0 && *BPre != 0; ++APre, ++BPre) {
61 if (int N = StrCmpOptionName(*APre, *BPre))
62 return N == -1;
63 }
64
Daniel Dunbarb3556922009-03-23 18:41:45 +000065 // Names are the same, check that classes are in order; exactly one
66 // should be joined, and it should succeed the other.
Daniel Dunbarcf51ece2009-03-25 03:06:26 +000067 assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
Daniel Dunbarb3556922009-03-23 18:41:45 +000068 "Unexpected classes for options with same name.");
69 return B.Kind == Option::JoinedClass;
70}
71
Daniel Dunbar1ce9cf02009-11-18 21:42:57 +000072// Support lower_bound between info and an option name.
73static inline bool operator<(const OptTable::Info &I, const char *Name) {
74 return StrCmpOptionName(I.Name, Name) == -1;
75}
76static inline bool operator<(const char *Name, const OptTable::Info &I) {
77 return StrCmpOptionName(Name, I.Name) == -1;
78}
79}
80}
81
Daniel Dunbarb3556922009-03-23 18:41:45 +000082//
83
Daniel Dunbar9e1f9822009-11-19 04:14:53 +000084OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
85
86//
87
Daniel Dunbara79a2b52009-11-18 20:19:36 +000088OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos)
Michael J. Spencere4151c52012-10-19 22:36:40 +000089 : OptionInfos(_OptionInfos),
90 NumOptionInfos(_NumOptionInfos),
91 TheInputOptionID(0),
92 TheUnknownOptionID(0),
93 FirstSearchableIndex(0)
Daniel Dunbara79a2b52009-11-18 20:19:36 +000094{
Daniel Dunbar47393ba2009-07-13 21:50:47 +000095 // Explicitly zero initialize the error to work around a bug in array
96 // value-initialization on MinGW with gcc 4.3.5.
Daniel Dunbar47393ba2009-07-13 21:50:47 +000097
Daniel Dunbarb3556922009-03-23 18:41:45 +000098 // Find start of normal options.
Daniel Dunbara79a2b52009-11-18 20:19:36 +000099 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
100 unsigned Kind = getInfo(i + 1).Kind;
101 if (Kind == Option::InputClass) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000102 assert(!TheInputOptionID && "Cannot have multiple input options!");
103 TheInputOptionID = getInfo(i + 1).ID;
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000104 } else if (Kind == Option::UnknownClass) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000105 assert(!TheUnknownOptionID && "Cannot have multiple unknown options!");
106 TheUnknownOptionID = getInfo(i + 1).ID;
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000107 } else if (Kind != Option::GroupClass) {
108 FirstSearchableIndex = i;
Daniel Dunbarb3556922009-03-23 18:41:45 +0000109 break;
110 }
111 }
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000112 assert(FirstSearchableIndex != 0 && "No searchable options?");
Daniel Dunbarb3556922009-03-23 18:41:45 +0000113
114#ifndef NDEBUG
115 // Check that everything after the first searchable option is a
116 // regular option class.
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000117 for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) {
118 Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind;
Daniel Dunbarb3556922009-03-23 18:41:45 +0000119 assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
120 Kind != Option::GroupClass) &&
121 "Special options should be defined first!");
122 }
123
124 // Check that options are in order.
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000125 for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) {
126 if (!(getInfo(i) < getInfo(i + 1))) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000127 getOption(i).dump();
128 getOption(i + 1).dump();
David Blaikieb219cfc2011-09-23 05:06:16 +0000129 llvm_unreachable("Options are not in order!");
Daniel Dunbarb3556922009-03-23 18:41:45 +0000130 }
131 }
Mike Stump1eb44332009-09-09 15:08:12 +0000132#endif
Michael J. Spencerc6357102012-10-22 22:13:48 +0000133
134 // Build prefixes.
135 for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) {
136 if (const char *const *P = getInfo(i).Prefixes) {
137 for (; *P != 0; ++P) {
138 PrefixesUnion.insert(*P);
139 }
140 }
141 }
142
143 // Build prefix chars.
144 for (llvm::StringSet<>::const_iterator I = PrefixesUnion.begin(),
145 E = PrefixesUnion.end(); I != E; ++I) {
146 StringRef Prefix = I->getKey();
147 for (StringRef::const_iterator C = Prefix.begin(), CE = Prefix.end();
148 C != CE; ++C)
149 if (std::find(PrefixChars.begin(), PrefixChars.end(), *C)
150 == PrefixChars.end())
151 PrefixChars.push_back(*C);
152 }
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000153}
154
Mike Stump1eb44332009-09-09 15:08:12 +0000155OptTable::~OptTable() {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000156}
157
158const Option OptTable::getOption(OptSpecifier Opt) const {
159 unsigned id = Opt.getID();
160 if (id == 0)
161 return Option(0, 0);
162 assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID.");
163 return Option(&getInfo(id), this);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000164}
165
Michael J. Spencer04a42792012-08-21 18:51:17 +0000166bool OptTable::isOptionHelpHidden(OptSpecifier id) const {
167 return getInfo(id).Flags & options::HelpHidden;
168}
169
Michael J. Spencerc6357102012-10-22 22:13:48 +0000170static bool isInput(const llvm::StringSet<> &Prefixes, StringRef Arg) {
171 if (Arg == "-")
172 return true;
173 for (llvm::StringSet<>::const_iterator I = Prefixes.begin(),
174 E = Prefixes.end(); I != E; ++I)
175 if (Arg.startswith(I->getKey()))
176 return false;
177 return true;
178}
179
180/// \returns Matched size. 0 means no match.
181static unsigned matchOption(const OptTable::Info *I, StringRef Str) {
182 for (const char * const *Pre = I->Prefixes; *Pre != 0; ++Pre) {
183 StringRef Prefix(*Pre);
184 if (Str.startswith(Prefix) && Str.substr(Prefix.size()).startswith(I->Name))
185 return Prefix.size() + StringRef(I->Name).size();
186 }
187 return 0;
188}
189
Daniel Dunbar785e7962010-06-11 22:00:17 +0000190Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index) const {
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000191 unsigned Prev = Index;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000192 const char *Str = Args.getArgString(Index);
193
Michael J. Spencerc6357102012-10-22 22:13:48 +0000194 // Anything that doesn't start with PrefixesUnion is an input, as is '-'
195 // itself.
196 if (isInput(PrefixesUnion, Str))
197 return new Arg(getOption(TheInputOptionID), Str, Index++, Str);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000198
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000199 const Info *Start = OptionInfos + FirstSearchableIndex;
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000200 const Info *End = OptionInfos + getNumOptions();
Michael J. Spencerc6357102012-10-22 22:13:48 +0000201 StringRef Name = StringRef(Str).ltrim(PrefixChars);
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000202
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000203 // Search for the first next option which could be a prefix.
Michael J. Spencerc6357102012-10-22 22:13:48 +0000204 Start = std::lower_bound(Start, End, Name.data());
Daniel Dunbar7547f742009-03-23 21:50:40 +0000205
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000206 // Options are stored in sorted order, with '\0' at the end of the
207 // alphabet. Since the only options which can accept a string must
208 // prefix it, we iteratively search for the next option which could
209 // be a prefix.
210 //
211 // FIXME: This is searching much more than necessary, but I am
212 // blanking on the simplest way to make it fast. We can solve this
213 // problem when we move to TableGen.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000214 for (; Start != End; ++Start) {
Michael J. Spencerc6357102012-10-22 22:13:48 +0000215 unsigned ArgSize = 0;
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000216 // Scan for first option which is a proper prefix.
217 for (; Start != End; ++Start)
Michael J. Spencerc6357102012-10-22 22:13:48 +0000218 if ((ArgSize = matchOption(Start, Str)))
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000219 break;
220 if (Start == End)
Daniel Dunbar7547f742009-03-23 21:50:40 +0000221 break;
222
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000223 // See if this option matches.
Michael J. Spencerc6357102012-10-22 22:13:48 +0000224 if (Arg *A = Option(Start, this).accept(Args, Index, ArgSize))
Daniel Dunbar7547f742009-03-23 21:50:40 +0000225 return A;
226
227 // Otherwise, see if this argument was missing values.
228 if (Prev != Index)
229 return 0;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000230 }
231
Michael J. Spencerc6357102012-10-22 22:13:48 +0000232 return new Arg(getOption(TheUnknownOptionID), Str, Index++, Str);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000233}
Daniel Dunbar847abaa2009-11-19 06:35:06 +0000234
Axel Naumann9d520c52010-10-11 09:18:43 +0000235InputArgList *OptTable::ParseArgs(const char* const *ArgBegin,
236 const char* const *ArgEnd,
Daniel Dunbar847abaa2009-11-19 06:35:06 +0000237 unsigned &MissingArgIndex,
238 unsigned &MissingArgCount) const {
239 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
240
241 // FIXME: Handle '@' args (or at least error on them).
242
243 MissingArgIndex = MissingArgCount = 0;
244 unsigned Index = 0, End = ArgEnd - ArgBegin;
245 while (Index < End) {
246 // Ignore empty arguments (other things may still take them as arguments).
247 if (Args->getArgString(Index)[0] == '\0') {
248 ++Index;
249 continue;
250 }
251
252 unsigned Prev = Index;
253 Arg *A = ParseOneArg(*Args, Index);
254 assert(Index > Prev && "Parser failed to consume argument.");
255
256 // Check for missing argument error.
257 if (!A) {
258 assert(Index >= End && "Unexpected parser error.");
259 assert(Index - Prev - 1 && "No missing arguments!");
260 MissingArgIndex = Prev;
261 MissingArgCount = Index - Prev - 1;
262 break;
263 }
264
265 Args->append(A);
266 }
267
268 return Args;
269}
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000270
271static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
Michael J. Spencerc6357102012-10-22 22:13:48 +0000272 const Option O = Opts.getOption(Id);
273 std::string Name = O.getPrefixedName();
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000274
275 // Add metavar, if used.
Michael J. Spencerc6357102012-10-22 22:13:48 +0000276 switch (O.getKind()) {
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000277 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000278 llvm_unreachable("Invalid option with help text.");
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000279
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000280 case Option::MultiArgClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000281 llvm_unreachable("Cannot print metavar for this kind of option.");
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000282
283 case Option::FlagClass:
284 break;
285
286 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
287 Name += ' ';
288 // FALLTHROUGH
289 case Option::JoinedClass: case Option::CommaJoinedClass:
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000290 case Option::JoinedAndSeparateClass:
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000291 if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
292 Name += MetaVarName;
293 else
294 Name += "<value>";
295 break;
296 }
297
298 return Name;
299}
300
Chris Lattner5f9e2722011-07-23 10:55:15 +0000301static void PrintHelpOptionList(raw_ostream &OS, StringRef Title,
Daniel Dunbar2658f052009-12-04 21:08:40 +0000302 std::vector<std::pair<std::string,
303 const char*> > &OptionHelp) {
304 OS << Title << ":\n";
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000305
306 // Find the maximum option length.
307 unsigned OptionFieldWidth = 0;
308 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
309 // Skip titles.
310 if (!OptionHelp[i].second)
311 continue;
312
313 // Limit the amount of padding we are willing to give up for alignment.
314 unsigned Length = OptionHelp[i].first.size();
315 if (Length <= 23)
316 OptionFieldWidth = std::max(OptionFieldWidth, Length);
317 }
318
319 const unsigned InitialPad = 2;
320 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
321 const std::string &Option = OptionHelp[i].first;
322 int Pad = OptionFieldWidth - int(Option.size());
323 OS.indent(InitialPad) << Option;
324
325 // Break on long option names.
326 if (Pad < 0) {
327 OS << "\n";
328 Pad = OptionFieldWidth + InitialPad;
329 }
330 OS.indent(Pad + 1) << OptionHelp[i].second << '\n';
331 }
Daniel Dunbar2658f052009-12-04 21:08:40 +0000332}
333
334static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
335 unsigned GroupID = Opts.getOptionGroupID(Id);
336
337 // If not in a group, return the default help group.
338 if (!GroupID)
339 return "OPTIONS";
340
341 // Abuse the help text of the option groups to store the "help group"
342 // name.
343 //
344 // FIXME: Split out option groups.
345 if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
346 return GroupHelp;
347
348 // Otherwise keep looking.
349 return getOptionHelpGroup(Opts, GroupID);
350}
351
Chris Lattner5f9e2722011-07-23 10:55:15 +0000352void OptTable::PrintHelp(raw_ostream &OS, const char *Name,
Daniel Dunbar2658f052009-12-04 21:08:40 +0000353 const char *Title, bool ShowHidden) const {
354 OS << "OVERVIEW: " << Title << "\n";
355 OS << '\n';
356 OS << "USAGE: " << Name << " [options] <inputs>\n";
357 OS << '\n';
358
359 // Render help text into a map of group-name to a list of (option, help)
360 // pairs.
361 typedef std::map<std::string,
362 std::vector<std::pair<std::string, const char*> > > helpmap_ty;
363 helpmap_ty GroupedOptionHelp;
364
365 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
366 unsigned Id = i + 1;
367
368 // FIXME: Split out option groups.
369 if (getOptionKind(Id) == Option::GroupClass)
370 continue;
371
372 if (!ShowHidden && isOptionHelpHidden(Id))
373 continue;
374
375 if (const char *Text = getOptionHelpText(Id)) {
376 const char *HelpGroup = getOptionHelpGroup(*this, Id);
377 const std::string &OptName = getOptionHelpName(*this, Id);
378 GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text));
379 }
380 }
381
382 for (helpmap_ty::iterator it = GroupedOptionHelp .begin(),
383 ie = GroupedOptionHelp.end(); it != ie; ++it) {
384 if (it != GroupedOptionHelp .begin())
385 OS << "\n";
386 PrintHelpOptionList(OS, it->first, it->second);
387 }
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000388
389 OS.flush();
390}