Hi, This patch implements clang's __has_feature and __has_extension in GCC. This is a v2 of the original RFC posted here: https://gcc.gnu.org/pipermail/gcc-patches/2023-May/617878.html Changes since v1: - Follow the clang behaviour where -pedantic-errors means that __has_extension behaves exactly like __has_feature. - We're now more conservative with reporting C++ features as extensions available in C++98. For features where we issue a pedwarn in C++98 mode, we no longer report these as available extensions for C++98. - Switch to using a hash_map to store the features. As well as ensuring lookup is constant time, this allows us to dynamically register features (right now based on frontend, but later we could allow the target to register additional features). - Also implement some Objective-C features, add a langhook to dispatch to each frontend to allow it to register language-specific features. There is an outstanding question around what to do with cxx_binary_literals in the C frontend for C2x. Should we introduce a new c_binary_literals feature that is a feature in C2x and an extension below that, or should we just continue using the cxx_binary_literals feature and mark that as a standard feature in C2x? See the comment in c_feature_table in the patch. There is also some doubt over what to do with the undocumented "tls" feature. In clang this is gated on whether the target supports TLS, but in clang (unlike GCC) it is a hard error to use TLS when the target doesn't support it. In GCC I believe you can always use TLS, you just get emulated TLS in the case that the target doesn't support it natively. So in this patch GCC always reports having the "tls" feature. Would appreciate if anyone has feedback on this aspect. I know Iain was concerned that it should be possible to have target-specific features. Hopefully it is clear that the design in this patch is more amenable in this. I think for Darwin it should be possible to add a targetcm hook to register additional features (either passing through a callback to allow the target code to add to the hash_map, or exposing a separate langhook that the target can call to register features). Bootstrapped/regtested on aarch64-linux-gnu and x86_64-apple-darwin. Any thoughts? Thanks, Alex ------ Co-Authored-By: Iain Sandoe gcc/c-family/ChangeLog: PR c++/60512 * c-common.cc (struct hf_feature_info): New. (struct hf_table_entry): New. (hf_generic_predicate): New. (c_common_register_feature): New. (init_has_feature): New. (has_feature_p): New. * c-common.h (c_common_has_feature): New. (has_feature_p): New. (c_common_register_feature): New. (c_register_features): New. (cp_register_features): New. * c-lex.cc (init_c_lex): Plumb through has_feature callback. (c_common_has_builtin): Generalize and move common part ... (c_common_lex_availability_macro): ... here. (c_common_has_feature): New. * c-ppoutput.cc (init_pp_output): Plumb through has_feature. gcc/c/ChangeLog: PR c++/60512 * c-lang.cc (LANG_HOOKS_REGISTER_FEATURES): Implement with c_register_features. * c-objc-common.cc (struct c_feature_info): New. (c_has_feature): New. (c_register_features): New. gcc/cp/ChangeLog: PR c++/60512 * cp-lang.cc (LANG_HOOKS_REGISTER_FEATURES): Implement with cp_register_features. * cp-objcp-common.cc (struct cp_feature_selector): New. (cp_feature_selector::has_feature): New. (struct cp_feature_info): New. (cp_has_feature): New. (cp_register_features): New. gcc/ChangeLog: PR c++/60512 * doc/cpp.texi: Document __has_{feature,extension}. * langhooks-def.h (LANG_HOOKS_REGISTER_FEATURES): New. (LANG_HOOKS_INITIALIZER): Add LANG_HOOKS_REGISTER_FEATURES. * langhooks.h (struct lang_hooks): Add register_features hook. gcc/objc/ChangeLog: PR c++/60512 * objc-act.cc (struct objc_feature_info): New. (objc_nonfragile_abi_p): New. (objc_has_feature): New. (objc_common_register_features): New. * objc-act.h (objc_register_features): New. (objc_common_register_features): New. * objc-lang.cc (LANG_HOOKS_REGISTER_FEATURES): Implement with objc_register_features. (objc_register_features): New. gcc/objcp/ChangeLog: PR c++/60512 * objcp-lang.cc (objcxx_register_features): New. (LANG_HOOKS_REGISTER_FEATURES): Implement with objcxx_register_features. libcpp/ChangeLog: PR c++/60512 * include/cpplib.h (struct cpp_callbacks): Add has_feature. (enum cpp_builtin_type): Add BT_HAS_{FEATURE,EXTENSION}. * init.cc: Add __has_{feature,extension}. * macro.cc (_cpp_builtin_macro_text): Handle BT_HAS_{FEATURE,EXTENSION}. gcc/testsuite/ChangeLog: PR c++/60512 * c-c++-common/has-feature-common.c: New test. * g++.dg/ext/has-feature.C: New test. * gcc.dg/asan/has-feature-asan.c: New test. * gcc.dg/has-feature.c: New test. * gcc.dg/ubsan/has-feature-ubsan.c: New test. * obj-c++.dg/has-feature.mm: New test. * objc.dg/has-feature.m: New test.