diff options
author | Jan Svoboda <jan_svoboda@apple.com> | 2025-07-07 09:01:42 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-07 09:01:42 -0700 |
commit | fee168913c752b1048a2fd48b27c698094462d52 (patch) | |
tree | 6fd441fe759b27e76b5cc4cdc501e23b9099c917 /clang/lib/Frontend/FrontendActions.cpp | |
parent | 3c76a054ac4c7c93b197f3f1aec6c9f72ece1377 (diff) | |
download | llvm-fee168913c752b1048a2fd48b27c698094462d52.zip llvm-fee168913c752b1048a2fd48b27c698094462d52.tar.gz llvm-fee168913c752b1048a2fd48b27c698094462d52.tar.bz2 |
[clang] Refactor `LangOptions` to specify compatibility as X macro arg (#146766)
This removes the `{BENIGN,COMPATIBLE}{,_ENUM,_VALUE}_LANGOPT` X macros
controlling `LangOptions`. These are permutations of the base `LANGOPT`,
`ENUM_LANGOPT` and `VALUE_LANGOPT` X macros that also carry the
information of their effect on AST (and therefore module compatibility).
Their functionality is now implemented by passing `Benign`, `Compatible`
or `NotCompatible` argument to the base X macros and using C++17 `if
constexpr` in the clients to achieve the same codegen.
This PR solves this FIXME:
```
// FIXME: Clients should be able to more easily select whether they want
// different levels of compatibility versus how to handle different kinds
// of option.
```
The base X macros are preserved, since they are used in `LangOptions.h`
to generate different kinds of field and function declarations for
flags, values and enums, which can't be achieved with `if constexpr`.
The new syntax also forces developers to think about compatibility when
adding new language option, hopefully reducing the number of new options
that are affecting by default even though they are benign or compatible.
Note that the `BENIGN_` macros used to forward to their `COMPATIBLE_`
counterparts. I don't think this ever kicked in, since there are no
clients of the `.def` file that define `COMPATIBLE_` without also
defining `BENIGN_`. However, this might be something downstream forks
need to take care of by doing `if constexpr (CK::Compatibility ==
CK::Benign || CK::Compatibility == CK::Compatible)` in place of `#define
COMPATIBLE_`.
Diffstat (limited to 'clang/lib/Frontend/FrontendActions.cpp')
-rw-r--r-- | clang/lib/Frontend/FrontendActions.cpp | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/clang/lib/Frontend/FrontendActions.cpp b/clang/lib/Frontend/FrontendActions.cpp index c0ab708..89eb0b6 100644 --- a/clang/lib/Frontend/FrontendActions.cpp +++ b/clang/lib/Frontend/FrontendActions.cpp @@ -643,16 +643,20 @@ namespace { bool ReadLanguageOptions(const LangOptions &LangOpts, StringRef ModuleFilename, bool Complain, bool AllowCompatibleDifferences) override { + // FIXME: Replace with C++20 `using enum LangOptions::CompatibilityKind`. + using CK = LangOptions::CompatibilityKind; + Out.indent(2) << "Language options:\n"; -#define LANGOPT(Name, Bits, Default, Description) \ +#define LANGOPT(Name, Bits, Default, Compatibility, Description) \ + if constexpr (CK::Compatibility != CK::Benign) \ DUMP_BOOLEAN(LangOpts.Name, Description); -#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ - Out.indent(4) << Description << ": " \ +#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \ + if constexpr (CK::Compatibility != CK::Benign) \ + Out.indent(4) << Description << ": " \ << static_cast<unsigned>(LangOpts.get##Name()) << "\n"; -#define VALUE_LANGOPT(Name, Bits, Default, Description) \ +#define VALUE_LANGOPT(Name, Bits, Default, Compatibility, Description) \ + if constexpr (CK::Compatibility != CK::Benign) \ Out.indent(4) << Description << ": " << LangOpts.Name << "\n"; -#define BENIGN_LANGOPT(Name, Bits, Default, Description) -#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) #include "clang/Basic/LangOptions.def" if (!LangOpts.ModuleFeatures.empty()) { |