aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/c-c++-common
AgeCommit message (Collapse)AuthorFilesLines
6 daystestsuite: Add tests for PR c/107419 and PR c++/107393H.J. Lu3-0/+50
Both C and C++ frontends should set a tentative TLS model in grokvardecl and update TLS mode with the default TLS access model after a TLS variable has been fully processed if the default TLS access model is stronger. PR c/107419 PR c++/107393 * c-c++-common/tls-attr-common.c: New test. * c-c++-common/tls-attr-le-pic.c: Likewise. * c-c++-common/tls-attr-le-pie.c: Likewise. Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
2025-08-26testsuite; Fix unprotected-allocas-1.c at -O3 [PR121684]Andrew Pinski1-2/+2
The problem here is after r16-101, the 2 functions containing alloca/VLA start to be cloned and then we un-VLA happens in using_vararray so this is no longer testing what it should be testing. The obvious fix is to mark using_vararray and using_alloca as noclone too. Pushed as obvious after a quick test to make sure it is now working. gcc/testsuite/ChangeLog: PR testsuite/121684 * c-c++-common/hwasan/unprotected-allocas-0.c: Mark using_vararray and using_alloca as noclone too. Signed-off-by: Andrew Pinski <andrew.pinski@oss.qualcomm.com>
2025-08-26OpenMP: Improve front-end error-checking for "declare variant"Sandra Loosemore1-2/+2
This patch fixes a number of problems with parser error checking of "declare variant", especially in the C front end. The new C testcase unprototyped-variant.c added by this patch used to ICE when gimplifying the call site, at least in part because the variant was being recorded even after it was diagnosed as invalid. There was also a large block of dead code in the C front end that was supposed to fix up an unprototyped declaration of a variant function to match the base function declaration, that was never executed because it was nested in a conditional that could never be true. I've fixed those problems by rearranging the code and only recording the variant if it passes the correctness checks. I also tried to add some comments and re-work some particularly confusing bits of code, so that it's easier to understand. The OpenMP specification doesn't say what the behavior of "declare variant" with the "append_args" clause should be when the base function is unprototyped. The additional arguments are supposed to be inserted between the last fixed argument of the base function and any varargs, but without a prototype, for any given call we have no idea which arguments are fixed and which are varargs, and therefore no idea where to insert the additional arguments. This used to trigger some other diagnostics (which one depending on whether the variant was also unprototyped), but I thought it was better to just reject this with an explicit "sorry". Finally, I also observed that a missing "match" clause was only rejected if "append_args" or "adjust_args" was present. Per the spec, "match" has the "required" property, so if it's missing it should be diagnosed unconditionally. The C++ and Fortran front ends had the same issue so I fixed this one there too. gcc/c/ChangeLog * c-parser.cc (c_finish_omp_declare_variant): Rework diagnostic code. Do not record variant if there are errors. Make check for a missing "match" clause unconditional. gcc/cp/ChangeLog * parser.cc (cp_finish_omp_declare_variant): Structure diagnostic code similarly to C front end. Make check for a missing "match" clause unconditional. gcc/fortran/ChangeLog * openmp.cc (gfc_match_omp_declare_variant): Make check for a missing "match" clause unconditional. gcc/testsuite/ChangeLog * c-c++-common/gomp/append-args-1.c: Adjust expected output. * g++.dg/gomp/adjust-args-1.C: Likewise. * g++.dg/gomp/adjust-args-3.C: Likewise. * gcc.dg/gomp/adjust-args-1.c: Likewise: * gcc.dg/gomp/append-args-1.c: Likewise. * gcc.dg/gomp/unprototyped-variant.c: New. * gfortran.dg/gomp/adjust-args-1.f90: Adjust expected output. * gfortran.dg/gomp/append_args-1.f90: Likewise.
2025-08-15c++: Warn on #undef/#define of remaining cpp.predefined macros [PR120778]Jakub Jelinek1-6/+6
We already warn on #undef or pedwarn on #define (but not on #define after #undef) of some builtin macros mentioned in cpp.predefined. The C++26 P2843R3 paper changes it from (compile time) undefined behavior to ill-formed. The following patch arranges for warning (for #undef) and pedwarn (on #define) for the remaining cpp.predefined macros. __cpp_* feature test macros only for C++20 which added some of them to cpp.predefined, in earlier C++ versions it was just an extension and for pedantic diagnostic I think we don't need to diagnose anything, __STDCPP_* and __cplusplus macros for all C++ versions where they appeared. Like the earlier posted -Wkeyword-macro diagnostics (which is done regardless whether the identifier is defined as a macro or not, obviously most likely none of the keywords are defined as macros initially), this one also warns on #undef when a macro isn't defined or later #define after #undef. 2025-08-15 Jakub Jelinek <jakub@redhat.com> PR preprocessor/120778 PR target/121520 gcc/c-family/ * c-cppbuiltin.cc (c_cpp_builtins): Implement C++26 DR 2581. Add cpp_define_warn lambda and use it as well as cpp_warn where needed. In the if (c_dialect_cxx ()) block with __cpp_* predefinitions add cpp_define lambda. Formatting fixes. gcc/c/ * c-decl.cc (c_init_decl_processing): Use cpp_warn instead of cpp_lookup and NODE_WARN bit setting. gcc/cp/ * lex.cc (cxx_init): Remove warn_on lambda. Use cpp_warn instead of cpp_lookup and NODE_WARN bit setting or warn_on. gcc/testsuite/ * g++.dg/DRs/dr2581-1.C: New test. * g++.dg/DRs/dr2581-2.C: New test. * c-c++-common/cpp/pr92296-2.c: Expect warnings also on defining special macros after undefining them. libcpp/ * include/cpplib.h (struct cpp_options): Add suppress_builtin_macro_warnings member. (cpp_warn): New inline functions. * init.cc (cpp_create_reader): Clear suppress_builtin_macro_warnings. (cpp_init_builtins): Call cpp_warn on __cplusplus, __STDC__, __STDC_VERSION__, __STDC_MB_MIGHT_NEQ_WC__ and __STDCPP_STRICT_POINTER_SAFETY__ when appropriate. * directives.cc (do_undef): Warn on undefining NODE_WARN macros if not cpp_keyword_p. Don't emit any NODE_WARN related diagnostics if CPP_OPTION (pfile, suppress_builtin_macro_warnings). (cpp_define, _cpp_define_builtin, cpp_undef): Temporarily set CPP_OPTION (pfile, suppress_builtin_macro_warnings) around run_directive calls. * macro.cc (_cpp_create_definition): Warn on defining NODE_WARN macros if they weren't previously defined and not cpp_keyword_p. Ignore NODE_WARN for diagnostics if CPP_OPTION (pfile, suppress_builtin_macro_warnings).
2025-08-08tailc: Handle other forms of finally_tmp.N conditional cleanups after ↵Jakub Jelinek4-0/+196
musttail [PR121389] My earlier r16-1886 PR120608 change incorrectly assumed that the finally_tmp.N vars introduced by eh pass will be only initialized to values 0 and 1 and there will be only EQ_EXPR/NE_EXPR comparisons of those. The following testcases show that is a bad assumption, the eh pass sets finally_tmp.N vars to 0 up to some highest index depending on hoiw many different exits there are from the finally region. And it emits then switch (finally_tmp.N) statement for all the different cases. So, if it uses more than 0/1 indexes, the lowering of the switch can turn it into a series of GIMPLE_CONDs, if (finally_tmp.N_M > 15) goto ... else goto ... if (finally_tmp.N_M > 7) goto ... else goto ... etc. (and that also means no longer single uses). And if unlucky, we can see a non-lowered GIMPLE_SWITCH as well. So, the following patch removes the assumption that it has to be 0/1 and EQ_EXPR/NE_EXPR, allows all the normal integral comparisons and handles GIMPLE_SWITCH too. 2025-08-08 Jakub Jelinek <jakub@redhat.com> PR middle-end/121389 * tree-tailcall.cc (find_tail_calls): For finally_tmp.N handle not just GIMPLE_CONDs with EQ_EXPR/NE_EXPR and only values 0 and 1, but arbitrary non-negative values, arbitrary comparisons in conditions and also GIMPLE_SWITCH next to GIMPLE_CONDs. * c-c++-common/asan/pr121389-1.c: New test. * c-c++-common/asan/pr121389-2.c: New test. * c-c++-common/asan/pr121389-3.c: New test. * c-c++-common/asan/pr121389-4.c: New test.
2025-08-06c++: Add test for vt/ff in line commentsJakub Jelinek2-0/+24
P2843R3 dropped the "If there is a form-feed or a vertical-tab character in such a comment, only whitespace characters shall appear between it and the new-line that terminates the comment; no diagnostic is required." sentence from [lex.comment]. AFAIK we've never diagnosed nor checked for that and C23 doesn't have anything like that, so the following testcase merely tests that we don't diagnose anything on it. 2025-08-06 Jakub Jelinek <jakub@redhat.com> PR preprocessor/120778 * c-c++-common/cpp/comment-ff-1.c: New test. * c-c++-common/cpp/comment-vtab-1.c: New test.
2025-08-06openmp: Add support for iterators in 'target update' clauses (C/C++)Kwok Cheung Yeung3-0/+61
This adds support for iterators in 'to' and 'from' clauses in the 'target update' OpenMP directive. gcc/c/ * c-parser.cc (c_parser_omp_clause_from_to): Parse 'iterator' modifier. * c-typeck.cc (c_finish_omp_clauses): Finish iterators for to/from clauses. gcc/cp/ * parser.cc (cp_parser_omp_clause_from_to): Parse 'iterator' modifier. * semantics.cc (finish_omp_clauses): Finish iterators for to/from clauses. gcc/ * gimplify.cc (remove_unused_omp_iterator_vars): Display unused variable warning for 'to' and 'from' clauses. (gimplify_scan_omp_clauses): Add argument for iterator loop sequence. Gimplify the clause decl and size into the iterator loop if iterators are used. (gimplify_omp_workshare): Add argument for iterator loops sequence in call to gimplify_scan_omp_clauses. (gimplify_omp_target_update): Call remove_unused_omp_iterator_vars and build_omp_iterators_loops. Add loop sequence as argument when calling gimplify_scan_omp_clauses, gimplify_adjust_omp_clauses and building the Gimple statement. * tree-pretty-print.cc (dump_omp_clause): Call dump_omp_iterators for to/from clauses with iterators. * tree.cc (omp_clause_num_ops): Add extra operand for OMP_CLAUSE_FROM and OMP_CLAUSE_TO. * tree.h (OMP_CLAUSE_HAS_ITERATORS): Add check for OMP_CLAUSE_TO and OMP_CLAUSE_FROM. (OMP_CLAUSE_ITERATORS): Likewise. gcc/testsuite/ * c-c++-common/gomp/target-update-iterators-1.c: New. * c-c++-common/gomp/target-update-iterators-2.c: New. * c-c++-common/gomp/target-update-iterators-3.c: New. libgomp/ * target.c (gomp_update): Call gomp_merge_iterator_maps. Free allocated variables. * testsuite/libgomp.c-c++-common/target-update-iterators-1.c: New. * testsuite/libgomp.c-c++-common/target-update-iterators-2.c: New. * testsuite/libgomp.c-c++-common/target-update-iterators-3.c: New.
2025-08-06openmp: Add support for iterators in map clauses (C/C++)Kwok Cheung Yeung5-10/+115
This adds preliminary support for iterators in map clauses within OpenMP 'target' constructs (which includes constructs such as 'target enter data'). Iterators with non-constant loop bounds are not currently supported. gcc/c/ * c-parser.cc (c_parser_omp_variable_list): Use location of the map expression as the clause location. (c_parser_omp_clause_map): Parse 'iterator' modifier. * c-typeck.cc (c_finish_omp_clauses): Finish iterators. Apply iterators to generated clauses. gcc/cp/ * parser.cc (cp_parser_omp_clause_map): Parse 'iterator' modifier. * semantics.cc (finish_omp_clauses): Finish iterators. Apply iterators to generated clauses. gcc/ * gimple-pretty-print.cc (dump_gimple_omp_target): Print expanded iterator loops. * gimple.cc (gimple_build_omp_target): Add argument for iterator loops sequence. Initialize iterator loops field. * gimple.def (GIMPLE_OMP_TARGET): Set GSS symbol to GSS_OMP_TARGET. * gimple.h (gomp_target): Set GSS symbol to GSS_OMP_TARGET. Add extra field for iterator loops. (gimple_build_omp_target): Add argument for iterator loops sequence. (gimple_omp_target_iterator_loops): New. (gimple_omp_target_iterator_loops_ptr): New. (gimple_omp_target_set_iterator_loops): New. * gimplify.cc (find_var_decl): New. (copy_omp_iterator): New. (remap_omp_iterator_var_1): New. (remap_omp_iterator_var): New. (remove_unused_omp_iterator_vars): New. (struct iterator_loop_info_t): New type. (iterator_loop_info_map_t): New type. (build_omp_iterators_loops): New. (enter_omp_iterator_loop_context_1): New. (enter_omp_iterator_loop_context): New. (enter_omp_iterator_loop_context): New. (exit_omp_iterator_loop_context): New. (gimplify_adjust_omp_clauses): Add argument for iterator loop sequence. Gimplify the clause decl and size into the iterator loop if iterators are used. (gimplify_omp_workshare): Call remove_unused_omp_iterator_vars and build_omp_iterators_loops for OpenMP target expressions. Add loop sequence as argument when calling gimplify_adjust_omp_clauses and building the Gimple statement. * gimplify.h (enter_omp_iterator_loop_context): New prototype. (exit_omp_iterator_loop_context): New prototype. * gsstruct.def (GSS_OMP_TARGET): New. * omp-low.cc (lower_omp_map_iterator_expr): New. (lower_omp_map_iterator_size): New. (finish_omp_map_iterators): New. (lower_omp_target): Add sorry if iterators used with deep mapping. Call lower_omp_map_iterator_expr before assigning to sender ref. Call lower_omp_map_iterator_size before setting the size. Insert iterator loop sequence before the statements for the target clause. * tree-nested.cc (convert_nonlocal_reference_stmt): Walk the iterator loop sequence of OpenMP target statements. (convert_local_reference_stmt): Likewise. (convert_tramp_reference_stmt): Likewise. * tree-pretty-print.cc (dump_omp_iterators): Dump extra iterator information if present. (dump_omp_clause): Call dump_omp_iterators for iterators in map clauses. * tree.cc (omp_clause_num_ops): Add operand for OMP_CLAUSE_MAP. (walk_tree_1): Do not walk last operand of OMP_CLAUSE_MAP. * tree.h (OMP_CLAUSE_HAS_ITERATORS): New. (OMP_CLAUSE_ITERATORS): New. gcc/testsuite/ * c-c++-common/gomp/map-6.c (foo): Amend expected error message. * c-c++-common/gomp/target-map-iterators-1.c: New. * c-c++-common/gomp/target-map-iterators-2.c: New. * c-c++-common/gomp/target-map-iterators-3.c: New. * c-c++-common/gomp/target-map-iterators-4.c: New. libgomp/ * target.c (kind_to_name): New. (gomp_merge_iterator_maps): New. (gomp_map_vars_internal): Call gomp_merge_iterator_maps. Copy address of only the first iteration to target vars. Free allocated variables. * testsuite/libgomp.c-c++-common/target-map-iterators-1.c: New. * testsuite/libgomp.c-c++-common/target-map-iterators-2.c: New. * testsuite/libgomp.c-c++-common/target-map-iterators-3.c: New. Co-authored-by: Andrew Stubbs <ams@baylibre.com>
2025-08-05libcpp: Add testcase for CWG2579 [PR120778]Jakub Jelinek1-5/+5
Another easy part from the paper. Part of the CWG2579 has been already done in an earlier paper (with test commits by Marek) and the remaining part is implemented correctly, we diagnose as error when token pasting doesn't form a valid token. Except that message pasting """" and """" does not give a valid preprocessing token looked weird and so I've updated the message to use %< and %> instead of \" quoting. 2025-08-05 Jakub Jelinek <jakub@redhat.com> PR preprocessor/120778 * macro.cc (paste_tokens): Use %< and %> instead of \" in diagnostics around %.*s. * g++.dg/DRs/dr2579.C: New test. * c-c++-common/cpp/va-opt-6.c: Expect ' rather than \" around tokens in incorrect pasting diagnostics. * gcc.dg/c23-attr-syntax-6.c: Likewise. * gcc.dg/cpp/paste12.c: Likewise. * gcc.dg/cpp/paste12-2.c: Likewise. * gcc.dg/cpp/paste14.c: Likewise. * gcc.dg/cpp/paste14-2.c: Likewise.
2025-07-29calls: Allow musttail calls to noreturn [PR121159]Jakub Jelinek1-0/+17
In the PR119483 r15-9003 change we've allowed musttail calls to noreturn functions, after all the decision not to normally tail call noreturn functions is not because it is not possible to tail call those, but because it screws up backtraces. As the following testcase shows, we've done that only for functions not declared [[noreturn]]/_Noreturn but later on discovered through IPA as noreturn. Functions explicitly declared [[noreturn]] have (for historical reasons) volatile FUNCTION_TYPE and the FUNCTION_DECLs are volatile as well, so in order to support those we shouldn't complain on ECF_NORETURN (we've stopped doing so for musttail in PR119483) but also shouldn't complain about TYPE_VOLATILE on their FUNCTION_TYPE (something that IPA doesn't change, I think it only sets TREE_THIS_VOLATILE on the FUNCTION_DECL). volatile on function type really means noreturn as well, it has no other meaning. 2025-07-29 Jakub Jelinek <jakub@redhat.com> PR middle-end/121159 * calls.cc (can_implement_as_sibling_call_p): Don't reject declared noreturn functions in musttail calls. * c-c++-common/pr121159.c: New test. * gcc.dg/plugin/must-tail-call-2.c (test_5): Don't expect an error.
2025-07-15c, c++: Fix unused result for empty types [PR82134]Jeremy Rifkin1-0/+16
Hi, This fixes PR c/82134 which concerns gcc emitting an incorrect unused result diagnostic for empty types. This diagnostic is emitted from tree-cfg.cc because of a couple code paths which attempt to avoid copying empty types, resulting in GIMPLE that isn't using the returned value of a call. To fix this I've added suppress_warning in three locations and a corresponding check in do_warn_unused_result. Cheers, Jeremy PR c/82134 gcc/cp/ChangeLog: * call.cc (build_call_a): Add suppress_warning * cp-gimplify.cc (cp_gimplify_expr): Add suppress_warning gcc/ChangeLog: * gimplify.cc (gimplify_modify_expr): Add suppress_warning * tree-cfg.cc (do_warn_unused_result): Check warning_suppressed_p gcc/testsuite/ChangeLog: * c-c++-common/attr-warn-unused-result-2.c: New test. Signed-off-by: Jeremy Rifkin <jeremy@rifkin.dev>
2025-07-15c, c++: Extend -Wunused-but-set-* warnings [PR44677]Jakub Jelinek13-2/+662
The -Wunused-but-set-* warnings work by using 2 bits on VAR_DECLs & PARM_DECLs, TREE_USED and DECL_READ_P. If neither is set, we typically emit -Wunused-variable or -Wunused-parameter warning, that is for variables which are just declared (including initializer) and completely unused. If TREE_USED is set and DECL_READ_P is unset, -Wunused-but-set-* warnings are emitted, i.e. for variables which can appear on the lhs of an assignment expression but aren't actually used elsewhere. The DECL_READ_P marking is done through mark_exp_read called from lots of places (e.g. lvalue to rvalue conversions etc.). LLVM has an extension on top of that in that it doesn't count pre/post inc/decrements as use (i.e. DECL_READ_P for GCC). The following patch does that too, though because we had the current behavior for 11+ years already and lot of people is -Wunused-but-set-* warning free in the current GCC behavior and not in the clang one (including GCC sources), it allows users to choose. Furthermore, it implements another level, where also var @= expr uses of var (except when it is also used in expr) aren't counted as DECL_READ_P. I think it would be nice to also handle var = var @ expr or var = expr @ var but unfortunately mark_exp_read is then done in both FEs during parsing of var @ expr or expr @ var and the code doesn't know it is rhs of an assignment with var as lhs. The patch works mostly by checking if DECL_READ_P is clear at some point and then clearing it again after some operation which might have set it. -Wunused or -Wall or -Wunused -Wextra or -Wall -Wextra turn on the 3 level of the new warning (i.e. the one which ignores also var++, ++var etc. as well as var @= expr), so does -Wunused-but-set-{variable,parameter}, but users can use explicit -Wunused-but-set-{variable,parameter}={1,2} to select a different level. 2025-07-15 Jakub Jelinek <jakub@redhat.com> Jason Merrill <jason@redhat.com> PR c/44677 gcc/ * common.opt (Wunused-but-set-parameter=, Wunused-but-set-variable=): New options. (Wunused-but-set-parameter, Wunused-but-set-variable): Turn into aliases. * common.opt.urls: Regenerate. * diagnostic-spec.cc (nowarn_spec_t::nowarn_spec_t): Use OPT_Wunused_but_set_variable_ instead of OPT_Wunused_but_set_variable and OPT_Wunused_but_set_parameter_ instead of OPT_Wunused_but_set_parameter. * gimple-ssa-store-merging.cc (find_bswap_or_nop_1): Remove unused but set variable tmp. * ipa-strub.cc (pass_ipa_strub::execute): Cast named_args to (void) if ATTR_FNSPEC_DECONST_WATERMARK is not defined. * doc/invoke.texi (Wunused-but-set-parameter=, Wunused-but-set-variable=): Document new options. (Wunused-but-set-parameter, Wunused-but-set-variable): Adjust documentation now that they are just aliases. gcc/c-family/ * c-opts.cc (c_common_post_options): Change warn_unused_but_set_parameter and warn_unused_but_set_variable from 1 to 3 if they were set only implicitly. * c-attribs.cc (build_attr_access_from_parms): Remove unused but set variable nelts. gcc/c/ * c-parser.cc (c_parser_unary_expression): Clear DECL_READ_P after default_function_array_read_conversion for -Wunused-but-set-{parameter,variable}={2,3} on PRE{IN,DE}CREMENT_EXPR argument. (c_parser_postfix_expression_after_primary): Similarly for POST{IN,DE}CREMENT_EXPR. * c-decl.cc (pop_scope): Use OPT_Wunused_but_set_variable_ instead of OPT_Wunused_but_set_variable. (finish_function): Use OPT_Wunused_but_set_parameter_ instead of OPT_Wunused_but_set_parameter. * c-typeck.cc (mark_exp_read): Handle {PRE,POST}{IN,DE}CREMENT_EXPR and don't handle it when cast to void. (build_modify_expr): Clear DECL_READ_P after build_binary_op for -Wunused-but-set-{parameter,variable}=3. gcc/cp/ * cp-gimplify.cc (cp_fold): Clear DECL_READ_P on lhs of MODIFY_EXPR after cp_fold_rvalue if it wasn't set before. * decl.cc (poplevel): Use OPT_Wunused_but_set_variable_ instead of OPT_Wunused_but_set_variable. (finish_function): Use OPT_Wunused_but_set_parameter_ instead of OPT_Wunused_but_set_parameter. * expr.cc (mark_use): Clear read_p for {PRE,POST}{IN,DE}CREMENT_EXPR cast to void on {VAR,PARM}_DECL for -Wunused-but-set-{parameter,variable}={2,3}. (mark_exp_read): Handle {PRE,POST}{IN,DE}CREMENT_EXPR and don't handle it when cast to void. * module.cc (trees_in::fn_parms_fini): Remove unused but set variable ix. * semantics.cc (finish_unary_op_expr): Return early for PRE{IN,DE}CREMENT_EXPR. * typeck.cc (cp_build_unary_op): Clear DECL_READ_P after mark_lvalue_use for -Wunused-but-set-{parameter,variable}={2,3} on PRE{IN,DE}CREMENT_EXPR argument. (cp_build_modify_expr): Clear DECL_READ_P after cp_build_binary_op for -Wunused-but-set-{parameter,variable}=3. gcc/go/ * gofrontend/gogo.cc (Function::export_func_with_type): Remove unused but set variable i. gcc/cobol/ * gcobolspec.cc (lang_specific_driver): Remove unused but set variable n_cobol_files. gcc/testsuite/ * c-c++-common/Wunused-parm-1.c: New test. * c-c++-common/Wunused-parm-2.c: New test. * c-c++-common/Wunused-parm-3.c: New test. * c-c++-common/Wunused-parm-4.c: New test. * c-c++-common/Wunused-parm-5.c: New test. * c-c++-common/Wunused-parm-6.c: New test. * c-c++-common/Wunused-var-7.c (bar, baz): Expect warning on a. * c-c++-common/Wunused-var-19.c: New test. * c-c++-common/Wunused-var-20.c: New test. * c-c++-common/Wunused-var-21.c: New test. * c-c++-common/Wunused-var-22.c: New test. * c-c++-common/Wunused-var-23.c: New test. * c-c++-common/Wunused-var-24.c: New test. * g++.dg/cpp26/name-independent-decl1.C (foo): Expect one set but not used warning. * g++.dg/warn/Wunused-parm-12.C: New test. * g++.dg/warn/Wunused-parm-13.C: New test. * g++.dg/warn/Wunused-var-2.C (f2): Expect set but not used warning on parameter x and variable a. * g++.dg/warn/Wunused-var-40.C: New test. * g++.dg/warn/Wunused-var-41.C: New test. * gcc.dg/memchr-3.c (test_find): Change return type from void to int, and add return n; statement. * gcc.dg/unused-9.c (g): Move dg-bogus to the correct line and expect a warning on i.
2025-07-11testsuite: Add testcase for already fixed PR [PR120954]Jakub Jelinek1-0/+21
This was a regression introduced by r16-1893 (and its backports) for C++, though for C it had false positive warning for years. Fixed by r16-2000 (and its backports). 2025-07-11 Jakub Jelinek <jakub@redhat.com> PR c++/120954 * c-c++-common/Warray-bounds-11.c: New test.
2025-07-11ipa: Disallow signature changes in fun->has_musttail functions [PR121023]Jakub Jelinek1-0/+23
As the following testcase shows e.g. on ia32, letting IPA opts change signature of functions which have [[{gnu,clang}::musttail]] calls can turn programs that would be compiled normally into something that is rejected because the caller has fewer argument stack slots than the function being tail called. The following patch prevents signature changes for such functions. It is perhaps too big hammer in some cases, but it might be hard to try to figure out what signature changes are still acceptable and which are not at IPA time. 2025-07-11 Jakub Jelinek <jakub@redhat.com> Martin Jambor <mjambor@suse.cz> PR ipa/121023 * ipa-fnsummary.cc (compute_fn_summary): Disallow signature changes on cfun->has_musttail functions. * c-c++-common/musttail32.c: New test.
2025-07-03OpenMP: Add omp_get_initial_device/omp_get_num_devices builtins: Fix test casesThomas Schwinge1-2/+2
With this fix-up for commit 387209938d2c476a67966c6ddbdbf817626f24a2 "OpenMP: Add omp_get_initial_device/omp_get_num_devices builtins", we progress: PASS: c-c++-common/gomp/omp_get_num_devices_initial_device.c (test for excess errors) PASS: c-c++-common/gomp/omp_get_num_devices_initial_device.c scan-tree-dump-not optimized "abort" -FAIL: c-c++-common/gomp/omp_get_num_devices_initial_device.c scan-tree-dump-times optimized "omp_get_num_devices;" 1 +PASS: c-c++-common/gomp/omp_get_num_devices_initial_device.c scan-tree-dump-times optimized "omp_get_num_devices" 1 PASS: c-c++-common/gomp/omp_get_num_devices_initial_device.c scan-tree-dump optimized "_1 = __builtin_omp_get_num_devices \\(\\);[\\r\\n]+[ ]+return _1;" ... etc. for offloading configurations. gcc/testsuite/ * c-c++-common/gomp/omp_get_num_devices_initial_device.c: Fix. * gfortran.dg/gomp/omp_get_num_devices_initial_device.f90: Likewise.
2025-07-01tailc: Handle musttail in case of non-cleaned-up cleanups, especially ASan ↵Jakub Jelinek2-0/+66
related [PR120608] The following testcases FAIL at -O0 -fsanitize=address. The problem is we end up with something like _26 = foo (x_24(D)); [must tail call] // predicted unlikely by early return (on trees) predictor. finally_tmp.3_27 = 0; goto <bb 5>; [INV] ... <bb 5> : # _6 = PHI <_26(3), _23(D)(4)> # finally_tmp.3_8 = PHI <finally_tmp.3_27(3), finally_tmp.3_22(4)> .ASAN_MARK (POISON, &c, 4); if (finally_tmp.3_8 == 1) goto <bb 7>; [INV] else goto <bb 6>; [INV] <bb 6> : <L4>: finally_tmp.4_31 = 0; goto <bb 8>; [INV] ... <bb 8> : # finally_tmp.4_9 = PHI <finally_tmp.4_31(6), finally_tmp.4_30(7)> .ASAN_MARK (POISON, &b, 4); if (finally_tmp.4_9 == 1) goto <bb 9>; [INV] else goto <bb 10>; [INV] ... <bb 10> : # _7 = PHI <_6(8), _34(9)> .ASAN_MARK (POISON, &a, 4); <bb 11> : <L11>: return _7; before the sanopt pass. This is -O0, we don't try to do forward propagation, jump threading etc. And what is worse, the sanopt pass lowers the .ASAN_MARK calls that the tailc/musttail passes already handle into somewthing that they can't easily pattern match. The following patch fixes that by 1) moving the musttail pass 2 passes earlier (this is mostly just for -O0/-Og, for normal optimization levels musttail calls are handled in the tailc pass), i.e. across the sanopt and cleanup_eh passes 2) recognizes these finally_tmp SSA_NAME assignments, PHIs using those and GIMPLE_CONDs deciding based on those both on the backwards walk (when we start from the edges to EXIT) and forwards walk (when we find a candidate tail call and process assignments after those up to the return statement). For backwards walk, ESUCC argument has been added which is either NULL for the noreturn musttail case, or the succ edge through which we've reached bb and if it sees GIMPLE_COND with such comparison, based on the ESUCC and comparison it will remember which later edges to ignore later on and which bb must be walked up to the start during tail call discovery (the one with the PHI). 3) the move of musttail pass across cleanup_eh pass resulted in g++.dg/opt/pr119613.C regressions but moving cleanup_eh before sanopt doesn't work too well, so I've extended empty_eh_cleanup to also handle resx which doesn't throw externally I know moving a pass on release branches feels risky, though the musttail pass is only relevant to functions with musttail calls, so something quite rare and only at -O0/-Og (unless one e.g. disables the tailc pass). 2025-07-01 Jakub Jelinek <jakub@redhat.com> PR middle-end/120608 * passes.def (pass_musttail): Move before pass_sanopt. * tree-tailcall.cc (empty_eh_cleanup): Handle GIMPLE_RESX which doesn't throw externally through recursion on single eh edge (if any and cnt still allows that). (find_tail_calls): Add ESUCC, IGNORED_EDGES and MUST_SEE_BBS arguments. Handle GIMPLE_CONDs for non-simplified cleanups with finally_tmp temporaries both on backward and forward walks, adjust recursive call. (tree_optimize_tail_calls_1): Adjust find_tail_calls callers. * c-c++-common/asan/pr120608-3.c: New test. * c-c++-common/asan/pr120608-4.c: New test. * g++.dg/asan/pr120608-3.C: New test. * g++.dg/asan/pr120608-4.C: New test.
2025-06-30diagnostics: remove "json" output formatDavid Malcolm9-204/+0
The "json" output format for diagnostics was deprecated in GCC 15, with advice to users seeking machine-readable diagnostics from GCC to use SARIF instead. This patch eliminates it from GCC 16, simplifying the diagnostics subsystem somewhat. Note that the Ada frontend seems to have its own implementation of this in errout.adb (Output_JSON_Message), and documented in gnat_ugn.texi. This patch does not touch Ada. gcc/ChangeLog: * Makefile.in (OBJS-libcommon): Drop diagnostic-format-json.o. * common.opt (fdiagnostics-format=): Drop "json|json-stderr|json-file". (diagnostics_output_format): Drop values "json", "json-stderr", and "json-file". * diagnostic-format-json.cc: Delete file. * diagnostic-format.h (diagnostic_output_format_init_json_stderr): Delete. (diagnostic_output_format_init_json_file): Delete. * diagnostic.cc (diagnostic_output_format_init): Delete cases for DIAGNOSTICS_OUTPUT_FORMAT_JSON_STDERR and DIAGNOSTICS_OUTPUT_FORMAT_JSON_FILE. * diagnostic.h (DIAGNOSTICS_OUTPUT_FORMAT_JSON_STDERR): Delete. (DIAGNOSTICS_OUTPUT_FORMAT_JSON_FILE): Delete. * doc/invoke.texi: Remove references to json output format. * doc/ux.texi: Likewise. * selftest-run-tests.cc (selftest::run_tests): Drop call to deleted selftest::diagnostic_format_json_cc_tests. * selftest.h (selftest::diagnostic_format_json_cc_tests): Delete. gcc/testsuite/ChangeLog: * c-c++-common/analyzer/out-of-bounds-diagram-1-json.c: Deleted test. * c-c++-common/diagnostic-format-json-1.c: Deleted test. * c-c++-common/diagnostic-format-json-2.c: Deleted test. * c-c++-common/diagnostic-format-json-3.c: Deleted test. * c-c++-common/diagnostic-format-json-4.c: Deleted test. * c-c++-common/diagnostic-format-json-5.c: Deleted test. * c-c++-common/diagnostic-format-json-file-1.c: Deleted test. * c-c++-common/diagnostic-format-json-stderr-1.c: Deleted test. * c-c++-common/pr106133.c: Deleted test. * g++.dg/pr90462.C: Deleted test. * gcc.dg/plugin/diagnostic-test-paths-3.c: Deleted test. * gcc.dg/plugin/plugin.exp (plugin_test_list): Remove deleted test. * gfortran.dg/diagnostic-format-json-1.F90: Deleted test. * gfortran.dg/diagnostic-format-json-2.F90: Deleted test. * gfortran.dg/diagnostic-format-json-3.F90: Deleted test. * gfortran.dg/diagnostic-format-json-pr105916.F90: Deleted test. Signed-off-by: David Malcolm <dmalcolm@redhat.com>
2025-06-30Extend nonnull_if_nonzero attribute [PR120520]Jakub Jelinek2-0/+73
C2Y voted in the https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3466.pdf paper, which clarifies some of the conditional nonnull cases. For strncat/__strncat_chk no changes are necessary, we already use __attribute__((nonnull (1), nonnull_if_nonzero (2, 3))) attributes on the builtin and glibc can do the same too, meaning that first argument must be nonnull always and second must be nonnull if the third one is nonzero. The problem is with the fread/fwrite changes, where the paper adds: If size or nmemb is zero, +ptr may be a null pointer, fread returns zero and the contents of the array and the state of the stream remain unchanged. and ditto for fwrite, so the two argument nonnull_if_nonzero attribute isn't usable to express that, because whether the pointer can be null depends on 2 integral arguments rather than one. The following patch extends the nonnull_if_nonzero attribute, so that instead of requiring 2 arguments it allows 2 or 3, the first one is still the pointer argument index which sometimes must not be null and the other one or two are integral arguments, if there are 2, the invalid case is only if pointer is null and both the integral arguments are nonzero. 2025-06-30 Jakub Jelinek <jakub@redhat.com> PR c/120520 PR c/117023 gcc/ * builtin-attrs.def (DEF_LIST_INT_INT_INT): Define it and use for 1,2,3. (ATTR_NONNULL_IF123_LIST): New DEF_ATTR_TREE_LIST. (ATTR_NONNULL_4_IF123_LIST): Likewise. * builtins.def (BUILT_IN_FWRITE): Use ATTR_NONNULL_4_IF123_LIST instead of ATTR_NONNULL_LIST. (BUILT_IN_FWRITE_UNLOCKED): Likewise. * gimple.h (infer_nonnull_range_by_attribute): Add another optional tree * argument defaulted to NULL. * gimple.cc (infer_nonnull_range_by_attribute): Add OP3 argument, handle 3 argument nonnull_if_nonzero attribute. * builtins.cc (validate_arglist): Handle 3 argument nonnull_if_nonzero attribute. * tree-ssa-ccp.cc (pass_post_ipa_warn::execute): Likewise. * ubsan.cc (instrument_nonnull_arg): Adjust infer_nonnull_range_by_attribute caller, handle 3 argument nonnull_if_nonzero attribute. * gimple-range-infer.cc (gimple_infer_range::gimple_infer_range): Handle 3 argument nonnull_if_nonzero attribute. * doc/extend.texi (nonnull_if_nonzero): Document 3 argument version of the attribute. gcc/c-family/ * c-attribs.cc (c_common_gnu_attributes): Allow 2 or 3 arguments for nonnull_if_nonzero attribute instead of only 2. (handle_nonnull_if_nonzero_attribute): Handle 3 argument nonnull_if_nonzero. * c-common.cc (struct nonnull_arg_ctx): Rename other member to other1, add other2 member. (check_function_nonnull): Clear a if nonnull attribute has an argument. Adjust for nonnull_arg_ctx changes. Handle 3 argument nonnull_if_nonzero attribute. (check_nonnull_arg): Adjust for nonnull_arg_ctx changes, emit different diagnostics for 3 argument nonnull_if_nonzero attributes. (check_function_arguments): Adjust ctx var initialization. gcc/analyzer/ * sm-malloc.cc (malloc_state_machine::on_stmt): Handle 3 argument nonnull_if_nonzero attribute. gcc/testsuite/ * gcc.dg/nonnull-9.c: Tweak for 3 argument nonnull_if_nonzero attribute support, add further tests. * gcc.dg/nonnull-12.c: New test. * gcc.dg/nonnull-13.c: New test. * gcc.dg/nonnull-14.c: New test. * c-c++-common/ubsan/nonnull-8.c: New test. * c-c++-common/ubsan/nonnull-9.c: New test.
2025-06-23OpenACC: Add 'if' clause to 'acc wait' directiveTobias Burnus1-0/+51
OpenACC 3.0 added the 'if' clause to four directives; this patch only adds it to 'acc wait'. gcc/c-family/ChangeLog: * c-omp.cc (c_finish_oacc_wait): Handle if clause. gcc/c/ChangeLog: * c-parser.cc (OACC_WAIT_CLAUSE_MASK): Add if clause. gcc/cp/ChangeLog: * parser.cc (OACC_WAIT_CLAUSE_MASK): Ass if clause. gcc/fortran/ChangeLog: * openmp.cc (OACC_WAIT_CLAUSES): Add if clause. * trans-openmp.cc (gfc_trans_oacc_wait_directive): Handle it. gcc/testsuite/ChangeLog: * c-c++-common/goacc/acc-wait-1.c: New test. * gfortran.dg/goacc/acc-wait-1.f90: New test.
2025-06-23tailc: Allow musttail tail calls with -fsanitize=address [PR120608]Jakub Jelinek2-0/+82
These testcases show another problem with -fsanitize=address vs. musttail tail calls. In particular, there can be .ASAN_MARK (POISON, &a, 4); etc. calls after a tail call and those just prevent the tailc pass to mark the musttail calls as [tail call]. Normally, the sanopt pass (which comes after tailc) will optimize those away, the optimization is if there are no .ASAN_CHECK calls or normal function calls dominated by those .ASAN_MARK (POSION, ...) calls, the poison is not needed, because in the epilog sequence (the one dealt with in the patch posted earlier today) all the stack slots are unpoisoned anyway (or poisoned for use-after-return). Unlike __builtin_tsan_exit_function, .ASAN_MARK is not a real function and is always expanded inline, so can be never tail called successfully, so the patch just ignores those for the cfun->has_musttail && diag_musttail cases. If there is a non-musttail call, it will fail worst case during expansion because there is the epilog asan sequence. 2025-06-12 Jakub Jelinek <jakub@redhat.com> PR middle-end/120608 * tree-tailcall.cc (empty_eh_cleanup): Ignore .ASAN_MARK (POISON) internal calls for the cfun->has_musttail case and diag_musttail. (find_tail_calls): Likewise. * c-c++-common/asan/pr120608-1.c: New test. * c-c++-common/asan/pr120608-2.c: New test.
2025-06-11c/c++: Handle '#pragma GCC target optimize' early [PR48026]Gwenole Beauchesne1-0/+10
Handle '#pragma GCC optimize' earlier as the __OPTIMIZE__ macro may need to be defined as well for certain usages. Add additional tests for the '#pragma GCC target' case with auto-vectorization enabled and multiple combinations of namespaces and/or class member functions. This is similar to what was done for `#pramga GCC target` in r14-4967-g8697d3a1dcf327, to fix the similar issue there. Add more complete tests for PR c++/41201 after git commit r14-4967-g8697d3a1dcf327. PR c++/41201 PR c++/48026 gcc/c-family/ChangeLog: * c-pragma.cc (init_pragma): Use c_register_pragma_with_early_handler instead of c_register_pragma for `#pragma GCC optimize`. gcc/testsuite/ChangeLog: * c-c++-common/pragma-optimize-1.c: New test. * g++.target/i386/vect-pragma-target-1.C: New test. * g++.target/i386/vect-pragma-target-2.C: New test. * gcc.target/i386/vect-pragma-target-1.c: New test. * gcc.target/i386/vect-pragma-target-2.c: New test. Signed-off-by: Gwenole Beauchesne <gb.devel@gmail.com> Co-authored-by: Andrew Pinski <quic_apinski@quicinc.com>
2025-06-06OpenMP: Add omp_get_initial_device/omp_get_num_devices builtinsTobias Burnus2-0/+61
By adding omp_get_initial_device and omp_get_num_devices builtins for C, C++, and Fortran, the following can be achieved: * By making them pure, multiple calls can be avoiding in some cases. * Some comparisons can be optimized at compile time. omp_get_initial_device will be converted to omp_get_num_devices for consistency; note that OpenMP 6 also permits omp_initial_device (== -1) as value. If GCC has not been configure for offloading, either intrinsic will leads to 0 - and on the offload side, -1 (= omp_initial_device) is returned for omp_initial_device. gcc/fortran/ChangeLog: * f95-lang.cc (ATTR_PURE_NOTHROW_LIST): Define. * trans-expr.cc (get_builtin_fn): Handle omp_get_num_devices and omp_get_intrinsic_device. * gfortran.h (gfc_option_t): Add disable_omp_... for them. * options.cc (gfc_handle_option): Handle them with -fno-builtin-. gcc/ChangeLog: * gimple-fold.cc (gimple_fold_builtin_omp_get_initial_device, gimple_fold_builtin_omp_get_num_devices): New. (gimple_fold_builtin): Call them. * omp-builtins.def (BUILT_IN_OMP_GET_INITIAL_DEVICE): Add (BUILT_IN_OMP_GET_NUM_DEVICES): Make uservisible + pure. libgomp/ChangeLog: * libgomp.texi (omp_get_num_devices, omp_get_intrinsic_device): Document builtin handling. gcc/testsuite/ChangeLog: * c-c++-common/gomp/omp_get_num_devices_initial_device-2.c: New test. * c-c++-common/gomp/omp_get_num_devices_initial_device.c: New test. * gfortran.dg/gomp/omp_get_num_devices_initial_device-2.f90: New test. * gfortran.dg/gomp/omp_get_num_devices_initial_device.f90: New test. Co-authored-by: Sandra Loosemore <sloosemore@baylibre.com>
2025-06-02OpenMP: Handle more cases in user/condition selectorSandra Loosemore4-3/+46
Tobias had noted that the C front end was not treating C23 constexprs as constant in the user/condition selector property, which led to missed opportunities to resolve metadirectives at parse time. Additionally neither C nor C++ was permitting the expression to have pointer or floating-point type -- the former being a common idiom in other C/C++ conditional expressions. By using the existing front-end hooks for the implicit conversion to bool in conditional expressions, we also get free support for using a C++ class object that has a bool conversion operator in the user/condition selector. gcc/c/ChangeLog * c-parser.cc (c_parser_omp_context_selector): Call convert_lvalue_to_rvalue and c_objc_common_truthvalue_conversion on the expression for OMP_TRAIT_PROPERTY_BOOL_EXPR. gcc/cp/ChangeLog * cp-tree.h (maybe_convert_cond): Declare. * parser.cc (cp_parser_omp_context_selector): Call maybe_convert_cond and fold_build_cleanup_point_expr on the expression for OMP_TRAIT_PROPERTY_BOOL_EXPR. * pt.cc (tsubst_omp_context_selector): Likewise. * semantics.cc (maybe_convert_cond): Remove static declaration. gcc/testsuite/ChangeLog * c-c++-common/gomp/declare-variant-2.c: Update expected output. * c-c++-common/gomp/metadirective-condition-constexpr.c: New. * c-c++-common/gomp/metadirective-condition.c: New. * c-c++-common/gomp/metadirective-error-recovery.c: Update expected output. * g++.dg/gomp/metadirective-condition-class.C: New. * g++.dg/gomp/metadirective-condition-template.C: New.
2025-05-30OpenMP: Support OpenMP 5.0 "declare mapper" directives for CJulian Brown11-18/+88
This patch adds support for "declare mapper" directives (and the "mapper" modifier on "map" clauses) for C. gcc/c/ChangeLog: * c-decl.cc (c_omp_mapper_id, c_omp_mapper_decl, c_omp_mapper_lookup, c_omp_extract_mapper_directive, c_omp_map_array_section, c_omp_scan_mapper_bindings_r, c_omp_scan_mapper_bindings): New functions. * c-objc-common.h (LANG_HOOKS_OMP_FINISH_MAPPER_CLAUSES, LANG_HOOKS_OMP_MAPPER_LOOKUP, LANG_HOOKS_OMP_EXTRACT_MAPPER_DIRECTIVE, LANG_HOOKS_OMP_MAP_ARRAY_SECTION): Define langhooks for C. * c-parser.cc (c_parser_omp_clause_map): Add declare_mapper_p parameter; handle mapper modifier. (c_parser_omp_all_clauses): Update call to c_parser_omp_clause_map. (c_parser_omp_target): Instantiate explicit mappers and record bindings for implicit mappers. (c_parser_omp_declare_mapper): Parse "declare mapper" directives. (c_parser_omp_declare): Support "declare mapper". (c_parser_omp_declare_reduction): Use inform not error_at. * c-tree.h (c_omp_finish_mapper_clauses, c_omp_mapper_lookup, c_omp_extract_mapper_directive, c_omp_map_array_section, c_omp_mapper_id, c_omp_mapper_decl, c_omp_scan_mapper_bindings, c_omp_instantiate_mappers): Add prototypes. * c-typeck.cc (c_finish_omp_clauses): Handle GOMP_MAP_PUSH_MAPPER_NAME and GOMP_MAP_POP_MAPPER_NAME. (c_omp_finish_mapper_clauses): New function (langhook). libgomp/ChangeLog: * testsuite/libgomp.c-c++-common/declare-mapper-9.c: Enable for C. * testsuite/libgomp.c-c++-common/declare-mapper-10.c: Likewise. * testsuite/libgomp.c-c++-common/declare-mapper-11.c: Likewise. * testsuite/libgomp.c-c++-common/declare-mapper-12.c: Likewise. * testsuite/libgomp.c-c++-common/declare-mapper-13.c: Likewise. * testsuite/libgomp.c-c++-common/declare-mapper-14.c: Likewise. gcc/testsuite/ChangeLog: * c-c++-common/gomp/declare-mapper-3.c: Enable for C. * c-c++-common/gomp/declare-mapper-4.c: Likewise. * c-c++-common/gomp/declare-mapper-5.c: Likewise. * c-c++-common/gomp/declare-mapper-6.c: Likewise. * c-c++-common/gomp/declare-mapper-7.c: Likewise. * c-c++-common/gomp/declare-mapper-8.c: Likewise. * c-c++-common/gomp/declare-mapper-9.c: Likewise. * c-c++-common/gomp/declare-mapper-10.c: Likewise. * c-c++-common/gomp/declare-mapper-12.c: Likewise. * c-c++-common/gomp/map-6.c: Update dg-error. * gcc.dg/gomp/udr-3.c: Update for change to dg-note. * c-c++-common/gomp/declare-mapper-11.c: New. * gcc.dg/gomp/declare-mapper-10.c: New test. * gcc.dg/gomp/declare-mapper-11.c: New test. * gcc.dg/gomp/declare-mapper-13.c: New test.
2025-05-30OpenMP: C++ "declare mapper" supportJulian Brown10-10/+325
This patch adds support for OpenMP 5.0 "declare mapper" functionality for C++. I've merged it to og13 based on the last version posted upstream, with some minor changes due to the newly-added 'present' map modifier support. There's also a fix to splay-tree traversal in gimplify.cc:omp_instantiate_implicit_mappers, and this patch omits the rearrangement of gimplify.cc:gimplify_{scan,adjust}_omp_clauses that I separated out into its own patch and applied (to og13) already. gcc/c-family/ * c-common.h (c_omp_region_type): Add C_ORT_DECLARE_MAPPER and C_ORT_OMP_DECLARE_MAPPER codes. (omp_mapper_list): Add forward declaration. (c_omp_find_nested_mappers, c_omp_instantiate_mappers): Add prototypes. * c-omp.cc (c_omp_find_nested_mappers): New function. (remap_mapper_decl_info): New struct. (remap_mapper_decl_1, omp_instantiate_mapper, c_omp_instantiate_mappers): New functions. gcc/cp/ * constexpr.cc (reduced_constant_expression_p): Add OMP_DECLARE_MAPPER case. (cxx_eval_constant_expression, potential_constant_expression_1): Likewise. * cp-gimplify.cc (cxx_omp_finish_mapper_clauses): New function. * cp-objcp-common.h (LANG_HOOKS_OMP_FINISH_MAPPER_CLAUSES, LANG_HOOKS_OMP_MAPPER_LOOKUP, LANG_HOOKS_OMP_EXTRACT_MAPPER_DIRECTIVE, LANG_HOOKS_OMP_MAP_ARRAY_SECTION): Define langhooks. * cp-tree.h (lang_decl_base): Add omp_declare_mapper_p field. Recount spare bits comment. (DECL_OMP_DECLARE_MAPPER_P): New macro. (omp_mapper_id): Add prototype. (cp_check_omp_declare_mapper): Add prototype. (omp_instantiate_mappers): Add prototype. (cxx_omp_finish_mapper_clauses): Add prototype. (cxx_omp_mapper_lookup): Add prototype. (cxx_omp_extract_mapper_directive): Add prototype. (cxx_omp_map_array_section): Add prototype. * decl.cc (check_initializer): Add OpenMP declare mapper support. (cp_finish_decl): Set DECL_INITIAL for OpenMP declare mapper var decls as appropriate. * decl2.cc (mark_used): Instantiate OpenMP "declare mapper" magic var decls. * error.cc (dump_omp_declare_mapper): New function. (dump_simple_decl): Use above. * parser.cc (cp_parser_omp_clause_map): Add KIND parameter. Support "mapper" modifier. (cp_parser_omp_all_clauses): Add KIND argument to cp_parser_omp_clause_map call. (cp_parser_omp_target): Call omp_instantiate_mappers before finish_omp_clauses. (cp_parser_omp_declare_mapper): New function. (cp_parser_omp_declare): Add "declare mapper" support. * pt.cc (tsubst_decl): Adjust name of "declare mapper" magic var decls once we know their type. (tsubst_omp_clauses): Call omp_instantiate_mappers before finish_omp_clauses, for target regions. (tsubst_expr): Support OMP_DECLARE_MAPPER nodes. (instantiate_decl): Instantiate initialiser (i.e definition) for OpenMP declare mappers. * semantics.cc (gimplify.h): Include. (omp_mapper_id, omp_mapper_lookup, omp_extract_mapper_directive, cxx_omp_map_array_section, cp_check_omp_declare_mapper): New functions. (finish_omp_clauses): Delete GOMP_MAP_PUSH_MAPPER_NAME and GOMP_MAP_POP_MAPPER_NAME artificial clauses. (omp_target_walk_data): Add MAPPERS field. (finish_omp_target_clauses_r): Scan for uses of struct/union/class type variables. (finish_omp_target_clauses): Create artificial mapper binding clauses for used structs/unions/classes in offload region. gcc/fortran/ * parse.cc (tree.h, fold-const.h, tree-hash-traits.h): Add includes (for additions to omp-general.h). gcc/ * gimplify.cc (gimplify_omp_ctx): Add IMPLICIT_MAPPERS field. (new_omp_context): Initialise IMPLICIT_MAPPERS hash map. (delete_omp_context): Delete IMPLICIT_MAPPERS hash map. (instantiate_mapper_info): New structs. (remap_mapper_decl_1, omp_mapper_copy_decl, omp_instantiate_mapper, omp_instantiate_implicit_mappers): New functions. (gimplify_scan_omp_clauses): Handle MAPPER_BINDING clauses. (gimplify_adjust_omp_clauses): Instantiate implicit declared mappers. (gimplify_omp_declare_mapper): New function. (gimplify_expr): Call above function. * langhooks-def.h (lhd_omp_mapper_lookup, lhd_omp_extract_mapper_directive, lhd_omp_map_array_section): Add prototypes. (LANG_HOOKS_OMP_FINISH_MAPPER_CLAUSES, LANG_HOOKS_OMP_MAPPER_LOOKUP, LANG_HOOKS_OMP_EXTRACT_MAPPER_DIRECTIVE, LANG_HOOKS_OMP_MAP_ARRAY_SECTION): Define macros. (LANG_HOOK_DECLS): Add above macros. * langhooks.cc (lhd_omp_mapper_lookup, lhd_omp_extract_mapper_directive, lhd_omp_map_array_section): New dummy functions. * langhooks.h (lang_hooks_for_decls): Add OMP_FINISH_MAPPER_CLAUSES, OMP_MAPPER_LOOKUP, OMP_EXTRACT_MAPPER_DIRECTIVE, OMP_MAP_ARRAY_SECTION hooks. * omp-general.h (omp_name_type<T>): Add templatized struct, hash type traits (for omp_name_type<tree> specialization). (omp_mapper_list<T>): Add struct. * tree-core.h (omp_clause_code): Add OMP_CLAUSE__MAPPER_BINDING_. * tree-pretty-print.cc (dump_omp_clause): Support GOMP_MAP_UNSET, GOMP_MAP_PUSH_MAPPER_NAME, GOMP_MAP_POP_MAPPER_NAME artificial mapping clauses. Support OMP_CLAUSE__MAPPER_BINDING_ and OMP_DECLARE_MAPPER. * tree.cc (omp_clause_num_ops, omp_clause_code_name): Add OMP_CLAUSE__MAPPER_BINDING_. * tree.def (OMP_DECLARE_MAPPER): New tree code. * tree.h (OMP_DECLARE_MAPPER_ID, OMP_DECLARE_MAPPER_DECL, OMP_DECLARE_MAPPER_CLAUSES): New defines. (OMP_CLAUSE__MAPPER_BINDING__ID, OMP_CLAUSE__MAPPER_BINDING__DECL, OMP_CLAUSE__MAPPER_BINDING__MAPPER): New defines. include/ * gomp-constants.h (gomp_map_kind): Add GOMP_MAP_UNSET, GOMP_MAP_PUSH_MAPPER_NAME, GOMP_MAP_POP_MAPPER_NAME artificial mapping clause types. gcc/testsuite/ * c-c++-common/gomp/map-6.c: Update error scan output. * c-c++-common/gomp/declare-mapper-3.c: New test (only enabled for C++ for now). * c-c++-common/gomp/declare-mapper-4.c: Likewise. * c-c++-common/gomp/declare-mapper-5.c: Likewise. * c-c++-common/gomp/declare-mapper-6.c: Likewise. * c-c++-common/gomp/declare-mapper-7.c: Likewise. * c-c++-common/gomp/declare-mapper-8.c: Likewise. * c-c++-common/gomp/declare-mapper-9.c: Likewise. * c-c++-common/gomp/declare-mapper-10.c: Likewise. * c-c++-common/gomp/declare-mapper-12.c: Likewise. * g++.dg/gomp/declare-mapper-1.C: New test. * g++.dg/gomp/declare-mapper-2.C: New test. * g++.dg/gomp/declare-mapper-3.C: New test. libgomp/ * testsuite/libgomp.c++/declare-mapper-1.C: New test. * testsuite/libgomp.c++/declare-mapper-2.C: New test. * testsuite/libgomp.c++/declare-mapper-3.C: New test. * testsuite/libgomp.c++/declare-mapper-4.C: New test. * testsuite/libgomp.c++/declare-mapper-5.C: New test. * testsuite/libgomp.c++/declare-mapper-6.C: New test. * testsuite/libgomp.c++/declare-mapper-7.C: New test. * testsuite/libgomp.c++/declare-mapper-8.C: New test. * testsuite/libgomp.c-c++-common/declare-mapper-9.c: New test (only enabled for C++ for now). * testsuite/libgomp.c-c++-common/declare-mapper-10.c: Likewise. * testsuite/libgomp.c-c++-common/declare-mapper-11.c: Likewise. * testsuite/libgomp.c-c++-common/declare-mapper-12.c: Likewise. * testsuite/libgomp.c-c++-common/declare-mapper-13.c: Likewise. * testsuite/libgomp.c-c++-common/declare-mapper-14.c: Likewise. Co-authored-by: Tobias Burnus <tburnus@baylibre.com>
2025-05-29OpenMP: Fix ICE and other issues in C/C++ metadirective error recovery.Sandra Loosemore2-7/+26
The new testcase included in this patch used to ICE in gcc after diagnosing the first error, and in g++ it only diagnosed the error in the first metadirective, ignoring the second one. The solution is to make error recovery in the C front end more like that in the C++ front end, and remove the code in both front ends that previously tried to skip all the way over the following statement (instead of just to the end of the metadirective pragma) after an error. gcc/c/ChangeLog * c-parser.cc (c_parser_skip_to_closing_brace): New, copied from the equivalent function in the C++ front end. (c_parser_skip_to_end_of_block_or_statement): Pass false to the error flag. (c_parser_omp_context_selector): Immediately return error_mark_node after giving an error that the integer trait property is invalid, similarly to C++ front end. (c_parser_omp_context_selector_specification): Likewise handle error return from c_parser_omp_context_selector similarly to C++. (c_parser_omp_metadirective): Do not call c_parser_skip_to_end_of_block_or_statement after an error. gcc/cp/ChangeLog * parser.cc (cp_parser_omp_metadirective): Do not call cp_parser_skip_to_end_of_block_or_statement after an error. gcc/testsuite/ChangeLog * c-c++-common/gomp/declare-variant-2.c: Adjust patterns now that C and C++ now behave similarly. * c-c++-common/gomp/metadirective-error-recovery.c: New.
2025-05-29OpenMP: Fix ICE in metadirective recovery after error [PR120180]Sandra Loosemore1-0/+22
It's not clear whether a metadirective in a loop nest is supposed to be valid, but GCC certainly shouldn't be ICE'ing after diagnosing it as an error. gcc/c/ChangeLog PR c/120180 * c-parser.cc (c_parser_omp_metadirective): Only consume the token if it is the expected close paren. gcc/cp/ChangeLog PR c/120180 * parser.cc (cp_parser_omp_metadirective): Only consume the token if it is the expected close paren. gcc/testsuite/ChangeLog PR c/120180 * c-c++-common/gomp/pr120180.c: New.
2025-05-26c-c++-common/gomp/{attrs-,}metadirective-3.c: Fix expected result [PR118694]Tobias Burnus2-6/+8
With compilation for nvptx enabled, two issues showed up: (a) "error: 'target' construct with nested 'teams' construct contains directives outside of the 'teams' construct" See PR comment 9 why this is difficult to fix. Solution: Add dg-bogus and accept/expect the error for 'target offload_nvptx'. (b) The assumptions about the dump for 'target offload_nvptx' were wrong as the metadirective was already expanded to a OMP_NEXT_VARIANT construct such that no 'omp metadirective' was left in either case. Solution: Check that no 'omp metadirective' is left; additionally, expect either OMP_NEXT_VARIANT (when offload_nvptx is available) or no 'teams' directive at all (if not). gcc/testsuite/ChangeLog: PR middle-end/118694 * c-c++-common/gomp/attrs-metadirective-3.c: Change to never expect 'omp metadirective' in the dump. If !offload_nvptx, check that no 'teams' shows up in the dump; for offload_nvptx, expect OMP_NEXT_VARIANT and an error about directive between 'target' and 'teams'. * c-c++-common/gomp/metadirective-3.c: Likewise.
2025-05-21[testsuite] [analyzer] [vxworks] define __STDC_WANT_LIB_EXT1__ to 1Alexandre Oliva1-1/+1
vxworks' headers use #if instead of #ifdef to test for __STDC_WANT_LIB_EXT1__, so the definition in the analyzer test strotok-cppreference.c catches a bug there, but not something it's meant to catch or that we could fix in GCC, so amend the definition to sidestep the libc bug. for gcc/testsuite/ChangeLog * c-c++-common/analyzer/strtok-cppreference.c (__STDC_WANT_LIB_EXT1__): Define to 1.
2025-05-21[testsuite] [vxworks] netinet includes atomic, reqs c++11Alexandre Oliva1-0/+2
On vxworks, the included netinet/in.h header indirectly includes <atomic>, that fails on C++ <11. Skip the test. for gcc/testsuite/ChangeLog * c-c++-common/analyzer/fd-glibc-byte-stream-socket.c: Skip on vxworks with C++ < 11.
2025-05-13verifier: Fix up PAREN_EXPR verification [PR118868]Andrew Pinski1-0/+9
The verification added in r12-1608-g2f1686ff70b25f, was incorrect for PAREN_EXPR, pointer types should be valid for PAREN_EXPR. Also for PAREN_EXPR, aggregate types don't make sense (currently they ICE much earlier in the gimplifier rather than error message) so we should disallow them here too. Bootstrapped and tested on x86_64-linux-gnu. PR middle-end/118868 gcc/ChangeLog: * tree-cfg.cc (verify_gimple_assign_unary): Allow pointers but disallow aggregate types for PAREN_EXPR. gcc/testsuite/ChangeLog: * c-c++-common/pr118868-1.c: New test. Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
2025-04-30analyzer: add more test coverage for sprintfDavid Malcolm1-0/+44
gcc/testsuite/ChangeLog: PR analyzer/107017 * c-c++-common/analyzer/sprintf-3.c: New test, covering use of sprintf with specific format strings. Doesn't yet find problems as the analyzer doesn't yet understand the format strings. Signed-off-by: David Malcolm <dmalcolm@redhat.com>
2025-04-30analyzer: avoid saying "'0' is NULL"David Malcolm3-4/+4
gcc/analyzer/ChangeLog: * sm-malloc.cc (malloc_diagnostic::describe_state_change): Tweak the "EXPR is NULL" message for the case where EXPR is a null pointer. gcc/testsuite/ChangeLog: * c-c++-common/analyzer/data-model-path-1.c: Check for "using NULL here" message. * c-c++-common/analyzer/null-deref-pr108251-smp_fetch_ssl_fc_has_early.c: Likewise. Check for "return of NULL" message. * c-c++-common/analyzer/null-deref-pr108400-SoftEtherVPN-WebUi.c: Likewise. * gcc.dg/analyzer/data-model-5.c: Likewise. * gcc.dg/analyzer/data-model-5b.c: Likewise. * gcc.dg/analyzer/data-model-5c.c: Likewise. * gcc.dg/analyzer/torture/pr93647.c: Likewise. Signed-off-by: David Malcolm <dmalcolm@redhat.com>
2025-04-28analyzer: handle NRVO and DECL_BY_REFERENCE [PR111536]David Malcolm1-0/+8
The analyzer was issuing false warnings about uninitialized variables in C++ in places where NRVO was marking DECL_RESULT with DECL_BY_REFERENCE. Fixed thusly. gcc/analyzer/ChangeLog: PR analyzer/111536 * engine.cc (maybe_update_for_edge): Update for new call_stmt param to region_model::push_frame. * program-state.cc (program_state::push_frame): Likewise. * region-model.cc (region_model::update_for_gcall): Likewise. (region_model::push_frame): Add "call_stmt" param. Handle DECL_RESULT with DECL_BY_REFERENCE set on it by stashing the region of the lhs of the call_stmt in the caller frame, and writing a reference to it within the "result" in the callee frame. (region_model::pop_frame): Don't write back to the LHS for DECL_BY_REFERENCE results. (selftest::test_stack_frames): Update for new call_stmt param to region_model::push_frame. (selftest::test_get_representative_path_var): Likewise. (selftest::test_state_merging): Likewise. (selftest::test_alloca): Likewise. * region-model.h (region_model::push_frame): Add "call_stmt" param. * region.cc: Include "tree-ssa.h". (region::can_have_initial_svalue_p): Use ssa_defined_default_def_p for ssa names, rather than special-casing it for just parameters. This should now also cover DECL_RESULT with DECL_BY_REFERENCE and hard registers. * sm-signal.cc (update_model_for_signal_handler): Update for new call_stmt param to region_model::push_frame. * state-purge.cc (state_purge_per_decl::process_worklists): Likewise. gcc/testsuite/ChangeLog: PR analyzer/111536 * c-c++-common/analyzer/hard-reg-1.c: New test. * g++.dg/analyzer/nrvo-1.C: New test. * g++.dg/analyzer/nrvo-2.C: New test. * g++.dg/analyzer/nrvo-pr111536-1.C: New test. * g++.dg/analyzer/nrvo-pr111536-1b.C: New test. * g++.dg/analyzer/nrvo-pr111536-2.C: New test. * g++.dg/analyzer/nrvo-pr111536-2b.C: New test. Signed-off-by: David Malcolm <dmalcolm@redhat.com>
2025-04-28analyzer: initial implementation of exception handling [PR97111]David Malcolm52-12/+84
This patch adds initial support for exception-handling to -fanalyzer, handling eh_dispatch for regions of type ERT_TRY and ERT_ALLOWED_EXCEPTIONS. I haven't managed yet seen eh_dispatch for regions of type ERT_CLEANUP and ERT_MUST_NOT_THROW in the analyzer; with this patch it will ICE if it sees those. Additionally, this patch only checks for exact matches of exception types, rather than supporting subclasses and references. I'm deferring fixing this for now whilst figuring out how best to interact with the C++ type system; I'm tracking it as PR analyzer/119697. The patch adds event classes for throwing and catching exceptions, and seems to generate readable warnings for the kinds of leak that might occur due to trying to manage resources manually and forgetting about exceptions; for example: exception-leak-1.C: In function ‘int test()’: exception-leak-1.C:7:9: warning: leak of ‘ptr’ [CWE-401] [-Wanalyzer-malloc-leak] 7 | throw 42; | ^~ ‘int test()’: events 1-3 5 | void *ptr = __builtin_malloc (1024); | ~~~~~~~~~~~~~~~~~^~~~~~ | | | (1) allocated here 6 | 7 | throw 42; | ~~ | | | (2) throwing exception of type ‘int’ here... | (3) ⚠️ ‘ptr’ leaks here; was allocated at (1) Although dynamic exception specifications are only available in C++14 and earlier, the need to support them meant it seemed relatively easy to add a warning to check them, hence the patch adds a new warning for code paths that throw an exception that doesn't match a dynamic exception specification: -Wanalyzer-throw-of-unexpected-type. gcc/analyzer/ChangeLog: PR analyzer/97111 * analyzer.cc (is_cxa_throw_p): New. (is_cxa_rethrow_p): New. * analyzer.opt (Wanalyzer-throw-of-unexpected-type): New. * analyzer.opt.urls: Regenerate. * call-info.cc (custom_edge_info::create_enode): New. * call-info.h (call_info::print): Drop "final". (call_info::add_events_to_path): Likewise. * checker-event.cc (event_kind_to_string): Add cases for event_kind::catch_, event_kind::throw_, and event_kind::unwind. (explicit_throw_event::print_desc): New. (throw_from_call_to_external_fn_event::print_desc): New. (unwind_event::print_desc): New. * checker-event.h (enum class event_kind): Add catch_, throw_, and unwind. (class catch_cfg_edge_event): New. (class throw_event): New. (class explicit_throw_event): New. (class throw_from_call_to_external_fn_event): New. (class unwind_event): New. * common.h (class eh_dispatch_cfg_superedge): New forward decl. (class eh_dispatch_try_cfg_superedge): New forward decl. (class eh_dispatch_allowed_cfg_superedge): New forward decl. (custom_edge_info::create_enode): New vfunc decl. (is_cxa_throw_p): New decl. (is_cxa_rethrow_p): New decl. * diagnostic-manager.cc (diagnostic_manager::add_events_for_superedge): Special-case edges for eh_dispach_try. (diagnostic_manager::prune_path): Call consolidate_unwind_events. (diagnostic_manager::prune_for_sm_diagnostic): Don't filter the new event_kinds. (diagnostic_manager::consolidate_unwind_events): New. * diagnostic-manager.h (diagnostic_manager::consolidate_unwind_events): New decl. * engine.cc (exploded_node::on_stmt_pre): Handle "__cxa_throw", "__cxa_rethrow", and resx statements. (class throw_custom_edge): New. (class unwind_custom_edge): New. (get_eh_outedge): New. (exploded_graph::unwind_from_exception): New. (exploded_node::on_throw): New. (exploded_node::on_resx): New. (exploded_graph::get_or_create_node): Add "add_to_worklist" param and use it. (exploded_graph::process_node): Use edge_info's create_enode vfunc to create enodes, rather than calling get_or_create_node directly. Ignore CFG edges in the sgraph flagged with EH whilst we're exploring the egraph. (exploded_graph_annotator::print_enode): Handle case exploded_node::status::special. * exploded-graph.h (exploded_node::status): Add value "special". (exploded_node::on_throw): New decl. (exploded_node::on_resx): New decl. (exploded_graph::get_or_create_node): Add optional "add_to_worklist" param. (exploded_graph::unwind_from_exception): New decl. * kf-lang-cp.cc (class kf_cxa_allocate_exception): New. (class kf_cxa_begin_catch): New. (class kf_cxa_end_catch): New. (class throw_of_unexpected_type): New. (class kf_cxa_call_unexpected): New. (register_known_functions_lang_cp): Register known functions "__cxa_allocate_exception", "__cxa_begin_catch", "__cxa_end_catch", and "__cxa_call_unexpected". * kf.cc (class kf_eh_pointer): New. (register_known_functions): Register it for BUILT_IN_EH_POINTER. * region-model.cc: Include "analyzer/function-set.h". (exception_node::operator==): New. (exception_node::dump_to_pp): New. (exception_node::dump): New. (exception_node::to_json): New. (exception_node::make_dump_widget): New. (exception_node::maybe_get_type): New. (exception_node::add_to_reachable_regions): New. (region_model::region_model): Initialize m_thrown_exceptions_stack and m_caught_exceptions_stack. (region_model::operator=): Likewise. (region_model::operator==): Compare them. (region_model::dump_to_pp): Dump exception stacks. (region_model::to_json): Add exception stacks. (region_model::make_dump_widget): Likewise. (class exception_thrown_from_unrecognized_call): New. (get_fns_assumed_not_to_throw): New. (can_throw_p): New. (region_model::check_for_throw_inside_call): New. (region_model::on_call_pre): Call check_for_throw_inside_call on unknown fns or those we don't have a body for. (region_model::maybe_update_for_edge): Handle eh_dispatch_stmt statements. Drop old code that called apply_constraints_for_exception on EDGE_EH edges. (class rejected_eh_dispatch): New. (exception_matches_type_p): New. (matches_any_exception_type_p): New. (region_model::apply_constraints_for_eh_dispatch): New. (region_model::apply_constraints_for_eh_dispatch_try): New. (region_model::apply_constraints_for_eh_dispatch_allowed): New. (region_model::apply_constraints_for_exception): Delete. (region_model::can_merge_with_p): Don't merge models with non-equal exception stacks. (region_model::get_referenced_base_regions): Add regions from exception stacks. * region-model.h (struct exception_node): New. (region_model::push_thrown_exception): New. (region_model::get_current_thrown_exception): New. (region_model::pop_thrown_exception): New. (region_model::push_caught_exception): New. (region_model::get_current_caught_exception): New. (region_model::pop_caught_exception): New. (region_model::apply_constraints_for_eh_dispatch_try): New decl. (region_model::apply_constraints_for_eh_dispatch_allowed) New decl. (region_model::apply_constraints_for_exception): Delete. (region_model::apply_constraints_for_eh_dispatch): New decl. (region_model::check_for_throw_inside_call): New decl. (region_model::m_thrown_exceptions_stack): New field. (region_model::m_caught_exceptions_stack): New field. * supergraph.cc: Include "except.h" and "analyzer/region-model.h". (supergraph::add_cfg_edge): Special-case eh_dispatch edges. (superedge::get_description): Use default_tree_printer. (get_catch): New. (eh_dispatch_cfg_superedge::make): New. (eh_dispatch_cfg_superedge::eh_dispatch_cfg_superedge): New. (eh_dispatch_cfg_superedge::get_eh_status): New. (eh_dispatch_try_cfg_superedge::dump_label_to_pp): New. (eh_dispatch_try_cfg_superedge::apply_constraints): New. (eh_dispatch_allowed_cfg_superedge::eh_dispatch_allowed_cfg_superedge): New. (eh_dispatch_allowed_cfg_superedge::dump_label_to_pp): New. (eh_dispatch_allowed_cfg_superedge::apply_constraints): New. * supergraph.h: Include "except.h". (superedge::dyn_cast_eh_dispatch_cfg_superedge): New vfunc. (superedge::dyn_cast_eh_dispatch_try_cfg_superedge): New vfunc. (superedge::dyn_cast_eh_dispatch_allowed_cfg_superedge): New vfunc. (class eh_dispatch_cfg_superedge): New. (is_a_helper <const eh_dispatch_cfg_superedge *>::test): New. (class eh_dispatch_try_cfg_superedge): New. (is_a_helper <const eh_dispatch_try_cfg_superedge *>::test): New. (class eh_dispatch_allowed_cfg_superedge): New. (is_a_helper <const eh_dispatch_allowed_cfg_superedge *>::test): New. * svalue.cc (svalue::maybe_get_type_from_typeinfo): New. * svalue.h (svalue::maybe_get_type_from_typeinfo): New decl. gcc/ChangeLog: PR analyzer/97111 * doc/invoke.texi: Add -Wanalyzer-throw-of-unexpected-type. * gimple.h (gimple_call_nothrow_p): Make arg const. gcc/testsuite/ChangeLog: PR analyzer/97111 * c-c++-common/analyzer/analyzer-verbosity-2a.c: Add -fno-exceptions. * c-c++-common/analyzer/analyzer-verbosity-3a.c: Likewise. * c-c++-common/analyzer/attr-const-2.c: Add __attribute__((nothrow)). * c-c++-common/analyzer/attr-malloc-4.c: Likewise. * c-c++-common/analyzer/attr-malloc-5.c: Likewise. * c-c++-common/analyzer/attr-malloc-6.c: Add -fno-exceptions. * c-c++-common/analyzer/attr-malloc-CVE-2019-19078-usb-leak.c: Likewise. * c-c++-common/analyzer/attr-malloc-exception.c: New test. * c-c++-common/analyzer/call-summaries-pr107158-2.c: Add -fno-exceptions. * c-c++-common/analyzer/call-summaries-pr107158.c: Likewise. * c-c++-common/analyzer/capacity-2.c: Likewise. * c-c++-common/analyzer/coreutils-sum-pr108666.c: Likewise. * c-c++-common/analyzer/data-model-22.c: Likewise. * c-c++-common/analyzer/data-model-5d.c: Likewise. * c-c++-common/analyzer/deref-before-check-pr108455-git-pack-revindex.c: Likewise. * c-c++-common/analyzer/deref-before-check-pr108475-haproxy-tcpcheck.c: Likewise. * c-c++-common/analyzer/edges-2.c: Likewise. * c-c++-common/analyzer/fd-2.c: Likewise. * c-c++-common/analyzer/fd-3.c: Likewise. * c-c++-common/analyzer/fd-meaning.c: Likewise. * c-c++-common/analyzer/file-1.c: Likewise. * c-c++-common/analyzer/file-3.c: Likewise. * c-c++-common/analyzer/file-meaning-1.c: Likewise. * c-c++-common/analyzer/infinite-recursion.c: Likewise. * c-c++-common/analyzer/leak-3.c: Likewise. * c-c++-common/analyzer/malloc-dedupe-1.c: Likewise. * c-c++-common/analyzer/malloc-in-loop.c: Likewise. * c-c++-common/analyzer/malloc-many-paths-3.c: Likewise. * c-c++-common/analyzer/malloc-paths-5.c: Likewise. * c-c++-common/analyzer/malloc-paths-7.c: Likewise. * c-c++-common/analyzer/malloc-paths-8.c: Likewise. * c-c++-common/analyzer/malloc-vs-local-1a.c: Likewise. * c-c++-common/analyzer/malloc-vs-local-2.c: Likewise. * c-c++-common/analyzer/malloc-vs-local-3.c: Likewise. * c-c++-common/analyzer/paths-7.c: Likewise. * c-c++-common/analyzer/pr110830.c: Likewise. * c-c++-common/analyzer/pr93032-mztools-simplified.c: Likewise. * c-c++-common/analyzer/pr93355-localealias-feasibility-3.c: Likewise. * c-c++-common/analyzer/pr93355-localealias-simplified.c: Likewise. * c-c++-common/analyzer/pr96650-1-trans.c: Likewise. * c-c++-common/analyzer/pr97072.c: Add __attribute__((nothrow)). * c-c++-common/analyzer/pr98575-1.c: Likewise. * c-c++-common/analyzer/pr99716-1.c: Add -fno-exceptions. * c-c++-common/analyzer/pr99716-2.c: Likewise. * c-c++-common/analyzer/pr99716-3.c: Likewise. * c-c++-common/analyzer/pragma-2.c: Likewise. * c-c++-common/analyzer/rhbz1878600.c: Likewise. * c-c++-common/analyzer/strndup-1.c: Likewise. * c-c++-common/analyzer/write-to-string-literal-4-disabled.c: Likewise. * c-c++-common/analyzer/write-to-string-literal-4.c: Likewise. * c-c++-common/analyzer/write-to-string-literal-5.c: Likewise. * c-c++-common/analyzer/zlib-5.c: Likewise. * g++.dg/analyzer/exception-could-throw-1.C: New test. * g++.dg/analyzer/exception-could-throw-2.C: New test. * g++.dg/analyzer/exception-dynamic-spec.C: New test. * g++.dg/analyzer/exception-leak-1.C: New test. * g++.dg/analyzer/exception-leak-2.C: New test. * g++.dg/analyzer/exception-leak-3.C: New test. * g++.dg/analyzer/exception-leak-4.C: New test. * g++.dg/analyzer/exception-leak-5.C: New test. * g++.dg/analyzer/exception-leak-6.C: New test. * g++.dg/analyzer/exception-nothrow.C: New test. * g++.dg/analyzer/exception-path-1.C: New test. * g++.dg/analyzer/exception-path-catch-all-1.C: New test. * g++.dg/analyzer/exception-path-catch-all-2.C: New test. * g++.dg/analyzer/exception-path-unwind-multiple-2.C: New test. * g++.dg/analyzer/exception-path-unwind-multiple.C: New test. * g++.dg/analyzer/exception-path-unwind-single.C: New test. * g++.dg/analyzer/exception-path-with-cleanups.C: New test. * g++.dg/analyzer/exception-rethrow-1.C: New test. * g++.dg/analyzer/exception-rethrow-2.C: New test. * g++.dg/analyzer/exception-stack-1.C: New test. * g++.dg/analyzer/exception-stack-2.C: New test. * g++.dg/analyzer/exception-subclass-1.C: New test. * g++.dg/analyzer/exception-subclass-2.C: New test. * g++.dg/analyzer/exception-value-1.C: New test. * g++.dg/analyzer/exception-value-2.C: New test. * g++.dg/analyzer/fno-exception.C: New test. * g++.dg/analyzer/pr94028.C: Drop xfail. * g++.dg/analyzer/std-unexpected.C: New test. * g++.dg/coroutines/pr105287.C: Drop dg-excess-errors. Signed-off-by: David Malcolm <dmalcolm@redhat.com>
2025-04-27c-family: Improve location for -Wunknown-pragmas in a _Pragma [PR118838]Lewis Hyatt1-0/+15
The warning for -Wunknown-pragmas is issued at the location provided by libcpp to the def_pragma() callback. This location is cpp_reader::directive_line, which is a location for the start of the line only; it is also not a valid location in case the unknown pragma was lexed from a _Pragma string. These factors make it impossible to suppress -Wunknown-pragmas via _Pragma("GCC diagnostic...") directives on the same source line, as in the PR and the test case. Address that by issuing the warning at a better location returned by cpp_get_diagnostic_override_loc(). libcpp already maintains this location to handle _Pragma-related diagnostics internally; it was needed also to make a publicly accessible version of it. gcc/c-family/ChangeLog: PR c/118838 * c-lex.cc (cb_def_pragma): Call cpp_get_diagnostic_override_loc() to get a valid location at which to issue -Wunknown-pragmas, in case it was triggered from a _Pragma. libcpp/ChangeLog: PR c/118838 * errors.cc (cpp_get_diagnostic_override_loc): New function. * include/cpplib.h (cpp_get_diagnostic_override_loc): Declare. gcc/testsuite/ChangeLog: PR c/118838 * c-c++-common/cpp/pragma-diagnostic-loc-2.c: New test. * g++.dg/gomp/macro-4.C: Adjust expected output. * gcc.dg/gomp/macro-4.c: Likewise. * gcc.dg/cpp/Wunknown-pragmas-1.c: Likewise.
2025-04-25testsuite: Add require target for SJLJ exception implementationDimitar Dimitrov2-2/+2
Testcases for musttail call optimization fail on pru-unknown-elf: FAIL: c-c++-common/musttail14.c -std=gnu++17 (test for excess errors) Excess errors: .../gcc/gcc/testsuite/c-c++-common/musttail14.c:37:14: error: cannot tail-call: caller uses sjlj exceptions Silence these errors by disabling the tests if target uses SJLJ for implementing exceptions. Use a new effective target check for this. Ensured that test results with and without this patch for x86_64-pc-linux-gnu are the same. gcc/ChangeLog: * doc/sourcebuild.texi: Document effective target using_sjlj_exceptions. gcc/testsuite/ChangeLog: * c-c++-common/musttail14.c: Disable test if effective target using_sjlj_exceptions. * c-c++-common/musttail22.c: Ditto. * g++.dg/musttail8.C: Ditto. * g++.dg/musttail9.C: Ditto. * g++.dg/opt/musttail3.C: Ditto. * g++.dg/opt/musttail4.C: Ditto. * g++.dg/opt/musttail5.C: Ditto. * g++.dg/opt/pr119613.C: Ditto. * lib/target-supports.exp (check_effective_target_using_sjlj_exceptions): New check. Signed-off-by: Dimitar Dimitrov <dimitar@dinux.eu>
2025-04-15tailc: Fix up musttail calls vs. -fsanitize=thread [PR119801]Jakub Jelinek1-0/+24
Calls with musttail attribute don't really work with -fsanitize=thread in GCC. The problem is that TSan instrumentation adds __tsan_func_entry (__builtin_return_address (0)); calls at the start of each instrumented function and __tsan_func_exit (); call at the end of those and the latter stands in a way of normal tail calls as well as musttail tail calls. Looking at what LLVM does, for normal calls -fsanitize=thread also prevents tail calls like in GCC (well, the __tsan_func_exit () call itself can be tail called in GCC (and from what I see not in clang)). But for [[clang::musttail]] calls it arranges to move the __tsan_func_exit () before the musttail call instead of after it. The following patch handles it similarly. If we for -fsanitize=thread instrumented function detect __builtin_tsan_func_exit () call, we process it normally (so that the call can be tail called in function returning void) but set a flag that the builtin has been seen (only for cfun->has_musttail in the diag_musttail phase). And then let tree_optimize_tail_calls_1 call find_tail_calls again in a new mode where the __tsan_func_exit () call is ignored and so we are able to find calls before it, but only accept that if the call before it is actually a musttail. For C++ it needs to verify that EH cleanup if any also has the __tsan_func_exit () call and if all goes well, the musttail call is registered for tailcalling with a flag that it has __tsan_func_exit () after it and when optimizing that we emit __tsan_func_exit (); call before the musttail tail call (or musttail tail recursion). 2025-04-15 Jakub Jelinek <jakub@redhat.com> PR sanitizer/119801 * sanitizer.def (BUILT_IN_TSAN_FUNC_EXIT): Use BT_FN_VOID rather than BT_FN_VOID_PTR. * tree-tailcall.cc: Include attribs.h and asan.h. (struct tailcall): Add has_tsan_func_exit member. (empty_eh_cleanup): Add eh_has_tsan_func_exit argument, set what it points to to 1 if there is exactly one __tsan_func_exit call and ignore that call otherwise. Adjust recursive call. (find_tail_calls): Add RETRY_TSAN_FUNC_EXIT argument, pass it to recursive calls. When seeing __tsan_func_exit call with RETRY_TSAN_FUNC_EXIT 0, set it to -1. If RETRY_TSAN_FUNC_EXIT is 1, initially ignore __tsan_func_exit calls. Adjust empty_eh_cleanup caller. When looking through stmts after the call, ignore exactly one __tsan_func_exit call but remember it in t->has_tsan_func_exit. Diagnose if EH cleanups didn't have __tsan_func_exit and normal path did or vice versa. (optimize_tail_call): Emit __tsan_func_exit before the tail call or tail recursion. (tree_optimize_tail_calls_1): Adjust find_tail_calls callers. If find_tail_calls changes retry_tsan_func_exit to -1, set it to 1 and call it again with otherwise the same arguments. * c-c++-common/tsan/pr119801.c: New test.
2025-04-11realloc-1.c: accept long long in warning for llp64Jonathan Yong1-1/+1
llp64 targets like mingw-w64 will print: warning: ignoring return value of ‘void* __builtin_realloc(void*, long long unsigned int)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] Change the regex pattern to accept it. Signed-off-by: Jonathan Yong <10walls@gmail.com> gcc/testsuite/ChangeLog: * c-c++-common/analyzer/realloc-1.c: Make diagnostic accept long long for __builtin_realloc warning.
2025-04-08OpenMP: Fix append_args handling in modify_call_for_omp_dispatchTobias Burnus1-2/+2
At tree level, the addr ref is also required for array dummy arguments, contrary to C; the GOMP_interop calls in modify_call_for_omp_dispatch were updated accordingly (using build_fold_addr_expr). As the GOMP_interop calls had no location data associated with them, the init call happened as soon as executing the previous line of code, which was confusing; solution: use the location data of the function call itself. PR middle-end/119662 gcc/ChangeLog: * gimplify.cc (modify_call_for_omp_dispatch): Fix GOMP_interop arg passing; add location info to function calls. libgomp/ChangeLog: * testsuite/libgomp.c/append-args-fr-1.c: New test. * testsuite/libgomp.c/append-args-fr.h: New test. gcc/testsuite/ChangeLog: * c-c++-common/gomp/append-args-interop.c: Update for fixed GOMP_interop call. * g++.dg/gomp/append-args-8.C: Likewise. * gfortran.dg/gomp/append-args-interop.f90: Likewise.
2025-04-04profile: Another musttail fix [PR119618]Jakub Jelinek1-0/+21
As the following testcase shows, sometimes we can have debug stmts after a musttail call and profile.cc in that case would incorrectly allow the edge from that, causing musttail error and -fcompare-debug failure (because if there are no debug stmts after it, then musttail is found there and the edge is ignored). The following patch uses gsi_last_nondebug_bb instead of gsi_last_bb to find the musttail call. And so that we don't uselessly skip over debug stmts at the end of many bbs, the patch limits it to cfun->has_musttail functions. 2025-04-04 Jakub Jelinek <jakub@redhat.com> PR gcov-profile/119618 * profile.cc (branch_prob): Only check for musttail calls if cfun->has_musttail. Use gsi_last_nondebug_bb instead of gsi_last_bb. * c-c++-common/pr119618.c: New test.
2025-04-04tailc: Don't reject all tail calls if param has addr taken [PR119616]Jakub Jelinek1-0/+23
Before my PR119376 r15-9145 changes, suitable_for_tail_call_opt_p would return the same value in the same caller, regardless of the calls in it. If it fails, the caller clears opt_tailcalls which is a reference and therefore shared by all calls in the caller and we only do tail recursion, all non-recursive or tail recursion non-optimizable calls are not tail call optimized. For musttail calls we want to allow address taken parameters, but the r15-9145 change effectively resulted in the behavior where if there are just musttail calls considered, they will be tail call optimized, and if there are also other tail call candidates (without musttail), we clear opt_tailcall and then error out on all the musttail calls. The following patch fixes that by moving the address taken parameter discovery from suitable_for_tail_call_opt_p to its single caller. If there are addressable parameters, if !cfun->has_musttail it will work as before, disable all tail calls in the caller but possibly allow tail recursions. If cfun->has_musttail, it will set a new bool automatic flag and reject non-tail recursions. This way musttail calls can be still accepted and normal tail call candidates rejected (and tail recursions accepted). 2025-04-04 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/119616 * tree-tailcall.cc (suitable_for_tail_call_opt_p): Move checking for addressable parameters from here ... (find_tail_calls): ... here. If cfun->has_musttail, don't clear opt_tailcalls for it, instead set a local flag and punt if we can't tail recurse optimize it. * c-c++-common/pr119616.c: New test.
2025-04-04tailc: Use the IPA-VRP tail call hack even for pointers [PR119614]Jakub Jelinek3-0/+84
As the first two testcases show, even with pointers IPA-VRP can optimize return values from functions if they have singleton ranges into just the exact value, so we need to virtually undo that for tail calls similarly to integers and floats. The third test just adds check that it works even with floats (which it does). 2025-04-04 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/119614 * tree-tailcall.cc (find_tail_calls): Handle also pointer types in the IPA-VRP workaround. * c-c++-common/pr119614-1.c: New test. * c-c++-common/pr119614-2.c: New test. * c-c++-common/pr119614-3.c: New test.
2025-04-02OpenMP: Require target and/or targetsync init modifier [PR118965]Sandra Loosemore10-111/+179
As noted in PR 118965, the initial interop implementation overlooked the requirement in the OpenMP spec that at least one of the "target" and "targetsync" modifiers is required in both the interop construct init clause and the declare variant append_args clause. Adding the check was fairly straightforward, but it broke about a gazillion existing test cases. In particular, things like "init (x, y)" which were previously accepted (and tested for being accepted) aren't supposed to be allowed by the spec, much less things like "init (target)" where target was previously interpreted as a variable name instead of a modifier. Since one of the effects of the change is that at least one modifier is always required, I found that deleting all the code that was trying to detect and handle the no-modifier case allowed for better diagnostics. gcc/c/ChangeLog PR middle-end/118965 * c-parser.cc (c_parser_omp_clause_init_modifiers): Adjust error message. (c_parser_omp_clause_init): Remove code for recognizing clauses without modifiers. Diagnose missing target/targetsync modifier. (c_finish_omp_declare_variant): Diagnose missing target/targetsync modifier. gcc/cp/ChangeLog PR middle-end/118965 * parser.cc (c_parser_omp_clause_init_modifiers): Adjust error message. (cp_parser_omp_clause_init): Remove code for recognizing clauses without modifiers. Diagnose missing target/targetsync modifier. (cp_finish_omp_declare_variant): Diagnose missing target/targetsync modifier. gcc/fortran/ChangeLog PR middle-end/118965 * openmp.cc (gfc_parser_omp_clause_init_modifiers): Fix some inconsistent code indentation. Remove code for recognizing clauses without modifiers. Diagnose prefer_type without a following paren. Adjust error message for an unrecognized modifier. Diagnose missing target/targetsync modifier. (gfc_match_omp_init): Fix more inconsistent code indentation. gcc/testsuite/ChangeLog PR middle-end/118965 * c-c++-common/gomp/append-args-1.c: Add target/targetsync modifiers so tests do what they were previously supposed to do. Adjust expected output. * c-c++-common/gomp/append-args-7.c: Likewise. * c-c++-common/gomp/append-args-8.c: Likewise. * c-c++-common/gomp/append-args-9.c: Likewise. * c-c++-common/gomp/interop-1.c: Likewise. * c-c++-common/gomp/interop-2.c: Likewise. * c-c++-common/gomp/interop-3.c: Likewise. * c-c++-common/gomp/interop-4.c: Likewise. * c-c++-common/gomp/pr118965-1.c: New. * c-c++-common/gomp/pr118965-2.c: New. * g++.dg/gomp/append-args-1.C: Add target/targetsync modifiers and adjust expected output. * g++.dg/gomp/append-args-2.C: Likewise. * g++.dg/gomp/append-args-6.C: Likewise. * g++.dg/gomp/append-args-7.C: Likewise. * g++.dg/gomp/append-args-8.C: Likewise. * g++.dg/gomp/interop-5.C: Likewise. * gfortran.dg/gomp/append_args-1.f90: Add target/targetsync modifiers and adjust expected output. * gfortran.dg/gomp/append_args-2.f90: Likewise. * gfortran.dg/gomp/append_args-3.f90: Likewise. * gfortran.dg/gomp/append_args-4.f90: Likewise. * gfortran.dg/gomp/interop-1.f90: Likewise. * gfortran.dg/gomp/interop-2.f90: Likewise. * gfortran.dg/gomp/interop-3.f90: Likewise. * gfortran.dg/gomp/interop-4.f90: Likewise. * gfortran.dg/gomp/pr118965-1.f90: New. * gfortran.dg/gomp/pr118965-2.f90: New.
2025-04-02tailc: Don't fail musttail calls if they use or could use local arguments, ↵Jakub Jelinek12-11/+448
instead warn [PR119376] As discussed here and in bugzilla, [[clang::musttail]] attribute in clang not just strongly asks for tail call or error, but changes behavior. To quote: https://clang.llvm.org/docs/AttributeReference.html#musttail "The lifetimes of all local variables and function parameters end immediately before the call to the function. This means that it is undefined behaviour to pass a pointer or reference to a local variable to the called function, which is not the case without the attribute. Clang will emit a warning in common cases where this happens." The GCC behavior was just to error if we can't prove the musttail callee could not have dereferenced escaped pointers to local vars or parameters of the caller. That is still the case for variables with non-trivial destruction (even in clang), like vars with C++ non-trivial destructors or variables with cleanup attribute. The following patch changes the behavior to match that of clang, for all of [[clang::musttail]], [[gnu::musttail]] and __attribute__((musttail)). clang 20 actually added warning for some cases of it in https://github.com/llvm/llvm-project/pull/109255 but it is under -Wreturn-stack-address warning. Now, gcc doesn't have that warning, but -Wreturn-local-addr instead, and IMHO it is better to have this under new warnings, because this isn't about returning local address, but about passing it to a musttail call, or maybe escaping to a musttail call. And perhaps users will appreciate they can control it separately as well. The patch introduces 2 new warnings. -Wmusttail-local-addr which is turn on by default and warns for the always dumb cases of passing an address of a local variable or parameter to musttail call's argument. And then -Wmaybe-musttail-local-addr which is only diagnosed if -Wmusttail-local-addr was not diagnosed and diagnoses at most one (so that we don't emit 100s of warnings for one call if 100s of vars can escape) case where an address of a local var could have escaped to the musttail call. This is less severe, the code doesn't have to be obviously wrong, so the warning is only enabled in -Wextra. And I've adjusted also the documentation for this change and addition of new warnings. 2025-04-02 Jakub Jelinek <jakub@redhat.com> PR ipa/119376 * common.opt (Wmusttail-local-addr, Wmaybe-musttail-local-addr): New. * tree-tailcall.cc (suitable_for_tail_call_opt_p): Don't fail for TREE_ADDRESSABLE PARM_DECLs for musttail calls if diag_musttail. Emit -Wmusttail-local-addr warnings. (maybe_error_musttail): Use gimple_location instead of directly accessing location member. (find_tail_calls): For musttail calls if diag_musttail, don't fail if address of local could escape to the call, instead emit -Wmaybe-musttail-local-addr warnings. Emit -Wmaybe-musttail-local-addr warnings also for address taken parameters. * common.opt.urls: Regenerate. * doc/extend.texi (musttail statement attribute): Clarify local variables without non-trivial destruction are considered out of scope before the tail call instruction. * doc/invoke.texi (-Wno-musttail-local-addr, -Wmaybe-musttail-local-addr): Document. * c-c++-common/musttail8.c: Expect a warning rather than error in one case. (f4): Add int * argument. * c-c++-common/musttail15.c: Don't disallow for C++98. * c-c++-common/musttail16.c: Likewise. * c-c++-common/musttail17.c: Likewise. * c-c++-common/musttail18.c: Likewise. * c-c++-common/musttail19.c: Likewise. Expect a warning rather than error in one case. (f4): Add int * argument. * c-c++-common/musttail20.c: Don't disallow for C++98. * c-c++-common/musttail21.c: Likewise. * c-c++-common/musttail28.c: New test. * c-c++-common/musttail29.c: New test. * c-c++-common/musttail30.c: New test. * c-c++-common/musttail31.c: New test. * g++.dg/ext/musttail1.C: New test. * g++.dg/ext/musttail2.C: New test. * g++.dg/ext/musttail3.C: New test.
2025-04-01profile: Another profiling musttail call fix [PR119535]Jakub Jelinek1-0/+31
As the following testcase shows, EDGE_FAKE edges from musttail calls to EXIT aren't the only edges we should ignore, we need to ignore also edges created by the splitting of blocks for the EDGE_FAKE creation that point from the musttail calls to the fallthrough block, which typically does the return or with PHIs for the return value. 2025-04-01 Jakub Jelinek <jakub@redhat.com> PR gcov-profile/119535 * profile.cc (branch_prob): Ignore any edges from bbs ending with musttail call, rather than only EDGE_FAKE edges from those to EXIT. * c-c++-common/pr119535.c: New test.
2025-04-01gimple-low: Diagnose assume attr expressions defining labels which are used ↵Jakub Jelinek2-0/+46
as unary && operands outside of those [PR119537] The following testcases ICE on invalid code which defines labels inside of statement expressions and then uses &&label from code outside of the statement expressions. The C++ FE diagnoses that with a warning (not specifically for assume attribute, genericallly about taking address of a label outside of a statement expression so computed goto could violate the requirement that statement expression is not entered from outside of it through a jump into it), the C FE doesn't diagnose anything. Normal direct gotos to such labels are diagnosed by both C and C++. In the assume attribute case it is actually worse than for addresses of labels in normal statement expressions, in that case the labels are still in the current function, so invalid program can still jump to those (and in case of OpenMP/OpenACC where it is also invalid and stuff is moved to a separate function, such movement is done post cfg discovery of FORCED_LABELs and worst case one can run into cases which fail to assemble, but I haven't succeeded in creating ICE for that). For assume at -O0 we'd just throw away the assume expression if it is not a simple condition and so the label is then not defined anywhere and we ICE during cfg pass. The gimplify.cc hunks fix that, as we don't have FORCED_LABELs discovery done yet, it preserves all assume expressions which contain used user labels. With that we ICE during IRA, which is upset about an indirect jump to a label which doesn't exist. So, the gimple-low.cc hunks add diagnostics of the problem, it gathers uids of all the user used labels inside of the assume expressions (usually none) and if it finds any, walks the IL to find uses of those from outside of those expressions now outlined into separate magic functions. 2025-04-01 Jakub Jelinek <jakub@redhat.com> PR middle-end/119537 * gimplify.cc (find_used_user_labels): New function. (gimplify_call_expr): Don't remove complex assume expression at -O0 if it defines any user labels. * gimple-low.cc: Include diagnostic-core.h. (assume_labels): New variable. (diagnose_assume_labels): New function. (lower_function_body): Call it via walk_gimple_seq if assume_labels is non-NULL, then BITMAP_FREE assume_labels. (find_assumption_locals_r): Record in assume_labels uids of user labels defined in assume attribute expressions. * c-c++-common/pr119537-1.c: New test. * c-c++-common/pr119537-2.c: New test.
2025-03-28tailc: Handle musttail noreturn calls [PR119483]Jakub Jelinek2-0/+41
The following (first) testcase is accepted by clang (if clang::musttail) and rejected by gcc, because we discover the call is noreturn and then bail out because we don't want noreturn tailcalls. The general reason not to support noreturn tail calls is for cases like abort where we want nicer backtrace, but if user asks explicitly to musttail a call which either is explicitly noreturn or is implicitly determined to be noreturn, I don't see a reason why we couldn't do that. Both for tail calls and tail recursions. An alternative would be to keep rejecting musttail to explicit noreturn, but not actually implicitly mark anything as noreturn if it has any musttail calls. But it is unclear how we could do that, such marking is I think done typically before IPA and e.g. for LTO we won't know whether some other TU could have musttail calls to it. And keeping around both explicit and implicit noreturn bits would be ugly. Well, I guess we could differentiate between presence of noreturn/_Noreturn attributes and just ECF_NORETURN without those, but then tailc would still need to support it, just error out if it was explicit. 2025-03-28 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/119483 * tree-tailcall.cc (find_tail_calls): Handle noreturn musttail calls. (eliminate_tail_call): Likewise. (tree_optimize_tail_calls_1): If cfun->has_musttail and diag_musttail, handle also basic blocks with no successors with noreturn musttail calls. * calls.cc (can_implement_as_sibling_call_p): Allow ECF_NORETURN calls if they are musttail calls. * c-c++-common/pr119483-1.c: New test. * c-c++-common/pr119483-2.c: New test.
2025-03-28ipa-sra: Don't change return type to void if there are musttail calls [PR119484]Jakub Jelinek1-0/+21
The following testcase is rejected, because IPA-SRA decides to turn bar.constprop call into bar.constprop.isra which returns void. While there is no explicit lhs on the call, as it is a musttail call the tailc pass checks if IPA-VRP returns singleton from that function and the function returns the same value and in that case it still turns it into a tail call. This can't work with IPA-SRA changing it into void returning function though. The following patch fixes this by forcing returning the original type if there are musttail calls. 2025-03-28 Jakub Jelinek <jakub@redhat.com> PR ipa/119484 * ipa-sra.cc (isra_analyze_call): Don't set m_return_ignored if gimple_call_must_tail_p even if it doesn't have lhs. * c-c++-common/pr119484.c: New test.
2025-03-27testsuite: harmless dg-* whitespace fixesSam James1-1/+1
These just fix inconsistent/unusual style to avoid noise when grepping and also people picking up bad habits when they see it (as similar mistakes can be harmful). gcc/testsuite/ChangeLog: * c-c++-common/goacc/pr69916.c: Fix unusual whitespace in dg-*. * g++.old-deja/g++.abi/vtable2.C: Ditto. * g++.old-deja/g++.bugs/900330_02.C: Ditto. * g++.old-deja/g++.bugs/900406_02.C: Ditto. * g++.old-deja/g++.bugs/900519_13.C: Ditto. * g++.old-deja/g++.mike/p9068.C: Ditto. * gcc.dg/20040203-1.c: Ditto. * gcc.dg/980502-1.c: Ditto. * gcc.dg/ipa/ipa-sra-14.c: Ditto. * gcc.dg/pr35468.c: Ditto. * gcc.dg/pr82597.c: Ditto. * gcc.dg/tree-ssa/phi-opt-7.c: Ditto. * gfortran.dg/assumed_charlen_in_main.f90: Ditto. * gfortran.dg/cray_pointers_2.f90: Ditto.