blob: a1c5ecd8e605bbc51b53fb9154b86eb25c1252f7 [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
58 // Names are the same, check that classes are in order; exactly one
59 // should be joined, and it should succeed the other.
Daniel Dunbarcf51ece2009-03-25 03:06:26 +000060 assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
Daniel Dunbarb3556922009-03-23 18:41:45 +000061 "Unexpected classes for options with same name.");
62 return B.Kind == Option::JoinedClass;
63}
64
Daniel Dunbar1ce9cf02009-11-18 21:42:57 +000065// Support lower_bound between info and an option name.
66static inline bool operator<(const OptTable::Info &I, const char *Name) {
67 return StrCmpOptionName(I.Name, Name) == -1;
68}
69static inline bool operator<(const char *Name, const OptTable::Info &I) {
70 return StrCmpOptionName(Name, I.Name) == -1;
71}
72}
73}
74
Daniel Dunbarb3556922009-03-23 18:41:45 +000075//
76
Daniel Dunbar9e1f9822009-11-19 04:14:53 +000077OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
78
79//
80
Daniel Dunbara79a2b52009-11-18 20:19:36 +000081OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos)
Michael J. Spencere4151c52012-10-19 22:36:40 +000082 : OptionInfos(_OptionInfos),
83 NumOptionInfos(_NumOptionInfos),
84 TheInputOptionID(0),
85 TheUnknownOptionID(0),
86 FirstSearchableIndex(0)
Daniel Dunbara79a2b52009-11-18 20:19:36 +000087{
Daniel Dunbar47393ba2009-07-13 21:50:47 +000088 // Explicitly zero initialize the error to work around a bug in array
89 // value-initialization on MinGW with gcc 4.3.5.
Daniel Dunbar47393ba2009-07-13 21:50:47 +000090
Daniel Dunbarb3556922009-03-23 18:41:45 +000091 // Find start of normal options.
Daniel Dunbara79a2b52009-11-18 20:19:36 +000092 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
93 unsigned Kind = getInfo(i + 1).Kind;
94 if (Kind == Option::InputClass) {
Michael J. Spencere4151c52012-10-19 22:36:40 +000095 assert(!TheInputOptionID && "Cannot have multiple input options!");
96 TheInputOptionID = getInfo(i + 1).ID;
Daniel Dunbara79a2b52009-11-18 20:19:36 +000097 } else if (Kind == Option::UnknownClass) {
Michael J. Spencere4151c52012-10-19 22:36:40 +000098 assert(!TheUnknownOptionID && "Cannot have multiple unknown options!");
99 TheUnknownOptionID = getInfo(i + 1).ID;
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000100 } else if (Kind != Option::GroupClass) {
101 FirstSearchableIndex = i;
Daniel Dunbarb3556922009-03-23 18:41:45 +0000102 break;
103 }
104 }
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000105 assert(FirstSearchableIndex != 0 && "No searchable options?");
Daniel Dunbarb3556922009-03-23 18:41:45 +0000106
107#ifndef NDEBUG
108 // Check that everything after the first searchable option is a
109 // regular option class.
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000110 for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) {
111 Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind;
Daniel Dunbarb3556922009-03-23 18:41:45 +0000112 assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
113 Kind != Option::GroupClass) &&
114 "Special options should be defined first!");
115 }
116
117 // Check that options are in order.
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000118 for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) {
119 if (!(getInfo(i) < getInfo(i + 1))) {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000120 getOption(i).dump();
121 getOption(i + 1).dump();
David Blaikieb219cfc2011-09-23 05:06:16 +0000122 llvm_unreachable("Options are not in order!");
Daniel Dunbarb3556922009-03-23 18:41:45 +0000123 }
124 }
Mike Stump1eb44332009-09-09 15:08:12 +0000125#endif
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000126}
127
Mike Stump1eb44332009-09-09 15:08:12 +0000128OptTable::~OptTable() {
Michael J. Spencere4151c52012-10-19 22:36:40 +0000129}
130
131const Option OptTable::getOption(OptSpecifier Opt) const {
132 unsigned id = Opt.getID();
133 if (id == 0)
134 return Option(0, 0);
135 assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID.");
136 return Option(&getInfo(id), this);
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000137}
138
Michael J. Spencer04a42792012-08-21 18:51:17 +0000139bool OptTable::isOptionHelpHidden(OptSpecifier id) const {
140 return getInfo(id).Flags & options::HelpHidden;
141}
142
Daniel Dunbar785e7962010-06-11 22:00:17 +0000143Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index) const {
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000144 unsigned Prev = Index;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000145 const char *Str = Args.getArgString(Index);
146
Daniel Dunbara9480452009-03-12 08:44:47 +0000147 // Anything that doesn't start with '-' is an input, as is '-' itself.
148 if (Str[0] != '-' || Str[1] == '\0')
Michael J. Spencere4151c52012-10-19 22:36:40 +0000149 return new Arg(getOption(TheInputOptionID), Index++, Str);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000150
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000151 const Info *Start = OptionInfos + FirstSearchableIndex;
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000152 const Info *End = OptionInfos + getNumOptions();
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000153
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000154 // Search for the first next option which could be a prefix.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000155 Start = std::lower_bound(Start, End, Str);
156
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000157 // Options are stored in sorted order, with '\0' at the end of the
158 // alphabet. Since the only options which can accept a string must
159 // prefix it, we iteratively search for the next option which could
160 // be a prefix.
161 //
162 // FIXME: This is searching much more than necessary, but I am
163 // blanking on the simplest way to make it fast. We can solve this
164 // problem when we move to TableGen.
Benjamin Kramercbdc1a32012-10-04 10:06:38 +0000165 StringRef StrRef(Str);
Daniel Dunbar7547f742009-03-23 21:50:40 +0000166 for (; Start != End; ++Start) {
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000167 // Scan for first option which is a proper prefix.
168 for (; Start != End; ++Start)
Benjamin Kramercbdc1a32012-10-04 10:06:38 +0000169 if (StrRef.startswith(Start->Name))
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000170 break;
171 if (Start == End)
Daniel Dunbar7547f742009-03-23 21:50:40 +0000172 break;
173
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000174 // See if this option matches.
Michael J. Spencere4151c52012-10-19 22:36:40 +0000175 if (Arg *A = getOption(Start - OptionInfos + 1).accept(Args, Index))
Daniel Dunbar7547f742009-03-23 21:50:40 +0000176 return A;
177
178 // Otherwise, see if this argument was missing values.
179 if (Prev != Index)
180 return 0;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000181 }
182
Michael J. Spencere4151c52012-10-19 22:36:40 +0000183 return new Arg(getOption(TheUnknownOptionID), Index++, Str);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000184}
Daniel Dunbar847abaa2009-11-19 06:35:06 +0000185
Axel Naumann9d520c52010-10-11 09:18:43 +0000186InputArgList *OptTable::ParseArgs(const char* const *ArgBegin,
187 const char* const *ArgEnd,
Daniel Dunbar847abaa2009-11-19 06:35:06 +0000188 unsigned &MissingArgIndex,
189 unsigned &MissingArgCount) const {
190 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
191
192 // FIXME: Handle '@' args (or at least error on them).
193
194 MissingArgIndex = MissingArgCount = 0;
195 unsigned Index = 0, End = ArgEnd - ArgBegin;
196 while (Index < End) {
197 // Ignore empty arguments (other things may still take them as arguments).
198 if (Args->getArgString(Index)[0] == '\0') {
199 ++Index;
200 continue;
201 }
202
203 unsigned Prev = Index;
204 Arg *A = ParseOneArg(*Args, Index);
205 assert(Index > Prev && "Parser failed to consume argument.");
206
207 // Check for missing argument error.
208 if (!A) {
209 assert(Index >= End && "Unexpected parser error.");
210 assert(Index - Prev - 1 && "No missing arguments!");
211 MissingArgIndex = Prev;
212 MissingArgCount = Index - Prev - 1;
213 break;
214 }
215
216 Args->append(A);
217 }
218
219 return Args;
220}
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000221
222static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
223 std::string Name = Opts.getOptionName(Id);
224
225 // Add metavar, if used.
226 switch (Opts.getOptionKind(Id)) {
227 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000228 llvm_unreachable("Invalid option with help text.");
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000229
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000230 case Option::MultiArgClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000231 llvm_unreachable("Cannot print metavar for this kind of option.");
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000232
233 case Option::FlagClass:
234 break;
235
236 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
237 Name += ' ';
238 // FALLTHROUGH
239 case Option::JoinedClass: case Option::CommaJoinedClass:
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000240 case Option::JoinedAndSeparateClass:
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000241 if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
242 Name += MetaVarName;
243 else
244 Name += "<value>";
245 break;
246 }
247
248 return Name;
249}
250
Chris Lattner5f9e2722011-07-23 10:55:15 +0000251static void PrintHelpOptionList(raw_ostream &OS, StringRef Title,
Daniel Dunbar2658f052009-12-04 21:08:40 +0000252 std::vector<std::pair<std::string,
253 const char*> > &OptionHelp) {
254 OS << Title << ":\n";
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000255
256 // Find the maximum option length.
257 unsigned OptionFieldWidth = 0;
258 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
259 // Skip titles.
260 if (!OptionHelp[i].second)
261 continue;
262
263 // Limit the amount of padding we are willing to give up for alignment.
264 unsigned Length = OptionHelp[i].first.size();
265 if (Length <= 23)
266 OptionFieldWidth = std::max(OptionFieldWidth, Length);
267 }
268
269 const unsigned InitialPad = 2;
270 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
271 const std::string &Option = OptionHelp[i].first;
272 int Pad = OptionFieldWidth - int(Option.size());
273 OS.indent(InitialPad) << Option;
274
275 // Break on long option names.
276 if (Pad < 0) {
277 OS << "\n";
278 Pad = OptionFieldWidth + InitialPad;
279 }
280 OS.indent(Pad + 1) << OptionHelp[i].second << '\n';
281 }
Daniel Dunbar2658f052009-12-04 21:08:40 +0000282}
283
284static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
285 unsigned GroupID = Opts.getOptionGroupID(Id);
286
287 // If not in a group, return the default help group.
288 if (!GroupID)
289 return "OPTIONS";
290
291 // Abuse the help text of the option groups to store the "help group"
292 // name.
293 //
294 // FIXME: Split out option groups.
295 if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
296 return GroupHelp;
297
298 // Otherwise keep looking.
299 return getOptionHelpGroup(Opts, GroupID);
300}
301
Chris Lattner5f9e2722011-07-23 10:55:15 +0000302void OptTable::PrintHelp(raw_ostream &OS, const char *Name,
Daniel Dunbar2658f052009-12-04 21:08:40 +0000303 const char *Title, bool ShowHidden) const {
304 OS << "OVERVIEW: " << Title << "\n";
305 OS << '\n';
306 OS << "USAGE: " << Name << " [options] <inputs>\n";
307 OS << '\n';
308
309 // Render help text into a map of group-name to a list of (option, help)
310 // pairs.
311 typedef std::map<std::string,
312 std::vector<std::pair<std::string, const char*> > > helpmap_ty;
313 helpmap_ty GroupedOptionHelp;
314
315 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
316 unsigned Id = i + 1;
317
318 // FIXME: Split out option groups.
319 if (getOptionKind(Id) == Option::GroupClass)
320 continue;
321
322 if (!ShowHidden && isOptionHelpHidden(Id))
323 continue;
324
325 if (const char *Text = getOptionHelpText(Id)) {
326 const char *HelpGroup = getOptionHelpGroup(*this, Id);
327 const std::string &OptName = getOptionHelpName(*this, Id);
328 GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text));
329 }
330 }
331
332 for (helpmap_ty::iterator it = GroupedOptionHelp .begin(),
333 ie = GroupedOptionHelp.end(); it != ie; ++it) {
334 if (it != GroupedOptionHelp .begin())
335 OS << "\n";
336 PrintHelpOptionList(OS, it->first, it->second);
337 }
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000338
339 OS.flush();
340}