aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Option/OptTable.cpp
diff options
context:
space:
mode:
authorJustin Bogner <mail@justinbogner.com>2023-07-21 14:08:19 -0700
committerJustin Bogner <mail@justinbogner.com>2023-08-14 13:24:54 -0700
commita16104e6da6f36f3d72dbf53d10ba56495a0d65a (patch)
tree3e46ec9acbc48fd1d8676390149359a8af123217 /llvm/lib/Option/OptTable.cpp
parentf50eaea8ce1342732fdc02d494e077aaafc3b3e4 (diff)
downloadllvm-a16104e6da6f36f3d72dbf53d10ba56495a0d65a.zip
llvm-a16104e6da6f36f3d72dbf53d10ba56495a0d65a.tar.gz
llvm-a16104e6da6f36f3d72dbf53d10ba56495a0d65a.tar.bz2
[Option] Add "Visibility" field and clone the OptTable APIs to use it
This splits OptTable's "Flags" field into "Flags" and "Visibility", updates the places where we instantiate Option tables, and adds variants of the OptTable APIs that use Visibility mask instead of Include/Exclude flags. We need to do this to clean up a bunch of complexity in the clang driver's option handling - there's a whole slew of flags like CoreOption, NoDriverOption, and FlangOnlyOption there today to try to handle all of the permutations of flags that the various drivers need, but it really doesn't scale well, as can be seen by things like the somewhat recently introduced CLDXCOption. Instead, we'll provide an additive model for visibility that's separate from the other flags. For things like "HelpHidden", which is used as a "subtractive" modifier for option visibility, we leave that in "Flags" and handle it as a special case. Note that we don't actually update the users of the Include/Exclude APIs here or change the flags that exist in clang at all - that will come in a follow up that refactors clang's Options.td to use the increased flexibility this change allows. Differential Revision: https://reviews.llvm.org/D157149
Diffstat (limited to 'llvm/lib/Option/OptTable.cpp')
-rw-r--r--llvm/lib/Option/OptTable.cpp128
1 files changed, 108 insertions, 20 deletions
diff --git a/llvm/lib/Option/OptTable.cpp b/llvm/lib/Option/OptTable.cpp
index 3d7bef8..baa537a 100644
--- a/llvm/lib/Option/OptTable.cpp
+++ b/llvm/lib/Option/OptTable.cpp
@@ -226,9 +226,35 @@ OptTable::findByPrefix(StringRef Cur, unsigned int DisableFlags) const {
}
unsigned OptTable::findNearest(StringRef Option, std::string &NearestString,
+ Visibility VisibilityMask,
+ unsigned MinimumLength,
+ unsigned MaximumDistance) const {
+ return internalFindNearest(
+ Option, NearestString, MinimumLength, MaximumDistance,
+ [VisibilityMask](const Info &CandidateInfo) {
+ return (CandidateInfo.Visibility & VisibilityMask) == 0;
+ });
+}
+
+unsigned OptTable::findNearest(StringRef Option, std::string &NearestString,
unsigned FlagsToInclude, unsigned FlagsToExclude,
unsigned MinimumLength,
unsigned MaximumDistance) const {
+ return internalFindNearest(
+ Option, NearestString, MinimumLength, MaximumDistance,
+ [FlagsToInclude, FlagsToExclude](const Info &CandidateInfo) {
+ if (FlagsToInclude && !(CandidateInfo.Flags & FlagsToInclude))
+ return true;
+ if (CandidateInfo.Flags & FlagsToExclude)
+ return true;
+ return false;
+ });
+}
+
+unsigned OptTable::internalFindNearest(
+ StringRef Option, std::string &NearestString, unsigned MinimumLength,
+ unsigned MaximumDistance,
+ std::function<bool(const Info &)> ExcludeOption) const {
assert(!Option.empty());
// Consider each [option prefix + option name] pair as a candidate, finding
@@ -248,12 +274,8 @@ unsigned OptTable::findNearest(StringRef Option, std::string &NearestString,
if (CandidateName.size() < MinimumLength)
continue;
- // * If FlagsToInclude were specified, ignore options that don't include
- // those flags.
- if (FlagsToInclude && !(CandidateInfo.Flags & FlagsToInclude))
- continue;
- // * Ignore options that contain the FlagsToExclude.
- if (CandidateInfo.Flags & FlagsToExclude)
+ // Ignore options that are excluded via masks
+ if (ExcludeOption(CandidateInfo))
continue;
// * Ignore positional argument option candidates (which do not
@@ -315,8 +337,8 @@ unsigned OptTable::findNearest(StringRef Option, std::string &NearestString,
// Parse a single argument, return the new argument, and update Index. If
// GroupedShortOptions is true, -a matches "-abc" and the argument in Args will
-// be updated to "-bc". This overload does not support
-// FlagsToInclude/FlagsToExclude or case insensitive options.
+// be updated to "-bc". This overload does not support VisibilityMask or case
+// insensitive options.
std::unique_ptr<Arg> OptTable::parseOneArgGrouped(InputArgList &Args,
unsigned &Index) const {
// Anything that doesn't start with PrefixesUnion is an input, as is '-'
@@ -381,8 +403,28 @@ std::unique_ptr<Arg> OptTable::parseOneArgGrouped(InputArgList &Args,
}
std::unique_ptr<Arg> OptTable::ParseOneArg(const ArgList &Args, unsigned &Index,
+ Visibility VisibilityMask) const {
+ return internalParseOneArg(Args, Index, [VisibilityMask](const Option &Opt) {
+ return !Opt.hasVisibilityFlag(VisibilityMask);
+ });
+}
+
+std::unique_ptr<Arg> OptTable::ParseOneArg(const ArgList &Args, unsigned &Index,
unsigned FlagsToInclude,
unsigned FlagsToExclude) const {
+ return internalParseOneArg(
+ Args, Index, [FlagsToInclude, FlagsToExclude](const Option &Opt) {
+ if (FlagsToInclude && !Opt.hasFlag(FlagsToInclude))
+ return true;
+ if (Opt.hasFlag(FlagsToExclude))
+ return true;
+ return false;
+ });
+}
+
+std::unique_ptr<Arg> OptTable::internalParseOneArg(
+ const ArgList &Args, unsigned &Index,
+ std::function<bool(const Option &)> ExcludeOption) const {
unsigned Prev = Index;
StringRef Str = Args.getArgString(Index);
@@ -418,9 +460,7 @@ std::unique_ptr<Arg> OptTable::ParseOneArg(const ArgList &Args, unsigned &Index,
Option Opt(Start, this);
- if (FlagsToInclude && !Opt.hasFlag(FlagsToInclude))
- continue;
- if (Opt.hasFlag(FlagsToExclude))
+ if (ExcludeOption(Opt))
continue;
// See if this option matches.
@@ -444,11 +484,37 @@ std::unique_ptr<Arg> OptTable::ParseOneArg(const ArgList &Args, unsigned &Index,
Str.data());
}
-InputArgList OptTable::ParseArgs(ArrayRef<const char *> ArgArr,
+InputArgList OptTable::ParseArgs(ArrayRef<const char *> Args,
+ unsigned &MissingArgIndex,
+ unsigned &MissingArgCount,
+ Visibility VisibilityMask) const {
+ return internalParseArgs(
+ Args, MissingArgIndex, MissingArgCount,
+ [VisibilityMask](const Option &Opt) {
+ return !Opt.hasVisibilityFlag(VisibilityMask);
+ });
+}
+
+InputArgList OptTable::ParseArgs(ArrayRef<const char *> Args,
unsigned &MissingArgIndex,
unsigned &MissingArgCount,
unsigned FlagsToInclude,
unsigned FlagsToExclude) const {
+ return internalParseArgs(
+ Args, MissingArgIndex, MissingArgCount,
+ [FlagsToInclude, FlagsToExclude](const Option &Opt) {
+ if (FlagsToInclude && !Opt.hasFlag(FlagsToInclude))
+ return true;
+ if (Opt.hasFlag(FlagsToExclude))
+ return true;
+ return false;
+ });
+}
+
+InputArgList OptTable::internalParseArgs(
+ ArrayRef<const char *> ArgArr, unsigned &MissingArgIndex,
+ unsigned &MissingArgCount,
+ std::function<bool(const Option &)> ExcludeOption) const {
InputArgList Args(ArgArr.begin(), ArgArr.end());
// FIXME: Handle '@' args (or at least error on them).
@@ -481,7 +547,7 @@ InputArgList OptTable::ParseArgs(ArrayRef<const char *> ArgArr,
unsigned Prev = Index;
std::unique_ptr<Arg> A = GroupedShortOptions
? parseOneArgGrouped(Args, Index)
- : ParseOneArg(Args, Index, FlagsToInclude, FlagsToExclude);
+ : internalParseOneArg(Args, Index, ExcludeOption);
assert((Index > Prev || GroupedShortOptions) &&
"Parser failed to consume argument.");
@@ -502,7 +568,7 @@ InputArgList OptTable::ParseArgs(ArrayRef<const char *> ArgArr,
InputArgList OptTable::parseArgs(int Argc, char *const *Argv,
OptSpecifier Unknown, StringSaver &Saver,
- function_ref<void(StringRef)> ErrorFn) const {
+ std::function<void(StringRef)> ErrorFn) const {
SmallVector<const char *, 0> NewArgv;
// The environment variable specifies initial options which can be overridden
// by commnad line options.
@@ -626,14 +692,35 @@ static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
}
void OptTable::printHelp(raw_ostream &OS, const char *Usage, const char *Title,
- bool ShowHidden, bool ShowAllAliases) const {
- printHelp(OS, Usage, Title, /*Include*/ 0, /*Exclude*/
- (ShowHidden ? 0 : HelpHidden), ShowAllAliases);
+ bool ShowHidden, bool ShowAllAliases,
+ Visibility VisibilityMask) const {
+ return internalPrintHelp(
+ OS, Usage, Title, ShowHidden, ShowAllAliases,
+ [VisibilityMask](const Info &CandidateInfo) -> bool {
+ return (CandidateInfo.Visibility & VisibilityMask) == 0;
+ });
}
void OptTable::printHelp(raw_ostream &OS, const char *Usage, const char *Title,
unsigned FlagsToInclude, unsigned FlagsToExclude,
bool ShowAllAliases) const {
+ bool ShowHidden = !(FlagsToExclude & HelpHidden);
+ FlagsToExclude &= ~HelpHidden;
+ return internalPrintHelp(
+ OS, Usage, Title, ShowHidden, ShowAllAliases,
+ [FlagsToInclude, FlagsToExclude](const Info &CandidateInfo) {
+ if (FlagsToInclude && !(CandidateInfo.Flags & FlagsToInclude))
+ return true;
+ if (CandidateInfo.Flags & FlagsToExclude)
+ return true;
+ return false;
+ });
+}
+
+void OptTable::internalPrintHelp(
+ raw_ostream &OS, const char *Usage, const char *Title, bool ShowHidden,
+ bool ShowAllAliases,
+ std::function<bool(const Info &)> ExcludeOption) const {
OS << "OVERVIEW: " << Title << "\n\n";
OS << "USAGE: " << Usage << "\n\n";
@@ -646,10 +733,11 @@ void OptTable::printHelp(raw_ostream &OS, const char *Usage, const char *Title,
if (getOptionKind(Id) == Option::GroupClass)
continue;
- unsigned Flags = getInfo(Id).Flags;
- if (FlagsToInclude && !(Flags & FlagsToInclude))
+ const Info &CandidateInfo = getInfo(Id);
+ if (!ShowHidden && (CandidateInfo.Flags & opt::HelpHidden))
continue;
- if (Flags & FlagsToExclude)
+
+ if (ExcludeOption(CandidateInfo))
continue;
// If an alias doesn't have a help text, show a help text for the aliased