aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp
AgeCommit message (Collapse)AuthorFilesLines
2023-11-29c++: Fix a compile time memory leak in finish_static_assertJakub Jelinek1-0/+1
On Tue, Nov 28, 2023 at 11:31:48AM -0500, Jason Merrill wrote: > Jonathan pointed out elsewhere that this gets leaked if error return > prevents us from getting to the XDELETEVEC. As there is a single error return in which it can leak, I've just added a XDELETEVEC (buf); statement to that path rather than introducing some RAII solution. 2023-11-29 Jakub Jelinek <jakub@redhat.com> * semantics.cc (finish_static_assert): Free buf on error return.
2023-11-29call maybe_return_this in build_cloneAlexandre Oliva3-4/+22
__dt_base doesn't get its body from a maybe_return_this caller, it's rather cloned with the full body within build_clone, and then it's left alone, without going through finish_function_body or build_delete_destructor_body, that call maybe_return_this. Now, this is correct as far as the generated code is concerned, since the cloned body of a cdtor that returns this is also a cdtor body that returns this. The problem is that the decl for THIS is also cloned, and it doesn't get the warning suppression introduced by maybe_return_this, so Wuse-after-free3.C fails with an excess warning at the closing brace of the dtor body. I've split out the warning suppression from maybe_return_this, and arranged to call that bit from the relevant build_clone case. Unfortunately, because the warning is silenced for all uses of the THIS decl, rather than only for the ABI-mandated return stmt, this also silences the very warning that the testcase checks for. I'm not revamping the warning suppression approach to overcome this, so I'm xfailing the expected warning on ARM EABI, hoping that's the only target with cdtor_return_this, and leaving it at that. for gcc/cp/ChangeLog * decl.cc (maybe_prepare_return_this): Split out of... (maybe_return_this): ... this. * cp-tree.h (maybe_prepare_return_this): Declare. * class.cc (build_clone): Call it. for gcc/testsuite/ChangeLog * g++.dg/warn/Wuse-after-free3.C: xfail on arm_eabi.
2023-11-29c++: for contracts, cdtors never return thisAlexandre Oliva1-1/+5
When targetm.cxx.cdtor_return_this() holds, cdtors have a non-VOID_TYPE_P result, but IMHO this ABI implementation detail shouldn't leak to the abstract language conceptual framework, in which cdtors don't have return values. For contracts, specifically those that establish postconditions on results, such a leakage is present, and the present patch puts an end to it: with it, cdtors get an error for result postconditions regardless of the ABI. This fixes g++.dg/contracts/contracts-ctor-dtor2.C on arm-eabi. for gcc/cp/ChangeLog * contracts.cc (check_postcondition_result): Cope with cdtor_return_this.
2023-11-29Daily bump.GCC Administrator1-0/+14
2023-11-28c++: prvalue array decay [PR94264]Jason Merrill2-11/+3
My change for PR53220 made array to pointer decay for prvalue arrays ill-formed to catch well-defined C code that produces a dangling pointer in C++ due to the shorter lifetime of compound literals. This wasn't really correct, but wasn't a problem until C++17 added prvalue arrays, at which point it started rejecting valid C++ code. I wanted to make sure that we still diagnose the problematic code; -Wdangling-pointer covers the array-lit.c case, but I needed to extend -Wreturn-local-addr to handle the return case. PR c++/94264 PR c++/53220 gcc/c/ChangeLog: * c-typeck.cc (array_to_pointer_conversion): Adjust -Wc++-compat diagnostic. gcc/cp/ChangeLog: * call.cc (convert_like_internal): Remove obsolete comment. * typeck.cc (decay_conversion): Allow array prvalue. (maybe_warn_about_returning_address_of_local): Check for returning pointer to temporary. gcc/testsuite/ChangeLog: * c-c++-common/array-lit.c: Adjust. * g++.dg/cpp1z/array-prvalue1.C: New test. * g++.dg/ext/complit17.C: New test.
2023-11-28c++: Fix up __has_extension (cxx_init_captures)Jakub Jelinek1-1/+1
On Mon, Nov 27, 2023 at 10:58:04AM +0000, Alex Coplan wrote: > Many thanks both for the reviews, this is now pushed (with Jason's > above changes implemented) as g:06280a906cb3dc80cf5e07cf3335b758848d488d. The new test FAILs everywhere with GXX_TESTSUITE_STDS=98,11,14,17,20,2b I'm normally using for testing. FAIL: g++.dg/ext/has-feature.C -std=gnu++11 (test for excess errors) Excess errors: /home/jakub/src/gcc/gcc/testsuite/g++.dg/ext/has-feature.C:185:2: error: #error This is on #if __has_extension (cxx_init_captures) != CXX11 #error #endif Comparing the values with clang++ on godbolt and with what is actually implemented: void foo () { auto a = [b = 3]() { return b; }; } both clang++ and GCC implement init captures as extension already in C++11 (and obviously not in C++98 because lambdas aren't implemented there), unless -pedantic-errors/-Werror=pedantic, so I think we should change the FE to match the test rather than the other way around. Making __has_extension return __has_feature for -pedantic-errors and not for -Werror=pedantic is just weird, but as that is what clang++ implements and this is for compatibility with it, I can live with it (but perhaps we should mention it in the documentation). Note, the warnings/errors can be changed using pragmas inside of the source, so whether one can use an extension or not depends on where in the code it is (__extension__ to the rescue if it can be specified around it). I wonder if the has-feature.C test shouldn't be #included in other 2 tests, one where -pedantic-errors would be in dg-options and through some macro tell the file that __has_extension will behave like __has_feature, and another with -Werror=pedantic to document that the option doesn't change it. 2023-11-28 Jakub Jelinek <jakub@redhat.com> * cp-objcp-common.cc (cp_feature_table): Evaluate __has_extension (cxx_init_captures) to 1 even for -std=c++11.
2023-11-28Daily bump.GCC Administrator1-0/+11
2023-11-27c-family: Implement __has_feature and __has_extension [PR60512]Alex Coplan3-0/+154
This patch implements clang's __has_feature and __has_extension in GCC. 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 exception of the legacy features for C++ type traits. These are omitted, since as the clang documentation notes, __has_builtin is the correct "modern" way to query for these (which GCC already implements). gcc/c-family/ChangeLog: PR c++/60512 * c-common.cc (struct hf_feature_info): New. (c_common_register_feature): New. (init_has_feature): New. (has_feature_p): New. * c-common.h (c_common_has_feature): New. (c_family_register_lang_features): New. (c_common_register_feature): New. (has_feature_p): 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 (c_family_register_lang_features): New. * c-objc-common.cc (struct c_feature_info): New. (c_register_features): New. * c-objc-common.h (c_register_features): New. gcc/cp/ChangeLog: PR c++/60512 * cp-lang.cc (c_family_register_lang_features): New. * cp-objcp-common.cc (struct cp_feature_selector): New. (cp_feature_selector::has_feature): New. (struct cp_feature_info): New. (cp_register_features): New. * cp-objcp-common.h (cp_register_features): New. gcc/ChangeLog: PR c++/60512 * doc/cpp.texi: Document __has_{feature,extension}. gcc/objc/ChangeLog: PR c++/60512 * objc-act.cc (struct objc_feature_info): New. (objc_nonfragile_abi_p): New. (objc_common_register_features): New. * objc-act.h (objc_common_register_features): New. * objc-lang.cc (c_family_register_lang_features): New. gcc/objcp/ChangeLog: PR c++/60512 * objcp-lang.cc (c_family_register_lang_features): New. 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. * c-c++-common/has-feature-pedantic.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. Co-Authored-By: Iain Sandoe <iain@sandoe.co.uk>
2023-11-26Daily bump.GCC Administrator1-0/+12
2023-11-25c++: more checks for exporting names with using-declarationsNathaniel Shead1-21/+54
Currently only functions are directly checked for validity when exporting via a using-declaration. This patch also checks exporting non-external names of variables, types, and enumerators. This also prevents ICEs with `export using enum` for internal-linkage enums. While we're at it this patch also improves the error messages for these cases to provide more context about what went wrong. gcc/cp/ChangeLog: * name-lookup.cc (check_can_export_using_decl): New. (do_nonmember_using_decl): Use above to check if names can be exported. gcc/testsuite/ChangeLog: * g++.dg/modules/using-10.C: New test. * g++.dg/modules/using-enum-2.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
2023-11-25c++: Allow exporting a typedef redeclaration [PR102341]Nathaniel Shead1-1/+5
A typedef doesn't create a new entity, and thus should be allowed to be exported even if it has been previously declared un-exported. See the example in [module.interface] p6: export module M; struct S { int n; }; typedef S S; export typedef S S; // OK, does not redeclare an entity PR c++/102341 gcc/cp/ChangeLog: * decl.cc (duplicate_decls): Allow exporting a redeclaration of a typedef. gcc/testsuite/ChangeLog: * g++.dg/modules/export-1.C: Adjust test. * g++.dg/modules/export-2_a.C: New test. * g++.dg/modules/export-2_b.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
2023-11-25Daily bump.GCC Administrator1-0/+32
2023-11-24OpenMP: Add -Wopenmp and use itTobias Burnus2-15/+19
The new warning has two purposes: First, it makes clearer to the user that it is about OpenMP and, secondly and more importantly, it permits to use -Wno-openmp. The newly added -Wopenmp is enabled by default and replaces the '0' (always warning) in several OpenMP-related warning calls. For code shared with OpenACC, it only uses OPT_Wopenmp for 'flag_openmp | flag_openmp_simd'. gcc/c-family/ChangeLog: * c.opt (Wopenmp): Add, enable by default. gcc/c/ChangeLog: * c-parser.cc (c_parser_omp_clause_num_threads, c_parser_omp_clause_num_tasks, c_parser_omp_clause_grainsize, c_parser_omp_clause_priority, c_parser_omp_clause_schedule, c_parser_omp_clause_num_teams, c_parser_omp_clause_thread_limit, c_parser_omp_clause_dist_schedule, c_parser_omp_depobj, c_parser_omp_scan_loop_body, c_parser_omp_assumption_clauses): Add OPT_Wopenmp to warning_at. gcc/cp/ChangeLog: * parser.cc (cp_parser_omp_clause_dist_schedule, cp_parser_omp_scan_loop_body, cp_parser_omp_assumption_clauses, cp_parser_omp_depobj): Add OPT_Wopenmp to warning_at. * semantics.cc (finish_omp_clauses): Likewise. gcc/ChangeLog: * doc/invoke.texi (-Wopenmp): Add. * gimplify.cc (gimplify_omp_for): Add OPT_Wopenmp to warning_at. * omp-expand.cc (expand_omp_ordered_sink): Likewise. * omp-general.cc (omp_check_context_selector): Likewise. * omp-low.cc (scan_omp_for, check_omp_nesting_restrictions, lower_omp_ordered_clauses): Likewise. * omp-simd-clone.cc (simd_clone_clauses_extract): Likewise. gcc/fortran/ChangeLog: * lang.opt (Wopenmp): Add, enabled by dafault and documented in C. * openmp.cc (gfc_match_omp_declare_target, resolve_positive_int_expr, resolve_nonnegative_int_expr, resolve_omp_clauses, gfc_resolve_omp_do_blocks): Use OPT_Wopenmp with gfc_warning{,_now}.
2023-11-24OpenMP: Accept argument to depobj's destroy clauseTobias Burnus1-1/+24
Since OpenMP 5.2, the destroy clause takes an depend argument as argument; for the depobj directive, it the new argument is optional but, if present, it must be identical to the directive's argument. gcc/c/ChangeLog: * c-parser.cc (c_parser_omp_depobj): Accept optionally an argument to the destroy clause. gcc/cp/ChangeLog: * parser.cc (cp_parser_omp_depobj): Accept optionally an argument to the destroy clause. gcc/fortran/ChangeLog: * openmp.cc (gfc_match_omp_depobj): Accept optionally an argument to the destroy clause. libgomp/ChangeLog: * libgomp.texi (5.2 Impl. Status): An argument to the destroy clause is now supported. gcc/testsuite/ChangeLog: * c-c++-common/gomp/depobj-3.c: New test. * gfortran.dg/gomp/depobj-3.f90: New test.
2023-11-25c++: Allow exporting const-qualified namespace-scope variables [PR99232]Nathaniel Shead1-1/+2
By [basic.link] p3.2.1, a non-template non-volatile const-qualified variable is not necessarily internal linkage in a module declaration, and rather may have module linkage (or external linkage if it is exported, see p4.8). PR c++/99232 gcc/cp/ChangeLog: * decl.cc (grokvardecl): Don't mark variables attached to modules as internal. gcc/testsuite/ChangeLog: * g++.dg/modules/pr99232_a.C: New test. * g++.dg/modules/pr99232_b.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
2023-11-24c++: Support lambdas in static template member initialisers [PR107398]Nathaniel Shead1-15/+23
The testcase noted in the PR fails because the context of the lambda is not in namespace scope, but rather in class scope. This patch removes the assertion that the context must be a namespace and ensures that lambdas in class scope still get the correct merge_kind. PR c++/107398 gcc/cp/ChangeLog: * module.cc (trees_out::get_merge_kind): Handle lambdas in class scope. (maybe_key_decl): Remove assertion and fix whitespace. gcc/testsuite/ChangeLog: * g++.dg/modules/lambda-6_a.C: New test. * g++.dg/modules/lambda-6_b.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
2023-11-24c++: check mismatching exports for class tags [PR98885]Nathaniel Shead1-3/+18
Checks for exporting a declaration that was previously declared as not exported is implemented in 'duplicate_decls', but this doesn't handle declarations of classes. This patch adds these checks and slightly adjusts the associated error messages for clarity. PR c++/98885 gcc/cp/ChangeLog: * decl.cc (duplicate_decls): Adjust error message. (xref_tag): Adjust error message. Check exporting decl that is already declared as non-exporting. gcc/testsuite/ChangeLog: * g++.dg/modules/export-1.C: Adjust error messages. Remove xfails for working case. Add new test case. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
2023-11-24Daily bump.GCC Administrator1-0/+14
2023-11-23c++: Implement C++26 P2741R3 - user-generated static_assert messages [PR110348]Jakub Jelinek3-18/+178
The following patch implements the user generated static_assert messages next to string literals. As I wrote already in the PR, in addition to looking through the paper I looked at the clang++ testcase for this feature implemented there from paper's author and on godbolt played with various parts of the testcase coverage below, and there are some differences between what the patch implements and what clang++ implements. The first is that clang++ diagnoses if M.size () or M.data () methods are present, but aren't constexpr; while the paper introduction talks about that, the standard wording changes don't seem to require that, all they say is that those methods need to exist (assuming accessible and the like) and be implicitly convertible to std::size_t or const char *, but rest is only if the static assertion fails. If there is intent to change that wording, the question is how far to go, e.g. while M.size () could be constexpr, they could e.g. return some class object which wouldn't have constexpr conversion operator to size_t/const char * and tons of other reasons why the constant evaluation could fail. Without actually evaluating it I don't see how we could guarantee anything for non-failed static_assert. The second difference is that static_assert (false, "foo"_myd); in the testcase is normal failed static assertion and static_assert (true, "foo"_myd); would be accepted, while clang++ rejects it. IMHO "foo"_myd doesn't match the syntactic requirements of unevaluated-string as mentioned in http://eel.is/c++draft/dcl.pre#10 , and because a constexpr udlit operator can return something which is valid, it shouldn't be rejected just in case. Last is clang++ ICEs on non-static data members size/data. The first version of this support had a difference where M.data () was not a constant expression but a core constant expression, but if M.size () != 0 M.data ()[0] ... M.data ()[M.size () - 1] were integer constant expressions. We don't have any routine to test whether an expression is a core constant expression, so what the code does is try silently whether M.data () is a constant expression (maybe_constant_value), if it is, nice, we can use that result to attempt to optimize the extraction of the message from it if it is some recognized form involving a STRING_CST and just to double-check try to constant evaluate M.data ()[0] and M.data ()[M.size () - 1] expressions as boundaries but not anything in between. If M.data () is not a constant expression, we don't fail, but use a slower method of evaluating M.data ()[i] for i 0, 1, ... M.size () - 1. And if M.size () == 0, the above wouldn't evaluate anything, so we try to constant evaluate (M.data (), 0) as constant expression, which should succeed if M.data () is a core constant expression and fail otherwise. The patch assumes that these expressions are manifestly constant evaluated. The patch implements what I see in the paper, because it is unclear what further changes will be voted in (and the changes can be done at that point). The initial patch used tf_none in 6 spots so that just the static_assert specific errors were emitted and not others, but during review this has been changed, so that we emit both the more detailed errors why something wasn't found or wasn't callable or wasn't convertible and diagnostics that static_assert second argument needs to satisfy some of the needed properties. 2023-11-23 Jakub Jelinek <jakub@redhat.com> PR c++/110348 gcc/ * doc/invoke.texi (-Wno-c++26-extensions): Document. gcc/c-family/ * c.opt (Wc++26-extensions): New option. * c-cppbuiltin.cc (c_cpp_builtins): For C++26 predefine __cpp_static_assert to 202306L rather than 201411L. gcc/cp/ * parser.cc: Implement C++26 P2741R3 - user-generated static_assert messages. (cp_parser_static_assert): Parse message argument as conditional-expression if it is not a pure string literal or several of them concatenated followed by closing paren. * semantics.cc (finish_static_assert): Handle message which is not STRING_CST. For condition with bare parameter packs return early. * pt.cc (tsubst_expr) <case STATIC_ASSERT>: Also tsubst_expr message and make sure that if it wasn't originally STRING_CST, it isn't after tsubst_expr either. gcc/testsuite/ * g++.dg/cpp26/static_assert1.C: New test. * g++.dg/cpp26/feat-cxx26.C (__cpp_static_assert): Expect 202306L rather than 201411L. * g++.dg/cpp0x/udlit-error1.C: Expect different diagnostics for static_assert with user-defined literal.
2023-11-23Daily bump.GCC Administrator1-0/+10
2023-11-22c++: alias template of non-template class [PR112633]Patrick Palka1-0/+1
The entering_scope adjustment in tsubst_aggr_type assumes if an alias is dependent, then so is the aliased type (and therefore it has template info) but that's not true for the dependent alias template specialization ty1<T> below which aliases the non-template class A. In this case no adjustment is needed anyway, so we can just punt. PR c++/112633 gcc/cp/ChangeLog: * pt.cc (tsubst_aggr_type): Handle empty TYPE_TEMPLATE_INFO in the entering_scope adjustment. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/alias-decl-75.C: New test.
2023-11-21c++: start_preparsed_function tweakJason Merrill1-7/+5
In review of the deducing 'this' patch, it came up that the logic in start_preparsed_function around the ctype variable was convoluted, being set for non-static member functions and friends, but not for static member functions. Let's set it for any member function, and not rely on it to decide whether to set up 'this'. gcc/cp/ChangeLog: * decl.cc (start_preparsed_function): Clarify ctype logic.
2023-11-21Daily bump.GCC Administrator1-0/+18
2023-11-20Trivial typo fix in variadicMarc Poulhiès1-1/+1
Fix all occurences of varadic, except for Rust (will be part of another change). gcc/ChangeLog: * config/nvptx/nvptx.h (struct machine_function): Fix typo in variadic. * config/nvptx/nvptx.cc (nvptx_function_arg_advance): Adjust to use fixed name. (nvptx_declare_function_name): Likewise. (nvptx_call_args): Likewise. (nvptx_expand_call): Likewise. gcc/cp/ChangeLog: * lambda.cc (compare_lambda_sig): Fix typo in variadic. libcpp/ChangeLog: * macro.cc (parse_params): Fix typo in variadic. (create_iso_definition): Likewise. Signed-off-by: Marc Poulhiès <dkm@kataplop.net>
2023-11-19c++: compare one level of template parmsJason Merrill1-57/+33
There should never be a reason to compare more than one level of template parameters; additional levels are for the enclosing context, which is either irrelevant (for a template template parameter) or already compared (for a member template). Also, the comp_template_parms handling of type parameters was wrongly checking for TEMPLATE_TYPE_PARM when a type parameter appears here as a TYPE_DECL. gcc/cp/ChangeLog: * pt.cc (comp_template_parms): Just one level. (template_parameter_lists_equivalent_p): Likewise.
2023-11-19c++: add DECL_IMPLICIT_TEMPLATE_PARM_P macroJason Merrill4-3/+23
Let's use a more informative name instead of DECL_VIRTUAL_P directly. gcc/cp/ChangeLog: * cp-tree.h (DECL_TEMPLATE_PARM_CHECK): New. (DECL_IMPLICIT_TEMPLATE_PARM_P): New. (decl_template_parm_check): New. * mangle.cc (write_closure_template_head): Use it. * parser.cc (synthesize_implicit_template_parm): Likewise. * pt.cc (template_parameters_equivalent_p): Likewise.
2023-11-20Daily bump.GCC Administrator1-0/+11
2023-11-19c++: Set DECL_CONTEXT for __cxa_thread_atexit [PR99187]Nathaniel Shead2-2/+17
Modules streaming requires DECL_CONTEXT to be set on declarations that are streamed. This ensures that __cxa_thread_atexit is given translation unit context much like is already done with many other support functions. PR c++/99187 gcc/cp/ChangeLog: * cp-tree.h (enum cp_tree_index): Add CPTI_THREAD_ATEXIT. (thread_atexit_node): New. * decl.cc (get_thread_atexit_node): Cache in thread_atexit_node. gcc/testsuite/ChangeLog: * g++.dg/modules/pr99187.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com> Signed-off-by: Nathan Sidwell <nathan@acm.org>
2023-11-19libcpp: split decls out to rich-location.hDavid Malcolm1-0/+1
The various decls relating to rich_location are in libcpp/include/line-map.h, but they don't relate to line maps. Split them out to their own header: libcpp/include/rich-location.h No functional change intended. gcc/ChangeLog: * Makefile.in (CPPLIB_H): Add libcpp/include/rich-location.h. * coretypes.h (class rich_location): New forward decl. gcc/analyzer/ChangeLog: * analyzer.h: Include "rich-location.h". gcc/c-family/ChangeLog: * c-lex.cc: Include "rich-location.h". gcc/cp/ChangeLog: * mapper-client.cc: Include "rich-location.h". gcc/ChangeLog: * diagnostic.h: Include "rich-location.h". * edit-context.h (class fixit_hint): New forward decl. * gcc-rich-location.h: Include "rich-location.h". * genmatch.cc: Likewise. * pretty-print.h: Likewise. gcc/rust/ChangeLog: * rust-location.h: Include "rich-location.h". libcpp/ChangeLog: * Makefile.in (TAGS_SOURCES): Add "include/rich-location.h". * include/cpplib.h (class rich_location): New forward decl. * include/line-map.h (class range_label) (enum range_display_kind, struct location_range) (class semi_embedded_vec, class rich_location, class label_text) (class range_label, class fixit_hint): Move to... * include/rich-location.h: ...this new file. * internal.h: Include "rich-location.h". Signed-off-by: David Malcolm <dmalcolm@redhat.com>
2023-11-17Daily bump.GCC Administrator1-0/+21
2023-11-16c++: Fix error recovery ICE [PR112365]Jakub Jelinek1-1/+2
check_field_decls for DECL_C_BIT_FIELD FIELD_DECLs with error_mark_node TREE_TYPE continues early and doesn't call check_bitfield_decl which would either set DECL_BIT_FIELD, or clear DECL_C_BIT_FIELD. So, the following testcase ICEs after emitting tons of errors, because SET_DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD asserts DECL_BIT_FIELD. The patch skips that for FIELD_DECLs with error_mark_node, another option would be to check DECL_BIT_FIELD in addition to DECL_C_BIT_FIELD. 2023-11-16 Jakub Jelinek <jakub@redhat.com> PR c++/112365 * class.cc (layout_class_type): Don't SET_DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD on FIELD_DECLs with error_mark_node type. * g++.dg/cpp0x/pr112365.C: New test.
2023-11-16c++: constantness of call to function pointer [PR111703]Patrick Palka1-1/+6
potential_constant_expression for CALL_EXPR tests FUNCTION_POINTER_TYPE_P on the callee rather than on the type of the callee, which means we always pass want_rval=any when recursing and so may fail to identify a non-constant function pointer callee as such. Fixing this turns out to further work around PR111703. PR c++/111703 PR c++/107939 gcc/cp/ChangeLog: * constexpr.cc (potential_constant_expression_1) <case CALL_EXPR>: Fix FUNCTION_POINTER_TYPE_P test. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-fn8.C: Extend test. * g++.dg/diagnostic/constexpr4.C: New test.
2023-11-15c++: fix parsing with auto(x) [PR112410]Marek Polacek1-0/+13
Here we are wrongly parsing int y(auto(42)); which uses the C++23 cast-to-prvalue feature, and initializes y to 42. However, we were treating the auto as an implicit template parameter. Fixing the auto{42} case is easy, but when auto is followed by a (, I found the fix to be much more involved. For instance, we cannot use cp_parser_expression, because that can give hard errors. It's also necessary to disambiguate 'auto(i)' as 'auto i', not a cast. auto(), auto(int), auto(f)(int), auto(*), auto(i[]), auto(...), etc. are all function declarations. This patch rectifies that by undoing the implicit function template modification. In the test above, we should notice that the parameter list is ill-formed, and since we've synthesized an implicit template parameter, we undo it by calling abort_fully_implicit_template. Then, we'll parse the "(auto(42))" as an initializer. PR c++/112410 gcc/cp/ChangeLog: * parser.cc (cp_parser_direct_declarator): Maybe call abort_fully_implicit_template if it turned out the parameter list was ill-formed. gcc/testsuite/ChangeLog: * g++.dg/cpp23/auto-fncast13.C: New test. * g++.dg/cpp23/auto-fncast14.C: New test.
2023-11-16Daily bump.GCC Administrator1-0/+56
2023-11-15c++, analyzer: Expand CAN_HAVE_LOCATION_P macro.Bernhard Reutner-Fischer1-1/+1
r14-985-gca2007a9bb3074 used the collapsed macro definition CAN_HAVE_LOCATION_P in gcc-rich-location.cc and r14-977-g8861c80733da5c in c++'s build_cplus_array_type (). However, although otherwise correct, the usage of CAN_HAVE_LOCATION_P in these two spots is misleading, so this patch reverts aforementioned two hunks. gcc/cp/ChangeLog: * tree.cc (build_cplus_array_type): Revert using the macro CAN_HAVE_LOCATION_P. gcc/ChangeLog: * gcc-rich-location.cc (maybe_range_label_for_tree_type_mismatch::get_text): Revert using the macro CAN_HAVE_LOCATION_P.
2023-11-15c++: direct enum init from type-dep elt [PR112515]Patrick Palka1-0/+1
The NON_DEPENDENT_EXPR removal exposed that is_direct_enum_init can be called in a template context on a CONSTRUCTOR that isn't type-dependent but whose element is. PR c++/112515 gcc/cp/ChangeLog: * decl.cc (is_direct_enum_init): Check type-dependence of the single element. gcc/testsuite/ChangeLog: * g++.dg/template/non-dependent30.C: New test.
2023-11-15c++: partially inst requires-expr in noexcept-spec [PR101043]Patrick Palka1-7/+12
Here we're ICEing from strip_typedefs for the partially instantiated requires-expression when walking its REQUIRES_EXPR_EXTRA_ARGS which in this case is a TREE_LIST with non-empty TREE_PURPOSE (to hold the captured local specialization 't' as per build_extra_args) which strip_typedefs doesn't expect. We can probably skip walking REQUIRES_EXPR_EXTRA_ARGS at all since it shouldn't contain any typedefs in the first place, but it seems safer and more generally useful to just teach strip_typedefs to handle non-empty TREE_PURPOSE the obvious way. (The code asserts TREE_PURPOSE was empty even since since its inception i.e. r189298.) PR c++/101043 gcc/cp/ChangeLog: * tree.cc (strip_typedefs_expr) <case TREE_LIST>: Handle non-empty TREE_PURPOSE. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-requires37.C: New test.
2023-11-15c++: non-dependent .* operand folding [PR112427]Patrick Palka2-2/+15
Here when building up the non-dependent .* expression, we crash from fold_convert on 'b.a' due to this (templated) COMPONENT_REF having an IDENTIFIER_NODE instead of FIELD_DECL operand that middle-end routines expect. Like in r14-4899-gd80a26cca02587, this patch fixes this by replacing the problematic piecemeal folding with a single call to cp_fully_fold. Also, don't bother building the POINTER_PLUS_EXPR in a template context. This means the returned non-dependent tree might not have TREE_SIDE_EFFECTS set when it used to, so we need to compensate by making build_min_non_dep propagate TREE_SIDE_EFFECTS from the original arguments like buildN and build_min do. PR c++/112427 gcc/cp/ChangeLog: * tree.cc (build_min_non_dep): Propagate TREE_SIDE_EFFECTS from the original arguments. (build_min_non_dep_call_vec): Likewise. * typeck2.cc (build_m_component_ref): Use cp_convert, build2 and cp_fully_fold instead of fold_build_pointer_plus and fold_convert. Don't build the POINTER_PLUS_EXPR in a template context. gcc/testsuite/ChangeLog: * g++.dg/template/non-dependent29.C: New test.
2023-11-15c++: constantness of local var in constexpr fn [PR111703, PR112269]Patrick Palka1-2/+2
potential_constant_expression was incorrectly treating most local variables from a constexpr function as constant because it wasn't considering the 'now' parameter. This patch fixes this by relaxing its var_in_maybe_constexpr_fn checks accordingly, which turns out to partially fix two recently reported regressions: PR111703 is a regression caused by r11-550-gf65a3299a521a4 for restricting constexpr evaluation during warning-dependent folding. The mechanism is intended to restrict only constant evaluation of the instantiated non-dependent expression, but it also ends up restricting constant evaluation occurring during instantiation of the expression, in particular when instantiating the converted argument 'x' (a VIEW_CONVERT_EXPR) into a copy constructor call. This seems like a flaw in the mechanism, though I don't know if we want to fix the mechanism or get rid of it completely since the original testcases which motivated the mechanism are fixed more simply by r13-1225-gb00b95198e6720. In any case, this patch partially fixes this by making us correctly treat 'x' as non-constant which prevents the problematic warning-dependent folding from occurring at all. PR112269 is caused by r14-4796-g3e3d73ed5e85e7 for merging tsubst_copy into tsubst_copy_and_build. tsubst_copy used to exit early when 'args' was empty, behavior which that commit deliberately didn't preserve. This early exit masked the fact that COMPLEX_EXPR wasn't handled by tsubst at all, and is a tree code that apparently we could see during warning-dependent folding on some targets. A complete fix is to add handling for this tree code in tsubst_expr, but this patch should fix the reported testsuite failures since the COMPLEX_EXPRs that crop up in <complex> are considered non-constant expressions after this patch. PR c++/111703 PR c++/112269 gcc/cp/ChangeLog: * constexpr.cc (potential_constant_expression_1) <case VAR_DECL>: Only consider var_in_maybe_constexpr_fn if 'now' is false. <case INDIRECT_REF>: Likewise. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-fn8.C: New test.
2023-11-15c++: Implement C++26 P2864R2 - Remove Deprecated Arithmetic Conversion on ↵Jakub Jelinek2-39/+98
Enumerations From C++26 The following patch implements C++26 P2864R2 by emitting pedwarn enabled by the same options as the C++20 and later warnings (i.e. -Wenum-compare, -Wdeprecated-enum-enum-conversion and -Wdeprecated-enum-float-conversion which are all enabled by default). I think we still want to allow users some option workaround, so am not using directly error. Additionally, for cxx_dialect >= cxx26 && (complain & tf_warning_or_error) == 0 it causes for these newly ill-formed constructs error_mark_node to be silently returned. 2023-11-15 Jakub Jelinek <jakub@redhat.com> gcc/cp/ * typeck.cc: Implement C++26 P2864R2 - Remove Deprecated Arithmetic Conversion on Enumerations From C++26. (do_warn_enum_conversions): Return bool rather than void, add COMPLAIN argument. Use pedwarn rather than warning_at for C++26 and remove " is deprecated" part of the diagnostics in that case. For SFINAE in C++26 return true on newly erroneous cases. (cp_build_binary_op): For C++26 call do_warn_enum_conversions unconditionally, pass complain argument to it and if it returns true, return error_mark_node. * call.cc (build_conditional_expr): Use pedwarn rather than warning_at for C++26 and remove " is deprecated" part of the diagnostics in that case and check for complain & tf_warning_or_error. Use emit_diagnostic with cxx_dialect >= cxx26 ? DK_PEDWARN : DK_WARNING. For SFINAE in C++26 return error_mark_node on newly erroneous cases. (build_new_op): Use emit_diagnostic with cxx_dialect >= cxx26 ? DK_PEDWARN : DK_WARNING and complain & tf_warning_or_error check for C++26. For SFINAE in C++26 return error_mark_node on newly erroneous cases. gcc/testsuite/ * g++.dg/cpp26/enum-conv1.C: New test. * g++.dg/cpp2a/enum-conv1.C: Adjust expected diagnostics in C++26. * g++.dg/diagnostic/enum3.C: Likewise. * g++.dg/parse/attr3.C: Likewise. * g++.dg/cpp0x/linkage2.C: Likewise.
2023-11-15Daily bump.GCC Administrator1-0/+22
2023-11-14c++: Stream virtual dtor vtable indicesNathaniel Shead1-0/+6
Virtual cloned functions have distinct vtable indices, stream them explicitly. As such, this patch ensures that DECL_VINDEX is properly passed on for cloned functions as well to prevent this from causing issues. PR c++/103499 gcc/cp/ChangeLog: * module.cc (trees_out::decl_node): Write DECL_VINDEX for virtual clones. (trees_in::tree_node): Read DECL_VINDEX for virtual clones. gcc/testsuite/ChangeLog: * g++.dg/modules/pr103499_a.C: New test. * g++.dg/modules/pr103499_b.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com> Signed-off-by: Nathan Sidwell <nathan@acm.org>
2023-11-14c++: Fix exported using decls of templatesNathaniel Shead1-4/+8
We need to look at DECL_TEMPLATE_RESULT to get the module attachment. PR c++/106849 gcc/cp/ChangeLog: * name-lookup.cc (do_nonmember_using_decl): Handle TEMPLATE_DECLs when checking module attachment. gcc/testsuite/ChangeLog: * g++.dg/modules/using-9.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com> Signed-off-by: Nathan Sidwell <nathan@acm.org>
2023-11-14diagnostics: make option-handling callbacks privateDavid Malcolm1-3/+1
No functional change intended. gcc/c-family/ChangeLog: * c-warn.cc (conversion_warning): Update call to global_dc->m_option_enabled to use option_enabled_p. gcc/cp/ChangeLog: * decl.cc (finish_function): Update call to global_dc->m_option_enabled to use option_enabled_p. gcc/ChangeLog: * diagnostic-format-json.cc (json_output_format::on_end_diagnostic): Update calls to m_context callbacks to use member functions; tighten up scopes. * diagnostic-format-sarif.cc (sarif_builder::make_result_object): Likewise. (sarif_builder::make_reporting_descriptor_object_for_warning): Likewise. * diagnostic.cc (diagnostic_context::initialize): Update for callbacks being moved into m_option_callbacks and being renamed. (diagnostic_context::set_option_hooks): New. (diagnostic_option_classifier::classify_diagnostic): Update call to global_dc->m_option_enabled to use option_enabled_p. (diagnostic_context::print_option_information): Update calls to m_context callbacks to use member functions; tighten up scopes. (diagnostic_context::diagnostic_enabled): Likewise. * diagnostic.h (diagnostic_option_enabled_cb): New typedef. (diagnostic_make_option_name_cb): New typedef. (diagnostic_make_option_url_cb): New typedef. (diagnostic_context::option_enabled_p): New. (diagnostic_context::make_option_name): New. (diagnostic_context::make_option_url): New. (diagnostic_context::set_option_hooks): New decl. (diagnostic_context::m_option_enabled): Rename to m_option_enabled_cb and move within m_option_callbacks, using typedef. (diagnostic_context::m_option_state): Move within m_option_callbacks. (diagnostic_context::m_option_name): Rename to m_make_option_name_cb and move within m_option_callbacks, using typedef. (diagnostic_context::m_get_option_url): Likewise, renaming to m_make_option_url_cb. * lto-wrapper.cc (print_lto_docs_link): Update call to m_context callback to use member function. (main): Use diagnostic_context::set_option_hooks. * opts-diagnostic.h (option_name): Make context param const. (get_option_url): Likewise. * opts.cc (option_name): Likewise. (get_option_url): Likewise. * toplev.cc (general_init): Use diagnostic_context::set_option_hooks. Signed-off-by: David Malcolm <dmalcolm@redhat.com>
2023-11-14input.h: eliminate implicit users of global_dc's file_cacheDavid Malcolm1-1/+2
This patch eliminates the following functions that implicitly used global_dc's file cache: extern char_span location_get_source_line (const char *file_path, int line); extern char_span get_source_file_content (const char *file_path); extern bool location_missing_trailing_newline (const char *file_path); in favor of explicitly using a specific file_cache throughout, and only using global_dc's file_cache in gcc-specific code. Rather than creating global_dc's file_cache the first time its needed, this patch simply creates one when a diagnostic_context is initialized, and eliminates diagnostic_file_cache_init. No functional change intended. gcc/c-family/ChangeLog: * c-common.cc (c_get_substring_location): Use global_dc's file_cache. * c-format.cc (get_corrected_substring): Likewise. * c-indentation.cc (get_visual_column): Add file_cache param. (get_first_nws_vis_column): Likewise. (detect_intervening_unindent): Likewise. (should_warn_for_misleading_indentation): Use global_dc's file_cache. (assert_get_visual_column_succeeds): Add file_cache param. (ASSERT_GET_VISUAL_COLUMN_SUCCEEDS): Likewise. (assert_get_visual_column_fails): Likewise. (define ASSERT_GET_VISUAL_COLUMN_FAILS): Likewise. (selftest::test_get_visual_column): Create and use a temporary file_cache. gcc/cp/ChangeLog: * contracts.cc (build_comment): Use global_dc's file_cache. gcc/ChangeLog: * diagnostic-format-sarif.cc (sarif_builder::get_sarif_column): Use m_context's file_cache. (sarif_builder::maybe_make_artifact_content_object): Likewise. (sarif_builder::get_source_lines): Likewise. * diagnostic-show-locus.cc (exploc_with_display_col::exploc_with_display_col): Add file_cache param. (layout::m_file_cache): New field. (make_range): Add file_cache param. (selftest::test_layout_range_for_single_point): Create and use a temporary file_cache. (selftest::test_layout_range_for_single_line): Likewise. (selftest::test_layout_range_for_multiple_lines): Likewise. (layout::layout): Initialize m_file_cache from the context and use it. (layout::maybe_add_location_range): Use m_file_cache. (layout::calculate_x_offset_display): Likewise. (get_affected_range): Add file_cache param. (get_printed_columns): Likewise. (line_corrections::line_corrections): Likewwise. (line_corrections::m_file_cache): New field. (source_line::source_line): Add file_cache param. (line_corrections::add_hint): Use m_file_cache. (layout::print_trailing_fixits): Likewise. (layout::print_line): Likewise. (selftest::test_layout_x_offset_display_utf8): Create and use a temporary file_cache. (selftest::test_layout_x_offset_display_tab): Likewise. (selftest::test_diagnostic_show_locus_one_liner_utf8): Likewise. (selftest::test_add_location_if_nearby): Pass global_dc's file_cache to temp_source_file ctor. (selftest::test_overlapped_fixit_printing): Create and use a temporary file_cache. (selftest::test_overlapped_fixit_printing_utf8): Likewise. (selftest::test_overlapped_fixit_printing_2): Use dc's file_cache. * diagnostic.cc (diagnostic_context::initialize): Always create a file_cache. (diagnostic_context::initialize_input_context): Assume m_file_cache has already been created. (diagnostic_context::create_edit_context): Pass m_file_cache to edit_context. (convert_column_unit): Add file_cache param. (diagnostic_context::converted_column): Use context's file_cache. (print_parseable_fixits): Add file_cache param. (diagnostic_context::report_diagnostic): Use context's file_cache. (selftest::test_print_parseable_fixits_none): Create and use a temporary file_cache. (selftest::test_print_parseable_fixits_insert): Likewise. (selftest::test_print_parseable_fixits_remove): Likewise. (selftest::test_print_parseable_fixits_replace): Likewise. (selftest::test_print_parseable_fixits_bytes_vs_display_columns): Likewise. * diagnostic.h (diagnostic_context::file_cache_init): Delete. (diagnostic_context::get_file_cache): Convert return type from pointer to reference. * edit-context.cc (edited_file::get_file_cache): New. (edited_file::m_edit_context): New. (edit_context::edit_context): Add file_cache param. (edit_context::get_or_insert_file): Pass this to edited_file's ctor. (edited_file::edited_file): Add edit_context param. (edited_file::print_content): Use get_file_cache. (edited_file::print_diff_hunk): Likewise. (edited_file::print_run_of_changed_lines): Likewise. (edited_file::get_or_insert_line): Likewise. (edited_file::get_num_lines): Likewise. (edited_line::edited_line): Pass in file_cache and use it. (selftest::test_get_content): Create and use a temporary file_cache. (selftest::test_applying_fixits_insert_before): Likewise. (selftest::test_applying_fixits_insert_after): Likewise. (selftest::test_applying_fixits_insert_after_at_line_end): Likewise. (selftest::test_applying_fixits_insert_after_failure): Likewise. (selftest::test_applying_fixits_insert_containing_newline): Likewise. (selftest::test_applying_fixits_growing_replace): Likewise. (selftest::test_applying_fixits_shrinking_replace): Likewise. (selftest::test_applying_fixits_replace_containing_newline): Likewise. (selftest::test_applying_fixits_remove): Likewise. (selftest::test_applying_fixits_multiple): Likewise. (selftest::test_applying_fixits_multiple_lines): Likewise. (selftest::test_applying_fixits_modernize_named_init): Likewise. (selftest::test_applying_fixits_modernize_named_init): Likewise. (selftest::test_applying_fixits_unreadable_file): Likewise. (selftest::test_applying_fixits_line_out_of_range): Likewise. (selftest::test_applying_fixits_column_validation): Likewise. (selftest::test_applying_fixits_column_validation): Likewise. (selftest::test_applying_fixits_column_validation): Likewise. (selftest::test_applying_fixits_column_validation): Likewise. * edit-context.h (edit_context::edit_context): Add file_cache param. (edit_context::get_file_cache): New. (edit_context::m_file_cache): New. * final.cc: Include "diagnostic.h". (asm_show_source): Use global_dc's file_cache. * gcc-rich-location.cc (blank_line_before_p): Add file_cache param. (use_new_line): Likewise. (gcc_rich_location::add_fixit_insert_formatted): Use global dc's file_cache. * input.cc (diagnostic_file_cache_init): Delete. (diagnostic_context::file_cache_init): Delete. (diagnostics_file_cache_forcibly_evict_file): Delete. (file_cache::missing_trailing_newline_p): New. (file_cache::evicted_cache_tab_entry): Don't call diagnostic_file_cache_init. (location_get_source_line): Delete. (get_source_text_between): Add file_cache param. (get_source_file_content): Delete. (location_missing_trailing_newline): Delete. (location_compute_display_column): Add file_cache param. (dump_location_info): Create and use temporary file_cache. (get_substring_ranges_for_loc): Add file_cache param. (get_location_within_string): Likewise. (get_source_range_for_char): Likewise. (get_num_source_ranges_for_substring): Likewise. (selftest::test_reading_source_line): Create and use temporary file_cache. (selftest::lexer_test::m_file_cache): New field. (selftest::assert_char_at_range): Use test.m_file_cache. (selftest::assert_num_substring_ranges): Likewise. (selftest::assert_has_no_substring_ranges): Likewise. (selftest::test_lexer_string_locations_concatenation_2): Likewise. * input.h (class file_cache): New forward decl. (location_compute_display_column): Add file_cache param. (location_get_source_line): Delete. (get_source_text_between): Add file_cache param. (get_source_file_content): Delete. (location_missing_trailing_newline): Delete. (file_cache::missing_trailing_newline_p): New decl. (diagnostics_file_cache_forcibly_evict_file): Delete. * selftest.cc (named_temp_file::named_temp_file): Add file_cache param. (named_temp_file::~named_temp_file): Optionally evict the file from the given file_cache. (temp_source_file::temp_source_file): Add file_cache param. * selftest.h (class file_cache): New forward decl. (named_temp_file::named_temp_file): Add file_cache param. (named_temp_file::m_file_cache): New field. (temp_source_file::temp_source_file): Add file_cache param. * substring-locations.h (get_location_within_string): Add file_cache param. gcc/testsuite/ChangeLog: * gcc.dg/plugin/diagnostic_plugin_test_show_locus.c: Use global_dc's file cache. * gcc.dg/plugin/expensive_selftests_plugin.c: Likewise. Signed-off-by: David Malcolm <dmalcolm@redhat.com>
2023-11-14Daily bump.GCC Administrator1-0/+34
2023-11-14c++: Link extended FP conversion pedwarns to -Wnarrowing [PR111842]Jonathan Wakely1-4/+6
Several users have been confused by the status of these warnings, which can be misunderstood as "this might not be what you want", rather than diagnostics required by the C++ standard. Add the text "ISO C++ does not allow" to make this clear. Also link them to -Wnarrowing so that they can be disabled or promoted to errors independently of other pedwarns. PR c++/111842 PR c++/112498 gcc/cp/ChangeLog: * call.cc (convert_like_internal): Use OPT_Wnarrowing for pedwarns about illformed conversions involving extended floating-point types. Clarify that ISO C++ requires these diagnostics. gcc/testsuite/ChangeLog: * g++.dg/cpp23/ext-floating16.C: New test. * g++.dg/cpp23/ext-floating17.C: New test.
2023-11-14Add type-generic clz/ctz/clrsb/ffs/parity/popcount builtins [PR111309]Jakub Jelinek2-2/+16
The following patch adds 6 new type-generic builtins, __builtin_clzg __builtin_ctzg __builtin_clrsbg __builtin_ffsg __builtin_parityg __builtin_popcountg The g at the end stands for generic because the unsuffixed variant of the builtins already have unsigned int or int arguments. The main reason to add these is to support arbitrary unsigned (for clrsb/ffs signed) bit-precise integer types and also __int128 which wasn't supported by the existing builtins, so that e.g. <stdbit.h> type-generic functions could then support not just bit-precise unsigned integer type whose width matches a standard or extended integer type, but others too. None of these new builtins promote their first argument, so the argument can be e.g. unsigned char or unsigned short or unsigned __int20 etc. The first 2 support either 1 or 2 arguments, if only 1 argument is supplied, the behavior is undefined for argument 0 like for other __builtin_c[lt]z* builtins, if 2 arguments are supplied, the second argument should be int that will be returned if the argument is 0. All other builtins have just one argument. For __builtin_clrsbg and __builtin_ffsg the argument shall be any signed standard/extended or bit-precise integer, for the others any unsigned standard/extended or bit-precise integer (bool not allowed). One possibility would be to also allow signed integer types for the clz/ctz/parity/popcount ones (and just cast the argument to unsigned_type_for during folding) and similarly unsigned integer types for the clrsb/ffs ones, dunno what is better; for stdbit.h the current version is sufficient and diagnoses use of the inappropriate sign, though on the other side I wonder if users won't be confused by __builtin_clzg (1) being an error and having to write __builtin_clzg (1U). The new builtins are lowered to corresponding builtins with other suffixes or internal calls (plus casts and adjustments where needed) during FE folding or during gimplification at latest, the non-suffixed builtins handling precisions up to precision of int, l up to precision of long, ll up to precision of long long, up to __int128 precision lowered to double-word expansion early and the rest (which must be _BitInt) lowered to internal fn calls - those are then lowered during bitint lowering pass. The patch also changes representation of IFN_CLZ and IFN_CTZ calls, previously they were in the IL only if they are directly supported optab and depending on C[LT]Z_DEFINED_VALUE_AT_ZERO (...) == 2 they had or didn't have defined behavior at 0, now they are in the IL either if directly supported optab, or for the large/huge BITINT_TYPEs and they have either 1 or 2 arguments. If one, the behavior is undefined at zero, if 2, the second argument is an int constant that should be returned for 0. As there is no extra support during expansion, for directly supported optab the second argument if present should still match the C[LT]Z_DEFINED_VALUE_AT_ZERO (...) == 2 value, but for BITINT_TYPE arguments it can be arbitrary int INTEGER_CST. The indended uses in stdbit.h are e.g. #ifdef __has_builtin #if __has_builtin(__builtin_clzg) && __has_builtin(__builtin_ctzg) && __has_builtin(__builtin_popcountg) #define stdc_leading_zeros(value) \ ((unsigned int) __builtin_clzg (value, __builtin_popcountg ((__typeof (value)) ~(__typeof (value)) 0))) #define stdc_leading_ones(value) \ ((unsigned int) __builtin_clzg ((__typeof (value)) ~(value), __builtin_popcountg ((__typeof (value)) ~(__typeof (value)) 0))) #define stdc_first_trailing_one(value) \ ((unsigned int) (__builtin_ctzg (value, -1) + 1)) #define stdc_trailing_zeros(value) \ ((unsigned int) __builtin_ctzg (value, __builtin_popcountg ((__typeof (value)) ~(__typeof (value)) 0))) #endif #endif where __builtin_popcountg ((__typeof (x)) -1) computes the bit precision of x's type (kind of _Bitwidthof (x) alternative). They also allow casting of arbitrary unsigned _BitInt other than unsigned _BitInt(1) to corresponding signed _BitInt by using signed _BitInt(__builtin_popcountg ((__typeof (a)) -1)) and of arbitrary signed _BitInt to corresponding unsigned _BitInt using unsigned _BitInt(__builtin_clrsbg ((__typeof (a)) -1) + 1). 2023-11-14 Jakub Jelinek <jakub@redhat.com> PR c/111309 gcc/ * builtins.def (BUILT_IN_CLZG, BUILT_IN_CTZG, BUILT_IN_CLRSBG, BUILT_IN_FFSG, BUILT_IN_PARITYG, BUILT_IN_POPCOUNTG): New builtins. * builtins.cc (fold_builtin_bit_query): New function. (fold_builtin_1): Use it for BUILT_IN_{CLZ,CTZ,CLRSB,FFS,PARITY,POPCOUNT}G. (fold_builtin_2): Use it for BUILT_IN_{CLZ,CTZ}G. * fold-const-call.cc: Fix comment typo on tm.h inclusion. (fold_const_call_ss): Handle CFN_BUILT_IN_{CLZ,CTZ,CLRSB,FFS,PARITY,POPCOUNT}G. (fold_const_call_sss): New function. (fold_const_call_1): Call it for 2 argument functions returning scalar when passed 2 INTEGER_CSTs. * genmatch.cc (cmp_operand): For function calls also compare number of arguments. (fns_cmp): New function. (dt_node::gen_kids): Sort fns and generic_fns. (dt_node::gen_kids_1): Handle fns with the same id but different number of arguments. * match.pd (CLZ simplifications): Drop checks for defined behavior at zero. Add variant of simplifications for IFN_CLZ with 2 arguments. (CTZ simplifications): Drop checks for defined behavior at zero, don't optimize precisions above MAX_FIXED_MODE_SIZE. Add variant of simplifications for IFN_CTZ with 2 arguments. (a != 0 ? CLZ(a) : CST -> .CLZ(a)): Use TREE_TYPE (@3) instead of type, add BITINT_TYPE handling, create 2 argument IFN_CLZ rather than one argument. Add variant for matching CLZ with 2 arguments. (a != 0 ? CTZ(a) : CST -> .CTZ(a)): Similarly. * gimple-lower-bitint.cc (bitint_large_huge::lower_bit_query): New method. (bitint_large_huge::lower_call): Use it for IFN_{CLZ,CTZ,CLRSB,FFS} and IFN_{PARITY,POPCOUNT} calls. * gimple-range-op.cc (cfn_clz::fold_range): Don't check CLZ_DEFINED_VALUE_AT_ZERO for m_gimple_call_internal_p, instead assume defined value at zero if the call has 2 arguments and use second argument value for that case. (cfn_ctz::fold_range): Similarly. (gimple_range_op_handler::maybe_builtin_call): Use op_cfn_clz_internal or op_cfn_ctz_internal only if internal fn call has 2 arguments and set m_op2 in that case. * tree-vect-patterns.cc (vect_recog_ctz_ffs_pattern, vect_recog_popcount_clz_ctz_ffs_pattern): For value defined at zero use second argument of calls if present, otherwise assume UB at zero, create 2 argument .CLZ/.CTZ calls if needed. * tree-vect-stmts.cc (vectorizable_call): Handle 2 argument .CLZ/.CTZ calls. * tree-ssa-loop-niter.cc (build_cltz_expr): Create 2 argument .CLZ/.CTZ calls if needed. * tree-ssa-forwprop.cc (simplify_count_trailing_zeroes): Create 2 argument .CTZ calls if needed. * tree-ssa-phiopt.cc (cond_removal_in_builtin_zero_pattern): Handle 2 argument .CLZ/.CTZ calls, handle BITINT_TYPE, create 2 argument .CLZ/.CTZ calls. * doc/extend.texi (__builtin_clzg, __builtin_ctzg, __builtin_clrsbg, __builtin_ffsg, __builtin_parityg, __builtin_popcountg): Document. gcc/c-family/ * c-common.cc (check_builtin_function_arguments): Handle BUILT_IN_{CLZ,CTZ,CLRSB,FFS,PARITY,POPCOUNT}G. * c-gimplify.cc (c_gimplify_expr): If __builtin_c[lt]zg second argument hasn't been folded into constant yet, transform it to one argument call inside of a COND_EXPR which for first argument 0 returns the second argument. gcc/c/ * c-typeck.cc (convert_arguments): Don't promote first argument of BUILT_IN_{CLZ,CTZ,CLRSB,FFS,PARITY,POPCOUNT}G. gcc/cp/ * call.cc (magic_varargs_p): Return 4 for BUILT_IN_{CLZ,CTZ,CLRSB,FFS,PARITY,POPCOUNT}G. (build_over_call): Don't promote first argument of BUILT_IN_{CLZ,CTZ,CLRSB,FFS,PARITY,POPCOUNT}G. * cp-gimplify.cc (cp_gimplify_expr): For BUILT_IN_C{L,T}ZG use c_gimplify_expr. gcc/testsuite/ * c-c++-common/pr111309-1.c: New test. * c-c++-common/pr111309-2.c: New test. * gcc.dg/torture/bitint-43.c: New test. * gcc.dg/torture/bitint-44.c: New test.
2023-11-10c++: decltype of (by-value captured reference) [PR79620]Patrick Palka2-2/+9
The capture_decltype handling in finish_decltype_type wasn't looking through implicit INDIRECT_REF (added by convert_from_reference), which caused us to incorrectly resolve decltype((r)) to float& below. This patch fixes this, and adds an assert to outer_automatic_var_p to help prevent against such bugs. We still don't fully accept the example ultimately because for the decltype inside the lambda's trailing return type, at that point we're in lambda type scope but not yet in lambda function scope that the capture_decltype handling looks for (which is an orthogonal bug). PR c++/79620 gcc/cp/ChangeLog: * cp-tree.h (STRIP_REFERENCE_REF): Define. * semantics.cc (outer_var_p): Assert REFERENCE_REF_P is false. (finish_decltype_type): Look through implicit INDIRECT_REF when deciding whether to call capture_decltype. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/lambda/lambda-decltype3.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
2023-11-10c++: decltype of capture proxy [PR79378, PR96917]Patrick Palka1-1/+21
We typically don't see capture proxies in finish_decltype_type because process_outer_var_ref is a no-op within an unevaluated context and so a use of a captured variable within decltype resolves to the captured variable, not the capture. But we can see them during decltype(auto) deduction and for decltype of an init-capture, which suggests we need to handle capture proxies specially within finish_decltype_type after all. This patch adds such handling. PR c++/79378 PR c++/96917 gcc/cp/ChangeLog: * semantics.cc (finish_decltype_type): Handle an id-expression naming a capture proxy specially. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/decltype-auto7.C: New test. * g++.dg/cpp1y/lambda-init20.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>