2009-11-19 08:15:11 +08:00
|
|
|
//===--- OptTable.cpp - Option Table Implementation ---------------------*-===//
|
2009-03-04 16:33:23 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-11-19 08:15:11 +08:00
|
|
|
#include "clang/Driver/OptTable.h"
|
2009-03-05 06:41:37 +08:00
|
|
|
#include "clang/Driver/Arg.h"
|
|
|
|
#include "clang/Driver/ArgList.h"
|
2009-03-04 16:33:23 +08:00
|
|
|
#include "clang/Driver/Option.h"
|
2009-12-03 15:01:38 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-03-24 05:50:40 +08:00
|
|
|
#include <algorithm>
|
2009-03-04 16:33:23 +08:00
|
|
|
#include <cassert>
|
2009-12-05 05:08:40 +08:00
|
|
|
#include <map>
|
2009-03-04 16:33:23 +08:00
|
|
|
using namespace clang::driver;
|
|
|
|
using namespace clang::driver::options;
|
|
|
|
|
2009-03-24 02:41:45 +08:00
|
|
|
// Ordering on Info. The ordering is *almost* lexicographic, with two
|
|
|
|
// exceptions. First, '\0' comes at the end of the alphabet instead of
|
|
|
|
// the beginning (thus options preceed any other options which prefix
|
|
|
|
// them). Second, for options with the same name, the less permissive
|
|
|
|
// version should come first; a Flag option should preceed a Joined
|
|
|
|
// option, for example.
|
|
|
|
|
|
|
|
static int StrCmpOptionName(const char *A, const char *B) {
|
|
|
|
char a = *A, b = *B;
|
|
|
|
while (a == b) {
|
|
|
|
if (a == '\0')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
a = *++A;
|
|
|
|
b = *++B;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a == '\0') // A is a prefix of B.
|
|
|
|
return 1;
|
|
|
|
if (b == '\0') // B is a prefix of A.
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Otherwise lexicographic.
|
|
|
|
return (a < b) ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
2009-11-19 05:42:57 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace driver {
|
2009-11-19 04:19:36 +08:00
|
|
|
static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
|
2009-03-24 02:41:45 +08:00
|
|
|
if (&A == &B)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (int N = StrCmpOptionName(A.Name, B.Name))
|
|
|
|
return N == -1;
|
|
|
|
|
|
|
|
// Names are the same, check that classes are in order; exactly one
|
|
|
|
// should be joined, and it should succeed the other.
|
2009-03-25 11:06:26 +08:00
|
|
|
assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
|
2009-03-24 02:41:45 +08:00
|
|
|
"Unexpected classes for options with same name.");
|
|
|
|
return B.Kind == Option::JoinedClass;
|
|
|
|
}
|
|
|
|
|
2009-11-19 05:42:57 +08:00
|
|
|
// Support lower_bound between info and an option name.
|
|
|
|
static inline bool operator<(const OptTable::Info &I, const char *Name) {
|
|
|
|
return StrCmpOptionName(I.Name, Name) == -1;
|
|
|
|
}
|
|
|
|
static inline bool operator<(const char *Name, const OptTable::Info &I) {
|
|
|
|
return StrCmpOptionName(Name, I.Name) == -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-24 02:41:45 +08:00
|
|
|
//
|
|
|
|
|
2009-11-19 12:14:53 +08:00
|
|
|
OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2009-11-19 04:19:36 +08:00
|
|
|
OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos)
|
|
|
|
: OptionInfos(_OptionInfos), NumOptionInfos(_NumOptionInfos),
|
|
|
|
Options(new Option*[NumOptionInfos]),
|
|
|
|
TheInputOption(0), TheUnknownOption(0), FirstSearchableIndex(0)
|
|
|
|
{
|
2009-07-14 05:50:47 +08:00
|
|
|
// Explicitly zero initialize the error to work around a bug in array
|
|
|
|
// value-initialization on MinGW with gcc 4.3.5.
|
2009-11-19 04:19:36 +08:00
|
|
|
memset(Options, 0, sizeof(*Options) * NumOptionInfos);
|
2009-07-14 05:50:47 +08:00
|
|
|
|
2009-03-24 02:41:45 +08:00
|
|
|
// Find start of normal options.
|
2009-11-19 04:19:36 +08:00
|
|
|
for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
|
|
|
|
unsigned Kind = getInfo(i + 1).Kind;
|
|
|
|
if (Kind == Option::InputClass) {
|
|
|
|
assert(!TheInputOption && "Cannot have multiple input options!");
|
|
|
|
TheInputOption = getOption(i + 1);
|
|
|
|
} else if (Kind == Option::UnknownClass) {
|
|
|
|
assert(!TheUnknownOption && "Cannot have multiple input options!");
|
|
|
|
TheUnknownOption = getOption(i + 1);
|
|
|
|
} else if (Kind != Option::GroupClass) {
|
|
|
|
FirstSearchableIndex = i;
|
2009-03-24 02:41:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-11-19 04:19:36 +08:00
|
|
|
assert(FirstSearchableIndex != 0 && "No searchable options?");
|
2009-03-24 02:41:45 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Check that everything after the first searchable option is a
|
|
|
|
// regular option class.
|
2009-11-19 04:19:36 +08:00
|
|
|
for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) {
|
|
|
|
Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind;
|
2009-03-24 02:41:45 +08:00
|
|
|
assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
|
|
|
|
Kind != Option::GroupClass) &&
|
|
|
|
"Special options should be defined first!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that options are in order.
|
2009-11-19 04:19:36 +08:00
|
|
|
for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) {
|
|
|
|
if (!(getInfo(i) < getInfo(i + 1))) {
|
|
|
|
getOption(i)->dump();
|
|
|
|
getOption(i + 1)->dump();
|
2009-03-24 02:41:45 +08:00
|
|
|
assert(0 && "Options are not in order!");
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
#endif
|
2009-03-04 16:33:23 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
OptTable::~OptTable() {
|
2009-11-19 04:19:36 +08:00
|
|
|
for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
|
2009-03-04 16:33:23 +08:00
|
|
|
delete Options[i];
|
|
|
|
delete[] Options;
|
|
|
|
}
|
|
|
|
|
2009-11-19 04:19:36 +08:00
|
|
|
Option *OptTable::CreateOption(unsigned id) const {
|
|
|
|
const Info &info = getInfo(id);
|
2009-09-09 23:08:12 +08:00
|
|
|
const OptionGroup *Group =
|
2009-11-19 04:19:36 +08:00
|
|
|
cast_or_null<OptionGroup>(getOption(info.GroupID));
|
|
|
|
const Option *Alias = getOption(info.AliasID);
|
2009-03-04 16:33:23 +08:00
|
|
|
|
|
|
|
Option *Opt = 0;
|
|
|
|
switch (info.Kind) {
|
2009-03-05 05:53:04 +08:00
|
|
|
case Option::InputClass:
|
2009-11-19 12:25:06 +08:00
|
|
|
Opt = new InputOption(id); break;
|
2009-03-05 05:53:04 +08:00
|
|
|
case Option::UnknownClass:
|
2009-11-19 12:25:06 +08:00
|
|
|
Opt = new UnknownOption(id); break;
|
2009-03-04 16:33:23 +08:00
|
|
|
case Option::GroupClass:
|
2009-03-05 05:53:04 +08:00
|
|
|
Opt = new OptionGroup(id, info.Name, Group); break;
|
2009-03-04 16:33:23 +08:00
|
|
|
case Option::FlagClass:
|
2009-03-05 05:53:04 +08:00
|
|
|
Opt = new FlagOption(id, info.Name, Group, Alias); break;
|
2009-03-04 16:33:23 +08:00
|
|
|
case Option::JoinedClass:
|
2009-03-05 05:53:04 +08:00
|
|
|
Opt = new JoinedOption(id, info.Name, Group, Alias); break;
|
2009-03-04 16:33:23 +08:00
|
|
|
case Option::SeparateClass:
|
2009-03-05 05:53:04 +08:00
|
|
|
Opt = new SeparateOption(id, info.Name, Group, Alias); break;
|
2009-03-04 16:33:23 +08:00
|
|
|
case Option::CommaJoinedClass:
|
2009-03-05 05:53:04 +08:00
|
|
|
Opt = new CommaJoinedOption(id, info.Name, Group, Alias); break;
|
2009-03-04 16:33:23 +08:00
|
|
|
case Option::MultiArgClass:
|
2009-03-05 05:53:04 +08:00
|
|
|
Opt = new MultiArgOption(id, info.Name, Group, Alias, info.Param); break;
|
2009-03-04 16:33:23 +08:00
|
|
|
case Option::JoinedOrSeparateClass:
|
2009-03-05 05:53:04 +08:00
|
|
|
Opt = new JoinedOrSeparateOption(id, info.Name, Group, Alias); break;
|
2009-03-04 16:33:23 +08:00
|
|
|
case Option::JoinedAndSeparateClass:
|
2009-03-05 05:53:04 +08:00
|
|
|
Opt = new JoinedAndSeparateOption(id, info.Name, Group, Alias); break;
|
2009-03-04 16:33:23 +08:00
|
|
|
}
|
|
|
|
|
2009-11-19 01:42:34 +08:00
|
|
|
if (info.Flags & DriverOption)
|
|
|
|
Opt->setDriverOption(true);
|
|
|
|
if (info.Flags & LinkerInput)
|
|
|
|
Opt->setLinkerInput(true);
|
|
|
|
if (info.Flags & NoArgumentUnused)
|
|
|
|
Opt->setNoArgumentUnused(true);
|
|
|
|
if (info.Flags & RenderAsInput)
|
|
|
|
Opt->setNoOptAsInput(true);
|
|
|
|
if (info.Flags & RenderJoined) {
|
2010-03-20 09:12:00 +08:00
|
|
|
assert((info.Kind == Option::JoinedOrSeparateClass ||
|
|
|
|
info.Kind == Option::SeparateClass) && "Invalid option.");
|
2010-06-10 06:31:04 +08:00
|
|
|
Opt->setRenderStyle(Option::RenderJoinedStyle);
|
2009-11-19 01:42:34 +08:00
|
|
|
}
|
|
|
|
if (info.Flags & RenderSeparate) {
|
2010-03-20 09:12:00 +08:00
|
|
|
assert((info.Kind == Option::JoinedOrSeparateClass ||
|
|
|
|
info.Kind == Option::JoinedClass) && "Invalid option.");
|
2010-06-10 06:31:04 +08:00
|
|
|
Opt->setRenderStyle(Option::RenderSeparateStyle);
|
2009-03-04 16:33:23 +08:00
|
|
|
}
|
2009-11-19 01:42:34 +08:00
|
|
|
if (info.Flags & Unsupported)
|
|
|
|
Opt->setUnsupported(true);
|
2009-03-04 16:33:23 +08:00
|
|
|
|
|
|
|
return Opt;
|
|
|
|
}
|
2009-03-05 06:41:37 +08:00
|
|
|
|
2010-06-12 06:00:17 +08:00
|
|
|
Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index) const {
|
2009-03-23 07:26:43 +08:00
|
|
|
unsigned Prev = Index;
|
2009-03-05 06:41:37 +08:00
|
|
|
const char *Str = Args.getArgString(Index);
|
|
|
|
|
2009-03-12 16:44:47 +08:00
|
|
|
// Anything that doesn't start with '-' is an input, as is '-' itself.
|
|
|
|
if (Str[0] != '-' || Str[1] == '\0')
|
2010-06-10 06:31:08 +08:00
|
|
|
return new Arg(TheInputOption, Index++, Str);
|
2009-03-05 06:41:37 +08:00
|
|
|
|
2009-11-19 04:19:36 +08:00
|
|
|
const Info *Start = OptionInfos + FirstSearchableIndex;
|
2009-11-19 12:25:06 +08:00
|
|
|
const Info *End = OptionInfos + getNumOptions();
|
2009-03-24 05:50:40 +08:00
|
|
|
|
2009-04-08 02:21:47 +08:00
|
|
|
// Search for the first next option which could be a prefix.
|
2009-03-24 05:50:40 +08:00
|
|
|
Start = std::lower_bound(Start, End, Str);
|
|
|
|
|
2009-04-08 02:21:47 +08:00
|
|
|
// Options are stored in sorted order, with '\0' at the end of the
|
|
|
|
// alphabet. Since the only options which can accept a string must
|
|
|
|
// prefix it, we iteratively search for the next option which could
|
|
|
|
// be a prefix.
|
|
|
|
//
|
|
|
|
// FIXME: This is searching much more than necessary, but I am
|
|
|
|
// blanking on the simplest way to make it fast. We can solve this
|
|
|
|
// problem when we move to TableGen.
|
2009-03-24 05:50:40 +08:00
|
|
|
for (; Start != End; ++Start) {
|
2009-04-08 02:21:47 +08:00
|
|
|
// Scan for first option which is a proper prefix.
|
|
|
|
for (; Start != End; ++Start)
|
|
|
|
if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0)
|
|
|
|
break;
|
|
|
|
if (Start == End)
|
2009-03-24 05:50:40 +08:00
|
|
|
break;
|
|
|
|
|
2009-04-08 02:21:47 +08:00
|
|
|
// See if this option matches.
|
2009-11-19 12:25:06 +08:00
|
|
|
if (Arg *A = getOption(Start - OptionInfos + 1)->accept(Args, Index))
|
2009-03-24 05:50:40 +08:00
|
|
|
return A;
|
|
|
|
|
|
|
|
// Otherwise, see if this argument was missing values.
|
|
|
|
if (Prev != Index)
|
|
|
|
return 0;
|
2009-03-05 06:41:37 +08:00
|
|
|
}
|
|
|
|
|
2010-06-10 06:31:08 +08:00
|
|
|
return new Arg(TheUnknownOption, Index++, Str);
|
2009-11-19 04:19:36 +08:00
|
|
|
}
|
2009-11-19 14:35:06 +08:00
|
|
|
|
|
|
|
InputArgList *OptTable::ParseArgs(const char **ArgBegin, const char **ArgEnd,
|
|
|
|
unsigned &MissingArgIndex,
|
|
|
|
unsigned &MissingArgCount) const {
|
|
|
|
InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
|
|
|
|
|
|
|
|
// FIXME: Handle '@' args (or at least error on them).
|
|
|
|
|
|
|
|
MissingArgIndex = MissingArgCount = 0;
|
|
|
|
unsigned Index = 0, End = ArgEnd - ArgBegin;
|
|
|
|
while (Index < End) {
|
|
|
|
// Ignore empty arguments (other things may still take them as arguments).
|
|
|
|
if (Args->getArgString(Index)[0] == '\0') {
|
|
|
|
++Index;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Prev = Index;
|
|
|
|
Arg *A = ParseOneArg(*Args, Index);
|
|
|
|
assert(Index > Prev && "Parser failed to consume argument.");
|
|
|
|
|
|
|
|
// Check for missing argument error.
|
|
|
|
if (!A) {
|
|
|
|
assert(Index >= End && "Unexpected parser error.");
|
|
|
|
assert(Index - Prev - 1 && "No missing arguments!");
|
|
|
|
MissingArgIndex = Prev;
|
|
|
|
MissingArgCount = Index - Prev - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Args->append(A);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Args;
|
|
|
|
}
|
2009-12-03 15:01:38 +08:00
|
|
|
|
|
|
|
static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
|
|
|
|
std::string Name = Opts.getOptionName(Id);
|
|
|
|
|
|
|
|
// Add metavar, if used.
|
|
|
|
switch (Opts.getOptionKind(Id)) {
|
|
|
|
case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
|
|
|
|
assert(0 && "Invalid option with help text.");
|
|
|
|
|
2010-06-17 00:59:23 +08:00
|
|
|
case Option::MultiArgClass:
|
2009-12-03 15:01:38 +08:00
|
|
|
assert(0 && "Cannot print metavar for this kind of option.");
|
|
|
|
|
|
|
|
case Option::FlagClass:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Option::SeparateClass: case Option::JoinedOrSeparateClass:
|
|
|
|
Name += ' ';
|
|
|
|
// FALLTHROUGH
|
|
|
|
case Option::JoinedClass: case Option::CommaJoinedClass:
|
2010-06-17 00:59:23 +08:00
|
|
|
case Option::JoinedAndSeparateClass:
|
2009-12-03 15:01:38 +08:00
|
|
|
if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
|
|
|
|
Name += MetaVarName;
|
|
|
|
else
|
|
|
|
Name += "<value>";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Name;
|
|
|
|
}
|
|
|
|
|
2009-12-05 05:08:40 +08:00
|
|
|
static void PrintHelpOptionList(llvm::raw_ostream &OS, llvm::StringRef Title,
|
|
|
|
std::vector<std::pair<std::string,
|
|
|
|
const char*> > &OptionHelp) {
|
|
|
|
OS << Title << ":\n";
|
2009-12-03 15:01:38 +08:00
|
|
|
|
|
|
|
// Find the maximum option length.
|
|
|
|
unsigned OptionFieldWidth = 0;
|
|
|
|
for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
|
|
|
|
// Skip titles.
|
|
|
|
if (!OptionHelp[i].second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Limit the amount of padding we are willing to give up for alignment.
|
|
|
|
unsigned Length = OptionHelp[i].first.size();
|
|
|
|
if (Length <= 23)
|
|
|
|
OptionFieldWidth = std::max(OptionFieldWidth, Length);
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned InitialPad = 2;
|
|
|
|
for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
|
|
|
|
const std::string &Option = OptionHelp[i].first;
|
|
|
|
int Pad = OptionFieldWidth - int(Option.size());
|
|
|
|
OS.indent(InitialPad) << Option;
|
|
|
|
|
|
|
|
// Break on long option names.
|
|
|
|
if (Pad < 0) {
|
|
|
|
OS << "\n";
|
|
|
|
Pad = OptionFieldWidth + InitialPad;
|
|
|
|
}
|
|
|
|
OS.indent(Pad + 1) << OptionHelp[i].second << '\n';
|
|
|
|
}
|
2009-12-05 05:08:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
|
|
|
|
unsigned GroupID = Opts.getOptionGroupID(Id);
|
|
|
|
|
|
|
|
// If not in a group, return the default help group.
|
|
|
|
if (!GroupID)
|
|
|
|
return "OPTIONS";
|
|
|
|
|
|
|
|
// Abuse the help text of the option groups to store the "help group"
|
|
|
|
// name.
|
|
|
|
//
|
|
|
|
// FIXME: Split out option groups.
|
|
|
|
if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
|
|
|
|
return GroupHelp;
|
|
|
|
|
|
|
|
// Otherwise keep looking.
|
|
|
|
return getOptionHelpGroup(Opts, GroupID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptTable::PrintHelp(llvm::raw_ostream &OS, const char *Name,
|
|
|
|
const char *Title, bool ShowHidden) const {
|
|
|
|
OS << "OVERVIEW: " << Title << "\n";
|
|
|
|
OS << '\n';
|
|
|
|
OS << "USAGE: " << Name << " [options] <inputs>\n";
|
|
|
|
OS << '\n';
|
|
|
|
|
|
|
|
// Render help text into a map of group-name to a list of (option, help)
|
|
|
|
// pairs.
|
|
|
|
typedef std::map<std::string,
|
|
|
|
std::vector<std::pair<std::string, const char*> > > helpmap_ty;
|
|
|
|
helpmap_ty GroupedOptionHelp;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
|
|
|
|
unsigned Id = i + 1;
|
|
|
|
|
|
|
|
// FIXME: Split out option groups.
|
|
|
|
if (getOptionKind(Id) == Option::GroupClass)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!ShowHidden && isOptionHelpHidden(Id))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (const char *Text = getOptionHelpText(Id)) {
|
|
|
|
const char *HelpGroup = getOptionHelpGroup(*this, Id);
|
|
|
|
const std::string &OptName = getOptionHelpName(*this, Id);
|
|
|
|
GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (helpmap_ty::iterator it = GroupedOptionHelp .begin(),
|
|
|
|
ie = GroupedOptionHelp.end(); it != ie; ++it) {
|
|
|
|
if (it != GroupedOptionHelp .begin())
|
|
|
|
OS << "\n";
|
|
|
|
PrintHelpOptionList(OS, it->first, it->second);
|
|
|
|
}
|
2009-12-03 15:01:38 +08:00
|
|
|
|
|
|
|
OS.flush();
|
|
|
|
}
|