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 "llvm/Support/raw_ostream.h"
15 #include "llvm/Support/ErrorHandling.h"
18 using namespace clang::driver;
19 using namespace clang::driver::options;
20 using namespace clang;
22 // Ordering on Info. The ordering is *almost* lexicographic, with two
23 // exceptions. First, '\0' comes at the end of the alphabet instead of
24 // the beginning (thus options precede any other options which prefix
25 // them). Second, for options with the same name, the less permissive
26 // version should come first; a Flag option should precede a Joined
27 // option, for example.
29 static int StrCmpOptionName(const char *A, const char *B) {
39 if (a == '\0') // A is a prefix of B.
41 if (b == '\0') // B is a prefix of A.
44 // Otherwise lexicographic.
45 return (a < b) ? -1 : 1;
50 static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
54 if (int N = StrCmpOptionName(A.Name, B.Name))
57 // Names are the same, check that classes are in order; exactly one
58 // should be joined, and it should succeed the other.
59 assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
60 "Unexpected classes for options with same name.");
61 return B.Kind == Option::JoinedClass;
64 // Support lower_bound between info and an option name.
65 static inline bool operator<(const OptTable::Info &I, const char *Name) {
66 return StrCmpOptionName(I.Name, Name) == -1;
68 static inline bool operator<(const char *Name, const OptTable::Info &I) {
69 return StrCmpOptionName(Name, I.Name) == -1;
76 OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
80 OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos)
81 : OptionInfos(_OptionInfos), NumOptionInfos(_NumOptionInfos),
82 Options(new Option*[NumOptionInfos]),
83 TheInputOption(0), TheUnknownOption(0), FirstSearchableIndex(0)
85 // Explicitly zero initialize the error to work around a bug in array
86 // value-initialization on MinGW with gcc 4.3.5.
87 memset(Options, 0, sizeof(*Options) * NumOptionInfos);
89 // Find start of normal options.
90 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
91 unsigned Kind = getInfo(i + 1).Kind;
92 if (Kind == Option::InputClass) {
93 assert(!TheInputOption && "Cannot have multiple input options!");
94 TheInputOption = getOption(i + 1);
95 } else if (Kind == Option::UnknownClass) {
96 assert(!TheUnknownOption && "Cannot have multiple input options!");
97 TheUnknownOption = getOption(i + 1);
98 } else if (Kind != Option::GroupClass) {
99 FirstSearchableIndex = i;
103 assert(FirstSearchableIndex != 0 && "No searchable options?");
106 // Check that everything after the first searchable option is a
107 // regular option class.
108 for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) {
109 Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind;
110 assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
111 Kind != Option::GroupClass) &&
112 "Special options should be defined first!");
115 // Check that options are in order.
116 for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) {
117 if (!(getInfo(i) < getInfo(i + 1))) {
118 getOption(i)->dump();
119 getOption(i + 1)->dump();
120 llvm_unreachable("Options are not in order!");
126 OptTable::~OptTable() {
127 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
132 Option *OptTable::CreateOption(unsigned id) const {
133 const Info &info = getInfo(id);
134 const OptionGroup *Group =
135 cast_or_null<OptionGroup>(getOption(info.GroupID));
136 const Option *Alias = getOption(info.AliasID);
140 case Option::InputClass:
141 Opt = new InputOption(id); break;
142 case Option::UnknownClass:
143 Opt = new UnknownOption(id); break;
144 case Option::GroupClass:
145 Opt = new OptionGroup(id, info.Name, Group); break;
146 case Option::FlagClass:
147 Opt = new FlagOption(id, info.Name, Group, Alias); break;
148 case Option::JoinedClass:
149 Opt = new JoinedOption(id, info.Name, Group, Alias); break;
150 case Option::SeparateClass:
151 Opt = new SeparateOption(id, info.Name, Group, Alias); break;
152 case Option::CommaJoinedClass:
153 Opt = new CommaJoinedOption(id, info.Name, Group, Alias); break;
154 case Option::MultiArgClass:
155 Opt = new MultiArgOption(id, info.Name, Group, Alias, info.Param); break;
156 case Option::JoinedOrSeparateClass:
157 Opt = new JoinedOrSeparateOption(id, info.Name, Group, Alias); break;
158 case Option::JoinedAndSeparateClass:
159 Opt = new JoinedAndSeparateOption(id, info.Name, Group, Alias); break;
162 if (info.Flags & DriverOption)
163 Opt->setDriverOption(true);
164 if (info.Flags & LinkerInput)
165 Opt->setLinkerInput(true);
166 if (info.Flags & NoArgumentUnused)
167 Opt->setNoArgumentUnused(true);
168 if (info.Flags & NoForward)
169 Opt->setNoForward(true);
170 if (info.Flags & RenderAsInput)
171 Opt->setNoOptAsInput(true);
172 if (info.Flags & RenderJoined) {
173 assert((info.Kind == Option::JoinedOrSeparateClass ||
174 info.Kind == Option::SeparateClass) && "Invalid option.");
175 Opt->setRenderStyle(Option::RenderJoinedStyle);
177 if (info.Flags & RenderSeparate) {
178 assert((info.Kind == Option::JoinedOrSeparateClass ||
179 info.Kind == Option::JoinedClass) && "Invalid option.");
180 Opt->setRenderStyle(Option::RenderSeparateStyle);
182 if (info.Flags & Unsupported)
183 Opt->setUnsupported(true);
188 Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index) const {
189 unsigned Prev = Index;
190 const char *Str = Args.getArgString(Index);
192 // Anything that doesn't start with '-' is an input, as is '-' itself.
193 if (Str[0] != '-' || Str[1] == '\0')
194 return new Arg(TheInputOption, Index++, Str);
196 const Info *Start = OptionInfos + FirstSearchableIndex;
197 const Info *End = OptionInfos + getNumOptions();
199 // Search for the first next option which could be a prefix.
200 Start = std::lower_bound(Start, End, Str);
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 // Scan for first option which is a proper prefix.
212 for (; Start != End; ++Start)
213 if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0)
218 // See if this option matches.
219 if (Arg *A = getOption(Start - OptionInfos + 1)->accept(Args, Index))
222 // Otherwise, see if this argument was missing values.
227 return new Arg(TheUnknownOption, Index++, Str);
230 InputArgList *OptTable::ParseArgs(const char* const *ArgBegin,
231 const char* const *ArgEnd,
232 unsigned &MissingArgIndex,
233 unsigned &MissingArgCount) const {
234 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
236 // FIXME: Handle '@' args (or at least error on them).
238 MissingArgIndex = MissingArgCount = 0;
239 unsigned Index = 0, End = ArgEnd - ArgBegin;
240 while (Index < End) {
241 // Ignore empty arguments (other things may still take them as arguments).
242 if (Args->getArgString(Index)[0] == '\0') {
247 unsigned Prev = Index;
248 Arg *A = ParseOneArg(*Args, Index);
249 assert(Index > Prev && "Parser failed to consume argument.");
251 // Check for missing argument error.
253 assert(Index >= End && "Unexpected parser error.");
254 assert(Index - Prev - 1 && "No missing arguments!");
255 MissingArgIndex = Prev;
256 MissingArgCount = Index - Prev - 1;
266 static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
267 std::string Name = Opts.getOptionName(Id);
269 // Add metavar, if used.
270 switch (Opts.getOptionKind(Id)) {
271 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
272 llvm_unreachable("Invalid option with help text.");
274 case Option::MultiArgClass:
275 llvm_unreachable("Cannot print metavar for this kind of option.");
277 case Option::FlagClass:
280 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
283 case Option::JoinedClass: case Option::CommaJoinedClass:
284 case Option::JoinedAndSeparateClass:
285 if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
295 static void PrintHelpOptionList(raw_ostream &OS, StringRef Title,
296 std::vector<std::pair<std::string,
297 const char*> > &OptionHelp) {
298 OS << Title << ":\n";
300 // Find the maximum option length.
301 unsigned OptionFieldWidth = 0;
302 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
304 if (!OptionHelp[i].second)
307 // Limit the amount of padding we are willing to give up for alignment.
308 unsigned Length = OptionHelp[i].first.size();
310 OptionFieldWidth = std::max(OptionFieldWidth, Length);
313 const unsigned InitialPad = 2;
314 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
315 const std::string &Option = OptionHelp[i].first;
316 int Pad = OptionFieldWidth - int(Option.size());
317 OS.indent(InitialPad) << Option;
319 // Break on long option names.
322 Pad = OptionFieldWidth + InitialPad;
324 OS.indent(Pad + 1) << OptionHelp[i].second << '\n';
328 static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
329 unsigned GroupID = Opts.getOptionGroupID(Id);
331 // If not in a group, return the default help group.
335 // Abuse the help text of the option groups to store the "help group"
338 // FIXME: Split out option groups.
339 if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
342 // Otherwise keep looking.
343 return getOptionHelpGroup(Opts, GroupID);
346 void OptTable::PrintHelp(raw_ostream &OS, const char *Name,
347 const char *Title, bool ShowHidden) const {
348 OS << "OVERVIEW: " << Title << "\n";
350 OS << "USAGE: " << Name << " [options] <inputs>\n";
353 // Render help text into a map of group-name to a list of (option, help)
355 typedef std::map<std::string,
356 std::vector<std::pair<std::string, const char*> > > helpmap_ty;
357 helpmap_ty GroupedOptionHelp;
359 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
362 // FIXME: Split out option groups.
363 if (getOptionKind(Id) == Option::GroupClass)
366 if (!ShowHidden && isOptionHelpHidden(Id))
369 if (const char *Text = getOptionHelpText(Id)) {
370 const char *HelpGroup = getOptionHelpGroup(*this, Id);
371 const std::string &OptName = getOptionHelpName(*this, Id);
372 GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text));
376 for (helpmap_ty::iterator it = GroupedOptionHelp .begin(),
377 ie = GroupedOptionHelp.end(); it != ie; ++it) {
378 if (it != GroupedOptionHelp .begin())
380 PrintHelpOptionList(OS, it->first, it->second);