1 //===--- OptTable.cpp - Option Table Implementation -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Driver/OptTable.h"
11 #include "clang/Driver/Arg.h"
12 #include "clang/Driver/ArgList.h"
13 #include "clang/Driver/Option.h"
14 #include "clang/Driver/Options.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/raw_ostream.h"
19 using namespace clang::driver;
20 using namespace clang::driver::options;
21 using namespace clang;
23 // Ordering on Info. The ordering is *almost* lexicographic, with two
24 // exceptions. First, '\0' comes at the end of the alphabet instead of
25 // the beginning (thus options precede any other options which prefix
26 // them). Second, for options with the same name, the less permissive
27 // version should come first; a Flag option should precede a Joined
28 // option, for example.
30 static int StrCmpOptionName(const char *A, const char *B) {
40 if (a == '\0') // A is a prefix of B.
42 if (b == '\0') // B is a prefix of A.
45 // Otherwise lexicographic.
46 return (a < b) ? -1 : 1;
51 static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
55 if (int N = StrCmpOptionName(A.Name, B.Name))
58 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))
65 // Names are the same, check that classes are in order; exactly one
66 // should be joined, and it should succeed the other.
67 assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
68 "Unexpected classes for options with same name.");
69 return B.Kind == Option::JoinedClass;
72 // Support lower_bound between info and an option name.
73 static inline bool operator<(const OptTable::Info &I, const char *Name) {
74 return StrCmpOptionName(I.Name, Name) == -1;
76 static inline bool operator<(const char *Name, const OptTable::Info &I) {
77 return StrCmpOptionName(Name, I.Name) == -1;
84 OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
88 OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos)
89 : OptionInfos(_OptionInfos),
90 NumOptionInfos(_NumOptionInfos),
92 TheUnknownOptionID(0),
93 FirstSearchableIndex(0)
95 // Explicitly zero initialize the error to work around a bug in array
96 // value-initialization on MinGW with gcc 4.3.5.
98 // Find start of normal options.
99 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
100 unsigned Kind = getInfo(i + 1).Kind;
101 if (Kind == Option::InputClass) {
102 assert(!TheInputOptionID && "Cannot have multiple input options!");
103 TheInputOptionID = getInfo(i + 1).ID;
104 } else if (Kind == Option::UnknownClass) {
105 assert(!TheUnknownOptionID && "Cannot have multiple unknown options!");
106 TheUnknownOptionID = getInfo(i + 1).ID;
107 } else if (Kind != Option::GroupClass) {
108 FirstSearchableIndex = i;
112 assert(FirstSearchableIndex != 0 && "No searchable options?");
115 // Check that everything after the first searchable option is a
116 // regular option class.
117 for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) {
118 Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind;
119 assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
120 Kind != Option::GroupClass) &&
121 "Special options should be defined first!");
124 // Check that options are in order.
125 for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) {
126 if (!(getInfo(i) < getInfo(i + 1))) {
128 getOption(i + 1).dump();
129 llvm_unreachable("Options are not in order!");
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);
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();
149 if (std::find(PrefixChars.begin(), PrefixChars.end(), *C)
150 == PrefixChars.end())
151 PrefixChars.push_back(*C);
155 OptTable::~OptTable() {
158 const Option OptTable::getOption(OptSpecifier Opt) const {
159 unsigned id = Opt.getID();
162 assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID.");
163 return Option(&getInfo(id), this);
166 static bool isInput(const llvm::StringSet<> &Prefixes, StringRef Arg) {
169 for (llvm::StringSet<>::const_iterator I = Prefixes.begin(),
170 E = Prefixes.end(); I != E; ++I)
171 if (Arg.startswith(I->getKey()))
176 /// \returns Matched size. 0 means no match.
177 static 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();
186 Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index) const {
187 unsigned Prev = Index;
188 const char *Str = Args.getArgString(Index);
190 // Anything that doesn't start with PrefixesUnion is an input, as is '-'
192 if (isInput(PrefixesUnion, Str))
193 return new Arg(getOption(TheInputOptionID), Str, Index++, Str);
195 const Info *Start = OptionInfos + FirstSearchableIndex;
196 const Info *End = OptionInfos + getNumOptions();
197 StringRef Name = StringRef(Str).ltrim(PrefixChars);
199 // Search for the first next option which could be a prefix.
200 Start = std::lower_bound(Start, End, Name.data());
202 // 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
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.
210 for (; Start != End; ++Start) {
211 unsigned ArgSize = 0;
212 // Scan for first option which is a proper prefix.
213 for (; Start != End; ++Start)
214 if ((ArgSize = matchOption(Start, Str)))
219 // See if this option matches.
220 if (Arg *A = Option(Start, this).accept(Args, Index, ArgSize))
223 // Otherwise, see if this argument was missing values.
228 return new Arg(getOption(TheUnknownOptionID), Str, Index++, Str);
231 InputArgList *OptTable::ParseArgs(const char* const *ArgBegin,
232 const char* const *ArgEnd,
233 unsigned &MissingArgIndex,
234 unsigned &MissingArgCount) const {
235 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
237 // FIXME: Handle '@' args (or at least error on them).
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') {
248 unsigned Prev = Index;
249 Arg *A = ParseOneArg(*Args, Index);
250 assert(Index > Prev && "Parser failed to consume argument.");
252 // Check for missing argument error.
254 assert(Index >= End && "Unexpected parser error.");
255 assert(Index - Prev - 1 && "No missing arguments!");
256 MissingArgIndex = Prev;
257 MissingArgCount = Index - Prev - 1;
267 static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
268 const Option O = Opts.getOption(Id);
269 std::string Name = O.getPrefixedName();
271 // Add metavar, if used.
272 switch (O.getKind()) {
273 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
274 llvm_unreachable("Invalid option with help text.");
276 case Option::MultiArgClass:
277 llvm_unreachable("Cannot print metavar for this kind of option.");
279 case Option::FlagClass:
282 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
285 case Option::JoinedClass: case Option::CommaJoinedClass:
286 case Option::JoinedAndSeparateClass:
287 if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
297 static void PrintHelpOptionList(raw_ostream &OS, StringRef Title,
298 std::vector<std::pair<std::string,
299 const char*> > &OptionHelp) {
300 OS << Title << ":\n";
302 // Find the maximum option length.
303 unsigned OptionFieldWidth = 0;
304 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
306 if (!OptionHelp[i].second)
309 // Limit the amount of padding we are willing to give up for alignment.
310 unsigned Length = OptionHelp[i].first.size();
312 OptionFieldWidth = std::max(OptionFieldWidth, Length);
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;
321 // Break on long option names.
324 Pad = OptionFieldWidth + InitialPad;
326 OS.indent(Pad + 1) << OptionHelp[i].second << '\n';
330 static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
331 unsigned GroupID = Opts.getOptionGroupID(Id);
333 // If not in a group, return the default help group.
337 // Abuse the help text of the option groups to store the "help group"
340 // FIXME: Split out option groups.
341 if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
344 // Otherwise keep looking.
345 return getOptionHelpGroup(Opts, GroupID);
348 void OptTable::PrintHelp(raw_ostream &OS, const char *Name,
349 const char *Title, unsigned short FlagsToInclude,
350 unsigned short FlagsToExclude) const {
351 OS << "OVERVIEW: " << Title << "\n";
353 OS << "USAGE: " << Name << " [options] <inputs>\n";
356 // Render help text into a map of group-name to a list of (option, help)
358 typedef std::map<std::string,
359 std::vector<std::pair<std::string, const char*> > > helpmap_ty;
360 helpmap_ty GroupedOptionHelp;
362 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
365 // FIXME: Split out option groups.
366 if (getOptionKind(Id) == Option::GroupClass)
369 if ((FlagsToInclude && !(getInfo(Id).Flags & FlagsToInclude)) ||
370 getInfo(Id).Flags & FlagsToExclude)
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));
380 for (helpmap_ty::iterator it = GroupedOptionHelp .begin(),
381 ie = GroupedOptionHelp.end(); it != ie; ++it) {
382 if (it != GroupedOptionHelp .begin())
384 PrintHelpOptionList(OS, it->first, it->second);