blob: 6e7b6951fb83958029da66c40e3e07ef0658d914 [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. Spencerc6357102012-10-22 22:13:48 +0000166static bool isInput(const llvm::StringSet<> &Prefixes, StringRef Arg) {
167 if (Arg == "-")
168 return true;
169 for (llvm::StringSet<>::const_iterator I = Prefixes.begin(),
170 E = Prefixes.end(); I != E; ++I)
171 if (Arg.startswith(I->getKey()))
172 return false;
173 return true;
174}
175
176/// \returns Matched size. 0 means no match.
177static unsigned matchOption(const OptTable::Info *I, StringRef Str) {
178 for (const char * const *Pre = I->Prefixes; *Pre != 0; ++Pre) {
179 StringRef Prefix(*Pre);
180 if (Str.startswith(Prefix) && Str.substr(Prefix.size()).startswith(I->Name))
181 return Prefix.size() + StringRef(I->Name).size();
182 }
183 return 0;
184}
185
Daniel Dunbar785e7962010-06-11 22:00:17 +0000186Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index) const {
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000187 unsigned Prev = Index;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000188 const char *Str = Args.getArgString(Index);
189
Michael J. Spencerc6357102012-10-22 22:13:48 +0000190 // Anything that doesn't start with PrefixesUnion is an input, as is '-'
191 // itself.
192 if (isInput(PrefixesUnion, Str))
193 return new Arg(getOption(TheInputOptionID), Str, Index++, Str);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000194
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000195 const Info *Start = OptionInfos + FirstSearchableIndex;
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000196 const Info *End = OptionInfos + getNumOptions();
Michael J. Spencerc6357102012-10-22 22:13:48 +0000197 StringRef Name = StringRef(Str).ltrim(PrefixChars);
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.
Michael J. Spencerc6357102012-10-22 22:13:48 +0000200 Start = std::lower_bound(Start, End, Name.data());
Daniel Dunbar7547f742009-03-23 21:50:40 +0000201
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) {
Michael J. Spencerc6357102012-10-22 22:13:48 +0000211 unsigned ArgSize = 0;
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000212 // Scan for first option which is a proper prefix.
213 for (; Start != End; ++Start)
Michael J. Spencerc6357102012-10-22 22:13:48 +0000214 if ((ArgSize = matchOption(Start, Str)))
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000215 break;
216 if (Start == End)
Daniel Dunbar7547f742009-03-23 21:50:40 +0000217 break;
218
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000219 // See if this option matches.
Michael J. Spencerc6357102012-10-22 22:13:48 +0000220 if (Arg *A = Option(Start, this).accept(Args, Index, ArgSize))
Daniel Dunbar7547f742009-03-23 21:50:40 +0000221 return A;
222
223 // Otherwise, see if this argument was missing values.
224 if (Prev != Index)
225 return 0;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000226 }
227
Michael J. Spencerc6357102012-10-22 22:13:48 +0000228 return new Arg(getOption(TheUnknownOptionID), Str, Index++, Str);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000229}
Daniel Dunbar847abaa2009-11-19 06:35:06 +0000230
Axel Naumann9d520c52010-10-11 09:18:43 +0000231InputArgList *OptTable::ParseArgs(const char* const *ArgBegin,
232 const char* const *ArgEnd,
Daniel Dunbar847abaa2009-11-19 06:35:06 +0000233 unsigned &MissingArgIndex,
234 unsigned &MissingArgCount) const {
235 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
236
237 // FIXME: Handle '@' args (or at least error on them).
238
239 MissingArgIndex = MissingArgCount = 0;
240 unsigned Index = 0, End = ArgEnd - ArgBegin;
241 while (Index < End) {
242 // Ignore empty arguments (other things may still take them as arguments).
243 if (Args->getArgString(Index)[0] == '\0') {
244 ++Index;
245 continue;
246 }
247
248 unsigned Prev = Index;
249 Arg *A = ParseOneArg(*Args, Index);
250 assert(Index > Prev && "Parser failed to consume argument.");
251
252 // Check for missing argument error.
253 if (!A) {
254 assert(Index >= End && "Unexpected parser error.");
255 assert(Index - Prev - 1 && "No missing arguments!");
256 MissingArgIndex = Prev;
257 MissingArgCount = Index - Prev - 1;
258 break;
259 }
260
261 Args->append(A);
262 }
263
264 return Args;
265}
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000266
267static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
Michael J. Spencerc6357102012-10-22 22:13:48 +0000268 const Option O = Opts.getOption(Id);
269 std::string Name = O.getPrefixedName();
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000270
271 // Add metavar, if used.
Michael J. Spencerc6357102012-10-22 22:13:48 +0000272 switch (O.getKind()) {
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000273 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000274 llvm_unreachable("Invalid option with help text.");
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000275
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000276 case Option::MultiArgClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000277 llvm_unreachable("Cannot print metavar for this kind of option.");
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000278
279 case Option::FlagClass:
280 break;
281
282 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
283 Name += ' ';
284 // FALLTHROUGH
285 case Option::JoinedClass: case Option::CommaJoinedClass:
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000286 case Option::JoinedAndSeparateClass:
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000287 if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
288 Name += MetaVarName;
289 else
290 Name += "<value>";
291 break;
292 }
293
294 return Name;
295}
296
Chris Lattner5f9e2722011-07-23 10:55:15 +0000297static void PrintHelpOptionList(raw_ostream &OS, StringRef Title,
Daniel Dunbar2658f052009-12-04 21:08:40 +0000298 std::vector<std::pair<std::string,
299 const char*> > &OptionHelp) {
300 OS << Title << ":\n";
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000301
302 // Find the maximum option length.
303 unsigned OptionFieldWidth = 0;
304 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
305 // Skip titles.
306 if (!OptionHelp[i].second)
307 continue;
308
309 // Limit the amount of padding we are willing to give up for alignment.
310 unsigned Length = OptionHelp[i].first.size();
311 if (Length <= 23)
312 OptionFieldWidth = std::max(OptionFieldWidth, Length);
313 }
314
315 const unsigned InitialPad = 2;
316 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
317 const std::string &Option = OptionHelp[i].first;
318 int Pad = OptionFieldWidth - int(Option.size());
319 OS.indent(InitialPad) << Option;
320
321 // Break on long option names.
322 if (Pad < 0) {
323 OS << "\n";
324 Pad = OptionFieldWidth + InitialPad;
325 }
326 OS.indent(Pad + 1) << OptionHelp[i].second << '\n';
327 }
Daniel Dunbar2658f052009-12-04 21:08:40 +0000328}
329
330static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
331 unsigned GroupID = Opts.getOptionGroupID(Id);
332
333 // If not in a group, return the default help group.
334 if (!GroupID)
335 return "OPTIONS";
336
337 // Abuse the help text of the option groups to store the "help group"
338 // name.
339 //
340 // FIXME: Split out option groups.
341 if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
342 return GroupHelp;
343
344 // Otherwise keep looking.
345 return getOptionHelpGroup(Opts, GroupID);
346}
347
Chris Lattner5f9e2722011-07-23 10:55:15 +0000348void OptTable::PrintHelp(raw_ostream &OS, const char *Name,
Richard Smithf1eaab12012-11-09 22:36:44 +0000349 const char *Title, unsigned short FlagsToInclude,
350 unsigned short FlagsToExclude) const {
Daniel Dunbar2658f052009-12-04 21:08:40 +0000351 OS << "OVERVIEW: " << Title << "\n";
352 OS << '\n';
353 OS << "USAGE: " << Name << " [options] <inputs>\n";
354 OS << '\n';
355
356 // Render help text into a map of group-name to a list of (option, help)
357 // pairs.
358 typedef std::map<std::string,
359 std::vector<std::pair<std::string, const char*> > > helpmap_ty;
360 helpmap_ty GroupedOptionHelp;
361
362 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
363 unsigned Id = i + 1;
364
365 // FIXME: Split out option groups.
366 if (getOptionKind(Id) == Option::GroupClass)
367 continue;
368
Richard Smithf1eaab12012-11-09 22:36:44 +0000369 if ((FlagsToInclude && !(getInfo(Id).Flags & FlagsToInclude)) ||
370 getInfo(Id).Flags & FlagsToExclude)
Daniel Dunbar2658f052009-12-04 21:08:40 +0000371 continue;
372
373 if (const char *Text = getOptionHelpText(Id)) {
374 const char *HelpGroup = getOptionHelpGroup(*this, Id);
375 const std::string &OptName = getOptionHelpName(*this, Id);
376 GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text));
377 }
378 }
379
380 for (helpmap_ty::iterator it = GroupedOptionHelp .begin(),
381 ie = GroupedOptionHelp.end(); it != ie; ++it) {
382 if (it != GroupedOptionHelp .begin())
383 OS << "\n";
384 PrintHelpOptionList(OS, it->first, it->second);
385 }
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000386
387 OS.flush();
388}