diff options
author | Justin Bogner <mail@justinbogner.com> | 2023-08-02 16:04:28 -0700 |
---|---|---|
committer | Justin Bogner <mail@justinbogner.com> | 2023-08-15 14:26:40 -0700 |
commit | 9478f661c26fbc22491218477917df5d8d73c51c (patch) | |
tree | f84c6bb1039c8359d6d4c86f3e5fa033a1c68480 /clang/lib | |
parent | 3c5b4dabdc06dd380391ac965b16961610c0db77 (diff) | |
download | llvm-9478f661c26fbc22491218477917df5d8d73c51c.zip llvm-9478f661c26fbc22491218477917df5d8d73c51c.tar.gz llvm-9478f661c26fbc22491218477917df5d8d73c51c.tar.bz2 |
[Driver] Refactor to use llvm Option's new Visibility flags
This is a big refactor of the clang driver's option handling to use
the Visibility flags introduced in https://reviews.llvm.org/D157149.
There are a few distinct parts, but they can't really be split into
separate commits and still be made to compile.
1. We split out some of the flags in ClangFlags to ClangVisibility.
Note that this does not include any subtractive flags.
2. We update the Flag definitions and OptIn/OptOut constructs in
Options.td by hand.
3. We introduce and use a script, update_options_td_flags, to ease
migration of flag definitions in Options.td, and we run that on
Options.td. I intend to remove this later, but I'm committing it so
that downstream forks can use the script to simplify merging.
4. We update calls to OptTable in the clang driver, cc1as, flang, and
clangd to use the visibility APIs instead of Include/Exclude flags.
5. We deprecate the Include/Exclude APIs and add a release note.
*if you are running into conflicts with this change:*
Note that https://reviews.llvm.org/D157150 may also be the culprit and
if so it should be handled first.
The script in `clang/utils/update_options_td_flags.py` can help. Take
the downstream side of all conflicts and then run the following:
```
% cd clang/include/clang/Driver
% ../../../utils/update_options_td_flags.py Options.td > Options.td.new
% mv Options.td.new Options.td
```
This will hopefully be sufficient, please take a look at the diff.
Differential Revision: https://reviews.llvm.org/D157151
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Driver/Driver.cpp | 112 | ||||
-rw-r--r-- | clang/lib/Frontend/CompilerInvocation.cpp | 12 | ||||
-rw-r--r-- | clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp | 4 | ||||
-rw-r--r-- | clang/lib/Tooling/InterpolatingCompilationDatabase.cpp | 3 |
4 files changed, 49 insertions, 82 deletions
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 150040cc..3947ae9 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -254,25 +254,14 @@ void Driver::setDriverMode(StringRef Value) { } InputArgList Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings, - bool IsClCompatMode, - bool &ContainsError) { + bool UseDriverMode, bool &ContainsError) { llvm::PrettyStackTraceString CrashInfo("Command line argument parsing"); ContainsError = false; - unsigned IncludedFlagsBitmask; - unsigned ExcludedFlagsBitmask; - std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) = - getIncludeExcludeOptionFlagMasks(IsClCompatMode); - - // Make sure that Flang-only options don't pollute the Clang output - // TODO: Make sure that Clang-only options don't pollute Flang output - if (!IsFlangMode()) - ExcludedFlagsBitmask |= options::FlangOnlyOption; - + llvm::opt::Visibility VisibilityMask = getOptionVisibilityMask(UseDriverMode); unsigned MissingArgIndex, MissingArgCount; - InputArgList Args = - getOpts().ParseArgs(ArgStrings, MissingArgIndex, MissingArgCount, - IncludedFlagsBitmask, ExcludedFlagsBitmask); + InputArgList Args = getOpts().ParseArgs(ArgStrings, MissingArgIndex, + MissingArgCount, VisibilityMask); // Check for missing argument error. if (MissingArgCount) { @@ -306,10 +295,10 @@ InputArgList Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings, unsigned DiagID; auto ArgString = A->getAsString(Args); std::string Nearest; - if (getOpts().findNearest(ArgString, Nearest, IncludedFlagsBitmask, - ExcludedFlagsBitmask) > 1) { + if (getOpts().findNearest(ArgString, Nearest, VisibilityMask) > 1) { if (!IsCLMode() && - getOpts().findExact(ArgString, Nearest, options::CC1Option)) { + getOpts().findExact(ArgString, Nearest, + llvm::opt::Visibility(options::CC1Option))) { DiagID = diag::err_drv_unknown_argument_with_suggestion; Diags.Report(DiagID) << ArgString << "-Xclang " + Nearest; } else { @@ -334,8 +323,7 @@ InputArgList Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings, // Warn on joined arguments that are similar to a long argument. std::string ArgString = ArgStrings[A->getIndex()]; std::string Nearest; - if (getOpts().findExact("-" + ArgString, Nearest, IncludedFlagsBitmask, - ExcludedFlagsBitmask)) + if (getOpts().findExact("-" + ArgString, Nearest, VisibilityMask)) Diags.Report(diag::warn_drv_potentially_misspelled_joined_argument) << A->getAsString(Args) << Nearest; } @@ -1021,7 +1009,7 @@ bool Driver::readConfigFile(StringRef FileName, llvm::sys::path::native(CfgFileName); bool ContainErrors; std::unique_ptr<InputArgList> NewOptions = std::make_unique<InputArgList>( - ParseArgStrings(NewCfgArgs, IsCLMode(), ContainErrors)); + ParseArgStrings(NewCfgArgs, /*UseDriverMode=*/true, ContainErrors)); if (ContainErrors) return true; @@ -1212,7 +1200,7 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) { // Arguments specified in command line. bool ContainsError; CLOptions = std::make_unique<InputArgList>( - ParseArgStrings(ArgList.slice(1), IsCLMode(), ContainsError)); + ParseArgStrings(ArgList.slice(1), /*UseDriverMode=*/true, ContainsError)); // Try parsing configuration file. if (!ContainsError) @@ -1245,7 +1233,8 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) { // Parse any pass through args using default clang processing rather // than clang-cl processing. auto CLModePassThroughOptions = std::make_unique<InputArgList>( - ParseArgStrings(CLModePassThroughArgList, false, ContainsError)); + ParseArgStrings(CLModePassThroughArgList, /*UseDriverMode=*/false, + ContainsError)); if (!ContainsError) for (auto *Opt : *CLModePassThroughOptions) { @@ -1943,24 +1932,18 @@ int Driver::ExecuteCompilation( } void Driver::PrintHelp(bool ShowHidden) const { - unsigned IncludedFlagsBitmask; - unsigned ExcludedFlagsBitmask; - std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) = - getIncludeExcludeOptionFlagMasks(IsCLMode()); - - ExcludedFlagsBitmask |= options::NoDriverOption; - if (!ShowHidden) - ExcludedFlagsBitmask |= HelpHidden; + llvm::opt::Visibility VisibilityMask = getOptionVisibilityMask(); + // TODO: We're overriding the mask for flang here to keep this NFC for the + // option refactoring, but what we really need to do is annotate the flags + // that Flang uses. if (IsFlangMode()) - IncludedFlagsBitmask |= options::FlangOption; - else - ExcludedFlagsBitmask |= options::FlangOnlyOption; + VisibilityMask = llvm::opt::Visibility(options::FlangOption); std::string Usage = llvm::formatv("{0} [options] file...", Name).str(); getOpts().printHelp(llvm::outs(), Usage.c_str(), DriverTitle.c_str(), - IncludedFlagsBitmask, ExcludedFlagsBitmask, - /*ShowAllAliases=*/false); + ShowHidden, /*ShowAllAliases=*/false, + VisibilityMask); } void Driver::PrintVersion(const Compilation &C, raw_ostream &OS) const { @@ -2008,13 +1991,12 @@ void Driver::HandleAutocompletions(StringRef PassedFlags) const { std::vector<std::string> SuggestedCompletions; std::vector<std::string> Flags; - unsigned int DisableFlags = - options::NoDriverOption | options::Unsupported | options::Ignored; + llvm::opt::Visibility VisibilityMask(options::ClangOption); // Make sure that Flang-only options don't pollute the Clang output // TODO: Make sure that Clang-only options don't pollute Flang output - if (!IsFlangMode()) - DisableFlags |= options::FlangOnlyOption; + if (IsFlangMode()) + VisibilityMask = llvm::opt::Visibility(options::FlangOption); // Distinguish "--autocomplete=-someflag" and "--autocomplete=-someflag," // because the latter indicates that the user put space before pushing tab @@ -2033,7 +2015,7 @@ void Driver::HandleAutocompletions(StringRef PassedFlags) const { // We want to show cc1-only options only when clang is invoked with -cc1 or // -Xclang. if (llvm::is_contained(Flags, "-Xclang") || llvm::is_contained(Flags, "-cc1")) - DisableFlags &= ~options::NoDriverOption; + VisibilityMask = llvm::opt::Visibility(options::CC1Option); const llvm::opt::OptTable &Opts = getOpts(); StringRef Cur; @@ -2063,7 +2045,9 @@ void Driver::HandleAutocompletions(StringRef PassedFlags) const { // If the flag is in the form of "--autocomplete=-foo", // we were requested to print out all option names that start with "-foo". // For example, "--autocomplete=-fsyn" is expanded to "-fsyntax-only". - SuggestedCompletions = Opts.findByPrefix(Cur, DisableFlags); + SuggestedCompletions = Opts.findByPrefix( + Cur, VisibilityMask, + /*DisableFlags=*/options::Unsupported | options::Ignored); // We have to query the -W flags manually as they're not in the OptTable. // TODO: Find a good way to add them to OptTable instead and them remove @@ -2516,13 +2500,8 @@ bool Driver::DiagnoseInputExistence(const DerivedArgList &Args, StringRef Value, // filenames, but e.g. `/diagnostic:caret` is more likely a typo for // the option `/diagnostics:caret` than a reference to a file in the root // directory. - unsigned IncludedFlagsBitmask; - unsigned ExcludedFlagsBitmask; - std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) = - getIncludeExcludeOptionFlagMasks(IsCLMode()); std::string Nearest; - if (getOpts().findNearest(Value, Nearest, IncludedFlagsBitmask, - ExcludedFlagsBitmask) <= 1) { + if (getOpts().findNearest(Value, Nearest, getOptionVisibilityMask()) <= 1) { Diag(clang::diag::err_drv_no_such_file_with_suggestion) << Value << Nearest; return false; @@ -6481,31 +6460,20 @@ bool Driver::GetReleaseVersion(StringRef Str, return false; } -std::pair<unsigned, unsigned> -Driver::getIncludeExcludeOptionFlagMasks(bool IsClCompatMode) const { - unsigned IncludedFlagsBitmask = 0; - unsigned ExcludedFlagsBitmask = options::NoDriverOption; - - if (IsClCompatMode) { - // Include CL and Core options. - IncludedFlagsBitmask |= options::CLOption; - IncludedFlagsBitmask |= options::CLDXCOption; - IncludedFlagsBitmask |= options::CoreOption; - } else { - ExcludedFlagsBitmask |= options::CLOption; - } - if (IsDXCMode()) { - // Include DXC and Core options. - IncludedFlagsBitmask |= options::DXCOption; - IncludedFlagsBitmask |= options::CLDXCOption; - IncludedFlagsBitmask |= options::CoreOption; - } else { - ExcludedFlagsBitmask |= options::DXCOption; +llvm::opt::Visibility +Driver::getOptionVisibilityMask(bool UseDriverMode) const { + if (!UseDriverMode) + return llvm::opt::Visibility(options::ClangOption); + if (IsCLMode()) + return llvm::opt::Visibility(options::CLOption); + if (IsDXCMode()) + return llvm::opt::Visibility(options::DXCOption); + if (IsFlangMode()) { + // TODO: Does flang really want *all* of the clang driver options? + // We probably need to annotate more specifically. + return llvm::opt::Visibility(options::ClangOption | options::FlangOption); } - if (!IsClCompatMode && !IsDXCMode()) - ExcludedFlagsBitmask |= options::CLDXCOption; - - return std::make_pair(IncludedFlagsBitmask, ExcludedFlagsBitmask); + return llvm::opt::Visibility(options::ClangOption); } const char *Driver::getExecutableForDriverMode(DriverMode Mode) { diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 0d7a497..a35f6aa 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -426,7 +426,7 @@ static T extractMaskValue(T KeyPath) { FLAGS, VISIBILITY, PARAM, HELPTEXT, METAVAR, VALUES, SHOULD_PARSE, \ ALWAYS_EMIT, KEYPATH, DEFAULT_VALUE, IMPLIED_CHECK, IMPLIED_VALUE, \ NORMALIZER, DENORMALIZER, MERGER, EXTRACTOR, TABLE_INDEX) \ - if ((FLAGS)&options::CC1Option) { \ + if ((VISIBILITY)&options::CC1Option) { \ KEYPATH = MERGER(KEYPATH, DEFAULT_VALUE); \ if (IMPLIED_CHECK) \ KEYPATH = MERGER(KEYPATH, IMPLIED_VALUE); \ @@ -440,10 +440,10 @@ static T extractMaskValue(T KeyPath) { // with lifetime extension of the reference. #define GENERATE_OPTION_WITH_MARSHALLING( \ CONSUMER, PREFIX_TYPE, SPELLING, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, \ - VISIBILKITY, PARAM, HELPTEXT, METAVAR, VALUES, SHOULD_PARSE, ALWAYS_EMIT, \ + VISIBILITY, PARAM, HELPTEXT, METAVAR, VALUES, SHOULD_PARSE, ALWAYS_EMIT, \ KEYPATH, DEFAULT_VALUE, IMPLIED_CHECK, IMPLIED_VALUE, NORMALIZER, \ DENORMALIZER, MERGER, EXTRACTOR, TABLE_INDEX) \ - if ((FLAGS)&options::CC1Option) { \ + if ((VISIBILITY)&options::CC1Option) { \ [&](const auto &Extracted) { \ if (ALWAYS_EMIT || \ (Extracted != \ @@ -4361,10 +4361,10 @@ bool CompilerInvocation::CreateFromArgsImpl( // Parse the arguments. const OptTable &Opts = getDriverOptTable(); - const unsigned IncludedFlagsBitmask = options::CC1Option; + llvm::opt::Visibility VisibilityMask(options::CC1Option); unsigned MissingArgIndex, MissingArgCount; InputArgList Args = Opts.ParseArgs(CommandLineArgs, MissingArgIndex, - MissingArgCount, IncludedFlagsBitmask); + MissingArgCount, VisibilityMask); LangOptions &LangOpts = *Res.getLangOpts(); // Check for missing argument error. @@ -4376,7 +4376,7 @@ bool CompilerInvocation::CreateFromArgsImpl( for (const auto *A : Args.filtered(OPT_UNKNOWN)) { auto ArgString = A->getAsString(Args); std::string Nearest; - if (Opts.findNearest(ArgString, Nearest, IncludedFlagsBitmask) > 1) + if (Opts.findNearest(ArgString, Nearest, VisibilityMask) > 1) Diags.Report(diag::err_drv_unknown_argument) << ArgString; else Diags.Report(diag::err_drv_unknown_argument_with_suggestion) diff --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp index 310f677..81145d2 100644 --- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp @@ -201,8 +201,8 @@ bool ExecuteCompilerInvocation(CompilerInstance *Clang) { driver::getDriverOptTable().printHelp( llvm::outs(), "clang -cc1 [options] file...", "LLVM 'Clang' Compiler: http://clang.llvm.org", - /*Include=*/driver::options::CC1Option, - /*Exclude=*/0, /*ShowAllAliases=*/false); + /*ShowHidden=*/false, /*ShowAllAliases=*/false, + llvm::opt::Visibility(driver::options::CC1Option)); return true; } diff --git a/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp b/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp index ff9cb4b..995019c 100644 --- a/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp +++ b/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp @@ -164,8 +164,7 @@ struct TransferableCommand { const unsigned OldPos = Pos; std::unique_ptr<llvm::opt::Arg> Arg(OptTable.ParseOneArg( ArgList, Pos, - /* Include */ ClangCLMode ? CoreOption | CLOption | CLDXCOption : 0, - /* Exclude */ ClangCLMode ? 0 : CLOption | CLDXCOption)); + llvm::opt::Visibility(ClangCLMode ? CLOption : ClangOption))); if (!Arg) continue; |