Skip to content

Commit 9478f66

Browse files
committed
[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
1 parent 3c5b4da commit 9478f66

File tree

23 files changed

+1866
-1120
lines changed

23 files changed

+1866
-1120
lines changed

clang-tools-extra/clangd/CompileCommands.cpp

+7-23
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,8 @@ void CommandMangler::operator()(tooling::CompileCommand &Command,
237237
llvm::opt::InputArgList ArgList;
238238
ArgList = OptTable.ParseArgs(
239239
llvm::ArrayRef(OriginalArgs).drop_front(), IgnoredCount, IgnoredCount,
240-
/*FlagsToInclude=*/
241-
IsCLMode ? (driver::options::CLOption | driver::options::CoreOption |
242-
driver::options::CLDXCOption)
243-
: /*everything*/ 0,
244-
/*FlagsToExclude=*/driver::options::NoDriverOption |
245-
(IsCLMode
246-
? 0
247-
: (driver::options::CLOption | driver::options::CLDXCOption)));
240+
llvm::opt::Visibility(IsCLMode ? driver::options::CLOption
241+
: driver::options::ClangOption));
248242

249243
llvm::SmallVector<unsigned, 1> IndicesToDrop;
250244
// Having multiple architecture options (e.g. when building fat binaries)
@@ -441,23 +435,13 @@ DriverMode getDriverMode(const std::vector<std::string> &Args) {
441435

442436
// Returns the set of DriverModes where an option may be used.
443437
unsigned char getModes(const llvm::opt::Option &Opt) {
444-
// Why is this so complicated?!
445-
// Reference is clang::driver::Driver::getIncludeExcludeOptionFlagMasks()
446438
unsigned char Result = DM_None;
447-
if (Opt.hasFlag(driver::options::CC1Option))
439+
if (Opt.hasVisibilityFlag(driver::options::ClangOption))
440+
Result |= DM_GCC;
441+
if (Opt.hasVisibilityFlag(driver::options::CC1Option))
448442
Result |= DM_CC1;
449-
if (!Opt.hasFlag(driver::options::NoDriverOption)) {
450-
if (Opt.hasFlag(driver::options::CLOption)) {
451-
Result |= DM_CL;
452-
} else if (Opt.hasFlag(driver::options::CLDXCOption)) {
453-
Result |= DM_CL;
454-
} else {
455-
Result |= DM_GCC;
456-
if (Opt.hasFlag(driver::options::CoreOption)) {
457-
Result |= DM_CL;
458-
}
459-
}
460-
}
443+
if (Opt.hasVisibilityFlag(driver::options::CLOption))
444+
Result |= DM_CL;
461445
return Result;
462446
}
463447

clang-tools-extra/modularize/Modularize.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,13 @@ std::string CommandLine;
336336

337337
// Helper function for finding the input file in an arguments list.
338338
static std::string findInputFile(const CommandLineArguments &CLArgs) {
339-
const unsigned IncludedFlagsBitmask = options::CC1Option;
339+
llvm::opt::Visibility VisibilityMask(options::CC1Option);
340340
unsigned MissingArgIndex, MissingArgCount;
341341
SmallVector<const char *, 256> Argv;
342342
for (auto I = CLArgs.begin(), E = CLArgs.end(); I != E; ++I)
343343
Argv.push_back(I->c_str());
344344
InputArgList Args = getDriverOptTable().ParseArgs(
345-
Argv, MissingArgIndex, MissingArgCount, IncludedFlagsBitmask);
345+
Argv, MissingArgIndex, MissingArgCount, VisibilityMask);
346346
std::vector<std::string> Inputs = Args.getAllArgValues(OPT_INPUT);
347347
return ModularizeUtilities::getCanonicalPath(Inputs.back());
348348
}

clang/docs/InternalsManual.rst

+35-22
Original file line numberDiff line numberDiff line change
@@ -663,9 +663,11 @@ Then, specify additional attributes via mix-ins:
663663
* ``HelpText`` holds the text that will be printed besides the option name when
664664
the user requests help (e.g. via ``clang --help``).
665665
* ``Group`` specifies the "category" of options this option belongs to. This is
666-
used by various tools to filter certain options of interest.
667-
* ``Flags`` may contain a number of "tags" associated with the option. This
668-
enables more granular filtering than the ``Group`` attribute.
666+
used by various tools to categorize and sometimes filter options.
667+
* ``Flags`` may contain "tags" associated with the option. These may affect how
668+
the option is rendered, or if it's hidden in some contexts.
669+
* ``Visibility`` should be used to specify the drivers in which a particular
670+
option would be available. This attribute will impact tool --help
669671
* ``Alias`` denotes that the option is an alias of another option. This may be
670672
combined with ``AliasArgs`` that holds the implied value.
671673

@@ -674,12 +676,14 @@ Then, specify additional attributes via mix-ins:
674676
// Options.td
675677
676678
def fpass_plugin_EQ : Joined<["-"], "fpass-plugin=">,
677-
+ Group<f_Group>, Flags<[CC1Option]>,
679+
+ Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
678680
+ HelpText<"Load pass plugin from a dynamic shared object file.">;
679681
680-
New options are recognized by the Clang driver unless marked with the
681-
``NoDriverOption`` flag. On the other hand, options intended for the ``-cc1``
682-
frontend must be explicitly marked with the ``CC1Option`` flag.
682+
New options are recognized by the ``clang`` driver mode if ``Visibility`` is
683+
not specified or contains ``ClangOption``. Options intended for ``clang -cc1``
684+
must be explicitly marked with the ``CC1Option`` flag. Flags that specify
685+
``CC1Option`` but not ``ClangOption`` will only be accessible via ``-cc1``.
686+
This is similar for other driver modes, such as ``clang-cl`` or ``flang``.
683687

684688
Next, parse (or manufacture) the command line arguments in the Clang driver and
685689
use them to construct the ``-cc1`` job:
@@ -874,7 +878,8 @@ present on command line.
874878

875879
.. code-block:: text
876880
877-
def fignore_exceptions : Flag<["-"], "fignore-exceptions">, Flags<[CC1Option]>,
881+
def fignore_exceptions : Flag<["-"], "fignore-exceptions">,
882+
Visibility<[ClangOption, CC1Option]>,
878883
MarshallingInfoFlag<LangOpts<"IgnoreExceptions">>;
879884
880885
**Negative Flag**
@@ -884,7 +889,8 @@ present on command line.
884889

885890
.. code-block:: text
886891
887-
def fno_verbose_asm : Flag<["-"], "fno-verbose-asm">, Flags<[CC1Option]>,
892+
def fno_verbose_asm : Flag<["-"], "fno-verbose-asm">,
893+
Visibility<[ClangOption, CC1Option]>,
888894
MarshallingInfoNegativeFlag<CodeGenOpts<"AsmVerbose">>;
889895
890896
**Negative and Positive Flag**
@@ -898,9 +904,9 @@ line.
898904
899905
defm legacy_pass_manager : BoolOption<"f", "legacy-pass-manager",
900906
CodeGenOpts<"LegacyPassManager">, DefaultFalse,
901-
PosFlag<SetTrue, [], "Use the legacy pass manager in LLVM">,
902-
NegFlag<SetFalse, [], "Use the new pass manager in LLVM">,
903-
BothFlags<[CC1Option]>>;
907+
PosFlag<SetTrue, [], [], "Use the legacy pass manager in LLVM">,
908+
NegFlag<SetFalse, [], [], "Use the new pass manager in LLVM">,
909+
BothFlags<[], [ClangOption, CC1Option]>>;
904910
905911
With most such pair of flags, the ``-cc1`` frontend accepts only the flag that
906912
changes the default key path value. The Clang driver is responsible for
@@ -912,10 +918,11 @@ full names of both flags. The positive flag would then be named
912918
``flegacy-pass-manager`` and the negative ``fno-legacy-pass-manager``.
913919
``BoolOption`` also implies the ``-`` prefix for both flags. It's also possible
914920
to use ``BoolFOption`` that implies the ``"f"`` prefix and ``Group<f_Group>``.
915-
The ``PosFlag`` and ``NegFlag`` classes hold the associated boolean value, an
916-
array of elements passed to the ``Flag`` class and the help text. The optional
917-
``BothFlags`` class holds an array of ``Flag`` elements that are common for both
918-
the positive and negative flag and their common help text suffix.
921+
The ``PosFlag`` and ``NegFlag`` classes hold the associated boolean value,
922+
arrays of elements passed to the ``Flag`` and ``Visibility`` classes and the
923+
help text. The optional ``BothFlags`` class holds arrays of ``Flag`` and
924+
``Visibility`` elements that are common for both the positive and negative flag
925+
and their common help text suffix.
919926

920927
**String**
921928

@@ -924,7 +931,8 @@ the option appears on the command line, the argument value is simply copied.
924931

925932
.. code-block:: text
926933
927-
def isysroot : JoinedOrSeparate<["-"], "isysroot">, Flags<[CC1Option]>,
934+
def isysroot : JoinedOrSeparate<["-"], "isysroot">,
935+
Visibility<[ClangOption, CC1Option]>,
928936
MarshallingInfoString<HeaderSearchOpts<"Sysroot">, [{"/"}]>;
929937
930938
**List of Strings**
@@ -935,7 +943,8 @@ vector.
935943

936944
.. code-block:: text
937945
938-
def frewrite_map_file : Separate<["-"], "frewrite-map-file">, Flags<[CC1Option]>,
946+
def frewrite_map_file : Separate<["-"], "frewrite-map-file">,
947+
Visibility<[ClangOption, CC1Option]>,
939948
MarshallingInfoStringVector<CodeGenOpts<"RewriteMapFiles">>;
940949
941950
**Integer**
@@ -946,7 +955,8 @@ and the result is assigned to the key path on success.
946955

947956
.. code-block:: text
948957
949-
def mstack_probe_size : Joined<["-"], "mstack-probe-size=">, Flags<[CC1Option]>,
958+
def mstack_probe_size : Joined<["-"], "mstack-probe-size=">,
959+
Visibility<[ClangOption, CC1Option]>,
950960
MarshallingInfoInt<CodeGenOpts<"StackProbeSize">, "4096">;
951961
952962
**Enumeration**
@@ -963,7 +973,8 @@ comma-separated string values and elements of the array within
963973

964974
.. code-block:: text
965975
966-
def mthread_model : Separate<["-"], "mthread-model">, Flags<[CC1Option]>,
976+
def mthread_model : Separate<["-"], "mthread-model">,
977+
Visibility<[ClangOption, CC1Option]>,
967978
Values<"posix,single">, NormalizedValues<["POSIX", "Single"]>,
968979
NormalizedValuesScope<"LangOptions::ThreadModelKind">,
969980
MarshallingInfoEnum<LangOpts<"ThreadModel">, "POSIX">;
@@ -983,7 +994,8 @@ Finally, the command line is parsed according to the primary annotation.
983994

984995
.. code-block:: text
985996
986-
def fms_extensions : Flag<["-"], "fms-extensions">, Flags<[CC1Option]>,
997+
def fms_extensions : Flag<["-"], "fms-extensions">,
998+
Visibility<[ClangOption, CC1Option]>,
987999
MarshallingInfoFlag<LangOpts<"MicrosoftExt">>,
9881000
ImpliedByAnyOf<[fms_compatibility.KeyPath], "true">;
9891001
@@ -994,7 +1006,8 @@ true.
9941006

9951007
.. code-block:: text
9961008
997-
def fopenmp_enable_irbuilder : Flag<["-"], "fopenmp-enable-irbuilder">, Flags<[CC1Option]>,
1009+
def fopenmp_enable_irbuilder : Flag<["-"], "fopenmp-enable-irbuilder">,
1010+
Visibility<[ClangOption, CC1Option]>,
9981011
MarshallingInfoFlag<LangOpts<"OpenMPIRBuilder">>,
9991012
ShouldParseIf<fopenmp.KeyPath>;
10001013

clang/include/clang/Driver/ClangOptionDocs.td

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ GCC-compatible ``clang`` and ``clang++`` drivers.
2828
}];
2929

