blob: 433c3869b5ce116f63e3c3298f4fe4305b61df27 [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. Spencer97b8fd92012-10-18 20:33:42 +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)
82 : OptionInfos(_OptionInfos), NumOptionInfos(_NumOptionInfos),
Michael J. Spencer97b8fd92012-10-18 20:33:42 +000083 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.
Daniel Dunbar47393ba2009-07-13 21:50:47 +000087
Daniel Dunbarb3556922009-03-23 18:41:45 +000088 // Find start of normal options.
Daniel Dunbara79a2b52009-11-18 20:19:36 +000089 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
90 unsigned Kind = getInfo(i + 1).Kind;
91 if (Kind == Option::InputClass) {
Daniel Dunbara79a2b52009-11-18 20:19:36 +000092 } else if (Kind == Option::UnknownClass) {
Daniel Dunbara79a2b52009-11-18 20:19:36 +000093 } else if (Kind != Option::GroupClass) {
94 FirstSearchableIndex = i;
Daniel Dunbarb3556922009-03-23 18:41:45 +000095 break;
96 }
97 }
Daniel Dunbara79a2b52009-11-18 20:19:36 +000098 assert(FirstSearchableIndex != 0 && "No searchable options?");
Daniel Dunbarb3556922009-03-23 18:41:45 +000099
100#ifndef NDEBUG
101 // Check that everything after the first searchable option is a
102 // regular option class.
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000103 for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) {
104 Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind;
Daniel Dunbarb3556922009-03-23 18:41:45 +0000105 assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
106 Kind != Option::GroupClass) &&
107 "Special options should be defined first!");
108 }
109
110 // Check that options are in order.
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000111 for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) {
112 if (!(getInfo(i) < getInfo(i + 1))) {
Michael J. Spencer97b8fd92012-10-18 20:33:42 +0000113 getOption(i).dump();
114 getOption(i + 1).dump();
David Blaikieb219cfc2011-09-23 05:06:16 +0000115 llvm_unreachable("Options are not in order!");
Daniel Dunbarb3556922009-03-23 18:41:45 +0000116 }
117 }
Mike Stump1eb44332009-09-09 15:08:12 +0000118#endif
Daniel Dunbar2c6f6f32009-03-04 08:33:23 +0000119}
120
Mike Stump1eb44332009-09-09 15:08:12 +0000121OptTable::~OptTable() {
Michael J. Spencer97b8fd92012-10-18 20:33:42 +0000122}
123
124const Option OptTable::getOption(OptSpecifier Opt) const {
125 unsigned id = Opt.getID();
126 if (id == 0)
127 return Option(0, 0);
128 assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID.");
129 return Option(&getInfo(id), this);
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
Daniel Dunbar785e7962010-06-11 22:00:17 +0000136Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index) const {
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000137 unsigned Prev = Index;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000138 const char *Str = Args.getArgString(Index);
139
Daniel Dunbara9480452009-03-12 08:44:47 +0000140 // Anything that doesn't start with '-' is an input, as is '-' itself.
141 if (Str[0] != '-' || Str[1] == '\0')
Michael J. Spencer97b8fd92012-10-18 20:33:42 +0000142 return new Arg(getOption(options::OPT_INPUT), Index++, Str);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000143
Daniel Dunbara79a2b52009-11-18 20:19:36 +0000144 const Info *Start = OptionInfos + FirstSearchableIndex;
Daniel Dunbara0289fd2009-11-19 04:25:06 +0000145 const Info *End = OptionInfos + getNumOptions();
Daniel Dunbarb0c4df52009-03-22 23:26:43 +0000146
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000147 // Search for the first next option which could be a prefix.
Daniel Dunbar7547f742009-03-23 21:50:40 +0000148 Start = std::lower_bound(Start, End, Str);
149
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000150 // Options are stored in sorted order, with '\0' at the end of the
151 // alphabet. Since the only options which can accept a string must
152 // prefix it, we iteratively search for the next option which could
153 // be a prefix.
154 //
155 // FIXME: This is searching much more than necessary, but I am
156 // blanking on the simplest way to make it fast. We can solve this
157 // problem when we move to TableGen.
Benjamin Kramercbdc1a32012-10-04 10:06:38 +0000158 StringRef StrRef(Str);
Daniel Dunbar7547f742009-03-23 21:50:40 +0000159 for (; Start != End; ++Start) {
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000160 // Scan for first option which is a proper prefix.
161 for (; Start != End; ++Start)
Benjamin Kramercbdc1a32012-10-04 10:06:38 +0000162 if (StrRef.startswith(Start->Name))
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000163 break;
164 if (Start == End)
Daniel Dunbar7547f742009-03-23 21:50:40 +0000165 break;
166
Daniel Dunbar4ae24e72009-04-07 18:21:47 +0000167 // See if this option matches.
Michael J. Spencer97b8fd92012-10-18 20:33:42 +0000168 if (Arg *A = getOption(Start - OptionInfos + 1).accept(Args, Index))
Daniel Dunbar7547f742009-03-23 21:50:40 +0000169 return A;
170
171 // Otherwise, see if this argument was missing values.
172 if (Prev != Index)
173 return 0;
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000174 }
175
Michael J. Spencer97b8fd92012-10-18 20:33:42 +0000176 return new Arg(getOption(options::OPT_UNKNOWN), Index++, Str);
Daniel Dunbar70a0dbb2009-03-04 22:41:37 +0000177}
Daniel Dunbar847abaa2009-11-19 06:35:06 +0000178
Axel Naumann9d520c52010-10-11 09:18:43 +0000179InputArgList *OptTable::ParseArgs(const char* const *ArgBegin,
180 const char* const *ArgEnd,
Daniel Dunbar847abaa2009-11-19 06:35:06 +0000181 unsigned &MissingArgIndex,
182 unsigned &MissingArgCount) const {
183 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
184
185 // FIXME: Handle '@' args (or at least error on them).
186
187 MissingArgIndex = MissingArgCount = 0;
188 unsigned Index = 0, End = ArgEnd - ArgBegin;
189 while (Index < End) {
190 // Ignore empty arguments (other things may still take them as arguments).
191 if (Args->getArgString(Index)[0] == '\0') {
192 ++Index;
193 continue;
194 }
195
196 unsigned Prev = Index;
197 Arg *A = ParseOneArg(*Args, Index);
198 assert(Index > Prev && "Parser failed to consume argument.");
199
200 // Check for missing argument error.
201 if (!A) {
202 assert(Index >= End && "Unexpected parser error.");
203 assert(Index - Prev - 1 && "No missing arguments!");
204 MissingArgIndex = Prev;
205 MissingArgCount = Index - Prev - 1;
206 break;
207 }
208
209 Args->append(A);
210 }
211
212 return Args;
213}
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000214
215static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
216 std::string Name = Opts.getOptionName(Id);
217
218 // Add metavar, if used.
219 switch (Opts.getOptionKind(Id)) {
220 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000221 llvm_unreachable("Invalid option with help text.");
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000222
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000223 case Option::MultiArgClass:
David Blaikieb219cfc2011-09-23 05:06:16 +0000224 llvm_unreachable("Cannot print metavar for this kind of option.");
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000225
226 case Option::FlagClass:
227 break;
228
229 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
230 Name += ' ';
231 // FALLTHROUGH
232 case Option::JoinedClass: case Option::CommaJoinedClass:
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000233 case Option::JoinedAndSeparateClass:
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000234 if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
235 Name += MetaVarName;
236 else
237 Name += "<value>";
238 break;
239 }
240
241 return Name;
242}
243
Chris Lattner5f9e2722011-07-23 10:55:15 +0000244static void PrintHelpOptionList(raw_ostream &OS, StringRef Title,
Daniel Dunbar2658f052009-12-04 21:08:40 +0000245 std::vector<std::pair<std::string,
246 const char*> > &OptionHelp) {
247 OS << Title << ":\n";
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000248
249 // Find the maximum option length.
250 unsigned OptionFieldWidth = 0;
251 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
252 // Skip titles.
253 if (!OptionHelp[i].second)
254 continue;
255
256 // Limit the amount of padding we are willing to give up for alignment.
257 unsigned Length = OptionHelp[i].first.size();
258 if (Length <= 23)
259 OptionFieldWidth = std::max(OptionFieldWidth, Length);
260 }
261
262 const unsigned InitialPad = 2;
263 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
264 const std::string &Option = OptionHelp[i].first;
265 int Pad = OptionFieldWidth - int(Option.size());
266 OS.indent(InitialPad) << Option;
267
268 // Break on long option names.
269 if (Pad < 0) {
270 OS << "\n";
271 Pad = OptionFieldWidth + InitialPad;
272 }
273 OS.indent(Pad + 1) << OptionHelp[i].second << '\n';
274 }
Daniel Dunbar2658f052009-12-04 21:08:40 +0000275}
276
277static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
278 unsigned GroupID = Opts.getOptionGroupID(Id);
279
280 // If not in a group, return the default help group.
281 if (!GroupID)
282 return "OPTIONS";
283
284 // Abuse the help text of the option groups to store the "help group"
285 // name.
286 //
287 // FIXME: Split out option groups.
288 if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
289 return GroupHelp;
290
291 // Otherwise keep looking.
292 return getOptionHelpGroup(Opts, GroupID);
293}
294
Chris Lattner5f9e2722011-07-23 10:55:15 +0000295void OptTable::PrintHelp(raw_ostream &OS, const char *Name,
Daniel Dunbar2658f052009-12-04 21:08:40 +0000296 const char *Title, bool ShowHidden) const {
297 OS << "OVERVIEW: " << Title << "\n";
298 OS << '\n';
299 OS << "USAGE: " << Name << " [options] <inputs>\n";
300 OS << '\n';
301
302 // Render help text into a map of group-name to a list of (option, help)
303 // pairs.
304 typedef std::map<std::string,
305 std::vector<std::pair<std::string, const char*> > > helpmap_ty;
306 helpmap_ty GroupedOptionHelp;
307
308 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
309 unsigned Id = i + 1;
310
311 // FIXME: Split out option groups.
312 if (getOptionKind(Id) == Option::GroupClass)
313 continue;
314
315 if (!ShowHidden && isOptionHelpHidden(Id))
316 continue;
317
318 if (const char *Text = getOptionHelpText(Id)) {
319 const char *HelpGroup = getOptionHelpGroup(*this, Id);
320 const std::string &OptName = getOptionHelpName(*this, Id);
321 GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text));
322 }
323 }
324
325 for (helpmap_ty::iterator it = GroupedOptionHelp .begin(),
326 ie = GroupedOptionHelp.end(); it != ie; ++it) {
327 if (it != GroupedOptionHelp .begin())
328 OS << "\n";
329 PrintHelpOptionList(OS, it->first, it->second);
330 }
Daniel Dunbar60e107f2009-12-03 07:01:38 +0000331
332 OS.flush();
333}