blob: 6842f4d57919ceb6c5230ba9fd6668b941e57312 [file] [log] [blame]
Michael J. Spencer41ee0412012-12-05 00:29:32 +00001//===--- OptTable.cpp - Option Table Implementation -----------------------===//
2//
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
10#include "llvm/Option/OptTable.h"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000011#include "llvm/Option/Arg.h"
12#include "llvm/Option/ArgList.h"
13#include "llvm/Option/Option.h"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000014#include "llvm/Support/ErrorHandling.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000015#include "llvm/Support/raw_ostream.h"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000016#include <algorithm>
Rui Ueyama8fb5a912013-08-28 20:04:31 +000017#include <cctype>
Michael J. Spencer41ee0412012-12-05 00:29:32 +000018#include <map>
19
20using namespace llvm;
21using namespace llvm::opt;
22
Rui Ueyama8fb5a912013-08-28 20:04:31 +000023namespace llvm {
24namespace opt {
Rui Ueyama7159bd92013-08-27 23:47:01 +000025
Rui Ueyama8fb5a912013-08-28 20:04:31 +000026// Ordering on Info. The ordering is *almost* case-insensitive lexicographic,
27// with an exceptions. '\0' comes at the end of the alphabet instead of the
28// beginning (thus options precede any other options which prefix them).
29static int StrCmpOptionNameIgnoreCase(const char *A, const char *B) {
30 const char *X = A, *Y = B;
31 char a = tolower(*A), b = tolower(*B);
Rui Ueyamac3779ff2013-08-28 00:02:06 +000032 while (a == b) {
33 if (a == '\0')
34 return 0;
35
Rui Ueyama8fb5a912013-08-28 20:04:31 +000036 a = tolower(*++X);
37 b = tolower(*++Y);
Rui Ueyamac3779ff2013-08-28 00:02:06 +000038 }
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;
Rui Ueyama7159bd92013-08-27 23:47:01 +000047}
48
Eli Friedman3e7dca62013-09-10 23:22:56 +000049#ifndef NDEBUG
50static int StrCmpOptionName(const char *A, const char *B) {
51 if (int N = StrCmpOptionNameIgnoreCase(A, B))
52 return N;
53 return strcmp(A, B);
54}
55
56static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
57 if (&A == &B)
58 return false;
59
60 if (int N = StrCmpOptionName(A.Name, B.Name))
61 return N < 0;
62
63 for (const char * const *APre = A.Prefixes,
64 * const *BPre = B.Prefixes;
Craig Topper2617dcc2014-04-15 06:32:26 +000065 *APre != nullptr && *BPre != nullptr; ++APre, ++BPre){
Eli Friedman3e7dca62013-09-10 23:22:56 +000066 if (int N = StrCmpOptionName(*APre, *BPre))
67 return N < 0;
68 }
69
70 // Names are the same, check that classes are in order; exactly one
71 // should be joined, and it should succeed the other.
72 assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
73 "Unexpected classes for options with same name.");
74 return B.Kind == Option::JoinedClass;
75}
76#endif
77
Michael J. Spencer41ee0412012-12-05 00:29:32 +000078// Support lower_bound between info and an option name.
79static inline bool operator<(const OptTable::Info &I, const char *Name) {
Rui Ueyama8fb5a912013-08-28 20:04:31 +000080 return StrCmpOptionNameIgnoreCase(I.Name, Name) < 0;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000081}
Michael J. Spencer41ee0412012-12-05 00:29:32 +000082}
83}
84
85OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
86
Rui Ueyama8fb5a912013-08-28 20:04:31 +000087OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos,
88 bool _IgnoreCase)
Michael J. Spencer41ee0412012-12-05 00:29:32 +000089 : OptionInfos(_OptionInfos),
90 NumOptionInfos(_NumOptionInfos),
Rui Ueyama8fb5a912013-08-28 20:04:31 +000091 IgnoreCase(_IgnoreCase),
Michael J. Spencer41ee0412012-12-05 00:29:32 +000092 TheInputOptionID(0),
93 TheUnknownOptionID(0),
94 FirstSearchableIndex(0)
95{
96 // Explicitly zero initialize the error to work around a bug in array
97 // value-initialization on MinGW with gcc 4.3.5.
98
99 // Find start of normal options.
100 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
101 unsigned Kind = getInfo(i + 1).Kind;
102 if (Kind == Option::InputClass) {
103 assert(!TheInputOptionID && "Cannot have multiple input options!");
104 TheInputOptionID = getInfo(i + 1).ID;
105 } else if (Kind == Option::UnknownClass) {
106 assert(!TheUnknownOptionID && "Cannot have multiple unknown options!");
107 TheUnknownOptionID = getInfo(i + 1).ID;
108 } else if (Kind != Option::GroupClass) {
109 FirstSearchableIndex = i;
110 break;
111 }
112 }
113 assert(FirstSearchableIndex != 0 && "No searchable options?");
114
115#ifndef NDEBUG
116 // Check that everything after the first searchable option is a
117 // regular option class.
118 for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) {
119 Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind;
120 assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
121 Kind != Option::GroupClass) &&
122 "Special options should be defined first!");
123 }
124
125 // Check that options are in order.
126 for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions(); i != e; ++i){
127 if (!(getInfo(i) < getInfo(i + 1))) {
128 getOption(i).dump();
129 getOption(i + 1).dump();
130 llvm_unreachable("Options are not in order!");
131 }
132 }
133#endif
134
135 // Build prefixes.
136 for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions() + 1;
137 i != e; ++i) {
138 if (const char *const *P = getInfo(i).Prefixes) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000139 for (; *P != nullptr; ++P) {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000140 PrefixesUnion.insert(*P);
141 }
142 }
143 }
144
145 // Build prefix chars.
146 for (llvm::StringSet<>::const_iterator I = PrefixesUnion.begin(),
147 E = PrefixesUnion.end(); I != E; ++I) {
148 StringRef Prefix = I->getKey();
149 for (StringRef::const_iterator C = Prefix.begin(), CE = Prefix.end();
150 C != CE; ++C)
151 if (std::find(PrefixChars.begin(), PrefixChars.end(), *C)
152 == PrefixChars.end())
153 PrefixChars.push_back(*C);
154 }
155}
156
157OptTable::~OptTable() {
158}
159
160const Option OptTable::getOption(OptSpecifier Opt) const {
161 unsigned id = Opt.getID();
162 if (id == 0)
Craig Topper2617dcc2014-04-15 06:32:26 +0000163 return Option(nullptr, nullptr);
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000164 assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID.");
165 return Option(&getInfo(id), this);
166}
167
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000168static bool isInput(const llvm::StringSet<> &Prefixes, StringRef Arg) {
169 if (Arg == "-")
170 return true;
171 for (llvm::StringSet<>::const_iterator I = Prefixes.begin(),
172 E = Prefixes.end(); I != E; ++I)
173 if (Arg.startswith(I->getKey()))
174 return false;
175 return true;
176}
177
178/// \returns Matched size. 0 means no match.
Rui Ueyama8fb5a912013-08-28 20:04:31 +0000179static unsigned matchOption(const OptTable::Info *I, StringRef Str,
180 bool IgnoreCase) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000181 for (const char * const *Pre = I->Prefixes; *Pre != nullptr; ++Pre) {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000182 StringRef Prefix(*Pre);
Rui Ueyama8fb5a912013-08-28 20:04:31 +0000183 if (Str.startswith(Prefix)) {
184 StringRef Rest = Str.substr(Prefix.size());
185 bool Matched = IgnoreCase
Jakub Staszakcfcfee02013-11-04 19:22:50 +0000186 ? Rest.startswith_lower(I->Name)
Rui Ueyama8fb5a912013-08-28 20:04:31 +0000187 : Rest.startswith(I->Name);
188 if (Matched)
189 return Prefix.size() + StringRef(I->Name).size();
190 }
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000191 }
192 return 0;
193}
194
Reid Klecknereadb7652013-07-19 18:04:57 +0000195Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index,
196 unsigned FlagsToInclude,
197 unsigned FlagsToExclude) const {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000198 unsigned Prev = Index;
199 const char *Str = Args.getArgString(Index);
200
201 // Anything that doesn't start with PrefixesUnion is an input, as is '-'
202 // itself.
203 if (isInput(PrefixesUnion, Str))
204 return new Arg(getOption(TheInputOptionID), Str, Index++, Str);
205
206 const Info *Start = OptionInfos + FirstSearchableIndex;
207 const Info *End = OptionInfos + getNumOptions();
208 StringRef Name = StringRef(Str).ltrim(PrefixChars);
209
210 // Search for the first next option which could be a prefix.
211 Start = std::lower_bound(Start, End, Name.data());
212
213 // Options are stored in sorted order, with '\0' at the end of the
214 // alphabet. Since the only options which can accept a string must
215 // prefix it, we iteratively search for the next option which could
216 // be a prefix.
217 //
218 // FIXME: This is searching much more than necessary, but I am
219 // blanking on the simplest way to make it fast. We can solve this
220 // problem when we move to TableGen.
221 for (; Start != End; ++Start) {
222 unsigned ArgSize = 0;
223 // Scan for first option which is a proper prefix.
224 for (; Start != End; ++Start)
Rui Ueyama8fb5a912013-08-28 20:04:31 +0000225 if ((ArgSize = matchOption(Start, Str, IgnoreCase)))
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000226 break;
227 if (Start == End)
228 break;
229
Reid Klecknereadb7652013-07-19 18:04:57 +0000230 Option Opt(Start, this);
231
232 if (FlagsToInclude && !Opt.hasFlag(FlagsToInclude))
233 continue;
234 if (Opt.hasFlag(FlagsToExclude))
235 continue;
236
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000237 // See if this option matches.
Reid Klecknereadb7652013-07-19 18:04:57 +0000238 if (Arg *A = Opt.accept(Args, Index, ArgSize))
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000239 return A;
240
241 // Otherwise, see if this argument was missing values.
242 if (Prev != Index)
Craig Topper2617dcc2014-04-15 06:32:26 +0000243 return nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000244 }
245
Reid Klecknereadb7652013-07-19 18:04:57 +0000246 // If we failed to find an option and this arg started with /, then it's
247 // probably an input path.
248 if (Str[0] == '/')
249 return new Arg(getOption(TheInputOptionID), Str, Index++, Str);
250
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000251 return new Arg(getOption(TheUnknownOptionID), Str, Index++, Str);
252}
253
Reid Klecknereadb7652013-07-19 18:04:57 +0000254InputArgList *OptTable::ParseArgs(const char *const *ArgBegin,
255 const char *const *ArgEnd,
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000256 unsigned &MissingArgIndex,
Reid Klecknereadb7652013-07-19 18:04:57 +0000257 unsigned &MissingArgCount,
258 unsigned FlagsToInclude,
259 unsigned FlagsToExclude) const {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000260 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
261
262 // FIXME: Handle '@' args (or at least error on them).
263
264 MissingArgIndex = MissingArgCount = 0;
265 unsigned Index = 0, End = ArgEnd - ArgBegin;
266 while (Index < End) {
267 // Ignore empty arguments (other things may still take them as arguments).
Hans Wennborgb8f34202013-08-02 21:20:27 +0000268 StringRef Str = Args->getArgString(Index);
269 if (Str == "") {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000270 ++Index;
271 continue;
272 }
273
274 unsigned Prev = Index;
Reid Klecknereadb7652013-07-19 18:04:57 +0000275 Arg *A = ParseOneArg(*Args, Index, FlagsToInclude, FlagsToExclude);
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000276 assert(Index > Prev && "Parser failed to consume argument.");
277
278 // Check for missing argument error.
279 if (!A) {
280 assert(Index >= End && "Unexpected parser error.");
281 assert(Index - Prev - 1 && "No missing arguments!");
282 MissingArgIndex = Prev;
283 MissingArgCount = Index - Prev - 1;
284 break;
285 }
286
287 Args->append(A);
288 }
289
290 return Args;
291}
292
293static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
294 const Option O = Opts.getOption(Id);
295 std::string Name = O.getPrefixedName();
296
297 // Add metavar, if used.
298 switch (O.getKind()) {
299 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
300 llvm_unreachable("Invalid option with help text.");
301
302 case Option::MultiArgClass:
303 llvm_unreachable("Cannot print metavar for this kind of option.");
304
305 case Option::FlagClass:
306 break;
307
308 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
Hans Wennborgd505fbf2013-08-13 21:09:50 +0000309 case Option::RemainingArgsClass:
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000310 Name += ' ';
311 // FALLTHROUGH
312 case Option::JoinedClass: case Option::CommaJoinedClass:
313 case Option::JoinedAndSeparateClass:
314 if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
315 Name += MetaVarName;
316 else
317 Name += "<value>";
318 break;
319 }
320
321 return Name;
322}
323
324static void PrintHelpOptionList(raw_ostream &OS, StringRef Title,
325 std::vector<std::pair<std::string,
326 const char*> > &OptionHelp) {
327 OS << Title << ":\n";
328
329 // Find the maximum option length.
330 unsigned OptionFieldWidth = 0;
331 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
332 // Skip titles.
333 if (!OptionHelp[i].second)
334 continue;
335
336 // Limit the amount of padding we are willing to give up for alignment.
337 unsigned Length = OptionHelp[i].first.size();
338 if (Length <= 23)
339 OptionFieldWidth = std::max(OptionFieldWidth, Length);
340 }
341
342 const unsigned InitialPad = 2;
343 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
344 const std::string &Option = OptionHelp[i].first;
345 int Pad = OptionFieldWidth - int(Option.size());
346 OS.indent(InitialPad) << Option;
347
348 // Break on long option names.
349 if (Pad < 0) {
350 OS << "\n";
351 Pad = OptionFieldWidth + InitialPad;
352 }
353 OS.indent(Pad + 1) << OptionHelp[i].second << '\n';
354 }
355}
356
357static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
358 unsigned GroupID = Opts.getOptionGroupID(Id);
359
360 // If not in a group, return the default help group.
361 if (!GroupID)
362 return "OPTIONS";
363
364 // Abuse the help text of the option groups to store the "help group"
365 // name.
366 //
367 // FIXME: Split out option groups.
368 if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
369 return GroupHelp;
370
371 // Otherwise keep looking.
372 return getOptionHelpGroup(Opts, GroupID);
373}
374
Reid Kleckner12e03322013-06-13 18:12:12 +0000375void OptTable::PrintHelp(raw_ostream &OS, const char *Name, const char *Title,
376 bool ShowHidden) const {
377 PrintHelp(OS, Name, Title, /*Include*/ 0, /*Exclude*/
378 (ShowHidden ? 0 : HelpHidden));
379}
380
381
382void OptTable::PrintHelp(raw_ostream &OS, const char *Name, const char *Title,
383 unsigned FlagsToInclude,
384 unsigned FlagsToExclude) const {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000385 OS << "OVERVIEW: " << Title << "\n";
386 OS << '\n';
387 OS << "USAGE: " << Name << " [options] <inputs>\n";
388 OS << '\n';
389
390 // Render help text into a map of group-name to a list of (option, help)
391 // pairs.
392 typedef std::map<std::string,
393 std::vector<std::pair<std::string, const char*> > > helpmap_ty;
394 helpmap_ty GroupedOptionHelp;
395
396 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
397 unsigned Id = i + 1;
398
399 // FIXME: Split out option groups.
400 if (getOptionKind(Id) == Option::GroupClass)
401 continue;
402
Reid Kleckner12e03322013-06-13 18:12:12 +0000403 unsigned Flags = getInfo(Id).Flags;
404 if (FlagsToInclude && !(Flags & FlagsToInclude))
405 continue;
406 if (Flags & FlagsToExclude)
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000407 continue;
408
409 if (const char *Text = getOptionHelpText(Id)) {
410 const char *HelpGroup = getOptionHelpGroup(*this, Id);
411 const std::string &OptName = getOptionHelpName(*this, Id);
412 GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text));
413 }
414 }
415
416 for (helpmap_ty::iterator it = GroupedOptionHelp .begin(),
417 ie = GroupedOptionHelp.end(); it != ie; ++it) {
418 if (it != GroupedOptionHelp .begin())
419 OS << "\n";
420 PrintHelpOptionList(OS, it->first, it->second);
421 }
422
423 OS.flush();
424}