return (a < b) ? -1 : 1;
}
+#ifndef NDEBUG
+static int StrCmpOptionName(const char *A, const char *B) {
+ if (int N = StrCmpOptionNameIgnoreCase(A, B))
+ return N;
+ return strcmp(A, B);
+}
+
+static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
+ if (&A == &B)
+ return false;
+
+ if (int N = StrCmpOptionName(A.Name, B.Name))
+ return N < 0;
+
+ for (const char * const *APre = A.Prefixes,
+ * const *BPre = B.Prefixes;
+ *APre != nullptr && *BPre != nullptr; ++APre, ++BPre){
+ if (int N = StrCmpOptionName(*APre, *BPre))
+ return N < 0;
+ }
+
+ // Names are the same, check that classes are in order; exactly one
+ // should be joined, and it should succeed the other.
+ assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
+ "Unexpected classes for options with same name.");
+ return B.Kind == Option::JoinedClass;
+}
+#endif
+
// Support lower_bound between info and an option name.
static inline bool operator<(const OptTable::Info &I, const char *Name) {
return StrCmpOptionNameIgnoreCase(I.Name, Name) < 0;
OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
-OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos,
- bool _IgnoreCase)
- : OptionInfos(_OptionInfos),
- NumOptionInfos(_NumOptionInfos),
- IgnoreCase(_IgnoreCase),
- TheInputOptionID(0),
- TheUnknownOptionID(0),
- FirstSearchableIndex(0)
-{
+OptTable::OptTable(const Info *OptionInfos, unsigned NumOptionInfos,
+ bool IgnoreCase)
+ : OptionInfos(OptionInfos), NumOptionInfos(NumOptionInfos),
+ IgnoreCase(IgnoreCase), TheInputOptionID(0), TheUnknownOptionID(0),
+ FirstSearchableIndex(0) {
// Explicitly zero initialize the error to work around a bug in array
// value-initialization on MinGW with gcc 4.3.5.
for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions() + 1;
i != e; ++i) {
if (const char *const *P = getInfo(i).Prefixes) {
- for (; *P != 0; ++P) {
+ for (; *P != nullptr; ++P) {
PrefixesUnion.insert(*P);
}
}
const Option OptTable::getOption(OptSpecifier Opt) const {
unsigned id = Opt.getID();
if (id == 0)
- return Option(0, 0);
+ return Option(nullptr, nullptr);
assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID.");
return Option(&getInfo(id), this);
}
return true;
}
-// Returns true if X starts with Y, ignoring case.
-static bool startsWithIgnoreCase(StringRef X, StringRef Y) {
- if (X.size() < Y.size())
- return false;
- return X.substr(0, Y.size()).equals_lower(Y);
-}
-
/// \returns Matched size. 0 means no match.
static unsigned matchOption(const OptTable::Info *I, StringRef Str,
bool IgnoreCase) {
- for (const char * const *Pre = I->Prefixes; *Pre != 0; ++Pre) {
+ for (const char * const *Pre = I->Prefixes; *Pre != nullptr; ++Pre) {
StringRef Prefix(*Pre);
if (Str.startswith(Prefix)) {
StringRef Rest = Str.substr(Prefix.size());
bool Matched = IgnoreCase
- ? startsWithIgnoreCase(Rest, I->Name)
+ ? Rest.startswith_lower(I->Name)
: Rest.startswith(I->Name);
if (Matched)
return Prefix.size() + StringRef(I->Name).size();
// Otherwise, see if this argument was missing values.
if (Prev != Index)
- return 0;
+ return nullptr;
}
// If we failed to find an option and this arg started with /, then it's
return new Arg(getOption(TheUnknownOptionID), Str, Index++, Str);
}
-InputArgList *OptTable::ParseArgs(const char *const *ArgBegin,
- const char *const *ArgEnd,
- unsigned &MissingArgIndex,
- unsigned &MissingArgCount,
- unsigned FlagsToInclude,
- unsigned FlagsToExclude) const {
- InputArgList *Args = new InputArgList(ArgBegin, ArgEnd);
+InputArgList OptTable::ParseArgs(ArrayRef<const char *> ArgArr,
+ unsigned &MissingArgIndex,
+ unsigned &MissingArgCount,
+ unsigned FlagsToInclude,
+ unsigned FlagsToExclude) const {
+ InputArgList Args(ArgArr.begin(), ArgArr.end());
// FIXME: Handle '@' args (or at least error on them).
MissingArgIndex = MissingArgCount = 0;
- unsigned Index = 0, End = ArgEnd - ArgBegin;
+ unsigned Index = 0, End = ArgArr.size();
while (Index < End) {
+ // Ingore nullptrs, they are response file's EOL markers
+ if (Args.getArgString(Index) == nullptr) {
+ ++Index;
+ continue;
+ }
// Ignore empty arguments (other things may still take them as arguments).
- StringRef Str = Args->getArgString(Index);
+ StringRef Str = Args.getArgString(Index);
if (Str == "") {
++Index;
continue;
}
unsigned Prev = Index;
- Arg *A = ParseOneArg(*Args, Index, FlagsToInclude, FlagsToExclude);
+ Arg *A = ParseOneArg(Args, Index, FlagsToInclude, FlagsToExclude);
assert(Index > Prev && "Parser failed to consume argument.");
// Check for missing argument error.
break;
}
- Args->append(A);
+ Args.append(A);
}
return Args;
llvm_unreachable("Invalid option with help text.");
case Option::MultiArgClass:
- llvm_unreachable("Cannot print metavar for this kind of option.");
+ if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) {
+ // For MultiArgs, metavar is full list of all argument names.
+ Name += ' ';
+ Name += MetaVarName;
+ }
+ else {
+ // For MultiArgs<N>, if metavar not supplied, print <value> N times.
+ for (unsigned i=0, e=O.getNumArgs(); i< e; ++i) {
+ Name += " <value>";
+ }
+ }
+ break;
case Option::FlagClass:
break;