#include "llvm/Option/OptTable.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Option/OptSpecifier.h"
#include "llvm/Option/Option.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstring>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace llvm;
using namespace llvm::opt;
namespace llvm {
namespace opt {
static int StrCmpOptionNameIgnoreCase(const char *A, const char *B) {
const char *X = A, *Y = B;
char a = tolower(*A), b = tolower(*B);
while (a == b) {
if (a == '\0')
return 0;
a = tolower(*++X);
b = tolower(*++Y);
}
if (a == '\0') return 1;
if (b == '\0') return -1;
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;
}
assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
"Unexpected classes for options with same name.");
return B.Kind == Option::JoinedClass;
}
#endif
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(ArrayRef<Info> OptionInfos, bool IgnoreCase)
: OptionInfos(OptionInfos), IgnoreCase(IgnoreCase) {
for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
unsigned Kind = getInfo(i + 1).Kind;
if (Kind == Option::InputClass) {
assert(!InputOptionID && "Cannot have multiple input options!");
InputOptionID = getInfo(i + 1).ID;
} else if (Kind == Option::UnknownClass) {
assert(!UnknownOptionID && "Cannot have multiple unknown options!");
UnknownOptionID = getInfo(i + 1).ID;
} else if (Kind != Option::GroupClass) {
FirstSearchableIndex = i;
break;
}
}
assert(FirstSearchableIndex != 0 && "No searchable options?");
#ifndef NDEBUG
for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) {
Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind;
assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
Kind != Option::GroupClass) &&
"Special options should be defined first!");
}
for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions(); i != e; ++i){
if (!(getInfo(i) < getInfo(i + 1))) {
getOption(i).dump();
getOption(i + 1).dump();
llvm_unreachable("Options are not in order!");
}
}
#endif
for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions() + 1;
i != e; ++i) {
if (const char *const *P = getInfo(i).Prefixes) {
for (; *P != nullptr; ++P) {
PrefixesUnion.insert(*P);
}
}
}
for (StringSet<>::const_iterator I = PrefixesUnion.begin(),
E = PrefixesUnion.end(); I != E; ++I) {
StringRef Prefix = I->getKey();
for (char C : Prefix)
if (!is_contained(PrefixChars, C))
PrefixChars.push_back(C);
}
}
OptTable::~OptTable() = default;
const Option OptTable::getOption(OptSpecifier Opt) const {
unsigned id = Opt.getID();
if (id == 0)
return Option(nullptr, nullptr);
assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID.");
return Option(&getInfo(id), this);
}
static bool isInput(const StringSet<> &Prefixes, StringRef Arg) {
if (Arg == "-")
return true;
for (StringSet<>::const_iterator I = Prefixes.begin(),
E = Prefixes.end(); I != E; ++I)
if (Arg.startswith(I->getKey()))
return false;
return true;
}
static unsigned matchOption(const OptTable::Info *I, StringRef Str,
bool IgnoreCase) {
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 ? Rest.startswith_insensitive(I->Name)
: Rest.startswith(I->Name);
if (Matched)
return Prefix.size() + StringRef(I->Name).size();
}
}
return 0;
}
static bool optionMatches(const OptTable::Info &In, StringRef Option) {
if (In.Prefixes) {
StringRef InName(In.Name);
for (size_t I = 0; In.Prefixes[I]; I++)
if (Option.endswith(InName))
if (Option.slice(0, Option.size() - InName.size()) == In.Prefixes[I])
return true;
}
return false;
}
std::vector<std::string>
OptTable::suggestValueCompletions(StringRef Option, StringRef Arg) const {
for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
const Info &In = OptionInfos[I];
if (!In.Values || !optionMatches(In, Option))
continue;
SmallVector<StringRef, 8> Candidates;
StringRef(In.Values).split(Candidates, ",", -1, false);
std::vector<std::string> Result;
for (StringRef Val : Candidates)
if (Val.startswith(Arg) && Arg.compare(Val))
Result.push_back(std::string(Val));
return Result;
}
return {};
}
std::vector<std::string>
OptTable::findByPrefix(StringRef Cur, unsigned int DisableFlags) const {
std::vector<std::string> Ret;
for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
const Info &In = OptionInfos[I];
if (!In.Prefixes || (!In.HelpText && !In.GroupID))
continue;
if (In.Flags & DisableFlags)
continue;
for (int I = 0; In.Prefixes[I]; I++) {
std::string S = std::string(In.Prefixes[I]) + std::string(In.Name) + "\t";
if (In.HelpText)
S += In.HelpText;
if (StringRef(S).startswith(Cur) && S != std::string(Cur) + "\t")
Ret.push_back(S);
}
}
return Ret;
}
unsigned OptTable::findNearest(StringRef Option, std::string &NearestString,
unsigned FlagsToInclude, unsigned FlagsToExclude,
unsigned MinimumLength) const {
assert(!Option.empty());
unsigned BestDistance = UINT_MAX;
for (const Info &CandidateInfo :
ArrayRef<Info>(OptionInfos).drop_front(FirstSearchableIndex)) {
StringRef CandidateName = CandidateInfo.Name;
if (CandidateName.empty() || CandidateName.size() < MinimumLength)
continue;
if (FlagsToInclude && !(CandidateInfo.Flags & FlagsToInclude))
continue;
if (CandidateInfo.Flags & FlagsToExclude)
continue;
if (!CandidateInfo.Prefixes)
continue;
StringRef LHS, RHS;
char Last = CandidateName.back();
bool CandidateHasDelimiter = Last == '=' || Last == ':';
std::string NormalizedName = std::string(Option);
if (CandidateHasDelimiter) {
std::tie(LHS, RHS) = Option.split(Last);
NormalizedName = std::string(LHS);
if (Option.find(Last) == LHS.size())
NormalizedName += Last;
}
for (int P = 0;
const char *const CandidatePrefix = CandidateInfo.Prefixes[P]; P++) {
std::string Candidate = (CandidatePrefix + CandidateName).str();
StringRef CandidateRef = Candidate;
unsigned Distance =
CandidateRef.edit_distance(NormalizedName, true,
BestDistance);
if (RHS.empty() && CandidateHasDelimiter) {
++Distance;
}
if (Distance < BestDistance) {
BestDistance = Distance;
NearestString = (Candidate + RHS).str();
}
}
}
return BestDistance;
}
bool OptTable::addValues(const char *Option, const char *Values) {
for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
Info &In = OptionInfos[I];
if (optionMatches(In, Option)) {
In.Values = Values;
return true;
}
}
return false;
}
std::unique_ptr<Arg> OptTable::parseOneArgGrouped(InputArgList &Args,
unsigned &Index) const {
const char *CStr = Args.getArgString(Index);
StringRef Str(CStr);
if (isInput(PrefixesUnion, Str))
return std::make_unique<Arg>(getOption(InputOptionID), Str, Index++, CStr);
const Info *End = OptionInfos.data() + OptionInfos.size();
StringRef Name = Str.ltrim(PrefixChars);
const Info *Start = std::lower_bound(
OptionInfos.data() + FirstSearchableIndex, End, Name.data());
const Info *Fallback = nullptr;
unsigned Prev = Index;
for (; Start != End; ++Start) {
unsigned ArgSize = matchOption(Start, Str, IgnoreCase);
if (!ArgSize)
continue;
Option Opt(Start, this);
if (std::unique_ptr<Arg> A =
Opt.accept(Args, StringRef(Args.getArgString(Index), ArgSize),
false, Index))
return A;
if (ArgSize == 2 && Opt.getKind() == Option::FlagClass)
Fallback = Start;
if (Prev != Index)
return nullptr;
}
if (Fallback) {
Option Opt(Fallback, this);
if (Str[2] == '=')
return std::make_unique<Arg>(getOption(UnknownOptionID), Str, Index++,
CStr);
if (std::unique_ptr<Arg> A = Opt.accept(
Args, Str.substr(0, 2), true, Index)) {
Args.replaceArgString(Index, Twine('-') + Str.substr(2));
return A;
}
}
if (Str[1] != '-') {
CStr = Args.MakeArgString(Str.substr(0, 2));
Args.replaceArgString(Index, Twine('-') + Str.substr(2));
return std::make_unique<Arg>(getOption(UnknownOptionID), CStr, Index, CStr);
}
return std::make_unique<Arg>(getOption(UnknownOptionID), Str, Index++, CStr);
}
std::unique_ptr<Arg> OptTable::ParseOneArg(const ArgList &Args, unsigned &Index,
unsigned FlagsToInclude,
unsigned FlagsToExclude) const {
unsigned Prev = Index;
const char *Str = Args.getArgString(Index);
if (isInput(PrefixesUnion, Str))
return std::make_unique<Arg>(getOption(InputOptionID), Str, Index++, Str);
const Info *Start = OptionInfos.data() + FirstSearchableIndex;
const Info *End = OptionInfos.data() + OptionInfos.size();
StringRef Name = StringRef(Str).ltrim(PrefixChars);
Start = std::lower_bound(Start, End, Name.data());
for (; Start != End; ++Start) {
unsigned ArgSize = 0;
for (; Start != End; ++Start)
if ((ArgSize = matchOption(Start, Str, IgnoreCase)))
break;
if (Start == End)
break;
Option Opt(Start, this);
if (FlagsToInclude && !Opt.hasFlag(FlagsToInclude))
continue;
if (Opt.hasFlag(FlagsToExclude))
continue;
if (std::unique_ptr<Arg> A =
Opt.accept(Args, StringRef(Args.getArgString(Index), ArgSize),
false, Index))
return A;
if (Prev != Index)
return nullptr;
}
if (Str[0] == '/')
return std::make_unique<Arg>(getOption(InputOptionID), Str, Index++, Str);
return std::make_unique<Arg>(getOption(UnknownOptionID), Str, Index++, Str);
}
InputArgList OptTable::ParseArgs(ArrayRef<const char *> ArgArr,
unsigned &MissingArgIndex,
unsigned &MissingArgCount,
unsigned FlagsToInclude,
unsigned FlagsToExclude) const {
InputArgList Args(ArgArr.begin(), ArgArr.end());
MissingArgIndex = MissingArgCount = 0;
unsigned Index = 0, End = ArgArr.size();
while (Index < End) {
if (Args.getArgString(Index) == nullptr) {
++Index;
continue;
}
StringRef Str = Args.getArgString(Index);
if (Str == "") {
++Index;
continue;
}
unsigned Prev = Index;
std::unique_ptr<Arg> A = GroupedShortOptions
? parseOneArgGrouped(Args, Index)
: ParseOneArg(Args, Index, FlagsToInclude, FlagsToExclude);
assert((Index > Prev || GroupedShortOptions) &&
"Parser failed to consume argument.");
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.release());
}
return Args;
}
InputArgList OptTable::parseArgs(int Argc, char *const *Argv,
OptSpecifier Unknown, StringSaver &Saver,
function_ref<void(StringRef)> ErrorFn) const {
SmallVector<const char *, 0> NewArgv;
cl::expandResponseFiles(Argc, Argv, EnvVar, Saver, NewArgv);
unsigned MAI, MAC;
opt::InputArgList Args = ParseArgs(makeArrayRef(NewArgv), MAI, MAC);
if (MAC)
ErrorFn((Twine(Args.getArgString(MAI)) + ": missing argument").str());
std::string Nearest;
for (const opt::Arg *A : Args.filtered(Unknown)) {
std::string Spelling = A->getAsString(Args);
if (findNearest(Spelling, Nearest) > 1)
ErrorFn("unknown argument '" + A->getAsString(Args) + "'");
else
ErrorFn("unknown argument '" + A->getAsString(Args) +
"', did you mean '" + Nearest + "'?");
}
return Args;
}
static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
const Option O = Opts.getOption(Id);
std::string Name = O.getPrefixedName();
switch (O.getKind()) {
case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
llvm_unreachable("Invalid option with help text.");
case Option::MultiArgClass:
if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) {
Name += ' ';
Name += MetaVarName;
}
else {
for (unsigned i=0, e=O.getNumArgs(); i< e; ++i) {
Name += " <value>";
}
}
break;
case Option::FlagClass:
break;
case Option::ValuesClass:
break;
case Option::SeparateClass: case Option::JoinedOrSeparateClass:
case Option::RemainingArgsClass: case Option::RemainingArgsJoinedClass:
Name += ' ';
LLVM_FALLTHROUGH;
case Option::JoinedClass: case Option::CommaJoinedClass:
case Option::JoinedAndSeparateClass:
if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
Name += MetaVarName;
else
Name += "<value>";
break;
}
return Name;
}
namespace {
struct OptionInfo {
std::string Name;
StringRef HelpText;
};
}
static void PrintHelpOptionList(raw_ostream &OS, StringRef Title,
std::vector<OptionInfo> &OptionHelp) {
OS << Title << ":\n";
unsigned OptionFieldWidth = 0;
for (const OptionInfo &Opt : OptionHelp) {
unsigned Length = Opt.Name.size();
if (Length <= 23)
OptionFieldWidth = std::max(OptionFieldWidth, Length);
}
const unsigned InitialPad = 2;
for (const OptionInfo &Opt : OptionHelp) {
const std::string &Option = Opt.Name;
int Pad = OptionFieldWidth - int(Option.size());
OS.indent(InitialPad) << Option;
if (Pad < 0) {
OS << "\n";
Pad = OptionFieldWidth + InitialPad;
}
OS.indent(Pad + 1) << Opt.HelpText << '\n';
}
}
static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
unsigned GroupID = Opts.getOptionGroupID(Id);
if (!GroupID)
return "OPTIONS";
if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
return GroupHelp;
return getOptionHelpGroup(Opts, GroupID);
}
void OptTable::printHelp(raw_ostream &OS, const char *Usage, const char *Title,
bool ShowHidden, bool ShowAllAliases) const {
printHelp(OS, Usage, Title, 0,
(ShowHidden ? 0 : HelpHidden), ShowAllAliases);
}
void OptTable::printHelp(raw_ostream &OS, const char *Usage, const char *Title,
unsigned FlagsToInclude, unsigned FlagsToExclude,
bool ShowAllAliases) const {
OS << "OVERVIEW: " << Title << "\n\n";
OS << "USAGE: " << Usage << "\n\n";
std::map<std::string, std::vector<OptionInfo>> GroupedOptionHelp;
for (unsigned Id = 1, e = getNumOptions() + 1; Id != e; ++Id) {
if (getOptionKind(Id) == Option::GroupClass)
continue;
unsigned Flags = getInfo(Id).Flags;
if (FlagsToInclude && !(Flags & FlagsToInclude))
continue;
if (Flags & FlagsToExclude)
continue;
const char *HelpText = getOptionHelpText(Id);
if (!HelpText && ShowAllAliases) {
const Option Alias = getOption(Id).getAlias();
if (Alias.isValid())
HelpText = getOptionHelpText(Alias.getID());
}
if (HelpText && (strlen(HelpText) != 0)) {
const char *HelpGroup = getOptionHelpGroup(*this, Id);
const std::string &OptName = getOptionHelpName(*this, Id);
GroupedOptionHelp[HelpGroup].push_back({OptName, HelpText});
}
}
for (auto& OptionGroup : GroupedOptionHelp) {
if (OptionGroup.first != GroupedOptionHelp.begin()->first)
OS << "\n";
PrintHelpOptionList(OS, OptionGroup.first, OptionGroup.second);
}
OS.flush();
}