gcc/gcc/doc
Jakub Jelinek dd346b6138 c: Add support for unsequenced and reproducible attributes
C23 added in N2956 ( https://open-std.org/JTC1/SC22/WG14/www/docs/n2956.htm )
two new attributes, which are described as similar to GCC const and pure
attributes, but they aren't really same and it seems that even the paper
is missing some of the differences.
The paper says unsequenced is the same as const on functions without pointer
arguments and reproducible is the same as pure on such functions (except
that they are function type attributes rather than function
declaration ones), but it seems the paper doesn't consider the finiteness GCC
relies on (aka non-DECL_LOOPING_CONST_OR_PURE_P) - the paper only talks
about using the attributes for CSE etc., not for DCE.

The following patch introduces (for now limited) support for those
attributes, both as standard C23 attributes and as GNU extensions (the
difference is that the patch is then less strict on where it allows them,
like other function type attributes they can be specified on function
declarations as well and apply to the type, while C23 standard ones must
go on the function declarators (i.e. after closing paren after function
parameters) or in type specifiers of function type.

If function doesn't have any pointer/reference arguments, the patch
adds additional internal attribute with " noptr" suffix which then is used
by flags_from_decl_or_type to handle those easy cases as
ECF_CONST|ECF_LOOPING_CONST_OR_PURE or
ECF_PURE|ECF_LOOPING_CONST_OR_PURE
The harder cases aren't handled right now, I'd hope they can be handled
incrementally.

I wonder whether we shouldn't emit a warning for the
gcc.dg/c23-attr-{reproducible,unsequenced}-5.c cases, while the standard
clearly specifies that composite types should union the attributes and it
is what GCC implements for decades, for ?: that feels dangerous for the
new attributes, it would be much better to be conservative on say
(cond ? unsequenced_function : normal_function) (args)

There is no diagnostics on incorrect [[unsequenced]] or [[reproducible]]
function definitions, while I think diagnosing non-const static/TLS
declarations in the former could be easy, the rest feels hard.  E.g. the
const/pure discovery can just punt on everything it doesn't understand,
but complete diagnostics would need to understand it.

2024-08-31  Jakub Jelinek  <jakub@redhat.com>

	PR c/116130
gcc/
	* doc/extend.texi (unsequenced, reproducible): Document new function
	type attributes.
	* calls.cc (flags_from_decl_or_type): Handle "unsequenced noptr" and
	"reproducible noptr" attributes.
gcc/c-family/
	* c-attribs.cc (c_common_gnu_attributes): Add entries for
	"unsequenced", "reproducible", "unsequenced noptr" and
	"reproducible noptr" attributes.
	(handle_unsequenced_attribute): New function.
	(handle_reproducible_attribute): Likewise.
	* c-common.h (handle_unsequenced_attribute): Declare.
	(handle_reproducible_attribute): Likewise.
	* c-lex.cc (c_common_has_attribute): Return 202311 for standard
	unsequenced and reproducible attributes.
gcc/c/
	* c-decl.cc (handle_std_unsequenced_attribute): New function.
	(handle_std_reproducible_attribute): Likewise.
	(std_attributes): Add entries for "unsequenced" and "reproducible"
	attributes.
	(c_warn_type_attributes): Add TYPE argument.  Allow unsequenced
	or reproducible attributes if it is FUNCTION_TYPE.
	(groktypename): Adjust c_warn_type_attributes caller.
	(grokdeclarator): Likewise.
	(finish_declspecs): Likewise.
	* c-parser.cc (c_parser_declaration_or_fndef): Likewise.
	* c-tree.h (c_warn_type_attributes): Add TYPE argument.
gcc/testsuite/
	* c-c++-common/attr-reproducible-1.c: New test.
	* c-c++-common/attr-reproducible-2.c: New test.
	* c-c++-common/attr-unsequenced-1.c: New test.
	* c-c++-common/attr-unsequenced-2.c: New test.
	* gcc.dg/c23-attr-reproducible-1.c: New test.
	* gcc.dg/c23-attr-reproducible-2.c: New test.
	* gcc.dg/c23-attr-reproducible-3.c: New test.
	* gcc.dg/c23-attr-reproducible-4.c: New test.
	* gcc.dg/c23-attr-reproducible-5.c: New test.
	* gcc.dg/c23-attr-reproducible-5-aux.c: New file.
	* gcc.dg/c23-attr-unsequenced-1.c: New test.
	* gcc.dg/c23-attr-unsequenced-2.c: New test.
	* gcc.dg/c23-attr-unsequenced-3.c: New test.
	* gcc.dg/c23-attr-unsequenced-4.c: New test.
	* gcc.dg/c23-attr-unsequenced-5.c: New test.
	* gcc.dg/c23-attr-unsequenced-5-aux.c: New file.
	* gcc.dg/c23-has-c-attribute-2.c: Add tests for unsequenced
	and reproducible attributes.
2024-08-31 15:58:23 +02:00
..
include
analyzer.texi
avr-mmcu.texi
bugreport.texi doc: Remove dubious example around bug reporting 2024-07-07 22:01:40 +02:00
cfg.texi Fix reference to the dom walker function in the documentation 2024-08-10 08:34:54 -07:00
collect2.texi
compat.texi
configfiles.texi
configterms.texi
contrib.texi doc: Add Dhruv Matani to Contributors 2024-08-28 21:34:21 +01:00
contribute.texi
cpp.texi c: Add -std=c2y, -std=gnu2y, -Wc23-c2y-compat, C2Y _Generic with type operand 2024-06-11 23:00:04 +00:00
cppdiropts.texi
cppenv.texi
cppinternals.texi
cppopts.texi
cppwarnopts.texi
extend.texi c: Add support for unsequenced and reproducible attributes 2024-08-31 15:58:23 +02:00
fragments.texi
frontends.texi
gcc.texi
gccint.texi
gcov-dump.texi
gcov-tool.texi
gcov.texi Add function filtering to gcov 2024-07-11 09:13:16 +02:00
generic.texi
gimple.texi
gm2.texi doc: Specifically link to GPL v3.0 for GM2 2024-08-23 10:04:43 +02:00
gnu.texi
gty.texi
headerdirs.texi
hostconfig.texi
implement-c.texi
implement-cxx.texi
install.texi doc: Normalize reference to binutils version for C6X 2024-08-20 16:55:25 +02:00
install.texi2html
interface.texi
invoke.texi PR target/116365: Add user-friendly arguments to --param aarch64-autovec-preference=N 2024-08-22 12:47:19 +02:00
languages.texi
libgcc.texi
loop.texi
lto-dump.texi
lto.texi
makefile.texi
match-and-simplify.texi
md.texi rtl: Enable the use of rtx values with int and mode attributes 2024-08-19 09:46:11 +01:00
objc.texi
optinfo.texi
options.texi
passes.texi Add a late-combine pass [PR106594] 2024-06-24 08:43:19 +01:00
plugins.texi
poly-int.texi
portability.texi
rtl.texi rtl-ssa: Define INCLUDE_ARRAY 2024-07-25 13:25:32 +01:00
service.texi
sourcebuild.texi testsuite: Add scan-ltrans-rtl* for use in dg-final [PR116140] 2024-08-28 12:18:47 +01:00
standards.texi c: Add -std=c2y, -std=gnu2y, -Wc23-c2y-compat, C2Y _Generic with type operand 2024-06-11 23:00:04 +00:00
tm.texi Add TARGET_MODE_CAN_TRANSFER_BITS 2024-08-01 12:27:04 +02:00
tm.texi.in Add TARGET_MODE_CAN_TRANSFER_BITS 2024-08-01 12:27:04 +02:00
tree-ssa.texi
trouble.texi
ux.texi diagnostics: add highlight-a vs highlight-b in colorization and pp_markup 2024-07-13 10:34:51 -04:00