From fee168913c752b1048a2fd48b27c698094462d52 Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Mon, 7 Jul 2025 09:01:42 -0700 Subject: [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_`. --- clang/lib/Basic/LangOptions.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'clang/lib/Basic/LangOptions.cpp') diff --git a/clang/lib/Basic/LangOptions.cpp b/clang/lib/Basic/LangOptions.cpp index 912b890..9c14a25 100644 --- a/clang/lib/Basic/LangOptions.cpp +++ b/clang/lib/Basic/LangOptions.cpp @@ -16,16 +16,19 @@ using namespace clang; LangOptions::LangOptions() : LangStd(LangStandard::lang_unspecified) { -#define LANGOPT(Name, Bits, Default, Description) Name = Default; -#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) set##Name(Default); +#define LANGOPT(Name, Bits, Default, Compatibility, Description) Name = Default; +#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \ + set##Name(Default); #include "clang/Basic/LangOptions.def" } void LangOptions::resetNonModularOptions() { -#define LANGOPT(Name, Bits, Default, Description) -#define BENIGN_LANGOPT(Name, Bits, Default, Description) Name = Default; -#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ - Name = static_cast(Default); +#define LANGOPT(Name, Bits, Default, Compatibility, Description) \ + if constexpr (CompatibilityKind::Compatibility == CompatibilityKind::Benign) \ + Name = Default; +#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \ + if constexpr (CompatibilityKind::Compatibility == CompatibilityKind::Benign) \ + Name = static_cast(Default); #include "clang/Basic/LangOptions.def" // Reset "benign" options with implied values (Options.td ImpliedBy relations) -- cgit v1.1