blob: e108106fa7e97996f6159b4dd31a41ceee98964d [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"
Daniel Dunbar60e107f2009-12-03 07:01:38 +000014#include "llvm/Support/raw_ostream.h"
David Blaikie548f6c82011-09-23 05:57:42 +000015#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar7547f742009-03-23 21:50:40 +000016#include <algorithm>
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;
Chris Lattner5f9e2722011-07-23 10:55:15 +000020using namespace clang;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +000021
Daniel Dunbarb3556922009-03-23 18:41:45 +000022// Ordering on Info. The ordering is *almost* lexicographic, with two
23// exceptions. First, '\0' comes at the end of the alphabet instead of
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000024// the beginning (thus options precede any other options which prefix
Daniel Dunbarb3556922009-03-23 18:41:45 +000025// them). Second, for options with the same name, the less permissive
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000026// version should come first; a Flag option should precede a Joined
Daniel Dunbarb3556922009-03-23 18:41:45 +000027// option, for example.
28
29static int StrCmpOptionName(const char *A, const char *B) {
30 char a = *A, b = *B;
31 while (a == b) {
32 if (a == '\0')
33 return 0;
34
35 a = *++A;
36 b = *++B;
37 }
38
39 if (a == '\0') // A is a prefix of B.
40 return 1;
41 if (b == '\0') // B is a prefix of A.
42 return -1;
43
44 // Otherwise lexicographic.
45 return (a < b) ? -1 : 1;
46}
47
Daniel Dunbar1ce9cf02009-11-18 21:42:57 +000048namespace clang {
49namespace driver {
Daniel Dunbara79a2b52009-11-18 20:19:36 +000050static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
Daniel Dunbarb3556922009-03-23 18:41:45 +000051 if (&A == &B)
52 return false;
53
54 if (int N = StrCmpOptionName(A.Name, B.Name))
55 return N == -1;
56
57 // Names are the same, check that classes are in order; exactly one
58 // should be joined, and it should succeed the other.
Daniel Dunbarcf51ece2009-03-25 03:06:26 +000059 assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
Daniel Dunbarb3556922009-03-23 18:41:45 +000060 "Unexpected classes for options with same name.");
61 return B.Kind == Option::JoinedClass;
62}
63
Daniel Dunbar1ce9cf02009-11-18 21:42:57 +000064// Support lower_bound between info and an option name.
65static inline bool operator<(const OptTable::Info &I, const char *Name) {
66 return StrCmpOptionName(I.Name, Name) == -1;
67}
68static inline bool operator<(const char *Name, const OptTable::Info &I) {
69 return StrCmpOptionName(Name, I.Name) == -1;
70}
71}
72}
73
Daniel Dunbarb3556922009-03-23 18:41:45 +000074//
75
Daniel Dunbar9e1f9822009-11-19 04:14:53 +000076OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
77
78//
79
Daniel Dunbara79a2b52009-11-18 20:19:36 +000080OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos)
81 : OptionInfos(_OptionInfos), NumOptionInfos(_NumOptionInfos),
Argyrios Kyrtzidisf3852332012-10-18 22:42:31 +000082 Options(new Option*[NumOptionInfos]),
83 TheInputOption(0), TheUnknownOption(0), FirstSearchableIndex(0)
Daniel Dunbara79a2b52009-11-18 20:19:36 +000084{
Daniel Dunbar47393ba2009-07-13 21:50:47 +000085 // Explicitly zero initialize the error to work around a bug in array
86 // value-initialization on MinGW with gcc 4.3.5.
Argyrios Kyrtzidisf3852332012-10-18 22:42:31 +000087 memset(Options, 0, sizeof(*Options) * NumOptionInfos);
Daniel Dunbar47393ba2009-07-13 21:50:47 +000088
Daniel Dunbarb3556922009-03-23 18:41:45 +000089 // Find start of normal options.
Daniel Dunbara79a2b52009-11-18 20:19:36 +000090 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
91 unsigned Kind = getInfo(i + 1).Kind;
92 if (Kind == Option::InputClass) {
Argyrios Kyrtzidisf3852332012-10-18 22:42:31 +000093 assert(!TheInputOption && "Cannot have multiple input options!");
94 TheInputOption = getOption(i + 1);
Daniel Dunbara79a2b52009-11-18 20:19:36 +000095 } else if (Kind == Option::UnknownClass) {
Argyrios Kyrtzidisf3852332012-10-18 22:42:31 +000096 assert(!TheUnknownOption && "Cannot have multiple input options!");
97 TheUnknownOption = getOption(i + 1);
Daniel Dunbara79a2b52009-11-18 20:19:36 +000098 } else if (Kind != Option::GroupClass) {
99 FirstSearchableIndex = i;
Daniel Dunbarb3556922009-03-23 18:41:45 +0000100 break;
101 }
102 }
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000103 assert(FirstSearchableIndex != 0 && "No searchable options?");
Daniel Dunbarb3556922009-03-23 18:41:45 +0000104
105#ifndef NDEBUG
106 // Check that everything after the first searchable option is a
107 // regular option class.
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000108 for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) {
109 Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind;
Daniel Dunbarb3556922009-03-23 18:41:45 +0000110 assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
111 Kind != Option::GroupClass) &&
112 "Special options should be defined first!");
113 }
114
115 // Check that options are in order.
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000116 for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) {
117 if (!(getInfo(i) < getInfo(i + 1))) {
Argyrios Kyrtzidisf3852332012-10-18 22:42:31 +0000118 getOption(i)->dump();
119 getOption(i + 1)->dump();
David Blaikieb219cfc2011-09-23 05:06:16 +0000120 llvm_unreachable("Options are not in order!");
Daniel Dunbarb3556922009-03-23 18:41:45 +0000121 }
122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123#endif
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000124}
125
Mike Stump1eb44332009-09-09 15:08:12 +0000126OptTable::~OptTable() {
Argyrios Kyrtzidisf3852332012-10-18 22:42:31 +0000127 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
128 delete Options[i];
129 delete[] Options;
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000130}
131
Michael J. Spencer04a42792012-08-21 18:51:17 +0000132bool OptTable::isOptionHelpHidden(OptSpecifier id) const {
133 return getInfo(id).Flags & options::HelpHidden;
134}
135
Argyrios Kyrtzidisf3852332012-10-18 22:42:31 +0000136Option *OptTable::CreateOption(unsigned id) const {
137 return new Option(&getInfo(id), this);
138}
139
Daniel Dunbar785e7962010-06-11 22:00:17 +0000140Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index) const {
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000141 unsigned Prev = Index;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000142 const char *Str = Args.getArgString(Index);
143
Daniel Dunbara9480452009-03-12 08:44:47 +0000144 // Anything that doesn't start with '-' is an input, as is '-' itself.
145 if (Str[0] != '-' || Str[1] == '\0')
Argyrios Kyrtzidisf3852332012-10-18 22:42:31 +0000146 return new Arg(TheInputOption, Index++, Str);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000147
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000148 const Info *Start = OptionInfos + FirstSearchableIndex;
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000149 const Info *End = OptionInfos + getNumOptions();
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000150
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000151 // Search for the first next option which could be a prefix.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000152 Start = std::lower_bound(Start, End, Str);
153
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000154 // Options are stored in sorted order, with '\0' at the end of the
155 // alphabet. Since the only options which can accept a string must
156 // prefix it, we iteratively search for the next option which could
157 // be a prefix.
158 //
159 // FIXME: This is searching much more than necessary, but I am
160 // blanking on the simplest way to make it fast. We can solve this
161 // problem when we move to TableGen.
Benjamin Kramercbdc1a32012-10-04 10:06:38 +0000162 StringRef StrRef(Str);
Daniel Dunbar7547f742009-03-23 21:50:40 +0000163 for (; Start != End; ++Start) {
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000164 // Scan for first option which is a proper prefix.
165 for (; Start != End; ++Start)
Benjamin Kramercbdc1a32012-10-04 10:06:38 +0000166 if (StrRef.startswith(Start->Name))
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000167 break;
168 if (Start == End)
Daniel Dunbar7547f742009-03-23 21:50:40 +0000169 break;
170
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000171 // See if this option matches.
Argyrios Kyrtzidisf3852332012-10-18 22:42:31 +0000172 if (Arg *A = getOption(Start - OptionInfos + 1)->accept(Args, Index))
Daniel Dunbar7547f742009-03-23 21:50:40 +0000173 return A;
174
175 // Otherwise, see if this argument was missing values.
176 if (Prev != Index)
177 return 0;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000178 }
179
Argyrios Kyrtzidisf3852332012-10-18 22:42:31 +0000180 return new Arg(TheUnknownOption, Index++, Str);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000181}
Daniel Dunbar847abaa2009-11-19 06:35:06 +0000182
Axel Naumann9d520c52010-10-11 09:18:43 +0000183InputArgList *OptTable::ParseArgs(const char* const *ArgBegin,
184 const char* const *ArgEnd,
Daniel Dunbar847abaa2009-11-19 06:35:06 +0000185 unsigned &MissingArgIndex,
186 unsigned &MissingArgCount) const {
187 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
188
189 // FIXME: Handle '@' args (or at least error on them).
190
191 MissingArgIndex = MissingArgCount = 0;
192 unsigned Index = 0, End = ArgEnd - ArgBegin;
193 while (Index < End) {
194 // Ignore empty arguments (other things may still take them as arguments).
195 if (Args->getArgString(Index)[0] == '\0') {
196 ++Index;
197 continue;
198 }
199
200 unsigned Prev = Index;
201 Arg *A = ParseOneArg(*Args, Index);
202 assert(Index > Prev && "Parser failed to consume argument.");
203
204 // Check for missing argument error.
205 if (!A) {
206 assert(Index >= End && "Unexpected parser error.");
207 assert(Index - Prev - 1 && "No missing arguments!");
208 MissingArgIndex = Prev;
209 MissingArgCount = Index - Prev - 1;
210 break;
211 }
212
213 Args->append(A);
214 }
215
216 return Args;
217}
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000218
219static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
220 std::string Name = Opts.getOptionName(Id);
221
222 // Add metavar, if used.
223 switch (Opts.getOptionKind(Id)) {
224 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000225 llvm_unreachable("Invalid option with help text.");
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000226
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000227 case Option::MultiArgClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000228 llvm_unreachable("Cannot print metavar for this kind of option.");
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000229
230 case Option::FlagClass:
231 break;
232
233 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
234 Name += ' ';
235 // FALLTHROUGH
236 case Option::JoinedClass: case Option::CommaJoinedClass:
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000237 case Option::JoinedAndSeparateClass:
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000238 if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
239 Name += MetaVarName;
240 else
241 Name += "<value>";
242 break;
243 }
244
245 return Name;
246}
247
Chris Lattner5f9e2722011-07-23 10:55:15 +0000248static void PrintHelpOptionList(raw_ostream &OS, StringRef Title,
Daniel Dunbar2658f052009-12-04 21:08:40 +0000249 std::vector<std::pair<std::string,
250 const char*> > &OptionHelp) {
251 OS << Title << ":\n";
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000252
253 // Find the maximum option length.
254 unsigned OptionFieldWidth = 0;
255 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
256 // Skip titles.
257 if (!OptionHelp[i].second)
258 continue;
259
260 // Limit the amount of padding we are willing to give up for alignment.
261 unsigned Length = OptionHelp[i].first.size();
262 if (Length <= 23)
263 OptionFieldWidth = std::max(OptionFieldWidth, Length);
264 }
265
266 const unsigned InitialPad = 2;
267 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
268 const std::string &Option = OptionHelp[i].first;
269 int Pad = OptionFieldWidth - int(Option.size());
270 OS.indent(InitialPad) << Option;
271
272 // Break on long option names.
273 if (Pad < 0) {
274 OS << "\n";
275 Pad = OptionFieldWidth + InitialPad;
276 }
277 OS.indent(Pad + 1) << OptionHelp[i].second << '\n';
278 }
Daniel Dunbar2658f052009-12-04 21:08:40 +0000279}
280
281static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
282 unsigned GroupID = Opts.getOptionGroupID(Id);
283
284 // If not in a group, return the default help group.
285 if (!GroupID)
286 return "OPTIONS";
287
288 // Abuse the help text of the option groups to store the "help group"
289 // name.
290 //
291 // FIXME: Split out option groups.
292 if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
293 return GroupHelp;
294
295 // Otherwise keep looking.
296 return getOptionHelpGroup(Opts, GroupID);
297}
298
Chris Lattner5f9e2722011-07-23 10:55:15 +0000299void OptTable::PrintHelp(raw_ostream &OS, const char *Name,
Daniel Dunbar2658f052009-12-04 21:08:40 +0000300 const char *Title, bool ShowHidden) const {
301 OS << "OVERVIEW: " << Title << "\n";
302 OS << '\n';
303 OS << "USAGE: " << Name << " [options] <inputs>\n";
304 OS << '\n';
305
306 // Render help text into a map of group-name to a list of (option, help)
307 // pairs.
308 typedef std::map<std::string,
309 std::vector<std::pair<std::string, const char*> > > helpmap_ty;
310 helpmap_ty GroupedOptionHelp;
311
312 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
313 unsigned Id = i + 1;
314
315 // FIXME: Split out option groups.
316 if (getOptionKind(Id) == Option::GroupClass)
317 continue;
318
319 if (!ShowHidden && isOptionHelpHidden(Id))
320 continue;
321
322 if (const char *Text = getOptionHelpText(Id)) {
323 const char *HelpGroup = getOptionHelpGroup(*this, Id);
324 const std::string &OptName = getOptionHelpName(*this, Id);
325 GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text));
326 }
327 }
328
329 for (helpmap_ty::iterator it = GroupedOptionHelp .begin(),
330 ie = GroupedOptionHelp.end(); it != ie; ++it) {
331 if (it != GroupedOptionHelp .begin())
332 OS << "\n";
333 PrintHelpOptionList(OS, it->first, it->second);
334 }
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000335
336 OS.flush();
337}