gcc/libgomp/testsuite
Jakub Jelinek 650e915665 c++: Implement C++23 P2718R0 - Wording for P2644R1 Fix for Range-based for Loop [PR107637]
The following patch implements the C++23 P2718R0 paper
- Wording for P2644R1 Fix for Range-based for Loop.
The patch introduces a new option, -f{,no-}range-for-ext-temps so that
user can control the behavior even in older C++ versions.
The option is on by default in C++23 and later (-fno-range-for-ext-temps
is an error in that case) and in the -std=gnu++11 ... -std=gnu++20 modes
(one can use -fno-range-for-ext-temps to request previous behavior in that
case), and is not enabled by default in -std=c++11 ... -std=c++20 modes
but one can explicitly enable it with -frange-for-ext-temps.
As all the temporaries from __for_range initialization should have life
extended until the end of __for_range scope, this patch disables (for
-frange-for-ext-temps and if !processing_template_decl) CLEANUP_POINT_EXPR wrapping
of the __for_range declaration, also disables -Wdangling-reference warning
as well as the rest of extend_ref_init_temps (we know the __for_range temporary
is not TREE_STATIC and as all the temporaries from the initializer will be life
extended, we shouldn't try to handle temporaries referenced by references any
differently) and adds an extra push_stmt_list/pop_stmt_list before
cp_finish_decl of __for_range and after end of the for body and wraps all
that into CLEANUP_POINT_EXPR.
I had to repeat that also for OpenMP range loops because those are handled
differently.

2024-09-24  Jakub Jelinek  <jakub@redhat.com>

	PR c++/107637
gcc/
	* omp-general.cc (find_combined_omp_for, find_nested_loop_xform):
	Handle CLEANUP_POINT_EXPR like TRY_FINALLY_EXPR.
	* doc/invoke.texi (frange-for-ext-temps): Document.  Add
	-fconcepts to the C++ option list.
gcc/c-family/
	* c.opt (frange-for-ext-temps): New option.
	* c-opts.cc (c_common_post_options): Set flag_range_for_ext_temps
	for C++23 or later or for C++11 or later in !flag_iso mode if
	the option wasn't set by user.
	* c-cppbuiltin.cc (c_cpp_builtins): Change __cpp_range_based_for
	value for flag_range_for_ext_temps from 201603L to 202212L in C++17
	or later.
	* c-omp.cc (c_find_nested_loop_xform_r): Handle CLEANUP_POINT_EXPR
	like TRY_FINALLY_EXPR.
gcc/cp/
	* cp-tree.h: Implement C++23 P2718R0 - Wording for P2644R1 Fix for
	Range-based for Loop.
	(cp_convert_omp_range_for): Add bool tmpl_p argument.
	(find_range_for_decls): Declare.
	* parser.cc (cp_convert_range_for): For flag_range_for_ext_temps call
	push_stmt_list () before cp_finish_decl for range_temp and save it
	temporarily to FOR_INIT_STMT.
	(cp_convert_omp_range_for): Add tmpl_p argument.  If set, remember
	DECL_NAME of range_temp and for cp_finish_decl call restore it before
	clearing it again, if unset, don't adjust DECL_NAME of range_temp at
	all.
	(cp_parser_omp_loop_nest): For flag_range_for_ext_temps range for add
	CLEANUP_POINT_EXPR around sl.  Call find_range_for_decls and adjust
	DECL_NAMEs for range fors if not processing_template_decl.  Adjust
	cp_convert_omp_range_for caller.  Remove superfluous backslash at the
	end of line.
	* decl.cc (initialize_local_var): For flag_range_for_ext_temps
	temporarily clear stmts_are_full_exprs_p rather than set for
	for_range__identifier decls.
	* call.cc (extend_ref_init_temps): For flag_range_for_ext_temps return
	init early for for_range__identifier decls.
	* semantics.cc (find_range_for_decls): New function.
	(finish_for_stmt): Use it.  For flag_range_for_ext_temps if
	cp_convert_range_for set FOR_INIT_STMT, pop_stmt_list it and wrap
	into CLEANUP_POINT_EXPR.
	* pt.cc (tsubst_omp_for_iterator): Adjust tsubst_omp_for_iterator
	caller.
	(tsubst_stmt) <case OMP_FOR>: For flag_range_for_ext_temps if there
	are any range fors in the loop nest, add push_stmt_list starting
	before the initializations, pop_stmt_list it after the body and wrap
	into CLEANUP_POINT_EXPR.  Change DECL_NAME of range for temps from
	NULL to for_range_identifier.
gcc/testsuite/
	* g++.dg/cpp23/range-for1.C: New test.
	* g++.dg/cpp23/range-for2.C: New test.
	* g++.dg/cpp23/range-for3.C: New test.
	* g++.dg/cpp23/range-for4.C: New test.
	* g++.dg/cpp23/range-for5.C: New test.
	* g++.dg/cpp23/range-for6.C: New test.
	* g++.dg/cpp23/range-for7.C: New test.
	* g++.dg/cpp23/range-for8.C: New test.
	* g++.dg/cpp23/feat-cxx2b.C (__cpp_range_based_for): Check for
	202212L rather than 201603L.
	* g++.dg/cpp26/feat-cxx26.C (__cpp_range_based_for): Likewise.
	* g++.dg/warn/Wdangling-reference4.C: Don't expect warning for C++23
	or newer.  Use dg-additional-options rather than dg-options.
libgomp/
	* testsuite/libgomp.c++/range-for-1.C: New test.
	* testsuite/libgomp.c++/range-for-2.C: New test.
	* testsuite/libgomp.c++/range-for-3.C: New test.
	* testsuite/libgomp.c++/range-for-4.C: New test.
	* testsuite/libgomp.c++/range-for-5.C: New test.
2024-09-24 20:24:57 +02:00
..
config
lib
libgomp.c OpenMP: Add get_device_from_uid/omp_get_uid_from_device routines 2024-09-20 09:25:33 +02:00
libgomp.c-c++-common libgomp: with USM, init 'link' variables with host address 2024-09-24 17:41:39 +02:00
libgomp.c++ c++: Implement C++23 P2718R0 - Wording for P2644R1 Fix for Range-based for Loop [PR107637] 2024-09-24 20:24:57 +02:00
libgomp.fortran OpenMP: Add support for 'self_maps' to the 'require' directive 2024-09-24 10:53:59 +02:00
libgomp.graphite
libgomp.oacc-c
libgomp.oacc-c-c++-common
libgomp.oacc-c++
libgomp.oacc-fortran
flock
libgomp-site-extra.exp.in
libgomp-test-support.exp.in
Makefile.am
Makefile.in