3030
string Program = "clang";
31-
list<string> ExcludedFlags = ["HelpHidden", "NoDriverOption",
32-
"CLOption", "Unsupported", "Ignored", "FlangOnlyOption"];
31+
// Note: We *must* use DefaultVis and not ClangOption, since that's
32+
// the name of the actual TableGen record. The alias will not work.
33+
list<string> VisibilityMask = ["DefaultVis"];
34+
list<string> IgnoreFlags = ["HelpHidden", "Unsupported", "Ignored"];
3335
}
3436

3537
include "Options.td"

clang/include/clang/Driver/Driver.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ class Driver {
455455
/// ParseArgStrings - Parse the given list of strings into an
456456
/// ArgList.
457457
llvm::opt::InputArgList ParseArgStrings(ArrayRef<const char *> Args,
458-
bool IsClCompatMode,
458+
bool UseDriverMode,
459459
bool &ContainsError);
460460

461461
/// BuildInputs - Construct the list of inputs and their types from
@@ -759,7 +759,8 @@ class Driver {
759759

760760
/// Get bitmasks for which option flags to include and exclude based on
761761
/// the driver mode.
762-
std::pair<unsigned, unsigned> getIncludeExcludeOptionFlagMasks(bool IsClCompatMode) const;
762+
llvm::opt::Visibility
763+
getOptionVisibilityMask(bool UseDriverMode = true) const;
763764

764765
/// Helper used in BuildJobsForAction. Doesn't use the cache when building
765766
/// jobs specifically for the given action, but will use the cache when

clang/include/clang/Driver/Options.h

+9-13
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,22 @@ enum ClangFlags {
2323
LinkerInput = (1 << 5),
2424
NoArgumentUnused = (1 << 6),
2525
Unsupported = (1 << 7),
26-
CoreOption = (1 << 8),
27-
CLOption = (1 << 9),
28-
CC1Option = (1 << 10),
29-
CC1AsOption = (1 << 11),
30-
NoDriverOption = (1 << 12),
31-
LinkOption = (1 << 13),
32-
FlangOption = (1 << 14),
33-
FC1Option = (1 << 15),
34-
FlangOnlyOption = (1 << 16),
35-
DXCOption = (1 << 17),
36-
CLDXCOption = (1 << 18),
37-
Ignored = (1 << 19),
38-
TargetSpecific = (1 << 20),
26+
LinkOption = (1 << 8),
27+
Ignored = (1 << 9),
28+
TargetSpecific = (1 << 10),
3929
};
4030

4131
// Flags specifically for clang option visibility. We alias DefaultVis to
4232
// ClangOption, because "DefaultVis" is confusing in Options.td, which is used
4333
// for multiple drivers (clang, cl, flang, etc).
4434
enum ClangVisibility {
4535
ClangOption = llvm::opt::DefaultVis,
36+
CLOption = (1 << 1),
37+
CC1Option = (1 << 2),
38+
CC1AsOption = (1 << 3),
39+
FlangOption = (1 << 4),
40+
FC1Option = (1 << 5),
41+
DXCOption = (1 << 6),
4642
};
4743

4844
enum ID {

0 commit comments

Comments
 (0)