On 5/9/23 08:07, Alex Coplan wrote:
This patch implements clang's __has_feature and __has_extension in GCC.
Thanks!
Currently the patch aims to implement all documented features (and some
undocumented ones) following the documentation at
https://clang.llvm.org/docs/LanguageExtensions.html with the following
omissions:
- C++ type traits.
- Objective-C-specific features.
C++ type traits aren't currently implemented since, as the clang
documentation notes, __has_builtin is the correct "modern" way to query
for these (which GCC already implements). Of course there's an argument
that we should recognize the legacy set of C++ type traits that can be
queried through __has_feature for backwards compatibility with older
code. I'm happy to do this if reviewers think that's a good idea.
That seems unnecessary unless there's a specific motivation.
There are some comments in the patch marked with XXX, I'm looking for
review comments from C/C++ maintainers on those areas in particular.
Bootstrapped/regtested on aarch64-linux-gnu. Any comments?
All the has_*_feature_p functions need to check flag_pedantic_errors,
for compatibility with the Clang documented behavior "If the
-pedantic-errors option is given, __has_extension is equivalent to
__has_feature."
+static const cp_feature_info cp_feature_table[] =
+{
+ { "cxx_exceptions", &flag_exceptions },
+ { "cxx_rtti", &flag_rtti },
+ { "cxx_access_control_sfinae", { cxx11, cxx98 } },
+ { "cxx_alias_templates", cxx11 },
+ { "cxx_alignas", cxx11 },
+ { "cxx_alignof", cxx11 },
+ { "cxx_attributes", cxx11 },
+ { "cxx_constexpr", cxx11 },
+ { "cxx_constexpr_string_builtins", cxx11 },
+ { "cxx_decltype", cxx11 },
+ { "cxx_decltype_incomplete_return_types", cxx11 },
+ { "cxx_default_function_template_args", cxx11 },
+ { "cxx_defaulted_functions", cxx11 }, /* XXX: extension in c++98? */
I'm not sure I see the benefit of advertising a lot of these as C++98
extensions, even if we do accept them with a pedwarn by default. The
ones that indicate DRs like cxx_access_control_sfinae, yes, but I'm
inclined to be conservative if it isn't an extension that libstdc++
relies on, like variadic templates or inline namespaces. My concern is
that important implementation is limited to C++11 mode even if we don't
immediately give an error. For instance,
struct A
{
int i = 42;
A() = default;
};
breaks in C++98 mode; even though we only warn for the two C++11
features, trying to actually combine them fails.
So if there's a question, let's say no.
+ { "cxx_delegating_constructors", { cxx11, cxx98 } },
+ { "cxx_deleted_functions", cxx11 },
+ { "cxx_explicit_conversions", { cxx11, cxx98 } },
+ { "cxx_generalized_initializers", cxx11 },
+ { "cxx_implicit_moves", cxx11 },
+ { "cxx_inheriting_constructors", cxx11 }, /* XXX: extension in c++98? */
+ { "cxx_inline_namespaces", { cxx11, cxx98 } },
+ { "cxx_lambdas", cxx11 }, /* XXX: extension in c++98? */
+ { "cxx_local_type_template_args", cxx11 },
+ { "cxx_noexcept", cxx11 },
+ { "cxx_nonstatic_member_init", { cxx11, cxx98 } },
+ { "cxx_nullptr", cxx11 },
+ { "cxx_override_control", { cxx11, cxx98 } },
+ { "cxx_reference_qualified_functions", cxx11 },
+ { "cxx_range_for", cxx11 },
+ { "cxx_raw_string_literals", cxx11 },
+ { "cxx_rvalue_references", cxx11 },
+ { "cxx_static_assert", cxx11 },
+ { "cxx_thread_local", cxx11 },
+ { "cxx_auto_type", cxx11 },
+ { "cxx_strong_enums", cxx11 },
+ { "cxx_trailing_return", cxx11 },
+ { "cxx_unicode_literals", cxx11 },
+ { "cxx_unrestricted_unions", cxx11 },
+ { "cxx_user_literals", cxx11 },
+ { "cxx_variadic_templates", { cxx11, cxx98 } },
+ { "cxx_binary_literals", { cxx14, cxx98 } },
+ { "cxx_contextual_conversions", { cxx14, cxx98 } },
+ { "cxx_decltype_auto", cxx14 },
+ { "cxx_aggregate_nsdmi", cxx14 },
+ { "cxx_init_captures", { cxx14, cxx11 } },
+ { "cxx_generic_lambdas", cxx14 },
+ { "cxx_relaxed_constexpr", cxx14 },
+ { "cxx_return_type_deduction", cxx14 },
+ { "cxx_variable_templates", { cxx14, cxx98 } },
+ { "modules", &flag_modules },